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_status(ec->ec_dev, &snapshot_msg);
76 	if (ret < 0)
77 		goto resched;
78 
79 	/* Loop until we have read everything, or there's an error. */
80 	mutex_lock(&debug_info->log_mutex);
81 	buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
82 
83 	while (1) {
84 		if (!buf_space) {
85 			dev_info_once(ec->dev,
86 				      "Some logs may have been dropped...\n");
87 			break;
88 		}
89 
90 		memset(read_params, '\0', sizeof(*read_params));
91 		read_params->subcmd = CONSOLE_READ_RECENT;
92 		ret = cros_ec_cmd_xfer_status(ec->ec_dev,
93 					      debug_info->read_msg);
94 		if (ret < 0)
95 			break;
96 
97 		/* If the buffer is empty, we're done here. */
98 		if (ret == 0 || ec_buffer[0] == '\0')
99 			break;
100 
101 		idx = 0;
102 		while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
103 			cb->buf[cb->head] = ec_buffer[idx];
104 			cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
105 			idx++;
106 			buf_space--;
107 		}
108 
109 		wake_up(&debug_info->log_wq);
110 	}
111 
112 	mutex_unlock(&debug_info->log_mutex);
113 
114 resched:
115 	schedule_delayed_work(&debug_info->log_poll_work,
116 			      msecs_to_jiffies(LOG_POLL_SEC * 1000));
117 }
118 
119 static int cros_ec_console_log_open(struct inode *inode, struct file *file)
120 {
121 	file->private_data = inode->i_private;
122 
123 	return stream_open(inode, file);
124 }
125 
126 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
127 					size_t count, loff_t *ppos)
128 {
129 	struct cros_ec_debugfs *debug_info = file->private_data;
130 	struct circ_buf *cb = &debug_info->log_buffer;
131 	ssize_t ret;
132 
133 	mutex_lock(&debug_info->log_mutex);
134 
135 	while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
136 		if (file->f_flags & O_NONBLOCK) {
137 			ret = -EAGAIN;
138 			goto error;
139 		}
140 
141 		mutex_unlock(&debug_info->log_mutex);
142 
143 		ret = wait_event_interruptible(debug_info->log_wq,
144 					CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
145 		if (ret < 0)
146 			return ret;
147 
148 		mutex_lock(&debug_info->log_mutex);
149 	}
150 
151 	/* Only copy until the end of the circular buffer, and let userspace
152 	 * retry to get the rest of the data.
153 	 */
154 	ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
155 		    count);
156 
157 	if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
158 		ret = -EFAULT;
159 		goto error;
160 	}
161 
162 	cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
163 
164 error:
165 	mutex_unlock(&debug_info->log_mutex);
166 	return ret;
167 }
168 
169 static __poll_t cros_ec_console_log_poll(struct file *file,
170 					     poll_table *wait)
171 {
172 	struct cros_ec_debugfs *debug_info = file->private_data;
173 	__poll_t mask = 0;
174 
175 	poll_wait(file, &debug_info->log_wq, wait);
176 
177 	mutex_lock(&debug_info->log_mutex);
178 	if (CIRC_CNT(debug_info->log_buffer.head,
179 		     debug_info->log_buffer.tail,
180 		     LOG_SIZE))
181 		mask |= EPOLLIN | EPOLLRDNORM;
182 	mutex_unlock(&debug_info->log_mutex);
183 
184 	return mask;
185 }
186 
187 static int cros_ec_console_log_release(struct inode *inode, struct file *file)
188 {
189 	return 0;
190 }
191 
192 static ssize_t cros_ec_pdinfo_read(struct file *file,
193 				   char __user *user_buf,
194 				   size_t count,
195 				   loff_t *ppos)
196 {
197 	char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
198 	struct cros_ec_debugfs *debug_info = file->private_data;
199 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
200 	struct {
201 		struct cros_ec_command msg;
202 		union {
203 			struct ec_response_usb_pd_control_v1 resp;
204 			struct ec_params_usb_pd_control params;
205 		};
206 	} __packed ec_buf;
207 	struct cros_ec_command *msg;
208 	struct ec_response_usb_pd_control_v1 *resp;
209 	struct ec_params_usb_pd_control *params;
210 	int i;
211 
212 	msg = &ec_buf.msg;
213 	params = (struct ec_params_usb_pd_control *)msg->data;
214 	resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
215 
216 	msg->command = EC_CMD_USB_PD_CONTROL;
217 	msg->version = 1;
218 	msg->insize = sizeof(*resp);
219 	msg->outsize = sizeof(*params);
220 
221 	/*
222 	 * Read status from all PD ports until failure, typically caused
223 	 * by attempting to read status on a port that doesn't exist.
224 	 */
225 	for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
226 		params->port = i;
227 		params->role = 0;
228 		params->mux = 0;
229 		params->swap = 0;
230 
231 		if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
232 			break;
233 
234 		p += scnprintf(p, sizeof(read_buf) + read_buf - p,
235 			       "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
236 			       resp->state, resp->enabled, resp->role,
237 			       resp->polarity);
238 	}
239 
240 	return simple_read_from_buffer(user_buf, count, ppos,
241 				       read_buf, p - read_buf);
242 }
243 
244 const struct file_operations cros_ec_console_log_fops = {
245 	.owner = THIS_MODULE,
246 	.open = cros_ec_console_log_open,
247 	.read = cros_ec_console_log_read,
248 	.llseek = no_llseek,
249 	.poll = cros_ec_console_log_poll,
250 	.release = cros_ec_console_log_release,
251 };
252 
253 const struct file_operations cros_ec_pdinfo_fops = {
254 	.owner = THIS_MODULE,
255 	.open = simple_open,
256 	.read = cros_ec_pdinfo_read,
257 	.llseek = default_llseek,
258 };
259 
260 static int ec_read_version_supported(struct cros_ec_dev *ec)
261 {
262 	struct ec_params_get_cmd_versions_v1 *params;
263 	struct ec_response_get_cmd_versions *response;
264 	int ret;
265 
266 	struct cros_ec_command *msg;
267 
268 	msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
269 		GFP_KERNEL);
270 	if (!msg)
271 		return 0;
272 
273 	msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
274 	msg->outsize = sizeof(*params);
275 	msg->insize = sizeof(*response);
276 
277 	params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
278 	params->cmd = EC_CMD_CONSOLE_READ;
279 	response = (struct ec_response_get_cmd_versions *)msg->data;
280 
281 	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
282 	      response->version_mask & EC_VER_MASK(1);
283 
284 	kfree(msg);
285 
286 	return ret;
287 }
288 
289 static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
290 {
291 	struct cros_ec_dev *ec = debug_info->ec;
292 	char *buf;
293 	int read_params_size;
294 	int read_response_size;
295 
296 	/*
297 	 * If the console log feature is not supported return silently and
298 	 * don't create the console_log entry.
299 	 */
300 	if (!ec_read_version_supported(ec))
301 		return 0;
302 
303 	buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
304 	if (!buf)
305 		return -ENOMEM;
306 
307 	read_params_size = sizeof(struct ec_params_console_read_v1);
308 	read_response_size = ec->ec_dev->max_response;
309 	debug_info->read_msg = devm_kzalloc(ec->dev,
310 		sizeof(*debug_info->read_msg) +
311 			max(read_params_size, read_response_size), GFP_KERNEL);
312 	if (!debug_info->read_msg)
313 		return -ENOMEM;
314 
315 	debug_info->read_msg->version = 1;
316 	debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
317 	debug_info->read_msg->outsize = read_params_size;
318 	debug_info->read_msg->insize = read_response_size;
319 
320 	debug_info->log_buffer.buf = buf;
321 	debug_info->log_buffer.head = 0;
322 	debug_info->log_buffer.tail = 0;
323 
324 	mutex_init(&debug_info->log_mutex);
325 	init_waitqueue_head(&debug_info->log_wq);
326 
327 	debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
328 			    debug_info, &cros_ec_console_log_fops);
329 
330 	INIT_DELAYED_WORK(&debug_info->log_poll_work,
331 			  cros_ec_console_log_work);
332 	schedule_delayed_work(&debug_info->log_poll_work, 0);
333 
334 	return 0;
335 }
336 
337 static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
338 {
339 	if (debug_info->log_buffer.buf) {
340 		cancel_delayed_work_sync(&debug_info->log_poll_work);
341 		mutex_destroy(&debug_info->log_mutex);
342 	}
343 }
344 
345 static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
346 {
347 	struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
348 	int ret;
349 	struct cros_ec_command *msg;
350 	int insize;
351 
352 	insize = ec_dev->max_response;
353 
354 	msg = devm_kzalloc(debug_info->ec->dev,
355 			sizeof(*msg) + insize, GFP_KERNEL);
356 	if (!msg)
357 		return -ENOMEM;
358 
359 	msg->command = EC_CMD_GET_PANIC_INFO;
360 	msg->insize = insize;
361 
362 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
363 	if (ret < 0) {
364 		ret = 0;
365 		goto free;
366 	}
367 
368 	/* No panic data */
369 	if (ret == 0)
370 		goto free;
371 
372 	debug_info->panicinfo_blob.data = msg->data;
373 	debug_info->panicinfo_blob.size = ret;
374 
375 	debugfs_create_blob("panicinfo", S_IFREG | 0444, debug_info->dir,
376 			    &debug_info->panicinfo_blob);
377 
378 	return 0;
379 
380 free:
381 	devm_kfree(debug_info->ec->dev, msg);
382 	return ret;
383 }
384 
385 static int cros_ec_debugfs_probe(struct platform_device *pd)
386 {
387 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
388 	struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
389 	const char *name = ec_platform->ec_name;
390 	struct cros_ec_debugfs *debug_info;
391 	int ret;
392 
393 	debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
394 	if (!debug_info)
395 		return -ENOMEM;
396 
397 	debug_info->ec = ec;
398 	debug_info->dir = debugfs_create_dir(name, NULL);
399 
400 	ret = cros_ec_create_panicinfo(debug_info);
401 	if (ret)
402 		goto remove_debugfs;
403 
404 	ret = cros_ec_create_console_log(debug_info);
405 	if (ret)
406 		goto remove_debugfs;
407 
408 	debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
409 			    &cros_ec_pdinfo_fops);
410 
411 	ec->debug_info = debug_info;
412 
413 	dev_set_drvdata(&pd->dev, ec);
414 
415 	return 0;
416 
417 remove_debugfs:
418 	debugfs_remove_recursive(debug_info->dir);
419 	return ret;
420 }
421 
422 static int cros_ec_debugfs_remove(struct platform_device *pd)
423 {
424 	struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
425 
426 	debugfs_remove_recursive(ec->debug_info->dir);
427 	cros_ec_cleanup_console_log(ec->debug_info);
428 
429 	return 0;
430 }
431 
432 static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
433 {
434 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
435 
436 	if (ec->debug_info->log_buffer.buf)
437 		cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
438 
439 	return 0;
440 }
441 
442 static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
443 {
444 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
445 
446 	if (ec->debug_info->log_buffer.buf)
447 		schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
448 
449 	return 0;
450 }
451 
452 static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
453 			 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
454 
455 static struct platform_driver cros_ec_debugfs_driver = {
456 	.driver = {
457 		.name = DRV_NAME,
458 		.pm = &cros_ec_debugfs_pm_ops,
459 	},
460 	.probe = cros_ec_debugfs_probe,
461 	.remove = cros_ec_debugfs_remove,
462 };
463 
464 module_platform_driver(cros_ec_debugfs_driver);
465 
466 MODULE_LICENSE("GPL");
467 MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
468 MODULE_ALIAS("platform:" DRV_NAME);
469