| 1 | |
| 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/array_size.h>
|
| 11 | #include <linux/bitfield.h>
|
| 12 | #include <linux/debugfs.h>
|
| 13 | #include <linux/delay.h>
|
| 14 | #include <linux/pm_runtime.h>
|
| 15 | #include <linux/uaccess.h>
|
| 16 |
|
| 17 | #include "tb.h"
|
| 18 | #include "sb_regs.h"
|
| 19 |
|
| 20 | #define PORT_CAP_V1_PCIE_LEN 1
|
| 21 | #define PORT_CAP_V2_PCIE_LEN 2
|
| 22 | #define PORT_CAP_POWER_LEN 2
|
| 23 | #define PORT_CAP_LANE_LEN 3
|
| 24 | #define PORT_CAP_USB3_LEN 5
|
| 25 | #define PORT_CAP_DP_V1_LEN 9
|
| 26 | #define PORT_CAP_DP_V2_LEN 14
|
| 27 | #define PORT_CAP_TMU_V1_LEN 8
|
| 28 | #define PORT_CAP_TMU_V2_LEN 10
|
| 29 | #define PORT_CAP_BASIC_LEN 9
|
| 30 | #define PORT_CAP_USB4_LEN 20
|
| 31 |
|
| 32 | #define SWITCH_CAP_TMU_LEN 26
|
| 33 | #define SWITCH_CAP_BASIC_LEN 27
|
| 34 |
|
| 35 | #define PATH_LEN 2
|
| 36 |
|
| 37 | #define COUNTER_SET_LEN 3
|
| 38 |
|
| 39 | |
| 40 | * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms
|
| 41 | * probed to give good results.
|
| 42 |
|
| 43 | #define MIN_DWELL_TIME 100
|
| 44 | #define MAX_DWELL_TIME 500
|
| 45 | #define DWELL_SAMPLE_INTERVAL 10
|
| 46 |
|
| 47 | enum usb4_margin_cap_voltage_indp {
|
| 48 | USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN,
|
| 49 | USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL,
|
| 50 | USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH,
|
| 51 | USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN,
|
| 52 | USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH,
|
| 53 | USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN,
|
| 54 | };
|
| 55 |
|
| 56 | enum usb4_margin_cap_time_indp {
|
| 57 | USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN,
|
| 58 | USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR,
|
| 59 | USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH,
|
| 60 | USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN,
|
| 61 | USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH,
|
| 62 | USB4_MARGIN_CAP_TIME_INDP_UNKNOWN,
|
| 63 | };
|
| 64 |
|
| 65 |
|
| 66 | struct sb_reg {
|
| 67 | unsigned int reg;
|
| 68 | unsigned int size;
|
| 69 | };
|
| 70 |
|
| 71 | #define SB_MAX_SIZE 64
|
| 72 |
|
| 73 |
|
| 74 | static const struct sb_reg port_sb_regs[] = {
|
| 75 | { USB4_SB_VENDOR_ID, 4 },
|
| 76 | { USB4_SB_PRODUCT_ID, 4 },
|
| 77 | { USB4_SB_DEBUG_CONF, 4 },
|
| 78 | { USB4_SB_DEBUG, 54 },
|
| 79 | { USB4_SB_LRD_TUNING, 4 },
|
| 80 | { USB4_SB_OPCODE, 4 },
|
| 81 | { USB4_SB_METADATA, 4 },
|
| 82 | { USB4_SB_LINK_CONF, 3 },
|
| 83 | { USB4_SB_GEN23_TXFFE, 4 },
|
| 84 | { USB4_SB_GEN4_TXFFE, 4 },
|
| 85 | { USB4_SB_VERSION, 4 },
|
| 86 | { USB4_SB_DATA, 64 },
|
| 87 | };
|
| 88 |
|
| 89 |
|
| 90 | static const struct sb_reg retimer_sb_regs[] = {
|
| 91 | { USB4_SB_VENDOR_ID, 4 },
|
| 92 | { USB4_SB_PRODUCT_ID, 4 },
|
| 93 | { USB4_SB_FW_VERSION, 4 },
|
| 94 | { USB4_SB_LRD_TUNING, 4 },
|
| 95 | { USB4_SB_OPCODE, 4 },
|
| 96 | { USB4_SB_METADATA, 4 },
|
| 97 | { USB4_SB_GEN23_TXFFE, 4 },
|
| 98 | { USB4_SB_GEN4_TXFFE, 4 },
|
| 99 | { USB4_SB_VERSION, 4 },
|
| 100 | { USB4_SB_DATA, 64 },
|
| 101 | };
|
| 102 |
|
| 103 | #define DEBUGFS_ATTR(__space, __write) \
|
| 104 | static int __space ## _open(struct inode *inode, struct file *file) \
|
| 105 | { \
|
| 106 | return single_open(file, __space ## _show, inode->i_private); \
|
| 107 | } \
|
| 108 | \
|
| 109 | static const struct file_operations __space ## _fops = { \
|
| 110 | .owner = THIS_MODULE, \
|
| 111 | .open = __space ## _open, \
|
| 112 | .release = single_release, \
|
| 113 | .read = seq_read, \
|
| 114 | .write = __write, \
|
| 115 | .llseek = seq_lseek, \
|
| 116 | }
|
| 117 |
|
| 118 | #define DEBUGFS_ATTR_RO(__space) \
|
| 119 | DEBUGFS_ATTR(__space, NULL)
|
| 120 |
|
| 121 | #define DEBUGFS_ATTR_RW(__space) \
|
| 122 | DEBUGFS_ATTR(__space, __space ## _write)
|
| 123 |
|
| 124 | static struct dentry *tb_debugfs_root;
|
| 125 |
|
| 126 | static void *validate_and_copy_from_user(const void __user *user_buf,
|
| 127 | size_t *count)
|
| 128 | {
|
| 129 | size_t nbytes;
|
| 130 | void *buf;
|
| 131 |
|
| 132 | if (!*count)
|
| 133 | return ERR_PTR(-EINVAL);
|
| 134 |
|
| 135 | if (!access_ok(user_buf, *count))
|
| 136 | return ERR_PTR(-EFAULT);
|
| 137 |
|
| 138 | buf = (void *)get_zeroed_page(GFP_KERNEL);
|
| 139 | if (!buf)
|
| 140 | return ERR_PTR(-ENOMEM);
|
| 141 |
|
| 142 | nbytes = min_t(size_t, *count, PAGE_SIZE);
|
| 143 | if (copy_from_user(buf, user_buf, nbytes)) {
|
| 144 | free_page((unsigned long)buf);
|
| 145 | return ERR_PTR(-EFAULT);
|
| 146 | }
|
| 147 |
|
| 148 | *count = nbytes;
|
| 149 | return buf;
|
| 150 | }
|
| 151 |
|
| 152 | static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
|
| 153 | int long_fmt_len)
|
| 154 | {
|
| 155 | char *token;
|
| 156 | u32 v[5];
|
| 157 | int ret;
|
| 158 |
|
| 159 | token = strsep(line, "\n");
|
| 160 | if (!token)
|
| 161 | return false;
|
| 162 |
|
| 163 | |
| 164 | * For Adapter/Router configuration space:
|
| 165 | * Short format is: offset value\n
|
| 166 | * v[0] v[1]
|
| 167 | * Long format as produced from the read side:
|
| 168 | * offset relative_offset cap_id vs_cap_id value\n
|
| 169 | * v[0] v[1] v[2] v[3] v[4]
|
| 170 | *
|
| 171 | * For Counter configuration space:
|
| 172 | * Short format is: offset\n
|
| 173 | * v[0]
|
| 174 | * Long format as produced from the read side:
|
| 175 | * offset relative_offset counter_id value\n
|
| 176 | * v[0] v[1] v[2] v[3]
|
| 177 |
|
| 178 | ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
|
| 179 |
|
| 180 | if (ret == short_fmt_len) {
|
| 181 | *offs = v[0];
|
| 182 | *val = v[short_fmt_len - 1];
|
| 183 | return true;
|
| 184 | } else if (ret == long_fmt_len) {
|
| 185 | *offs = v[0];
|
| 186 | *val = v[long_fmt_len - 1];
|
| 187 | return true;
|
| 188 | }
|
| 189 |
|
| 190 | return false;
|
| 191 | }
|
| 192 |
|
| 193 | #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
|
| 194 | static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
|
| 195 | const char __user *user_buf, size_t count,
|
| 196 | loff_t *ppos)
|
| 197 | {
|
| 198 | struct tb *tb = sw->tb;
|
| 199 | char *line, *buf;
|
| 200 | u32 val, offset;
|
| 201 | int ret = 0;
|
| 202 |
|
| 203 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 204 | if (IS_ERR(buf))
|
| 205 | return PTR_ERR(buf);
|
| 206 |
|
| 207 | pm_runtime_get_sync(&sw->dev);
|
| 208 |
|
| 209 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 210 | ret = -ERESTARTSYS;
|
| 211 | goto out;
|
| 212 | }
|
| 213 |
|
| 214 |
|
| 215 | add_taint(TAINT_USER, LOCKDEP_STILL_OK);
|
| 216 |
|
| 217 | line = buf;
|
| 218 | while (parse_line(&line, &offset, &val, 2, 5)) {
|
| 219 | if (port)
|
| 220 | ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
|
| 221 | else
|
| 222 | ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
|
| 223 | if (ret)
|
| 224 | break;
|
| 225 | }
|
| 226 |
|
| 227 | mutex_unlock(&tb->lock);
|
| 228 |
|
| 229 | out:
|
| 230 | pm_runtime_mark_last_busy(&sw->dev);
|
| 231 | pm_runtime_put_autosuspend(&sw->dev);
|
| 232 | free_page((unsigned long)buf);
|
| 233 |
|
| 234 | return ret < 0 ? ret : count;
|
| 235 | }
|
| 236 |
|
| 237 | static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
|
| 238 | size_t count, loff_t *ppos)
|
| 239 | {
|
| 240 | struct seq_file *s = file->private_data;
|
| 241 | struct tb_port *port = s->private;
|
| 242 |
|
| 243 | return regs_write(port->sw, port, user_buf, count, ppos);
|
| 244 | }
|
| 245 |
|
| 246 | static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
|
| 247 | size_t count, loff_t *ppos)
|
| 248 | {
|
| 249 | struct seq_file *s = file->private_data;
|
| 250 | struct tb_switch *sw = s->private;
|
| 251 |
|
| 252 | return regs_write(sw, NULL, user_buf, count, ppos);
|
| 253 | }
|
| 254 |
|
| 255 | static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size,
|
| 256 | size_t *bytes_read)
|
| 257 | {
|
| 258 | char *field, *token;
|
| 259 | int i;
|
| 260 |
|
| 261 | token = strsep(line, "\n");
|
| 262 | if (!token)
|
| 263 | return false;
|
| 264 |
|
| 265 |
|
| 266 | field = strsep(&token, " ");
|
| 267 | if (!field)
|
| 268 | return false;
|
| 269 | if (kstrtou8(field, 0, reg))
|
| 270 | return false;
|
| 271 |
|
| 272 |
|
| 273 | for (i = 0; i < data_size; i++) {
|
| 274 | field = strsep(&token, " ");
|
| 275 | if (!field)
|
| 276 | break;
|
| 277 | if (kstrtou8(field, 0, &data[i]))
|
| 278 | return false;
|
| 279 | }
|
| 280 |
|
| 281 | *bytes_read = i;
|
| 282 | return true;
|
| 283 | }
|
| 284 |
|
| 285 | static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs,
|
| 286 | size_t size, enum usb4_sb_target target, u8 index,
|
| 287 | char *buf, size_t count, loff_t *ppos)
|
| 288 | {
|
| 289 | u8 reg, data[SB_MAX_SIZE];
|
| 290 | size_t bytes_read;
|
| 291 | char *line = buf;
|
| 292 |
|
| 293 |
|
| 294 | add_taint(TAINT_USER, LOCKDEP_STILL_OK);
|
| 295 |
|
| 296 | |
| 297 | * For sideband registers we accept:
|
| 298 | * reg b0 b1 b2...\n
|
| 299 | *
|
| 300 | * Here "reg" is the byte offset of the sideband register and "b0"..
|
| 301 | * are the byte values. There can be less byte values than the register
|
| 302 | * size. The leftovers will not be overwritten.
|
| 303 |
|
| 304 | while (parse_sb_line(&line, ®, data, ARRAY_SIZE(data), &bytes_read)) {
|
| 305 | const struct sb_reg *sb_reg;
|
| 306 | int ret;
|
| 307 |
|
| 308 |
|
| 309 | if (bytes_read < 1)
|
| 310 | return -EINVAL;
|
| 311 |
|
| 312 |
|
| 313 | sb_reg = NULL;
|
| 314 | for (int i = 0; i < size; i++) {
|
| 315 | if (sb_regs[i].reg == reg) {
|
| 316 | sb_reg = &sb_regs[i];
|
| 317 | break;
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | if (!sb_reg)
|
| 322 | return -EINVAL;
|
| 323 |
|
| 324 | if (bytes_read > sb_regs->size)
|
| 325 | return -E2BIG;
|
| 326 |
|
| 327 | ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data,
|
| 328 | bytes_read);
|
| 329 | if (ret)
|
| 330 | return ret;
|
| 331 | }
|
| 332 |
|
| 333 | return 0;
|
| 334 | }
|
| 335 |
|
| 336 | static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf,
|
| 337 | size_t count, loff_t *ppos)
|
| 338 | {
|
| 339 | struct seq_file *s = file->private_data;
|
| 340 | struct tb_port *port = s->private;
|
| 341 | struct tb_switch *sw = port->sw;
|
| 342 | struct tb *tb = sw->tb;
|
| 343 | char *buf;
|
| 344 | int ret;
|
| 345 |
|
| 346 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 347 | if (IS_ERR(buf))
|
| 348 | return PTR_ERR(buf);
|
| 349 |
|
| 350 | pm_runtime_get_sync(&sw->dev);
|
| 351 |
|
| 352 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 353 | ret = -ERESTARTSYS;
|
| 354 | goto out;
|
| 355 | }
|
| 356 |
|
| 357 | ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
|
| 358 | USB4_SB_TARGET_ROUTER, 0, buf, count, ppos);
|
| 359 |
|
| 360 | mutex_unlock(&tb->lock);
|
| 361 | out:
|
| 362 | pm_runtime_mark_last_busy(&sw->dev);
|
| 363 | pm_runtime_put_autosuspend(&sw->dev);
|
| 364 | free_page((unsigned long)buf);
|
| 365 |
|
| 366 | return ret < 0 ? ret : count;
|
| 367 | }
|
| 368 |
|
| 369 | static ssize_t retimer_sb_regs_write(struct file *file,
|
| 370 | const char __user *user_buf,
|
| 371 | size_t count, loff_t *ppos)
|
| 372 | {
|
| 373 | struct seq_file *s = file->private_data;
|
| 374 | struct tb_retimer *rt = s->private;
|
| 375 | struct tb *tb = rt->tb;
|
| 376 | char *buf;
|
| 377 | int ret;
|
| 378 |
|
| 379 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 380 | if (IS_ERR(buf))
|
| 381 | return PTR_ERR(buf);
|
| 382 |
|
| 383 | pm_runtime_get_sync(&rt->dev);
|
| 384 |
|
| 385 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 386 | ret = -ERESTARTSYS;
|
| 387 | goto out;
|
| 388 | }
|
| 389 |
|
| 390 | ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
|
| 391 | USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos);
|
| 392 |
|
| 393 | mutex_unlock(&tb->lock);
|
| 394 | out:
|
| 395 | pm_runtime_mark_last_busy(&rt->dev);
|
| 396 | pm_runtime_put_autosuspend(&rt->dev);
|
| 397 | free_page((unsigned long)buf);
|
| 398 |
|
| 399 | return ret < 0 ? ret : count;
|
| 400 | }
|
| 401 | #define DEBUGFS_MODE 0600
|
| 402 | #else
|
| 403 | #define port_regs_write NULL
|
| 404 | #define switch_regs_write NULL
|
| 405 | #define port_sb_regs_write NULL
|
| 406 | #define retimer_sb_regs_write NULL
|
| 407 | #define DEBUGFS_MODE 0400
|
| 408 | #endif
|
| 409 |
|
| 410 | #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
|
| 411 | |
| 412 | * struct tb_margining - Lane margining support
|
| 413 | * @port: USB4 port through which the margining operations are run
|
| 414 | * @target: Sideband target
|
| 415 | * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
|
| 416 | * @dev: Pointer to the device that is the target (USB4 port or retimer)
|
| 417 | * @gen: Link generation
|
| 418 | * @asym_rx: %true% if @port supports asymmetric link with 3 Rx
|
| 419 | * @caps: Port lane margining capabilities
|
| 420 | * @results: Last lane margining results
|
| 421 | * @lanes: %0, %1 or %7 (all)
|
| 422 | * @min_ber_level: Minimum supported BER level contour value
|
| 423 | * @max_ber_level: Maximum supported BER level contour value
|
| 424 | * @ber_level: Current BER level contour value
|
| 425 | * @voltage_steps: Number of mandatory voltage steps
|
| 426 | * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
|
| 427 | * @voltage_steps_optional_range: Number of voltage steps for optional range
|
| 428 | * @max_voltage_offset_optional_range: Maximum voltage offset for the optional
|
| 429 | * range (in mV).
|
| 430 | * @time_steps: Number of time margin steps
|
| 431 | * @max_time_offset: Maximum time margin offset (in mUI)
|
| 432 | * @voltage_time_offset: Offset for voltage / time for software margining
|
| 433 | * @dwell_time: Dwell time for software margining (in ms)
|
| 434 | * @error_counter: Error counter operation for software margining
|
| 435 | * @optional_voltage_offset_range: Enable optional extended voltage range
|
| 436 | * @software: %true if software margining is used instead of hardware
|
| 437 | * @time: %true if time margining is used instead of voltage
|
| 438 | * @right_high: %false if left/low margin test is performed, %true if
|
| 439 | * right/high
|
| 440 | * @upper_eye: %false if the lower PAM3 eye is used, %true if the upper
|
| 441 | * eye is used
|
| 442 |
|
| 443 | struct tb_margining {
|
| 444 | struct tb_port *port;
|
| 445 | enum usb4_sb_target target;
|
| 446 | u8 index;
|
| 447 | struct device *dev;
|
| 448 | unsigned int gen;
|
| 449 | bool asym_rx;
|
| 450 | u32 caps[3];
|
| 451 | u32 results[3];
|
| 452 | enum usb4_margining_lane lanes;
|
| 453 | unsigned int min_ber_level;
|
| 454 | unsigned int max_ber_level;
|
| 455 | unsigned int ber_level;
|
| 456 | unsigned int voltage_steps;
|
| 457 | unsigned int max_voltage_offset;
|
| 458 | unsigned int voltage_steps_optional_range;
|
| 459 | unsigned int max_voltage_offset_optional_range;
|
| 460 | unsigned int time_steps;
|
| 461 | unsigned int max_time_offset;
|
| 462 | unsigned int voltage_time_offset;
|
| 463 | unsigned int dwell_time;
|
| 464 | enum usb4_margin_sw_error_counter error_counter;
|
| 465 | bool optional_voltage_offset_range;
|
| 466 | bool software;
|
| 467 | bool time;
|
| 468 | bool right_high;
|
| 469 | bool upper_eye;
|
| 470 | };
|
| 471 |
|
| 472 | static int margining_modify_error_counter(struct tb_margining *margining,
|
| 473 | u32 lanes, enum usb4_margin_sw_error_counter error_counter)
|
| 474 | {
|
| 475 | struct usb4_port_margining_params params = { 0 };
|
| 476 | struct tb_port *port = margining->port;
|
| 477 | u32 result;
|
| 478 |
|
| 479 | if (error_counter != USB4_MARGIN_SW_ERROR_COUNTER_CLEAR &&
|
| 480 | error_counter != USB4_MARGIN_SW_ERROR_COUNTER_STOP)
|
| 481 | return -EOPNOTSUPP;
|
| 482 |
|
| 483 | params.error_counter = error_counter;
|
| 484 | params.lanes = lanes;
|
| 485 |
|
| 486 | return usb4_port_sw_margin(port, margining->target, margining->index,
|
| 487 | ¶ms, &result);
|
| 488 | }
|
| 489 |
|
| 490 | static bool supports_software(const struct tb_margining *margining)
|
| 491 | {
|
| 492 | if (margining->gen < 4)
|
| 493 | return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
|
| 494 | return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_SW;
|
| 495 | }
|
| 496 |
|
| 497 | static bool supports_hardware(const struct tb_margining *margining)
|
| 498 | {
|
| 499 | if (margining->gen < 4)
|
| 500 | return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
|
| 501 | return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_HW;
|
| 502 | }
|
| 503 |
|
| 504 | static bool all_lanes(const struct tb_margining *margining)
|
| 505 | {
|
| 506 | return margining->caps[0] & USB4_MARGIN_CAP_0_ALL_LANES;
|
| 507 | }
|
| 508 |
|
| 509 | static enum usb4_margin_cap_voltage_indp
|
| 510 | independent_voltage_margins(const struct tb_margining *margining)
|
| 511 | {
|
| 512 | if (margining->gen < 4) {
|
| 513 | switch (FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK, margining->caps[0])) {
|
| 514 | case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
|
| 515 | return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN;
|
| 516 | case USB4_MARGIN_CAP_0_VOLTAGE_HL:
|
| 517 | return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL;
|
| 518 | case USB4_MARGIN_CAP_1_TIME_BOTH:
|
| 519 | return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH;
|
| 520 | }
|
| 521 | } else {
|
| 522 | switch (FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_INDP_MASK, margining->caps[2])) {
|
| 523 | case USB4_MARGIN_CAP_2_VOLTAGE_MIN:
|
| 524 | return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN;
|
| 525 | case USB4_MARGIN_CAP_2_VOLTAGE_BOTH:
|
| 526 | return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH;
|
| 527 | }
|
| 528 | }
|
| 529 | return USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN;
|
| 530 | }
|
| 531 |
|
| 532 | static bool supports_time(const struct tb_margining *margining)
|
| 533 | {
|
| 534 | if (margining->gen < 4)
|
| 535 | return margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
|
| 536 | return margining->caps[2] & USB4_MARGIN_CAP_2_TIME;
|
| 537 | }
|
| 538 |
|
| 539 |
|
| 540 | static enum usb4_margin_cap_time_indp
|
| 541 | independent_time_margins(const struct tb_margining *margining)
|
| 542 | {
|
| 543 | if (margining->gen < 4) {
|
| 544 | switch (FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK, margining->caps[1])) {
|
| 545 | case USB4_MARGIN_CAP_1_TIME_MIN:
|
| 546 | return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN;
|
| 547 | case USB4_MARGIN_CAP_1_TIME_LR:
|
| 548 | return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR;
|
| 549 | case USB4_MARGIN_CAP_1_TIME_BOTH:
|
| 550 | return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH;
|
| 551 | }
|
| 552 | } else {
|
| 553 | switch (FIELD_GET(USB4_MARGIN_CAP_2_TIME_INDP_MASK, margining->caps[2])) {
|
| 554 | case USB4_MARGIN_CAP_2_TIME_MIN:
|
| 555 | return USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN;
|
| 556 | case USB4_MARGIN_CAP_2_TIME_BOTH:
|
| 557 | return USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH;
|
| 558 | }
|
| 559 | }
|
| 560 | return USB4_MARGIN_CAP_TIME_INDP_UNKNOWN;
|
| 561 | }
|
| 562 |
|
| 563 | static bool
|
| 564 | supports_optional_voltage_offset_range(const struct tb_margining *margining)
|
| 565 | {
|
| 566 | return margining->caps[0] & USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT;
|
| 567 | }
|
| 568 |
|
| 569 | static ssize_t
|
| 570 | margining_ber_level_write(struct file *file, const char __user *user_buf,
|
| 571 | size_t count, loff_t *ppos)
|
| 572 | {
|
| 573 | struct seq_file *s = file->private_data;
|
| 574 | struct tb_margining *margining = s->private;
|
| 575 | struct tb *tb = margining->port->sw->tb;
|
| 576 | unsigned int val;
|
| 577 | int ret = 0;
|
| 578 | char *buf;
|
| 579 |
|
| 580 | if (mutex_lock_interruptible(&tb->lock))
|
| 581 | return -ERESTARTSYS;
|
| 582 |
|
| 583 | if (margining->software) {
|
| 584 | ret = -EINVAL;
|
| 585 | goto out_unlock;
|
| 586 | }
|
| 587 |
|
| 588 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 589 | if (IS_ERR(buf)) {
|
| 590 | ret = PTR_ERR(buf);
|
| 591 | goto out_unlock;
|
| 592 | }
|
| 593 |
|
| 594 | buf[count - 1] = '\0';
|
| 595 |
|
| 596 | ret = kstrtouint(buf, 10, &val);
|
| 597 | if (ret)
|
| 598 | goto out_free;
|
| 599 |
|
| 600 | if (val < margining->min_ber_level ||
|
| 601 | val > margining->max_ber_level) {
|
| 602 | ret = -EINVAL;
|
| 603 | goto out_free;
|
| 604 | }
|
| 605 |
|
| 606 | margining->ber_level = val;
|
| 607 |
|
| 608 | out_free:
|
| 609 | free_page((unsigned long)buf);
|
| 610 | out_unlock:
|
| 611 | mutex_unlock(&tb->lock);
|
| 612 |
|
| 613 | return ret < 0 ? ret : count;
|
| 614 | }
|
| 615 |
|
| 616 | static void ber_level_show(struct seq_file *s, unsigned int val)
|
| 617 | {
|
| 618 | if (val % 2)
|
| 619 | seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
|
| 620 | else
|
| 621 | seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
|
| 622 | }
|
| 623 |
|
| 624 | static int margining_ber_level_show(struct seq_file *s, void *not_used)
|
| 625 | {
|
| 626 | const struct tb_margining *margining = s->private;
|
| 627 |
|
| 628 | if (margining->software)
|
| 629 | return -EINVAL;
|
| 630 | ber_level_show(s, margining->ber_level);
|
| 631 | return 0;
|
| 632 | }
|
| 633 | DEBUGFS_ATTR_RW(margining_ber_level);
|
| 634 |
|
| 635 | static int margining_caps_show(struct seq_file *s, void *not_used)
|
| 636 | {
|
| 637 | struct tb_margining *margining = s->private;
|
| 638 | struct tb *tb = margining->port->sw->tb;
|
| 639 | int ret = 0;
|
| 640 |
|
| 641 | if (mutex_lock_interruptible(&tb->lock))
|
| 642 | return -ERESTARTSYS;
|
| 643 |
|
| 644 |
|
| 645 | for (int i = 0; i < ARRAY_SIZE(margining->caps); i++)
|
| 646 | seq_printf(s, "0x%08x\n", margining->caps[i]);
|
| 647 |
|
| 648 | seq_printf(s, "# software margining: %s\n",
|
| 649 | supports_software(margining) ? "yes" : "no");
|
| 650 | if (supports_hardware(margining)) {
|
| 651 | seq_puts(s, "# hardware margining: yes\n");
|
| 652 | seq_puts(s, "# minimum BER level contour: ");
|
| 653 | ber_level_show(s, margining->min_ber_level);
|
| 654 | seq_puts(s, "# maximum BER level contour: ");
|
| 655 | ber_level_show(s, margining->max_ber_level);
|
| 656 | } else {
|
| 657 | seq_puts(s, "# hardware margining: no\n");
|
| 658 | }
|
| 659 |
|
| 660 | seq_printf(s, "# all lanes simultaneously: %s\n",
|
| 661 | str_yes_no(all_lanes(margining)));
|
| 662 | seq_printf(s, "# voltage margin steps: %u\n",
|
| 663 | margining->voltage_steps);
|
| 664 | seq_printf(s, "# maximum voltage offset: %u mV\n",
|
| 665 | margining->max_voltage_offset);
|
| 666 | seq_printf(s, "# optional voltage offset range support: %s\n",
|
| 667 | str_yes_no(supports_optional_voltage_offset_range(margining)));
|
| 668 | if (supports_optional_voltage_offset_range(margining)) {
|
| 669 | seq_printf(s, "# voltage margin steps, optional range: %u\n",
|
| 670 | margining->voltage_steps_optional_range);
|
| 671 | seq_printf(s, "# maximum voltage offset, optional range: %u mV\n",
|
| 672 | margining->max_voltage_offset_optional_range);
|
| 673 | }
|
| 674 |
|
| 675 | switch (independent_voltage_margins(margining)) {
|
| 676 | case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN:
|
| 677 | seq_puts(s, "# returns minimum between high and low voltage margins\n");
|
| 678 | break;
|
| 679 | case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL:
|
| 680 | seq_puts(s, "# returns high or low voltage margin\n");
|
| 681 | break;
|
| 682 | case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH:
|
| 683 | seq_puts(s, "# returns both high and low margins\n");
|
| 684 | break;
|
| 685 | case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN:
|
| 686 | seq_puts(s, "# returns minimum between high and low voltage margins in both lower and upper eye\n");
|
| 687 | break;
|
| 688 | case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH:
|
| 689 | seq_puts(s, "# returns both high and low margins of both upper and lower eye\n");
|
| 690 | break;
|
| 691 | case USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN:
|
| 692 | tb_port_warn(margining->port,
|
| 693 | "failed to parse independent voltage margining capabilities\n");
|
| 694 | ret = -EIO;
|
| 695 | goto out;
|
| 696 | }
|
| 697 |
|
| 698 | if (supports_time(margining)) {
|
| 699 | seq_puts(s, "# time margining: yes\n");
|
| 700 | seq_printf(s, "# time margining is destructive: %s\n",
|
| 701 | str_yes_no(margining->caps[1] & USB4_MARGIN_CAP_1_TIME_DESTR));
|
| 702 |
|
| 703 | switch (independent_time_margins(margining)) {
|
| 704 | case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN:
|
| 705 | seq_puts(s, "# returns minimum between left and right time margins\n");
|
| 706 | break;
|
| 707 | case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR:
|
| 708 | seq_puts(s, "# returns left or right margin\n");
|
| 709 | break;
|
| 710 | case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH:
|
| 711 | seq_puts(s, "# returns both left and right margins\n");
|
| 712 | break;
|
| 713 | case USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN:
|
| 714 | seq_puts(s, "# returns minimum between left and right time margins in both lower and upper eye\n");
|
| 715 | break;
|
| 716 | case USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH:
|
| 717 | seq_puts(s, "# returns both left and right margins of both upper and lower eye\n");
|
| 718 | break;
|
| 719 | case USB4_MARGIN_CAP_TIME_INDP_UNKNOWN:
|
| 720 | tb_port_warn(margining->port,
|
| 721 | "failed to parse independent time margining capabilities\n");
|
| 722 | ret = -EIO;
|
| 723 | goto out;
|
| 724 | }
|
| 725 |
|
| 726 | seq_printf(s, "# time margin steps: %u\n",
|
| 727 | margining->time_steps);
|
| 728 | seq_printf(s, "# maximum time offset: %u mUI\n",
|
| 729 | margining->max_time_offset);
|
| 730 | } else {
|
| 731 | seq_puts(s, "# time margining: no\n");
|
| 732 | }
|
| 733 |
|
| 734 | out:
|
| 735 | mutex_unlock(&tb->lock);
|
| 736 | return ret;
|
| 737 | }
|
| 738 | DEBUGFS_ATTR_RO(margining_caps);
|
| 739 |
|
| 740 | static const struct {
|
| 741 | enum usb4_margining_lane lane;
|
| 742 | const char *name;
|
| 743 | } lane_names[] = {
|
| 744 | {
|
| 745 | .lane = USB4_MARGINING_LANE_RX0,
|
| 746 | .name = "0",
|
| 747 | },
|
| 748 | {
|
| 749 | .lane = USB4_MARGINING_LANE_RX1,
|
| 750 | .name = "1",
|
| 751 | },
|
| 752 | {
|
| 753 | .lane = USB4_MARGINING_LANE_RX2,
|
| 754 | .name = "2",
|
| 755 | },
|
| 756 | {
|
| 757 | .lane = USB4_MARGINING_LANE_ALL,
|
| 758 | .name = "all",
|
| 759 | },
|
| 760 | };
|
| 761 |
|
| 762 | static ssize_t
|
| 763 | margining_lanes_write(struct file *file, const char __user *user_buf,
|
| 764 | size_t count, loff_t *ppos)
|
| 765 | {
|
| 766 | struct seq_file *s = file->private_data;
|
| 767 | struct tb_margining *margining = s->private;
|
| 768 | struct tb_port *port = margining->port;
|
| 769 | struct tb *tb = port->sw->tb;
|
| 770 | int lane = -1;
|
| 771 | char *buf;
|
| 772 |
|
| 773 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 774 | if (IS_ERR(buf))
|
| 775 | return PTR_ERR(buf);
|
| 776 |
|
| 777 | buf[count - 1] = '\0';
|
| 778 |
|
| 779 | for (int i = 0; i < ARRAY_SIZE(lane_names); i++) {
|
| 780 | if (!strcmp(buf, lane_names[i].name)) {
|
| 781 | lane = lane_names[i].lane;
|
| 782 | break;
|
| 783 | }
|
| 784 | }
|
| 785 |
|
| 786 | free_page((unsigned long)buf);
|
| 787 |
|
| 788 | if (lane == -1)
|
| 789 | return -EINVAL;
|
| 790 |
|
| 791 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 792 | if (lane == USB4_MARGINING_LANE_ALL && !all_lanes(margining))
|
| 793 | return -EINVAL;
|
| 794 | |
| 795 | * Enabling on RX2 requires that it is supported by the
|
| 796 | * USB4 port.
|
| 797 |
|
| 798 | if (lane == USB4_MARGINING_LANE_RX2 && !margining->asym_rx)
|
| 799 | return -EINVAL;
|
| 800 |
|
| 801 | margining->lanes = lane;
|
| 802 | }
|
| 803 |
|
| 804 | return count;
|
| 805 | }
|
| 806 |
|
| 807 | static int margining_lanes_show(struct seq_file *s, void *not_used)
|
| 808 | {
|
| 809 | struct tb_margining *margining = s->private;
|
| 810 | struct tb_port *port = margining->port;
|
| 811 | struct tb *tb = port->sw->tb;
|
| 812 |
|
| 813 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 814 | for (int i = 0; i < ARRAY_SIZE(lane_names); i++) {
|
| 815 | if (lane_names[i].lane == USB4_MARGINING_LANE_ALL &&
|
| 816 | !all_lanes(margining))
|
| 817 | continue;
|
| 818 | if (lane_names[i].lane == USB4_MARGINING_LANE_RX2 &&
|
| 819 | !margining->asym_rx)
|
| 820 | continue;
|
| 821 |
|
| 822 | if (i != 0)
|
| 823 | seq_putc(s, ' ');
|
| 824 |
|
| 825 | if (lane_names[i].lane == margining->lanes)
|
| 826 | seq_printf(s, "[%s]", lane_names[i].name);
|
| 827 | else
|
| 828 | seq_printf(s, "%s", lane_names[i].name);
|
| 829 | }
|
| 830 | seq_puts(s, "\n");
|
| 831 | }
|
| 832 |
|
| 833 | return 0;
|
| 834 | }
|
| 835 | DEBUGFS_ATTR_RW(margining_lanes);
|
| 836 |
|
| 837 | static ssize_t
|
| 838 | margining_voltage_time_offset_write(struct file *file,
|
| 839 | const char __user *user_buf,
|
| 840 | size_t count, loff_t *ppos)
|
| 841 | {
|
| 842 | struct seq_file *s = file->private_data;
|
| 843 | struct tb_margining *margining = s->private;
|
| 844 | struct tb *tb = margining->port->sw->tb;
|
| 845 | unsigned int max_margin;
|
| 846 | unsigned int val;
|
| 847 | int ret;
|
| 848 |
|
| 849 | ret = kstrtouint_from_user(user_buf, count, 10, &val);
|
| 850 | if (ret)
|
| 851 | return ret;
|
| 852 |
|
| 853 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 854 | if (!margining->software)
|
| 855 | return -EOPNOTSUPP;
|
| 856 |
|
| 857 | if (margining->time)
|
| 858 | max_margin = margining->time_steps;
|
| 859 | else
|
| 860 | if (margining->optional_voltage_offset_range)
|
| 861 | max_margin = margining->voltage_steps_optional_range;
|
| 862 | else
|
| 863 | max_margin = margining->voltage_steps;
|
| 864 |
|
| 865 | margining->voltage_time_offset = clamp(val, 0, max_margin);
|
| 866 | }
|
| 867 |
|
| 868 | return count;
|
| 869 | }
|
| 870 |
|
| 871 | static int margining_voltage_time_offset_show(struct seq_file *s,
|
| 872 | void *not_used)
|
| 873 | {
|
| 874 | const struct tb_margining *margining = s->private;
|
| 875 | struct tb *tb = margining->port->sw->tb;
|
| 876 |
|
| 877 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 878 | if (!margining->software)
|
| 879 | return -EOPNOTSUPP;
|
| 880 |
|
| 881 | seq_printf(s, "%d\n", margining->voltage_time_offset);
|
| 882 | }
|
| 883 |
|
| 884 | return 0;
|
| 885 | }
|
| 886 | DEBUGFS_ATTR_RW(margining_voltage_time_offset);
|
| 887 |
|
| 888 | static ssize_t
|
| 889 | margining_error_counter_write(struct file *file, const char __user *user_buf,
|
| 890 | size_t count, loff_t *ppos)
|
| 891 | {
|
| 892 | enum usb4_margin_sw_error_counter error_counter;
|
| 893 | struct seq_file *s = file->private_data;
|
| 894 | struct tb_margining *margining = s->private;
|
| 895 | struct tb *tb = margining->port->sw->tb;
|
| 896 | char *buf;
|
| 897 |
|
| 898 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 899 | if (IS_ERR(buf))
|
| 900 | return PTR_ERR(buf);
|
| 901 |
|
| 902 | buf[count - 1] = '\0';
|
| 903 |
|
| 904 | if (!strcmp(buf, "nop"))
|
| 905 | error_counter = USB4_MARGIN_SW_ERROR_COUNTER_NOP;
|
| 906 | else if (!strcmp(buf, "clear"))
|
| 907 | error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR;
|
| 908 | else if (!strcmp(buf, "start"))
|
| 909 | error_counter = USB4_MARGIN_SW_ERROR_COUNTER_START;
|
| 910 | else if (!strcmp(buf, "stop"))
|
| 911 | error_counter = USB4_MARGIN_SW_ERROR_COUNTER_STOP;
|
| 912 | else
|
| 913 | return -EINVAL;
|
| 914 |
|
| 915 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 916 | if (!margining->software)
|
| 917 | return -EOPNOTSUPP;
|
| 918 |
|
| 919 | margining->error_counter = error_counter;
|
| 920 | }
|
| 921 |
|
| 922 | return count;
|
| 923 | }
|
| 924 |
|
| 925 | static int margining_error_counter_show(struct seq_file *s, void *not_used)
|
| 926 | {
|
| 927 | const struct tb_margining *margining = s->private;
|
| 928 | struct tb *tb = margining->port->sw->tb;
|
| 929 |
|
| 930 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 931 | if (!margining->software)
|
| 932 | return -EOPNOTSUPP;
|
| 933 |
|
| 934 | switch (margining->error_counter) {
|
| 935 | case USB4_MARGIN_SW_ERROR_COUNTER_NOP:
|
| 936 | seq_puts(s, "[nop] clear start stop\n");
|
| 937 | break;
|
| 938 | case USB4_MARGIN_SW_ERROR_COUNTER_CLEAR:
|
| 939 | seq_puts(s, "nop [clear] start stop\n");
|
| 940 | break;
|
| 941 | case USB4_MARGIN_SW_ERROR_COUNTER_START:
|
| 942 | seq_puts(s, "nop clear [start] stop\n");
|
| 943 | break;
|
| 944 | case USB4_MARGIN_SW_ERROR_COUNTER_STOP:
|
| 945 | seq_puts(s, "nop clear start [stop]\n");
|
| 946 | break;
|
| 947 | }
|
| 948 | }
|
| 949 |
|
| 950 | return 0;
|
| 951 | }
|
| 952 | DEBUGFS_ATTR_RW(margining_error_counter);
|
| 953 |
|
| 954 | static ssize_t
|
| 955 | margining_dwell_time_write(struct file *file, const char __user *user_buf,
|
| 956 | size_t count, loff_t *ppos)
|
| 957 | {
|
| 958 | struct seq_file *s = file->private_data;
|
| 959 | struct tb_margining *margining = s->private;
|
| 960 | struct tb *tb = margining->port->sw->tb;
|
| 961 | unsigned int val;
|
| 962 | int ret;
|
| 963 |
|
| 964 | ret = kstrtouint_from_user(user_buf, count, 10, &val);
|
| 965 | if (ret)
|
| 966 | return ret;
|
| 967 |
|
| 968 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 969 | if (!margining->software)
|
| 970 | return -EOPNOTSUPP;
|
| 971 |
|
| 972 | margining->dwell_time = clamp(val, MIN_DWELL_TIME, MAX_DWELL_TIME);
|
| 973 | }
|
| 974 |
|
| 975 | return count;
|
| 976 | }
|
| 977 |
|
| 978 | static int margining_dwell_time_show(struct seq_file *s, void *not_used)
|
| 979 | {
|
| 980 | struct tb_margining *margining = s->private;
|
| 981 | struct tb *tb = margining->port->sw->tb;
|
| 982 |
|
| 983 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 984 | if (!margining->software)
|
| 985 | return -EOPNOTSUPP;
|
| 986 |
|
| 987 | seq_printf(s, "%d\n", margining->dwell_time);
|
| 988 | }
|
| 989 |
|
| 990 | return 0;
|
| 991 | }
|
| 992 | DEBUGFS_ATTR_RW(margining_dwell_time);
|
| 993 |
|
| 994 | static ssize_t
|
| 995 | margining_optional_voltage_offset_write(struct file *file, const char __user *user_buf,
|
| 996 | size_t count, loff_t *ppos)
|
| 997 | {
|
| 998 | struct seq_file *s = file->private_data;
|
| 999 | struct tb_margining *margining = s->private;
|
| 1000 | struct tb *tb = margining->port->sw->tb;
|
| 1001 | bool val;
|
| 1002 | int ret;
|
| 1003 |
|
| 1004 | ret = kstrtobool_from_user(user_buf, count, &val);
|
| 1005 | if (ret)
|
| 1006 | return ret;
|
| 1007 |
|
| 1008 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 1009 | margining->optional_voltage_offset_range = val;
|
| 1010 | }
|
| 1011 |
|
| 1012 | return count;
|
| 1013 | }
|
| 1014 |
|
| 1015 | static int margining_optional_voltage_offset_show(struct seq_file *s,
|
| 1016 | void *not_used)
|
| 1017 | {
|
| 1018 | struct tb_margining *margining = s->private;
|
| 1019 | struct tb *tb = margining->port->sw->tb;
|
| 1020 |
|
| 1021 | scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
|
| 1022 | seq_printf(s, "%u\n", margining->optional_voltage_offset_range);
|
| 1023 | }
|
| 1024 |
|
| 1025 | return 0;
|
| 1026 | }
|
| 1027 | DEBUGFS_ATTR_RW(margining_optional_voltage_offset);
|
| 1028 |
|
| 1029 | static ssize_t margining_mode_write(struct file *file,
|
| 1030 | const char __user *user_buf,
|
| 1031 | size_t count, loff_t *ppos)
|
| 1032 | {
|
| 1033 | struct seq_file *s = file->private_data;
|
| 1034 | struct tb_margining *margining = s->private;
|
| 1035 | struct tb *tb = margining->port->sw->tb;
|
| 1036 | int ret = 0;
|
| 1037 | char *buf;
|
| 1038 |
|
| 1039 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 1040 | if (IS_ERR(buf))
|
| 1041 | return PTR_ERR(buf);
|
| 1042 |
|
| 1043 | buf[count - 1] = '\0';
|
| 1044 |
|
| 1045 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 1046 | ret = -ERESTARTSYS;
|
| 1047 | goto out_free;
|
| 1048 | }
|
| 1049 |
|
| 1050 | if (!strcmp(buf, "software")) {
|
| 1051 | if (supports_software(margining))
|
| 1052 | margining->software = true;
|
| 1053 | else
|
| 1054 | ret = -EINVAL;
|
| 1055 | } else if (!strcmp(buf, "hardware")) {
|
| 1056 | if (supports_hardware(margining))
|
| 1057 | margining->software = false;
|
| 1058 | else
|
| 1059 | ret = -EINVAL;
|
| 1060 | } else {
|
| 1061 | ret = -EINVAL;
|
| 1062 | }
|
| 1063 |
|
| 1064 | mutex_unlock(&tb->lock);
|
| 1065 |
|
| 1066 | out_free:
|
| 1067 | free_page((unsigned long)buf);
|
| 1068 | return ret ? ret : count;
|
| 1069 | }
|
| 1070 |
|
| 1071 | static int margining_mode_show(struct seq_file *s, void *not_used)
|
| 1072 | {
|
| 1073 | struct tb_margining *margining = s->private;
|
| 1074 | struct tb *tb = margining->port->sw->tb;
|
| 1075 | const char *space = "";
|
| 1076 |
|
| 1077 | if (mutex_lock_interruptible(&tb->lock))
|
| 1078 | return -ERESTARTSYS;
|
| 1079 |
|
| 1080 | if (supports_software(margining)) {
|
| 1081 | if (margining->software)
|
| 1082 | seq_puts(s, "[software]");
|
| 1083 | else
|
| 1084 | seq_puts(s, "software");
|
| 1085 | space = " ";
|
| 1086 | }
|
| 1087 | if (supports_hardware(margining)) {
|
| 1088 | if (margining->software)
|
| 1089 | seq_printf(s, "%shardware", space);
|
| 1090 | else
|
| 1091 | seq_printf(s, "%s[hardware]", space);
|
| 1092 | }
|
| 1093 |
|
| 1094 | mutex_unlock(&tb->lock);
|
| 1095 |
|
| 1096 | seq_puts(s, "\n");
|
| 1097 | return 0;
|
| 1098 | }
|
| 1099 | DEBUGFS_ATTR_RW(margining_mode);
|
| 1100 |
|
| 1101 | static int margining_run_sw(struct tb_margining *margining,
|
| 1102 | struct usb4_port_margining_params *params)
|
| 1103 | {
|
| 1104 | u32 nsamples = margining->dwell_time / DWELL_SAMPLE_INTERVAL;
|
| 1105 | int ret, i;
|
| 1106 |
|
| 1107 | ret = usb4_port_sw_margin(margining->port, margining->target, margining->index,
|
| 1108 | params, margining->results);
|
| 1109 | if (ret)
|
| 1110 | goto out_stop;
|
| 1111 |
|
| 1112 | for (i = 0; i <= nsamples; i++) {
|
| 1113 | u32 errors = 0;
|
| 1114 |
|
| 1115 | ret = usb4_port_sw_margin_errors(margining->port, margining->target,
|
| 1116 | margining->index, &margining->results[1]);
|
| 1117 | if (ret)
|
| 1118 | break;
|
| 1119 |
|
| 1120 | if (margining->lanes == USB4_MARGINING_LANE_RX0)
|
| 1121 | errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK,
|
| 1122 | margining->results[1]);
|
| 1123 | else if (margining->lanes == USB4_MARGINING_LANE_RX1)
|
| 1124 | errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK,
|
| 1125 | margining->results[1]);
|
| 1126 | else if (margining->lanes == USB4_MARGINING_LANE_RX2)
|
| 1127 | errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK,
|
| 1128 | margining->results[1]);
|
| 1129 | else if (margining->lanes == USB4_MARGINING_LANE_ALL)
|
| 1130 | errors = margining->results[1];
|
| 1131 |
|
| 1132 |
|
| 1133 | if (errors)
|
| 1134 | break;
|
| 1135 |
|
| 1136 | fsleep(DWELL_SAMPLE_INTERVAL * USEC_PER_MSEC);
|
| 1137 | }
|
| 1138 |
|
| 1139 | out_stop:
|
| 1140 | |
| 1141 | * Stop the counters but don't clear them to allow the
|
| 1142 | * different error counter configurations.
|
| 1143 |
|
| 1144 | margining_modify_error_counter(margining, margining->lanes,
|
| 1145 | USB4_MARGIN_SW_ERROR_COUNTER_STOP);
|
| 1146 | return ret;
|
| 1147 | }
|
| 1148 |
|
| 1149 | static int validate_margining(struct tb_margining *margining)
|
| 1150 | {
|
| 1151 | |
| 1152 | * For running on RX2 the link must be asymmetric with 3
|
| 1153 | * receivers. Because this is can change dynamically, check it
|
| 1154 | * here before we start the margining and report back error if
|
| 1155 | * expectations are not met.
|
| 1156 |
|
| 1157 | if (margining->lanes == USB4_MARGINING_LANE_RX2) {
|
| 1158 | int ret;
|
| 1159 |
|
| 1160 | ret = tb_port_get_link_width(margining->port);
|
| 1161 | if (ret < 0)
|
| 1162 | return ret;
|
| 1163 | if (ret != TB_LINK_WIDTH_ASYM_RX) {
|
| 1164 | tb_port_warn(margining->port, "link is %s expected %s",
|
| 1165 | tb_width_name(ret),
|
| 1166 | tb_width_name(TB_LINK_WIDTH_ASYM_RX));
|
| 1167 | return -EINVAL;
|
| 1168 | }
|
| 1169 | }
|
| 1170 |
|
| 1171 | return 0;
|
| 1172 | }
|
| 1173 |
|
| 1174 | static int margining_run_write(void *data, u64 val)
|
| 1175 | {
|
| 1176 | struct tb_margining *margining = data;
|
| 1177 | struct tb_port *port = margining->port;
|
| 1178 | struct device *dev = margining->dev;
|
| 1179 | struct tb_switch *sw = port->sw;
|
| 1180 | struct tb_switch *down_sw;
|
| 1181 | struct tb *tb = sw->tb;
|
| 1182 | int ret, clx;
|
| 1183 |
|
| 1184 | if (val != 1)
|
| 1185 | return -EINVAL;
|
| 1186 |
|
| 1187 | pm_runtime_get_sync(dev);
|
| 1188 |
|
| 1189 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 1190 | ret = -ERESTARTSYS;
|
| 1191 | goto out_rpm_put;
|
| 1192 | }
|
| 1193 |
|
| 1194 | ret = validate_margining(margining);
|
| 1195 | if (ret)
|
| 1196 | goto out_unlock;
|
| 1197 |
|
| 1198 | if (tb_is_upstream_port(port))
|
| 1199 | down_sw = sw;
|
| 1200 | else if (port->remote)
|
| 1201 | down_sw = port->remote->sw;
|
| 1202 | else
|
| 1203 | down_sw = NULL;
|
| 1204 |
|
| 1205 | if (down_sw) {
|
| 1206 | |
| 1207 | * CL states may interfere with lane margining so
|
| 1208 | * disable them temporarily now.
|
| 1209 |
|
| 1210 | ret = tb_switch_clx_disable(down_sw);
|
| 1211 | if (ret < 0) {
|
| 1212 | tb_sw_warn(down_sw, "failed to disable CL states\n");
|
| 1213 | goto out_unlock;
|
| 1214 | }
|
| 1215 | clx = ret;
|
| 1216 | }
|
| 1217 |
|
| 1218 |
|
| 1219 | memset(margining->results, 0, sizeof(margining->results));
|
| 1220 |
|
| 1221 | if (margining->software) {
|
| 1222 | struct usb4_port_margining_params params = {
|
| 1223 | .error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR,
|
| 1224 | .lanes = margining->lanes,
|
| 1225 | .time = margining->time,
|
| 1226 | .voltage_time_offset = margining->voltage_time_offset,
|
| 1227 | .right_high = margining->right_high,
|
| 1228 | .upper_eye = margining->upper_eye,
|
| 1229 | .optional_voltage_offset_range = margining->optional_voltage_offset_range,
|
| 1230 | };
|
| 1231 |
|
| 1232 | tb_port_dbg(port,
|
| 1233 | "running software %s lane margining for %s lanes %u\n",
|
| 1234 | margining->time ? "time" : "voltage", dev_name(dev),
|
| 1235 | margining->lanes);
|
| 1236 |
|
| 1237 | ret = margining_run_sw(margining, ¶ms);
|
| 1238 | } else {
|
| 1239 | struct usb4_port_margining_params params = {
|
| 1240 | .ber_level = margining->ber_level,
|
| 1241 | .lanes = margining->lanes,
|
| 1242 | .time = margining->time,
|
| 1243 | .right_high = margining->right_high,
|
| 1244 | .upper_eye = margining->upper_eye,
|
| 1245 | .optional_voltage_offset_range = margining->optional_voltage_offset_range,
|
| 1246 | };
|
| 1247 |
|
| 1248 | tb_port_dbg(port,
|
| 1249 | "running hardware %s lane margining for %s lanes %u\n",
|
| 1250 | margining->time ? "time" : "voltage", dev_name(dev),
|
| 1251 | margining->lanes);
|
| 1252 |
|
| 1253 | ret = usb4_port_hw_margin(port, margining->target, margining->index, ¶ms,
|
| 1254 | margining->results, ARRAY_SIZE(margining->results));
|
| 1255 | }
|
| 1256 |
|
| 1257 | if (down_sw)
|
| 1258 | tb_switch_clx_enable(down_sw, clx);
|
| 1259 | out_unlock:
|
| 1260 | mutex_unlock(&tb->lock);
|
| 1261 | out_rpm_put:
|
| 1262 | pm_runtime_mark_last_busy(dev);
|
| 1263 | pm_runtime_put_autosuspend(dev);
|
| 1264 |
|
| 1265 | return ret;
|
| 1266 | }
|
| 1267 | DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
|
| 1268 | "%llu\n");
|
| 1269 |
|
| 1270 | static ssize_t margining_results_write(struct file *file,
|
| 1271 | const char __user *user_buf,
|
| 1272 | size_t count, loff_t *ppos)
|
| 1273 | {
|
| 1274 | struct seq_file *s = file->private_data;
|
| 1275 | struct tb_margining *margining = s->private;
|
| 1276 | struct tb *tb = margining->port->sw->tb;
|
| 1277 |
|
| 1278 | if (mutex_lock_interruptible(&tb->lock))
|
| 1279 | return -ERESTARTSYS;
|
| 1280 |
|
| 1281 |
|
| 1282 | memset(margining->results, 0, sizeof(margining->results));
|
| 1283 |
|
| 1284 | if (margining->software) {
|
| 1285 |
|
| 1286 | margining_modify_error_counter(margining,
|
| 1287 | USB4_MARGINING_LANE_ALL,
|
| 1288 | USB4_MARGIN_SW_ERROR_COUNTER_CLEAR);
|
| 1289 | }
|
| 1290 |
|
| 1291 | mutex_unlock(&tb->lock);
|
| 1292 | return count;
|
| 1293 | }
|
| 1294 |
|
| 1295 | static void voltage_margin_show(struct seq_file *s,
|
| 1296 | const struct tb_margining *margining, u8 val)
|
| 1297 | {
|
| 1298 | unsigned int tmp, voltage;
|
| 1299 |
|
| 1300 | tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val);
|
| 1301 | voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
|
| 1302 | seq_printf(s, "%u mV (%u)", voltage, tmp);
|
| 1303 | if (val & USB4_MARGIN_HW_RES_EXCEEDS)
|
| 1304 | seq_puts(s, " exceeds maximum");
|
| 1305 | seq_puts(s, "\n");
|
| 1306 | if (margining->optional_voltage_offset_range)
|
| 1307 | seq_puts(s, " optional voltage offset range enabled\n");
|
| 1308 | }
|
| 1309 |
|
| 1310 | static void time_margin_show(struct seq_file *s,
|
| 1311 | const struct tb_margining *margining, u8 val)
|
| 1312 | {
|
| 1313 | unsigned int tmp, interval;
|
| 1314 |
|
| 1315 | tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val);
|
| 1316 | interval = tmp * margining->max_time_offset / margining->time_steps;
|
| 1317 | seq_printf(s, "%u mUI (%u)", interval, tmp);
|
| 1318 | if (val & USB4_MARGIN_HW_RES_EXCEEDS)
|
| 1319 | seq_puts(s, " exceeds maximum");
|
| 1320 | seq_puts(s, "\n");
|
| 1321 | }
|
| 1322 |
|
| 1323 | static u8 margining_hw_result_val(const u32 *results,
|
| 1324 | enum usb4_margining_lane lane,
|
| 1325 | bool right_high)
|
| 1326 | {
|
| 1327 | u32 val;
|
| 1328 |
|
| 1329 | if (lane == USB4_MARGINING_LANE_RX0)
|
| 1330 | val = results[1];
|
| 1331 | else if (lane == USB4_MARGINING_LANE_RX1)
|
| 1332 | val = results[1] >> USB4_MARGIN_HW_RES_LANE_SHIFT;
|
| 1333 | else if (lane == USB4_MARGINING_LANE_RX2)
|
| 1334 | val = results[2];
|
| 1335 | else
|
| 1336 | val = 0;
|
| 1337 |
|
| 1338 | return right_high ? val : val >> USB4_MARGIN_HW_RES_LL_SHIFT;
|
| 1339 | }
|
| 1340 |
|
| 1341 | static void margining_hw_result_format(struct seq_file *s,
|
| 1342 | const struct tb_margining *margining,
|
| 1343 | enum usb4_margining_lane lane)
|
| 1344 | {
|
| 1345 | u8 val;
|
| 1346 |
|
| 1347 | if (margining->time) {
|
| 1348 | val = margining_hw_result_val(margining->results, lane, true);
|
| 1349 | seq_printf(s, "# lane %u right time margin: ", lane);
|
| 1350 | time_margin_show(s, margining, val);
|
| 1351 | val = margining_hw_result_val(margining->results, lane, false);
|
| 1352 | seq_printf(s, "# lane %u left time margin: ", lane);
|
| 1353 | time_margin_show(s, margining, val);
|
| 1354 | } else {
|
| 1355 | val = margining_hw_result_val(margining->results, lane, true);
|
| 1356 | seq_printf(s, "# lane %u high voltage margin: ", lane);
|
| 1357 | voltage_margin_show(s, margining, val);
|
| 1358 | val = margining_hw_result_val(margining->results, lane, false);
|
| 1359 | seq_printf(s, "# lane %u low voltage margin: ", lane);
|
| 1360 | voltage_margin_show(s, margining, val);
|
| 1361 | }
|
| 1362 | }
|
| 1363 |
|
| 1364 | static int margining_results_show(struct seq_file *s, void *not_used)
|
| 1365 | {
|
| 1366 | struct tb_margining *margining = s->private;
|
| 1367 | struct tb *tb = margining->port->sw->tb;
|
| 1368 |
|
| 1369 | if (mutex_lock_interruptible(&tb->lock))
|
| 1370 | return -ERESTARTSYS;
|
| 1371 |
|
| 1372 |
|
| 1373 | seq_printf(s, "0x%08x\n", margining->results[0]);
|
| 1374 |
|
| 1375 | if (!margining->software) {
|
| 1376 | for (int i = 1; i < ARRAY_SIZE(margining->results); i++)
|
| 1377 | seq_printf(s, "0x%08x\n", margining->results[i]);
|
| 1378 |
|
| 1379 | if (margining->lanes == USB4_MARGINING_LANE_ALL) {
|
| 1380 | margining_hw_result_format(s, margining,
|
| 1381 | USB4_MARGINING_LANE_RX0);
|
| 1382 | margining_hw_result_format(s, margining,
|
| 1383 | USB4_MARGINING_LANE_RX1);
|
| 1384 | if (margining->asym_rx)
|
| 1385 | margining_hw_result_format(s, margining,
|
| 1386 | USB4_MARGINING_LANE_RX2);
|
| 1387 | } else {
|
| 1388 | margining_hw_result_format(s, margining,
|
| 1389 | margining->lanes);
|
| 1390 | }
|
| 1391 | } else {
|
| 1392 | u32 lane_errors, result;
|
| 1393 |
|
| 1394 | seq_printf(s, "0x%08x\n", margining->results[1]);
|
| 1395 |
|
| 1396 | result = FIELD_GET(USB4_MARGIN_SW_LANES_MASK, margining->results[0]);
|
| 1397 | if (result == USB4_MARGINING_LANE_RX0 ||
|
| 1398 | result == USB4_MARGINING_LANE_ALL) {
|
| 1399 | lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK,
|
| 1400 | margining->results[1]);
|
| 1401 | seq_printf(s, "# lane 0 errors: %u\n", lane_errors);
|
| 1402 | }
|
| 1403 | if (result == USB4_MARGINING_LANE_RX1 ||
|
| 1404 | result == USB4_MARGINING_LANE_ALL) {
|
| 1405 | lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK,
|
| 1406 | margining->results[1]);
|
| 1407 | seq_printf(s, "# lane 1 errors: %u\n", lane_errors);
|
| 1408 | }
|
| 1409 | if (margining->asym_rx &&
|
| 1410 | (result == USB4_MARGINING_LANE_RX2 ||
|
| 1411 | result == USB4_MARGINING_LANE_ALL)) {
|
| 1412 | lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK,
|
| 1413 | margining->results[1]);
|
| 1414 | seq_printf(s, "# lane 2 errors: %u\n", lane_errors);
|
| 1415 | }
|
| 1416 | }
|
| 1417 |
|
| 1418 | mutex_unlock(&tb->lock);
|
| 1419 | return 0;
|
| 1420 | }
|
| 1421 | DEBUGFS_ATTR_RW(margining_results);
|
| 1422 |
|
| 1423 | static ssize_t margining_test_write(struct file *file,
|
| 1424 | const char __user *user_buf,
|
| 1425 | size_t count, loff_t *ppos)
|
| 1426 | {
|
| 1427 | struct seq_file *s = file->private_data;
|
| 1428 | struct tb_margining *margining = s->private;
|
| 1429 | struct tb *tb = margining->port->sw->tb;
|
| 1430 | int ret = 0;
|
| 1431 | char *buf;
|
| 1432 |
|
| 1433 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 1434 | if (IS_ERR(buf))
|
| 1435 | return PTR_ERR(buf);
|
| 1436 |
|
| 1437 | buf[count - 1] = '\0';
|
| 1438 |
|
| 1439 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 1440 | ret = -ERESTARTSYS;
|
| 1441 | goto out_free;
|
| 1442 | }
|
| 1443 |
|
| 1444 | if (!strcmp(buf, "time") && supports_time(margining))
|
| 1445 | margining->time = true;
|
| 1446 | else if (!strcmp(buf, "voltage"))
|
| 1447 | margining->time = false;
|
| 1448 | else
|
| 1449 | ret = -EINVAL;
|
| 1450 |
|
| 1451 | mutex_unlock(&tb->lock);
|
| 1452 |
|
| 1453 | out_free:
|
| 1454 | free_page((unsigned long)buf);
|
| 1455 | return ret ? ret : count;
|
| 1456 | }
|
| 1457 |
|
| 1458 | static int margining_test_show(struct seq_file *s, void *not_used)
|
| 1459 | {
|
| 1460 | struct tb_margining *margining = s->private;
|
| 1461 | struct tb *tb = margining->port->sw->tb;
|
| 1462 |
|
| 1463 | if (mutex_lock_interruptible(&tb->lock))
|
| 1464 | return -ERESTARTSYS;
|
| 1465 |
|
| 1466 | if (supports_time(margining)) {
|
| 1467 | if (margining->time)
|
| 1468 | seq_puts(s, "voltage [time]\n");
|
| 1469 | else
|
| 1470 | seq_puts(s, "[voltage] time\n");
|
| 1471 | } else {
|
| 1472 | seq_puts(s, "[voltage]\n");
|
| 1473 | }
|
| 1474 |
|
| 1475 | mutex_unlock(&tb->lock);
|
| 1476 | return 0;
|
| 1477 | }
|
| 1478 | DEBUGFS_ATTR_RW(margining_test);
|
| 1479 |
|
| 1480 | static ssize_t margining_margin_write(struct file *file,
|
| 1481 | const char __user *user_buf,
|
| 1482 | size_t count, loff_t *ppos)
|
| 1483 | {
|
| 1484 | struct seq_file *s = file->private_data;
|
| 1485 | struct tb_margining *margining = s->private;
|
| 1486 | struct tb *tb = margining->port->sw->tb;
|
| 1487 | int ret = 0;
|
| 1488 | char *buf;
|
| 1489 |
|
| 1490 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 1491 | if (IS_ERR(buf))
|
| 1492 | return PTR_ERR(buf);
|
| 1493 |
|
| 1494 | buf[count - 1] = '\0';
|
| 1495 |
|
| 1496 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 1497 | ret = -ERESTARTSYS;
|
| 1498 | goto out_free;
|
| 1499 | }
|
| 1500 |
|
| 1501 | if (margining->time) {
|
| 1502 | if (!strcmp(buf, "left"))
|
| 1503 | margining->right_high = false;
|
| 1504 | else if (!strcmp(buf, "right"))
|
| 1505 | margining->right_high = true;
|
| 1506 | else
|
| 1507 | ret = -EINVAL;
|
| 1508 | } else {
|
| 1509 | if (!strcmp(buf, "low"))
|
| 1510 | margining->right_high = false;
|
| 1511 | else if (!strcmp(buf, "high"))
|
| 1512 | margining->right_high = true;
|
| 1513 | else
|
| 1514 | ret = -EINVAL;
|
| 1515 | }
|
| 1516 |
|
| 1517 | mutex_unlock(&tb->lock);
|
| 1518 |
|
| 1519 | out_free:
|
| 1520 | free_page((unsigned long)buf);
|
| 1521 | return ret ? ret : count;
|
| 1522 | }
|
| 1523 |
|
| 1524 | static int margining_margin_show(struct seq_file *s, void *not_used)
|
| 1525 | {
|
| 1526 | struct tb_margining *margining = s->private;
|
| 1527 | struct tb *tb = margining->port->sw->tb;
|
| 1528 |
|
| 1529 | if (mutex_lock_interruptible(&tb->lock))
|
| 1530 | return -ERESTARTSYS;
|
| 1531 |
|
| 1532 | if (margining->time) {
|
| 1533 | if (margining->right_high)
|
| 1534 | seq_puts(s, "left [right]\n");
|
| 1535 | else
|
| 1536 | seq_puts(s, "[left] right\n");
|
| 1537 | } else {
|
| 1538 | if (margining->right_high)
|
| 1539 | seq_puts(s, "low [high]\n");
|
| 1540 | else
|
| 1541 | seq_puts(s, "[low] high\n");
|
| 1542 | }
|
| 1543 |
|
| 1544 | mutex_unlock(&tb->lock);
|
| 1545 | return 0;
|
| 1546 | }
|
| 1547 | DEBUGFS_ATTR_RW(margining_margin);
|
| 1548 |
|
| 1549 | static ssize_t margining_eye_write(struct file *file,
|
| 1550 | const char __user *user_buf,
|
| 1551 | size_t count, loff_t *ppos)
|
| 1552 | {
|
| 1553 | struct seq_file *s = file->private_data;
|
| 1554 | struct tb_port *port = s->private;
|
| 1555 | struct usb4_port *usb4 = port->usb4;
|
| 1556 | struct tb *tb = port->sw->tb;
|
| 1557 | int ret = 0;
|
| 1558 | char *buf;
|
| 1559 |
|
| 1560 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 1561 | if (IS_ERR(buf))
|
| 1562 | return PTR_ERR(buf);
|
| 1563 |
|
| 1564 | buf[count - 1] = '\0';
|
| 1565 |
|
| 1566 | scoped_cond_guard(mutex_intr, ret = -ERESTARTSYS, &tb->lock) {
|
| 1567 | if (!strcmp(buf, "lower"))
|
| 1568 | usb4->margining->upper_eye = false;
|
| 1569 | else if (!strcmp(buf, "upper"))
|
| 1570 | usb4->margining->upper_eye = true;
|
| 1571 | else
|
| 1572 | ret = -EINVAL;
|
| 1573 | }
|
| 1574 |
|
| 1575 | free_page((unsigned long)buf);
|
| 1576 | return ret ? ret : count;
|
| 1577 | }
|
| 1578 |
|
| 1579 | static int margining_eye_show(struct seq_file *s, void *not_used)
|
| 1580 | {
|
| 1581 | struct tb_port *port = s->private;
|
| 1582 | struct usb4_port *usb4 = port->usb4;
|
| 1583 | struct tb *tb = port->sw->tb;
|
| 1584 |
|
| 1585 | scoped_guard(mutex_intr, &tb->lock) {
|
| 1586 | if (usb4->margining->upper_eye)
|
| 1587 | seq_puts(s, "lower [upper]\n");
|
| 1588 | else
|
| 1589 | seq_puts(s, "[lower] upper\n");
|
| 1590 |
|
| 1591 | return 0;
|
| 1592 | }
|
| 1593 |
|
| 1594 | return -ERESTARTSYS;
|
| 1595 | }
|
| 1596 | DEBUGFS_ATTR_RW(margining_eye);
|
| 1597 |
|
| 1598 | static struct tb_margining *margining_alloc(struct tb_port *port,
|
| 1599 | struct device *dev,
|
| 1600 | enum usb4_sb_target target,
|
| 1601 | u8 index, struct dentry *parent)
|
| 1602 | {
|
| 1603 | struct tb_margining *margining;
|
| 1604 | struct dentry *dir;
|
| 1605 | unsigned int val;
|
| 1606 | int ret;
|
| 1607 |
|
| 1608 | ret = tb_port_get_link_generation(port);
|
| 1609 | if (ret < 0) {
|
| 1610 | tb_port_warn(port, "failed to read link generation\n");
|
| 1611 | return NULL;
|
| 1612 | }
|
| 1613 |
|
| 1614 | margining = kzalloc(sizeof(*margining), GFP_KERNEL);
|
| 1615 | if (!margining)
|
| 1616 | return NULL;
|
| 1617 |
|
| 1618 | margining->port = port;
|
| 1619 | margining->target = target;
|
| 1620 | margining->index = index;
|
| 1621 | margining->dev = dev;
|
| 1622 | margining->gen = ret;
|
| 1623 | margining->asym_rx = tb_port_width_supported(port, TB_LINK_WIDTH_ASYM_RX);
|
| 1624 |
|
| 1625 | ret = usb4_port_margining_caps(port, target, index, margining->caps,
|
| 1626 | ARRAY_SIZE(margining->caps));
|
| 1627 | if (ret) {
|
| 1628 | kfree(margining);
|
| 1629 | return NULL;
|
| 1630 | }
|
| 1631 |
|
| 1632 |
|
| 1633 | if (supports_software(margining))
|
| 1634 | margining->software = true;
|
| 1635 |
|
| 1636 | if (margining->gen < 4) {
|
| 1637 | val = FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK, margining->caps[0]);
|
| 1638 | margining->voltage_steps = val;
|
| 1639 | val = FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK, margining->caps[0]);
|
| 1640 | margining->max_voltage_offset = 74 + val * 2;
|
| 1641 | } else {
|
| 1642 | val = FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_STEPS_MASK, margining->caps[2]);
|
| 1643 | margining->voltage_steps = val;
|
| 1644 | val = FIELD_GET(USB4_MARGIN_CAP_2_MAX_VOLTAGE_OFFSET_MASK, margining->caps[2]);
|
| 1645 | margining->max_voltage_offset = 74 + val * 2;
|
| 1646 | }
|
| 1647 |
|
| 1648 | if (supports_optional_voltage_offset_range(margining)) {
|
| 1649 | val = FIELD_GET(USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK,
|
| 1650 | margining->caps[0]);
|
| 1651 | margining->voltage_steps_optional_range = val;
|
| 1652 | val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK,
|
| 1653 | margining->caps[1]);
|
| 1654 | margining->max_voltage_offset_optional_range = 74 + val * 2;
|
| 1655 | }
|
| 1656 |
|
| 1657 | if (supports_time(margining)) {
|
| 1658 | val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK, margining->caps[1]);
|
| 1659 | margining->time_steps = val;
|
| 1660 | val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK, margining->caps[1]);
|
| 1661 | |
| 1662 | * Store it as mUI (milli Unit Interval) because we want
|
| 1663 | * to keep it as integer.
|
| 1664 |
|
| 1665 | margining->max_time_offset = 200 + 10 * val;
|
| 1666 | }
|
| 1667 |
|
| 1668 | dir = debugfs_create_dir("margining", parent);
|
| 1669 | if (supports_hardware(margining)) {
|
| 1670 | val = FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK, margining->caps[1]);
|
| 1671 | margining->min_ber_level = val;
|
| 1672 | val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK, margining->caps[1]);
|
| 1673 | margining->max_ber_level = val;
|
| 1674 |
|
| 1675 |
|
| 1676 | margining->ber_level = margining->min_ber_level;
|
| 1677 |
|
| 1678 | debugfs_create_file("ber_level_contour", 0400, dir, margining,
|
| 1679 | &margining_ber_level_fops);
|
| 1680 | }
|
| 1681 | debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops);
|
| 1682 | debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops);
|
| 1683 | debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops);
|
| 1684 | debugfs_create_file("run", 0600, dir, margining, &margining_run_fops);
|
| 1685 | debugfs_create_file("results", 0600, dir, margining,
|
| 1686 | &margining_results_fops);
|
| 1687 | debugfs_create_file("test", 0600, dir, margining, &margining_test_fops);
|
| 1688 | if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL ||
|
| 1689 | (supports_time(margining) &&
|
| 1690 | independent_time_margins(margining) == USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR))
|
| 1691 | debugfs_create_file("margin", 0600, dir, margining, &margining_margin_fops);
|
| 1692 |
|
| 1693 | margining->error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR;
|
| 1694 | margining->dwell_time = MIN_DWELL_TIME;
|
| 1695 |
|
| 1696 | if (supports_optional_voltage_offset_range(margining))
|
| 1697 | debugfs_create_file("optional_voltage_offset", DEBUGFS_MODE, dir, margining,
|
| 1698 | &margining_optional_voltage_offset_fops);
|
| 1699 |
|
| 1700 | if (supports_software(margining)) {
|
| 1701 | debugfs_create_file("voltage_time_offset", DEBUGFS_MODE, dir, margining,
|
| 1702 | &margining_voltage_time_offset_fops);
|
| 1703 | debugfs_create_file("error_counter", DEBUGFS_MODE, dir, margining,
|
| 1704 | &margining_error_counter_fops);
|
| 1705 | debugfs_create_file("dwell_time", DEBUGFS_MODE, dir, margining,
|
| 1706 | &margining_dwell_time_fops);
|
| 1707 | }
|
| 1708 |
|
| 1709 | if (margining->gen >= 4)
|
| 1710 | debugfs_create_file("eye", 0600, dir, port, &margining_eye_fops);
|
| 1711 |
|
| 1712 | return margining;
|
| 1713 | }
|
| 1714 |
|
| 1715 | static void margining_port_init(struct tb_port *port)
|
| 1716 | {
|
| 1717 | struct dentry *parent;
|
| 1718 | char dir_name[10];
|
| 1719 |
|
| 1720 | if (!port->usb4)
|
| 1721 | return;
|
| 1722 |
|
| 1723 | snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
|
| 1724 | parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
|
| 1725 | port->usb4->margining = margining_alloc(port, &port->usb4->dev,
|
| 1726 | USB4_SB_TARGET_ROUTER, 0,
|
| 1727 | parent);
|
| 1728 | }
|
| 1729 |
|
| 1730 | static void margining_port_remove(struct tb_port *port)
|
| 1731 | {
|
| 1732 | struct dentry *parent;
|
| 1733 | char dir_name[10];
|
| 1734 |
|
| 1735 | if (!port->usb4)
|
| 1736 | return;
|
| 1737 |
|
| 1738 | snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
|
| 1739 | parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
|
| 1740 | if (parent)
|
| 1741 | debugfs_lookup_and_remove("margining", parent);
|
| 1742 |
|
| 1743 | kfree(port->usb4->margining);
|
| 1744 | port->usb4->margining = NULL;
|
| 1745 | }
|
| 1746 |
|
| 1747 | static void margining_switch_init(struct tb_switch *sw)
|
| 1748 | {
|
| 1749 | struct tb_port *upstream, *downstream;
|
| 1750 | struct tb_switch *parent_sw;
|
| 1751 | u64 route = tb_route(sw);
|
| 1752 |
|
| 1753 | if (!route)
|
| 1754 | return;
|
| 1755 |
|
| 1756 | upstream = tb_upstream_port(sw);
|
| 1757 | parent_sw = tb_switch_parent(sw);
|
| 1758 | downstream = tb_port_at(route, parent_sw);
|
| 1759 |
|
| 1760 | margining_port_init(downstream);
|
| 1761 | margining_port_init(upstream);
|
| 1762 | }
|
| 1763 |
|
| 1764 | static void margining_switch_remove(struct tb_switch *sw)
|
| 1765 | {
|
| 1766 | struct tb_port *upstream, *downstream;
|
| 1767 | struct tb_switch *parent_sw;
|
| 1768 | u64 route = tb_route(sw);
|
| 1769 |
|
| 1770 | if (!route)
|
| 1771 | return;
|
| 1772 |
|
| 1773 | upstream = tb_upstream_port(sw);
|
| 1774 | parent_sw = tb_switch_parent(sw);
|
| 1775 | downstream = tb_port_at(route, parent_sw);
|
| 1776 |
|
| 1777 | margining_port_remove(upstream);
|
| 1778 | margining_port_remove(downstream);
|
| 1779 | }
|
| 1780 |
|
| 1781 | static void margining_xdomain_init(struct tb_xdomain *xd)
|
| 1782 | {
|
| 1783 | struct tb_switch *parent_sw;
|
| 1784 | struct tb_port *downstream;
|
| 1785 |
|
| 1786 | parent_sw = tb_xdomain_parent(xd);
|
| 1787 | downstream = tb_port_at(xd->route, parent_sw);
|
| 1788 |
|
| 1789 | margining_port_init(downstream);
|
| 1790 | }
|
| 1791 |
|
| 1792 | static void margining_xdomain_remove(struct tb_xdomain *xd)
|
| 1793 | {
|
| 1794 | struct tb_switch *parent_sw;
|
| 1795 | struct tb_port *downstream;
|
| 1796 |
|
| 1797 | parent_sw = tb_xdomain_parent(xd);
|
| 1798 | downstream = tb_port_at(xd->route, parent_sw);
|
| 1799 | margining_port_remove(downstream);
|
| 1800 | }
|
| 1801 |
|
| 1802 | static void margining_retimer_init(struct tb_retimer *rt, struct dentry *debugfs_dir)
|
| 1803 | {
|
| 1804 | rt->margining = margining_alloc(rt->port, &rt->dev,
|
| 1805 | USB4_SB_TARGET_RETIMER, rt->index,
|
| 1806 | debugfs_dir);
|
| 1807 | }
|
| 1808 |
|
| 1809 | static void margining_retimer_remove(struct tb_retimer *rt)
|
| 1810 | {
|
| 1811 | kfree(rt->margining);
|
| 1812 | rt->margining = NULL;
|
| 1813 | }
|
| 1814 | #else
|
| 1815 | static inline void margining_switch_init(struct tb_switch *sw) { }
|
| 1816 | static inline void margining_switch_remove(struct tb_switch *sw) { }
|
| 1817 | static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
|
| 1818 | static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
|
| 1819 | static inline void margining_retimer_init(struct tb_retimer *rt,
|
| 1820 | struct dentry *debugfs_dir) { }
|
| 1821 | static inline void margining_retimer_remove(struct tb_retimer *rt) { }
|
| 1822 | #endif
|
| 1823 |
|
| 1824 | static int port_clear_all_counters(struct tb_port *port)
|
| 1825 | {
|
| 1826 | u32 *buf;
|
| 1827 | int ret;
|
| 1828 |
|
| 1829 | buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
|
| 1830 | GFP_KERNEL);
|
| 1831 | if (!buf)
|
| 1832 | return -ENOMEM;
|
| 1833 |
|
| 1834 | ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
|
| 1835 | COUNTER_SET_LEN * port->config.max_counters);
|
| 1836 | kfree(buf);
|
| 1837 |
|
| 1838 | return ret;
|
| 1839 | }
|
| 1840 |
|
| 1841 | static ssize_t counters_write(struct file *file, const char __user *user_buf,
|
| 1842 | size_t count, loff_t *ppos)
|
| 1843 | {
|
| 1844 | struct seq_file *s = file->private_data;
|
| 1845 | struct tb_port *port = s->private;
|
| 1846 | struct tb_switch *sw = port->sw;
|
| 1847 | struct tb *tb = port->sw->tb;
|
| 1848 | char *buf;
|
| 1849 | int ret;
|
| 1850 |
|
| 1851 | buf = validate_and_copy_from_user(user_buf, &count);
|
| 1852 | if (IS_ERR(buf))
|
| 1853 | return PTR_ERR(buf);
|
| 1854 |
|
| 1855 | pm_runtime_get_sync(&sw->dev);
|
| 1856 |
|
| 1857 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 1858 | ret = -ERESTARTSYS;
|
| 1859 | goto out;
|
| 1860 | }
|
| 1861 |
|
| 1862 |
|
| 1863 | if (buf[0] == '\n') {
|
| 1864 | ret = port_clear_all_counters(port);
|
| 1865 | } else {
|
| 1866 | char *line = buf;
|
| 1867 | u32 val, offset;
|
| 1868 |
|
| 1869 | ret = -EINVAL;
|
| 1870 | while (parse_line(&line, &offset, &val, 1, 4)) {
|
| 1871 | ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
|
| 1872 | offset, 1);
|
| 1873 | if (ret)
|
| 1874 | break;
|
| 1875 | }
|
| 1876 | }
|
| 1877 |
|
| 1878 | mutex_unlock(&tb->lock);
|
| 1879 |
|
| 1880 | out:
|
| 1881 | pm_runtime_mark_last_busy(&sw->dev);
|
| 1882 | pm_runtime_put_autosuspend(&sw->dev);
|
| 1883 | free_page((unsigned long)buf);
|
| 1884 |
|
| 1885 | return ret < 0 ? ret : count;
|
| 1886 | }
|
| 1887 |
|
| 1888 | static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
|
| 1889 | struct tb_port *port, unsigned int cap,
|
| 1890 | unsigned int offset, u8 cap_id, u8 vsec_id,
|
| 1891 | int dwords)
|
| 1892 | {
|
| 1893 | int i, ret;
|
| 1894 | u32 data;
|
| 1895 |
|
| 1896 | for (i = 0; i < dwords; i++) {
|
| 1897 | if (port)
|
| 1898 | ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
|
| 1899 | else
|
| 1900 | ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
|
| 1901 | if (ret) {
|
| 1902 | seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
|
| 1903 | continue;
|
| 1904 | }
|
| 1905 |
|
| 1906 | seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
|
| 1907 | offset + i, cap_id, vsec_id, data);
|
| 1908 | }
|
| 1909 | }
|
| 1910 |
|
| 1911 | static void cap_show(struct seq_file *s, struct tb_switch *sw,
|
| 1912 | struct tb_port *port, unsigned int cap, u8 cap_id,
|
| 1913 | u8 vsec_id, int length)
|
| 1914 | {
|
| 1915 | int ret, offset = 0;
|
| 1916 |
|
| 1917 | while (length > 0) {
|
| 1918 | int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
|
| 1919 | u32 data[TB_MAX_CONFIG_RW_LENGTH];
|
| 1920 |
|
| 1921 | if (port)
|
| 1922 | ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
|
| 1923 | dwords);
|
| 1924 | else
|
| 1925 | ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
|
| 1926 | if (ret) {
|
| 1927 | cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
|
| 1928 | return;
|
| 1929 | }
|
| 1930 |
|
| 1931 | for (i = 0; i < dwords; i++) {
|
| 1932 | seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
|
| 1933 | cap + offset + i, offset + i,
|
| 1934 | cap_id, vsec_id, data[i]);
|
| 1935 | }
|
| 1936 |
|
| 1937 | length -= dwords;
|
| 1938 | offset += dwords;
|
| 1939 | }
|
| 1940 | }
|
| 1941 |
|
| 1942 | static void port_cap_show(struct tb_port *port, struct seq_file *s,
|
| 1943 | unsigned int cap)
|
| 1944 | {
|
| 1945 | struct tb_cap_any header;
|
| 1946 | u8 vsec_id = 0;
|
| 1947 | size_t length;
|
| 1948 | int ret;
|
| 1949 |
|
| 1950 | ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
|
| 1951 | if (ret) {
|
| 1952 | seq_printf(s, "0x%04x <capability read failed>\n", cap);
|
| 1953 | return;
|
| 1954 | }
|
| 1955 |
|
| 1956 | switch (header.basic.cap) {
|
| 1957 | case TB_PORT_CAP_PHY:
|
| 1958 | length = PORT_CAP_LANE_LEN;
|
| 1959 | break;
|
| 1960 |
|
| 1961 | case TB_PORT_CAP_TIME1:
|
| 1962 | if (usb4_switch_version(port->sw) < 2)
|
| 1963 | length = PORT_CAP_TMU_V1_LEN;
|
| 1964 | else
|
| 1965 | length = PORT_CAP_TMU_V2_LEN;
|
| 1966 | break;
|
| 1967 |
|
| 1968 | case TB_PORT_CAP_POWER:
|
| 1969 | length = PORT_CAP_POWER_LEN;
|
| 1970 | break;
|
| 1971 |
|
| 1972 | case TB_PORT_CAP_ADAP:
|
| 1973 | if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
|
| 1974 | if (usb4_switch_version(port->sw) < 2)
|
| 1975 | length = PORT_CAP_V1_PCIE_LEN;
|
| 1976 | else
|
| 1977 | length = PORT_CAP_V2_PCIE_LEN;
|
| 1978 | } else if (tb_port_is_dpin(port)) {
|
| 1979 | if (usb4_switch_version(port->sw) < 2)
|
| 1980 | length = PORT_CAP_DP_V1_LEN;
|
| 1981 | else
|
| 1982 | length = PORT_CAP_DP_V2_LEN;
|
| 1983 | } else if (tb_port_is_dpout(port)) {
|
| 1984 | length = PORT_CAP_DP_V1_LEN;
|
| 1985 | } else if (tb_port_is_usb3_down(port) ||
|
| 1986 | tb_port_is_usb3_up(port)) {
|
| 1987 | length = PORT_CAP_USB3_LEN;
|
| 1988 | } else {
|
| 1989 | seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
|
| 1990 | cap, header.basic.cap);
|
| 1991 | return;
|
| 1992 | }
|
| 1993 | break;
|
| 1994 |
|
| 1995 | case TB_PORT_CAP_VSE:
|
| 1996 | if (!header.extended_short.length) {
|
| 1997 | ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
|
| 1998 | cap + 1, 1);
|
| 1999 | if (ret) {
|
| 2000 | seq_printf(s, "0x%04x <capability read failed>\n",
|
| 2001 | cap + 1);
|
| 2002 | return;
|
| 2003 | }
|
| 2004 | length = header.extended_long.length;
|
| 2005 | vsec_id = header.extended_short.vsec_id;
|
| 2006 | } else {
|
| 2007 | length = header.extended_short.length;
|
| 2008 | vsec_id = header.extended_short.vsec_id;
|
| 2009 | }
|
| 2010 | break;
|
| 2011 |
|
| 2012 | case TB_PORT_CAP_USB4:
|
| 2013 | length = PORT_CAP_USB4_LEN;
|
| 2014 | break;
|
| 2015 |
|
| 2016 | default:
|
| 2017 | seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
|
| 2018 | cap, header.basic.cap);
|
| 2019 | return;
|
| 2020 | }
|
| 2021 |
|
| 2022 | cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
|
| 2023 | }
|
| 2024 |
|
| 2025 | static void port_caps_show(struct tb_port *port, struct seq_file *s)
|
| 2026 | {
|
| 2027 | int cap;
|
| 2028 |
|
| 2029 | cap = tb_port_next_cap(port, 0);
|
| 2030 | while (cap > 0) {
|
| 2031 | port_cap_show(port, s, cap);
|
| 2032 | cap = tb_port_next_cap(port, cap);
|
| 2033 | }
|
| 2034 | }
|
| 2035 |
|
| 2036 | static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
|
| 2037 | {
|
| 2038 | u32 data[PORT_CAP_BASIC_LEN];
|
| 2039 | int ret, i;
|
| 2040 |
|
| 2041 | ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
|
| 2042 | if (ret)
|
| 2043 | return ret;
|
| 2044 |
|
| 2045 | for (i = 0; i < ARRAY_SIZE(data); i++)
|
| 2046 | seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
|
| 2047 |
|
| 2048 | return 0;
|
| 2049 | }
|
| 2050 |
|
| 2051 | static int port_regs_show(struct seq_file *s, void *not_used)
|
| 2052 | {
|
| 2053 | struct tb_port *port = s->private;
|
| 2054 | struct tb_switch *sw = port->sw;
|
| 2055 | struct tb *tb = sw->tb;
|
| 2056 | int ret;
|
| 2057 |
|
| 2058 | pm_runtime_get_sync(&sw->dev);
|
| 2059 |
|
| 2060 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2061 | ret = -ERESTARTSYS;
|
| 2062 | goto out_rpm_put;
|
| 2063 | }
|
| 2064 |
|
| 2065 | seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
|
| 2066 |
|
| 2067 | ret = port_basic_regs_show(port, s);
|
| 2068 | if (ret)
|
| 2069 | goto out_unlock;
|
| 2070 |
|
| 2071 | port_caps_show(port, s);
|
| 2072 |
|
| 2073 | out_unlock:
|
| 2074 | mutex_unlock(&tb->lock);
|
| 2075 | out_rpm_put:
|
| 2076 | pm_runtime_mark_last_busy(&sw->dev);
|
| 2077 | pm_runtime_put_autosuspend(&sw->dev);
|
| 2078 |
|
| 2079 | return ret;
|
| 2080 | }
|
| 2081 | DEBUGFS_ATTR_RW(port_regs);
|
| 2082 |
|
| 2083 | static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
|
| 2084 | unsigned int cap)
|
| 2085 | {
|
| 2086 | struct tb_cap_any header;
|
| 2087 | int ret, length;
|
| 2088 | u8 vsec_id = 0;
|
| 2089 |
|
| 2090 | ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
|
| 2091 | if (ret) {
|
| 2092 | seq_printf(s, "0x%04x <capability read failed>\n", cap);
|
| 2093 | return;
|
| 2094 | }
|
| 2095 |
|
| 2096 | if (header.basic.cap == TB_SWITCH_CAP_VSE) {
|
| 2097 | if (!header.extended_short.length) {
|
| 2098 | ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
|
| 2099 | cap + 1, 1);
|
| 2100 | if (ret) {
|
| 2101 | seq_printf(s, "0x%04x <capability read failed>\n",
|
| 2102 | cap + 1);
|
| 2103 | return;
|
| 2104 | }
|
| 2105 | length = header.extended_long.length;
|
| 2106 | } else {
|
| 2107 | length = header.extended_short.length;
|
| 2108 | }
|
| 2109 | vsec_id = header.extended_short.vsec_id;
|
| 2110 | } else {
|
| 2111 | if (header.basic.cap == TB_SWITCH_CAP_TMU) {
|
| 2112 | length = SWITCH_CAP_TMU_LEN;
|
| 2113 | } else {
|
| 2114 | seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
|
| 2115 | cap, header.basic.cap);
|
| 2116 | return;
|
| 2117 | }
|
| 2118 | }
|
| 2119 |
|
| 2120 | cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
|
| 2121 | }
|
| 2122 |
|
| 2123 | static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
|
| 2124 | {
|
| 2125 | int cap;
|
| 2126 |
|
| 2127 | cap = tb_switch_next_cap(sw, 0);
|
| 2128 | while (cap > 0) {
|
| 2129 | switch_cap_show(sw, s, cap);
|
| 2130 | cap = tb_switch_next_cap(sw, cap);
|
| 2131 | }
|
| 2132 | }
|
| 2133 |
|
| 2134 | static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
|
| 2135 | {
|
| 2136 | u32 data[SWITCH_CAP_BASIC_LEN];
|
| 2137 | size_t dwords;
|
| 2138 | int ret, i;
|
| 2139 |
|
| 2140 |
|
| 2141 | if (tb_switch_is_usb4(sw))
|
| 2142 | dwords = ARRAY_SIZE(data);
|
| 2143 | else
|
| 2144 | dwords = 5;
|
| 2145 |
|
| 2146 | ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
|
| 2147 | if (ret)
|
| 2148 | return ret;
|
| 2149 |
|
| 2150 | for (i = 0; i < dwords; i++)
|
| 2151 | seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
|
| 2152 |
|
| 2153 | return 0;
|
| 2154 | }
|
| 2155 |
|
| 2156 | static int switch_regs_show(struct seq_file *s, void *not_used)
|
| 2157 | {
|
| 2158 | struct tb_switch *sw = s->private;
|
| 2159 | struct tb *tb = sw->tb;
|
| 2160 | int ret;
|
| 2161 |
|
| 2162 | pm_runtime_get_sync(&sw->dev);
|
| 2163 |
|
| 2164 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2165 | ret = -ERESTARTSYS;
|
| 2166 | goto out_rpm_put;
|
| 2167 | }
|
| 2168 |
|
| 2169 | seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
|
| 2170 |
|
| 2171 | ret = switch_basic_regs_show(sw, s);
|
| 2172 | if (ret)
|
| 2173 | goto out_unlock;
|
| 2174 |
|
| 2175 | switch_caps_show(sw, s);
|
| 2176 |
|
| 2177 | out_unlock:
|
| 2178 | mutex_unlock(&tb->lock);
|
| 2179 | out_rpm_put:
|
| 2180 | pm_runtime_mark_last_busy(&sw->dev);
|
| 2181 | pm_runtime_put_autosuspend(&sw->dev);
|
| 2182 |
|
| 2183 | return ret;
|
| 2184 | }
|
| 2185 | DEBUGFS_ATTR_RW(switch_regs);
|
| 2186 |
|
| 2187 | static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
|
| 2188 | {
|
| 2189 | u32 data[PATH_LEN];
|
| 2190 | int ret, i;
|
| 2191 |
|
| 2192 | ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
|
| 2193 | ARRAY_SIZE(data));
|
| 2194 | if (ret) {
|
| 2195 | seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
|
| 2196 | return ret;
|
| 2197 | }
|
| 2198 |
|
| 2199 | for (i = 0; i < ARRAY_SIZE(data); i++) {
|
| 2200 | seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
|
| 2201 | hopid * PATH_LEN + i, i, hopid, data[i]);
|
| 2202 | }
|
| 2203 |
|
| 2204 | return 0;
|
| 2205 | }
|
| 2206 |
|
| 2207 | static int path_show(struct seq_file *s, void *not_used)
|
| 2208 | {
|
| 2209 | struct tb_port *port = s->private;
|
| 2210 | struct tb_switch *sw = port->sw;
|
| 2211 | struct tb *tb = sw->tb;
|
| 2212 | int start, i, ret = 0;
|
| 2213 |
|
| 2214 | pm_runtime_get_sync(&sw->dev);
|
| 2215 |
|
| 2216 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2217 | ret = -ERESTARTSYS;
|
| 2218 | goto out_rpm_put;
|
| 2219 | }
|
| 2220 |
|
| 2221 | seq_puts(s, "# offset relative_offset in_hop_id value\n");
|
| 2222 |
|
| 2223 |
|
| 2224 | if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
|
| 2225 | ret = path_show_one(port, s, 0);
|
| 2226 | if (ret)
|
| 2227 | goto out_unlock;
|
| 2228 | }
|
| 2229 |
|
| 2230 | start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
|
| 2231 |
|
| 2232 | for (i = start; i <= port->config.max_in_hop_id; i++) {
|
| 2233 | ret = path_show_one(port, s, i);
|
| 2234 | if (ret)
|
| 2235 | break;
|
| 2236 | }
|
| 2237 |
|
| 2238 | out_unlock:
|
| 2239 | mutex_unlock(&tb->lock);
|
| 2240 | out_rpm_put:
|
| 2241 | pm_runtime_mark_last_busy(&sw->dev);
|
| 2242 | pm_runtime_put_autosuspend(&sw->dev);
|
| 2243 |
|
| 2244 | return ret;
|
| 2245 | }
|
| 2246 | DEBUGFS_ATTR_RO(path);
|
| 2247 |
|
| 2248 | static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
|
| 2249 | int counter)
|
| 2250 | {
|
| 2251 | u32 data[COUNTER_SET_LEN];
|
| 2252 | int ret, i;
|
| 2253 |
|
| 2254 | ret = tb_port_read(port, data, TB_CFG_COUNTERS,
|
| 2255 | counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
|
| 2256 | if (ret) {
|
| 2257 | seq_printf(s, "0x%04x <not accessible>\n",
|
| 2258 | counter * COUNTER_SET_LEN);
|
| 2259 | return ret;
|
| 2260 | }
|
| 2261 |
|
| 2262 | for (i = 0; i < ARRAY_SIZE(data); i++) {
|
| 2263 | seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
|
| 2264 | counter * COUNTER_SET_LEN + i, i, counter, data[i]);
|
| 2265 | }
|
| 2266 |
|
| 2267 | return 0;
|
| 2268 | }
|
| 2269 |
|
| 2270 | static int counters_show(struct seq_file *s, void *not_used)
|
| 2271 | {
|
| 2272 | struct tb_port *port = s->private;
|
| 2273 | struct tb_switch *sw = port->sw;
|
| 2274 | struct tb *tb = sw->tb;
|
| 2275 | int i, ret = 0;
|
| 2276 |
|
| 2277 | pm_runtime_get_sync(&sw->dev);
|
| 2278 |
|
| 2279 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2280 | ret = -ERESTARTSYS;
|
| 2281 | goto out;
|
| 2282 | }
|
| 2283 |
|
| 2284 | seq_puts(s, "# offset relative_offset counter_id value\n");
|
| 2285 |
|
| 2286 | for (i = 0; i < port->config.max_counters; i++) {
|
| 2287 | ret = counter_set_regs_show(port, s, i);
|
| 2288 | if (ret)
|
| 2289 | break;
|
| 2290 | }
|
| 2291 |
|
| 2292 | mutex_unlock(&tb->lock);
|
| 2293 |
|
| 2294 | out:
|
| 2295 | pm_runtime_mark_last_busy(&sw->dev);
|
| 2296 | pm_runtime_put_autosuspend(&sw->dev);
|
| 2297 |
|
| 2298 | return ret;
|
| 2299 | }
|
| 2300 | DEBUGFS_ATTR_RW(counters);
|
| 2301 |
|
| 2302 | static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs,
|
| 2303 | size_t size, enum usb4_sb_target target, u8 index,
|
| 2304 | struct seq_file *s)
|
| 2305 | {
|
| 2306 | int ret, i;
|
| 2307 |
|
| 2308 | seq_puts(s, "# register value\n");
|
| 2309 |
|
| 2310 | for (i = 0; i < size; i++) {
|
| 2311 | const struct sb_reg *regs = &sb_regs[i];
|
| 2312 | u8 data[64];
|
| 2313 | int j;
|
| 2314 |
|
| 2315 | memset(data, 0, sizeof(data));
|
| 2316 | ret = usb4_port_sb_read(port, target, index, regs->reg, data,
|
| 2317 | regs->size);
|
| 2318 | if (ret)
|
| 2319 | return ret;
|
| 2320 |
|
| 2321 | seq_printf(s, "0x%02x", regs->reg);
|
| 2322 | for (j = 0; j < regs->size; j++)
|
| 2323 | seq_printf(s, " 0x%02x", data[j]);
|
| 2324 | seq_puts(s, "\n");
|
| 2325 | }
|
| 2326 |
|
| 2327 | return 0;
|
| 2328 | }
|
| 2329 |
|
| 2330 | static int port_sb_regs_show(struct seq_file *s, void *not_used)
|
| 2331 | {
|
| 2332 | struct tb_port *port = s->private;
|
| 2333 | struct tb_switch *sw = port->sw;
|
| 2334 | struct tb *tb = sw->tb;
|
| 2335 | int ret;
|
| 2336 |
|
| 2337 | pm_runtime_get_sync(&sw->dev);
|
| 2338 |
|
| 2339 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2340 | ret = -ERESTARTSYS;
|
| 2341 | goto out_rpm_put;
|
| 2342 | }
|
| 2343 |
|
| 2344 | ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
|
| 2345 | USB4_SB_TARGET_ROUTER, 0, s);
|
| 2346 |
|
| 2347 | mutex_unlock(&tb->lock);
|
| 2348 | out_rpm_put:
|
| 2349 | pm_runtime_mark_last_busy(&sw->dev);
|
| 2350 | pm_runtime_put_autosuspend(&sw->dev);
|
| 2351 |
|
| 2352 | return ret;
|
| 2353 | }
|
| 2354 | DEBUGFS_ATTR_RW(port_sb_regs);
|
| 2355 |
|
| 2356 | |
| 2357 | * tb_switch_debugfs_init() - Add debugfs entries for router
|
| 2358 | * @sw: Pointer to the router
|
| 2359 | *
|
| 2360 | * Adds debugfs directories and files for given router.
|
| 2361 |
|
| 2362 | void tb_switch_debugfs_init(struct tb_switch *sw)
|
| 2363 | {
|
| 2364 | struct dentry *debugfs_dir;
|
| 2365 | struct tb_port *port;
|
| 2366 |
|
| 2367 | debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
|
| 2368 | sw->debugfs_dir = debugfs_dir;
|
| 2369 | debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
|
| 2370 | &switch_regs_fops);
|
| 2371 |
|
| 2372 | tb_switch_for_each_port(sw, port) {
|
| 2373 | struct dentry *debugfs_dir;
|
| 2374 | char dir_name[10];
|
| 2375 |
|
| 2376 | if (port->disabled)
|
| 2377 | continue;
|
| 2378 | if (port->config.type == TB_TYPE_INACTIVE)
|
| 2379 | continue;
|
| 2380 |
|
| 2381 | snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
|
| 2382 | debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
|
| 2383 | debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
|
| 2384 | port, &port_regs_fops);
|
| 2385 | debugfs_create_file("path", 0400, debugfs_dir, port,
|
| 2386 | &path_fops);
|
| 2387 | if (port->config.counters_support)
|
| 2388 | debugfs_create_file("counters", 0600, debugfs_dir, port,
|
| 2389 | &counters_fops);
|
| 2390 | if (port->usb4)
|
| 2391 | debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
|
| 2392 | port, &port_sb_regs_fops);
|
| 2393 | }
|
| 2394 |
|
| 2395 | margining_switch_init(sw);
|
| 2396 | }
|
| 2397 |
|
| 2398 | |
| 2399 | * tb_switch_debugfs_remove() - Remove all router debugfs entries
|
| 2400 | * @sw: Pointer to the router
|
| 2401 | *
|
| 2402 | * Removes all previously added debugfs entries under this router.
|
| 2403 |
|
| 2404 | void tb_switch_debugfs_remove(struct tb_switch *sw)
|
| 2405 | {
|
| 2406 | margining_switch_remove(sw);
|
| 2407 | debugfs_remove_recursive(sw->debugfs_dir);
|
| 2408 | }
|
| 2409 |
|
| 2410 | void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
|
| 2411 | {
|
| 2412 | margining_xdomain_init(xd);
|
| 2413 | }
|
| 2414 |
|
| 2415 | void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
|
| 2416 | {
|
| 2417 | margining_xdomain_remove(xd);
|
| 2418 | }
|
| 2419 |
|
| 2420 | |
| 2421 | * tb_service_debugfs_init() - Add debugfs directory for service
|
| 2422 | * @svc: Thunderbolt service pointer
|
| 2423 | *
|
| 2424 | * Adds debugfs directory for service.
|
| 2425 |
|
| 2426 | void tb_service_debugfs_init(struct tb_service *svc)
|
| 2427 | {
|
| 2428 | svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
|
| 2429 | tb_debugfs_root);
|
| 2430 | }
|
| 2431 |
|
| 2432 | |
| 2433 | * tb_service_debugfs_remove() - Remove service debugfs directory
|
| 2434 | * @svc: Thunderbolt service pointer
|
| 2435 | *
|
| 2436 | * Removes the previously created debugfs directory for @svc.
|
| 2437 |
|
| 2438 | void tb_service_debugfs_remove(struct tb_service *svc)
|
| 2439 | {
|
| 2440 | debugfs_remove_recursive(svc->debugfs_dir);
|
| 2441 | svc->debugfs_dir = NULL;
|
| 2442 | }
|
| 2443 |
|
| 2444 | static int retimer_sb_regs_show(struct seq_file *s, void *not_used)
|
| 2445 | {
|
| 2446 | struct tb_retimer *rt = s->private;
|
| 2447 | struct tb *tb = rt->tb;
|
| 2448 | int ret;
|
| 2449 |
|
| 2450 | pm_runtime_get_sync(&rt->dev);
|
| 2451 |
|
| 2452 | if (mutex_lock_interruptible(&tb->lock)) {
|
| 2453 | ret = -ERESTARTSYS;
|
| 2454 | goto out_rpm_put;
|
| 2455 | }
|
| 2456 |
|
| 2457 | ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
|
| 2458 | USB4_SB_TARGET_RETIMER, rt->index, s);
|
| 2459 |
|
| 2460 | mutex_unlock(&tb->lock);
|
| 2461 | out_rpm_put:
|
| 2462 | pm_runtime_mark_last_busy(&rt->dev);
|
| 2463 | pm_runtime_put_autosuspend(&rt->dev);
|
| 2464 |
|
| 2465 | return ret;
|
| 2466 | }
|
| 2467 | DEBUGFS_ATTR_RW(retimer_sb_regs);
|
| 2468 |
|
| 2469 | |
| 2470 | * tb_retimer_debugfs_init() - Add debugfs directory for retimer
|
| 2471 | * @rt: Pointer to retimer structure
|
| 2472 | *
|
| 2473 | * Adds and populates retimer debugfs directory.
|
| 2474 |
|
| 2475 | void tb_retimer_debugfs_init(struct tb_retimer *rt)
|
| 2476 | {
|
| 2477 | struct dentry *debugfs_dir;
|
| 2478 |
|
| 2479 | debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root);
|
| 2480 | debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt,
|
| 2481 | &retimer_sb_regs_fops);
|
| 2482 | margining_retimer_init(rt, debugfs_dir);
|
| 2483 | }
|
| 2484 |
|
| 2485 | |
| 2486 | * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
|
| 2487 | * @rt: Pointer to retimer structure
|
| 2488 | *
|
| 2489 | * Removes the retimer debugfs directory along with its contents.
|
| 2490 |
|
| 2491 | void tb_retimer_debugfs_remove(struct tb_retimer *rt)
|
| 2492 | {
|
| 2493 | debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root);
|
| 2494 | margining_retimer_remove(rt);
|
| 2495 | }
|
| 2496 |
|
| 2497 | void tb_debugfs_init(void)
|
| 2498 | {
|
| 2499 | tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
|
| 2500 | }
|
| 2501 |
|
| 2502 | void tb_debugfs_exit(void)
|
| 2503 | {
|
| 2504 | debugfs_remove_recursive(tb_debugfs_root);
|
| 2505 | }
|