1b5beae5eSOliver O'Halloran // SPDX-License-Identifier: GPL-2.0
2b5beae5eSOliver O'Halloran
3b5beae5eSOliver O'Halloran #define pr_fmt(fmt) "papr-scm: " fmt
4b5beae5eSOliver O'Halloran
5b5beae5eSOliver O'Halloran #include <linux/of.h>
6b5beae5eSOliver O'Halloran #include <linux/kernel.h>
7b5beae5eSOliver O'Halloran #include <linux/module.h>
8b5beae5eSOliver O'Halloran #include <linux/ioport.h>
9b5beae5eSOliver O'Halloran #include <linux/slab.h>
10b5beae5eSOliver O'Halloran #include <linux/ndctl.h>
11b5beae5eSOliver O'Halloran #include <linux/sched.h>
12b5beae5eSOliver O'Halloran #include <linux/libnvdimm.h>
13b5beae5eSOliver O'Halloran #include <linux/platform_device.h>
140d7fc080SVaibhav Jain #include <linux/delay.h>
15b791abf3SVaibhav Jain #include <linux/seq_buf.h>
1685343a8dSSantosh Sivaraj #include <linux/nd.h>
17b5beae5eSOliver O'Halloran
18b5beae5eSOliver O'Halloran #include <asm/plpar_wrappers.h>
19f517f792SVaibhav Jain #include <asm/papr_pdsm.h>
2085343a8dSSantosh Sivaraj #include <asm/mce.h>
210e8554b5SAndy Shevchenko #include <asm/unaligned.h>
224c08d4bbSKajol Jain #include <linux/perf_event.h>
23b5beae5eSOliver O'Halloran
24b5beae5eSOliver O'Halloran #define BIND_ANY_ADDR (~0ul)
25b5beae5eSOliver O'Halloran
26b5beae5eSOliver O'Halloran #define PAPR_SCM_DIMM_CMD_MASK \
27b5beae5eSOliver O'Halloran ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
28b5beae5eSOliver O'Halloran (1ul << ND_CMD_GET_CONFIG_DATA) | \
29f517f792SVaibhav Jain (1ul << ND_CMD_SET_CONFIG_DATA) | \
30f517f792SVaibhav Jain (1ul << ND_CMD_CALL))
31b5beae5eSOliver O'Halloran
322b461880SMichael Ellerman /* DIMM health bitmap indicators */
33b791abf3SVaibhav Jain /* SCM device is unable to persist memory contents */
34b791abf3SVaibhav Jain #define PAPR_PMEM_UNARMED (1ULL << (63 - 0))
35b791abf3SVaibhav Jain /* SCM device failed to persist memory contents */
36b791abf3SVaibhav Jain #define PAPR_PMEM_SHUTDOWN_DIRTY (1ULL << (63 - 1))
37b791abf3SVaibhav Jain /* SCM device contents are persisted from previous IPL */
38b791abf3SVaibhav Jain #define PAPR_PMEM_SHUTDOWN_CLEAN (1ULL << (63 - 2))
39b791abf3SVaibhav Jain /* SCM device contents are not persisted from previous IPL */
40b791abf3SVaibhav Jain #define PAPR_PMEM_EMPTY (1ULL << (63 - 3))
41b791abf3SVaibhav Jain /* SCM device memory life remaining is critically low */
42b791abf3SVaibhav Jain #define PAPR_PMEM_HEALTH_CRITICAL (1ULL << (63 - 4))
43b791abf3SVaibhav Jain /* SCM device will be garded off next IPL due to failure */
44b791abf3SVaibhav Jain #define PAPR_PMEM_HEALTH_FATAL (1ULL << (63 - 5))
45b791abf3SVaibhav Jain /* SCM contents cannot persist due to current platform health status */
46b791abf3SVaibhav Jain #define PAPR_PMEM_HEALTH_UNHEALTHY (1ULL << (63 - 6))
47b791abf3SVaibhav Jain /* SCM device is unable to persist memory contents in certain conditions */
48b791abf3SVaibhav Jain #define PAPR_PMEM_HEALTH_NON_CRITICAL (1ULL << (63 - 7))
49b791abf3SVaibhav Jain /* SCM device is encrypted */
50b791abf3SVaibhav Jain #define PAPR_PMEM_ENCRYPTED (1ULL << (63 - 8))
51b791abf3SVaibhav Jain /* SCM device has been scrubbed and locked */
52b791abf3SVaibhav Jain #define PAPR_PMEM_SCRUBBED_AND_LOCKED (1ULL << (63 - 9))
53b791abf3SVaibhav Jain
54b791abf3SVaibhav Jain /* Bits status indicators for health bitmap indicating unarmed dimm */
55b791abf3SVaibhav Jain #define PAPR_PMEM_UNARMED_MASK (PAPR_PMEM_UNARMED | \
56b791abf3SVaibhav Jain PAPR_PMEM_HEALTH_UNHEALTHY)
57b791abf3SVaibhav Jain
58b791abf3SVaibhav Jain /* Bits status indicators for health bitmap indicating unflushed dimm */
59b791abf3SVaibhav Jain #define PAPR_PMEM_BAD_SHUTDOWN_MASK (PAPR_PMEM_SHUTDOWN_DIRTY)
60b791abf3SVaibhav Jain
61b791abf3SVaibhav Jain /* Bits status indicators for health bitmap indicating unrestored dimm */
62b791abf3SVaibhav Jain #define PAPR_PMEM_BAD_RESTORE_MASK (PAPR_PMEM_EMPTY)
63b791abf3SVaibhav Jain
64b791abf3SVaibhav Jain /* Bit status indicators for smart event notification */
65b791abf3SVaibhav Jain #define PAPR_PMEM_SMART_EVENT_MASK (PAPR_PMEM_HEALTH_CRITICAL | \
66b791abf3SVaibhav Jain PAPR_PMEM_HEALTH_FATAL | \
67b791abf3SVaibhav Jain PAPR_PMEM_HEALTH_UNHEALTHY)
68b791abf3SVaibhav Jain
692d02bf83SVaibhav Jain #define PAPR_SCM_PERF_STATS_EYECATCHER __stringify(SCMSTATS)
702d02bf83SVaibhav Jain #define PAPR_SCM_PERF_STATS_VERSION 0x1
712d02bf83SVaibhav Jain
722d02bf83SVaibhav Jain /* Struct holding a single performance metric */
732d02bf83SVaibhav Jain struct papr_scm_perf_stat {
742d02bf83SVaibhav Jain u8 stat_id[8];
752d02bf83SVaibhav Jain __be64 stat_val;
762d02bf83SVaibhav Jain } __packed;
772d02bf83SVaibhav Jain
782d02bf83SVaibhav Jain /* Struct exchanged between kernel and PHYP for fetching drc perf stats */
792d02bf83SVaibhav Jain struct papr_scm_perf_stats {
802d02bf83SVaibhav Jain u8 eye_catcher[8];
812d02bf83SVaibhav Jain /* Should be PAPR_SCM_PERF_STATS_VERSION */
822d02bf83SVaibhav Jain __be32 stats_version;
832d02bf83SVaibhav Jain /* Number of stats following */
842d02bf83SVaibhav Jain __be32 num_statistics;
852d02bf83SVaibhav Jain /* zero or more performance matrics */
862d02bf83SVaibhav Jain struct papr_scm_perf_stat scm_statistic[];
872d02bf83SVaibhav Jain } __packed;
882d02bf83SVaibhav Jain
89b791abf3SVaibhav Jain /* private struct associated with each region */
90b5beae5eSOliver O'Halloran struct papr_scm_priv {
91b5beae5eSOliver O'Halloran struct platform_device *pdev;
92b5beae5eSOliver O'Halloran struct device_node *dn;
93b5beae5eSOliver O'Halloran uint32_t drc_index;
94b5beae5eSOliver O'Halloran uint64_t blocks;
95b5beae5eSOliver O'Halloran uint64_t block_size;
96b5beae5eSOliver O'Halloran int metadata_size;
972a0ffbd4SAneesh Kumar K.V bool is_volatile;
9875b7c05eSShivaprasad G Bhat bool hcall_flush_required;
99b5beae5eSOliver O'Halloran
100b5beae5eSOliver O'Halloran uint64_t bound_addr;
101b5beae5eSOliver O'Halloran
102b5beae5eSOliver O'Halloran struct nvdimm_bus_descriptor bus_desc;
103b5beae5eSOliver O'Halloran struct nvdimm_bus *bus;
104b5beae5eSOliver O'Halloran struct nvdimm *nvdimm;
105b5beae5eSOliver O'Halloran struct resource res;
106b5beae5eSOliver O'Halloran struct nd_region *region;
107b5beae5eSOliver O'Halloran struct nd_interleave_set nd_set;
10885343a8dSSantosh Sivaraj struct list_head region_list;
109b791abf3SVaibhav Jain
110b791abf3SVaibhav Jain /* Protect dimm health data from concurrent read/writes */
111b791abf3SVaibhav Jain struct mutex health_mutex;
112b791abf3SVaibhav Jain
113b791abf3SVaibhav Jain /* Last time the health information of the dimm was updated */
114b791abf3SVaibhav Jain unsigned long lasthealth_jiffies;
115b791abf3SVaibhav Jain
116b791abf3SVaibhav Jain /* Health information for the dimm */
117b791abf3SVaibhav Jain u64 health_bitmap;
1182d02bf83SVaibhav Jain
119de21e137SVaibhav Jain /* Holds the last known dirty shutdown counter value */
120de21e137SVaibhav Jain u64 dirty_shutdown_counter;
121de21e137SVaibhav Jain
1222d02bf83SVaibhav Jain /* length of the stat buffer as expected by phyp */
1232d02bf83SVaibhav Jain size_t stat_buffer_len;
124bbbca723SVaibhav Jain
125bbbca723SVaibhav Jain /* The bits which needs to be overridden */
126bbbca723SVaibhav Jain u64 health_bitmap_inject_mask;
127b5beae5eSOliver O'Halloran };
128b5beae5eSOliver O'Halloran
papr_scm_pmem_flush(struct nd_region * nd_region,struct bio * bio __maybe_unused)12975b7c05eSShivaprasad G Bhat static int papr_scm_pmem_flush(struct nd_region *nd_region,
13075b7c05eSShivaprasad G Bhat struct bio *bio __maybe_unused)
13175b7c05eSShivaprasad G Bhat {
13275b7c05eSShivaprasad G Bhat struct papr_scm_priv *p = nd_region_provider_data(nd_region);
13375b7c05eSShivaprasad G Bhat unsigned long ret_buf[PLPAR_HCALL_BUFSIZE], token = 0;
13475b7c05eSShivaprasad G Bhat long rc;
13575b7c05eSShivaprasad G Bhat
13675b7c05eSShivaprasad G Bhat dev_dbg(&p->pdev->dev, "flush drc 0x%x", p->drc_index);
13775b7c05eSShivaprasad G Bhat
13875b7c05eSShivaprasad G Bhat do {
13975b7c05eSShivaprasad G Bhat rc = plpar_hcall(H_SCM_FLUSH, ret_buf, p->drc_index, token);
14075b7c05eSShivaprasad G Bhat token = ret_buf[0];
14175b7c05eSShivaprasad G Bhat
14275b7c05eSShivaprasad G Bhat /* Check if we are stalled for some time */
14375b7c05eSShivaprasad G Bhat if (H_IS_LONG_BUSY(rc)) {
14475b7c05eSShivaprasad G Bhat msleep(get_longbusy_msecs(rc));
14575b7c05eSShivaprasad G Bhat rc = H_BUSY;
14675b7c05eSShivaprasad G Bhat } else if (rc == H_BUSY) {
14775b7c05eSShivaprasad G Bhat cond_resched();
14875b7c05eSShivaprasad G Bhat }
14975b7c05eSShivaprasad G Bhat } while (rc == H_BUSY);
15075b7c05eSShivaprasad G Bhat
15175b7c05eSShivaprasad G Bhat if (rc) {
1527767d9acSMichael Ellerman dev_err(&p->pdev->dev, "flush error: %ld", rc);
15375b7c05eSShivaprasad G Bhat rc = -EIO;
15475b7c05eSShivaprasad G Bhat } else {
15575b7c05eSShivaprasad G Bhat dev_dbg(&p->pdev->dev, "flush drc 0x%x complete", p->drc_index);
15675b7c05eSShivaprasad G Bhat }
15775b7c05eSShivaprasad G Bhat
15875b7c05eSShivaprasad G Bhat return rc;
15975b7c05eSShivaprasad G Bhat }
16075b7c05eSShivaprasad G Bhat
16119a551b2SWei Yongjun static LIST_HEAD(papr_nd_regions);
16219a551b2SWei Yongjun static DEFINE_MUTEX(papr_ndr_lock);
16385343a8dSSantosh Sivaraj
drc_pmem_bind(struct papr_scm_priv * p)164b5beae5eSOliver O'Halloran static int drc_pmem_bind(struct papr_scm_priv *p)
165b5beae5eSOliver O'Halloran {
166b5beae5eSOliver O'Halloran unsigned long ret[PLPAR_HCALL_BUFSIZE];
1675a3840a4SOliver O'Halloran uint64_t saved = 0;
1683a855b7aSVaibhav Jain uint64_t token;
1693a855b7aSVaibhav Jain int64_t rc;
170b5beae5eSOliver O'Halloran
171b5beae5eSOliver O'Halloran /*
172b5beae5eSOliver O'Halloran * When the hypervisor cannot map all the requested memory in a single
173b5beae5eSOliver O'Halloran * hcall it returns H_BUSY and we call again with the token until
174b5beae5eSOliver O'Halloran * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
175b5beae5eSOliver O'Halloran * leave the system in an undefined state, so we wait.
176b5beae5eSOliver O'Halloran */
177b5beae5eSOliver O'Halloran token = 0;
178b5beae5eSOliver O'Halloran
179b5beae5eSOliver O'Halloran do {
180b5beae5eSOliver O'Halloran rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
181b5beae5eSOliver O'Halloran p->blocks, BIND_ANY_ADDR, token);
182409dd7dcSOliver O'Halloran token = ret[0];
1835a3840a4SOliver O'Halloran if (!saved)
1845a3840a4SOliver O'Halloran saved = ret[1];
185b5beae5eSOliver O'Halloran cond_resched();
186b5beae5eSOliver O'Halloran } while (rc == H_BUSY);
187b5beae5eSOliver O'Halloran
188faa6d211SAneesh Kumar K.V if (rc)
1894111cdefSAneesh Kumar K.V return rc;
190b5beae5eSOliver O'Halloran
1915a3840a4SOliver O'Halloran p->bound_addr = saved;
1920eb59382SAneesh Kumar K.V dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
1930eb59382SAneesh Kumar K.V p->drc_index, (unsigned long)saved);
1944111cdefSAneesh Kumar K.V return rc;
195b5beae5eSOliver O'Halloran }
196b5beae5eSOliver O'Halloran
drc_pmem_unbind(struct papr_scm_priv * p)1974111cdefSAneesh Kumar K.V static void drc_pmem_unbind(struct papr_scm_priv *p)
198b5beae5eSOliver O'Halloran {
199b5beae5eSOliver O'Halloran unsigned long ret[PLPAR_HCALL_BUFSIZE];
2000d7fc080SVaibhav Jain uint64_t token = 0;
2010d7fc080SVaibhav Jain int64_t rc;
202b5beae5eSOliver O'Halloran
2034111cdefSAneesh Kumar K.V dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
204b5beae5eSOliver O'Halloran
2050d7fc080SVaibhav Jain /* NB: unbind has the same retry requirements as drc_pmem_bind() */
206b5beae5eSOliver O'Halloran do {
2070d7fc080SVaibhav Jain
2080d7fc080SVaibhav Jain /* Unbind of all SCM resources associated with drcIndex */
2090d7fc080SVaibhav Jain rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
2100d7fc080SVaibhav Jain p->drc_index, token);
211409dd7dcSOliver O'Halloran token = ret[0];
2120d7fc080SVaibhav Jain
2130d7fc080SVaibhav Jain /* Check if we are stalled for some time */
2140d7fc080SVaibhav Jain if (H_IS_LONG_BUSY(rc)) {
2150d7fc080SVaibhav Jain msleep(get_longbusy_msecs(rc));
2160d7fc080SVaibhav Jain rc = H_BUSY;
2170d7fc080SVaibhav Jain } else if (rc == H_BUSY) {
218b5beae5eSOliver O'Halloran cond_resched();
2190d7fc080SVaibhav Jain }
2200d7fc080SVaibhav Jain
221b5beae5eSOliver O'Halloran } while (rc == H_BUSY);
222b5beae5eSOliver O'Halloran
223b5beae5eSOliver O'Halloran if (rc)
224b5beae5eSOliver O'Halloran dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
2250d7fc080SVaibhav Jain else
2264111cdefSAneesh Kumar K.V dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
2270d7fc080SVaibhav Jain p->drc_index);
228b5beae5eSOliver O'Halloran
2294111cdefSAneesh Kumar K.V return;
230b5beae5eSOliver O'Halloran }
231b5beae5eSOliver O'Halloran
drc_pmem_query_n_bind(struct papr_scm_priv * p)232faa6d211SAneesh Kumar K.V static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
233faa6d211SAneesh Kumar K.V {
234faa6d211SAneesh Kumar K.V unsigned long start_addr;
235faa6d211SAneesh Kumar K.V unsigned long end_addr;
236faa6d211SAneesh Kumar K.V unsigned long ret[PLPAR_HCALL_BUFSIZE];
237faa6d211SAneesh Kumar K.V int64_t rc;
238faa6d211SAneesh Kumar K.V
239faa6d211SAneesh Kumar K.V
240faa6d211SAneesh Kumar K.V rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
241faa6d211SAneesh Kumar K.V p->drc_index, 0);
242faa6d211SAneesh Kumar K.V if (rc)
243faa6d211SAneesh Kumar K.V goto err_out;
244faa6d211SAneesh Kumar K.V start_addr = ret[0];
245faa6d211SAneesh Kumar K.V
246faa6d211SAneesh Kumar K.V /* Make sure the full region is bound. */
247faa6d211SAneesh Kumar K.V rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
248faa6d211SAneesh Kumar K.V p->drc_index, p->blocks - 1);
249faa6d211SAneesh Kumar K.V if (rc)
250faa6d211SAneesh Kumar K.V goto err_out;
251faa6d211SAneesh Kumar K.V end_addr = ret[0];
252faa6d211SAneesh Kumar K.V
253faa6d211SAneesh Kumar K.V if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
254faa6d211SAneesh Kumar K.V goto err_out;
255faa6d211SAneesh Kumar K.V
256faa6d211SAneesh Kumar K.V p->bound_addr = start_addr;
2570eb59382SAneesh Kumar K.V dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
258faa6d211SAneesh Kumar K.V return rc;
259faa6d211SAneesh Kumar K.V
260faa6d211SAneesh Kumar K.V err_out:
261faa6d211SAneesh Kumar K.V dev_info(&p->pdev->dev,
262faa6d211SAneesh Kumar K.V "Failed to query, trying an unbind followed by bind");
263faa6d211SAneesh Kumar K.V drc_pmem_unbind(p);
264faa6d211SAneesh Kumar K.V return drc_pmem_bind(p);
265faa6d211SAneesh Kumar K.V }
266faa6d211SAneesh Kumar K.V
267b791abf3SVaibhav Jain /*
2682d02bf83SVaibhav Jain * Query the Dimm performance stats from PHYP and copy them (if returned) to
2692d02bf83SVaibhav Jain * provided struct papr_scm_perf_stats instance 'stats' that can hold atleast
2702d02bf83SVaibhav Jain * (num_stats + header) bytes.
271d2827e5eSKajol Jain * - If buff_stats == NULL the return value is the size in bytes of the buffer
2722d02bf83SVaibhav Jain * needed to hold all supported performance-statistics.
2732d02bf83SVaibhav Jain * - If buff_stats != NULL and num_stats == 0 then we copy all known
2742d02bf83SVaibhav Jain * performance-statistics to 'buff_stat' and expect to be large enough to
2752d02bf83SVaibhav Jain * hold them.
2762d02bf83SVaibhav Jain * - if buff_stats != NULL and num_stats > 0 then copy the requested
2772d02bf83SVaibhav Jain * performance-statistics to buff_stats.
2782d02bf83SVaibhav Jain */
drc_pmem_query_stats(struct papr_scm_priv * p,struct papr_scm_perf_stats * buff_stats,unsigned int num_stats)2792d02bf83SVaibhav Jain static ssize_t drc_pmem_query_stats(struct papr_scm_priv *p,
2802d02bf83SVaibhav Jain struct papr_scm_perf_stats *buff_stats,
2812d02bf83SVaibhav Jain unsigned int num_stats)
2822d02bf83SVaibhav Jain {
2832d02bf83SVaibhav Jain unsigned long ret[PLPAR_HCALL_BUFSIZE];
2842d02bf83SVaibhav Jain size_t size;
2852d02bf83SVaibhav Jain s64 rc;
2862d02bf83SVaibhav Jain
2872d02bf83SVaibhav Jain /* Setup the out buffer */
2882d02bf83SVaibhav Jain if (buff_stats) {
2892d02bf83SVaibhav Jain memcpy(buff_stats->eye_catcher,
2902d02bf83SVaibhav Jain PAPR_SCM_PERF_STATS_EYECATCHER, 8);
2912d02bf83SVaibhav Jain buff_stats->stats_version =
2922d02bf83SVaibhav Jain cpu_to_be32(PAPR_SCM_PERF_STATS_VERSION);
2932d02bf83SVaibhav Jain buff_stats->num_statistics =
2942d02bf83SVaibhav Jain cpu_to_be32(num_stats);
2952d02bf83SVaibhav Jain
2962d02bf83SVaibhav Jain /*
2972d02bf83SVaibhav Jain * Calculate the buffer size based on num-stats provided
2982d02bf83SVaibhav Jain * or use the prefetched max buffer length
2992d02bf83SVaibhav Jain */
3002d02bf83SVaibhav Jain if (num_stats)
3012d02bf83SVaibhav Jain /* Calculate size from the num_stats */
3022d02bf83SVaibhav Jain size = sizeof(struct papr_scm_perf_stats) +
3032d02bf83SVaibhav Jain num_stats * sizeof(struct papr_scm_perf_stat);
3042d02bf83SVaibhav Jain else
3052d02bf83SVaibhav Jain size = p->stat_buffer_len;
3062d02bf83SVaibhav Jain } else {
3072d02bf83SVaibhav Jain /* In case of no out buffer ignore the size */
3082d02bf83SVaibhav Jain size = 0;
3092d02bf83SVaibhav Jain }
3102d02bf83SVaibhav Jain
3112d02bf83SVaibhav Jain /* Do the HCALL asking PHYP for info */
3122d02bf83SVaibhav Jain rc = plpar_hcall(H_SCM_PERFORMANCE_STATS, ret, p->drc_index,
3132d02bf83SVaibhav Jain buff_stats ? virt_to_phys(buff_stats) : 0,
3142d02bf83SVaibhav Jain size);
3152d02bf83SVaibhav Jain
3162d02bf83SVaibhav Jain /* Check if the error was due to an unknown stat-id */
3172d02bf83SVaibhav Jain if (rc == H_PARTIAL) {
3182d02bf83SVaibhav Jain dev_err(&p->pdev->dev,
3192d02bf83SVaibhav Jain "Unknown performance stats, Err:0x%016lX\n", ret[0]);
3202d02bf83SVaibhav Jain return -ENOENT;
321f3f6d184SVaibhav Jain } else if (rc == H_AUTHORITY) {
322f3f6d184SVaibhav Jain dev_info(&p->pdev->dev,
323f3f6d184SVaibhav Jain "Permission denied while accessing performance stats");
324f3f6d184SVaibhav Jain return -EPERM;
325f3f6d184SVaibhav Jain } else if (rc == H_UNSUPPORTED) {
326f3f6d184SVaibhav Jain dev_dbg(&p->pdev->dev, "Performance stats unsupported\n");
327f3f6d184SVaibhav Jain return -EOPNOTSUPP;
3282d02bf83SVaibhav Jain } else if (rc != H_SUCCESS) {
3292d02bf83SVaibhav Jain dev_err(&p->pdev->dev,
3302d02bf83SVaibhav Jain "Failed to query performance stats, Err:%lld\n", rc);
3312d02bf83SVaibhav Jain return -EIO;
3322d02bf83SVaibhav Jain
3332d02bf83SVaibhav Jain } else if (!size) {
3342d02bf83SVaibhav Jain /* Handle case where stat buffer size was requested */
3352d02bf83SVaibhav Jain dev_dbg(&p->pdev->dev,
3362d02bf83SVaibhav Jain "Performance stats size %ld\n", ret[0]);
3372d02bf83SVaibhav Jain return ret[0];
3382d02bf83SVaibhav Jain }
3392d02bf83SVaibhav Jain
3402d02bf83SVaibhav Jain /* Successfully fetched the requested stats from phyp */
3412d02bf83SVaibhav Jain dev_dbg(&p->pdev->dev,
3422d02bf83SVaibhav Jain "Performance stats returned %d stats\n",
3432d02bf83SVaibhav Jain be32_to_cpu(buff_stats->num_statistics));
3442d02bf83SVaibhav Jain return 0;
3452d02bf83SVaibhav Jain }
3462d02bf83SVaibhav Jain
347d0007eb1SKajol Jain #ifdef CONFIG_PERF_EVENTS
348d0007eb1SKajol Jain #define to_nvdimm_pmu(_pmu) container_of(_pmu, struct nvdimm_pmu, pmu)
349d0007eb1SKajol Jain
3509b1ac046SKajol Jain static const char * const nvdimm_events_map[] = {
3519b1ac046SKajol Jain [1] = "CtlResCt",
3529b1ac046SKajol Jain [2] = "CtlResTm",
3539b1ac046SKajol Jain [3] = "PonSecs ",
3549b1ac046SKajol Jain [4] = "MemLife ",
3559b1ac046SKajol Jain [5] = "CritRscU",
3569b1ac046SKajol Jain [6] = "HostLCnt",
3579b1ac046SKajol Jain [7] = "HostSCnt",
3589b1ac046SKajol Jain [8] = "HostSDur",
3599b1ac046SKajol Jain [9] = "HostLDur",
3609b1ac046SKajol Jain [10] = "MedRCnt ",
3619b1ac046SKajol Jain [11] = "MedWCnt ",
3629b1ac046SKajol Jain [12] = "MedRDur ",
3639b1ac046SKajol Jain [13] = "MedWDur ",
3649b1ac046SKajol Jain [14] = "CchRHCnt",
3659b1ac046SKajol Jain [15] = "CchWHCnt",
3669b1ac046SKajol Jain [16] = "FastWCnt",
3679b1ac046SKajol Jain };
3689b1ac046SKajol Jain
papr_scm_pmu_get_value(struct perf_event * event,struct device * dev,u64 * count)3694c08d4bbSKajol Jain static int papr_scm_pmu_get_value(struct perf_event *event, struct device *dev, u64 *count)
3704c08d4bbSKajol Jain {
3714c08d4bbSKajol Jain struct papr_scm_perf_stat *stat;
3724c08d4bbSKajol Jain struct papr_scm_perf_stats *stats;
373d9abe36dSHaowen Bai struct papr_scm_priv *p = dev_get_drvdata(dev);
3744c08d4bbSKajol Jain int rc, size;
3754c08d4bbSKajol Jain
3769b1ac046SKajol Jain /* Invalid eventcode */
3779b1ac046SKajol Jain if (event->attr.config == 0 || event->attr.config >= ARRAY_SIZE(nvdimm_events_map))
3789b1ac046SKajol Jain return -EINVAL;
3799b1ac046SKajol Jain
3804c08d4bbSKajol Jain /* Allocate request buffer enough to hold single performance stat */
3814c08d4bbSKajol Jain size = sizeof(struct papr_scm_perf_stats) +
3824c08d4bbSKajol Jain sizeof(struct papr_scm_perf_stat);
3834c08d4bbSKajol Jain
3849b1ac046SKajol Jain if (!p)
3854c08d4bbSKajol Jain return -EINVAL;
3864c08d4bbSKajol Jain
3874c08d4bbSKajol Jain stats = kzalloc(size, GFP_KERNEL);
3884c08d4bbSKajol Jain if (!stats)
3894c08d4bbSKajol Jain return -ENOMEM;
3904c08d4bbSKajol Jain
3914c08d4bbSKajol Jain stat = &stats->scm_statistic[0];
3924c08d4bbSKajol Jain memcpy(&stat->stat_id,
3939b1ac046SKajol Jain nvdimm_events_map[event->attr.config],
3944c08d4bbSKajol Jain sizeof(stat->stat_id));
3954c08d4bbSKajol Jain stat->stat_val = 0;
3964c08d4bbSKajol Jain
3974c08d4bbSKajol Jain rc = drc_pmem_query_stats(p, stats, 1);
3984c08d4bbSKajol Jain if (rc < 0) {
3994c08d4bbSKajol Jain kfree(stats);
4004c08d4bbSKajol Jain return rc;
4014c08d4bbSKajol Jain }
4024c08d4bbSKajol Jain
4034c08d4bbSKajol Jain *count = be64_to_cpu(stat->stat_val);
4044c08d4bbSKajol Jain kfree(stats);
4054c08d4bbSKajol Jain return 0;
4064c08d4bbSKajol Jain }
4074c08d4bbSKajol Jain
papr_scm_pmu_event_init(struct perf_event * event)4084c08d4bbSKajol Jain static int papr_scm_pmu_event_init(struct perf_event *event)
4094c08d4bbSKajol Jain {
4104c08d4bbSKajol Jain struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
4114c08d4bbSKajol Jain struct papr_scm_priv *p;
4124c08d4bbSKajol Jain
4134c08d4bbSKajol Jain if (!nd_pmu)
4144c08d4bbSKajol Jain return -EINVAL;
4154c08d4bbSKajol Jain
4164c08d4bbSKajol Jain /* test the event attr type for PMU enumeration */
4174c08d4bbSKajol Jain if (event->attr.type != event->pmu->type)
4184c08d4bbSKajol Jain return -ENOENT;
4194c08d4bbSKajol Jain
4204c08d4bbSKajol Jain /* it does not support event sampling mode */
4214c08d4bbSKajol Jain if (is_sampling_event(event))
4224c08d4bbSKajol Jain return -EOPNOTSUPP;
4234c08d4bbSKajol Jain
4244c08d4bbSKajol Jain /* no branch sampling */
4254c08d4bbSKajol Jain if (has_branch_stack(event))
4264c08d4bbSKajol Jain return -EOPNOTSUPP;
4274c08d4bbSKajol Jain
4284c08d4bbSKajol Jain p = (struct papr_scm_priv *)nd_pmu->dev->driver_data;
4294c08d4bbSKajol Jain if (!p)
4304c08d4bbSKajol Jain return -EINVAL;
4314c08d4bbSKajol Jain
4324c08d4bbSKajol Jain /* Invalid eventcode */
4334c08d4bbSKajol Jain if (event->attr.config == 0 || event->attr.config > 16)
4344c08d4bbSKajol Jain return -EINVAL;
4354c08d4bbSKajol Jain
4364c08d4bbSKajol Jain return 0;
4374c08d4bbSKajol Jain }
4384c08d4bbSKajol Jain
papr_scm_pmu_add(struct perf_event * event,int flags)4394c08d4bbSKajol Jain static int papr_scm_pmu_add(struct perf_event *event, int flags)
4404c08d4bbSKajol Jain {
4414c08d4bbSKajol Jain u64 count;
4424c08d4bbSKajol Jain int rc;
4434c08d4bbSKajol Jain struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
4444c08d4bbSKajol Jain
4454c08d4bbSKajol Jain if (!nd_pmu)
4464c08d4bbSKajol Jain return -EINVAL;
4474c08d4bbSKajol Jain
4484c08d4bbSKajol Jain if (flags & PERF_EF_START) {
4494c08d4bbSKajol Jain rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &count);
4504c08d4bbSKajol Jain if (rc)
4514c08d4bbSKajol Jain return rc;
4524c08d4bbSKajol Jain
4534c08d4bbSKajol Jain local64_set(&event->hw.prev_count, count);
4544c08d4bbSKajol Jain }
4554c08d4bbSKajol Jain
4564c08d4bbSKajol Jain return 0;
4574c08d4bbSKajol Jain }
4584c08d4bbSKajol Jain
papr_scm_pmu_read(struct perf_event * event)4594c08d4bbSKajol Jain static void papr_scm_pmu_read(struct perf_event *event)
4604c08d4bbSKajol Jain {
4614c08d4bbSKajol Jain u64 prev, now;
4624c08d4bbSKajol Jain int rc;
4634c08d4bbSKajol Jain struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
4644c08d4bbSKajol Jain
4654c08d4bbSKajol Jain if (!nd_pmu)
4664c08d4bbSKajol Jain return;
4674c08d4bbSKajol Jain
4684c08d4bbSKajol Jain rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &now);
4694c08d4bbSKajol Jain if (rc)
4704c08d4bbSKajol Jain return;
4714c08d4bbSKajol Jain
4724c08d4bbSKajol Jain prev = local64_xchg(&event->hw.prev_count, now);
4734c08d4bbSKajol Jain local64_add(now - prev, &event->count);
4744c08d4bbSKajol Jain }
4754c08d4bbSKajol Jain
papr_scm_pmu_del(struct perf_event * event,int flags)4764c08d4bbSKajol Jain static void papr_scm_pmu_del(struct perf_event *event, int flags)
4774c08d4bbSKajol Jain {
4784c08d4bbSKajol Jain papr_scm_pmu_read(event);
4794c08d4bbSKajol Jain }
4804c08d4bbSKajol Jain
papr_scm_pmu_register(struct papr_scm_priv * p)4814c08d4bbSKajol Jain static void papr_scm_pmu_register(struct papr_scm_priv *p)
4824c08d4bbSKajol Jain {
4834c08d4bbSKajol Jain struct nvdimm_pmu *nd_pmu;
4844c08d4bbSKajol Jain int rc, nodeid;
4854c08d4bbSKajol Jain
4864c08d4bbSKajol Jain nd_pmu = kzalloc(sizeof(*nd_pmu), GFP_KERNEL);
4874c08d4bbSKajol Jain if (!nd_pmu) {
4884c08d4bbSKajol Jain rc = -ENOMEM;
4894c08d4bbSKajol Jain goto pmu_err_print;
4904c08d4bbSKajol Jain }
4914c08d4bbSKajol Jain
4926cf07810SNathan Chancellor if (!p->stat_buffer_len) {
4936cf07810SNathan Chancellor rc = -ENOENT;
4944c08d4bbSKajol Jain goto pmu_check_events_err;
4956cf07810SNathan Chancellor }
4964c08d4bbSKajol Jain
4974c08d4bbSKajol Jain nd_pmu->pmu.task_ctx_nr = perf_invalid_context;
4984c08d4bbSKajol Jain nd_pmu->pmu.name = nvdimm_name(p->nvdimm);
4994c08d4bbSKajol Jain nd_pmu->pmu.event_init = papr_scm_pmu_event_init;
5004c08d4bbSKajol Jain nd_pmu->pmu.read = papr_scm_pmu_read;
5014c08d4bbSKajol Jain nd_pmu->pmu.add = papr_scm_pmu_add;
5024c08d4bbSKajol Jain nd_pmu->pmu.del = papr_scm_pmu_del;
5034c08d4bbSKajol Jain
5044c08d4bbSKajol Jain nd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_INTERRUPT |
5054c08d4bbSKajol Jain PERF_PMU_CAP_NO_EXCLUDE;
5064c08d4bbSKajol Jain
5074c08d4bbSKajol Jain /*updating the cpumask variable */
5084c08d4bbSKajol Jain nodeid = numa_map_to_online_node(dev_to_node(&p->pdev->dev));
5094c08d4bbSKajol Jain nd_pmu->arch_cpumask = *cpumask_of_node(nodeid);
5104c08d4bbSKajol Jain
5114c08d4bbSKajol Jain rc = register_nvdimm_pmu(nd_pmu, p->pdev);
5124c08d4bbSKajol Jain if (rc)
5139b1ac046SKajol Jain goto pmu_check_events_err;
5144c08d4bbSKajol Jain
5154c08d4bbSKajol Jain /*
5164c08d4bbSKajol Jain * Set archdata.priv value to nvdimm_pmu structure, to handle the
5174c08d4bbSKajol Jain * unregistering of pmu device.
5184c08d4bbSKajol Jain */
5194c08d4bbSKajol Jain p->pdev->archdata.priv = nd_pmu;
5204c08d4bbSKajol Jain return;
5214c08d4bbSKajol Jain
5224c08d4bbSKajol Jain pmu_check_events_err:
5234c08d4bbSKajol Jain kfree(nd_pmu);
5244c08d4bbSKajol Jain pmu_err_print:
5254c08d4bbSKajol Jain dev_info(&p->pdev->dev, "nvdimm pmu didn't register rc=%d\n", rc);
5264c08d4bbSKajol Jain }
5274c08d4bbSKajol Jain
528d0007eb1SKajol Jain #else
papr_scm_pmu_register(struct papr_scm_priv * p)529d0007eb1SKajol Jain static void papr_scm_pmu_register(struct papr_scm_priv *p) { }
530d0007eb1SKajol Jain #endif
531d0007eb1SKajol Jain
5322d02bf83SVaibhav Jain /*
533b791abf3SVaibhav Jain * Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
534b791abf3SVaibhav Jain * health information.
535b791abf3SVaibhav Jain */
__drc_pmem_query_health(struct papr_scm_priv * p)536b791abf3SVaibhav Jain static int __drc_pmem_query_health(struct papr_scm_priv *p)
537b791abf3SVaibhav Jain {
538b791abf3SVaibhav Jain unsigned long ret[PLPAR_HCALL_BUFSIZE];
539bbbca723SVaibhav Jain u64 bitmap = 0;
540b791abf3SVaibhav Jain long rc;
541b791abf3SVaibhav Jain
542b791abf3SVaibhav Jain /* issue the hcall */
543b791abf3SVaibhav Jain rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);
544bbbca723SVaibhav Jain if (rc == H_SUCCESS)
545bbbca723SVaibhav Jain bitmap = ret[0] & ret[1];
546bbbca723SVaibhav Jain else if (rc == H_FUNCTION)
547bbbca723SVaibhav Jain dev_info_once(&p->pdev->dev,
548bbbca723SVaibhav Jain "Hcall H_SCM_HEALTH not implemented, assuming empty health bitmap");
549bbbca723SVaibhav Jain else {
550bbbca723SVaibhav Jain
551b791abf3SVaibhav Jain dev_err(&p->pdev->dev,
552b791abf3SVaibhav Jain "Failed to query health information, Err:%ld\n", rc);
553b791abf3SVaibhav Jain return -ENXIO;
554b791abf3SVaibhav Jain }
555b791abf3SVaibhav Jain
556b791abf3SVaibhav Jain p->lasthealth_jiffies = jiffies;
557bbbca723SVaibhav Jain /* Allow injecting specific health bits via inject mask. */
558bbbca723SVaibhav Jain if (p->health_bitmap_inject_mask)
559bbbca723SVaibhav Jain bitmap = (bitmap & ~p->health_bitmap_inject_mask) |
560bbbca723SVaibhav Jain p->health_bitmap_inject_mask;
561bbbca723SVaibhav Jain WRITE_ONCE(p->health_bitmap, bitmap);
562b791abf3SVaibhav Jain dev_dbg(&p->pdev->dev,
563b791abf3SVaibhav Jain "Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",
564b791abf3SVaibhav Jain ret[0], ret[1]);
565b791abf3SVaibhav Jain
566b791abf3SVaibhav Jain return 0;
567b791abf3SVaibhav Jain }
568b791abf3SVaibhav Jain
569b791abf3SVaibhav Jain /* Min interval in seconds for assuming stable dimm health */
570b791abf3SVaibhav Jain #define MIN_HEALTH_QUERY_INTERVAL 60
571b791abf3SVaibhav Jain
572b791abf3SVaibhav Jain /* Query cached health info and if needed call drc_pmem_query_health */
drc_pmem_query_health(struct papr_scm_priv * p)573b791abf3SVaibhav Jain static int drc_pmem_query_health(struct papr_scm_priv *p)
574b791abf3SVaibhav Jain {
575b791abf3SVaibhav Jain unsigned long cache_timeout;
576b791abf3SVaibhav Jain int rc;
577b791abf3SVaibhav Jain
578b791abf3SVaibhav Jain /* Protect concurrent modifications to papr_scm_priv */
579b791abf3SVaibhav Jain rc = mutex_lock_interruptible(&p->health_mutex);
580b791abf3SVaibhav Jain if (rc)
581b791abf3SVaibhav Jain return rc;
582b791abf3SVaibhav Jain
583b791abf3SVaibhav Jain /* Jiffies offset for which the health data is assumed to be same */
584b791abf3SVaibhav Jain cache_timeout = p->lasthealth_jiffies +
585b791abf3SVaibhav Jain msecs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL * 1000);
586b791abf3SVaibhav Jain
587b791abf3SVaibhav Jain /* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */
588b791abf3SVaibhav Jain if (time_after(jiffies, cache_timeout))
589b791abf3SVaibhav Jain rc = __drc_pmem_query_health(p);
590b791abf3SVaibhav Jain else
591b791abf3SVaibhav Jain /* Assume cached health data is valid */
592b791abf3SVaibhav Jain rc = 0;
593b791abf3SVaibhav Jain
594b791abf3SVaibhav Jain mutex_unlock(&p->health_mutex);
595b791abf3SVaibhav Jain return rc;
596b791abf3SVaibhav Jain }
597faa6d211SAneesh Kumar K.V
papr_scm_meta_get(struct papr_scm_priv * p,struct nd_cmd_get_config_data_hdr * hdr)598b5beae5eSOliver O'Halloran static int papr_scm_meta_get(struct papr_scm_priv *p,
599b5beae5eSOliver O'Halloran struct nd_cmd_get_config_data_hdr *hdr)
600b5beae5eSOliver O'Halloran {
601b5beae5eSOliver O'Halloran unsigned long data[PLPAR_HCALL_BUFSIZE];
60253e80bd0SAneesh Kumar K.V unsigned long offset, data_offset;
60353e80bd0SAneesh Kumar K.V int len, read;
604b5beae5eSOliver O'Halloran int64_t ret;
605b5beae5eSOliver O'Halloran
606612ee81bSVaibhav Jain if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
607b5beae5eSOliver O'Halloran return -EINVAL;
608b5beae5eSOliver O'Halloran
60953e80bd0SAneesh Kumar K.V for (len = hdr->in_length; len; len -= read) {
61053e80bd0SAneesh Kumar K.V
61153e80bd0SAneesh Kumar K.V data_offset = hdr->in_length - len;
61253e80bd0SAneesh Kumar K.V offset = hdr->in_offset + data_offset;
61353e80bd0SAneesh Kumar K.V
61453e80bd0SAneesh Kumar K.V if (len >= 8)
61553e80bd0SAneesh Kumar K.V read = 8;
61653e80bd0SAneesh Kumar K.V else if (len >= 4)
61753e80bd0SAneesh Kumar K.V read = 4;
61853e80bd0SAneesh Kumar K.V else if (len >= 2)
61953e80bd0SAneesh Kumar K.V read = 2;
62053e80bd0SAneesh Kumar K.V else
62153e80bd0SAneesh Kumar K.V read = 1;
62253e80bd0SAneesh Kumar K.V
623b5beae5eSOliver O'Halloran ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
62453e80bd0SAneesh Kumar K.V offset, read);
625b5beae5eSOliver O'Halloran
626b5beae5eSOliver O'Halloran if (ret == H_PARAMETER) /* bad DRC index */
627b5beae5eSOliver O'Halloran return -ENODEV;
628b5beae5eSOliver O'Halloran if (ret)
629b5beae5eSOliver O'Halloran return -EINVAL; /* other invalid parameter */
630b5beae5eSOliver O'Halloran
63153e80bd0SAneesh Kumar K.V switch (read) {
63253e80bd0SAneesh Kumar K.V case 8:
63353e80bd0SAneesh Kumar K.V *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
63453e80bd0SAneesh Kumar K.V break;
63553e80bd0SAneesh Kumar K.V case 4:
63653e80bd0SAneesh Kumar K.V *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
63753e80bd0SAneesh Kumar K.V break;
638b5beae5eSOliver O'Halloran
63953e80bd0SAneesh Kumar K.V case 2:
64053e80bd0SAneesh Kumar K.V *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
64153e80bd0SAneesh Kumar K.V break;
64253e80bd0SAneesh Kumar K.V
64353e80bd0SAneesh Kumar K.V case 1:
64453e80bd0SAneesh Kumar K.V *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
64553e80bd0SAneesh Kumar K.V break;
64653e80bd0SAneesh Kumar K.V }
64753e80bd0SAneesh Kumar K.V }
648b5beae5eSOliver O'Halloran return 0;
649b5beae5eSOliver O'Halloran }
650b5beae5eSOliver O'Halloran
papr_scm_meta_set(struct papr_scm_priv * p,struct nd_cmd_set_config_hdr * hdr)651b5beae5eSOliver O'Halloran static int papr_scm_meta_set(struct papr_scm_priv *p,
652b5beae5eSOliver O'Halloran struct nd_cmd_set_config_hdr *hdr)
653b5beae5eSOliver O'Halloran {
65453e80bd0SAneesh Kumar K.V unsigned long offset, data_offset;
65553e80bd0SAneesh Kumar K.V int len, wrote;
65653e80bd0SAneesh Kumar K.V unsigned long data;
65753e80bd0SAneesh Kumar K.V __be64 data_be;
658b5beae5eSOliver O'Halloran int64_t ret;
659b5beae5eSOliver O'Halloran
660612ee81bSVaibhav Jain if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
661b5beae5eSOliver O'Halloran return -EINVAL;
662b5beae5eSOliver O'Halloran
66353e80bd0SAneesh Kumar K.V for (len = hdr->in_length; len; len -= wrote) {
664b5beae5eSOliver O'Halloran
66553e80bd0SAneesh Kumar K.V data_offset = hdr->in_length - len;
66653e80bd0SAneesh Kumar K.V offset = hdr->in_offset + data_offset;
66753e80bd0SAneesh Kumar K.V
66853e80bd0SAneesh Kumar K.V if (len >= 8) {
66953e80bd0SAneesh Kumar K.V data = *(uint64_t *)(hdr->in_buf + data_offset);
67053e80bd0SAneesh Kumar K.V data_be = cpu_to_be64(data);
67153e80bd0SAneesh Kumar K.V wrote = 8;
67253e80bd0SAneesh Kumar K.V } else if (len >= 4) {
67353e80bd0SAneesh Kumar K.V data = *(uint32_t *)(hdr->in_buf + data_offset);
67453e80bd0SAneesh Kumar K.V data &= 0xffffffff;
67553e80bd0SAneesh Kumar K.V data_be = cpu_to_be32(data);
67653e80bd0SAneesh Kumar K.V wrote = 4;
67753e80bd0SAneesh Kumar K.V } else if (len >= 2) {
67853e80bd0SAneesh Kumar K.V data = *(uint16_t *)(hdr->in_buf + data_offset);
67953e80bd0SAneesh Kumar K.V data &= 0xffff;
68053e80bd0SAneesh Kumar K.V data_be = cpu_to_be16(data);
68153e80bd0SAneesh Kumar K.V wrote = 2;
68253e80bd0SAneesh Kumar K.V } else {
68353e80bd0SAneesh Kumar K.V data_be = *(uint8_t *)(hdr->in_buf + data_offset);
68453e80bd0SAneesh Kumar K.V data_be &= 0xff;
68553e80bd0SAneesh Kumar K.V wrote = 1;
68653e80bd0SAneesh Kumar K.V }
68753e80bd0SAneesh Kumar K.V
68853e80bd0SAneesh Kumar K.V ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
68953e80bd0SAneesh Kumar K.V offset, data_be, wrote);
690b5beae5eSOliver O'Halloran if (ret == H_PARAMETER) /* bad DRC index */
691b5beae5eSOliver O'Halloran return -ENODEV;
692b5beae5eSOliver O'Halloran if (ret)
693b5beae5eSOliver O'Halloran return -EINVAL; /* other invalid parameter */
69453e80bd0SAneesh Kumar K.V }
695b5beae5eSOliver O'Halloran
696b5beae5eSOliver O'Halloran return 0;
697b5beae5eSOliver O'Halloran }
698b5beae5eSOliver O'Halloran
699f517f792SVaibhav Jain /*
700f517f792SVaibhav Jain * Do a sanity checks on the inputs args to dimm-control function and return
701f517f792SVaibhav Jain * '0' if valid. Validation of PDSM payloads happens later in
702f517f792SVaibhav Jain * papr_scm_service_pdsm.
703f517f792SVaibhav Jain */
is_cmd_valid(struct nvdimm * nvdimm,unsigned int cmd,void * buf,unsigned int buf_len)704f517f792SVaibhav Jain static int is_cmd_valid(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
705f517f792SVaibhav Jain unsigned int buf_len)
706f517f792SVaibhav Jain {
707f517f792SVaibhav Jain unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK;
708f517f792SVaibhav Jain struct nd_cmd_pkg *nd_cmd;
709f517f792SVaibhav Jain struct papr_scm_priv *p;
710f517f792SVaibhav Jain enum papr_pdsm pdsm;
711f517f792SVaibhav Jain
712f517f792SVaibhav Jain /* Only dimm-specific calls are supported atm */
713f517f792SVaibhav Jain if (!nvdimm)
714f517f792SVaibhav Jain return -EINVAL;
715f517f792SVaibhav Jain
716f517f792SVaibhav Jain /* get the provider data from struct nvdimm */
717f517f792SVaibhav Jain p = nvdimm_provider_data(nvdimm);
718f517f792SVaibhav Jain
719f517f792SVaibhav Jain if (!test_bit(cmd, &cmd_mask)) {
720f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "Unsupported cmd=%u\n", cmd);
721f517f792SVaibhav Jain return -EINVAL;
722f517f792SVaibhav Jain }
723f517f792SVaibhav Jain
724f517f792SVaibhav Jain /* For CMD_CALL verify pdsm request */
725f517f792SVaibhav Jain if (cmd == ND_CMD_CALL) {
726f517f792SVaibhav Jain /* Verify the envelope and envelop size */
727f517f792SVaibhav Jain if (!buf ||
728f517f792SVaibhav Jain buf_len < (sizeof(struct nd_cmd_pkg) + ND_PDSM_HDR_SIZE)) {
729f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "Invalid pkg size=%u\n",
730f517f792SVaibhav Jain buf_len);
731f517f792SVaibhav Jain return -EINVAL;
732f517f792SVaibhav Jain }
733f517f792SVaibhav Jain
734f517f792SVaibhav Jain /* Verify that the nd_cmd_pkg.nd_family is correct */
735f517f792SVaibhav Jain nd_cmd = (struct nd_cmd_pkg *)buf;
736f517f792SVaibhav Jain
737f517f792SVaibhav Jain if (nd_cmd->nd_family != NVDIMM_FAMILY_PAPR) {
738f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "Invalid pkg family=0x%llx\n",
739f517f792SVaibhav Jain nd_cmd->nd_family);
740f517f792SVaibhav Jain return -EINVAL;
741f517f792SVaibhav Jain }
742f517f792SVaibhav Jain
743f517f792SVaibhav Jain pdsm = (enum papr_pdsm)nd_cmd->nd_command;
744f517f792SVaibhav Jain
745f517f792SVaibhav Jain /* Verify if the pdsm command is valid */
746f517f792SVaibhav Jain if (pdsm <= PAPR_PDSM_MIN || pdsm >= PAPR_PDSM_MAX) {
747f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid PDSM\n",
748f517f792SVaibhav Jain pdsm);
749f517f792SVaibhav Jain return -EINVAL;
750f517f792SVaibhav Jain }
751f517f792SVaibhav Jain
752f517f792SVaibhav Jain /* Have enough space to hold returned 'nd_pkg_pdsm' header */
753f517f792SVaibhav Jain if (nd_cmd->nd_size_out < ND_PDSM_HDR_SIZE) {
754f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid payload\n",
755f517f792SVaibhav Jain pdsm);
756f517f792SVaibhav Jain return -EINVAL;
757f517f792SVaibhav Jain }
758f517f792SVaibhav Jain }
759f517f792SVaibhav Jain
760f517f792SVaibhav Jain /* Let the command be further processed */
761f517f792SVaibhav Jain return 0;
762f517f792SVaibhav Jain }
763f517f792SVaibhav Jain
papr_pdsm_fuel_gauge(struct papr_scm_priv * p,union nd_pdsm_payload * payload)764af0870c4SVaibhav Jain static int papr_pdsm_fuel_gauge(struct papr_scm_priv *p,
765af0870c4SVaibhav Jain union nd_pdsm_payload *payload)
766af0870c4SVaibhav Jain {
767af0870c4SVaibhav Jain int rc, size;
768af0870c4SVaibhav Jain u64 statval;
769af0870c4SVaibhav Jain struct papr_scm_perf_stat *stat;
770af0870c4SVaibhav Jain struct papr_scm_perf_stats *stats;
771af0870c4SVaibhav Jain
772af0870c4SVaibhav Jain /* Silently fail if fetching performance metrics isn't supported */
773af0870c4SVaibhav Jain if (!p->stat_buffer_len)
774af0870c4SVaibhav Jain return 0;
775af0870c4SVaibhav Jain
776af0870c4SVaibhav Jain /* Allocate request buffer enough to hold single performance stat */
777af0870c4SVaibhav Jain size = sizeof(struct papr_scm_perf_stats) +
778af0870c4SVaibhav Jain sizeof(struct papr_scm_perf_stat);
779af0870c4SVaibhav Jain
780af0870c4SVaibhav Jain stats = kzalloc(size, GFP_KERNEL);
781af0870c4SVaibhav Jain if (!stats)
782af0870c4SVaibhav Jain return -ENOMEM;
783af0870c4SVaibhav Jain
784af0870c4SVaibhav Jain stat = &stats->scm_statistic[0];
785af0870c4SVaibhav Jain memcpy(&stat->stat_id, "MemLife ", sizeof(stat->stat_id));
786af0870c4SVaibhav Jain stat->stat_val = 0;
787af0870c4SVaibhav Jain
788af0870c4SVaibhav Jain /* Fetch the fuel gauge and populate it in payload */
789af0870c4SVaibhav Jain rc = drc_pmem_query_stats(p, stats, 1);
790af0870c4SVaibhav Jain if (rc < 0) {
791af0870c4SVaibhav Jain dev_dbg(&p->pdev->dev, "Err(%d) fetching fuel gauge\n", rc);
792af0870c4SVaibhav Jain goto free_stats;
793af0870c4SVaibhav Jain }
794af0870c4SVaibhav Jain
795af0870c4SVaibhav Jain statval = be64_to_cpu(stat->stat_val);
796af0870c4SVaibhav Jain dev_dbg(&p->pdev->dev,
797af0870c4SVaibhav Jain "Fetched fuel-gauge %llu", statval);
798af0870c4SVaibhav Jain payload->health.extension_flags |=
799af0870c4SVaibhav Jain PDSM_DIMM_HEALTH_RUN_GAUGE_VALID;
800af0870c4SVaibhav Jain payload->health.dimm_fuel_gauge = statval;
801af0870c4SVaibhav Jain
802af0870c4SVaibhav Jain rc = sizeof(struct nd_papr_pdsm_health);
803af0870c4SVaibhav Jain
804af0870c4SVaibhav Jain free_stats:
805af0870c4SVaibhav Jain kfree(stats);
806af0870c4SVaibhav Jain return rc;
807af0870c4SVaibhav Jain }
808af0870c4SVaibhav Jain
809de21e137SVaibhav Jain /* Add the dirty-shutdown-counter value to the pdsm */
papr_pdsm_dsc(struct papr_scm_priv * p,union nd_pdsm_payload * payload)810de21e137SVaibhav Jain static int papr_pdsm_dsc(struct papr_scm_priv *p,
811de21e137SVaibhav Jain union nd_pdsm_payload *payload)
812de21e137SVaibhav Jain {
813de21e137SVaibhav Jain payload->health.extension_flags |= PDSM_DIMM_DSC_VALID;
814de21e137SVaibhav Jain payload->health.dimm_dsc = p->dirty_shutdown_counter;
815de21e137SVaibhav Jain
816de21e137SVaibhav Jain return sizeof(struct nd_papr_pdsm_health);
817de21e137SVaibhav Jain }
818de21e137SVaibhav Jain
819d35f18b5SVaibhav Jain /* Fetch the DIMM health info and populate it in provided package. */
papr_pdsm_health(struct papr_scm_priv * p,union nd_pdsm_payload * payload)820d35f18b5SVaibhav Jain static int papr_pdsm_health(struct papr_scm_priv *p,
821d35f18b5SVaibhav Jain union nd_pdsm_payload *payload)
822d35f18b5SVaibhav Jain {
823d35f18b5SVaibhav Jain int rc;
824d35f18b5SVaibhav Jain
825d35f18b5SVaibhav Jain /* Ensure dimm health mutex is taken preventing concurrent access */
826d35f18b5SVaibhav Jain rc = mutex_lock_interruptible(&p->health_mutex);
827d35f18b5SVaibhav Jain if (rc)
828d35f18b5SVaibhav Jain goto out;
829d35f18b5SVaibhav Jain
830d35f18b5SVaibhav Jain /* Always fetch upto date dimm health data ignoring cached values */
831d35f18b5SVaibhav Jain rc = __drc_pmem_query_health(p);
832d35f18b5SVaibhav Jain if (rc) {
833d35f18b5SVaibhav Jain mutex_unlock(&p->health_mutex);
834d35f18b5SVaibhav Jain goto out;
835d35f18b5SVaibhav Jain }
836d35f18b5SVaibhav Jain
837d35f18b5SVaibhav Jain /* update health struct with various flags derived from health bitmap */
838d35f18b5SVaibhav Jain payload->health = (struct nd_papr_pdsm_health) {
839d35f18b5SVaibhav Jain .extension_flags = 0,
840d35f18b5SVaibhav Jain .dimm_unarmed = !!(p->health_bitmap & PAPR_PMEM_UNARMED_MASK),
841d35f18b5SVaibhav Jain .dimm_bad_shutdown = !!(p->health_bitmap & PAPR_PMEM_BAD_SHUTDOWN_MASK),
842d35f18b5SVaibhav Jain .dimm_bad_restore = !!(p->health_bitmap & PAPR_PMEM_BAD_RESTORE_MASK),
843d35f18b5SVaibhav Jain .dimm_scrubbed = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
844d35f18b5SVaibhav Jain .dimm_locked = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
845d35f18b5SVaibhav Jain .dimm_encrypted = !!(p->health_bitmap & PAPR_PMEM_ENCRYPTED),
846d35f18b5SVaibhav Jain .dimm_health = PAPR_PDSM_DIMM_HEALTHY,
847d35f18b5SVaibhav Jain };
848d35f18b5SVaibhav Jain
849d35f18b5SVaibhav Jain /* Update field dimm_health based on health_bitmap flags */
850d35f18b5SVaibhav Jain if (p->health_bitmap & PAPR_PMEM_HEALTH_FATAL)
851d35f18b5SVaibhav Jain payload->health.dimm_health = PAPR_PDSM_DIMM_FATAL;
852d35f18b5SVaibhav Jain else if (p->health_bitmap & PAPR_PMEM_HEALTH_CRITICAL)
853d35f18b5SVaibhav Jain payload->health.dimm_health = PAPR_PDSM_DIMM_CRITICAL;
854d35f18b5SVaibhav Jain else if (p->health_bitmap & PAPR_PMEM_HEALTH_UNHEALTHY)
855d35f18b5SVaibhav Jain payload->health.dimm_health = PAPR_PDSM_DIMM_UNHEALTHY;
856d35f18b5SVaibhav Jain
857d35f18b5SVaibhav Jain /* struct populated hence can release the mutex now */
858d35f18b5SVaibhav Jain mutex_unlock(&p->health_mutex);
859af0870c4SVaibhav Jain
860af0870c4SVaibhav Jain /* Populate the fuel gauge meter in the payload */
861af0870c4SVaibhav Jain papr_pdsm_fuel_gauge(p, payload);
862de21e137SVaibhav Jain /* Populate the dirty-shutdown-counter field */
863de21e137SVaibhav Jain papr_pdsm_dsc(p, payload);
864af0870c4SVaibhav Jain
865d35f18b5SVaibhav Jain rc = sizeof(struct nd_papr_pdsm_health);
866d35f18b5SVaibhav Jain
867d35f18b5SVaibhav Jain out:
868d35f18b5SVaibhav Jain return rc;
869d35f18b5SVaibhav Jain }
870d35f18b5SVaibhav Jain
871bbbca723SVaibhav Jain /* Inject a smart error Add the dirty-shutdown-counter value to the pdsm */
papr_pdsm_smart_inject(struct papr_scm_priv * p,union nd_pdsm_payload * payload)872bbbca723SVaibhav Jain static int papr_pdsm_smart_inject(struct papr_scm_priv *p,
873bbbca723SVaibhav Jain union nd_pdsm_payload *payload)
874bbbca723SVaibhav Jain {
875bbbca723SVaibhav Jain int rc;
876bbbca723SVaibhav Jain u32 supported_flags = 0;
877bbbca723SVaibhav Jain u64 inject_mask = 0, clear_mask = 0;
878bbbca723SVaibhav Jain u64 mask;
879bbbca723SVaibhav Jain
880bbbca723SVaibhav Jain /* Check for individual smart error flags and update inject/clear masks */
881bbbca723SVaibhav Jain if (payload->smart_inject.flags & PDSM_SMART_INJECT_HEALTH_FATAL) {
882bbbca723SVaibhav Jain supported_flags |= PDSM_SMART_INJECT_HEALTH_FATAL;
883bbbca723SVaibhav Jain if (payload->smart_inject.fatal_enable)
884bbbca723SVaibhav Jain inject_mask |= PAPR_PMEM_HEALTH_FATAL;
885bbbca723SVaibhav Jain else
886bbbca723SVaibhav Jain clear_mask |= PAPR_PMEM_HEALTH_FATAL;
887bbbca723SVaibhav Jain }
888bbbca723SVaibhav Jain
889bbbca723SVaibhav Jain if (payload->smart_inject.flags & PDSM_SMART_INJECT_BAD_SHUTDOWN) {
890bbbca723SVaibhav Jain supported_flags |= PDSM_SMART_INJECT_BAD_SHUTDOWN;
891bbbca723SVaibhav Jain if (payload->smart_inject.unsafe_shutdown_enable)
892bbbca723SVaibhav Jain inject_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;
893bbbca723SVaibhav Jain else
894bbbca723SVaibhav Jain clear_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;
895bbbca723SVaibhav Jain }
896bbbca723SVaibhav Jain
897bbbca723SVaibhav Jain dev_dbg(&p->pdev->dev, "[Smart-inject] inject_mask=%#llx clear_mask=%#llx\n",
898bbbca723SVaibhav Jain inject_mask, clear_mask);
899bbbca723SVaibhav Jain
900bbbca723SVaibhav Jain /* Prevent concurrent access to dimm health bitmap related members */
901bbbca723SVaibhav Jain rc = mutex_lock_interruptible(&p->health_mutex);
902bbbca723SVaibhav Jain if (rc)
903bbbca723SVaibhav Jain return rc;
904bbbca723SVaibhav Jain
905bbbca723SVaibhav Jain /* Use inject/clear masks to set health_bitmap_inject_mask */
906bbbca723SVaibhav Jain mask = READ_ONCE(p->health_bitmap_inject_mask);
907bbbca723SVaibhav Jain mask = (mask & ~clear_mask) | inject_mask;
908bbbca723SVaibhav Jain WRITE_ONCE(p->health_bitmap_inject_mask, mask);
909bbbca723SVaibhav Jain
910bbbca723SVaibhav Jain /* Invalidate cached health bitmap */
911bbbca723SVaibhav Jain p->lasthealth_jiffies = 0;
912bbbca723SVaibhav Jain
913bbbca723SVaibhav Jain mutex_unlock(&p->health_mutex);
914bbbca723SVaibhav Jain
915bbbca723SVaibhav Jain /* Return the supported flags back to userspace */
916bbbca723SVaibhav Jain payload->smart_inject.flags = supported_flags;
917bbbca723SVaibhav Jain
918bbbca723SVaibhav Jain return sizeof(struct nd_papr_pdsm_health);
919bbbca723SVaibhav Jain }
920bbbca723SVaibhav Jain
921f517f792SVaibhav Jain /*
922f517f792SVaibhav Jain * 'struct pdsm_cmd_desc'
923f517f792SVaibhav Jain * Identifies supported PDSMs' expected length of in/out payloads
924f517f792SVaibhav Jain * and pdsm service function.
925f517f792SVaibhav Jain *
926f517f792SVaibhav Jain * size_in : Size of input payload if any in the PDSM request.
927f517f792SVaibhav Jain * size_out : Size of output payload if any in the PDSM request.
928f517f792SVaibhav Jain * service : Service function for the PDSM request. Return semantics:
929f517f792SVaibhav Jain * rc < 0 : Error servicing PDSM and rc indicates the error.
930f517f792SVaibhav Jain * rc >=0 : Serviced successfully and 'rc' indicate number of
931f517f792SVaibhav Jain * bytes written to payload.
932f517f792SVaibhav Jain */
933f517f792SVaibhav Jain struct pdsm_cmd_desc {
934f517f792SVaibhav Jain u32 size_in;
935f517f792SVaibhav Jain u32 size_out;
936f517f792SVaibhav Jain int (*service)(struct papr_scm_priv *dimm,
937f517f792SVaibhav Jain union nd_pdsm_payload *payload);
938f517f792SVaibhav Jain };
939f517f792SVaibhav Jain
940f517f792SVaibhav Jain /* Holds all supported PDSMs' command descriptors */
941f517f792SVaibhav Jain static const struct pdsm_cmd_desc __pdsm_cmd_descriptors[] = {
942f517f792SVaibhav Jain [PAPR_PDSM_MIN] = {
943f517f792SVaibhav Jain .size_in = 0,
944f517f792SVaibhav Jain .size_out = 0,
945f517f792SVaibhav Jain .service = NULL,
946f517f792SVaibhav Jain },
947f517f792SVaibhav Jain /* New PDSM command descriptors to be added below */
948f517f792SVaibhav Jain
949d35f18b5SVaibhav Jain [PAPR_PDSM_HEALTH] = {
950d35f18b5SVaibhav Jain .size_in = 0,
951d35f18b5SVaibhav Jain .size_out = sizeof(struct nd_papr_pdsm_health),
952d35f18b5SVaibhav Jain .service = papr_pdsm_health,
953d35f18b5SVaibhav Jain },
954bbbca723SVaibhav Jain
955bbbca723SVaibhav Jain [PAPR_PDSM_SMART_INJECT] = {
956bbbca723SVaibhav Jain .size_in = sizeof(struct nd_papr_pdsm_smart_inject),
957bbbca723SVaibhav Jain .size_out = sizeof(struct nd_papr_pdsm_smart_inject),
958bbbca723SVaibhav Jain .service = papr_pdsm_smart_inject,
959bbbca723SVaibhav Jain },
960f517f792SVaibhav Jain /* Empty */
961f517f792SVaibhav Jain [PAPR_PDSM_MAX] = {
962f517f792SVaibhav Jain .size_in = 0,
963f517f792SVaibhav Jain .size_out = 0,
964f517f792SVaibhav Jain .service = NULL,
965f517f792SVaibhav Jain },
966f517f792SVaibhav Jain };
967f517f792SVaibhav Jain
968f517f792SVaibhav Jain /* Given a valid pdsm cmd return its command descriptor else return NULL */
pdsm_cmd_desc(enum papr_pdsm cmd)969f517f792SVaibhav Jain static inline const struct pdsm_cmd_desc *pdsm_cmd_desc(enum papr_pdsm cmd)
970f517f792SVaibhav Jain {
971f517f792SVaibhav Jain if (cmd >= 0 || cmd < ARRAY_SIZE(__pdsm_cmd_descriptors))
972f517f792SVaibhav Jain return &__pdsm_cmd_descriptors[cmd];
973f517f792SVaibhav Jain
974f517f792SVaibhav Jain return NULL;
975f517f792SVaibhav Jain }
976f517f792SVaibhav Jain
977f517f792SVaibhav Jain /*
978f517f792SVaibhav Jain * For a given pdsm request call an appropriate service function.
979f517f792SVaibhav Jain * Returns errors if any while handling the pdsm command package.
980f517f792SVaibhav Jain */
papr_scm_service_pdsm(struct papr_scm_priv * p,struct nd_cmd_pkg * pkg)981f517f792SVaibhav Jain static int papr_scm_service_pdsm(struct papr_scm_priv *p,
982f517f792SVaibhav Jain struct nd_cmd_pkg *pkg)
983f517f792SVaibhav Jain {
984f517f792SVaibhav Jain /* Get the PDSM header and PDSM command */
985f517f792SVaibhav Jain struct nd_pkg_pdsm *pdsm_pkg = (struct nd_pkg_pdsm *)pkg->nd_payload;
986f517f792SVaibhav Jain enum papr_pdsm pdsm = (enum papr_pdsm)pkg->nd_command;
987f517f792SVaibhav Jain const struct pdsm_cmd_desc *pdsc;
988f517f792SVaibhav Jain int rc;
989f517f792SVaibhav Jain
990f517f792SVaibhav Jain /* Fetch corresponding pdsm descriptor for validation and servicing */
991f517f792SVaibhav Jain pdsc = pdsm_cmd_desc(pdsm);
992f517f792SVaibhav Jain
993f517f792SVaibhav Jain /* Validate pdsm descriptor */
994f517f792SVaibhav Jain /* Ensure that reserved fields are 0 */
995f517f792SVaibhav Jain if (pdsm_pkg->reserved[0] || pdsm_pkg->reserved[1]) {
996f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid reserved field\n",
997f517f792SVaibhav Jain pdsm);
998f517f792SVaibhav Jain return -EINVAL;
999f517f792SVaibhav Jain }
1000f517f792SVaibhav Jain
1001f517f792SVaibhav Jain /* If pdsm expects some input, then ensure that the size_in matches */
1002f517f792SVaibhav Jain if (pdsc->size_in &&
1003f517f792SVaibhav Jain pkg->nd_size_in != (pdsc->size_in + ND_PDSM_HDR_SIZE)) {
1004f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_in=%d\n",
1005f517f792SVaibhav Jain pdsm, pkg->nd_size_in);
1006f517f792SVaibhav Jain return -EINVAL;
1007f517f792SVaibhav Jain }
1008f517f792SVaibhav Jain
1009f517f792SVaibhav Jain /* If pdsm wants to return data, then ensure that size_out matches */
1010f517f792SVaibhav Jain if (pdsc->size_out &&
1011f517f792SVaibhav Jain pkg->nd_size_out != (pdsc->size_out + ND_PDSM_HDR_SIZE)) {
1012f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_out=%d\n",
1013f517f792SVaibhav Jain pdsm, pkg->nd_size_out);
1014f517f792SVaibhav Jain return -EINVAL;
1015f517f792SVaibhav Jain }
1016f517f792SVaibhav Jain
1017f517f792SVaibhav Jain /* Service the pdsm */
1018f517f792SVaibhav Jain if (pdsc->service) {
1019f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Servicing..\n", pdsm);
1020f517f792SVaibhav Jain
1021f517f792SVaibhav Jain rc = pdsc->service(p, &pdsm_pkg->payload);
1022f517f792SVaibhav Jain
1023f517f792SVaibhav Jain if (rc < 0) {
1024f517f792SVaibhav Jain /* error encountered while servicing pdsm */
1025f517f792SVaibhav Jain pdsm_pkg->cmd_status = rc;
1026f517f792SVaibhav Jain pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
1027f517f792SVaibhav Jain } else {
1028f517f792SVaibhav Jain /* pdsm serviced and 'rc' bytes written to payload */
1029f517f792SVaibhav Jain pdsm_pkg->cmd_status = 0;
1030f517f792SVaibhav Jain pkg->nd_fw_size = ND_PDSM_HDR_SIZE + rc;
1031f517f792SVaibhav Jain }
1032f517f792SVaibhav Jain } else {
1033f517f792SVaibhav Jain dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Unsupported PDSM request\n",
1034f517f792SVaibhav Jain pdsm);
1035f517f792SVaibhav Jain pdsm_pkg->cmd_status = -ENOENT;
1036f517f792SVaibhav Jain pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
1037f517f792SVaibhav Jain }
1038f517f792SVaibhav Jain
1039f517f792SVaibhav Jain return pdsm_pkg->cmd_status;
1040f517f792SVaibhav Jain }
1041f517f792SVaibhav Jain
papr_scm_ndctl(struct nvdimm_bus_descriptor * nd_desc,struct nvdimm * nvdimm,unsigned int cmd,void * buf,unsigned int buf_len,int * cmd_rc)104272c4ebbaSVaibhav Jain static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,
104372c4ebbaSVaibhav Jain struct nvdimm *nvdimm, unsigned int cmd, void *buf,
104472c4ebbaSVaibhav Jain unsigned int buf_len, int *cmd_rc)
1045b5beae5eSOliver O'Halloran {
1046b5beae5eSOliver O'Halloran struct nd_cmd_get_config_size *get_size_hdr;
1047f517f792SVaibhav Jain struct nd_cmd_pkg *call_pkg = NULL;
1048b5beae5eSOliver O'Halloran struct papr_scm_priv *p;
1049b5f38f09SVaibhav Jain int rc;
1050b5beae5eSOliver O'Halloran
1051f517f792SVaibhav Jain rc = is_cmd_valid(nvdimm, cmd, buf, buf_len);
1052f517f792SVaibhav Jain if (rc) {
1053f517f792SVaibhav Jain pr_debug("Invalid cmd=0x%x. Err=%d\n", cmd, rc);
1054f517f792SVaibhav Jain return rc;
1055f517f792SVaibhav Jain }
1056b5beae5eSOliver O'Halloran
1057b5f38f09SVaibhav Jain /* Use a local variable in case cmd_rc pointer is NULL */
1058b5f38f09SVaibhav Jain if (!cmd_rc)
1059b5f38f09SVaibhav Jain cmd_rc = &rc;
1060b5f38f09SVaibhav Jain
1061b5beae5eSOliver O'Halloran p = nvdimm_provider_data(nvdimm);
1062b5beae5eSOliver O'Halloran
1063b5beae5eSOliver O'Halloran switch (cmd) {
1064b5beae5eSOliver O'Halloran case ND_CMD_GET_CONFIG_SIZE:
1065b5beae5eSOliver O'Halloran get_size_hdr = buf;
1066b5beae5eSOliver O'Halloran
1067b5beae5eSOliver O'Halloran get_size_hdr->status = 0;
106853e80bd0SAneesh Kumar K.V get_size_hdr->max_xfer = 8;
1069b5beae5eSOliver O'Halloran get_size_hdr->config_size = p->metadata_size;
1070b5beae5eSOliver O'Halloran *cmd_rc = 0;
1071b5beae5eSOliver O'Halloran break;
1072b5beae5eSOliver O'Halloran
1073b5beae5eSOliver O'Halloran case ND_CMD_GET_CONFIG_DATA:
1074b5beae5eSOliver O'Halloran *cmd_rc = papr_scm_meta_get(p, buf);
1075b5beae5eSOliver O'Halloran break;
1076b5beae5eSOliver O'Halloran
1077b5beae5eSOliver O'Halloran case ND_CMD_SET_CONFIG_DATA:
1078b5beae5eSOliver O'Halloran *cmd_rc = papr_scm_meta_set(p, buf);
1079b5beae5eSOliver O'Halloran break;
1080b5beae5eSOliver O'Halloran
1081f517f792SVaibhav Jain case ND_CMD_CALL:
1082f517f792SVaibhav Jain call_pkg = (struct nd_cmd_pkg *)buf;
1083f517f792SVaibhav Jain *cmd_rc = papr_scm_service_pdsm(p, call_pkg);
1084f517f792SVaibhav Jain break;
1085f517f792SVaibhav Jain
1086b5beae5eSOliver O'Halloran default:
1087b5f38f09SVaibhav Jain dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd);
1088b5beae5eSOliver O'Halloran return -EINVAL;
1089b5beae5eSOliver O'Halloran }
1090b5beae5eSOliver O'Halloran
1091b5beae5eSOliver O'Halloran dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
1092b5beae5eSOliver O'Halloran
1093b5beae5eSOliver O'Halloran return 0;
1094b5beae5eSOliver O'Halloran }
1095b5beae5eSOliver O'Halloran
health_bitmap_inject_show(struct device * dev,struct device_attribute * attr,char * buf)1096bbbca723SVaibhav Jain static ssize_t health_bitmap_inject_show(struct device *dev,
1097bbbca723SVaibhav Jain struct device_attribute *attr,
1098bbbca723SVaibhav Jain char *buf)
1099bbbca723SVaibhav Jain {
1100bbbca723SVaibhav Jain struct nvdimm *dimm = to_nvdimm(dev);
1101bbbca723SVaibhav Jain struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1102bbbca723SVaibhav Jain
1103bbbca723SVaibhav Jain return sprintf(buf, "%#llx\n",
1104bbbca723SVaibhav Jain READ_ONCE(p->health_bitmap_inject_mask));
1105bbbca723SVaibhav Jain }
1106bbbca723SVaibhav Jain
1107bbbca723SVaibhav Jain static DEVICE_ATTR_ADMIN_RO(health_bitmap_inject);
1108bbbca723SVaibhav Jain
perf_stats_show(struct device * dev,struct device_attribute * attr,char * buf)11092d02bf83SVaibhav Jain static ssize_t perf_stats_show(struct device *dev,
11102d02bf83SVaibhav Jain struct device_attribute *attr, char *buf)
11112d02bf83SVaibhav Jain {
1112ca78ef2fSVaibhav Jain int index;
1113ca78ef2fSVaibhav Jain ssize_t rc;
11142d02bf83SVaibhav Jain struct seq_buf s;
11152d02bf83SVaibhav Jain struct papr_scm_perf_stat *stat;
11162d02bf83SVaibhav Jain struct papr_scm_perf_stats *stats;
11172d02bf83SVaibhav Jain struct nvdimm *dimm = to_nvdimm(dev);
11182d02bf83SVaibhav Jain struct papr_scm_priv *p = nvdimm_provider_data(dimm);
11192d02bf83SVaibhav Jain
11202d02bf83SVaibhav Jain if (!p->stat_buffer_len)
11212d02bf83SVaibhav Jain return -ENOENT;
11222d02bf83SVaibhav Jain
11232d02bf83SVaibhav Jain /* Allocate the buffer for phyp where stats are written */
11242d02bf83SVaibhav Jain stats = kzalloc(p->stat_buffer_len, GFP_KERNEL);
11252d02bf83SVaibhav Jain if (!stats)
11262d02bf83SVaibhav Jain return -ENOMEM;
11272d02bf83SVaibhav Jain
11282d02bf83SVaibhav Jain /* Ask phyp to return all dimm perf stats */
11292d02bf83SVaibhav Jain rc = drc_pmem_query_stats(p, stats, 0);
11302d02bf83SVaibhav Jain if (rc)
11312d02bf83SVaibhav Jain goto free_stats;
11322d02bf83SVaibhav Jain /*
11332d02bf83SVaibhav Jain * Go through the returned output buffer and print stats and
11342d02bf83SVaibhav Jain * values. Since stat_id is essentially a char string of
11352d02bf83SVaibhav Jain * 8 bytes, simply use the string format specifier to print it.
11362d02bf83SVaibhav Jain */
11372d02bf83SVaibhav Jain seq_buf_init(&s, buf, PAGE_SIZE);
11382d02bf83SVaibhav Jain for (index = 0, stat = stats->scm_statistic;
11392d02bf83SVaibhav Jain index < be32_to_cpu(stats->num_statistics);
11402d02bf83SVaibhav Jain ++index, ++stat) {
11412d02bf83SVaibhav Jain seq_buf_printf(&s, "%.8s = 0x%016llX\n",
11422d02bf83SVaibhav Jain stat->stat_id,
11432d02bf83SVaibhav Jain be64_to_cpu(stat->stat_val));
11442d02bf83SVaibhav Jain }
11452d02bf83SVaibhav Jain
11462d02bf83SVaibhav Jain free_stats:
11472d02bf83SVaibhav Jain kfree(stats);
1148ca78ef2fSVaibhav Jain return rc ? rc : (ssize_t)seq_buf_used(&s);
11492d02bf83SVaibhav Jain }
115043663374SWang Wensheng static DEVICE_ATTR_ADMIN_RO(perf_stats);
11512d02bf83SVaibhav Jain
flags_show(struct device * dev,struct device_attribute * attr,char * buf)1152b791abf3SVaibhav Jain static ssize_t flags_show(struct device *dev,
1153b791abf3SVaibhav Jain struct device_attribute *attr, char *buf)
1154b791abf3SVaibhav Jain {
1155b791abf3SVaibhav Jain struct nvdimm *dimm = to_nvdimm(dev);
1156b791abf3SVaibhav Jain struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1157b791abf3SVaibhav Jain struct seq_buf s;
1158b791abf3SVaibhav Jain u64 health;
1159b791abf3SVaibhav Jain int rc;
1160b791abf3SVaibhav Jain
1161b791abf3SVaibhav Jain rc = drc_pmem_query_health(p);
1162b791abf3SVaibhav Jain if (rc)
1163b791abf3SVaibhav Jain return rc;
1164b791abf3SVaibhav Jain
1165b791abf3SVaibhav Jain /* Copy health_bitmap locally, check masks & update out buffer */
1166b791abf3SVaibhav Jain health = READ_ONCE(p->health_bitmap);
1167b791abf3SVaibhav Jain
1168b791abf3SVaibhav Jain seq_buf_init(&s, buf, PAGE_SIZE);
1169b791abf3SVaibhav Jain if (health & PAPR_PMEM_UNARMED_MASK)
1170b791abf3SVaibhav Jain seq_buf_printf(&s, "not_armed ");
1171b791abf3SVaibhav Jain
1172b791abf3SVaibhav Jain if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)
1173b791abf3SVaibhav Jain seq_buf_printf(&s, "flush_fail ");
1174b791abf3SVaibhav Jain
1175b791abf3SVaibhav Jain if (health & PAPR_PMEM_BAD_RESTORE_MASK)
1176b791abf3SVaibhav Jain seq_buf_printf(&s, "restore_fail ");
1177b791abf3SVaibhav Jain
1178b791abf3SVaibhav Jain if (health & PAPR_PMEM_ENCRYPTED)
1179b791abf3SVaibhav Jain seq_buf_printf(&s, "encrypted ");
1180b791abf3SVaibhav Jain
1181b791abf3SVaibhav Jain if (health & PAPR_PMEM_SMART_EVENT_MASK)
1182b791abf3SVaibhav Jain seq_buf_printf(&s, "smart_notify ");
1183b791abf3SVaibhav Jain
1184b791abf3SVaibhav Jain if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)
1185b791abf3SVaibhav Jain seq_buf_printf(&s, "scrubbed locked ");
1186b791abf3SVaibhav Jain
1187b791abf3SVaibhav Jain if (seq_buf_used(&s))
1188b791abf3SVaibhav Jain seq_buf_printf(&s, "\n");
1189b791abf3SVaibhav Jain
1190b791abf3SVaibhav Jain return seq_buf_used(&s);
1191b791abf3SVaibhav Jain }
1192b791abf3SVaibhav Jain DEVICE_ATTR_RO(flags);
1193b791abf3SVaibhav Jain
dirty_shutdown_show(struct device * dev,struct device_attribute * attr,char * buf)1194de21e137SVaibhav Jain static ssize_t dirty_shutdown_show(struct device *dev,
1195de21e137SVaibhav Jain struct device_attribute *attr, char *buf)
1196de21e137SVaibhav Jain {
1197de21e137SVaibhav Jain struct nvdimm *dimm = to_nvdimm(dev);
1198de21e137SVaibhav Jain struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1199de21e137SVaibhav Jain
1200de21e137SVaibhav Jain return sysfs_emit(buf, "%llu\n", p->dirty_shutdown_counter);
1201de21e137SVaibhav Jain }
1202de21e137SVaibhav Jain DEVICE_ATTR_RO(dirty_shutdown);
1203de21e137SVaibhav Jain
papr_nd_attribute_visible(struct kobject * kobj,struct attribute * attr,int n)1204ed78f56eSVaibhav Jain static umode_t papr_nd_attribute_visible(struct kobject *kobj,
1205ed78f56eSVaibhav Jain struct attribute *attr, int n)
1206ed78f56eSVaibhav Jain {
1207ed78f56eSVaibhav Jain struct device *dev = kobj_to_dev(kobj);
1208ed78f56eSVaibhav Jain struct nvdimm *nvdimm = to_nvdimm(dev);
1209ed78f56eSVaibhav Jain struct papr_scm_priv *p = nvdimm_provider_data(nvdimm);
1210ed78f56eSVaibhav Jain
1211ed78f56eSVaibhav Jain /* For if perf-stats not available remove perf_stats sysfs */
1212ed78f56eSVaibhav Jain if (attr == &dev_attr_perf_stats.attr && p->stat_buffer_len == 0)
1213ed78f56eSVaibhav Jain return 0;
1214ed78f56eSVaibhav Jain
1215ed78f56eSVaibhav Jain return attr->mode;
1216ed78f56eSVaibhav Jain }
1217ed78f56eSVaibhav Jain
1218b791abf3SVaibhav Jain /* papr_scm specific dimm attributes */
1219b791abf3SVaibhav Jain static struct attribute *papr_nd_attributes[] = {
1220b791abf3SVaibhav Jain &dev_attr_flags.attr,
12212d02bf83SVaibhav Jain &dev_attr_perf_stats.attr,
1222de21e137SVaibhav Jain &dev_attr_dirty_shutdown.attr,
1223bbbca723SVaibhav Jain &dev_attr_health_bitmap_inject.attr,
1224b791abf3SVaibhav Jain NULL,
1225b791abf3SVaibhav Jain };
1226b791abf3SVaibhav Jain
12276b3a3e12SRohan McLure static const struct attribute_group papr_nd_attribute_group = {
1228b791abf3SVaibhav Jain .name = "papr",
1229ed78f56eSVaibhav Jain .is_visible = papr_nd_attribute_visible,
1230b791abf3SVaibhav Jain .attrs = papr_nd_attributes,
1231b791abf3SVaibhav Jain };
1232b791abf3SVaibhav Jain
1233b791abf3SVaibhav Jain static const struct attribute_group *papr_nd_attr_groups[] = {
1234b791abf3SVaibhav Jain &papr_nd_attribute_group,
1235b791abf3SVaibhav Jain NULL,
1236b791abf3SVaibhav Jain };
1237b791abf3SVaibhav Jain
papr_scm_nvdimm_init(struct papr_scm_priv * p)1238b5beae5eSOliver O'Halloran static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
1239b5beae5eSOliver O'Halloran {
1240b5beae5eSOliver O'Halloran struct device *dev = &p->pdev->dev;
1241b5beae5eSOliver O'Halloran struct nd_mapping_desc mapping;
1242b5beae5eSOliver O'Halloran struct nd_region_desc ndr_desc;
1243b5beae5eSOliver O'Halloran unsigned long dimm_flags;
1244da1115fdSAneesh Kumar K.V int target_nid, online_nid;
1245b5beae5eSOliver O'Halloran
1246b5beae5eSOliver O'Halloran p->bus_desc.ndctl = papr_scm_ndctl;
1247b5beae5eSOliver O'Halloran p->bus_desc.module = THIS_MODULE;
1248b5beae5eSOliver O'Halloran p->bus_desc.of_node = p->pdev->dev.of_node;
1249b5beae5eSOliver O'Halloran p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
1250b5beae5eSOliver O'Halloran
125113135b46SVaibhav Jain /* Set the dimm command family mask to accept PDSMs */
125213135b46SVaibhav Jain set_bit(NVDIMM_FAMILY_PAPR, &p->bus_desc.dimm_family_mask);
125313135b46SVaibhav Jain
1254b5beae5eSOliver O'Halloran if (!p->bus_desc.provider_name)
1255b5beae5eSOliver O'Halloran return -ENOMEM;
1256b5beae5eSOliver O'Halloran
1257b5beae5eSOliver O'Halloran p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
1258b5beae5eSOliver O'Halloran if (!p->bus) {
1259b5beae5eSOliver O'Halloran dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
12605649607aSVaibhav Jain kfree(p->bus_desc.provider_name);
1261b5beae5eSOliver O'Halloran return -ENXIO;
1262b5beae5eSOliver O'Halloran }
1263b5beae5eSOliver O'Halloran
1264b5beae5eSOliver O'Halloran dimm_flags = 0;
1265a0e37452SDan Williams set_bit(NDD_LABELING, &dimm_flags);
1266b5beae5eSOliver O'Halloran
1267adb68c38SVaibhav Jain /*
1268adb68c38SVaibhav Jain * Check if the nvdimm is unarmed. No locking needed as we are still
1269adb68c38SVaibhav Jain * initializing. Ignore error encountered if any.
1270adb68c38SVaibhav Jain */
1271adb68c38SVaibhav Jain __drc_pmem_query_health(p);
1272adb68c38SVaibhav Jain
1273adb68c38SVaibhav Jain if (p->health_bitmap & PAPR_PMEM_UNARMED_MASK)
1274adb68c38SVaibhav Jain set_bit(NDD_UNARMED, &dimm_flags);
1275adb68c38SVaibhav Jain
1276b791abf3SVaibhav Jain p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,
1277b791abf3SVaibhav Jain dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
1278b5beae5eSOliver O'Halloran if (!p->nvdimm) {
1279b5beae5eSOliver O'Halloran dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
1280b5beae5eSOliver O'Halloran goto err;
1281b5beae5eSOliver O'Halloran }
1282b5beae5eSOliver O'Halloran
1283b0d65a8cSOliver O'Halloran if (nvdimm_bus_check_dimm_count(p->bus, 1))
1284b0d65a8cSOliver O'Halloran goto err;
1285b0d65a8cSOliver O'Halloran
1286b5beae5eSOliver O'Halloran /* now add the region */
1287b5beae5eSOliver O'Halloran
1288b5beae5eSOliver O'Halloran memset(&mapping, 0, sizeof(mapping));
1289b5beae5eSOliver O'Halloran mapping.nvdimm = p->nvdimm;
1290b5beae5eSOliver O'Halloran mapping.start = 0;
1291b5beae5eSOliver O'Halloran mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
1292b5beae5eSOliver O'Halloran
1293b5beae5eSOliver O'Halloran memset(&ndr_desc, 0, sizeof(ndr_desc));
1294da1115fdSAneesh Kumar K.V target_nid = dev_to_node(&p->pdev->dev);
1295575e23b6SDan Williams online_nid = numa_map_to_online_node(target_nid);
1296da1115fdSAneesh Kumar K.V ndr_desc.numa_node = online_nid;
1297da1115fdSAneesh Kumar K.V ndr_desc.target_node = target_nid;
1298b5beae5eSOliver O'Halloran ndr_desc.res = &p->res;
1299b5beae5eSOliver O'Halloran ndr_desc.of_node = p->dn;
1300b5beae5eSOliver O'Halloran ndr_desc.provider_data = p;
1301b5beae5eSOliver O'Halloran ndr_desc.mapping = &mapping;
1302b5beae5eSOliver O'Halloran ndr_desc.num_mappings = 1;
1303b5beae5eSOliver O'Halloran ndr_desc.nd_set = &p->nd_set;
1304b5beae5eSOliver O'Halloran
130575b7c05eSShivaprasad G Bhat if (p->hcall_flush_required) {
130675b7c05eSShivaprasad G Bhat set_bit(ND_REGION_ASYNC, &ndr_desc.flags);
130775b7c05eSShivaprasad G Bhat ndr_desc.flush = papr_scm_pmem_flush;
130875b7c05eSShivaprasad G Bhat }
130975b7c05eSShivaprasad G Bhat
13102a0ffbd4SAneesh Kumar K.V if (p->is_volatile)
13112a0ffbd4SAneesh Kumar K.V p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
1312338f6dacSAneesh Kumar K.V else {
1313338f6dacSAneesh Kumar K.V set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
1314b5beae5eSOliver O'Halloran p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
1315338f6dacSAneesh Kumar K.V }
1316b5beae5eSOliver O'Halloran if (!p->region) {
1317b5beae5eSOliver O'Halloran dev_err(dev, "Error registering region %pR from %pOF\n",
1318b5beae5eSOliver O'Halloran ndr_desc.res, p->dn);
1319b5beae5eSOliver O'Halloran goto err;
1320b5beae5eSOliver O'Halloran }
1321da1115fdSAneesh Kumar K.V if (target_nid != online_nid)
1322da1115fdSAneesh Kumar K.V dev_info(dev, "Region registered with target node %d and online node %d",
1323da1115fdSAneesh Kumar K.V target_nid, online_nid);
1324b5beae5eSOliver O'Halloran
132585343a8dSSantosh Sivaraj mutex_lock(&papr_ndr_lock);
132685343a8dSSantosh Sivaraj list_add_tail(&p->region_list, &papr_nd_regions);
132785343a8dSSantosh Sivaraj mutex_unlock(&papr_ndr_lock);
132885343a8dSSantosh Sivaraj
1329b5beae5eSOliver O'Halloran return 0;
1330b5beae5eSOliver O'Halloran
1331b5beae5eSOliver O'Halloran err: nvdimm_bus_unregister(p->bus);
1332b5beae5eSOliver O'Halloran kfree(p->bus_desc.provider_name);
1333b5beae5eSOliver O'Halloran return -ENXIO;
1334b5beae5eSOliver O'Halloran }
1335b5beae5eSOliver O'Halloran
papr_scm_add_badblock(struct nd_region * region,struct nvdimm_bus * bus,u64 phys_addr)133685343a8dSSantosh Sivaraj static void papr_scm_add_badblock(struct nd_region *region,
133785343a8dSSantosh Sivaraj struct nvdimm_bus *bus, u64 phys_addr)
133885343a8dSSantosh Sivaraj {
133985343a8dSSantosh Sivaraj u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES);
134085343a8dSSantosh Sivaraj
134185343a8dSSantosh Sivaraj if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) {
134285343a8dSSantosh Sivaraj pr_err("Bad block registration for 0x%llx failed\n", phys_addr);
134385343a8dSSantosh Sivaraj return;
134485343a8dSSantosh Sivaraj }
134585343a8dSSantosh Sivaraj
134685343a8dSSantosh Sivaraj pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n",
134785343a8dSSantosh Sivaraj aligned_addr, aligned_addr + L1_CACHE_BYTES);
134885343a8dSSantosh Sivaraj
134985343a8dSSantosh Sivaraj nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON);
135085343a8dSSantosh Sivaraj }
135185343a8dSSantosh Sivaraj
handle_mce_ue(struct notifier_block * nb,unsigned long val,void * data)135285343a8dSSantosh Sivaraj static int handle_mce_ue(struct notifier_block *nb, unsigned long val,
135385343a8dSSantosh Sivaraj void *data)
135485343a8dSSantosh Sivaraj {
135585343a8dSSantosh Sivaraj struct machine_check_event *evt = data;
135685343a8dSSantosh Sivaraj struct papr_scm_priv *p;
135785343a8dSSantosh Sivaraj u64 phys_addr;
135885343a8dSSantosh Sivaraj bool found = false;
135985343a8dSSantosh Sivaraj
136085343a8dSSantosh Sivaraj if (evt->error_type != MCE_ERROR_TYPE_UE)
136185343a8dSSantosh Sivaraj return NOTIFY_DONE;
136285343a8dSSantosh Sivaraj
136385343a8dSSantosh Sivaraj if (list_empty(&papr_nd_regions))
136485343a8dSSantosh Sivaraj return NOTIFY_DONE;
136585343a8dSSantosh Sivaraj
136685343a8dSSantosh Sivaraj /*
136785343a8dSSantosh Sivaraj * The physical address obtained here is PAGE_SIZE aligned, so get the
136885343a8dSSantosh Sivaraj * exact address from the effective address
136985343a8dSSantosh Sivaraj */
137085343a8dSSantosh Sivaraj phys_addr = evt->u.ue_error.physical_address +
137185343a8dSSantosh Sivaraj (evt->u.ue_error.effective_address & ~PAGE_MASK);
137285343a8dSSantosh Sivaraj
137385343a8dSSantosh Sivaraj if (!evt->u.ue_error.physical_address_provided ||
137485343a8dSSantosh Sivaraj !is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT)))
137585343a8dSSantosh Sivaraj return NOTIFY_DONE;
137685343a8dSSantosh Sivaraj
137785343a8dSSantosh Sivaraj /* mce notifier is called from a process context, so mutex is safe */
137885343a8dSSantosh Sivaraj mutex_lock(&papr_ndr_lock);
137985343a8dSSantosh Sivaraj list_for_each_entry(p, &papr_nd_regions, region_list) {
138085343a8dSSantosh Sivaraj if (phys_addr >= p->res.start && phys_addr <= p->res.end) {
138185343a8dSSantosh Sivaraj found = true;
138285343a8dSSantosh Sivaraj break;
138385343a8dSSantosh Sivaraj }
138485343a8dSSantosh Sivaraj }
138585343a8dSSantosh Sivaraj
138685343a8dSSantosh Sivaraj if (found)
138785343a8dSSantosh Sivaraj papr_scm_add_badblock(p->region, p->bus, phys_addr);
138885343a8dSSantosh Sivaraj
138985343a8dSSantosh Sivaraj mutex_unlock(&papr_ndr_lock);
139085343a8dSSantosh Sivaraj
139185343a8dSSantosh Sivaraj return found ? NOTIFY_OK : NOTIFY_DONE;
139285343a8dSSantosh Sivaraj }
139385343a8dSSantosh Sivaraj
139485343a8dSSantosh Sivaraj static struct notifier_block mce_ue_nb = {
139585343a8dSSantosh Sivaraj .notifier_call = handle_mce_ue
139685343a8dSSantosh Sivaraj };
139785343a8dSSantosh Sivaraj
papr_scm_probe(struct platform_device * pdev)1398b5beae5eSOliver O'Halloran static int papr_scm_probe(struct platform_device *pdev)
1399b5beae5eSOliver O'Halloran {
1400b5beae5eSOliver O'Halloran struct device_node *dn = pdev->dev.of_node;
1401683ec0e0SOliver O'Halloran u32 drc_index, metadata_size;
1402683ec0e0SOliver O'Halloran u64 blocks, block_size;
1403b5beae5eSOliver O'Halloran struct papr_scm_priv *p;
14040e8554b5SAndy Shevchenko u8 uuid_raw[UUID_SIZE];
140543001c52SOliver O'Halloran const char *uuid_str;
1406ed78f56eSVaibhav Jain ssize_t stat_size;
14070e8554b5SAndy Shevchenko uuid_t uuid;
1408b5beae5eSOliver O'Halloran int rc;
1409b5beae5eSOliver O'Halloran
1410b5beae5eSOliver O'Halloran /* check we have all the required DT properties */
1411b5beae5eSOliver O'Halloran if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
1412b5beae5eSOliver O'Halloran dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
1413b5beae5eSOliver O'Halloran return -ENODEV;
1414b5beae5eSOliver O'Halloran }
1415b5beae5eSOliver O'Halloran
1416683ec0e0SOliver O'Halloran if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
1417683ec0e0SOliver O'Halloran dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
1418683ec0e0SOliver O'Halloran return -ENODEV;
1419683ec0e0SOliver O'Halloran }
1420683ec0e0SOliver O'Halloran
1421683ec0e0SOliver O'Halloran if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
1422683ec0e0SOliver O'Halloran dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
1423b5beae5eSOliver O'Halloran return -ENODEV;
1424b5beae5eSOliver O'Halloran }
1425b5beae5eSOliver O'Halloran
142643001c52SOliver O'Halloran if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
142743001c52SOliver O'Halloran dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
142843001c52SOliver O'Halloran return -ENODEV;
142943001c52SOliver O'Halloran }
143043001c52SOliver O'Halloran
1431*b277fc79SAneesh Kumar K.V /*
1432*b277fc79SAneesh Kumar K.V * open firmware platform device create won't update the NUMA
1433*b277fc79SAneesh Kumar K.V * distance table. For PAPR SCM devices we use numa_map_to_online_node()
1434*b277fc79SAneesh Kumar K.V * to find the nearest online NUMA node and that requires correct
1435*b277fc79SAneesh Kumar K.V * distance table information.
1436*b277fc79SAneesh Kumar K.V */
1437*b277fc79SAneesh Kumar K.V update_numa_distance(dn);
14382a0ffbd4SAneesh Kumar K.V
1439b5beae5eSOliver O'Halloran p = kzalloc(sizeof(*p), GFP_KERNEL);
1440b5beae5eSOliver O'Halloran if (!p)
1441b5beae5eSOliver O'Halloran return -ENOMEM;
1442b5beae5eSOliver O'Halloran
1443b791abf3SVaibhav Jain /* Initialize the dimm mutex */
1444b791abf3SVaibhav Jain mutex_init(&p->health_mutex);
1445b791abf3SVaibhav Jain
1446b5beae5eSOliver O'Halloran /* optional DT properties */
1447b5beae5eSOliver O'Halloran of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
1448b5beae5eSOliver O'Halloran
1449b5beae5eSOliver O'Halloran p->dn = dn;
1450b5beae5eSOliver O'Halloran p->drc_index = drc_index;
1451683ec0e0SOliver O'Halloran p->block_size = block_size;
1452683ec0e0SOliver O'Halloran p->blocks = blocks;
14532a0ffbd4SAneesh Kumar K.V p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
145475b7c05eSShivaprasad G Bhat p->hcall_flush_required = of_property_read_bool(dn, "ibm,hcall-flush-required");
1455b5beae5eSOliver O'Halloran
1456de21e137SVaibhav Jain if (of_property_read_u64(dn, "ibm,persistence-failed-count",
1457de21e137SVaibhav Jain &p->dirty_shutdown_counter))
1458de21e137SVaibhav Jain p->dirty_shutdown_counter = 0;
1459de21e137SVaibhav Jain
146043001c52SOliver O'Halloran /* We just need to ensure that set cookies are unique across */
14610e8554b5SAndy Shevchenko uuid_parse(uuid_str, &uuid);
14620e8554b5SAndy Shevchenko
1463259a948cSAneesh Kumar K.V /*
14640e8554b5SAndy Shevchenko * The cookie1 and cookie2 are not really little endian.
14650e8554b5SAndy Shevchenko * We store a raw buffer representation of the
14660e8554b5SAndy Shevchenko * uuid string so that we can compare this with the label
14670e8554b5SAndy Shevchenko * area cookie irrespective of the endian configuration
14680e8554b5SAndy Shevchenko * with which the kernel is built.
14690e8554b5SAndy Shevchenko *
14700e8554b5SAndy Shevchenko * Historically we stored the cookie in the below format.
14710e8554b5SAndy Shevchenko * for a uuid string 72511b67-0b3b-42fd-8d1d-5be3cae8bcaa
14720e8554b5SAndy Shevchenko * cookie1 was 0xfd423b0b671b5172
14730e8554b5SAndy Shevchenko * cookie2 was 0xaabce8cae35b1d8d
1474259a948cSAneesh Kumar K.V */
14750e8554b5SAndy Shevchenko export_uuid(uuid_raw, &uuid);
14760e8554b5SAndy Shevchenko p->nd_set.cookie1 = get_unaligned_le64(&uuid_raw[0]);
14770e8554b5SAndy Shevchenko p->nd_set.cookie2 = get_unaligned_le64(&uuid_raw[8]);
147843001c52SOliver O'Halloran
1479b5beae5eSOliver O'Halloran /* might be zero */
1480b5beae5eSOliver O'Halloran p->metadata_size = metadata_size;
1481b5beae5eSOliver O'Halloran p->pdev = pdev;
1482b5beae5eSOliver O'Halloran
1483b5beae5eSOliver O'Halloran /* request the hypervisor to bind this region to somewhere in memory */
1484b5beae5eSOliver O'Halloran rc = drc_pmem_bind(p);
14853a855b7aSVaibhav Jain
14863a855b7aSVaibhav Jain /* If phyp says drc memory still bound then force unbound and retry */
1487faa6d211SAneesh Kumar K.V if (rc == H_OVERLAP)
1488faa6d211SAneesh Kumar K.V rc = drc_pmem_query_n_bind(p);
14893a855b7aSVaibhav Jain
14904111cdefSAneesh Kumar K.V if (rc != H_SUCCESS) {
1491faa6d211SAneesh Kumar K.V dev_err(&p->pdev->dev, "bind err: %d\n", rc);
14924111cdefSAneesh Kumar K.V rc = -ENXIO;
1493b5beae5eSOliver O'Halloran goto err;
14944111cdefSAneesh Kumar K.V }
1495b5beae5eSOliver O'Halloran
1496b5beae5eSOliver O'Halloran /* setup the resource for the newly bound range */
1497b5beae5eSOliver O'Halloran p->res.start = p->bound_addr;
149859613526SOliver O'Halloran p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
1499b5beae5eSOliver O'Halloran p->res.name = pdev->name;
1500b5beae5eSOliver O'Halloran p->res.flags = IORESOURCE_MEM;
1501b5beae5eSOliver O'Halloran
1502ed78f56eSVaibhav Jain /* Try retrieving the stat buffer and see if its supported */
1503ed78f56eSVaibhav Jain stat_size = drc_pmem_query_stats(p, NULL, 0);
1504ed78f56eSVaibhav Jain if (stat_size > 0) {
1505ed78f56eSVaibhav Jain p->stat_buffer_len = stat_size;
1506ed78f56eSVaibhav Jain dev_dbg(&p->pdev->dev, "Max perf-stat size %lu-bytes\n",
1507ed78f56eSVaibhav Jain p->stat_buffer_len);
1508ed78f56eSVaibhav Jain }
1509ed78f56eSVaibhav Jain
1510b5beae5eSOliver O'Halloran rc = papr_scm_nvdimm_init(p);
1511b5beae5eSOliver O'Halloran if (rc)
1512b5beae5eSOliver O'Halloran goto err2;
1513b5beae5eSOliver O'Halloran
1514b5beae5eSOliver O'Halloran platform_set_drvdata(pdev, p);
15154c08d4bbSKajol Jain papr_scm_pmu_register(p);
1516b5beae5eSOliver O'Halloran
1517b5beae5eSOliver O'Halloran return 0;
1518b5beae5eSOliver O'Halloran
1519b5beae5eSOliver O'Halloran err2: drc_pmem_unbind(p);
1520b5beae5eSOliver O'Halloran err: kfree(p);
1521b5beae5eSOliver O'Halloran return rc;
1522b5beae5eSOliver O'Halloran }
1523b5beae5eSOliver O'Halloran
papr_scm_remove(struct platform_device * pdev)1524b5beae5eSOliver O'Halloran static int papr_scm_remove(struct platform_device *pdev)
1525b5beae5eSOliver O'Halloran {
1526b5beae5eSOliver O'Halloran struct papr_scm_priv *p = platform_get_drvdata(pdev);
1527b5beae5eSOliver O'Halloran
152885343a8dSSantosh Sivaraj mutex_lock(&papr_ndr_lock);
152985343a8dSSantosh Sivaraj list_del(&p->region_list);
153085343a8dSSantosh Sivaraj mutex_unlock(&papr_ndr_lock);
153185343a8dSSantosh Sivaraj
1532b5beae5eSOliver O'Halloran nvdimm_bus_unregister(p->bus);
1533b5beae5eSOliver O'Halloran drc_pmem_unbind(p);
15344c08d4bbSKajol Jain
15354c08d4bbSKajol Jain if (pdev->archdata.priv)
15364c08d4bbSKajol Jain unregister_nvdimm_pmu(pdev->archdata.priv);
15374c08d4bbSKajol Jain
15384c08d4bbSKajol Jain pdev->archdata.priv = NULL;
15395649607aSVaibhav Jain kfree(p->bus_desc.provider_name);
1540b5beae5eSOliver O'Halloran kfree(p);
1541b5beae5eSOliver O'Halloran
1542b5beae5eSOliver O'Halloran return 0;
1543b5beae5eSOliver O'Halloran }
1544b5beae5eSOliver O'Halloran
1545b5beae5eSOliver O'Halloran static const struct of_device_id papr_scm_match[] = {
1546b5beae5eSOliver O'Halloran { .compatible = "ibm,pmemory" },
15478c26ab72SAneesh Kumar K.V { .compatible = "ibm,pmemory-v2" },
1548b5beae5eSOliver O'Halloran { },
1549b5beae5eSOliver O'Halloran };
1550b5beae5eSOliver O'Halloran
1551b5beae5eSOliver O'Halloran static struct platform_driver papr_scm_driver = {
1552b5beae5eSOliver O'Halloran .probe = papr_scm_probe,
1553b5beae5eSOliver O'Halloran .remove = papr_scm_remove,
1554b5beae5eSOliver O'Halloran .driver = {
1555b5beae5eSOliver O'Halloran .name = "papr_scm",
1556b5beae5eSOliver O'Halloran .of_match_table = papr_scm_match,
1557b5beae5eSOliver O'Halloran },
1558b5beae5eSOliver O'Halloran };
1559b5beae5eSOliver O'Halloran
papr_scm_init(void)156085343a8dSSantosh Sivaraj static int __init papr_scm_init(void)
156185343a8dSSantosh Sivaraj {
156285343a8dSSantosh Sivaraj int ret;
156385343a8dSSantosh Sivaraj
156485343a8dSSantosh Sivaraj ret = platform_driver_register(&papr_scm_driver);
156585343a8dSSantosh Sivaraj if (!ret)
156685343a8dSSantosh Sivaraj mce_register_notifier(&mce_ue_nb);
156785343a8dSSantosh Sivaraj
156885343a8dSSantosh Sivaraj return ret;
156985343a8dSSantosh Sivaraj }
157085343a8dSSantosh Sivaraj module_init(papr_scm_init);
157185343a8dSSantosh Sivaraj
papr_scm_exit(void)157285343a8dSSantosh Sivaraj static void __exit papr_scm_exit(void)
157385343a8dSSantosh Sivaraj {
157485343a8dSSantosh Sivaraj mce_unregister_notifier(&mce_ue_nb);
157585343a8dSSantosh Sivaraj platform_driver_unregister(&papr_scm_driver);
157685343a8dSSantosh Sivaraj }
157785343a8dSSantosh Sivaraj module_exit(papr_scm_exit);
157885343a8dSSantosh Sivaraj
1579b5beae5eSOliver O'Halloran MODULE_DEVICE_TABLE(of, papr_scm_match);
1580b5beae5eSOliver O'Halloran MODULE_LICENSE("GPL");
1581b5beae5eSOliver O'Halloran MODULE_AUTHOR("IBM Corporation");
1582