1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define pr_fmt(fmt)	"papr-scm: " fmt
4 
5 #include <linux/of.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/ioport.h>
9 #include <linux/slab.h>
10 #include <linux/ndctl.h>
11 #include <linux/sched.h>
12 #include <linux/libnvdimm.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/seq_buf.h>
16 #include <linux/nd.h>
17 
18 #include <asm/plpar_wrappers.h>
19 #include <asm/papr_pdsm.h>
20 #include <asm/mce.h>
21 
22 #define BIND_ANY_ADDR (~0ul)
23 
24 #define PAPR_SCM_DIMM_CMD_MASK \
25 	((1ul << ND_CMD_GET_CONFIG_SIZE) | \
26 	 (1ul << ND_CMD_GET_CONFIG_DATA) | \
27 	 (1ul << ND_CMD_SET_CONFIG_DATA) | \
28 	 (1ul << ND_CMD_CALL))
29 
30 /* DIMM health bitmap bitmap indicators */
31 /* SCM device is unable to persist memory contents */
32 #define PAPR_PMEM_UNARMED                   (1ULL << (63 - 0))
33 /* SCM device failed to persist memory contents */
34 #define PAPR_PMEM_SHUTDOWN_DIRTY            (1ULL << (63 - 1))
35 /* SCM device contents are persisted from previous IPL */
36 #define PAPR_PMEM_SHUTDOWN_CLEAN            (1ULL << (63 - 2))
37 /* SCM device contents are not persisted from previous IPL */
38 #define PAPR_PMEM_EMPTY                     (1ULL << (63 - 3))
39 /* SCM device memory life remaining is critically low */
40 #define PAPR_PMEM_HEALTH_CRITICAL           (1ULL << (63 - 4))
41 /* SCM device will be garded off next IPL due to failure */
42 #define PAPR_PMEM_HEALTH_FATAL              (1ULL << (63 - 5))
43 /* SCM contents cannot persist due to current platform health status */
44 #define PAPR_PMEM_HEALTH_UNHEALTHY          (1ULL << (63 - 6))
45 /* SCM device is unable to persist memory contents in certain conditions */
46 #define PAPR_PMEM_HEALTH_NON_CRITICAL       (1ULL << (63 - 7))
47 /* SCM device is encrypted */
48 #define PAPR_PMEM_ENCRYPTED                 (1ULL << (63 - 8))
49 /* SCM device has been scrubbed and locked */
50 #define PAPR_PMEM_SCRUBBED_AND_LOCKED       (1ULL << (63 - 9))
51 
52 /* Bits status indicators for health bitmap indicating unarmed dimm */
53 #define PAPR_PMEM_UNARMED_MASK (PAPR_PMEM_UNARMED |		\
54 				PAPR_PMEM_HEALTH_UNHEALTHY)
55 
56 /* Bits status indicators for health bitmap indicating unflushed dimm */
57 #define PAPR_PMEM_BAD_SHUTDOWN_MASK (PAPR_PMEM_SHUTDOWN_DIRTY)
58 
59 /* Bits status indicators for health bitmap indicating unrestored dimm */
60 #define PAPR_PMEM_BAD_RESTORE_MASK  (PAPR_PMEM_EMPTY)
61 
62 /* Bit status indicators for smart event notification */
63 #define PAPR_PMEM_SMART_EVENT_MASK (PAPR_PMEM_HEALTH_CRITICAL | \
64 				    PAPR_PMEM_HEALTH_FATAL |	\
65 				    PAPR_PMEM_HEALTH_UNHEALTHY)
66 
67 #define PAPR_SCM_PERF_STATS_EYECATCHER __stringify(SCMSTATS)
68 #define PAPR_SCM_PERF_STATS_VERSION 0x1
69 
70 /* Struct holding a single performance metric */
71 struct papr_scm_perf_stat {
72 	u8 stat_id[8];
73 	__be64 stat_val;
74 } __packed;
75 
76 /* Struct exchanged between kernel and PHYP for fetching drc perf stats */
77 struct papr_scm_perf_stats {
78 	u8 eye_catcher[8];
79 	/* Should be PAPR_SCM_PERF_STATS_VERSION */
80 	__be32 stats_version;
81 	/* Number of stats following */
82 	__be32 num_statistics;
83 	/* zero or more performance matrics */
84 	struct papr_scm_perf_stat scm_statistic[];
85 } __packed;
86 
87 /* private struct associated with each region */
88 struct papr_scm_priv {
89 	struct platform_device *pdev;
90 	struct device_node *dn;
91 	uint32_t drc_index;
92 	uint64_t blocks;
93 	uint64_t block_size;
94 	int metadata_size;
95 	bool is_volatile;
96 	bool hcall_flush_required;
97 
98 	uint64_t bound_addr;
99 
100 	struct nvdimm_bus_descriptor bus_desc;
101 	struct nvdimm_bus *bus;
102 	struct nvdimm *nvdimm;
103 	struct resource res;
104 	struct nd_region *region;
105 	struct nd_interleave_set nd_set;
106 	struct list_head region_list;
107 
108 	/* Protect dimm health data from concurrent read/writes */
109 	struct mutex health_mutex;
110 
111 	/* Last time the health information of the dimm was updated */
112 	unsigned long lasthealth_jiffies;
113 
114 	/* Health information for the dimm */
115 	u64 health_bitmap;
116 
117 	/* length of the stat buffer as expected by phyp */
118 	size_t stat_buffer_len;
119 };
120 
121 static int papr_scm_pmem_flush(struct nd_region *nd_region,
122 			       struct bio *bio __maybe_unused)
123 {
124 	struct papr_scm_priv *p = nd_region_provider_data(nd_region);
125 	unsigned long ret_buf[PLPAR_HCALL_BUFSIZE], token = 0;
126 	long rc;
127 
128 	dev_dbg(&p->pdev->dev, "flush drc 0x%x", p->drc_index);
129 
130 	do {
131 		rc = plpar_hcall(H_SCM_FLUSH, ret_buf, p->drc_index, token);
132 		token = ret_buf[0];
133 
134 		/* Check if we are stalled for some time */
135 		if (H_IS_LONG_BUSY(rc)) {
136 			msleep(get_longbusy_msecs(rc));
137 			rc = H_BUSY;
138 		} else if (rc == H_BUSY) {
139 			cond_resched();
140 		}
141 	} while (rc == H_BUSY);
142 
143 	if (rc) {
144 		dev_err(&p->pdev->dev, "flush error: %ld", rc);
145 		rc = -EIO;
146 	} else {
147 		dev_dbg(&p->pdev->dev, "flush drc 0x%x complete", p->drc_index);
148 	}
149 
150 	return rc;
151 }
152 
153 static LIST_HEAD(papr_nd_regions);
154 static DEFINE_MUTEX(papr_ndr_lock);
155 
156 static int drc_pmem_bind(struct papr_scm_priv *p)
157 {
158 	unsigned long ret[PLPAR_HCALL_BUFSIZE];
159 	uint64_t saved = 0;
160 	uint64_t token;
161 	int64_t rc;
162 
163 	/*
164 	 * When the hypervisor cannot map all the requested memory in a single
165 	 * hcall it returns H_BUSY and we call again with the token until
166 	 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
167 	 * leave the system in an undefined state, so we wait.
168 	 */
169 	token = 0;
170 
171 	do {
172 		rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
173 				p->blocks, BIND_ANY_ADDR, token);
174 		token = ret[0];
175 		if (!saved)
176 			saved = ret[1];
177 		cond_resched();
178 	} while (rc == H_BUSY);
179 
180 	if (rc)
181 		return rc;
182 
183 	p->bound_addr = saved;
184 	dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
185 		p->drc_index, (unsigned long)saved);
186 	return rc;
187 }
188 
189 static void drc_pmem_unbind(struct papr_scm_priv *p)
190 {
191 	unsigned long ret[PLPAR_HCALL_BUFSIZE];
192 	uint64_t token = 0;
193 	int64_t rc;
194 
195 	dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
196 
197 	/* NB: unbind has the same retry requirements as drc_pmem_bind() */
198 	do {
199 
200 		/* Unbind of all SCM resources associated with drcIndex */
201 		rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
202 				 p->drc_index, token);
203 		token = ret[0];
204 
205 		/* Check if we are stalled for some time */
206 		if (H_IS_LONG_BUSY(rc)) {
207 			msleep(get_longbusy_msecs(rc));
208 			rc = H_BUSY;
209 		} else if (rc == H_BUSY) {
210 			cond_resched();
211 		}
212 
213 	} while (rc == H_BUSY);
214 
215 	if (rc)
216 		dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
217 	else
218 		dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
219 			p->drc_index);
220 
221 	return;
222 }
223 
224 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
225 {
226 	unsigned long start_addr;
227 	unsigned long end_addr;
228 	unsigned long ret[PLPAR_HCALL_BUFSIZE];
229 	int64_t rc;
230 
231 
232 	rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
233 			 p->drc_index, 0);
234 	if (rc)
235 		goto err_out;
236 	start_addr = ret[0];
237 
238 	/* Make sure the full region is bound. */
239 	rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
240 			 p->drc_index, p->blocks - 1);
241 	if (rc)
242 		goto err_out;
243 	end_addr = ret[0];
244 
245 	if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
246 		goto err_out;
247 
248 	p->bound_addr = start_addr;
249 	dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
250 	return rc;
251 
252 err_out:
253 	dev_info(&p->pdev->dev,
254 		 "Failed to query, trying an unbind followed by bind");
255 	drc_pmem_unbind(p);
256 	return drc_pmem_bind(p);
257 }
258 
259 /*
260  * Query the Dimm performance stats from PHYP and copy them (if returned) to
261  * provided struct papr_scm_perf_stats instance 'stats' that can hold atleast
262  * (num_stats + header) bytes.
263  * - If buff_stats == NULL the return value is the size in byes of the buffer
264  * needed to hold all supported performance-statistics.
265  * - If buff_stats != NULL and num_stats == 0 then we copy all known
266  * performance-statistics to 'buff_stat' and expect to be large enough to
267  * hold them.
268  * - if buff_stats != NULL and num_stats > 0 then copy the requested
269  * performance-statistics to buff_stats.
270  */
271 static ssize_t drc_pmem_query_stats(struct papr_scm_priv *p,
272 				    struct papr_scm_perf_stats *buff_stats,
273 				    unsigned int num_stats)
274 {
275 	unsigned long ret[PLPAR_HCALL_BUFSIZE];
276 	size_t size;
277 	s64 rc;
278 
279 	/* Setup the out buffer */
280 	if (buff_stats) {
281 		memcpy(buff_stats->eye_catcher,
282 		       PAPR_SCM_PERF_STATS_EYECATCHER, 8);
283 		buff_stats->stats_version =
284 			cpu_to_be32(PAPR_SCM_PERF_STATS_VERSION);
285 		buff_stats->num_statistics =
286 			cpu_to_be32(num_stats);
287 
288 		/*
289 		 * Calculate the buffer size based on num-stats provided
290 		 * or use the prefetched max buffer length
291 		 */
292 		if (num_stats)
293 			/* Calculate size from the num_stats */
294 			size = sizeof(struct papr_scm_perf_stats) +
295 				num_stats * sizeof(struct papr_scm_perf_stat);
296 		else
297 			size = p->stat_buffer_len;
298 	} else {
299 		/* In case of no out buffer ignore the size */
300 		size = 0;
301 	}
302 
303 	/* Do the HCALL asking PHYP for info */
304 	rc = plpar_hcall(H_SCM_PERFORMANCE_STATS, ret, p->drc_index,
305 			 buff_stats ? virt_to_phys(buff_stats) : 0,
306 			 size);
307 
308 	/* Check if the error was due to an unknown stat-id */
309 	if (rc == H_PARTIAL) {
310 		dev_err(&p->pdev->dev,
311 			"Unknown performance stats, Err:0x%016lX\n", ret[0]);
312 		return -ENOENT;
313 	} else if (rc != H_SUCCESS) {
314 		dev_err(&p->pdev->dev,
315 			"Failed to query performance stats, Err:%lld\n", rc);
316 		return -EIO;
317 
318 	} else if (!size) {
319 		/* Handle case where stat buffer size was requested */
320 		dev_dbg(&p->pdev->dev,
321 			"Performance stats size %ld\n", ret[0]);
322 		return ret[0];
323 	}
324 
325 	/* Successfully fetched the requested stats from phyp */
326 	dev_dbg(&p->pdev->dev,
327 		"Performance stats returned %d stats\n",
328 		be32_to_cpu(buff_stats->num_statistics));
329 	return 0;
330 }
331 
332 /*
333  * Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
334  * health information.
335  */
336 static int __drc_pmem_query_health(struct papr_scm_priv *p)
337 {
338 	unsigned long ret[PLPAR_HCALL_BUFSIZE];
339 	long rc;
340 
341 	/* issue the hcall */
342 	rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);
343 	if (rc != H_SUCCESS) {
344 		dev_err(&p->pdev->dev,
345 			"Failed to query health information, Err:%ld\n", rc);
346 		return -ENXIO;
347 	}
348 
349 	p->lasthealth_jiffies = jiffies;
350 	p->health_bitmap = ret[0] & ret[1];
351 
352 	dev_dbg(&p->pdev->dev,
353 		"Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",
354 		ret[0], ret[1]);
355 
356 	return 0;
357 }
358 
359 /* Min interval in seconds for assuming stable dimm health */
360 #define MIN_HEALTH_QUERY_INTERVAL 60
361 
362 /* Query cached health info and if needed call drc_pmem_query_health */
363 static int drc_pmem_query_health(struct papr_scm_priv *p)
364 {
365 	unsigned long cache_timeout;
366 	int rc;
367 
368 	/* Protect concurrent modifications to papr_scm_priv */
369 	rc = mutex_lock_interruptible(&p->health_mutex);
370 	if (rc)
371 		return rc;
372 
373 	/* Jiffies offset for which the health data is assumed to be same */
374 	cache_timeout = p->lasthealth_jiffies +
375 		msecs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL * 1000);
376 
377 	/* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */
378 	if (time_after(jiffies, cache_timeout))
379 		rc = __drc_pmem_query_health(p);
380 	else
381 		/* Assume cached health data is valid */
382 		rc = 0;
383 
384 	mutex_unlock(&p->health_mutex);
385 	return rc;
386 }
387 
388 static int papr_scm_meta_get(struct papr_scm_priv *p,
389 			     struct nd_cmd_get_config_data_hdr *hdr)
390 {
391 	unsigned long data[PLPAR_HCALL_BUFSIZE];
392 	unsigned long offset, data_offset;
393 	int len, read;
394 	int64_t ret;
395 
396 	if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
397 		return -EINVAL;
398 
399 	for (len = hdr->in_length; len; len -= read) {
400 
401 		data_offset = hdr->in_length - len;
402 		offset = hdr->in_offset + data_offset;
403 
404 		if (len >= 8)
405 			read = 8;
406 		else if (len >= 4)
407 			read = 4;
408 		else if (len >= 2)
409 			read = 2;
410 		else
411 			read = 1;
412 
413 		ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
414 				  offset, read);
415 
416 		if (ret == H_PARAMETER) /* bad DRC index */
417 			return -ENODEV;
418 		if (ret)
419 			return -EINVAL; /* other invalid parameter */
420 
421 		switch (read) {
422 		case 8:
423 			*(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
424 			break;
425 		case 4:
426 			*(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
427 			break;
428 
429 		case 2:
430 			*(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
431 			break;
432 
433 		case 1:
434 			*(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
435 			break;
436 		}
437 	}
438 	return 0;
439 }
440 
441 static int papr_scm_meta_set(struct papr_scm_priv *p,
442 			     struct nd_cmd_set_config_hdr *hdr)
443 {
444 	unsigned long offset, data_offset;
445 	int len, wrote;
446 	unsigned long data;
447 	__be64 data_be;
448 	int64_t ret;
449 
450 	if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
451 		return -EINVAL;
452 
453 	for (len = hdr->in_length; len; len -= wrote) {
454 
455 		data_offset = hdr->in_length - len;
456 		offset = hdr->in_offset + data_offset;
457 
458 		if (len >= 8) {
459 			data = *(uint64_t *)(hdr->in_buf + data_offset);
460 			data_be = cpu_to_be64(data);
461 			wrote = 8;
462 		} else if (len >= 4) {
463 			data = *(uint32_t *)(hdr->in_buf + data_offset);
464 			data &= 0xffffffff;
465 			data_be = cpu_to_be32(data);
466 			wrote = 4;
467 		} else if (len >= 2) {
468 			data = *(uint16_t *)(hdr->in_buf + data_offset);
469 			data &= 0xffff;
470 			data_be = cpu_to_be16(data);
471 			wrote = 2;
472 		} else {
473 			data_be = *(uint8_t *)(hdr->in_buf + data_offset);
474 			data_be &= 0xff;
475 			wrote = 1;
476 		}
477 
478 		ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
479 					 offset, data_be, wrote);
480 		if (ret == H_PARAMETER) /* bad DRC index */
481 			return -ENODEV;
482 		if (ret)
483 			return -EINVAL; /* other invalid parameter */
484 	}
485 
486 	return 0;
487 }
488 
489 /*
490  * Do a sanity checks on the inputs args to dimm-control function and return
491  * '0' if valid. Validation of PDSM payloads happens later in
492  * papr_scm_service_pdsm.
493  */
494 static int is_cmd_valid(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
495 			unsigned int buf_len)
496 {
497 	unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK;
498 	struct nd_cmd_pkg *nd_cmd;
499 	struct papr_scm_priv *p;
500 	enum papr_pdsm pdsm;
501 
502 	/* Only dimm-specific calls are supported atm */
503 	if (!nvdimm)
504 		return -EINVAL;
505 
506 	/* get the provider data from struct nvdimm */
507 	p = nvdimm_provider_data(nvdimm);
508 
509 	if (!test_bit(cmd, &cmd_mask)) {
510 		dev_dbg(&p->pdev->dev, "Unsupported cmd=%u\n", cmd);
511 		return -EINVAL;
512 	}
513 
514 	/* For CMD_CALL verify pdsm request */
515 	if (cmd == ND_CMD_CALL) {
516 		/* Verify the envelope and envelop size */
517 		if (!buf ||
518 		    buf_len < (sizeof(struct nd_cmd_pkg) + ND_PDSM_HDR_SIZE)) {
519 			dev_dbg(&p->pdev->dev, "Invalid pkg size=%u\n",
520 				buf_len);
521 			return -EINVAL;
522 		}
523 
524 		/* Verify that the nd_cmd_pkg.nd_family is correct */
525 		nd_cmd = (struct nd_cmd_pkg *)buf;
526 
527 		if (nd_cmd->nd_family != NVDIMM_FAMILY_PAPR) {
528 			dev_dbg(&p->pdev->dev, "Invalid pkg family=0x%llx\n",
529 				nd_cmd->nd_family);
530 			return -EINVAL;
531 		}
532 
533 		pdsm = (enum papr_pdsm)nd_cmd->nd_command;
534 
535 		/* Verify if the pdsm command is valid */
536 		if (pdsm <= PAPR_PDSM_MIN || pdsm >= PAPR_PDSM_MAX) {
537 			dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid PDSM\n",
538 				pdsm);
539 			return -EINVAL;
540 		}
541 
542 		/* Have enough space to hold returned 'nd_pkg_pdsm' header */
543 		if (nd_cmd->nd_size_out < ND_PDSM_HDR_SIZE) {
544 			dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid payload\n",
545 				pdsm);
546 			return -EINVAL;
547 		}
548 	}
549 
550 	/* Let the command be further processed */
551 	return 0;
552 }
553 
554 static int papr_pdsm_fuel_gauge(struct papr_scm_priv *p,
555 				union nd_pdsm_payload *payload)
556 {
557 	int rc, size;
558 	u64 statval;
559 	struct papr_scm_perf_stat *stat;
560 	struct papr_scm_perf_stats *stats;
561 
562 	/* Silently fail if fetching performance metrics isn't  supported */
563 	if (!p->stat_buffer_len)
564 		return 0;
565 
566 	/* Allocate request buffer enough to hold single performance stat */
567 	size = sizeof(struct papr_scm_perf_stats) +
568 		sizeof(struct papr_scm_perf_stat);
569 
570 	stats = kzalloc(size, GFP_KERNEL);
571 	if (!stats)
572 		return -ENOMEM;
573 
574 	stat = &stats->scm_statistic[0];
575 	memcpy(&stat->stat_id, "MemLife ", sizeof(stat->stat_id));
576 	stat->stat_val = 0;
577 
578 	/* Fetch the fuel gauge and populate it in payload */
579 	rc = drc_pmem_query_stats(p, stats, 1);
580 	if (rc < 0) {
581 		dev_dbg(&p->pdev->dev, "Err(%d) fetching fuel gauge\n", rc);
582 		goto free_stats;
583 	}
584 
585 	statval = be64_to_cpu(stat->stat_val);
586 	dev_dbg(&p->pdev->dev,
587 		"Fetched fuel-gauge %llu", statval);
588 	payload->health.extension_flags |=
589 		PDSM_DIMM_HEALTH_RUN_GAUGE_VALID;
590 	payload->health.dimm_fuel_gauge = statval;
591 
592 	rc = sizeof(struct nd_papr_pdsm_health);
593 
594 free_stats:
595 	kfree(stats);
596 	return rc;
597 }
598 
599 /* Fetch the DIMM health info and populate it in provided package. */
600 static int papr_pdsm_health(struct papr_scm_priv *p,
601 			    union nd_pdsm_payload *payload)
602 {
603 	int rc;
604 
605 	/* Ensure dimm health mutex is taken preventing concurrent access */
606 	rc = mutex_lock_interruptible(&p->health_mutex);
607 	if (rc)
608 		goto out;
609 
610 	/* Always fetch upto date dimm health data ignoring cached values */
611 	rc = __drc_pmem_query_health(p);
612 	if (rc) {
613 		mutex_unlock(&p->health_mutex);
614 		goto out;
615 	}
616 
617 	/* update health struct with various flags derived from health bitmap */
618 	payload->health = (struct nd_papr_pdsm_health) {
619 		.extension_flags = 0,
620 		.dimm_unarmed = !!(p->health_bitmap & PAPR_PMEM_UNARMED_MASK),
621 		.dimm_bad_shutdown = !!(p->health_bitmap & PAPR_PMEM_BAD_SHUTDOWN_MASK),
622 		.dimm_bad_restore = !!(p->health_bitmap & PAPR_PMEM_BAD_RESTORE_MASK),
623 		.dimm_scrubbed = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
624 		.dimm_locked = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
625 		.dimm_encrypted = !!(p->health_bitmap & PAPR_PMEM_ENCRYPTED),
626 		.dimm_health = PAPR_PDSM_DIMM_HEALTHY,
627 	};
628 
629 	/* Update field dimm_health based on health_bitmap flags */
630 	if (p->health_bitmap & PAPR_PMEM_HEALTH_FATAL)
631 		payload->health.dimm_health = PAPR_PDSM_DIMM_FATAL;
632 	else if (p->health_bitmap & PAPR_PMEM_HEALTH_CRITICAL)
633 		payload->health.dimm_health = PAPR_PDSM_DIMM_CRITICAL;
634 	else if (p->health_bitmap & PAPR_PMEM_HEALTH_UNHEALTHY)
635 		payload->health.dimm_health = PAPR_PDSM_DIMM_UNHEALTHY;
636 
637 	/* struct populated hence can release the mutex now */
638 	mutex_unlock(&p->health_mutex);
639 
640 	/* Populate the fuel gauge meter in the payload */
641 	papr_pdsm_fuel_gauge(p, payload);
642 
643 	rc = sizeof(struct nd_papr_pdsm_health);
644 
645 out:
646 	return rc;
647 }
648 
649 /*
650  * 'struct pdsm_cmd_desc'
651  * Identifies supported PDSMs' expected length of in/out payloads
652  * and pdsm service function.
653  *
654  * size_in	: Size of input payload if any in the PDSM request.
655  * size_out	: Size of output payload if any in the PDSM request.
656  * service	: Service function for the PDSM request. Return semantics:
657  *		  rc < 0 : Error servicing PDSM and rc indicates the error.
658  *		  rc >=0 : Serviced successfully and 'rc' indicate number of
659  *			bytes written to payload.
660  */
661 struct pdsm_cmd_desc {
662 	u32 size_in;
663 	u32 size_out;
664 	int (*service)(struct papr_scm_priv *dimm,
665 		       union nd_pdsm_payload *payload);
666 };
667 
668 /* Holds all supported PDSMs' command descriptors */
669 static const struct pdsm_cmd_desc __pdsm_cmd_descriptors[] = {
670 	[PAPR_PDSM_MIN] = {
671 		.size_in = 0,
672 		.size_out = 0,
673 		.service = NULL,
674 	},
675 	/* New PDSM command descriptors to be added below */
676 
677 	[PAPR_PDSM_HEALTH] = {
678 		.size_in = 0,
679 		.size_out = sizeof(struct nd_papr_pdsm_health),
680 		.service = papr_pdsm_health,
681 	},
682 	/* Empty */
683 	[PAPR_PDSM_MAX] = {
684 		.size_in = 0,
685 		.size_out = 0,
686 		.service = NULL,
687 	},
688 };
689 
690 /* Given a valid pdsm cmd return its command descriptor else return NULL */
691 static inline const struct pdsm_cmd_desc *pdsm_cmd_desc(enum papr_pdsm cmd)
692 {
693 	if (cmd >= 0 || cmd < ARRAY_SIZE(__pdsm_cmd_descriptors))
694 		return &__pdsm_cmd_descriptors[cmd];
695 
696 	return NULL;
697 }
698 
699 /*
700  * For a given pdsm request call an appropriate service function.
701  * Returns errors if any while handling the pdsm command package.
702  */
703 static int papr_scm_service_pdsm(struct papr_scm_priv *p,
704 				 struct nd_cmd_pkg *pkg)
705 {
706 	/* Get the PDSM header and PDSM command */
707 	struct nd_pkg_pdsm *pdsm_pkg = (struct nd_pkg_pdsm *)pkg->nd_payload;
708 	enum papr_pdsm pdsm = (enum papr_pdsm)pkg->nd_command;
709 	const struct pdsm_cmd_desc *pdsc;
710 	int rc;
711 
712 	/* Fetch corresponding pdsm descriptor for validation and servicing */
713 	pdsc = pdsm_cmd_desc(pdsm);
714 
715 	/* Validate pdsm descriptor */
716 	/* Ensure that reserved fields are 0 */
717 	if (pdsm_pkg->reserved[0] || pdsm_pkg->reserved[1]) {
718 		dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid reserved field\n",
719 			pdsm);
720 		return -EINVAL;
721 	}
722 
723 	/* If pdsm expects some input, then ensure that the size_in matches */
724 	if (pdsc->size_in &&
725 	    pkg->nd_size_in != (pdsc->size_in + ND_PDSM_HDR_SIZE)) {
726 		dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_in=%d\n",
727 			pdsm, pkg->nd_size_in);
728 		return -EINVAL;
729 	}
730 
731 	/* If pdsm wants to return data, then ensure that  size_out matches */
732 	if (pdsc->size_out &&
733 	    pkg->nd_size_out != (pdsc->size_out + ND_PDSM_HDR_SIZE)) {
734 		dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_out=%d\n",
735 			pdsm, pkg->nd_size_out);
736 		return -EINVAL;
737 	}
738 
739 	/* Service the pdsm */
740 	if (pdsc->service) {
741 		dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Servicing..\n", pdsm);
742 
743 		rc = pdsc->service(p, &pdsm_pkg->payload);
744 
745 		if (rc < 0) {
746 			/* error encountered while servicing pdsm */
747 			pdsm_pkg->cmd_status = rc;
748 			pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
749 		} else {
750 			/* pdsm serviced and 'rc' bytes written to payload */
751 			pdsm_pkg->cmd_status = 0;
752 			pkg->nd_fw_size = ND_PDSM_HDR_SIZE + rc;
753 		}
754 	} else {
755 		dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Unsupported PDSM request\n",
756 			pdsm);
757 		pdsm_pkg->cmd_status = -ENOENT;
758 		pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
759 	}
760 
761 	return pdsm_pkg->cmd_status;
762 }
763 
764 static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,
765 			  struct nvdimm *nvdimm, unsigned int cmd, void *buf,
766 			  unsigned int buf_len, int *cmd_rc)
767 {
768 	struct nd_cmd_get_config_size *get_size_hdr;
769 	struct nd_cmd_pkg *call_pkg = NULL;
770 	struct papr_scm_priv *p;
771 	int rc;
772 
773 	rc = is_cmd_valid(nvdimm, cmd, buf, buf_len);
774 	if (rc) {
775 		pr_debug("Invalid cmd=0x%x. Err=%d\n", cmd, rc);
776 		return rc;
777 	}
778 
779 	/* Use a local variable in case cmd_rc pointer is NULL */
780 	if (!cmd_rc)
781 		cmd_rc = &rc;
782 
783 	p = nvdimm_provider_data(nvdimm);
784 
785 	switch (cmd) {
786 	case ND_CMD_GET_CONFIG_SIZE:
787 		get_size_hdr = buf;
788 
789 		get_size_hdr->status = 0;
790 		get_size_hdr->max_xfer = 8;
791 		get_size_hdr->config_size = p->metadata_size;
792 		*cmd_rc = 0;
793 		break;
794 
795 	case ND_CMD_GET_CONFIG_DATA:
796 		*cmd_rc = papr_scm_meta_get(p, buf);
797 		break;
798 
799 	case ND_CMD_SET_CONFIG_DATA:
800 		*cmd_rc = papr_scm_meta_set(p, buf);
801 		break;
802 
803 	case ND_CMD_CALL:
804 		call_pkg = (struct nd_cmd_pkg *)buf;
805 		*cmd_rc = papr_scm_service_pdsm(p, call_pkg);
806 		break;
807 
808 	default:
809 		dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd);
810 		return -EINVAL;
811 	}
812 
813 	dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
814 
815 	return 0;
816 }
817 
818 static ssize_t perf_stats_show(struct device *dev,
819 			       struct device_attribute *attr, char *buf)
820 {
821 	int index;
822 	ssize_t rc;
823 	struct seq_buf s;
824 	struct papr_scm_perf_stat *stat;
825 	struct papr_scm_perf_stats *stats;
826 	struct nvdimm *dimm = to_nvdimm(dev);
827 	struct papr_scm_priv *p = nvdimm_provider_data(dimm);
828 
829 	if (!p->stat_buffer_len)
830 		return -ENOENT;
831 
832 	/* Allocate the buffer for phyp where stats are written */
833 	stats = kzalloc(p->stat_buffer_len, GFP_KERNEL);
834 	if (!stats)
835 		return -ENOMEM;
836 
837 	/* Ask phyp to return all dimm perf stats */
838 	rc = drc_pmem_query_stats(p, stats, 0);
839 	if (rc)
840 		goto free_stats;
841 	/*
842 	 * Go through the returned output buffer and print stats and
843 	 * values. Since stat_id is essentially a char string of
844 	 * 8 bytes, simply use the string format specifier to print it.
845 	 */
846 	seq_buf_init(&s, buf, PAGE_SIZE);
847 	for (index = 0, stat = stats->scm_statistic;
848 	     index < be32_to_cpu(stats->num_statistics);
849 	     ++index, ++stat) {
850 		seq_buf_printf(&s, "%.8s = 0x%016llX\n",
851 			       stat->stat_id,
852 			       be64_to_cpu(stat->stat_val));
853 	}
854 
855 free_stats:
856 	kfree(stats);
857 	return rc ? rc : (ssize_t)seq_buf_used(&s);
858 }
859 static DEVICE_ATTR_ADMIN_RO(perf_stats);
860 
861 static ssize_t flags_show(struct device *dev,
862 			  struct device_attribute *attr, char *buf)
863 {
864 	struct nvdimm *dimm = to_nvdimm(dev);
865 	struct papr_scm_priv *p = nvdimm_provider_data(dimm);
866 	struct seq_buf s;
867 	u64 health;
868 	int rc;
869 
870 	rc = drc_pmem_query_health(p);
871 	if (rc)
872 		return rc;
873 
874 	/* Copy health_bitmap locally, check masks & update out buffer */
875 	health = READ_ONCE(p->health_bitmap);
876 
877 	seq_buf_init(&s, buf, PAGE_SIZE);
878 	if (health & PAPR_PMEM_UNARMED_MASK)
879 		seq_buf_printf(&s, "not_armed ");
880 
881 	if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)
882 		seq_buf_printf(&s, "flush_fail ");
883 
884 	if (health & PAPR_PMEM_BAD_RESTORE_MASK)
885 		seq_buf_printf(&s, "restore_fail ");
886 
887 	if (health & PAPR_PMEM_ENCRYPTED)
888 		seq_buf_printf(&s, "encrypted ");
889 
890 	if (health & PAPR_PMEM_SMART_EVENT_MASK)
891 		seq_buf_printf(&s, "smart_notify ");
892 
893 	if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)
894 		seq_buf_printf(&s, "scrubbed locked ");
895 
896 	if (seq_buf_used(&s))
897 		seq_buf_printf(&s, "\n");
898 
899 	return seq_buf_used(&s);
900 }
901 DEVICE_ATTR_RO(flags);
902 
903 /* papr_scm specific dimm attributes */
904 static struct attribute *papr_nd_attributes[] = {
905 	&dev_attr_flags.attr,
906 	&dev_attr_perf_stats.attr,
907 	NULL,
908 };
909 
910 static struct attribute_group papr_nd_attribute_group = {
911 	.name = "papr",
912 	.attrs = papr_nd_attributes,
913 };
914 
915 static const struct attribute_group *papr_nd_attr_groups[] = {
916 	&papr_nd_attribute_group,
917 	NULL,
918 };
919 
920 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
921 {
922 	struct device *dev = &p->pdev->dev;
923 	struct nd_mapping_desc mapping;
924 	struct nd_region_desc ndr_desc;
925 	unsigned long dimm_flags;
926 	int target_nid, online_nid;
927 	ssize_t stat_size;
928 
929 	p->bus_desc.ndctl = papr_scm_ndctl;
930 	p->bus_desc.module = THIS_MODULE;
931 	p->bus_desc.of_node = p->pdev->dev.of_node;
932 	p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
933 
934 	/* Set the dimm command family mask to accept PDSMs */
935 	set_bit(NVDIMM_FAMILY_PAPR, &p->bus_desc.dimm_family_mask);
936 
937 	if (!p->bus_desc.provider_name)
938 		return -ENOMEM;
939 
940 	p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
941 	if (!p->bus) {
942 		dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
943 		kfree(p->bus_desc.provider_name);
944 		return -ENXIO;
945 	}
946 
947 	dimm_flags = 0;
948 	set_bit(NDD_LABELING, &dimm_flags);
949 
950 	/*
951 	 * Check if the nvdimm is unarmed. No locking needed as we are still
952 	 * initializing. Ignore error encountered if any.
953 	 */
954 	__drc_pmem_query_health(p);
955 
956 	if (p->health_bitmap & PAPR_PMEM_UNARMED_MASK)
957 		set_bit(NDD_UNARMED, &dimm_flags);
958 
959 	p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,
960 				  dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
961 	if (!p->nvdimm) {
962 		dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
963 		goto err;
964 	}
965 
966 	if (nvdimm_bus_check_dimm_count(p->bus, 1))
967 		goto err;
968 
969 	/* now add the region */
970 
971 	memset(&mapping, 0, sizeof(mapping));
972 	mapping.nvdimm = p->nvdimm;
973 	mapping.start = 0;
974 	mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
975 
976 	memset(&ndr_desc, 0, sizeof(ndr_desc));
977 	target_nid = dev_to_node(&p->pdev->dev);
978 	online_nid = numa_map_to_online_node(target_nid);
979 	ndr_desc.numa_node = online_nid;
980 	ndr_desc.target_node = target_nid;
981 	ndr_desc.res = &p->res;
982 	ndr_desc.of_node = p->dn;
983 	ndr_desc.provider_data = p;
984 	ndr_desc.mapping = &mapping;
985 	ndr_desc.num_mappings = 1;
986 	ndr_desc.nd_set = &p->nd_set;
987 
988 	if (p->hcall_flush_required) {
989 		set_bit(ND_REGION_ASYNC, &ndr_desc.flags);
990 		ndr_desc.flush = papr_scm_pmem_flush;
991 	}
992 
993 	if (p->is_volatile)
994 		p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
995 	else {
996 		set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
997 		p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
998 	}
999 	if (!p->region) {
1000 		dev_err(dev, "Error registering region %pR from %pOF\n",
1001 				ndr_desc.res, p->dn);
1002 		goto err;
1003 	}
1004 	if (target_nid != online_nid)
1005 		dev_info(dev, "Region registered with target node %d and online node %d",
1006 			 target_nid, online_nid);
1007 
1008 	mutex_lock(&papr_ndr_lock);
1009 	list_add_tail(&p->region_list, &papr_nd_regions);
1010 	mutex_unlock(&papr_ndr_lock);
1011 
1012 	/* Try retriving the stat buffer and see if its supported */
1013 	stat_size = drc_pmem_query_stats(p, NULL, 0);
1014 	if (stat_size > 0) {
1015 		p->stat_buffer_len = stat_size;
1016 		dev_dbg(&p->pdev->dev, "Max perf-stat size %lu-bytes\n",
1017 			p->stat_buffer_len);
1018 	} else {
1019 		dev_info(&p->pdev->dev, "Dimm performance stats unavailable\n");
1020 	}
1021 
1022 	return 0;
1023 
1024 err:	nvdimm_bus_unregister(p->bus);
1025 	kfree(p->bus_desc.provider_name);
1026 	return -ENXIO;
1027 }
1028 
1029 static void papr_scm_add_badblock(struct nd_region *region,
1030 				  struct nvdimm_bus *bus, u64 phys_addr)
1031 {
1032 	u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES);
1033 
1034 	if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) {
1035 		pr_err("Bad block registration for 0x%llx failed\n", phys_addr);
1036 		return;
1037 	}
1038 
1039 	pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n",
1040 		 aligned_addr, aligned_addr + L1_CACHE_BYTES);
1041 
1042 	nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON);
1043 }
1044 
1045 static int handle_mce_ue(struct notifier_block *nb, unsigned long val,
1046 			 void *data)
1047 {
1048 	struct machine_check_event *evt = data;
1049 	struct papr_scm_priv *p;
1050 	u64 phys_addr;
1051 	bool found = false;
1052 
1053 	if (evt->error_type != MCE_ERROR_TYPE_UE)
1054 		return NOTIFY_DONE;
1055 
1056 	if (list_empty(&papr_nd_regions))
1057 		return NOTIFY_DONE;
1058 
1059 	/*
1060 	 * The physical address obtained here is PAGE_SIZE aligned, so get the
1061 	 * exact address from the effective address
1062 	 */
1063 	phys_addr = evt->u.ue_error.physical_address +
1064 			(evt->u.ue_error.effective_address & ~PAGE_MASK);
1065 
1066 	if (!evt->u.ue_error.physical_address_provided ||
1067 	    !is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT)))
1068 		return NOTIFY_DONE;
1069 
1070 	/* mce notifier is called from a process context, so mutex is safe */
1071 	mutex_lock(&papr_ndr_lock);
1072 	list_for_each_entry(p, &papr_nd_regions, region_list) {
1073 		if (phys_addr >= p->res.start && phys_addr <= p->res.end) {
1074 			found = true;
1075 			break;
1076 		}
1077 	}
1078 
1079 	if (found)
1080 		papr_scm_add_badblock(p->region, p->bus, phys_addr);
1081 
1082 	mutex_unlock(&papr_ndr_lock);
1083 
1084 	return found ? NOTIFY_OK : NOTIFY_DONE;
1085 }
1086 
1087 static struct notifier_block mce_ue_nb = {
1088 	.notifier_call = handle_mce_ue
1089 };
1090 
1091 static int papr_scm_probe(struct platform_device *pdev)
1092 {
1093 	struct device_node *dn = pdev->dev.of_node;
1094 	u32 drc_index, metadata_size;
1095 	u64 blocks, block_size;
1096 	struct papr_scm_priv *p;
1097 	const char *uuid_str;
1098 	u64 uuid[2];
1099 	int rc;
1100 
1101 	/* check we have all the required DT properties */
1102 	if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
1103 		dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
1104 		return -ENODEV;
1105 	}
1106 
1107 	if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
1108 		dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
1109 		return -ENODEV;
1110 	}
1111 
1112 	if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
1113 		dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
1114 		return -ENODEV;
1115 	}
1116 
1117 	if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
1118 		dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
1119 		return -ENODEV;
1120 	}
1121 
1122 
1123 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1124 	if (!p)
1125 		return -ENOMEM;
1126 
1127 	/* Initialize the dimm mutex */
1128 	mutex_init(&p->health_mutex);
1129 
1130 	/* optional DT properties */
1131 	of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
1132 
1133 	p->dn = dn;
1134 	p->drc_index = drc_index;
1135 	p->block_size = block_size;
1136 	p->blocks = blocks;
1137 	p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
1138 	p->hcall_flush_required = of_property_read_bool(dn, "ibm,hcall-flush-required");
1139 
1140 	/* We just need to ensure that set cookies are unique across */
1141 	uuid_parse(uuid_str, (uuid_t *) uuid);
1142 	/*
1143 	 * cookie1 and cookie2 are not really little endian
1144 	 * we store a little endian representation of the
1145 	 * uuid str so that we can compare this with the label
1146 	 * area cookie irrespective of the endian config with which
1147 	 * the kernel is built.
1148 	 */
1149 	p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
1150 	p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
1151 
1152 	/* might be zero */
1153 	p->metadata_size = metadata_size;
1154 	p->pdev = pdev;
1155 
1156 	/* request the hypervisor to bind this region to somewhere in memory */
1157 	rc = drc_pmem_bind(p);
1158 
1159 	/* If phyp says drc memory still bound then force unbound and retry */
1160 	if (rc == H_OVERLAP)
1161 		rc = drc_pmem_query_n_bind(p);
1162 
1163 	if (rc != H_SUCCESS) {
1164 		dev_err(&p->pdev->dev, "bind err: %d\n", rc);
1165 		rc = -ENXIO;
1166 		goto err;
1167 	}
1168 
1169 	/* setup the resource for the newly bound range */
1170 	p->res.start = p->bound_addr;
1171 	p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
1172 	p->res.name  = pdev->name;
1173 	p->res.flags = IORESOURCE_MEM;
1174 
1175 	rc = papr_scm_nvdimm_init(p);
1176 	if (rc)
1177 		goto err2;
1178 
1179 	platform_set_drvdata(pdev, p);
1180 
1181 	return 0;
1182 
1183 err2:	drc_pmem_unbind(p);
1184 err:	kfree(p);
1185 	return rc;
1186 }
1187 
1188 static int papr_scm_remove(struct platform_device *pdev)
1189 {
1190 	struct papr_scm_priv *p = platform_get_drvdata(pdev);
1191 
1192 	mutex_lock(&papr_ndr_lock);
1193 	list_del(&p->region_list);
1194 	mutex_unlock(&papr_ndr_lock);
1195 
1196 	nvdimm_bus_unregister(p->bus);
1197 	drc_pmem_unbind(p);
1198 	kfree(p->bus_desc.provider_name);
1199 	kfree(p);
1200 
1201 	return 0;
1202 }
1203 
1204 static const struct of_device_id papr_scm_match[] = {
1205 	{ .compatible = "ibm,pmemory" },
1206 	{ .compatible = "ibm,pmemory-v2" },
1207 	{ },
1208 };
1209 
1210 static struct platform_driver papr_scm_driver = {
1211 	.probe = papr_scm_probe,
1212 	.remove = papr_scm_remove,
1213 	.driver = {
1214 		.name = "papr_scm",
1215 		.of_match_table = papr_scm_match,
1216 	},
1217 };
1218 
1219 static int __init papr_scm_init(void)
1220 {
1221 	int ret;
1222 
1223 	ret = platform_driver_register(&papr_scm_driver);
1224 	if (!ret)
1225 		mce_register_notifier(&mce_ue_nb);
1226 
1227 	return ret;
1228 }
1229 module_init(papr_scm_init);
1230 
1231 static void __exit papr_scm_exit(void)
1232 {
1233 	mce_unregister_notifier(&mce_ue_nb);
1234 	platform_driver_unregister(&papr_scm_driver);
1235 }
1236 module_exit(papr_scm_exit);
1237 
1238 MODULE_DEVICE_TABLE(of, papr_scm_match);
1239 MODULE_LICENSE("GPL");
1240 MODULE_AUTHOR("IBM Corporation");
1241