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