aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/platform/chrome/cros_ec_debugfs.c
diff options
context:
space:
mode:
authorEric Caruso <ejcaruso@chromium.org>2017-05-16 11:46:48 -0400
committerBenson Leung <bleung@chromium.org>2017-06-16 16:57:45 -0400
commite86264595225d2764a903965356ef59aeb7d1c47 (patch)
treef4999d2bf8c25004afaa1e351a7776e5378698a4 /drivers/platform/chrome/cros_ec_debugfs.c
parent0aa877c558477e5c4b0faaa618cfd41f8c0b3319 (diff)
mfd: cros_ec: add debugfs, console log file
If the EC supports the new CONSOLE_READ command type, then we place a console_log file in debugfs for that EC device which allows us to grab EC logs. The kernel will poll every 10 seconds for the log and keep its own buffer, but userspace should grab this and write it out to some logs which actually get rotated. Signed-off-by: Eric Caruso <ejcaruso@chromium.org> Signed-off-by: Nicolas Boichat <drinkcat@chromium.org> Acked-by: Lee Jones <lee.jones@linaro.org> Tested-by: Enric Balletbo i Serra <enric.balletbo@collabora.com> [bleung: restored original version of this commit, with pointer size issue to be fixed in next commit] Signed-off-by: Benson Leung <bleung@chromium.org>
Diffstat (limited to 'drivers/platform/chrome/cros_ec_debugfs.c')
-rw-r--r--drivers/platform/chrome/cros_ec_debugfs.c347
1 files changed, 347 insertions, 0 deletions
diff --git a/drivers/platform/chrome/cros_ec_debugfs.c b/drivers/platform/chrome/cros_ec_debugfs.c
new file mode 100644
index 000000000000..225f93631051
--- /dev/null
+++ b/drivers/platform/chrome/cros_ec_debugfs.c
@@ -0,0 +1,347 @@
1/*
2 * cros_ec_debugfs - debug logs for Chrome OS EC
3 *
4 * Copyright 2015 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/circ_buf.h>
21#include <linux/debugfs.h>
22#include <linux/delay.h>
23#include <linux/fs.h>
24#include <linux/mfd/cros_ec.h>
25#include <linux/mfd/cros_ec_commands.h>
26#include <linux/mutex.h>
27#include <linux/poll.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/wait.h>
31
32#include "cros_ec_dev.h"
33#include "cros_ec_debugfs.h"
34
35#define LOG_SHIFT 14
36#define LOG_SIZE (1 << LOG_SHIFT)
37#define LOG_POLL_SEC 10
38
39#define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
40
41/* struct cros_ec_debugfs - ChromeOS EC debugging information
42 *
43 * @ec: EC device this debugfs information belongs to
44 * @dir: dentry for debugfs files
45 * @log_buffer: circular buffer for console log information
46 * @read_msg: preallocated EC command and buffer to read console log
47 * @log_mutex: mutex to protect circular buffer
48 * @log_wq: waitqueue for log readers
49 * @log_poll_work: recurring task to poll EC for new console log data
50 */
51struct cros_ec_debugfs {
52 struct cros_ec_dev *ec;
53 struct dentry *dir;
54 struct circ_buf log_buffer;
55 struct cros_ec_command *read_msg;
56 struct mutex log_mutex;
57 wait_queue_head_t log_wq;
58 struct delayed_work log_poll_work;
59};
60
61/*
62 * We need to make sure that the EC log buffer on the UART is large enough,
63 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
64 */
65static void cros_ec_console_log_work(struct work_struct *__work)
66{
67 struct cros_ec_debugfs *debug_info =
68 container_of(to_delayed_work(__work),
69 struct cros_ec_debugfs,
70 log_poll_work);
71 struct cros_ec_dev *ec = debug_info->ec;
72 struct circ_buf *cb = &debug_info->log_buffer;
73 struct cros_ec_command snapshot_msg = {
74 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
75 };
76
77 struct ec_params_console_read_v1 *read_params =
78 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
79 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
80 int idx;
81 int buf_space;
82 int ret;
83
84 ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
85 if (ret < 0) {
86 dev_err(ec->dev, "EC communication failed\n");
87 goto resched;
88 }
89 if (snapshot_msg.result != EC_RES_SUCCESS) {
90 dev_err(ec->dev, "EC failed to snapshot the console log\n");
91 goto resched;
92 }
93
94 /* Loop until we have read everything, or there's an error. */
95 mutex_lock(&debug_info->log_mutex);
96 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
97
98 while (1) {
99 if (!buf_space) {
100 dev_info_once(ec->dev,
101 "Some logs may have been dropped...\n");
102 break;
103 }
104
105 memset(read_params, '\0', sizeof(*read_params));
106 read_params->subcmd = CONSOLE_READ_RECENT;
107 ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
108 if (ret < 0) {
109 dev_err(ec->dev, "EC communication failed\n");
110 break;
111 }
112 if (debug_info->read_msg->result != EC_RES_SUCCESS) {
113 dev_err(ec->dev,
114 "EC failed to read the console log\n");
115 break;
116 }
117
118 /* If the buffer is empty, we're done here. */
119 if (ret == 0 || ec_buffer[0] == '\0')
120 break;
121
122 idx = 0;
123 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
124 cb->buf[cb->head] = ec_buffer[idx];
125 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
126 idx++;
127 buf_space--;
128 }
129
130 wake_up(&debug_info->log_wq);
131 }
132
133 mutex_unlock(&debug_info->log_mutex);
134
135resched:
136 schedule_delayed_work(&debug_info->log_poll_work,
137 msecs_to_jiffies(LOG_POLL_SEC * 1000));
138}
139
140static int cros_ec_console_log_open(struct inode *inode, struct file *file)
141{
142 file->private_data = inode->i_private;
143
144 return nonseekable_open(inode, file);
145}
146
147static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
148 size_t count, loff_t *ppos)
149{
150 struct cros_ec_debugfs *debug_info = file->private_data;
151 struct circ_buf *cb = &debug_info->log_buffer;
152 ssize_t ret;
153
154 mutex_lock(&debug_info->log_mutex);
155
156 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
157 if (file->f_flags & O_NONBLOCK) {
158 ret = -EAGAIN;
159 goto error;
160 }
161
162 mutex_unlock(&debug_info->log_mutex);
163
164 ret = wait_event_interruptible(debug_info->log_wq,
165 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
166 if (ret < 0)
167 return ret;
168
169 mutex_lock(&debug_info->log_mutex);
170 }
171
172 /* Only copy until the end of the circular buffer, and let userspace
173 * retry to get the rest of the data.
174 */
175 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
176 count);
177
178 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
179 ret = -EFAULT;
180 goto error;
181 }
182
183 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
184
185error:
186 mutex_unlock(&debug_info->log_mutex);
187 return ret;
188}
189
190static unsigned int cros_ec_console_log_poll(struct file *file,
191 poll_table *wait)
192{
193 struct cros_ec_debugfs *debug_info = file->private_data;
194 unsigned int mask = 0;
195
196 poll_wait(file, &debug_info->log_wq, wait);
197
198 mutex_lock(&debug_info->log_mutex);
199 if (CIRC_CNT(debug_info->log_buffer.head,
200 debug_info->log_buffer.tail,
201 LOG_SIZE))
202 mask |= POLLIN | POLLRDNORM;
203 mutex_unlock(&debug_info->log_mutex);
204
205 return mask;
206}
207
208static int cros_ec_console_log_release(struct inode *inode, struct file *file)
209{
210 return 0;
211}
212
213const struct file_operations cros_ec_console_log_fops = {
214 .owner = THIS_MODULE,
215 .open = cros_ec_console_log_open,
216 .read = cros_ec_console_log_read,
217 .llseek = no_llseek,
218 .poll = cros_ec_console_log_poll,
219 .release = cros_ec_console_log_release,
220};
221
222static int ec_read_version_supported(struct cros_ec_dev *ec)
223{
224 struct ec_params_get_cmd_versions_v1 *params;
225 struct ec_response_get_cmd_versions *response;
226 int ret;
227
228 struct cros_ec_command *msg;
229
230 msg = kzalloc(sizeof(*msg) + max(sizeof(params), sizeof(response)),
231 GFP_KERNEL);
232 if (!msg)
233 return 0;
234
235 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
236 msg->outsize = sizeof(params);
237 msg->insize = sizeof(response);
238
239 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
240 params->cmd = EC_CMD_CONSOLE_READ;
241 response = (struct ec_response_get_cmd_versions *)msg->data;
242
243 ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
244 msg->result == EC_RES_SUCCESS &&
245 (response->version_mask & EC_VER_MASK(1));
246
247 kfree(msg);
248
249 return ret;
250}
251
252static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
253{
254 struct cros_ec_dev *ec = debug_info->ec;
255 char *buf;
256 int read_params_size;
257 int read_response_size;
258
259 if (!ec_read_version_supported(ec)) {
260 dev_warn(ec->dev,
261 "device does not support reading the console log\n");
262 return 0;
263 }
264
265 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
266 if (!buf)
267 return -ENOMEM;
268
269 read_params_size = sizeof(struct ec_params_console_read_v1);
270 read_response_size = ec->ec_dev->max_response;
271 debug_info->read_msg = devm_kzalloc(ec->dev,
272 sizeof(*debug_info->read_msg) +
273 max(read_params_size, read_response_size), GFP_KERNEL);
274 if (!debug_info->read_msg)
275 return -ENOMEM;
276
277 debug_info->read_msg->version = 1;
278 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
279 debug_info->read_msg->outsize = read_params_size;
280 debug_info->read_msg->insize = read_response_size;
281
282 debug_info->log_buffer.buf = buf;
283 debug_info->log_buffer.head = 0;
284 debug_info->log_buffer.tail = 0;
285
286 mutex_init(&debug_info->log_mutex);
287 init_waitqueue_head(&debug_info->log_wq);
288
289 if (!debugfs_create_file("console_log",
290 S_IFREG | S_IRUGO,
291 debug_info->dir,
292 debug_info,
293 &cros_ec_console_log_fops))
294 return -ENOMEM;
295
296 INIT_DELAYED_WORK(&debug_info->log_poll_work,
297 cros_ec_console_log_work);
298 schedule_delayed_work(&debug_info->log_poll_work, 0);
299
300 return 0;
301}
302
303static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
304{
305 if (debug_info->log_buffer.buf) {
306 cancel_delayed_work_sync(&debug_info->log_poll_work);
307 mutex_destroy(&debug_info->log_mutex);
308 }
309}
310
311int cros_ec_debugfs_init(struct cros_ec_dev *ec)
312{
313 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
314 const char *name = ec_platform->ec_name;
315 struct cros_ec_debugfs *debug_info;
316 int ret;
317
318 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
319 if (!debug_info)
320 return -ENOMEM;
321
322 debug_info->ec = ec;
323 debug_info->dir = debugfs_create_dir(name, NULL);
324 if (!debug_info->dir)
325 return -ENOMEM;
326
327 ret = cros_ec_create_console_log(debug_info);
328 if (ret)
329 goto remove_debugfs;
330
331 ec->debug_info = debug_info;
332
333 return 0;
334
335remove_debugfs:
336 debugfs_remove_recursive(debug_info->dir);
337 return ret;
338}
339
340void cros_ec_debugfs_remove(struct cros_ec_dev *ec)
341{
342 if (!ec->debug_info)
343 return;
344
345 debugfs_remove_recursive(ec->debug_info->dir);
346 cros_ec_cleanup_console_log(ec->debug_info);
347}