18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * APEI Error Record Serialization Table debug support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * ERST is a way provided by APEI to save and retrieve hardware error
68c2ecf20Sopenharmony_ci * information to and from a persistent store. This file provide the
78c2ecf20Sopenharmony_ci * debugging/testing support for ERST kernel support and firmware
88c2ecf20Sopenharmony_ci * implementation.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright 2010 Intel Corp.
118c2ecf20Sopenharmony_ci *   Author: Huang Ying <ying.huang@intel.com>
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
178c2ecf20Sopenharmony_ci#include <acpi/apei.h>
188c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "apei-internal.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define ERST_DBG_PFX			"ERST DBG: "
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define ERST_DBG_RECORD_LEN_MAX		0x4000
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic void *erst_dbg_buf;
278c2ecf20Sopenharmony_cistatic unsigned int erst_dbg_buf_len;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Prevent erst_dbg_read/write from being invoked concurrently */
308c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(erst_dbg_mutex);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int erst_dbg_open(struct inode *inode, struct file *file)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	int rc, *pos;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	if (erst_disable)
378c2ecf20Sopenharmony_ci		return -ENODEV;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	pos = (int *)&file->private_data;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	rc = erst_get_record_id_begin(pos);
428c2ecf20Sopenharmony_ci	if (rc)
438c2ecf20Sopenharmony_ci		return rc;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return nonseekable_open(inode, file);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int erst_dbg_release(struct inode *inode, struct file *file)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	erst_get_record_id_end();
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return 0;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int rc;
588c2ecf20Sopenharmony_ci	u64 record_id;
598c2ecf20Sopenharmony_ci	u32 record_count;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	switch (cmd) {
628c2ecf20Sopenharmony_ci	case APEI_ERST_CLEAR_RECORD:
638c2ecf20Sopenharmony_ci		rc = copy_from_user(&record_id, (void __user *)arg,
648c2ecf20Sopenharmony_ci				    sizeof(record_id));
658c2ecf20Sopenharmony_ci		if (rc)
668c2ecf20Sopenharmony_ci			return -EFAULT;
678c2ecf20Sopenharmony_ci		return erst_clear(record_id);
688c2ecf20Sopenharmony_ci	case APEI_ERST_GET_RECORD_COUNT:
698c2ecf20Sopenharmony_ci		rc = erst_get_record_count();
708c2ecf20Sopenharmony_ci		if (rc < 0)
718c2ecf20Sopenharmony_ci			return rc;
728c2ecf20Sopenharmony_ci		record_count = rc;
738c2ecf20Sopenharmony_ci		rc = put_user(record_count, (u32 __user *)arg);
748c2ecf20Sopenharmony_ci		if (rc)
758c2ecf20Sopenharmony_ci			return rc;
768c2ecf20Sopenharmony_ci		return 0;
778c2ecf20Sopenharmony_ci	default:
788c2ecf20Sopenharmony_ci		return -ENOTTY;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
838c2ecf20Sopenharmony_ci			     size_t usize, loff_t *off)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	int rc, *pos;
868c2ecf20Sopenharmony_ci	ssize_t len = 0;
878c2ecf20Sopenharmony_ci	u64 id;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (*off)
908c2ecf20Sopenharmony_ci		return -EINVAL;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
938c2ecf20Sopenharmony_ci		return -EINTR;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	pos = (int *)&filp->private_data;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciretry_next:
988c2ecf20Sopenharmony_ci	rc = erst_get_record_id_next(pos, &id);
998c2ecf20Sopenharmony_ci	if (rc)
1008c2ecf20Sopenharmony_ci		goto out;
1018c2ecf20Sopenharmony_ci	/* no more record */
1028c2ecf20Sopenharmony_ci	if (id == APEI_ERST_INVALID_RECORD_ID) {
1038c2ecf20Sopenharmony_ci		/*
1048c2ecf20Sopenharmony_ci		 * If the persistent store is empty initially, the function
1058c2ecf20Sopenharmony_ci		 * 'erst_read' below will return "-ENOENT" value. This causes
1068c2ecf20Sopenharmony_ci		 * 'retry_next' label is entered again. The returned value
1078c2ecf20Sopenharmony_ci		 * should be zero indicating the read operation is EOF.
1088c2ecf20Sopenharmony_ci		 */
1098c2ecf20Sopenharmony_ci		len = 0;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		goto out;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ciretry:
1148c2ecf20Sopenharmony_ci	rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
1158c2ecf20Sopenharmony_ci	/* The record may be cleared by others, try read next record */
1168c2ecf20Sopenharmony_ci	if (rc == -ENOENT)
1178c2ecf20Sopenharmony_ci		goto retry_next;
1188c2ecf20Sopenharmony_ci	if (rc < 0)
1198c2ecf20Sopenharmony_ci		goto out;
1208c2ecf20Sopenharmony_ci	if (len > ERST_DBG_RECORD_LEN_MAX) {
1218c2ecf20Sopenharmony_ci		pr_warn(ERST_DBG_PFX
1228c2ecf20Sopenharmony_ci			"Record (ID: 0x%llx) length is too long: %zd\n", id, len);
1238c2ecf20Sopenharmony_ci		rc = -EIO;
1248c2ecf20Sopenharmony_ci		goto out;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci	if (len > erst_dbg_buf_len) {
1278c2ecf20Sopenharmony_ci		void *p;
1288c2ecf20Sopenharmony_ci		rc = -ENOMEM;
1298c2ecf20Sopenharmony_ci		p = kmalloc(len, GFP_KERNEL);
1308c2ecf20Sopenharmony_ci		if (!p)
1318c2ecf20Sopenharmony_ci			goto out;
1328c2ecf20Sopenharmony_ci		kfree(erst_dbg_buf);
1338c2ecf20Sopenharmony_ci		erst_dbg_buf = p;
1348c2ecf20Sopenharmony_ci		erst_dbg_buf_len = len;
1358c2ecf20Sopenharmony_ci		goto retry;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	rc = -EINVAL;
1398c2ecf20Sopenharmony_ci	if (len > usize)
1408c2ecf20Sopenharmony_ci		goto out;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	rc = -EFAULT;
1438c2ecf20Sopenharmony_ci	if (copy_to_user(ubuf, erst_dbg_buf, len))
1448c2ecf20Sopenharmony_ci		goto out;
1458c2ecf20Sopenharmony_ci	rc = 0;
1468c2ecf20Sopenharmony_ciout:
1478c2ecf20Sopenharmony_ci	mutex_unlock(&erst_dbg_mutex);
1488c2ecf20Sopenharmony_ci	return rc ? rc : len;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
1528c2ecf20Sopenharmony_ci			      size_t usize, loff_t *off)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	int rc;
1558c2ecf20Sopenharmony_ci	struct cper_record_header *rcd;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
1588c2ecf20Sopenharmony_ci		return -EPERM;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	if (usize > ERST_DBG_RECORD_LEN_MAX) {
1618c2ecf20Sopenharmony_ci		pr_err(ERST_DBG_PFX "Too long record to be written\n");
1628c2ecf20Sopenharmony_ci		return -EINVAL;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&erst_dbg_mutex))
1668c2ecf20Sopenharmony_ci		return -EINTR;
1678c2ecf20Sopenharmony_ci	if (usize > erst_dbg_buf_len) {
1688c2ecf20Sopenharmony_ci		void *p;
1698c2ecf20Sopenharmony_ci		rc = -ENOMEM;
1708c2ecf20Sopenharmony_ci		p = kmalloc(usize, GFP_KERNEL);
1718c2ecf20Sopenharmony_ci		if (!p)
1728c2ecf20Sopenharmony_ci			goto out;
1738c2ecf20Sopenharmony_ci		kfree(erst_dbg_buf);
1748c2ecf20Sopenharmony_ci		erst_dbg_buf = p;
1758c2ecf20Sopenharmony_ci		erst_dbg_buf_len = usize;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci	rc = copy_from_user(erst_dbg_buf, ubuf, usize);
1788c2ecf20Sopenharmony_ci	if (rc) {
1798c2ecf20Sopenharmony_ci		rc = -EFAULT;
1808c2ecf20Sopenharmony_ci		goto out;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci	rcd = erst_dbg_buf;
1838c2ecf20Sopenharmony_ci	rc = -EINVAL;
1848c2ecf20Sopenharmony_ci	if (rcd->record_length != usize)
1858c2ecf20Sopenharmony_ci		goto out;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	rc = erst_write(erst_dbg_buf);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ciout:
1908c2ecf20Sopenharmony_ci	mutex_unlock(&erst_dbg_mutex);
1918c2ecf20Sopenharmony_ci	return rc < 0 ? rc : usize;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic const struct file_operations erst_dbg_ops = {
1958c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1968c2ecf20Sopenharmony_ci	.open		= erst_dbg_open,
1978c2ecf20Sopenharmony_ci	.release	= erst_dbg_release,
1988c2ecf20Sopenharmony_ci	.read		= erst_dbg_read,
1998c2ecf20Sopenharmony_ci	.write		= erst_dbg_write,
2008c2ecf20Sopenharmony_ci	.unlocked_ioctl	= erst_dbg_ioctl,
2018c2ecf20Sopenharmony_ci	.llseek		= no_llseek,
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic struct miscdevice erst_dbg_dev = {
2058c2ecf20Sopenharmony_ci	.minor	= MISC_DYNAMIC_MINOR,
2068c2ecf20Sopenharmony_ci	.name	= "erst_dbg",
2078c2ecf20Sopenharmony_ci	.fops	= &erst_dbg_ops,
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic __init int erst_dbg_init(void)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	if (erst_disable) {
2138c2ecf20Sopenharmony_ci		pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
2148c2ecf20Sopenharmony_ci		return -ENODEV;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci	return misc_register(&erst_dbg_dev);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic __exit void erst_dbg_exit(void)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	misc_deregister(&erst_dbg_dev);
2228c2ecf20Sopenharmony_ci	kfree(erst_dbg_buf);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cimodule_init(erst_dbg_init);
2268c2ecf20Sopenharmony_cimodule_exit(erst_dbg_exit);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciMODULE_AUTHOR("Huang Ying");
2298c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
2308c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
231