xref: /linux/drivers/thunderbolt/debugfs.c (revision ad9a72f9c6fd3998f0080701f1cc1f43a4317b7c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Debugfs interface
4  *
5  * Copyright (C) 2020, Intel Corporation
6  * Authors: Gil Fine <gil.fine@intel.com>
7  *	    Mika Westerberg <mika.westerberg@linux.intel.com>
8  */
9 
10 #include <linux/debugfs.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/uaccess.h>
13 
14 #include "tb.h"
15 
16 #define PORT_CAP_PCIE_LEN	1
17 #define PORT_CAP_POWER_LEN	2
18 #define PORT_CAP_LANE_LEN	3
19 #define PORT_CAP_USB3_LEN	5
20 #define PORT_CAP_DP_LEN		8
21 #define PORT_CAP_TMU_LEN	8
22 #define PORT_CAP_BASIC_LEN	9
23 #define PORT_CAP_USB4_LEN	20
24 
25 #define SWITCH_CAP_TMU_LEN	26
26 #define SWITCH_CAP_BASIC_LEN	27
27 
28 #define PATH_LEN		2
29 
30 #define COUNTER_SET_LEN		3
31 
32 #define DEBUGFS_ATTR(__space, __write)					\
33 static int __space ## _open(struct inode *inode, struct file *file)	\
34 {									\
35 	return single_open(file, __space ## _show, inode->i_private);	\
36 }									\
37 									\
38 static const struct file_operations __space ## _fops = {		\
39 	.owner = THIS_MODULE,						\
40 	.open = __space ## _open,					\
41 	.release = single_release,					\
42 	.read  = seq_read,						\
43 	.write = __write,						\
44 	.llseek = seq_lseek,						\
45 }
46 
47 #define DEBUGFS_ATTR_RO(__space)					\
48 	DEBUGFS_ATTR(__space, NULL)
49 
50 #define DEBUGFS_ATTR_RW(__space)					\
51 	DEBUGFS_ATTR(__space, __space ## _write)
52 
53 static struct dentry *tb_debugfs_root;
54 
55 static void *validate_and_copy_from_user(const void __user *user_buf,
56 					 size_t *count)
57 {
58 	size_t nbytes;
59 	void *buf;
60 
61 	if (!*count)
62 		return ERR_PTR(-EINVAL);
63 
64 	if (!access_ok(user_buf, *count))
65 		return ERR_PTR(-EFAULT);
66 
67 	buf = (void *)get_zeroed_page(GFP_KERNEL);
68 	if (!buf)
69 		return ERR_PTR(-ENOMEM);
70 
71 	nbytes = min_t(size_t, *count, PAGE_SIZE);
72 	if (copy_from_user(buf, user_buf, nbytes)) {
73 		free_page((unsigned long)buf);
74 		return ERR_PTR(-EFAULT);
75 	}
76 
77 	*count = nbytes;
78 	return buf;
79 }
80 
81 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
82 		       int long_fmt_len)
83 {
84 	char *token;
85 	u32 v[5];
86 	int ret;
87 
88 	token = strsep(line, "\n");
89 	if (!token)
90 		return false;
91 
92 	/*
93 	 * For Adapter/Router configuration space:
94 	 * Short format is: offset value\n
95 	 *		    v[0]   v[1]
96 	 * Long format as produced from the read side:
97 	 * offset relative_offset cap_id vs_cap_id value\n
98 	 * v[0]   v[1]            v[2]   v[3]      v[4]
99 	 *
100 	 * For Counter configuration space:
101 	 * Short format is: offset\n
102 	 *		    v[0]
103 	 * Long format as produced from the read side:
104 	 * offset relative_offset counter_id value\n
105 	 * v[0]   v[1]            v[2]       v[3]
106 	 */
107 	ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
108 	/* In case of Counters, clear counter, "val" content is NA */
109 	if (ret == short_fmt_len) {
110 		*offs = v[0];
111 		*val = v[short_fmt_len - 1];
112 		return true;
113 	} else if (ret == long_fmt_len) {
114 		*offs = v[0];
115 		*val = v[long_fmt_len - 1];
116 		return true;
117 	}
118 
119 	return false;
120 }
121 
122 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
123 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
124 			  const char __user *user_buf, size_t count,
125 			  loff_t *ppos)
126 {
127 	struct tb *tb = sw->tb;
128 	char *line, *buf;
129 	u32 val, offset;
130 	int ret = 0;
131 
132 	buf = validate_and_copy_from_user(user_buf, &count);
133 	if (IS_ERR(buf))
134 		return PTR_ERR(buf);
135 
136 	pm_runtime_get_sync(&sw->dev);
137 
138 	if (mutex_lock_interruptible(&tb->lock)) {
139 		ret = -ERESTARTSYS;
140 		goto out;
141 	}
142 
143 	/* User did hardware changes behind the driver's back */
144 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
145 
146 	line = buf;
147 	while (parse_line(&line, &offset, &val, 2, 5)) {
148 		if (port)
149 			ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
150 		else
151 			ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
152 		if (ret)
153 			break;
154 	}
155 
156 	mutex_unlock(&tb->lock);
157 
158 out:
159 	pm_runtime_mark_last_busy(&sw->dev);
160 	pm_runtime_put_autosuspend(&sw->dev);
161 	free_page((unsigned long)buf);
162 
163 	return ret < 0 ? ret : count;
164 }
165 
166 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
167 			       size_t count, loff_t *ppos)
168 {
169 	struct seq_file *s = file->private_data;
170 	struct tb_port *port = s->private;
171 
172 	return regs_write(port->sw, port, user_buf, count, ppos);
173 }
174 
175 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
176 				 size_t count, loff_t *ppos)
177 {
178 	struct seq_file *s = file->private_data;
179 	struct tb_switch *sw = s->private;
180 
181 	return regs_write(sw, NULL, user_buf, count, ppos);
182 }
183 #define DEBUGFS_MODE		0600
184 #else
185 #define port_regs_write		NULL
186 #define switch_regs_write	NULL
187 #define DEBUGFS_MODE		0400
188 #endif
189 
190 static int port_clear_all_counters(struct tb_port *port)
191 {
192 	u32 *buf;
193 	int ret;
194 
195 	buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
196 		      GFP_KERNEL);
197 	if (!buf)
198 		return -ENOMEM;
199 
200 	ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
201 			    COUNTER_SET_LEN * port->config.max_counters);
202 	kfree(buf);
203 
204 	return ret;
205 }
206 
207 static ssize_t counters_write(struct file *file, const char __user *user_buf,
208 			      size_t count, loff_t *ppos)
209 {
210 	struct seq_file *s = file->private_data;
211 	struct tb_port *port = s->private;
212 	struct tb_switch *sw = port->sw;
213 	struct tb *tb = port->sw->tb;
214 	char *buf;
215 	int ret;
216 
217 	buf = validate_and_copy_from_user(user_buf, &count);
218 	if (IS_ERR(buf))
219 		return PTR_ERR(buf);
220 
221 	pm_runtime_get_sync(&sw->dev);
222 
223 	if (mutex_lock_interruptible(&tb->lock)) {
224 		ret = -ERESTARTSYS;
225 		goto out;
226 	}
227 
228 	/* If written delimiter only, clear all counters in one shot */
229 	if (buf[0] == '\n') {
230 		ret = port_clear_all_counters(port);
231 	} else  {
232 		char *line = buf;
233 		u32 val, offset;
234 
235 		ret = -EINVAL;
236 		while (parse_line(&line, &offset, &val, 1, 4)) {
237 			ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
238 					    offset, 1);
239 			if (ret)
240 				break;
241 		}
242 	}
243 
244 	mutex_unlock(&tb->lock);
245 
246 out:
247 	pm_runtime_mark_last_busy(&sw->dev);
248 	pm_runtime_put_autosuspend(&sw->dev);
249 	free_page((unsigned long)buf);
250 
251 	return ret < 0 ? ret : count;
252 }
253 
254 static void cap_show(struct seq_file *s, struct tb_switch *sw,
255 		     struct tb_port *port, unsigned int cap, u8 cap_id,
256 		     u8 vsec_id, int length)
257 {
258 	int ret, offset = 0;
259 
260 	while (length > 0) {
261 		int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
262 		u32 data[TB_MAX_CONFIG_RW_LENGTH];
263 
264 		if (port)
265 			ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
266 					   dwords);
267 		else
268 			ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
269 		if (ret) {
270 			seq_printf(s, "0x%04x <not accessible>\n",
271 				   cap + offset);
272 			if (dwords > 1)
273 				seq_printf(s, "0x%04x ...\n", cap + offset + 1);
274 			return;
275 		}
276 
277 		for (i = 0; i < dwords; i++) {
278 			seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
279 				   cap + offset + i, offset + i,
280 				   cap_id, vsec_id, data[i]);
281 		}
282 
283 		length -= dwords;
284 		offset += dwords;
285 	}
286 }
287 
288 static void port_cap_show(struct tb_port *port, struct seq_file *s,
289 			  unsigned int cap)
290 {
291 	struct tb_cap_any header;
292 	u8 vsec_id = 0;
293 	size_t length;
294 	int ret;
295 
296 	ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
297 	if (ret) {
298 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
299 		return;
300 	}
301 
302 	switch (header.basic.cap) {
303 	case TB_PORT_CAP_PHY:
304 		length = PORT_CAP_LANE_LEN;
305 		break;
306 
307 	case TB_PORT_CAP_TIME1:
308 		length = PORT_CAP_TMU_LEN;
309 		break;
310 
311 	case TB_PORT_CAP_POWER:
312 		length = PORT_CAP_POWER_LEN;
313 		break;
314 
315 	case TB_PORT_CAP_ADAP:
316 		if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
317 			length = PORT_CAP_PCIE_LEN;
318 		} else if (tb_port_is_dpin(port) || tb_port_is_dpout(port)) {
319 			length = PORT_CAP_DP_LEN;
320 		} else if (tb_port_is_usb3_down(port) ||
321 			   tb_port_is_usb3_up(port)) {
322 			length = PORT_CAP_USB3_LEN;
323 		} else {
324 			seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
325 				   cap, header.basic.cap);
326 			return;
327 		}
328 		break;
329 
330 	case TB_PORT_CAP_VSE:
331 		if (!header.extended_short.length) {
332 			ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
333 					   cap + 1, 1);
334 			if (ret) {
335 				seq_printf(s, "0x%04x <capability read failed>\n",
336 					   cap + 1);
337 				return;
338 			}
339 			length = header.extended_long.length;
340 			vsec_id = header.extended_short.vsec_id;
341 		} else {
342 			length = header.extended_short.length;
343 			vsec_id = header.extended_short.vsec_id;
344 			/*
345 			 * Ice Lake and Tiger Lake do not implement the
346 			 * full length of the capability, only first 32
347 			 * dwords so hard-code it here.
348 			 */
349 			if (!vsec_id &&
350 			    (tb_switch_is_ice_lake(port->sw) ||
351 			     tb_switch_is_tiger_lake(port->sw)))
352 				length = 32;
353 		}
354 		break;
355 
356 	case TB_PORT_CAP_USB4:
357 		length = PORT_CAP_USB4_LEN;
358 		break;
359 
360 	default:
361 		seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
362 			   cap, header.basic.cap);
363 		return;
364 	}
365 
366 	cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
367 }
368 
369 static void port_caps_show(struct tb_port *port, struct seq_file *s)
370 {
371 	int cap;
372 
373 	cap = tb_port_next_cap(port, 0);
374 	while (cap > 0) {
375 		port_cap_show(port, s, cap);
376 		cap = tb_port_next_cap(port, cap);
377 	}
378 }
379 
380 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
381 {
382 	u32 data[PORT_CAP_BASIC_LEN];
383 	int ret, i;
384 
385 	ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
386 	if (ret)
387 		return ret;
388 
389 	for (i = 0; i < ARRAY_SIZE(data); i++)
390 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
391 
392 	return 0;
393 }
394 
395 static int port_regs_show(struct seq_file *s, void *not_used)
396 {
397 	struct tb_port *port = s->private;
398 	struct tb_switch *sw = port->sw;
399 	struct tb *tb = sw->tb;
400 	int ret;
401 
402 	pm_runtime_get_sync(&sw->dev);
403 
404 	if (mutex_lock_interruptible(&tb->lock)) {
405 		ret = -ERESTARTSYS;
406 		goto out_rpm_put;
407 	}
408 
409 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
410 
411 	ret = port_basic_regs_show(port, s);
412 	if (ret)
413 		goto out_unlock;
414 
415 	port_caps_show(port, s);
416 
417 out_unlock:
418 	mutex_unlock(&tb->lock);
419 out_rpm_put:
420 	pm_runtime_mark_last_busy(&sw->dev);
421 	pm_runtime_put_autosuspend(&sw->dev);
422 
423 	return ret;
424 }
425 DEBUGFS_ATTR_RW(port_regs);
426 
427 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
428 			    unsigned int cap)
429 {
430 	struct tb_cap_any header;
431 	int ret, length;
432 	u8 vsec_id = 0;
433 
434 	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
435 	if (ret) {
436 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
437 		return;
438 	}
439 
440 	if (header.basic.cap == TB_SWITCH_CAP_VSE) {
441 		if (!header.extended_short.length) {
442 			ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
443 					 cap + 1, 1);
444 			if (ret) {
445 				seq_printf(s, "0x%04x <capability read failed>\n",
446 					   cap + 1);
447 				return;
448 			}
449 			length = header.extended_long.length;
450 		} else {
451 			length = header.extended_short.length;
452 		}
453 		vsec_id = header.extended_short.vsec_id;
454 	} else {
455 		if (header.basic.cap == TB_SWITCH_CAP_TMU) {
456 			length = SWITCH_CAP_TMU_LEN;
457 		} else  {
458 			seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
459 				   cap, header.basic.cap);
460 			return;
461 		}
462 	}
463 
464 	cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
465 }
466 
467 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
468 {
469 	int cap;
470 
471 	cap = tb_switch_next_cap(sw, 0);
472 	while (cap > 0) {
473 		switch_cap_show(sw, s, cap);
474 		cap = tb_switch_next_cap(sw, cap);
475 	}
476 }
477 
478 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
479 {
480 	u32 data[SWITCH_CAP_BASIC_LEN];
481 	size_t dwords;
482 	int ret, i;
483 
484 	/* Only USB4 has the additional registers */
485 	if (tb_switch_is_usb4(sw))
486 		dwords = ARRAY_SIZE(data);
487 	else
488 		dwords = 7;
489 
490 	ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
491 	if (ret)
492 		return ret;
493 
494 	for (i = 0; i < dwords; i++)
495 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
496 
497 	return 0;
498 }
499 
500 static int switch_regs_show(struct seq_file *s, void *not_used)
501 {
502 	struct tb_switch *sw = s->private;
503 	struct tb *tb = sw->tb;
504 	int ret;
505 
506 	pm_runtime_get_sync(&sw->dev);
507 
508 	if (mutex_lock_interruptible(&tb->lock)) {
509 		ret = -ERESTARTSYS;
510 		goto out_rpm_put;
511 	}
512 
513 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
514 
515 	ret = switch_basic_regs_show(sw, s);
516 	if (ret)
517 		goto out_unlock;
518 
519 	switch_caps_show(sw, s);
520 
521 out_unlock:
522 	mutex_unlock(&tb->lock);
523 out_rpm_put:
524 	pm_runtime_mark_last_busy(&sw->dev);
525 	pm_runtime_put_autosuspend(&sw->dev);
526 
527 	return ret;
528 }
529 DEBUGFS_ATTR_RW(switch_regs);
530 
531 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
532 {
533 	u32 data[PATH_LEN];
534 	int ret, i;
535 
536 	ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
537 			   ARRAY_SIZE(data));
538 	if (ret) {
539 		seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
540 		return ret;
541 	}
542 
543 	for (i = 0; i < ARRAY_SIZE(data); i++) {
544 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
545 			   hopid * PATH_LEN + i, i, hopid, data[i]);
546 	}
547 
548 	return 0;
549 }
550 
551 static int path_show(struct seq_file *s, void *not_used)
552 {
553 	struct tb_port *port = s->private;
554 	struct tb_switch *sw = port->sw;
555 	struct tb *tb = sw->tb;
556 	int start, i, ret = 0;
557 
558 	pm_runtime_get_sync(&sw->dev);
559 
560 	if (mutex_lock_interruptible(&tb->lock)) {
561 		ret = -ERESTARTSYS;
562 		goto out_rpm_put;
563 	}
564 
565 	seq_puts(s, "# offset relative_offset in_hop_id value\n");
566 
567 	/* NHI and lane adapters have entry for path 0 */
568 	if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
569 		ret = path_show_one(port, s, 0);
570 		if (ret)
571 			goto out_unlock;
572 	}
573 
574 	start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
575 
576 	for (i = start; i <= port->config.max_in_hop_id; i++) {
577 		ret = path_show_one(port, s, i);
578 		if (ret)
579 			break;
580 	}
581 
582 out_unlock:
583 	mutex_unlock(&tb->lock);
584 out_rpm_put:
585 	pm_runtime_mark_last_busy(&sw->dev);
586 	pm_runtime_put_autosuspend(&sw->dev);
587 
588 	return ret;
589 }
590 DEBUGFS_ATTR_RO(path);
591 
592 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
593 				 int counter)
594 {
595 	u32 data[COUNTER_SET_LEN];
596 	int ret, i;
597 
598 	ret = tb_port_read(port, data, TB_CFG_COUNTERS,
599 			   counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
600 	if (ret) {
601 		seq_printf(s, "0x%04x <not accessible>\n",
602 			   counter * COUNTER_SET_LEN);
603 		return ret;
604 	}
605 
606 	for (i = 0; i < ARRAY_SIZE(data); i++) {
607 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
608 			   counter * COUNTER_SET_LEN + i, i, counter, data[i]);
609 	}
610 
611 	return 0;
612 }
613 
614 static int counters_show(struct seq_file *s, void *not_used)
615 {
616 	struct tb_port *port = s->private;
617 	struct tb_switch *sw = port->sw;
618 	struct tb *tb = sw->tb;
619 	int i, ret = 0;
620 
621 	pm_runtime_get_sync(&sw->dev);
622 
623 	if (mutex_lock_interruptible(&tb->lock)) {
624 		ret = -ERESTARTSYS;
625 		goto out;
626 	}
627 
628 	seq_puts(s, "# offset relative_offset counter_id value\n");
629 
630 	for (i = 0; i < port->config.max_counters; i++) {
631 		ret = counter_set_regs_show(port, s, i);
632 		if (ret)
633 			break;
634 	}
635 
636 	mutex_unlock(&tb->lock);
637 
638 out:
639 	pm_runtime_mark_last_busy(&sw->dev);
640 	pm_runtime_put_autosuspend(&sw->dev);
641 
642 	return ret;
643 }
644 DEBUGFS_ATTR_RW(counters);
645 
646 /**
647  * tb_switch_debugfs_init() - Add debugfs entries for router
648  * @sw: Pointer to the router
649  *
650  * Adds debugfs directories and files for given router.
651  */
652 void tb_switch_debugfs_init(struct tb_switch *sw)
653 {
654 	struct dentry *debugfs_dir;
655 	struct tb_port *port;
656 
657 	debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
658 	sw->debugfs_dir = debugfs_dir;
659 	debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
660 			    &switch_regs_fops);
661 
662 	tb_switch_for_each_port(sw, port) {
663 		struct dentry *debugfs_dir;
664 		char dir_name[10];
665 
666 		if (port->disabled)
667 			continue;
668 		if (port->config.type == TB_TYPE_INACTIVE)
669 			continue;
670 
671 		snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
672 		debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
673 		debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
674 				    port, &port_regs_fops);
675 		debugfs_create_file("path", 0400, debugfs_dir, port,
676 				    &path_fops);
677 		if (port->config.counters_support)
678 			debugfs_create_file("counters", 0600, debugfs_dir, port,
679 					    &counters_fops);
680 	}
681 }
682 
683 /**
684  * tb_switch_debugfs_remove() - Remove all router debugfs entries
685  * @sw: Pointer to the router
686  *
687  * Removes all previously added debugfs entries under this router.
688  */
689 void tb_switch_debugfs_remove(struct tb_switch *sw)
690 {
691 	debugfs_remove_recursive(sw->debugfs_dir);
692 }
693 
694 void tb_debugfs_init(void)
695 {
696 	tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
697 }
698 
699 void tb_debugfs_exit(void)
700 {
701 	debugfs_remove_recursive(tb_debugfs_root);
702 }
703