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