blob: 04ab5c9d3cedc16ac5250ee70b610829216d6117 [file] [log] [blame]
Huang Ying2ff729d2010-08-12 11:55:17 +08001/*
2 * APEI Error Record Serialization Table debug support
3 *
4 * ERST is a way provided by APEI to save and retrieve hardware error
Lucas De Marchi58f87ed2010-09-07 12:49:45 -04005 * information to and from a persistent store. This file provide the
Huang Ying2ff729d2010-08-12 11:55:17 +08006 * debugging/testing support for ERST kernel support and firmware
7 * implementation.
8 *
9 * Copyright 2010 Intel Corp.
10 * Author: Huang Ying <ying.huang@intel.com>
11 *
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.
15 *
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.
20 *
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
24 */
25
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>
31
32#include "apei-internal.h"
33
34#define ERST_DBG_PFX "ERST DBG: "
35
Chen Gongd37afc52011-07-13 13:14:14 +080036#define ERST_DBG_RECORD_LEN_MAX 0x4000
Huang Ying2ff729d2010-08-12 11:55:17 +080037
38static void *erst_dbg_buf;
39static unsigned int erst_dbg_buf_len;
40
41/* Prevent erst_dbg_read/write from being invoked concurrently */
42static DEFINE_MUTEX(erst_dbg_mutex);
43
44static int erst_dbg_open(struct inode *inode, struct file *file)
45{
Huang Ying885b9762011-02-21 13:54:41 +080046 int rc, *pos;
47
Huang Ying2ff729d2010-08-12 11:55:17 +080048 if (erst_disable)
49 return -ENODEV;
50
Huang Ying885b9762011-02-21 13:54:41 +080051 pos = (int *)&file->private_data;
52
53 rc = erst_get_record_id_begin(pos);
54 if (rc)
55 return rc;
56
Huang Ying2ff729d2010-08-12 11:55:17 +080057 return nonseekable_open(inode, file);
58}
59
Huang Ying885b9762011-02-21 13:54:41 +080060static int erst_dbg_release(struct inode *inode, struct file *file)
61{
62 erst_get_record_id_end();
63
64 return 0;
65}
66
Huang Ying2ff729d2010-08-12 11:55:17 +080067static long erst_dbg_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
68{
69 int rc;
70 u64 record_id;
71 u32 record_count;
72
73 switch (cmd) {
74 case APEI_ERST_CLEAR_RECORD:
75 rc = copy_from_user(&record_id, (void __user *)arg,
76 sizeof(record_id));
77 if (rc)
78 return -EFAULT;
79 return erst_clear(record_id);
80 case APEI_ERST_GET_RECORD_COUNT:
81 rc = erst_get_record_count();
82 if (rc < 0)
83 return rc;
84 record_count = rc;
85 rc = put_user(record_count, (u32 __user *)arg);
86 if (rc)
87 return rc;
88 return 0;
89 default:
90 return -ENOTTY;
91 }
92}
93
94static ssize_t erst_dbg_read(struct file *filp, char __user *ubuf,
95 size_t usize, loff_t *off)
96{
Huang Ying885b9762011-02-21 13:54:41 +080097 int rc, *pos;
Huang Ying2ff729d2010-08-12 11:55:17 +080098 ssize_t len = 0;
99 u64 id;
100
Huang Ying885b9762011-02-21 13:54:41 +0800101 if (*off)
Huang Ying2ff729d2010-08-12 11:55:17 +0800102 return -EINVAL;
103
104 if (mutex_lock_interruptible(&erst_dbg_mutex) != 0)
105 return -EINTR;
106
Huang Ying885b9762011-02-21 13:54:41 +0800107 pos = (int *)&filp->private_data;
108
Huang Ying2ff729d2010-08-12 11:55:17 +0800109retry_next:
Huang Ying885b9762011-02-21 13:54:41 +0800110 rc = erst_get_record_id_next(pos, &id);
Huang Ying2ff729d2010-08-12 11:55:17 +0800111 if (rc)
112 goto out;
113 /* no more record */
Adrian Huang6780aa62012-12-15 00:30:07 +0100114 if (id == APEI_ERST_INVALID_RECORD_ID) {
115 /*
116 * If the persistent store is empty initially, the function
117 * 'erst_read' below will return "-ENOENT" value. This causes
118 * 'retry_next' label is entered again. The returned value
119 * should be zero indicating the read operation is EOF.
120 */
121 len = 0;
122
Huang Ying2ff729d2010-08-12 11:55:17 +0800123 goto out;
Adrian Huang6780aa62012-12-15 00:30:07 +0100124 }
Huang Ying2ff729d2010-08-12 11:55:17 +0800125retry:
126 rc = len = erst_read(id, erst_dbg_buf, erst_dbg_buf_len);
127 /* The record may be cleared by others, try read next record */
128 if (rc == -ENOENT)
129 goto retry_next;
130 if (rc < 0)
131 goto out;
132 if (len > ERST_DBG_RECORD_LEN_MAX) {
133 pr_warning(ERST_DBG_PFX
134 "Record (ID: 0x%llx) length is too long: %zd\n",
135 id, len);
136 rc = -EIO;
137 goto out;
138 }
139 if (len > erst_dbg_buf_len) {
Huang Ying23f124c2010-09-29 19:53:54 +0800140 void *p;
Huang Ying2ff729d2010-08-12 11:55:17 +0800141 rc = -ENOMEM;
Huang Ying23f124c2010-09-29 19:53:54 +0800142 p = kmalloc(len, GFP_KERNEL);
143 if (!p)
Huang Ying2ff729d2010-08-12 11:55:17 +0800144 goto out;
Huang Ying23f124c2010-09-29 19:53:54 +0800145 kfree(erst_dbg_buf);
146 erst_dbg_buf = p;
Huang Ying2ff729d2010-08-12 11:55:17 +0800147 erst_dbg_buf_len = len;
148 goto retry;
149 }
150
151 rc = -EINVAL;
152 if (len > usize)
153 goto out;
154
155 rc = -EFAULT;
156 if (copy_to_user(ubuf, erst_dbg_buf, len))
157 goto out;
158 rc = 0;
159out:
160 mutex_unlock(&erst_dbg_mutex);
161 return rc ? rc : len;
162}
163
164static ssize_t erst_dbg_write(struct file *filp, const char __user *ubuf,
165 size_t usize, loff_t *off)
166{
167 int rc;
168 struct cper_record_header *rcd;
169
170 if (!capable(CAP_SYS_ADMIN))
171 return -EPERM;
172
173 if (usize > ERST_DBG_RECORD_LEN_MAX) {
174 pr_err(ERST_DBG_PFX "Too long record to be written\n");
175 return -EINVAL;
176 }
177
178 if (mutex_lock_interruptible(&erst_dbg_mutex))
179 return -EINTR;
180 if (usize > erst_dbg_buf_len) {
Huang Ying23f124c2010-09-29 19:53:54 +0800181 void *p;
Huang Ying2ff729d2010-08-12 11:55:17 +0800182 rc = -ENOMEM;
Huang Ying23f124c2010-09-29 19:53:54 +0800183 p = kmalloc(usize, GFP_KERNEL);
184 if (!p)
Huang Ying2ff729d2010-08-12 11:55:17 +0800185 goto out;
Huang Ying23f124c2010-09-29 19:53:54 +0800186 kfree(erst_dbg_buf);
187 erst_dbg_buf = p;
Huang Ying2ff729d2010-08-12 11:55:17 +0800188 erst_dbg_buf_len = usize;
189 }
190 rc = copy_from_user(erst_dbg_buf, ubuf, usize);
191 if (rc) {
192 rc = -EFAULT;
193 goto out;
194 }
195 rcd = erst_dbg_buf;
196 rc = -EINVAL;
197 if (rcd->record_length != usize)
198 goto out;
199
200 rc = erst_write(erst_dbg_buf);
201
202out:
203 mutex_unlock(&erst_dbg_mutex);
204 return rc < 0 ? rc : usize;
205}
206
207static const struct file_operations erst_dbg_ops = {
208 .owner = THIS_MODULE,
209 .open = erst_dbg_open,
Huang Ying885b9762011-02-21 13:54:41 +0800210 .release = erst_dbg_release,
Huang Ying2ff729d2010-08-12 11:55:17 +0800211 .read = erst_dbg_read,
212 .write = erst_dbg_write,
213 .unlocked_ioctl = erst_dbg_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200214 .llseek = no_llseek,
Huang Ying2ff729d2010-08-12 11:55:17 +0800215};
216
217static struct miscdevice erst_dbg_dev = {
218 .minor = MISC_DYNAMIC_MINOR,
219 .name = "erst_dbg",
220 .fops = &erst_dbg_ops,
221};
222
223static __init int erst_dbg_init(void)
224{
Huang Yingca7cc512011-07-13 13:14:13 +0800225 if (erst_disable) {
226 pr_info(ERST_DBG_PFX "ERST support is disabled.\n");
227 return -ENODEV;
228 }
Huang Ying2ff729d2010-08-12 11:55:17 +0800229 return misc_register(&erst_dbg_dev);
230}
231
232static __exit void erst_dbg_exit(void)
233{
234 misc_deregister(&erst_dbg_dev);
235 kfree(erst_dbg_buf);
236}
237
238module_init(erst_dbg_init);
239module_exit(erst_dbg_exit);
240
241MODULE_AUTHOR("Huang Ying");
242MODULE_DESCRIPTION("APEI Error Record Serialization Table debug support");
243MODULE_LICENSE("GPL");