xref: /linux/drivers/platform/chrome/cros_ec_debugfs.c (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Debug logs for the ChromeOS EC
3 //
4 // Copyright (C) 2015 Google, Inc.
5 
6 #include <linux/circ_buf.h>
7 #include <linux/debugfs.h>
8 #include <linux/delay.h>
9 #include <linux/fs.h>
10 #include <linux/mfd/cros_ec.h>
11 #include <linux/mfd/cros_ec_commands.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/wait.h>
19 
20 #define DRV_NAME "cros-ec-debugfs"
21 
22 #define LOG_SHIFT		14
23 #define LOG_SIZE		(1 << LOG_SHIFT)
24 #define LOG_POLL_SEC		10
25 
26 #define CIRC_ADD(idx, size, value)	(((idx) + (value)) & ((size) - 1))
27 
28 /* struct cros_ec_debugfs - ChromeOS EC debugging information
29  *
30  * @ec: EC device this debugfs information belongs to
31  * @dir: dentry for debugfs files
32  * @log_buffer: circular buffer for console log information
33  * @read_msg: preallocated EC command and buffer to read console log
34  * @log_mutex: mutex to protect circular buffer
35  * @log_wq: waitqueue for log readers
36  * @log_poll_work: recurring task to poll EC for new console log data
37  * @panicinfo_blob: panicinfo debugfs blob
38  */
39 struct cros_ec_debugfs {
40 	struct cros_ec_dev *ec;
41 	struct dentry *dir;
42 	/* EC log */
43 	struct circ_buf log_buffer;
44 	struct cros_ec_command *read_msg;
45 	struct mutex log_mutex;
46 	wait_queue_head_t log_wq;
47 	struct delayed_work log_poll_work;
48 	/* EC panicinfo */
49 	struct debugfs_blob_wrapper panicinfo_blob;
50 };
51 
52 /*
53  * We need to make sure that the EC log buffer on the UART is large enough,
54  * so that it is unlikely enough to overlow within LOG_POLL_SEC.
55  */
56 static void cros_ec_console_log_work(struct work_struct *__work)
57 {
58 	struct cros_ec_debugfs *debug_info =
59 		container_of(to_delayed_work(__work),
60 			     struct cros_ec_debugfs,
61 			     log_poll_work);
62 	struct cros_ec_dev *ec = debug_info->ec;
63 	struct circ_buf *cb = &debug_info->log_buffer;
64 	struct cros_ec_command snapshot_msg = {
65 		.command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
66 	};
67 
68 	struct ec_params_console_read_v1 *read_params =
69 		(struct ec_params_console_read_v1 *)debug_info->read_msg->data;
70 	uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
71 	int idx;
72 	int buf_space;
73 	int ret;
74 
75 	ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
76 	if (ret < 0) {
77 		dev_err(ec->dev, "EC communication failed\n");
78 		goto resched;
79 	}
80 	if (snapshot_msg.result != EC_RES_SUCCESS) {
81 		dev_err(ec->dev, "EC failed to snapshot the console log\n");
82 		goto resched;
83 	}
84 
85 	/* Loop until we have read everything, or there's an error. */
86 	mutex_lock(&debug_info->log_mutex);
87 	buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
88 
89 	while (1) {
90 		if (!buf_space) {
91 			dev_info_once(ec->dev,
92 				      "Some logs may have been dropped...\n");
93 			break;
94 		}
95 
96 		memset(read_params, '\0', sizeof(*read_params));
97 		read_params->subcmd = CONSOLE_READ_RECENT;
98 		ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
99 		if (ret < 0) {
100 			dev_err(ec->dev, "EC communication failed\n");
101 			break;
102 		}
103 		if (debug_info->read_msg->result != EC_RES_SUCCESS) {
104 			dev_err(ec->dev,
105 				"EC failed to read the console log\n");
106 			break;
107 		}
108 
109 		/* If the buffer is empty, we're done here. */
110 		if (ret == 0 || ec_buffer[0] == '\0')
111 			break;
112 
113 		idx = 0;
114 		while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
115 			cb->buf[cb->head] = ec_buffer[idx];
116 			cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
117 			idx++;
118 			buf_space--;
119 		}
120 
121 		wake_up(&debug_info->log_wq);
122 	}
123 
124 	mutex_unlock(&debug_info->log_mutex);
125 
126 resched:
127 	schedule_delayed_work(&debug_info->log_poll_work,
128 			      msecs_to_jiffies(LOG_POLL_SEC * 1000));
129 }
130 
131 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
132 {
133 	file->private_data = inode->i_private;
134 
135 	return nonseekable_open(inode, file);
136 }
137 
138 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
139 					size_t count, loff_t *ppos)
140 {
141 	struct cros_ec_debugfs *debug_info = file->private_data;
142 	struct circ_buf *cb = &debug_info->log_buffer;
143 	ssize_t ret;
144 
145 	mutex_lock(&debug_info->log_mutex);
146 
147 	while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
148 		if (file->f_flags & O_NONBLOCK) {
149 			ret = -EAGAIN;
150 			goto error;
151 		}
152 
153 		mutex_unlock(&debug_info->log_mutex);
154 
155 		ret = wait_event_interruptible(debug_info->log_wq,
156 					CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
157 		if (ret < 0)
158 			return ret;
159 
160 		mutex_lock(&debug_info->log_mutex);
161 	}
162 
163 	/* Only copy until the end of the circular buffer, and let userspace
164 	 * retry to get the rest of the data.
165 	 */
166 	ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
167 		    count);
168 
169 	if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
170 		ret = -EFAULT;
171 		goto error;
172 	}
173 
174 	cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
175 
176 error:
177 	mutex_unlock(&debug_info->log_mutex);
178 	return ret;
179 }
180 
181 static __poll_t cros_ec_console_log_poll(struct file *file,
182 					     poll_table *wait)
183 {
184 	struct cros_ec_debugfs *debug_info = file->private_data;
185 	__poll_t mask = 0;
186 
187 	poll_wait(file, &debug_info->log_wq, wait);
188 
189 	mutex_lock(&debug_info->log_mutex);
190 	if (CIRC_CNT(debug_info->log_buffer.head,
191 		     debug_info->log_buffer.tail,
192 		     LOG_SIZE))
193 		mask |= EPOLLIN | EPOLLRDNORM;
194 	mutex_unlock(&debug_info->log_mutex);
195 
196 	return mask;
197 }
198 
199 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
200 {
201 	return 0;
202 }
203 
204 static ssize_t cros_ec_pdinfo_read(struct file *file,
205 				   char __user *user_buf,
206 				   size_t count,
207 				   loff_t *ppos)
208 {
209 	char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
210 	struct cros_ec_debugfs *debug_info = file->private_data;
211 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
212 	struct {
213 		struct cros_ec_command msg;
214 		union {
215 			struct ec_response_usb_pd_control_v1 resp;
216 			struct ec_params_usb_pd_control params;
217 		};
218 	} __packed ec_buf;
219 	struct cros_ec_command *msg;
220 	struct ec_response_usb_pd_control_v1 *resp;
221 	struct ec_params_usb_pd_control *params;
222 	int i;
223 
224 	msg = &ec_buf.msg;
225 	params = (struct ec_params_usb_pd_control *)msg->data;
226 	resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
227 
228 	msg->command = EC_CMD_USB_PD_CONTROL;
229 	msg->version = 1;
230 	msg->insize = sizeof(*resp);
231 	msg->outsize = sizeof(*params);
232 
233 	/*
234 	 * Read status from all PD ports until failure, typically caused
235 	 * by attempting to read status on a port that doesn't exist.
236 	 */
237 	for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
238 		params->port = i;
239 		params->role = 0;
240 		params->mux = 0;
241 		params->swap = 0;
242 
243 		if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
244 			break;
245 
246 		p += scnprintf(p, sizeof(read_buf) + read_buf - p,
247 			       "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
248 			       resp->state, resp->enabled, resp->role,
249 			       resp->polarity);
250 	}
251 
252 	return simple_read_from_buffer(user_buf, count, ppos,
253 				       read_buf, p - read_buf);
254 }
255 
256 const struct file_operations cros_ec_console_log_fops = {
257 	.owner = THIS_MODULE,
258 	.open = cros_ec_console_log_open,
259 	.read = cros_ec_console_log_read,
260 	.llseek = no_llseek,
261 	.poll = cros_ec_console_log_poll,
262 	.release = cros_ec_console_log_release,
263 };
264 
265 const struct file_operations cros_ec_pdinfo_fops = {
266 	.owner = THIS_MODULE,
267 	.open = simple_open,
268 	.read = cros_ec_pdinfo_read,
269 	.llseek = default_llseek,
270 };
271 
272 static int ec_read_version_supported(struct cros_ec_dev *ec)
273 {
274 	struct ec_params_get_cmd_versions_v1 *params;
275 	struct ec_response_get_cmd_versions *response;
276 	int ret;
277 
278 	struct cros_ec_command *msg;
279 
280 	msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
281 		GFP_KERNEL);
282 	if (!msg)
283 		return 0;
284 
285 	msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
286 	msg->outsize = sizeof(*params);
287 	msg->insize = sizeof(*response);
288 
289 	params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
290 	params->cmd = EC_CMD_CONSOLE_READ;
291 	response = (struct ec_response_get_cmd_versions *)msg->data;
292 
293 	ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
294 		msg->result == EC_RES_SUCCESS &&
295 		(response->version_mask & EC_VER_MASK(1));
296 
297 	kfree(msg);
298 
299 	return ret;
300 }
301 
302 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
303 {
304 	struct cros_ec_dev *ec = debug_info->ec;
305 	char *buf;
306 	int read_params_size;
307 	int read_response_size;
308 
309 	if (!ec_read_version_supported(ec)) {
310 		dev_warn(ec->dev,
311 			"device does not support reading the console log\n");
312 		return 0;
313 	}
314 
315 	buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
316 	if (!buf)
317 		return -ENOMEM;
318 
319 	read_params_size = sizeof(struct ec_params_console_read_v1);
320 	read_response_size = ec->ec_dev->max_response;
321 	debug_info->read_msg = devm_kzalloc(ec->dev,
322 		sizeof(*debug_info->read_msg) +
323 			max(read_params_size, read_response_size), GFP_KERNEL);
324 	if (!debug_info->read_msg)
325 		return -ENOMEM;
326 
327 	debug_info->read_msg->version = 1;
328 	debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
329 	debug_info->read_msg->outsize = read_params_size;
330 	debug_info->read_msg->insize = read_response_size;
331 
332 	debug_info->log_buffer.buf = buf;
333 	debug_info->log_buffer.head = 0;
334 	debug_info->log_buffer.tail = 0;
335 
336 	mutex_init(&debug_info->log_mutex);
337 	init_waitqueue_head(&debug_info->log_wq);
338 
339 	if (!debugfs_create_file("console_log",
340 				 S_IFREG | 0444,
341 				 debug_info->dir,
342 				 debug_info,
343 				 &cros_ec_console_log_fops))
344 		return -ENOMEM;
345 
346 	INIT_DELAYED_WORK(&debug_info->log_poll_work,
347 			  cros_ec_console_log_work);
348 	schedule_delayed_work(&debug_info->log_poll_work, 0);
349 
350 	return 0;
351 }
352 
353 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
354 {
355 	if (debug_info->log_buffer.buf) {
356 		cancel_delayed_work_sync(&debug_info->log_poll_work);
357 		mutex_destroy(&debug_info->log_mutex);
358 	}
359 }
360 
361 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
362 {
363 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
364 	int ret;
365 	struct cros_ec_command *msg;
366 	int insize;
367 
368 	insize = ec_dev->max_response;
369 
370 	msg = devm_kzalloc(debug_info->ec->dev,
371 			sizeof(*msg) + insize, GFP_KERNEL);
372 	if (!msg)
373 		return -ENOMEM;
374 
375 	msg->command = EC_CMD_GET_PANIC_INFO;
376 	msg->insize = insize;
377 
378 	ret = cros_ec_cmd_xfer(ec_dev, msg);
379 	if (ret < 0) {
380 		dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n");
381 		ret = 0;
382 		goto free;
383 	}
384 
385 	/* No panic data */
386 	if (ret == 0)
387 		goto free;
388 
389 	debug_info->panicinfo_blob.data = msg->data;
390 	debug_info->panicinfo_blob.size = ret;
391 
392 	if (!debugfs_create_blob("panicinfo",
393 				 S_IFREG | 0444,
394 				 debug_info->dir,
395 				 &debug_info->panicinfo_blob)) {
396 		ret = -ENOMEM;
397 		goto free;
398 	}
399 
400 	return 0;
401 
402 free:
403 	devm_kfree(debug_info->ec->dev, msg);
404 	return ret;
405 }
406 
407 static int cros_ec_create_pdinfo(struct cros_ec_debugfs *debug_info)
408 {
409 	if (!debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
410 				 &cros_ec_pdinfo_fops))
411 		return -ENOMEM;
412 
413 	return 0;
414 }
415 
416 static int cros_ec_debugfs_probe(struct platform_device *pd)
417 {
418 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
419 	struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
420 	const char *name = ec_platform->ec_name;
421 	struct cros_ec_debugfs *debug_info;
422 	int ret;
423 
424 	debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
425 	if (!debug_info)
426 		return -ENOMEM;
427 
428 	debug_info->ec = ec;
429 	debug_info->dir = debugfs_create_dir(name, NULL);
430 	if (!debug_info->dir)
431 		return -ENOMEM;
432 
433 	ret = cros_ec_create_panicinfo(debug_info);
434 	if (ret)
435 		goto remove_debugfs;
436 
437 	ret = cros_ec_create_console_log(debug_info);
438 	if (ret)
439 		goto remove_debugfs;
440 
441 	ret = cros_ec_create_pdinfo(debug_info);
442 	if (ret)
443 		goto remove_debugfs;
444 
445 	ec->debug_info = debug_info;
446 
447 	dev_set_drvdata(&pd->dev, ec);
448 
449 	return 0;
450 
451 remove_debugfs:
452 	debugfs_remove_recursive(debug_info->dir);
453 	return ret;
454 }
455 
456 static int cros_ec_debugfs_remove(struct platform_device *pd)
457 {
458 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
459 
460 	debugfs_remove_recursive(ec->debug_info->dir);
461 	cros_ec_cleanup_console_log(ec->debug_info);
462 
463 	return 0;
464 }
465 
466 static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
467 {
468 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
469 
470 	cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
471 
472 	return 0;
473 }
474 
475 static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
476 {
477 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
478 
479 	schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
480 
481 	return 0;
482 }
483 
484 static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
485 			 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
486 
487 static struct platform_driver cros_ec_debugfs_driver = {
488 	.driver = {
489 		.name = DRV_NAME,
490 		.pm = &cros_ec_debugfs_pm_ops,
491 	},
492 	.probe = cros_ec_debugfs_probe,
493 	.remove = cros_ec_debugfs_remove,
494 };
495 
496 module_platform_driver(cros_ec_debugfs_driver);
497 
498 MODULE_LICENSE("GPL");
499 MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
500 MODULE_ALIAS("platform:" DRV_NAME);
501