]> Git Repo - linux.git/blob - drivers/thunderbolt/debugfs.c
x86/kaslr: Expose and use the end of the physical memory address space
[linux.git] / drivers / thunderbolt / debugfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Debugfs interface
4  *
5  * Copyright (C) 2020, Intel Corporation
6  * Authors: Gil Fine <[email protected]>
7  *          Mika Westerberg <[email protected]>
8  */
9
10 #include <linux/bitfield.h>
11 #include <linux/debugfs.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/uaccess.h>
14
15 #include "tb.h"
16 #include "sb_regs.h"
17
18 #define PORT_CAP_V1_PCIE_LEN    1
19 #define PORT_CAP_V2_PCIE_LEN    2
20 #define PORT_CAP_POWER_LEN      2
21 #define PORT_CAP_LANE_LEN       3
22 #define PORT_CAP_USB3_LEN       5
23 #define PORT_CAP_DP_V1_LEN      9
24 #define PORT_CAP_DP_V2_LEN      14
25 #define PORT_CAP_TMU_V1_LEN     8
26 #define PORT_CAP_TMU_V2_LEN     10
27 #define PORT_CAP_BASIC_LEN      9
28 #define PORT_CAP_USB4_LEN       20
29
30 #define SWITCH_CAP_TMU_LEN      26
31 #define SWITCH_CAP_BASIC_LEN    27
32
33 #define PATH_LEN                2
34
35 #define COUNTER_SET_LEN         3
36
37 /* Sideband registers and their sizes as defined in the USB4 spec */
38 struct sb_reg {
39         unsigned int reg;
40         unsigned int size;
41 };
42
43 #define SB_MAX_SIZE             64
44
45 /* Sideband registers for router */
46 static const struct sb_reg port_sb_regs[] = {
47         { USB4_SB_VENDOR_ID, 4 },
48         { USB4_SB_PRODUCT_ID, 4 },
49         { USB4_SB_DEBUG_CONF, 4 },
50         { USB4_SB_DEBUG, 54 },
51         { USB4_SB_LRD_TUNING, 4 },
52         { USB4_SB_OPCODE, 4 },
53         { USB4_SB_METADATA, 4 },
54         { USB4_SB_LINK_CONF, 3 },
55         { USB4_SB_GEN23_TXFFE, 4 },
56         { USB4_SB_GEN4_TXFFE, 4 },
57         { USB4_SB_VERSION, 4 },
58         { USB4_SB_DATA, 64 },
59 };
60
61 /* Sideband registers for retimer */
62 static const struct sb_reg retimer_sb_regs[] = {
63         { USB4_SB_VENDOR_ID, 4 },
64         { USB4_SB_PRODUCT_ID, 4 },
65         { USB4_SB_FW_VERSION, 4 },
66         { USB4_SB_LRD_TUNING, 4 },
67         { USB4_SB_OPCODE, 4 },
68         { USB4_SB_METADATA, 4 },
69         { USB4_SB_GEN23_TXFFE, 4 },
70         { USB4_SB_GEN4_TXFFE, 4 },
71         { USB4_SB_VERSION, 4 },
72         { USB4_SB_DATA, 64 },
73 };
74
75 #define DEBUGFS_ATTR(__space, __write)                                  \
76 static int __space ## _open(struct inode *inode, struct file *file)     \
77 {                                                                       \
78         return single_open(file, __space ## _show, inode->i_private);   \
79 }                                                                       \
80                                                                         \
81 static const struct file_operations __space ## _fops = {                \
82         .owner = THIS_MODULE,                                           \
83         .open = __space ## _open,                                       \
84         .release = single_release,                                      \
85         .read  = seq_read,                                              \
86         .write = __write,                                               \
87         .llseek = seq_lseek,                                            \
88 }
89
90 #define DEBUGFS_ATTR_RO(__space)                                        \
91         DEBUGFS_ATTR(__space, NULL)
92
93 #define DEBUGFS_ATTR_RW(__space)                                        \
94         DEBUGFS_ATTR(__space, __space ## _write)
95
96 static struct dentry *tb_debugfs_root;
97
98 static void *validate_and_copy_from_user(const void __user *user_buf,
99                                          size_t *count)
100 {
101         size_t nbytes;
102         void *buf;
103
104         if (!*count)
105                 return ERR_PTR(-EINVAL);
106
107         if (!access_ok(user_buf, *count))
108                 return ERR_PTR(-EFAULT);
109
110         buf = (void *)get_zeroed_page(GFP_KERNEL);
111         if (!buf)
112                 return ERR_PTR(-ENOMEM);
113
114         nbytes = min_t(size_t, *count, PAGE_SIZE);
115         if (copy_from_user(buf, user_buf, nbytes)) {
116                 free_page((unsigned long)buf);
117                 return ERR_PTR(-EFAULT);
118         }
119
120         *count = nbytes;
121         return buf;
122 }
123
124 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
125                        int long_fmt_len)
126 {
127         char *token;
128         u32 v[5];
129         int ret;
130
131         token = strsep(line, "\n");
132         if (!token)
133                 return false;
134
135         /*
136          * For Adapter/Router configuration space:
137          * Short format is: offset value\n
138          *                  v[0]   v[1]
139          * Long format as produced from the read side:
140          * offset relative_offset cap_id vs_cap_id value\n
141          * v[0]   v[1]            v[2]   v[3]      v[4]
142          *
143          * For Counter configuration space:
144          * Short format is: offset\n
145          *                  v[0]
146          * Long format as produced from the read side:
147          * offset relative_offset counter_id value\n
148          * v[0]   v[1]            v[2]       v[3]
149          */
150         ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
151         /* In case of Counters, clear counter, "val" content is NA */
152         if (ret == short_fmt_len) {
153                 *offs = v[0];
154                 *val = v[short_fmt_len - 1];
155                 return true;
156         } else if (ret == long_fmt_len) {
157                 *offs = v[0];
158                 *val = v[long_fmt_len - 1];
159                 return true;
160         }
161
162         return false;
163 }
164
165 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
166 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
167                           const char __user *user_buf, size_t count,
168                           loff_t *ppos)
169 {
170         struct tb *tb = sw->tb;
171         char *line, *buf;
172         u32 val, offset;
173         int ret = 0;
174
175         buf = validate_and_copy_from_user(user_buf, &count);
176         if (IS_ERR(buf))
177                 return PTR_ERR(buf);
178
179         pm_runtime_get_sync(&sw->dev);
180
181         if (mutex_lock_interruptible(&tb->lock)) {
182                 ret = -ERESTARTSYS;
183                 goto out;
184         }
185
186         /* User did hardware changes behind the driver's back */
187         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
188
189         line = buf;
190         while (parse_line(&line, &offset, &val, 2, 5)) {
191                 if (port)
192                         ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
193                 else
194                         ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
195                 if (ret)
196                         break;
197         }
198
199         mutex_unlock(&tb->lock);
200
201 out:
202         pm_runtime_mark_last_busy(&sw->dev);
203         pm_runtime_put_autosuspend(&sw->dev);
204         free_page((unsigned long)buf);
205
206         return ret < 0 ? ret : count;
207 }
208
209 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
210                                size_t count, loff_t *ppos)
211 {
212         struct seq_file *s = file->private_data;
213         struct tb_port *port = s->private;
214
215         return regs_write(port->sw, port, user_buf, count, ppos);
216 }
217
218 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
219                                  size_t count, loff_t *ppos)
220 {
221         struct seq_file *s = file->private_data;
222         struct tb_switch *sw = s->private;
223
224         return regs_write(sw, NULL, user_buf, count, ppos);
225 }
226
227 static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size,
228                           size_t *bytes_read)
229 {
230         char *field, *token;
231         int i;
232
233         token = strsep(line, "\n");
234         if (!token)
235                 return false;
236
237         /* Parse the register first */
238         field = strsep(&token, " ");
239         if (!field)
240                 return false;
241         if (kstrtou8(field, 0, reg))
242                 return false;
243
244         /* Then the values for the register, up to data_size */
245         for (i = 0; i < data_size; i++) {
246                 field = strsep(&token, " ");
247                 if (!field)
248                         break;
249                 if (kstrtou8(field, 0, &data[i]))
250                         return false;
251         }
252
253         *bytes_read = i;
254         return true;
255 }
256
257 static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs,
258                              size_t size, enum usb4_sb_target target, u8 index,
259                              char *buf, size_t count, loff_t *ppos)
260 {
261         u8 reg, data[SB_MAX_SIZE];
262         size_t bytes_read;
263         char *line = buf;
264
265         /* User did hardware changes behind the driver's back */
266         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
267
268         /*
269          * For sideband registers we accept:
270          * reg b0 b1 b2...\n
271          *
272          * Here "reg" is the byte offset of the sideband register and "b0"..
273          * are the byte values. There can be less byte values than the register
274          * size. The leftovers will not be overwritten.
275          */
276         while (parse_sb_line(&line, &reg, data, ARRAY_SIZE(data), &bytes_read)) {
277                 const struct sb_reg *sb_reg;
278                 int ret;
279
280                 /* At least one byte must be passed */
281                 if (bytes_read < 1)
282                         return -EINVAL;
283
284                 /* Find the register */
285                 sb_reg = NULL;
286                 for (int i = 0; i < size; i++) {
287                         if (sb_regs[i].reg == reg) {
288                                 sb_reg = &sb_regs[i];
289                                 break;
290                         }
291                 }
292
293                 if (!sb_reg)
294                         return -EINVAL;
295
296                 if (bytes_read > sb_regs->size)
297                         return -E2BIG;
298
299                 ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data,
300                                          bytes_read);
301                 if (ret)
302                         return ret;
303         }
304
305         return 0;
306 }
307
308 static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf,
309                                   size_t count, loff_t *ppos)
310 {
311         struct seq_file *s = file->private_data;
312         struct tb_port *port = s->private;
313         struct tb_switch *sw = port->sw;
314         struct tb *tb = sw->tb;
315         char *buf;
316         int ret;
317
318         buf = validate_and_copy_from_user(user_buf, &count);
319         if (IS_ERR(buf))
320                 return PTR_ERR(buf);
321
322         pm_runtime_get_sync(&sw->dev);
323
324         if (mutex_lock_interruptible(&tb->lock)) {
325                 ret = -ERESTARTSYS;
326                 goto out_rpm_put;
327         }
328
329         ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
330                             USB4_SB_TARGET_ROUTER, 0, buf, count, ppos);
331
332         mutex_unlock(&tb->lock);
333 out_rpm_put:
334         pm_runtime_mark_last_busy(&sw->dev);
335         pm_runtime_put_autosuspend(&sw->dev);
336
337         return ret < 0 ? ret : count;
338 }
339
340 static ssize_t retimer_sb_regs_write(struct file *file,
341                                      const char __user *user_buf,
342                                      size_t count, loff_t *ppos)
343 {
344         struct seq_file *s = file->private_data;
345         struct tb_retimer *rt = s->private;
346         struct tb *tb = rt->tb;
347         char *buf;
348         int ret;
349
350         buf = validate_and_copy_from_user(user_buf, &count);
351         if (IS_ERR(buf))
352                 return PTR_ERR(buf);
353
354         pm_runtime_get_sync(&rt->dev);
355
356         if (mutex_lock_interruptible(&tb->lock)) {
357                 ret = -ERESTARTSYS;
358                 goto out_rpm_put;
359         }
360
361         ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
362                             USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos);
363
364         mutex_unlock(&tb->lock);
365 out_rpm_put:
366         pm_runtime_mark_last_busy(&rt->dev);
367         pm_runtime_put_autosuspend(&rt->dev);
368
369         return ret < 0 ? ret : count;
370 }
371 #define DEBUGFS_MODE            0600
372 #else
373 #define port_regs_write         NULL
374 #define switch_regs_write       NULL
375 #define port_sb_regs_write      NULL
376 #define retimer_sb_regs_write   NULL
377 #define DEBUGFS_MODE            0400
378 #endif
379
380 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
381 /**
382  * struct tb_margining - Lane margining support
383  * @port: USB4 port through which the margining operations are run
384  * @target: Sideband target
385  * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
386  * @dev: Pointer to the device that is the target (USB4 port or retimer)
387  * @caps: Port lane margining capabilities
388  * @results: Last lane margining results
389  * @lanes: %0, %1 or %7 (all)
390  * @min_ber_level: Minimum supported BER level contour value
391  * @max_ber_level: Maximum supported BER level contour value
392  * @ber_level: Current BER level contour value
393  * @voltage_steps: Number of mandatory voltage steps
394  * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
395  * @time_steps: Number of time margin steps
396  * @max_time_offset: Maximum time margin offset (in mUI)
397  * @software: %true if software margining is used instead of hardware
398  * @time: %true if time margining is used instead of voltage
399  * @right_high: %false if left/low margin test is performed, %true if
400  *              right/high
401  */
402 struct tb_margining {
403         struct tb_port *port;
404         enum usb4_sb_target target;
405         u8 index;
406         struct device *dev;
407         u32 caps[2];
408         u32 results[2];
409         unsigned int lanes;
410         unsigned int min_ber_level;
411         unsigned int max_ber_level;
412         unsigned int ber_level;
413         unsigned int voltage_steps;
414         unsigned int max_voltage_offset;
415         unsigned int time_steps;
416         unsigned int max_time_offset;
417         bool software;
418         bool time;
419         bool right_high;
420 };
421
422 static bool supports_software(const struct tb_margining *margining)
423 {
424         return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
425 }
426
427 static bool supports_hardware(const struct tb_margining *margining)
428 {
429         return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
430 }
431
432 static bool both_lanes(const struct tb_margining *margining)
433 {
434         return margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
435 }
436
437 static unsigned int
438 independent_voltage_margins(const struct tb_margining *margining)
439 {
440         return FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK, margining->caps[0]);
441 }
442
443 static bool supports_time(const struct tb_margining *margining)
444 {
445         return margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
446 }
447
448 /* Only applicable if supports_time() returns true */
449 static unsigned int
450 independent_time_margins(const struct tb_margining *margining)
451 {
452         return FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK, margining->caps[1]);
453 }
454
455 static ssize_t
456 margining_ber_level_write(struct file *file, const char __user *user_buf,
457                            size_t count, loff_t *ppos)
458 {
459         struct seq_file *s = file->private_data;
460         struct tb_margining *margining = s->private;
461         struct tb *tb = margining->port->sw->tb;
462         unsigned int val;
463         int ret = 0;
464         char *buf;
465
466         if (mutex_lock_interruptible(&tb->lock))
467                 return -ERESTARTSYS;
468
469         if (margining->software) {
470                 ret = -EINVAL;
471                 goto out_unlock;
472         }
473
474         buf = validate_and_copy_from_user(user_buf, &count);
475         if (IS_ERR(buf)) {
476                 ret = PTR_ERR(buf);
477                 goto out_unlock;
478         }
479
480         buf[count - 1] = '\0';
481
482         ret = kstrtouint(buf, 10, &val);
483         if (ret)
484                 goto out_free;
485
486         if (val < margining->min_ber_level ||
487             val > margining->max_ber_level) {
488                 ret = -EINVAL;
489                 goto out_free;
490         }
491
492         margining->ber_level = val;
493
494 out_free:
495         free_page((unsigned long)buf);
496 out_unlock:
497         mutex_unlock(&tb->lock);
498
499         return ret < 0 ? ret : count;
500 }
501
502 static void ber_level_show(struct seq_file *s, unsigned int val)
503 {
504         if (val % 2)
505                 seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
506         else
507                 seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
508 }
509
510 static int margining_ber_level_show(struct seq_file *s, void *not_used)
511 {
512         const struct tb_margining *margining = s->private;
513
514         if (margining->software)
515                 return -EINVAL;
516         ber_level_show(s, margining->ber_level);
517         return 0;
518 }
519 DEBUGFS_ATTR_RW(margining_ber_level);
520
521 static int margining_caps_show(struct seq_file *s, void *not_used)
522 {
523         struct tb_margining *margining = s->private;
524         struct tb *tb = margining->port->sw->tb;
525         u32 cap0, cap1;
526
527         if (mutex_lock_interruptible(&tb->lock))
528                 return -ERESTARTSYS;
529
530         /* Dump the raw caps first */
531         cap0 = margining->caps[0];
532         seq_printf(s, "0x%08x\n", cap0);
533         cap1 = margining->caps[1];
534         seq_printf(s, "0x%08x\n", cap1);
535
536         seq_printf(s, "# software margining: %s\n",
537                    supports_software(margining) ? "yes" : "no");
538         if (supports_hardware(margining)) {
539                 seq_puts(s, "# hardware margining: yes\n");
540                 seq_puts(s, "# minimum BER level contour: ");
541                 ber_level_show(s, margining->min_ber_level);
542                 seq_puts(s, "# maximum BER level contour: ");
543                 ber_level_show(s, margining->max_ber_level);
544         } else {
545                 seq_puts(s, "# hardware margining: no\n");
546         }
547
548         seq_printf(s, "# both lanes simultaneously: %s\n",
549                   both_lanes(margining) ? "yes" : "no");
550         seq_printf(s, "# voltage margin steps: %u\n",
551                    margining->voltage_steps);
552         seq_printf(s, "# maximum voltage offset: %u mV\n",
553                    margining->max_voltage_offset);
554
555         switch (independent_voltage_margins(margining)) {
556         case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
557                 seq_puts(s, "# returns minimum between high and low voltage margins\n");
558                 break;
559         case USB4_MARGIN_CAP_0_VOLTAGE_HL:
560                 seq_puts(s, "# returns high or low voltage margin\n");
561                 break;
562         case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
563                 seq_puts(s, "# returns both high and low margins\n");
564                 break;
565         }
566
567         if (supports_time(margining)) {
568                 seq_puts(s, "# time margining: yes\n");
569                 seq_printf(s, "# time margining is destructive: %s\n",
570                            cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
571
572                 switch (independent_time_margins(margining)) {
573                 case USB4_MARGIN_CAP_1_TIME_MIN:
574                         seq_puts(s, "# returns minimum between left and right time margins\n");
575                         break;
576                 case USB4_MARGIN_CAP_1_TIME_LR:
577                         seq_puts(s, "# returns left or right margin\n");
578                         break;
579                 case USB4_MARGIN_CAP_1_TIME_BOTH:
580                         seq_puts(s, "# returns both left and right margins\n");
581                         break;
582                 }
583
584                 seq_printf(s, "# time margin steps: %u\n",
585                            margining->time_steps);
586                 seq_printf(s, "# maximum time offset: %u mUI\n",
587                            margining->max_time_offset);
588         } else {
589                 seq_puts(s, "# time margining: no\n");
590         }
591
592         mutex_unlock(&tb->lock);
593         return 0;
594 }
595 DEBUGFS_ATTR_RO(margining_caps);
596
597 static ssize_t
598 margining_lanes_write(struct file *file, const char __user *user_buf,
599                       size_t count, loff_t *ppos)
600 {
601         struct seq_file *s = file->private_data;
602         struct tb_margining *margining = s->private;
603         struct tb *tb = margining->port->sw->tb;
604         int ret = 0;
605         char *buf;
606
607         buf = validate_and_copy_from_user(user_buf, &count);
608         if (IS_ERR(buf))
609                 return PTR_ERR(buf);
610
611         buf[count - 1] = '\0';
612
613         if (mutex_lock_interruptible(&tb->lock)) {
614                 ret = -ERESTARTSYS;
615                 goto out_free;
616         }
617
618         if (!strcmp(buf, "0")) {
619                 margining->lanes = 0;
620         } else if (!strcmp(buf, "1")) {
621                 margining->lanes = 1;
622         } else if (!strcmp(buf, "all")) {
623                 /* Needs to be supported */
624                 if (both_lanes(margining))
625                         margining->lanes = 7;
626                 else
627                         ret = -EINVAL;
628         } else {
629                 ret = -EINVAL;
630         }
631
632         mutex_unlock(&tb->lock);
633
634 out_free:
635         free_page((unsigned long)buf);
636         return ret < 0 ? ret : count;
637 }
638
639 static int margining_lanes_show(struct seq_file *s, void *not_used)
640 {
641         struct tb_margining *margining = s->private;
642         struct tb *tb = margining->port->sw->tb;
643         unsigned int lanes;
644
645         if (mutex_lock_interruptible(&tb->lock))
646                 return -ERESTARTSYS;
647
648         lanes = margining->lanes;
649         if (both_lanes(margining)) {
650                 if (!lanes)
651                         seq_puts(s, "[0] 1 all\n");
652                 else if (lanes == 1)
653                         seq_puts(s, "0 [1] all\n");
654                 else
655                         seq_puts(s, "0 1 [all]\n");
656         } else {
657                 if (!lanes)
658                         seq_puts(s, "[0] 1\n");
659                 else
660                         seq_puts(s, "0 [1]\n");
661         }
662
663         mutex_unlock(&tb->lock);
664         return 0;
665 }
666 DEBUGFS_ATTR_RW(margining_lanes);
667
668 static ssize_t margining_mode_write(struct file *file,
669                                    const char __user *user_buf,
670                                    size_t count, loff_t *ppos)
671 {
672         struct seq_file *s = file->private_data;
673         struct tb_margining *margining = s->private;
674         struct tb *tb = margining->port->sw->tb;
675         int ret = 0;
676         char *buf;
677
678         buf = validate_and_copy_from_user(user_buf, &count);
679         if (IS_ERR(buf))
680                 return PTR_ERR(buf);
681
682         buf[count - 1] = '\0';
683
684         if (mutex_lock_interruptible(&tb->lock)) {
685                 ret = -ERESTARTSYS;
686                 goto out_free;
687         }
688
689         if (!strcmp(buf, "software")) {
690                 if (supports_software(margining))
691                         margining->software = true;
692                 else
693                         ret = -EINVAL;
694         } else if (!strcmp(buf, "hardware")) {
695                 if (supports_hardware(margining))
696                         margining->software = false;
697                 else
698                         ret = -EINVAL;
699         } else {
700                 ret = -EINVAL;
701         }
702
703         mutex_unlock(&tb->lock);
704
705 out_free:
706         free_page((unsigned long)buf);
707         return ret ? ret : count;
708 }
709
710 static int margining_mode_show(struct seq_file *s, void *not_used)
711 {
712         struct tb_margining *margining = s->private;
713         struct tb *tb = margining->port->sw->tb;
714         const char *space = "";
715
716         if (mutex_lock_interruptible(&tb->lock))
717                 return -ERESTARTSYS;
718
719         if (supports_software(margining)) {
720                 if (margining->software)
721                         seq_puts(s, "[software]");
722                 else
723                         seq_puts(s, "software");
724                 space = " ";
725         }
726         if (supports_hardware(margining)) {
727                 if (margining->software)
728                         seq_printf(s, "%shardware", space);
729                 else
730                         seq_printf(s, "%s[hardware]", space);
731         }
732
733         mutex_unlock(&tb->lock);
734
735         seq_puts(s, "\n");
736         return 0;
737 }
738 DEBUGFS_ATTR_RW(margining_mode);
739
740 static int margining_run_write(void *data, u64 val)
741 {
742         struct tb_margining *margining = data;
743         struct tb_port *port = margining->port;
744         struct device *dev = margining->dev;
745         struct tb_switch *sw = port->sw;
746         struct tb_switch *down_sw;
747         struct tb *tb = sw->tb;
748         int ret, clx;
749
750         if (val != 1)
751                 return -EINVAL;
752
753         pm_runtime_get_sync(dev);
754
755         if (mutex_lock_interruptible(&tb->lock)) {
756                 ret = -ERESTARTSYS;
757                 goto out_rpm_put;
758         }
759
760         if (tb_is_upstream_port(port))
761                 down_sw = sw;
762         else if (port->remote)
763                 down_sw = port->remote->sw;
764         else
765                 down_sw = NULL;
766
767         if (down_sw) {
768                 /*
769                  * CL states may interfere with lane margining so
770                  * disable them temporarily now.
771                  */
772                 ret = tb_switch_clx_disable(down_sw);
773                 if (ret < 0) {
774                         tb_sw_warn(down_sw, "failed to disable CL states\n");
775                         goto out_unlock;
776                 }
777                 clx = ret;
778         }
779
780         if (margining->software) {
781                 tb_port_dbg(port,
782                             "running software %s lane margining for %s lanes %u\n",
783                             margining->time ? "time" : "voltage", dev_name(dev),
784                             margining->lanes);
785                 ret = usb4_port_sw_margin(port, margining->target, margining->index,
786                                           margining->lanes, margining->time,
787                                           margining->right_high,
788                                           USB4_MARGIN_SW_COUNTER_CLEAR);
789                 if (ret)
790                         goto out_clx;
791
792                 ret = usb4_port_sw_margin_errors(port, margining->target,
793                                                  margining->index,
794                                                  &margining->results[0]);
795         } else {
796                 tb_port_dbg(port,
797                             "running hardware %s lane margining for %s lanes %u\n",
798                             margining->time ? "time" : "voltage", dev_name(dev),
799                             margining->lanes);
800                 /* Clear the results */
801                 margining->results[0] = 0;
802                 margining->results[1] = 0;
803                 ret = usb4_port_hw_margin(port, margining->target, margining->index,
804                                           margining->lanes, margining->ber_level,
805                                           margining->time, margining->right_high,
806                                           margining->results);
807         }
808
809 out_clx:
810         if (down_sw)
811                 tb_switch_clx_enable(down_sw, clx);
812 out_unlock:
813         mutex_unlock(&tb->lock);
814 out_rpm_put:
815         pm_runtime_mark_last_busy(dev);
816         pm_runtime_put_autosuspend(dev);
817
818         return ret;
819 }
820 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
821                          "%llu\n");
822
823 static ssize_t margining_results_write(struct file *file,
824                                        const char __user *user_buf,
825                                        size_t count, loff_t *ppos)
826 {
827         struct seq_file *s = file->private_data;
828         struct tb_margining *margining = s->private;
829         struct tb *tb = margining->port->sw->tb;
830
831         if (mutex_lock_interruptible(&tb->lock))
832                 return -ERESTARTSYS;
833
834         /* Just clear the results */
835         margining->results[0] = 0;
836         margining->results[1] = 0;
837
838         mutex_unlock(&tb->lock);
839         return count;
840 }
841
842 static void voltage_margin_show(struct seq_file *s,
843                                 const struct tb_margining *margining, u8 val)
844 {
845         unsigned int tmp, voltage;
846
847         tmp = FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK, val);
848         voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
849         seq_printf(s, "%u mV (%u)", voltage, tmp);
850         if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
851                 seq_puts(s, " exceeds maximum");
852         seq_puts(s, "\n");
853 }
854
855 static void time_margin_show(struct seq_file *s,
856                              const struct tb_margining *margining, u8 val)
857 {
858         unsigned int tmp, interval;
859
860         tmp = FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK, val);
861         interval = tmp * margining->max_time_offset / margining->time_steps;
862         seq_printf(s, "%u mUI (%u)", interval, tmp);
863         if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
864                 seq_puts(s, " exceeds maximum");
865         seq_puts(s, "\n");
866 }
867
868 static int margining_results_show(struct seq_file *s, void *not_used)
869 {
870         struct tb_margining *margining = s->private;
871         struct tb *tb = margining->port->sw->tb;
872
873         if (mutex_lock_interruptible(&tb->lock))
874                 return -ERESTARTSYS;
875
876         /* Dump the raw results first */
877         seq_printf(s, "0x%08x\n", margining->results[0]);
878         /* Only the hardware margining has two result dwords */
879         if (!margining->software) {
880                 unsigned int val;
881
882                 seq_printf(s, "0x%08x\n", margining->results[1]);
883
884                 if (margining->time) {
885                         if (!margining->lanes || margining->lanes == 7) {
886                                 val = margining->results[1];
887                                 seq_puts(s, "# lane 0 right time margin: ");
888                                 time_margin_show(s, margining, val);
889                                 val = margining->results[1] >>
890                                         USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
891                                 seq_puts(s, "# lane 0 left time margin: ");
892                                 time_margin_show(s, margining, val);
893                         }
894                         if (margining->lanes == 1 || margining->lanes == 7) {
895                                 val = margining->results[1] >>
896                                         USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
897                                 seq_puts(s, "# lane 1 right time margin: ");
898                                 time_margin_show(s, margining, val);
899                                 val = margining->results[1] >>
900                                         USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
901                                 seq_puts(s, "# lane 1 left time margin: ");
902                                 time_margin_show(s, margining, val);
903                         }
904                 } else {
905                         if (!margining->lanes || margining->lanes == 7) {
906                                 val = margining->results[1];
907                                 seq_puts(s, "# lane 0 high voltage margin: ");
908                                 voltage_margin_show(s, margining, val);
909                                 val = margining->results[1] >>
910                                         USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
911                                 seq_puts(s, "# lane 0 low voltage margin: ");
912                                 voltage_margin_show(s, margining, val);
913                         }
914                         if (margining->lanes == 1 || margining->lanes == 7) {
915                                 val = margining->results[1] >>
916                                         USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
917                                 seq_puts(s, "# lane 1 high voltage margin: ");
918                                 voltage_margin_show(s, margining, val);
919                                 val = margining->results[1] >>
920                                         USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
921                                 seq_puts(s, "# lane 1 low voltage margin: ");
922                                 voltage_margin_show(s, margining, val);
923                         }
924                 }
925         }
926
927         mutex_unlock(&tb->lock);
928         return 0;
929 }
930 DEBUGFS_ATTR_RW(margining_results);
931
932 static ssize_t margining_test_write(struct file *file,
933                                     const char __user *user_buf,
934                                     size_t count, loff_t *ppos)
935 {
936         struct seq_file *s = file->private_data;
937         struct tb_margining *margining = s->private;
938         struct tb *tb = margining->port->sw->tb;
939         int ret = 0;
940         char *buf;
941
942         buf = validate_and_copy_from_user(user_buf, &count);
943         if (IS_ERR(buf))
944                 return PTR_ERR(buf);
945
946         buf[count - 1] = '\0';
947
948         if (mutex_lock_interruptible(&tb->lock)) {
949                 ret = -ERESTARTSYS;
950                 goto out_free;
951         }
952
953         if (!strcmp(buf, "time") && supports_time(margining))
954                 margining->time = true;
955         else if (!strcmp(buf, "voltage"))
956                 margining->time = false;
957         else
958                 ret = -EINVAL;
959
960         mutex_unlock(&tb->lock);
961
962 out_free:
963         free_page((unsigned long)buf);
964         return ret ? ret : count;
965 }
966
967 static int margining_test_show(struct seq_file *s, void *not_used)
968 {
969         struct tb_margining *margining = s->private;
970         struct tb *tb = margining->port->sw->tb;
971
972         if (mutex_lock_interruptible(&tb->lock))
973                 return -ERESTARTSYS;
974
975         if (supports_time(margining)) {
976                 if (margining->time)
977                         seq_puts(s, "voltage [time]\n");
978                 else
979                         seq_puts(s, "[voltage] time\n");
980         } else {
981                 seq_puts(s, "[voltage]\n");
982         }
983
984         mutex_unlock(&tb->lock);
985         return 0;
986 }
987 DEBUGFS_ATTR_RW(margining_test);
988
989 static ssize_t margining_margin_write(struct file *file,
990                                     const char __user *user_buf,
991                                     size_t count, loff_t *ppos)
992 {
993         struct seq_file *s = file->private_data;
994         struct tb_margining *margining = s->private;
995         struct tb *tb = margining->port->sw->tb;
996         int ret = 0;
997         char *buf;
998
999         buf = validate_and_copy_from_user(user_buf, &count);
1000         if (IS_ERR(buf))
1001                 return PTR_ERR(buf);
1002
1003         buf[count - 1] = '\0';
1004
1005         if (mutex_lock_interruptible(&tb->lock)) {
1006                 ret = -ERESTARTSYS;
1007                 goto out_free;
1008         }
1009
1010         if (margining->time) {
1011                 if (!strcmp(buf, "left"))
1012                         margining->right_high = false;
1013                 else if (!strcmp(buf, "right"))
1014                         margining->right_high = true;
1015                 else
1016                         ret = -EINVAL;
1017         } else {
1018                 if (!strcmp(buf, "low"))
1019                         margining->right_high = false;
1020                 else if (!strcmp(buf, "high"))
1021                         margining->right_high = true;
1022                 else
1023                         ret = -EINVAL;
1024         }
1025
1026         mutex_unlock(&tb->lock);
1027
1028 out_free:
1029         free_page((unsigned long)buf);
1030         return ret ? ret : count;
1031 }
1032
1033 static int margining_margin_show(struct seq_file *s, void *not_used)
1034 {
1035         struct tb_margining *margining = s->private;
1036         struct tb *tb = margining->port->sw->tb;
1037
1038         if (mutex_lock_interruptible(&tb->lock))
1039                 return -ERESTARTSYS;
1040
1041         if (margining->time) {
1042                 if (margining->right_high)
1043                         seq_puts(s, "left [right]\n");
1044                 else
1045                         seq_puts(s, "[left] right\n");
1046         } else {
1047                 if (margining->right_high)
1048                         seq_puts(s, "low [high]\n");
1049                 else
1050                         seq_puts(s, "[low] high\n");
1051         }
1052
1053         mutex_unlock(&tb->lock);
1054         return 0;
1055 }
1056 DEBUGFS_ATTR_RW(margining_margin);
1057
1058 static struct tb_margining *margining_alloc(struct tb_port *port,
1059                                             struct device *dev,
1060                                             enum usb4_sb_target target,
1061                                             u8 index, struct dentry *parent)
1062 {
1063         struct tb_margining *margining;
1064         struct dentry *dir;
1065         unsigned int val;
1066         int ret;
1067
1068         margining = kzalloc(sizeof(*margining), GFP_KERNEL);
1069         if (!margining)
1070                 return NULL;
1071
1072         margining->port = port;
1073         margining->target = target;
1074         margining->index = index;
1075         margining->dev = dev;
1076
1077         ret = usb4_port_margining_caps(port, target, index, margining->caps);
1078         if (ret) {
1079                 kfree(margining);
1080                 return NULL;
1081         }
1082
1083         /* Set the initial mode */
1084         if (supports_software(margining))
1085                 margining->software = true;
1086
1087         val = FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK, margining->caps[0]);
1088         margining->voltage_steps = val;
1089         val = FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK, margining->caps[0]);
1090         margining->max_voltage_offset = 74 + val * 2;
1091
1092         if (supports_time(margining)) {
1093                 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK, margining->caps[1]);
1094                 margining->time_steps = val;
1095                 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK, margining->caps[1]);
1096                 /*
1097                  * Store it as mUI (milli Unit Interval) because we want
1098                  * to keep it as integer.
1099                  */
1100                 margining->max_time_offset = 200 + 10 * val;
1101         }
1102
1103         dir = debugfs_create_dir("margining", parent);
1104         if (supports_hardware(margining)) {
1105                 val = FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK, margining->caps[1]);
1106                 margining->min_ber_level = val;
1107                 val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK, margining->caps[1]);
1108                 margining->max_ber_level = val;
1109
1110                 /* Set the default to minimum */
1111                 margining->ber_level = margining->min_ber_level;
1112
1113                 debugfs_create_file("ber_level_contour", 0400, dir, margining,
1114                                     &margining_ber_level_fops);
1115         }
1116         debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops);
1117         debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops);
1118         debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops);
1119         debugfs_create_file("run", 0600, dir, margining, &margining_run_fops);
1120         debugfs_create_file("results", 0600, dir, margining,
1121                             &margining_results_fops);
1122         debugfs_create_file("test", 0600, dir, margining, &margining_test_fops);
1123         if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_0_VOLTAGE_HL ||
1124             (supports_time(margining) &&
1125              independent_time_margins(margining) == USB4_MARGIN_CAP_1_TIME_LR))
1126                 debugfs_create_file("margin", 0600, dir, margining,
1127                                     &margining_margin_fops);
1128         return margining;
1129 }
1130
1131 static void margining_port_init(struct tb_port *port)
1132 {
1133         struct dentry *parent;
1134         char dir_name[10];
1135
1136         if (!port->usb4)
1137                 return;
1138
1139         snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1140         parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1141         port->usb4->margining = margining_alloc(port, &port->usb4->dev,
1142                                                 USB4_SB_TARGET_ROUTER, 0,
1143                                                 parent);
1144 }
1145
1146 static void margining_port_remove(struct tb_port *port)
1147 {
1148         struct dentry *parent;
1149         char dir_name[10];
1150
1151         if (!port->usb4)
1152                 return;
1153
1154         snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1155         parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1156         if (parent)
1157                 debugfs_lookup_and_remove("margining", parent);
1158
1159         kfree(port->usb4->margining);
1160         port->usb4->margining = NULL;
1161 }
1162
1163 static void margining_switch_init(struct tb_switch *sw)
1164 {
1165         struct tb_port *upstream, *downstream;
1166         struct tb_switch *parent_sw;
1167         u64 route = tb_route(sw);
1168
1169         if (!route)
1170                 return;
1171
1172         upstream = tb_upstream_port(sw);
1173         parent_sw = tb_switch_parent(sw);
1174         downstream = tb_port_at(route, parent_sw);
1175
1176         margining_port_init(downstream);
1177         margining_port_init(upstream);
1178 }
1179
1180 static void margining_switch_remove(struct tb_switch *sw)
1181 {
1182         struct tb_port *upstream, *downstream;
1183         struct tb_switch *parent_sw;
1184         u64 route = tb_route(sw);
1185
1186         if (!route)
1187                 return;
1188
1189         upstream = tb_upstream_port(sw);
1190         parent_sw = tb_switch_parent(sw);
1191         downstream = tb_port_at(route, parent_sw);
1192
1193         margining_port_remove(upstream);
1194         margining_port_remove(downstream);
1195 }
1196
1197 static void margining_xdomain_init(struct tb_xdomain *xd)
1198 {
1199         struct tb_switch *parent_sw;
1200         struct tb_port *downstream;
1201
1202         parent_sw = tb_xdomain_parent(xd);
1203         downstream = tb_port_at(xd->route, parent_sw);
1204
1205         margining_port_init(downstream);
1206 }
1207
1208 static void margining_xdomain_remove(struct tb_xdomain *xd)
1209 {
1210         struct tb_switch *parent_sw;
1211         struct tb_port *downstream;
1212
1213         parent_sw = tb_xdomain_parent(xd);
1214         downstream = tb_port_at(xd->route, parent_sw);
1215         margining_port_remove(downstream);
1216 }
1217
1218 static void margining_retimer_init(struct tb_retimer *rt, struct dentry *debugfs_dir)
1219 {
1220         rt->margining = margining_alloc(rt->port, &rt->dev,
1221                                         USB4_SB_TARGET_RETIMER, rt->index,
1222                                         debugfs_dir);
1223 }
1224
1225 static void margining_retimer_remove(struct tb_retimer *rt)
1226 {
1227         kfree(rt->margining);
1228         rt->margining = NULL;
1229 }
1230 #else
1231 static inline void margining_switch_init(struct tb_switch *sw) { }
1232 static inline void margining_switch_remove(struct tb_switch *sw) { }
1233 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1234 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1235 static inline void margining_retimer_init(struct tb_retimer *rt,
1236                                           struct dentry *debugfs_dir) { }
1237 static inline void margining_retimer_remove(struct tb_retimer *rt) { }
1238 #endif
1239
1240 static int port_clear_all_counters(struct tb_port *port)
1241 {
1242         u32 *buf;
1243         int ret;
1244
1245         buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1246                       GFP_KERNEL);
1247         if (!buf)
1248                 return -ENOMEM;
1249
1250         ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1251                             COUNTER_SET_LEN * port->config.max_counters);
1252         kfree(buf);
1253
1254         return ret;
1255 }
1256
1257 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1258                               size_t count, loff_t *ppos)
1259 {
1260         struct seq_file *s = file->private_data;
1261         struct tb_port *port = s->private;
1262         struct tb_switch *sw = port->sw;
1263         struct tb *tb = port->sw->tb;
1264         char *buf;
1265         int ret;
1266
1267         buf = validate_and_copy_from_user(user_buf, &count);
1268         if (IS_ERR(buf))
1269                 return PTR_ERR(buf);
1270
1271         pm_runtime_get_sync(&sw->dev);
1272
1273         if (mutex_lock_interruptible(&tb->lock)) {
1274                 ret = -ERESTARTSYS;
1275                 goto out;
1276         }
1277
1278         /* If written delimiter only, clear all counters in one shot */
1279         if (buf[0] == '\n') {
1280                 ret = port_clear_all_counters(port);
1281         } else  {
1282                 char *line = buf;
1283                 u32 val, offset;
1284
1285                 ret = -EINVAL;
1286                 while (parse_line(&line, &offset, &val, 1, 4)) {
1287                         ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1288                                             offset, 1);
1289                         if (ret)
1290                                 break;
1291                 }
1292         }
1293
1294         mutex_unlock(&tb->lock);
1295
1296 out:
1297         pm_runtime_mark_last_busy(&sw->dev);
1298         pm_runtime_put_autosuspend(&sw->dev);
1299         free_page((unsigned long)buf);
1300
1301         return ret < 0 ? ret : count;
1302 }
1303
1304 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1305                            struct tb_port *port, unsigned int cap,
1306                            unsigned int offset, u8 cap_id, u8 vsec_id,
1307                            int dwords)
1308 {
1309         int i, ret;
1310         u32 data;
1311
1312         for (i = 0; i < dwords; i++) {
1313                 if (port)
1314                         ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1315                 else
1316                         ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1317                 if (ret) {
1318                         seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1319                         continue;
1320                 }
1321
1322                 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1323                            offset + i, cap_id, vsec_id, data);
1324         }
1325 }
1326
1327 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1328                      struct tb_port *port, unsigned int cap, u8 cap_id,
1329                      u8 vsec_id, int length)
1330 {
1331         int ret, offset = 0;
1332
1333         while (length > 0) {
1334                 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1335                 u32 data[TB_MAX_CONFIG_RW_LENGTH];
1336
1337                 if (port)
1338                         ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1339                                            dwords);
1340                 else
1341                         ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1342                 if (ret) {
1343                         cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1344                         return;
1345                 }
1346
1347                 for (i = 0; i < dwords; i++) {
1348                         seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1349                                    cap + offset + i, offset + i,
1350                                    cap_id, vsec_id, data[i]);
1351                 }
1352
1353                 length -= dwords;
1354                 offset += dwords;
1355         }
1356 }
1357
1358 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1359                           unsigned int cap)
1360 {
1361         struct tb_cap_any header;
1362         u8 vsec_id = 0;
1363         size_t length;
1364         int ret;
1365
1366         ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1367         if (ret) {
1368                 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1369                 return;
1370         }
1371
1372         switch (header.basic.cap) {
1373         case TB_PORT_CAP_PHY:
1374                 length = PORT_CAP_LANE_LEN;
1375                 break;
1376
1377         case TB_PORT_CAP_TIME1:
1378                 if (usb4_switch_version(port->sw) < 2)
1379                         length = PORT_CAP_TMU_V1_LEN;
1380                 else
1381                         length = PORT_CAP_TMU_V2_LEN;
1382                 break;
1383
1384         case TB_PORT_CAP_POWER:
1385                 length = PORT_CAP_POWER_LEN;
1386                 break;
1387
1388         case TB_PORT_CAP_ADAP:
1389                 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1390                         if (usb4_switch_version(port->sw) < 2)
1391                                 length = PORT_CAP_V1_PCIE_LEN;
1392                         else
1393                                 length = PORT_CAP_V2_PCIE_LEN;
1394                 } else if (tb_port_is_dpin(port)) {
1395                         if (usb4_switch_version(port->sw) < 2)
1396                                 length = PORT_CAP_DP_V1_LEN;
1397                         else
1398                                 length = PORT_CAP_DP_V2_LEN;
1399                 } else if (tb_port_is_dpout(port)) {
1400                         length = PORT_CAP_DP_V1_LEN;
1401                 } else if (tb_port_is_usb3_down(port) ||
1402                            tb_port_is_usb3_up(port)) {
1403                         length = PORT_CAP_USB3_LEN;
1404                 } else {
1405                         seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1406                                    cap, header.basic.cap);
1407                         return;
1408                 }
1409                 break;
1410
1411         case TB_PORT_CAP_VSE:
1412                 if (!header.extended_short.length) {
1413                         ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1414                                            cap + 1, 1);
1415                         if (ret) {
1416                                 seq_printf(s, "0x%04x <capability read failed>\n",
1417                                            cap + 1);
1418                                 return;
1419                         }
1420                         length = header.extended_long.length;
1421                         vsec_id = header.extended_short.vsec_id;
1422                 } else {
1423                         length = header.extended_short.length;
1424                         vsec_id = header.extended_short.vsec_id;
1425                 }
1426                 break;
1427
1428         case TB_PORT_CAP_USB4:
1429                 length = PORT_CAP_USB4_LEN;
1430                 break;
1431
1432         default:
1433                 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1434                            cap, header.basic.cap);
1435                 return;
1436         }
1437
1438         cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1439 }
1440
1441 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1442 {
1443         int cap;
1444
1445         cap = tb_port_next_cap(port, 0);
1446         while (cap > 0) {
1447                 port_cap_show(port, s, cap);
1448                 cap = tb_port_next_cap(port, cap);
1449         }
1450 }
1451
1452 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1453 {
1454         u32 data[PORT_CAP_BASIC_LEN];
1455         int ret, i;
1456
1457         ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1458         if (ret)
1459                 return ret;
1460
1461         for (i = 0; i < ARRAY_SIZE(data); i++)
1462                 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1463
1464         return 0;
1465 }
1466
1467 static int port_regs_show(struct seq_file *s, void *not_used)
1468 {
1469         struct tb_port *port = s->private;
1470         struct tb_switch *sw = port->sw;
1471         struct tb *tb = sw->tb;
1472         int ret;
1473
1474         pm_runtime_get_sync(&sw->dev);
1475
1476         if (mutex_lock_interruptible(&tb->lock)) {
1477                 ret = -ERESTARTSYS;
1478                 goto out_rpm_put;
1479         }
1480
1481         seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1482
1483         ret = port_basic_regs_show(port, s);
1484         if (ret)
1485                 goto out_unlock;
1486
1487         port_caps_show(port, s);
1488
1489 out_unlock:
1490         mutex_unlock(&tb->lock);
1491 out_rpm_put:
1492         pm_runtime_mark_last_busy(&sw->dev);
1493         pm_runtime_put_autosuspend(&sw->dev);
1494
1495         return ret;
1496 }
1497 DEBUGFS_ATTR_RW(port_regs);
1498
1499 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1500                             unsigned int cap)
1501 {
1502         struct tb_cap_any header;
1503         int ret, length;
1504         u8 vsec_id = 0;
1505
1506         ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1507         if (ret) {
1508                 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1509                 return;
1510         }
1511
1512         if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1513                 if (!header.extended_short.length) {
1514                         ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1515                                          cap + 1, 1);
1516                         if (ret) {
1517                                 seq_printf(s, "0x%04x <capability read failed>\n",
1518                                            cap + 1);
1519                                 return;
1520                         }
1521                         length = header.extended_long.length;
1522                 } else {
1523                         length = header.extended_short.length;
1524                 }
1525                 vsec_id = header.extended_short.vsec_id;
1526         } else {
1527                 if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1528                         length = SWITCH_CAP_TMU_LEN;
1529                 } else  {
1530                         seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1531                                    cap, header.basic.cap);
1532                         return;
1533                 }
1534         }
1535
1536         cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1537 }
1538
1539 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1540 {
1541         int cap;
1542
1543         cap = tb_switch_next_cap(sw, 0);
1544         while (cap > 0) {
1545                 switch_cap_show(sw, s, cap);
1546                 cap = tb_switch_next_cap(sw, cap);
1547         }
1548 }
1549
1550 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1551 {
1552         u32 data[SWITCH_CAP_BASIC_LEN];
1553         size_t dwords;
1554         int ret, i;
1555
1556         /* Only USB4 has the additional registers */
1557         if (tb_switch_is_usb4(sw))
1558                 dwords = ARRAY_SIZE(data);
1559         else
1560                 dwords = 5;
1561
1562         ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1563         if (ret)
1564                 return ret;
1565
1566         for (i = 0; i < dwords; i++)
1567                 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1568
1569         return 0;
1570 }
1571
1572 static int switch_regs_show(struct seq_file *s, void *not_used)
1573 {
1574         struct tb_switch *sw = s->private;
1575         struct tb *tb = sw->tb;
1576         int ret;
1577
1578         pm_runtime_get_sync(&sw->dev);
1579
1580         if (mutex_lock_interruptible(&tb->lock)) {
1581                 ret = -ERESTARTSYS;
1582                 goto out_rpm_put;
1583         }
1584
1585         seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1586
1587         ret = switch_basic_regs_show(sw, s);
1588         if (ret)
1589                 goto out_unlock;
1590
1591         switch_caps_show(sw, s);
1592
1593 out_unlock:
1594         mutex_unlock(&tb->lock);
1595 out_rpm_put:
1596         pm_runtime_mark_last_busy(&sw->dev);
1597         pm_runtime_put_autosuspend(&sw->dev);
1598
1599         return ret;
1600 }
1601 DEBUGFS_ATTR_RW(switch_regs);
1602
1603 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1604 {
1605         u32 data[PATH_LEN];
1606         int ret, i;
1607
1608         ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1609                            ARRAY_SIZE(data));
1610         if (ret) {
1611                 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1612                 return ret;
1613         }
1614
1615         for (i = 0; i < ARRAY_SIZE(data); i++) {
1616                 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1617                            hopid * PATH_LEN + i, i, hopid, data[i]);
1618         }
1619
1620         return 0;
1621 }
1622
1623 static int path_show(struct seq_file *s, void *not_used)
1624 {
1625         struct tb_port *port = s->private;
1626         struct tb_switch *sw = port->sw;
1627         struct tb *tb = sw->tb;
1628         int start, i, ret = 0;
1629
1630         pm_runtime_get_sync(&sw->dev);
1631
1632         if (mutex_lock_interruptible(&tb->lock)) {
1633                 ret = -ERESTARTSYS;
1634                 goto out_rpm_put;
1635         }
1636
1637         seq_puts(s, "# offset relative_offset in_hop_id value\n");
1638
1639         /* NHI and lane adapters have entry for path 0 */
1640         if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1641                 ret = path_show_one(port, s, 0);
1642                 if (ret)
1643                         goto out_unlock;
1644         }
1645
1646         start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1647
1648         for (i = start; i <= port->config.max_in_hop_id; i++) {
1649                 ret = path_show_one(port, s, i);
1650                 if (ret)
1651                         break;
1652         }
1653
1654 out_unlock:
1655         mutex_unlock(&tb->lock);
1656 out_rpm_put:
1657         pm_runtime_mark_last_busy(&sw->dev);
1658         pm_runtime_put_autosuspend(&sw->dev);
1659
1660         return ret;
1661 }
1662 DEBUGFS_ATTR_RO(path);
1663
1664 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1665                                  int counter)
1666 {
1667         u32 data[COUNTER_SET_LEN];
1668         int ret, i;
1669
1670         ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1671                            counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1672         if (ret) {
1673                 seq_printf(s, "0x%04x <not accessible>\n",
1674                            counter * COUNTER_SET_LEN);
1675                 return ret;
1676         }
1677
1678         for (i = 0; i < ARRAY_SIZE(data); i++) {
1679                 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1680                            counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1681         }
1682
1683         return 0;
1684 }
1685
1686 static int counters_show(struct seq_file *s, void *not_used)
1687 {
1688         struct tb_port *port = s->private;
1689         struct tb_switch *sw = port->sw;
1690         struct tb *tb = sw->tb;
1691         int i, ret = 0;
1692
1693         pm_runtime_get_sync(&sw->dev);
1694
1695         if (mutex_lock_interruptible(&tb->lock)) {
1696                 ret = -ERESTARTSYS;
1697                 goto out;
1698         }
1699
1700         seq_puts(s, "# offset relative_offset counter_id value\n");
1701
1702         for (i = 0; i < port->config.max_counters; i++) {
1703                 ret = counter_set_regs_show(port, s, i);
1704                 if (ret)
1705                         break;
1706         }
1707
1708         mutex_unlock(&tb->lock);
1709
1710 out:
1711         pm_runtime_mark_last_busy(&sw->dev);
1712         pm_runtime_put_autosuspend(&sw->dev);
1713
1714         return ret;
1715 }
1716 DEBUGFS_ATTR_RW(counters);
1717
1718 static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs,
1719                         size_t size, enum usb4_sb_target target, u8 index,
1720                         struct seq_file *s)
1721 {
1722         int ret, i;
1723
1724         seq_puts(s, "# register value\n");
1725
1726         for (i = 0; i < size; i++) {
1727                 const struct sb_reg *regs = &sb_regs[i];
1728                 u8 data[64];
1729                 int j;
1730
1731                 memset(data, 0, sizeof(data));
1732                 ret = usb4_port_sb_read(port, target, index, regs->reg, data,
1733                                         regs->size);
1734                 if (ret)
1735                         return ret;
1736
1737                 seq_printf(s, "0x%02x", regs->reg);
1738                 for (j = 0; j < regs->size; j++)
1739                         seq_printf(s, " 0x%02x", data[j]);
1740                 seq_puts(s, "\n");
1741         }
1742
1743         return 0;
1744 }
1745
1746 static int port_sb_regs_show(struct seq_file *s, void *not_used)
1747 {
1748         struct tb_port *port = s->private;
1749         struct tb_switch *sw = port->sw;
1750         struct tb *tb = sw->tb;
1751         int ret;
1752
1753         pm_runtime_get_sync(&sw->dev);
1754
1755         if (mutex_lock_interruptible(&tb->lock)) {
1756                 ret = -ERESTARTSYS;
1757                 goto out_rpm_put;
1758         }
1759
1760         ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
1761                            USB4_SB_TARGET_ROUTER, 0, s);
1762
1763         mutex_unlock(&tb->lock);
1764 out_rpm_put:
1765         pm_runtime_mark_last_busy(&sw->dev);
1766         pm_runtime_put_autosuspend(&sw->dev);
1767
1768         return ret;
1769 }
1770 DEBUGFS_ATTR_RW(port_sb_regs);
1771
1772 /**
1773  * tb_switch_debugfs_init() - Add debugfs entries for router
1774  * @sw: Pointer to the router
1775  *
1776  * Adds debugfs directories and files for given router.
1777  */
1778 void tb_switch_debugfs_init(struct tb_switch *sw)
1779 {
1780         struct dentry *debugfs_dir;
1781         struct tb_port *port;
1782
1783         debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1784         sw->debugfs_dir = debugfs_dir;
1785         debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1786                             &switch_regs_fops);
1787
1788         tb_switch_for_each_port(sw, port) {
1789                 struct dentry *debugfs_dir;
1790                 char dir_name[10];
1791
1792                 if (port->disabled)
1793                         continue;
1794                 if (port->config.type == TB_TYPE_INACTIVE)
1795                         continue;
1796
1797                 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1798                 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1799                 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1800                                     port, &port_regs_fops);
1801                 debugfs_create_file("path", 0400, debugfs_dir, port,
1802                                     &path_fops);
1803                 if (port->config.counters_support)
1804                         debugfs_create_file("counters", 0600, debugfs_dir, port,
1805                                             &counters_fops);
1806                 if (port->usb4)
1807                         debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
1808                                             port, &port_sb_regs_fops);
1809         }
1810
1811         margining_switch_init(sw);
1812 }
1813
1814 /**
1815  * tb_switch_debugfs_remove() - Remove all router debugfs entries
1816  * @sw: Pointer to the router
1817  *
1818  * Removes all previously added debugfs entries under this router.
1819  */
1820 void tb_switch_debugfs_remove(struct tb_switch *sw)
1821 {
1822         margining_switch_remove(sw);
1823         debugfs_remove_recursive(sw->debugfs_dir);
1824 }
1825
1826 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1827 {
1828         margining_xdomain_init(xd);
1829 }
1830
1831 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1832 {
1833         margining_xdomain_remove(xd);
1834 }
1835
1836 /**
1837  * tb_service_debugfs_init() - Add debugfs directory for service
1838  * @svc: Thunderbolt service pointer
1839  *
1840  * Adds debugfs directory for service.
1841  */
1842 void tb_service_debugfs_init(struct tb_service *svc)
1843 {
1844         svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1845                                               tb_debugfs_root);
1846 }
1847
1848 /**
1849  * tb_service_debugfs_remove() - Remove service debugfs directory
1850  * @svc: Thunderbolt service pointer
1851  *
1852  * Removes the previously created debugfs directory for @svc.
1853  */
1854 void tb_service_debugfs_remove(struct tb_service *svc)
1855 {
1856         debugfs_remove_recursive(svc->debugfs_dir);
1857         svc->debugfs_dir = NULL;
1858 }
1859
1860 static int retimer_sb_regs_show(struct seq_file *s, void *not_used)
1861 {
1862         struct tb_retimer *rt = s->private;
1863         struct tb *tb = rt->tb;
1864         int ret;
1865
1866         pm_runtime_get_sync(&rt->dev);
1867
1868         if (mutex_lock_interruptible(&tb->lock)) {
1869                 ret = -ERESTARTSYS;
1870                 goto out_rpm_put;
1871         }
1872
1873         ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
1874                            USB4_SB_TARGET_RETIMER, rt->index, s);
1875
1876         mutex_unlock(&tb->lock);
1877 out_rpm_put:
1878         pm_runtime_mark_last_busy(&rt->dev);
1879         pm_runtime_put_autosuspend(&rt->dev);
1880
1881         return ret;
1882 }
1883 DEBUGFS_ATTR_RW(retimer_sb_regs);
1884
1885 /**
1886  * tb_retimer_debugfs_init() - Add debugfs directory for retimer
1887  * @rt: Pointer to retimer structure
1888  *
1889  * Adds and populates retimer debugfs directory.
1890  */
1891 void tb_retimer_debugfs_init(struct tb_retimer *rt)
1892 {
1893         struct dentry *debugfs_dir;
1894
1895         debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root);
1896         debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt,
1897                             &retimer_sb_regs_fops);
1898         margining_retimer_init(rt, debugfs_dir);
1899 }
1900
1901 /**
1902  * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
1903  * @rt: Pointer to retimer structure
1904  *
1905  * Removes the retimer debugfs directory along with its contents.
1906  */
1907 void tb_retimer_debugfs_remove(struct tb_retimer *rt)
1908 {
1909         debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root);
1910         margining_retimer_remove(rt);
1911 }
1912
1913 void tb_debugfs_init(void)
1914 {
1915         tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1916 }
1917
1918 void tb_debugfs_exit(void)
1919 {
1920         debugfs_remove_recursive(tb_debugfs_root);
1921 }
This page took 0.141196 seconds and 4 git commands to generate.