0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/debugfs.h>
0011 #include <linux/pm_runtime.h>
0012 #include <linux/uaccess.h>
0013
0014 #include "tb.h"
0015
0016 #define PORT_CAP_PCIE_LEN 1
0017 #define PORT_CAP_POWER_LEN 2
0018 #define PORT_CAP_LANE_LEN 3
0019 #define PORT_CAP_USB3_LEN 5
0020 #define PORT_CAP_DP_LEN 8
0021 #define PORT_CAP_TMU_LEN 8
0022 #define PORT_CAP_BASIC_LEN 9
0023 #define PORT_CAP_USB4_LEN 20
0024
0025 #define SWITCH_CAP_TMU_LEN 26
0026 #define SWITCH_CAP_BASIC_LEN 27
0027
0028 #define PATH_LEN 2
0029
0030 #define COUNTER_SET_LEN 3
0031
0032 #define DEBUGFS_ATTR(__space, __write) \
0033 static int __space ## _open(struct inode *inode, struct file *file) \
0034 { \
0035 return single_open(file, __space ## _show, inode->i_private); \
0036 } \
0037 \
0038 static const struct file_operations __space ## _fops = { \
0039 .owner = THIS_MODULE, \
0040 .open = __space ## _open, \
0041 .release = single_release, \
0042 .read = seq_read, \
0043 .write = __write, \
0044 .llseek = seq_lseek, \
0045 }
0046
0047 #define DEBUGFS_ATTR_RO(__space) \
0048 DEBUGFS_ATTR(__space, NULL)
0049
0050 #define DEBUGFS_ATTR_RW(__space) \
0051 DEBUGFS_ATTR(__space, __space ## _write)
0052
0053 static struct dentry *tb_debugfs_root;
0054
0055 static void *validate_and_copy_from_user(const void __user *user_buf,
0056 size_t *count)
0057 {
0058 size_t nbytes;
0059 void *buf;
0060
0061 if (!*count)
0062 return ERR_PTR(-EINVAL);
0063
0064 if (!access_ok(user_buf, *count))
0065 return ERR_PTR(-EFAULT);
0066
0067 buf = (void *)get_zeroed_page(GFP_KERNEL);
0068 if (!buf)
0069 return ERR_PTR(-ENOMEM);
0070
0071 nbytes = min_t(size_t, *count, PAGE_SIZE);
0072 if (copy_from_user(buf, user_buf, nbytes)) {
0073 free_page((unsigned long)buf);
0074 return ERR_PTR(-EFAULT);
0075 }
0076
0077 *count = nbytes;
0078 return buf;
0079 }
0080
0081 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
0082 int long_fmt_len)
0083 {
0084 char *token;
0085 u32 v[5];
0086 int ret;
0087
0088 token = strsep(line, "\n");
0089 if (!token)
0090 return false;
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
0108
0109 if (ret == short_fmt_len) {
0110 *offs = v[0];
0111 *val = v[short_fmt_len - 1];
0112 return true;
0113 } else if (ret == long_fmt_len) {
0114 *offs = v[0];
0115 *val = v[long_fmt_len - 1];
0116 return true;
0117 }
0118
0119 return false;
0120 }
0121
0122 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
0123 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
0124 const char __user *user_buf, size_t count,
0125 loff_t *ppos)
0126 {
0127 struct tb *tb = sw->tb;
0128 char *line, *buf;
0129 u32 val, offset;
0130 int ret = 0;
0131
0132 buf = validate_and_copy_from_user(user_buf, &count);
0133 if (IS_ERR(buf))
0134 return PTR_ERR(buf);
0135
0136 pm_runtime_get_sync(&sw->dev);
0137
0138 if (mutex_lock_interruptible(&tb->lock)) {
0139 ret = -ERESTARTSYS;
0140 goto out;
0141 }
0142
0143
0144 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
0145
0146 line = buf;
0147 while (parse_line(&line, &offset, &val, 2, 5)) {
0148 if (port)
0149 ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
0150 else
0151 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
0152 if (ret)
0153 break;
0154 }
0155
0156 mutex_unlock(&tb->lock);
0157
0158 out:
0159 pm_runtime_mark_last_busy(&sw->dev);
0160 pm_runtime_put_autosuspend(&sw->dev);
0161 free_page((unsigned long)buf);
0162
0163 return ret < 0 ? ret : count;
0164 }
0165
0166 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
0167 size_t count, loff_t *ppos)
0168 {
0169 struct seq_file *s = file->private_data;
0170 struct tb_port *port = s->private;
0171
0172 return regs_write(port->sw, port, user_buf, count, ppos);
0173 }
0174
0175 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
0176 size_t count, loff_t *ppos)
0177 {
0178 struct seq_file *s = file->private_data;
0179 struct tb_switch *sw = s->private;
0180
0181 return regs_write(sw, NULL, user_buf, count, ppos);
0182 }
0183 #define DEBUGFS_MODE 0600
0184 #else
0185 #define port_regs_write NULL
0186 #define switch_regs_write NULL
0187 #define DEBUGFS_MODE 0400
0188 #endif
0189
0190 static int port_clear_all_counters(struct tb_port *port)
0191 {
0192 u32 *buf;
0193 int ret;
0194
0195 buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
0196 GFP_KERNEL);
0197 if (!buf)
0198 return -ENOMEM;
0199
0200 ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
0201 COUNTER_SET_LEN * port->config.max_counters);
0202 kfree(buf);
0203
0204 return ret;
0205 }
0206
0207 static ssize_t counters_write(struct file *file, const char __user *user_buf,
0208 size_t count, loff_t *ppos)
0209 {
0210 struct seq_file *s = file->private_data;
0211 struct tb_port *port = s->private;
0212 struct tb_switch *sw = port->sw;
0213 struct tb *tb = port->sw->tb;
0214 char *buf;
0215 int ret;
0216
0217 buf = validate_and_copy_from_user(user_buf, &count);
0218 if (IS_ERR(buf))
0219 return PTR_ERR(buf);
0220
0221 pm_runtime_get_sync(&sw->dev);
0222
0223 if (mutex_lock_interruptible(&tb->lock)) {
0224 ret = -ERESTARTSYS;
0225 goto out;
0226 }
0227
0228
0229 if (buf[0] == '\n') {
0230 ret = port_clear_all_counters(port);
0231 } else {
0232 char *line = buf;
0233 u32 val, offset;
0234
0235 ret = -EINVAL;
0236 while (parse_line(&line, &offset, &val, 1, 4)) {
0237 ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
0238 offset, 1);
0239 if (ret)
0240 break;
0241 }
0242 }
0243
0244 mutex_unlock(&tb->lock);
0245
0246 out:
0247 pm_runtime_mark_last_busy(&sw->dev);
0248 pm_runtime_put_autosuspend(&sw->dev);
0249 free_page((unsigned long)buf);
0250
0251 return ret < 0 ? ret : count;
0252 }
0253
0254 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
0255 struct tb_port *port, unsigned int cap,
0256 unsigned int offset, u8 cap_id, u8 vsec_id,
0257 int dwords)
0258 {
0259 int i, ret;
0260 u32 data;
0261
0262 for (i = 0; i < dwords; i++) {
0263 if (port)
0264 ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
0265 else
0266 ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
0267 if (ret) {
0268 seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
0269 continue;
0270 }
0271
0272 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
0273 offset + i, cap_id, vsec_id, data);
0274 }
0275 }
0276
0277 static void cap_show(struct seq_file *s, struct tb_switch *sw,
0278 struct tb_port *port, unsigned int cap, u8 cap_id,
0279 u8 vsec_id, int length)
0280 {
0281 int ret, offset = 0;
0282
0283 while (length > 0) {
0284 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
0285 u32 data[TB_MAX_CONFIG_RW_LENGTH];
0286
0287 if (port)
0288 ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
0289 dwords);
0290 else
0291 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
0292 if (ret) {
0293 cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
0294 return;
0295 }
0296
0297 for (i = 0; i < dwords; i++) {
0298 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
0299 cap + offset + i, offset + i,
0300 cap_id, vsec_id, data[i]);
0301 }
0302
0303 length -= dwords;
0304 offset += dwords;
0305 }
0306 }
0307
0308 static void port_cap_show(struct tb_port *port, struct seq_file *s,
0309 unsigned int cap)
0310 {
0311 struct tb_cap_any header;
0312 u8 vsec_id = 0;
0313 size_t length;
0314 int ret;
0315
0316 ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
0317 if (ret) {
0318 seq_printf(s, "0x%04x <capability read failed>\n", cap);
0319 return;
0320 }
0321
0322 switch (header.basic.cap) {
0323 case TB_PORT_CAP_PHY:
0324 length = PORT_CAP_LANE_LEN;
0325 break;
0326
0327 case TB_PORT_CAP_TIME1:
0328 length = PORT_CAP_TMU_LEN;
0329 break;
0330
0331 case TB_PORT_CAP_POWER:
0332 length = PORT_CAP_POWER_LEN;
0333 break;
0334
0335 case TB_PORT_CAP_ADAP:
0336 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
0337 length = PORT_CAP_PCIE_LEN;
0338 } else if (tb_port_is_dpin(port) || tb_port_is_dpout(port)) {
0339 length = PORT_CAP_DP_LEN;
0340 } else if (tb_port_is_usb3_down(port) ||
0341 tb_port_is_usb3_up(port)) {
0342 length = PORT_CAP_USB3_LEN;
0343 } else {
0344 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
0345 cap, header.basic.cap);
0346 return;
0347 }
0348 break;
0349
0350 case TB_PORT_CAP_VSE:
0351 if (!header.extended_short.length) {
0352 ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
0353 cap + 1, 1);
0354 if (ret) {
0355 seq_printf(s, "0x%04x <capability read failed>\n",
0356 cap + 1);
0357 return;
0358 }
0359 length = header.extended_long.length;
0360 vsec_id = header.extended_short.vsec_id;
0361 } else {
0362 length = header.extended_short.length;
0363 vsec_id = header.extended_short.vsec_id;
0364 }
0365 break;
0366
0367 case TB_PORT_CAP_USB4:
0368 length = PORT_CAP_USB4_LEN;
0369 break;
0370
0371 default:
0372 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
0373 cap, header.basic.cap);
0374 return;
0375 }
0376
0377 cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
0378 }
0379
0380 static void port_caps_show(struct tb_port *port, struct seq_file *s)
0381 {
0382 int cap;
0383
0384 cap = tb_port_next_cap(port, 0);
0385 while (cap > 0) {
0386 port_cap_show(port, s, cap);
0387 cap = tb_port_next_cap(port, cap);
0388 }
0389 }
0390
0391 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
0392 {
0393 u32 data[PORT_CAP_BASIC_LEN];
0394 int ret, i;
0395
0396 ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
0397 if (ret)
0398 return ret;
0399
0400 for (i = 0; i < ARRAY_SIZE(data); i++)
0401 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
0402
0403 return 0;
0404 }
0405
0406 static int port_regs_show(struct seq_file *s, void *not_used)
0407 {
0408 struct tb_port *port = s->private;
0409 struct tb_switch *sw = port->sw;
0410 struct tb *tb = sw->tb;
0411 int ret;
0412
0413 pm_runtime_get_sync(&sw->dev);
0414
0415 if (mutex_lock_interruptible(&tb->lock)) {
0416 ret = -ERESTARTSYS;
0417 goto out_rpm_put;
0418 }
0419
0420 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
0421
0422 ret = port_basic_regs_show(port, s);
0423 if (ret)
0424 goto out_unlock;
0425
0426 port_caps_show(port, s);
0427
0428 out_unlock:
0429 mutex_unlock(&tb->lock);
0430 out_rpm_put:
0431 pm_runtime_mark_last_busy(&sw->dev);
0432 pm_runtime_put_autosuspend(&sw->dev);
0433
0434 return ret;
0435 }
0436 DEBUGFS_ATTR_RW(port_regs);
0437
0438 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
0439 unsigned int cap)
0440 {
0441 struct tb_cap_any header;
0442 int ret, length;
0443 u8 vsec_id = 0;
0444
0445 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
0446 if (ret) {
0447 seq_printf(s, "0x%04x <capability read failed>\n", cap);
0448 return;
0449 }
0450
0451 if (header.basic.cap == TB_SWITCH_CAP_VSE) {
0452 if (!header.extended_short.length) {
0453 ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
0454 cap + 1, 1);
0455 if (ret) {
0456 seq_printf(s, "0x%04x <capability read failed>\n",
0457 cap + 1);
0458 return;
0459 }
0460 length = header.extended_long.length;
0461 } else {
0462 length = header.extended_short.length;
0463 }
0464 vsec_id = header.extended_short.vsec_id;
0465 } else {
0466 if (header.basic.cap == TB_SWITCH_CAP_TMU) {
0467 length = SWITCH_CAP_TMU_LEN;
0468 } else {
0469 seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
0470 cap, header.basic.cap);
0471 return;
0472 }
0473 }
0474
0475 cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
0476 }
0477
0478 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
0479 {
0480 int cap;
0481
0482 cap = tb_switch_next_cap(sw, 0);
0483 while (cap > 0) {
0484 switch_cap_show(sw, s, cap);
0485 cap = tb_switch_next_cap(sw, cap);
0486 }
0487 }
0488
0489 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
0490 {
0491 u32 data[SWITCH_CAP_BASIC_LEN];
0492 size_t dwords;
0493 int ret, i;
0494
0495
0496 if (tb_switch_is_usb4(sw))
0497 dwords = ARRAY_SIZE(data);
0498 else
0499 dwords = 7;
0500
0501 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
0502 if (ret)
0503 return ret;
0504
0505 for (i = 0; i < dwords; i++)
0506 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
0507
0508 return 0;
0509 }
0510
0511 static int switch_regs_show(struct seq_file *s, void *not_used)
0512 {
0513 struct tb_switch *sw = s->private;
0514 struct tb *tb = sw->tb;
0515 int ret;
0516
0517 pm_runtime_get_sync(&sw->dev);
0518
0519 if (mutex_lock_interruptible(&tb->lock)) {
0520 ret = -ERESTARTSYS;
0521 goto out_rpm_put;
0522 }
0523
0524 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
0525
0526 ret = switch_basic_regs_show(sw, s);
0527 if (ret)
0528 goto out_unlock;
0529
0530 switch_caps_show(sw, s);
0531
0532 out_unlock:
0533 mutex_unlock(&tb->lock);
0534 out_rpm_put:
0535 pm_runtime_mark_last_busy(&sw->dev);
0536 pm_runtime_put_autosuspend(&sw->dev);
0537
0538 return ret;
0539 }
0540 DEBUGFS_ATTR_RW(switch_regs);
0541
0542 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
0543 {
0544 u32 data[PATH_LEN];
0545 int ret, i;
0546
0547 ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
0548 ARRAY_SIZE(data));
0549 if (ret) {
0550 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
0551 return ret;
0552 }
0553
0554 for (i = 0; i < ARRAY_SIZE(data); i++) {
0555 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
0556 hopid * PATH_LEN + i, i, hopid, data[i]);
0557 }
0558
0559 return 0;
0560 }
0561
0562 static int path_show(struct seq_file *s, void *not_used)
0563 {
0564 struct tb_port *port = s->private;
0565 struct tb_switch *sw = port->sw;
0566 struct tb *tb = sw->tb;
0567 int start, i, ret = 0;
0568
0569 pm_runtime_get_sync(&sw->dev);
0570
0571 if (mutex_lock_interruptible(&tb->lock)) {
0572 ret = -ERESTARTSYS;
0573 goto out_rpm_put;
0574 }
0575
0576 seq_puts(s, "# offset relative_offset in_hop_id value\n");
0577
0578
0579 if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
0580 ret = path_show_one(port, s, 0);
0581 if (ret)
0582 goto out_unlock;
0583 }
0584
0585 start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
0586
0587 for (i = start; i <= port->config.max_in_hop_id; i++) {
0588 ret = path_show_one(port, s, i);
0589 if (ret)
0590 break;
0591 }
0592
0593 out_unlock:
0594 mutex_unlock(&tb->lock);
0595 out_rpm_put:
0596 pm_runtime_mark_last_busy(&sw->dev);
0597 pm_runtime_put_autosuspend(&sw->dev);
0598
0599 return ret;
0600 }
0601 DEBUGFS_ATTR_RO(path);
0602
0603 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
0604 int counter)
0605 {
0606 u32 data[COUNTER_SET_LEN];
0607 int ret, i;
0608
0609 ret = tb_port_read(port, data, TB_CFG_COUNTERS,
0610 counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
0611 if (ret) {
0612 seq_printf(s, "0x%04x <not accessible>\n",
0613 counter * COUNTER_SET_LEN);
0614 return ret;
0615 }
0616
0617 for (i = 0; i < ARRAY_SIZE(data); i++) {
0618 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
0619 counter * COUNTER_SET_LEN + i, i, counter, data[i]);
0620 }
0621
0622 return 0;
0623 }
0624
0625 static int counters_show(struct seq_file *s, void *not_used)
0626 {
0627 struct tb_port *port = s->private;
0628 struct tb_switch *sw = port->sw;
0629 struct tb *tb = sw->tb;
0630 int i, ret = 0;
0631
0632 pm_runtime_get_sync(&sw->dev);
0633
0634 if (mutex_lock_interruptible(&tb->lock)) {
0635 ret = -ERESTARTSYS;
0636 goto out;
0637 }
0638
0639 seq_puts(s, "# offset relative_offset counter_id value\n");
0640
0641 for (i = 0; i < port->config.max_counters; i++) {
0642 ret = counter_set_regs_show(port, s, i);
0643 if (ret)
0644 break;
0645 }
0646
0647 mutex_unlock(&tb->lock);
0648
0649 out:
0650 pm_runtime_mark_last_busy(&sw->dev);
0651 pm_runtime_put_autosuspend(&sw->dev);
0652
0653 return ret;
0654 }
0655 DEBUGFS_ATTR_RW(counters);
0656
0657
0658
0659
0660
0661
0662
0663 void tb_switch_debugfs_init(struct tb_switch *sw)
0664 {
0665 struct dentry *debugfs_dir;
0666 struct tb_port *port;
0667
0668 debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
0669 sw->debugfs_dir = debugfs_dir;
0670 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
0671 &switch_regs_fops);
0672
0673 tb_switch_for_each_port(sw, port) {
0674 struct dentry *debugfs_dir;
0675 char dir_name[10];
0676
0677 if (port->disabled)
0678 continue;
0679 if (port->config.type == TB_TYPE_INACTIVE)
0680 continue;
0681
0682 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
0683 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
0684 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
0685 port, &port_regs_fops);
0686 debugfs_create_file("path", 0400, debugfs_dir, port,
0687 &path_fops);
0688 if (port->config.counters_support)
0689 debugfs_create_file("counters", 0600, debugfs_dir, port,
0690 &counters_fops);
0691 }
0692 }
0693
0694
0695
0696
0697
0698
0699
0700 void tb_switch_debugfs_remove(struct tb_switch *sw)
0701 {
0702 debugfs_remove_recursive(sw->debugfs_dir);
0703 }
0704
0705
0706
0707
0708
0709
0710
0711 void tb_service_debugfs_init(struct tb_service *svc)
0712 {
0713 svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
0714 tb_debugfs_root);
0715 }
0716
0717
0718
0719
0720
0721
0722
0723 void tb_service_debugfs_remove(struct tb_service *svc)
0724 {
0725 debugfs_remove_recursive(svc->debugfs_dir);
0726 svc->debugfs_dir = NULL;
0727 }
0728
0729 void tb_debugfs_init(void)
0730 {
0731 tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
0732 }
0733
0734 void tb_debugfs_exit(void)
0735 {
0736 debugfs_remove_recursive(tb_debugfs_root);
0737 }