2 * APEI Error Record Serialization Table debug support
4 * ERST is a way provided by APEI to save and retrieve hardware error
5 * infomation to and from a persistent store. This file provide the
6 * debugging/testing support for ERST kernel support and firmware
9 * Copyright 2010 Intel Corp.
10 * Author: Huang Ying <ying.huang@intel.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version
14 * 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/uaccess.h>
29 #include <acpi/apei.h>
30 #include <linux/miscdevice.h>
32 #include "apei-internal.h"
34 #define ERST_DBG_PFX "ERST DBG: "
36 #define ERST_DBG_RECORD_LEN_MAX 4096
38 static void *erst_dbg_buf;
39 static unsigned int erst_dbg_buf_len;
41 /* Prevent erst_dbg_read/write from being invoked concurrently */
42 static DEFINE_MUTEX(erst_dbg_mutex);
44 static int erst_dbg_open(struct inode *inode, struct file *file)
49 return nonseekable_open(inode, file);
52 static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
59 case APEI_ERST_CLEAR_RECORD:
60 rc = copy_from_user(&record_id, (void __user *)arg,
64 return erst_clear(record_id);
65 case APEI_ERST_GET_RECORD_COUNT:
66 rc = erst_get_record_count();
70 rc = put_user(record_count, (u32 __user *)arg);
79 static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
80 size_t usize, loff_t *off)
89 if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
93 rc = erst_get_next_record_id(&id);
97 if (id == APEI_ERST_INVALID_RECORD_ID)
100 rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
101 /* The record may be cleared by others, try read next record */
106 if (len > ERST_DBG_RECORD_LEN_MAX) {
107 pr_warning(ERST_DBG_PFX
108 "Record (ID: 0x%llx) length is too long: %zd\n",
113 if (len > erst_dbg_buf_len) {
116 erst_dbg_buf = kmalloc(len, GFP_KERNEL);
119 erst_dbg_buf_len = len;
128 if (copy_to_user(ubuf, erst_dbg_buf, len))
132 mutex_unlock(&erst_dbg_mutex);
133 return rc ? rc : len;
136 static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
137 size_t usize, loff_t *off)
140 struct cper_record_header *rcd;
142 if (!capable(CAP_SYS_ADMIN))
145 if (usize > ERST_DBG_RECORD_LEN_MAX) {
146 pr_err(ERST_DBG_PFX "Too long record to be written\n");
150 if (mutex_lock_interruptible(&erst_dbg_mutex))
152 if (usize > erst_dbg_buf_len) {
155 erst_dbg_buf = kmalloc(usize, GFP_KERNEL);
158 erst_dbg_buf_len = usize;
160 rc = copy_from_user(erst_dbg_buf, ubuf, usize);
167 if (rcd->record_length != usize)
170 rc = erst_write(erst_dbg_buf);
173 mutex_unlock(&erst_dbg_mutex);
174 return rc < 0 ? rc : usize;
177 static const struct file_operations erst_dbg_ops = {
178 .owner = THIS_MODULE,
179 .open = erst_dbg_open,
180 .read = erst_dbg_read,
181 .write = erst_dbg_write,
182 .unlocked_ioctl = erst_dbg_ioctl,
185 static struct miscdevice erst_dbg_dev = {
186 .minor = MISC_DYNAMIC_MINOR,
188 .fops = &erst_dbg_ops,
191 static __init int erst_dbg_init(void)
193 return misc_register(&erst_dbg_dev);
196 static __exit void erst_dbg_exit(void)
198 misc_deregister(&erst_dbg_dev);
202 module_init(erst_dbg_init);
203 module_exit(erst_dbg_exit);
205 MODULE_AUTHOR("Huang Ying");
206 MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
207 MODULE_LICENSE("GPL");