debugfs.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Debugfs interface
  4. *
  5. * Copyright (C) 2020, Intel Corporation
  6. * Authors: Gil Fine <[email protected]>
  7. * Mika Westerberg <[email protected]>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/uaccess.h>
  12. #include "tb.h"
  13. #include "sb_regs.h"
  14. #define PORT_CAP_PCIE_LEN 1
  15. #define PORT_CAP_POWER_LEN 2
  16. #define PORT_CAP_LANE_LEN 3
  17. #define PORT_CAP_USB3_LEN 5
  18. #define PORT_CAP_DP_LEN 8
  19. #define PORT_CAP_TMU_LEN 8
  20. #define PORT_CAP_BASIC_LEN 9
  21. #define PORT_CAP_USB4_LEN 20
  22. #define SWITCH_CAP_TMU_LEN 26
  23. #define SWITCH_CAP_BASIC_LEN 27
  24. #define PATH_LEN 2
  25. #define COUNTER_SET_LEN 3
  26. #define DEBUGFS_ATTR(__space, __write) \
  27. static int __space ## _open(struct inode *inode, struct file *file) \
  28. { \
  29. return single_open(file, __space ## _show, inode->i_private); \
  30. } \
  31. \
  32. static const struct file_operations __space ## _fops = { \
  33. .owner = THIS_MODULE, \
  34. .open = __space ## _open, \
  35. .release = single_release, \
  36. .read = seq_read, \
  37. .write = __write, \
  38. .llseek = seq_lseek, \
  39. }
  40. #define DEBUGFS_ATTR_RO(__space) \
  41. DEBUGFS_ATTR(__space, NULL)
  42. #define DEBUGFS_ATTR_RW(__space) \
  43. DEBUGFS_ATTR(__space, __space ## _write)
  44. static struct dentry *tb_debugfs_root;
  45. static void *validate_and_copy_from_user(const void __user *user_buf,
  46. size_t *count)
  47. {
  48. size_t nbytes;
  49. void *buf;
  50. if (!*count)
  51. return ERR_PTR(-EINVAL);
  52. if (!access_ok(user_buf, *count))
  53. return ERR_PTR(-EFAULT);
  54. buf = (void *)get_zeroed_page(GFP_KERNEL);
  55. if (!buf)
  56. return ERR_PTR(-ENOMEM);
  57. nbytes = min_t(size_t, *count, PAGE_SIZE);
  58. if (copy_from_user(buf, user_buf, nbytes)) {
  59. free_page((unsigned long)buf);
  60. return ERR_PTR(-EFAULT);
  61. }
  62. *count = nbytes;
  63. return buf;
  64. }
  65. static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
  66. int long_fmt_len)
  67. {
  68. char *token;
  69. u32 v[5];
  70. int ret;
  71. token = strsep(line, "\n");
  72. if (!token)
  73. return false;
  74. /*
  75. * For Adapter/Router configuration space:
  76. * Short format is: offset value\n
  77. * v[0] v[1]
  78. * Long format as produced from the read side:
  79. * offset relative_offset cap_id vs_cap_id value\n
  80. * v[0] v[1] v[2] v[3] v[4]
  81. *
  82. * For Counter configuration space:
  83. * Short format is: offset\n
  84. * v[0]
  85. * Long format as produced from the read side:
  86. * offset relative_offset counter_id value\n
  87. * v[0] v[1] v[2] v[3]
  88. */
  89. ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
  90. /* In case of Counters, clear counter, "val" content is NA */
  91. if (ret == short_fmt_len) {
  92. *offs = v[0];
  93. *val = v[short_fmt_len - 1];
  94. return true;
  95. } else if (ret == long_fmt_len) {
  96. *offs = v[0];
  97. *val = v[long_fmt_len - 1];
  98. return true;
  99. }
  100. return false;
  101. }
  102. #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
  103. static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
  104. const char __user *user_buf, size_t count,
  105. loff_t *ppos)
  106. {
  107. struct tb *tb = sw->tb;
  108. char *line, *buf;
  109. u32 val, offset;
  110. int ret = 0;
  111. buf = validate_and_copy_from_user(user_buf, &count);
  112. if (IS_ERR(buf))
  113. return PTR_ERR(buf);
  114. pm_runtime_get_sync(&sw->dev);
  115. if (mutex_lock_interruptible(&tb->lock)) {
  116. ret = -ERESTARTSYS;
  117. goto out;
  118. }
  119. /* User did hardware changes behind the driver's back */
  120. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  121. line = buf;
  122. while (parse_line(&line, &offset, &val, 2, 5)) {
  123. if (port)
  124. ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
  125. else
  126. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
  127. if (ret)
  128. break;
  129. }
  130. mutex_unlock(&tb->lock);
  131. out:
  132. pm_runtime_mark_last_busy(&sw->dev);
  133. pm_runtime_put_autosuspend(&sw->dev);
  134. free_page((unsigned long)buf);
  135. return ret < 0 ? ret : count;
  136. }
  137. static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
  138. size_t count, loff_t *ppos)
  139. {
  140. struct seq_file *s = file->private_data;
  141. struct tb_port *port = s->private;
  142. return regs_write(port->sw, port, user_buf, count, ppos);
  143. }
  144. static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
  145. size_t count, loff_t *ppos)
  146. {
  147. struct seq_file *s = file->private_data;
  148. struct tb_switch *sw = s->private;
  149. return regs_write(sw, NULL, user_buf, count, ppos);
  150. }
  151. #define DEBUGFS_MODE 0600
  152. #else
  153. #define port_regs_write NULL
  154. #define switch_regs_write NULL
  155. #define DEBUGFS_MODE 0400
  156. #endif
  157. #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
  158. /**
  159. * struct tb_margining - Lane margining support
  160. * @caps: Port lane margining capabilities
  161. * @results: Last lane margining results
  162. * @lanes: %0, %1 or %7 (all)
  163. * @min_ber_level: Minimum supported BER level contour value
  164. * @max_ber_level: Maximum supported BER level contour value
  165. * @ber_level: Current BER level contour value
  166. * @voltage_steps: Number of mandatory voltage steps
  167. * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
  168. * @time_steps: Number of time margin steps
  169. * @max_time_offset: Maximum time margin offset (in mUI)
  170. * @software: %true if software margining is used instead of hardware
  171. * @time: %true if time margining is used instead of voltage
  172. * @right_high: %false if left/low margin test is performed, %true if
  173. * right/high
  174. */
  175. struct tb_margining {
  176. u32 caps[2];
  177. u32 results[2];
  178. unsigned int lanes;
  179. unsigned int min_ber_level;
  180. unsigned int max_ber_level;
  181. unsigned int ber_level;
  182. unsigned int voltage_steps;
  183. unsigned int max_voltage_offset;
  184. unsigned int time_steps;
  185. unsigned int max_time_offset;
  186. bool software;
  187. bool time;
  188. bool right_high;
  189. };
  190. static bool supports_software(const struct usb4_port *usb4)
  191. {
  192. return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
  193. }
  194. static bool supports_hardware(const struct usb4_port *usb4)
  195. {
  196. return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
  197. }
  198. static bool both_lanes(const struct usb4_port *usb4)
  199. {
  200. return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
  201. }
  202. static unsigned int independent_voltage_margins(const struct usb4_port *usb4)
  203. {
  204. return (usb4->margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK) >>
  205. USB4_MARGIN_CAP_0_VOLTAGE_INDP_SHIFT;
  206. }
  207. static bool supports_time(const struct usb4_port *usb4)
  208. {
  209. return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
  210. }
  211. /* Only applicable if supports_time() returns true */
  212. static unsigned int independent_time_margins(const struct usb4_port *usb4)
  213. {
  214. return (usb4->margining->caps[1] & USB4_MARGIN_CAP_1_TIME_INDP_MASK) >>
  215. USB4_MARGIN_CAP_1_TIME_INDP_SHIFT;
  216. }
  217. static ssize_t
  218. margining_ber_level_write(struct file *file, const char __user *user_buf,
  219. size_t count, loff_t *ppos)
  220. {
  221. struct seq_file *s = file->private_data;
  222. struct tb_port *port = s->private;
  223. struct usb4_port *usb4 = port->usb4;
  224. struct tb *tb = port->sw->tb;
  225. unsigned int val;
  226. int ret = 0;
  227. char *buf;
  228. if (mutex_lock_interruptible(&tb->lock))
  229. return -ERESTARTSYS;
  230. if (usb4->margining->software) {
  231. ret = -EINVAL;
  232. goto out_unlock;
  233. }
  234. buf = validate_and_copy_from_user(user_buf, &count);
  235. if (IS_ERR(buf)) {
  236. ret = PTR_ERR(buf);
  237. goto out_unlock;
  238. }
  239. buf[count - 1] = '\0';
  240. ret = kstrtouint(buf, 10, &val);
  241. if (ret)
  242. goto out_free;
  243. if (val < usb4->margining->min_ber_level ||
  244. val > usb4->margining->max_ber_level) {
  245. ret = -EINVAL;
  246. goto out_free;
  247. }
  248. usb4->margining->ber_level = val;
  249. out_free:
  250. free_page((unsigned long)buf);
  251. out_unlock:
  252. mutex_unlock(&tb->lock);
  253. return ret < 0 ? ret : count;
  254. }
  255. static void ber_level_show(struct seq_file *s, unsigned int val)
  256. {
  257. if (val % 2)
  258. seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
  259. else
  260. seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
  261. }
  262. static int margining_ber_level_show(struct seq_file *s, void *not_used)
  263. {
  264. struct tb_port *port = s->private;
  265. struct usb4_port *usb4 = port->usb4;
  266. if (usb4->margining->software)
  267. return -EINVAL;
  268. ber_level_show(s, usb4->margining->ber_level);
  269. return 0;
  270. }
  271. DEBUGFS_ATTR_RW(margining_ber_level);
  272. static int margining_caps_show(struct seq_file *s, void *not_used)
  273. {
  274. struct tb_port *port = s->private;
  275. struct usb4_port *usb4 = port->usb4;
  276. struct tb *tb = port->sw->tb;
  277. u32 cap0, cap1;
  278. if (mutex_lock_interruptible(&tb->lock))
  279. return -ERESTARTSYS;
  280. /* Dump the raw caps first */
  281. cap0 = usb4->margining->caps[0];
  282. seq_printf(s, "0x%08x\n", cap0);
  283. cap1 = usb4->margining->caps[1];
  284. seq_printf(s, "0x%08x\n", cap1);
  285. seq_printf(s, "# software margining: %s\n",
  286. supports_software(usb4) ? "yes" : "no");
  287. if (supports_hardware(usb4)) {
  288. seq_puts(s, "# hardware margining: yes\n");
  289. seq_puts(s, "# minimum BER level contour: ");
  290. ber_level_show(s, usb4->margining->min_ber_level);
  291. seq_puts(s, "# maximum BER level contour: ");
  292. ber_level_show(s, usb4->margining->max_ber_level);
  293. } else {
  294. seq_puts(s, "# hardware margining: no\n");
  295. }
  296. seq_printf(s, "# both lanes simultaneously: %s\n",
  297. both_lanes(usb4) ? "yes" : "no");
  298. seq_printf(s, "# voltage margin steps: %u\n",
  299. usb4->margining->voltage_steps);
  300. seq_printf(s, "# maximum voltage offset: %u mV\n",
  301. usb4->margining->max_voltage_offset);
  302. switch (independent_voltage_margins(usb4)) {
  303. case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
  304. seq_puts(s, "# returns minimum between high and low voltage margins\n");
  305. break;
  306. case USB4_MARGIN_CAP_0_VOLTAGE_HL:
  307. seq_puts(s, "# returns high or low voltage margin\n");
  308. break;
  309. case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
  310. seq_puts(s, "# returns both high and low margins\n");
  311. break;
  312. }
  313. if (supports_time(usb4)) {
  314. seq_puts(s, "# time margining: yes\n");
  315. seq_printf(s, "# time margining is destructive: %s\n",
  316. cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
  317. switch (independent_time_margins(usb4)) {
  318. case USB4_MARGIN_CAP_1_TIME_MIN:
  319. seq_puts(s, "# returns minimum between left and right time margins\n");
  320. break;
  321. case USB4_MARGIN_CAP_1_TIME_LR:
  322. seq_puts(s, "# returns left or right margin\n");
  323. break;
  324. case USB4_MARGIN_CAP_1_TIME_BOTH:
  325. seq_puts(s, "# returns both left and right margins\n");
  326. break;
  327. }
  328. seq_printf(s, "# time margin steps: %u\n",
  329. usb4->margining->time_steps);
  330. seq_printf(s, "# maximum time offset: %u mUI\n",
  331. usb4->margining->max_time_offset);
  332. } else {
  333. seq_puts(s, "# time margining: no\n");
  334. }
  335. mutex_unlock(&tb->lock);
  336. return 0;
  337. }
  338. DEBUGFS_ATTR_RO(margining_caps);
  339. static ssize_t
  340. margining_lanes_write(struct file *file, const char __user *user_buf,
  341. size_t count, loff_t *ppos)
  342. {
  343. struct seq_file *s = file->private_data;
  344. struct tb_port *port = s->private;
  345. struct usb4_port *usb4 = port->usb4;
  346. struct tb *tb = port->sw->tb;
  347. int ret = 0;
  348. char *buf;
  349. buf = validate_and_copy_from_user(user_buf, &count);
  350. if (IS_ERR(buf))
  351. return PTR_ERR(buf);
  352. buf[count - 1] = '\0';
  353. if (mutex_lock_interruptible(&tb->lock)) {
  354. ret = -ERESTARTSYS;
  355. goto out_free;
  356. }
  357. if (!strcmp(buf, "0")) {
  358. usb4->margining->lanes = 0;
  359. } else if (!strcmp(buf, "1")) {
  360. usb4->margining->lanes = 1;
  361. } else if (!strcmp(buf, "all")) {
  362. /* Needs to be supported */
  363. if (both_lanes(usb4))
  364. usb4->margining->lanes = 7;
  365. else
  366. ret = -EINVAL;
  367. } else {
  368. ret = -EINVAL;
  369. }
  370. mutex_unlock(&tb->lock);
  371. out_free:
  372. free_page((unsigned long)buf);
  373. return ret < 0 ? ret : count;
  374. }
  375. static int margining_lanes_show(struct seq_file *s, void *not_used)
  376. {
  377. struct tb_port *port = s->private;
  378. struct usb4_port *usb4 = port->usb4;
  379. struct tb *tb = port->sw->tb;
  380. unsigned int lanes;
  381. if (mutex_lock_interruptible(&tb->lock))
  382. return -ERESTARTSYS;
  383. lanes = usb4->margining->lanes;
  384. if (both_lanes(usb4)) {
  385. if (!lanes)
  386. seq_puts(s, "[0] 1 all\n");
  387. else if (lanes == 1)
  388. seq_puts(s, "0 [1] all\n");
  389. else
  390. seq_puts(s, "0 1 [all]\n");
  391. } else {
  392. if (!lanes)
  393. seq_puts(s, "[0] 1\n");
  394. else
  395. seq_puts(s, "0 [1]\n");
  396. }
  397. mutex_unlock(&tb->lock);
  398. return 0;
  399. }
  400. DEBUGFS_ATTR_RW(margining_lanes);
  401. static ssize_t margining_mode_write(struct file *file,
  402. const char __user *user_buf,
  403. size_t count, loff_t *ppos)
  404. {
  405. struct seq_file *s = file->private_data;
  406. struct tb_port *port = s->private;
  407. struct usb4_port *usb4 = port->usb4;
  408. struct tb *tb = port->sw->tb;
  409. int ret = 0;
  410. char *buf;
  411. buf = validate_and_copy_from_user(user_buf, &count);
  412. if (IS_ERR(buf))
  413. return PTR_ERR(buf);
  414. buf[count - 1] = '\0';
  415. if (mutex_lock_interruptible(&tb->lock)) {
  416. ret = -ERESTARTSYS;
  417. goto out_free;
  418. }
  419. if (!strcmp(buf, "software")) {
  420. if (supports_software(usb4))
  421. usb4->margining->software = true;
  422. else
  423. ret = -EINVAL;
  424. } else if (!strcmp(buf, "hardware")) {
  425. if (supports_hardware(usb4))
  426. usb4->margining->software = false;
  427. else
  428. ret = -EINVAL;
  429. } else {
  430. ret = -EINVAL;
  431. }
  432. mutex_unlock(&tb->lock);
  433. out_free:
  434. free_page((unsigned long)buf);
  435. return ret ? ret : count;
  436. }
  437. static int margining_mode_show(struct seq_file *s, void *not_used)
  438. {
  439. const struct tb_port *port = s->private;
  440. const struct usb4_port *usb4 = port->usb4;
  441. struct tb *tb = port->sw->tb;
  442. const char *space = "";
  443. if (mutex_lock_interruptible(&tb->lock))
  444. return -ERESTARTSYS;
  445. if (supports_software(usb4)) {
  446. if (usb4->margining->software)
  447. seq_puts(s, "[software]");
  448. else
  449. seq_puts(s, "software");
  450. space = " ";
  451. }
  452. if (supports_hardware(usb4)) {
  453. if (usb4->margining->software)
  454. seq_printf(s, "%shardware", space);
  455. else
  456. seq_printf(s, "%s[hardware]", space);
  457. }
  458. mutex_unlock(&tb->lock);
  459. seq_puts(s, "\n");
  460. return 0;
  461. }
  462. DEBUGFS_ATTR_RW(margining_mode);
  463. static int margining_run_write(void *data, u64 val)
  464. {
  465. struct tb_port *port = data;
  466. struct usb4_port *usb4 = port->usb4;
  467. struct tb_switch *sw = port->sw;
  468. struct tb_margining *margining;
  469. struct tb *tb = sw->tb;
  470. int ret;
  471. if (val != 1)
  472. return -EINVAL;
  473. pm_runtime_get_sync(&sw->dev);
  474. if (mutex_lock_interruptible(&tb->lock)) {
  475. ret = -ERESTARTSYS;
  476. goto out_rpm_put;
  477. }
  478. /*
  479. * CL states may interfere with lane margining so inform the user know
  480. * and bail out.
  481. */
  482. if (tb_port_is_clx_enabled(port, TB_CL1 | TB_CL2)) {
  483. tb_port_warn(port,
  484. "CL states are enabled, Disable them with clx=0 and re-connect\n");
  485. ret = -EINVAL;
  486. goto out_unlock;
  487. }
  488. margining = usb4->margining;
  489. if (margining->software) {
  490. tb_port_dbg(port, "running software %s lane margining for lanes %u\n",
  491. margining->time ? "time" : "voltage", margining->lanes);
  492. ret = usb4_port_sw_margin(port, margining->lanes, margining->time,
  493. margining->right_high,
  494. USB4_MARGIN_SW_COUNTER_CLEAR);
  495. if (ret)
  496. goto out_unlock;
  497. ret = usb4_port_sw_margin_errors(port, &margining->results[0]);
  498. } else {
  499. tb_port_dbg(port, "running hardware %s lane margining for lanes %u\n",
  500. margining->time ? "time" : "voltage", margining->lanes);
  501. /* Clear the results */
  502. margining->results[0] = 0;
  503. margining->results[1] = 0;
  504. ret = usb4_port_hw_margin(port, margining->lanes,
  505. margining->ber_level, margining->time,
  506. margining->right_high, margining->results);
  507. }
  508. out_unlock:
  509. mutex_unlock(&tb->lock);
  510. out_rpm_put:
  511. pm_runtime_mark_last_busy(&sw->dev);
  512. pm_runtime_put_autosuspend(&sw->dev);
  513. return ret;
  514. }
  515. DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
  516. "%llu\n");
  517. static ssize_t margining_results_write(struct file *file,
  518. const char __user *user_buf,
  519. size_t count, loff_t *ppos)
  520. {
  521. struct seq_file *s = file->private_data;
  522. struct tb_port *port = s->private;
  523. struct usb4_port *usb4 = port->usb4;
  524. struct tb *tb = port->sw->tb;
  525. if (mutex_lock_interruptible(&tb->lock))
  526. return -ERESTARTSYS;
  527. /* Just clear the results */
  528. usb4->margining->results[0] = 0;
  529. usb4->margining->results[1] = 0;
  530. mutex_unlock(&tb->lock);
  531. return count;
  532. }
  533. static void voltage_margin_show(struct seq_file *s,
  534. const struct tb_margining *margining, u8 val)
  535. {
  536. unsigned int tmp, voltage;
  537. tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
  538. voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
  539. seq_printf(s, "%u mV (%u)", voltage, tmp);
  540. if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
  541. seq_puts(s, " exceeds maximum");
  542. seq_puts(s, "\n");
  543. }
  544. static void time_margin_show(struct seq_file *s,
  545. const struct tb_margining *margining, u8 val)
  546. {
  547. unsigned int tmp, interval;
  548. tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
  549. interval = tmp * margining->max_time_offset / margining->time_steps;
  550. seq_printf(s, "%u mUI (%u)", interval, tmp);
  551. if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
  552. seq_puts(s, " exceeds maximum");
  553. seq_puts(s, "\n");
  554. }
  555. static int margining_results_show(struct seq_file *s, void *not_used)
  556. {
  557. struct tb_port *port = s->private;
  558. struct usb4_port *usb4 = port->usb4;
  559. struct tb_margining *margining;
  560. struct tb *tb = port->sw->tb;
  561. if (mutex_lock_interruptible(&tb->lock))
  562. return -ERESTARTSYS;
  563. margining = usb4->margining;
  564. /* Dump the raw results first */
  565. seq_printf(s, "0x%08x\n", margining->results[0]);
  566. /* Only the hardware margining has two result dwords */
  567. if (!margining->software) {
  568. unsigned int val;
  569. seq_printf(s, "0x%08x\n", margining->results[1]);
  570. if (margining->time) {
  571. if (!margining->lanes || margining->lanes == 7) {
  572. val = margining->results[1];
  573. seq_puts(s, "# lane 0 right time margin: ");
  574. time_margin_show(s, margining, val);
  575. val = margining->results[1] >>
  576. USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
  577. seq_puts(s, "# lane 0 left time margin: ");
  578. time_margin_show(s, margining, val);
  579. }
  580. if (margining->lanes == 1 || margining->lanes == 7) {
  581. val = margining->results[1] >>
  582. USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
  583. seq_puts(s, "# lane 1 right time margin: ");
  584. time_margin_show(s, margining, val);
  585. val = margining->results[1] >>
  586. USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
  587. seq_puts(s, "# lane 1 left time margin: ");
  588. time_margin_show(s, margining, val);
  589. }
  590. } else {
  591. if (!margining->lanes || margining->lanes == 7) {
  592. val = margining->results[1];
  593. seq_puts(s, "# lane 0 high voltage margin: ");
  594. voltage_margin_show(s, margining, val);
  595. val = margining->results[1] >>
  596. USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
  597. seq_puts(s, "# lane 0 low voltage margin: ");
  598. voltage_margin_show(s, margining, val);
  599. }
  600. if (margining->lanes == 1 || margining->lanes == 7) {
  601. val = margining->results[1] >>
  602. USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
  603. seq_puts(s, "# lane 1 high voltage margin: ");
  604. voltage_margin_show(s, margining, val);
  605. val = margining->results[1] >>
  606. USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
  607. seq_puts(s, "# lane 1 low voltage margin: ");
  608. voltage_margin_show(s, margining, val);
  609. }
  610. }
  611. }
  612. mutex_unlock(&tb->lock);
  613. return 0;
  614. }
  615. DEBUGFS_ATTR_RW(margining_results);
  616. static ssize_t margining_test_write(struct file *file,
  617. const char __user *user_buf,
  618. size_t count, loff_t *ppos)
  619. {
  620. struct seq_file *s = file->private_data;
  621. struct tb_port *port = s->private;
  622. struct usb4_port *usb4 = port->usb4;
  623. struct tb *tb = port->sw->tb;
  624. int ret = 0;
  625. char *buf;
  626. buf = validate_and_copy_from_user(user_buf, &count);
  627. if (IS_ERR(buf))
  628. return PTR_ERR(buf);
  629. buf[count - 1] = '\0';
  630. if (mutex_lock_interruptible(&tb->lock)) {
  631. ret = -ERESTARTSYS;
  632. goto out_free;
  633. }
  634. if (!strcmp(buf, "time") && supports_time(usb4))
  635. usb4->margining->time = true;
  636. else if (!strcmp(buf, "voltage"))
  637. usb4->margining->time = false;
  638. else
  639. ret = -EINVAL;
  640. mutex_unlock(&tb->lock);
  641. out_free:
  642. free_page((unsigned long)buf);
  643. return ret ? ret : count;
  644. }
  645. static int margining_test_show(struct seq_file *s, void *not_used)
  646. {
  647. struct tb_port *port = s->private;
  648. struct usb4_port *usb4 = port->usb4;
  649. struct tb *tb = port->sw->tb;
  650. if (mutex_lock_interruptible(&tb->lock))
  651. return -ERESTARTSYS;
  652. if (supports_time(usb4)) {
  653. if (usb4->margining->time)
  654. seq_puts(s, "voltage [time]\n");
  655. else
  656. seq_puts(s, "[voltage] time\n");
  657. } else {
  658. seq_puts(s, "[voltage]\n");
  659. }
  660. mutex_unlock(&tb->lock);
  661. return 0;
  662. }
  663. DEBUGFS_ATTR_RW(margining_test);
  664. static ssize_t margining_margin_write(struct file *file,
  665. const char __user *user_buf,
  666. size_t count, loff_t *ppos)
  667. {
  668. struct seq_file *s = file->private_data;
  669. struct tb_port *port = s->private;
  670. struct usb4_port *usb4 = port->usb4;
  671. struct tb *tb = port->sw->tb;
  672. int ret = 0;
  673. char *buf;
  674. buf = validate_and_copy_from_user(user_buf, &count);
  675. if (IS_ERR(buf))
  676. return PTR_ERR(buf);
  677. buf[count - 1] = '\0';
  678. if (mutex_lock_interruptible(&tb->lock)) {
  679. ret = -ERESTARTSYS;
  680. goto out_free;
  681. }
  682. if (usb4->margining->time) {
  683. if (!strcmp(buf, "left"))
  684. usb4->margining->right_high = false;
  685. else if (!strcmp(buf, "right"))
  686. usb4->margining->right_high = true;
  687. else
  688. ret = -EINVAL;
  689. } else {
  690. if (!strcmp(buf, "low"))
  691. usb4->margining->right_high = false;
  692. else if (!strcmp(buf, "high"))
  693. usb4->margining->right_high = true;
  694. else
  695. ret = -EINVAL;
  696. }
  697. mutex_unlock(&tb->lock);
  698. out_free:
  699. free_page((unsigned long)buf);
  700. return ret ? ret : count;
  701. }
  702. static int margining_margin_show(struct seq_file *s, void *not_used)
  703. {
  704. struct tb_port *port = s->private;
  705. struct usb4_port *usb4 = port->usb4;
  706. struct tb *tb = port->sw->tb;
  707. if (mutex_lock_interruptible(&tb->lock))
  708. return -ERESTARTSYS;
  709. if (usb4->margining->time) {
  710. if (usb4->margining->right_high)
  711. seq_puts(s, "left [right]\n");
  712. else
  713. seq_puts(s, "[left] right\n");
  714. } else {
  715. if (usb4->margining->right_high)
  716. seq_puts(s, "low [high]\n");
  717. else
  718. seq_puts(s, "[low] high\n");
  719. }
  720. mutex_unlock(&tb->lock);
  721. return 0;
  722. }
  723. DEBUGFS_ATTR_RW(margining_margin);
  724. static void margining_port_init(struct tb_port *port)
  725. {
  726. struct tb_margining *margining;
  727. struct dentry *dir, *parent;
  728. struct usb4_port *usb4;
  729. char dir_name[10];
  730. unsigned int val;
  731. int ret;
  732. usb4 = port->usb4;
  733. if (!usb4)
  734. return;
  735. snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
  736. parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
  737. margining = kzalloc(sizeof(*margining), GFP_KERNEL);
  738. if (!margining)
  739. return;
  740. ret = usb4_port_margining_caps(port, margining->caps);
  741. if (ret) {
  742. kfree(margining);
  743. return;
  744. }
  745. usb4->margining = margining;
  746. /* Set the initial mode */
  747. if (supports_software(usb4))
  748. margining->software = true;
  749. val = (margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK) >>
  750. USB4_MARGIN_CAP_0_VOLTAGE_STEPS_SHIFT;
  751. margining->voltage_steps = val;
  752. val = (margining->caps[0] & USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK) >>
  753. USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_SHIFT;
  754. margining->max_voltage_offset = 74 + val * 2;
  755. if (supports_time(usb4)) {
  756. val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_STEPS_MASK) >>
  757. USB4_MARGIN_CAP_1_TIME_STEPS_SHIFT;
  758. margining->time_steps = val;
  759. val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_OFFSET_MASK) >>
  760. USB4_MARGIN_CAP_1_TIME_OFFSET_SHIFT;
  761. /*
  762. * Store it as mUI (milli Unit Interval) because we want
  763. * to keep it as integer.
  764. */
  765. margining->max_time_offset = 200 + 10 * val;
  766. }
  767. dir = debugfs_create_dir("margining", parent);
  768. if (supports_hardware(usb4)) {
  769. val = (margining->caps[1] & USB4_MARGIN_CAP_1_MIN_BER_MASK) >>
  770. USB4_MARGIN_CAP_1_MIN_BER_SHIFT;
  771. margining->min_ber_level = val;
  772. val = (margining->caps[1] & USB4_MARGIN_CAP_1_MAX_BER_MASK) >>
  773. USB4_MARGIN_CAP_1_MAX_BER_SHIFT;
  774. margining->max_ber_level = val;
  775. /* Set the default to minimum */
  776. margining->ber_level = margining->min_ber_level;
  777. debugfs_create_file("ber_level_contour", 0400, dir, port,
  778. &margining_ber_level_fops);
  779. }
  780. debugfs_create_file("caps", 0400, dir, port, &margining_caps_fops);
  781. debugfs_create_file("lanes", 0600, dir, port, &margining_lanes_fops);
  782. debugfs_create_file("mode", 0600, dir, port, &margining_mode_fops);
  783. debugfs_create_file("run", 0600, dir, port, &margining_run_fops);
  784. debugfs_create_file("results", 0600, dir, port, &margining_results_fops);
  785. debugfs_create_file("test", 0600, dir, port, &margining_test_fops);
  786. if (independent_voltage_margins(usb4) ||
  787. (supports_time(usb4) && independent_time_margins(usb4)))
  788. debugfs_create_file("margin", 0600, dir, port, &margining_margin_fops);
  789. }
  790. static void margining_port_remove(struct tb_port *port)
  791. {
  792. struct dentry *parent;
  793. char dir_name[10];
  794. if (!port->usb4)
  795. return;
  796. snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
  797. parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
  798. if (parent)
  799. debugfs_remove_recursive(debugfs_lookup("margining", parent));
  800. kfree(port->usb4->margining);
  801. port->usb4->margining = NULL;
  802. }
  803. static void margining_switch_init(struct tb_switch *sw)
  804. {
  805. struct tb_port *upstream, *downstream;
  806. struct tb_switch *parent_sw;
  807. u64 route = tb_route(sw);
  808. if (!route)
  809. return;
  810. upstream = tb_upstream_port(sw);
  811. parent_sw = tb_switch_parent(sw);
  812. downstream = tb_port_at(route, parent_sw);
  813. margining_port_init(downstream);
  814. margining_port_init(upstream);
  815. }
  816. static void margining_switch_remove(struct tb_switch *sw)
  817. {
  818. struct tb_port *upstream, *downstream;
  819. struct tb_switch *parent_sw;
  820. u64 route = tb_route(sw);
  821. if (!route)
  822. return;
  823. upstream = tb_upstream_port(sw);
  824. parent_sw = tb_switch_parent(sw);
  825. downstream = tb_port_at(route, parent_sw);
  826. margining_port_remove(upstream);
  827. margining_port_remove(downstream);
  828. }
  829. static void margining_xdomain_init(struct tb_xdomain *xd)
  830. {
  831. struct tb_switch *parent_sw;
  832. struct tb_port *downstream;
  833. parent_sw = tb_xdomain_parent(xd);
  834. downstream = tb_port_at(xd->route, parent_sw);
  835. margining_port_init(downstream);
  836. }
  837. static void margining_xdomain_remove(struct tb_xdomain *xd)
  838. {
  839. struct tb_switch *parent_sw;
  840. struct tb_port *downstream;
  841. parent_sw = tb_xdomain_parent(xd);
  842. downstream = tb_port_at(xd->route, parent_sw);
  843. margining_port_remove(downstream);
  844. }
  845. #else
  846. static inline void margining_switch_init(struct tb_switch *sw) { }
  847. static inline void margining_switch_remove(struct tb_switch *sw) { }
  848. static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
  849. static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
  850. #endif
  851. static int port_clear_all_counters(struct tb_port *port)
  852. {
  853. u32 *buf;
  854. int ret;
  855. buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
  856. GFP_KERNEL);
  857. if (!buf)
  858. return -ENOMEM;
  859. ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
  860. COUNTER_SET_LEN * port->config.max_counters);
  861. kfree(buf);
  862. return ret;
  863. }
  864. static ssize_t counters_write(struct file *file, const char __user *user_buf,
  865. size_t count, loff_t *ppos)
  866. {
  867. struct seq_file *s = file->private_data;
  868. struct tb_port *port = s->private;
  869. struct tb_switch *sw = port->sw;
  870. struct tb *tb = port->sw->tb;
  871. char *buf;
  872. int ret;
  873. buf = validate_and_copy_from_user(user_buf, &count);
  874. if (IS_ERR(buf))
  875. return PTR_ERR(buf);
  876. pm_runtime_get_sync(&sw->dev);
  877. if (mutex_lock_interruptible(&tb->lock)) {
  878. ret = -ERESTARTSYS;
  879. goto out;
  880. }
  881. /* If written delimiter only, clear all counters in one shot */
  882. if (buf[0] == '\n') {
  883. ret = port_clear_all_counters(port);
  884. } else {
  885. char *line = buf;
  886. u32 val, offset;
  887. ret = -EINVAL;
  888. while (parse_line(&line, &offset, &val, 1, 4)) {
  889. ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
  890. offset, 1);
  891. if (ret)
  892. break;
  893. }
  894. }
  895. mutex_unlock(&tb->lock);
  896. out:
  897. pm_runtime_mark_last_busy(&sw->dev);
  898. pm_runtime_put_autosuspend(&sw->dev);
  899. free_page((unsigned long)buf);
  900. return ret < 0 ? ret : count;
  901. }
  902. static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
  903. struct tb_port *port, unsigned int cap,
  904. unsigned int offset, u8 cap_id, u8 vsec_id,
  905. int dwords)
  906. {
  907. int i, ret;
  908. u32 data;
  909. for (i = 0; i < dwords; i++) {
  910. if (port)
  911. ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
  912. else
  913. ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
  914. if (ret) {
  915. seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
  916. continue;
  917. }
  918. seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
  919. offset + i, cap_id, vsec_id, data);
  920. }
  921. }
  922. static void cap_show(struct seq_file *s, struct tb_switch *sw,
  923. struct tb_port *port, unsigned int cap, u8 cap_id,
  924. u8 vsec_id, int length)
  925. {
  926. int ret, offset = 0;
  927. while (length > 0) {
  928. int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
  929. u32 data[TB_MAX_CONFIG_RW_LENGTH];
  930. if (port)
  931. ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
  932. dwords);
  933. else
  934. ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
  935. if (ret) {
  936. cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
  937. return;
  938. }
  939. for (i = 0; i < dwords; i++) {
  940. seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
  941. cap + offset + i, offset + i,
  942. cap_id, vsec_id, data[i]);
  943. }
  944. length -= dwords;
  945. offset += dwords;
  946. }
  947. }
  948. static void port_cap_show(struct tb_port *port, struct seq_file *s,
  949. unsigned int cap)
  950. {
  951. struct tb_cap_any header;
  952. u8 vsec_id = 0;
  953. size_t length;
  954. int ret;
  955. ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
  956. if (ret) {
  957. seq_printf(s, "0x%04x <capability read failed>\n", cap);
  958. return;
  959. }
  960. switch (header.basic.cap) {
  961. case TB_PORT_CAP_PHY:
  962. length = PORT_CAP_LANE_LEN;
  963. break;
  964. case TB_PORT_CAP_TIME1:
  965. length = PORT_CAP_TMU_LEN;
  966. break;
  967. case TB_PORT_CAP_POWER:
  968. length = PORT_CAP_POWER_LEN;
  969. break;
  970. case TB_PORT_CAP_ADAP:
  971. if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
  972. length = PORT_CAP_PCIE_LEN;
  973. } else if (tb_port_is_dpin(port) || tb_port_is_dpout(port)) {
  974. length = PORT_CAP_DP_LEN;
  975. } else if (tb_port_is_usb3_down(port) ||
  976. tb_port_is_usb3_up(port)) {
  977. length = PORT_CAP_USB3_LEN;
  978. } else {
  979. seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
  980. cap, header.basic.cap);
  981. return;
  982. }
  983. break;
  984. case TB_PORT_CAP_VSE:
  985. if (!header.extended_short.length) {
  986. ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
  987. cap + 1, 1);
  988. if (ret) {
  989. seq_printf(s, "0x%04x <capability read failed>\n",
  990. cap + 1);
  991. return;
  992. }
  993. length = header.extended_long.length;
  994. vsec_id = header.extended_short.vsec_id;
  995. } else {
  996. length = header.extended_short.length;
  997. vsec_id = header.extended_short.vsec_id;
  998. }
  999. break;
  1000. case TB_PORT_CAP_USB4:
  1001. length = PORT_CAP_USB4_LEN;
  1002. break;
  1003. default:
  1004. seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
  1005. cap, header.basic.cap);
  1006. return;
  1007. }
  1008. cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
  1009. }
  1010. static void port_caps_show(struct tb_port *port, struct seq_file *s)
  1011. {
  1012. int cap;
  1013. cap = tb_port_next_cap(port, 0);
  1014. while (cap > 0) {
  1015. port_cap_show(port, s, cap);
  1016. cap = tb_port_next_cap(port, cap);
  1017. }
  1018. }
  1019. static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
  1020. {
  1021. u32 data[PORT_CAP_BASIC_LEN];
  1022. int ret, i;
  1023. ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
  1024. if (ret)
  1025. return ret;
  1026. for (i = 0; i < ARRAY_SIZE(data); i++)
  1027. seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
  1028. return 0;
  1029. }
  1030. static int port_regs_show(struct seq_file *s, void *not_used)
  1031. {
  1032. struct tb_port *port = s->private;
  1033. struct tb_switch *sw = port->sw;
  1034. struct tb *tb = sw->tb;
  1035. int ret;
  1036. pm_runtime_get_sync(&sw->dev);
  1037. if (mutex_lock_interruptible(&tb->lock)) {
  1038. ret = -ERESTARTSYS;
  1039. goto out_rpm_put;
  1040. }
  1041. seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
  1042. ret = port_basic_regs_show(port, s);
  1043. if (ret)
  1044. goto out_unlock;
  1045. port_caps_show(port, s);
  1046. out_unlock:
  1047. mutex_unlock(&tb->lock);
  1048. out_rpm_put:
  1049. pm_runtime_mark_last_busy(&sw->dev);
  1050. pm_runtime_put_autosuspend(&sw->dev);
  1051. return ret;
  1052. }
  1053. DEBUGFS_ATTR_RW(port_regs);
  1054. static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
  1055. unsigned int cap)
  1056. {
  1057. struct tb_cap_any header;
  1058. int ret, length;
  1059. u8 vsec_id = 0;
  1060. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
  1061. if (ret) {
  1062. seq_printf(s, "0x%04x <capability read failed>\n", cap);
  1063. return;
  1064. }
  1065. if (header.basic.cap == TB_SWITCH_CAP_VSE) {
  1066. if (!header.extended_short.length) {
  1067. ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
  1068. cap + 1, 1);
  1069. if (ret) {
  1070. seq_printf(s, "0x%04x <capability read failed>\n",
  1071. cap + 1);
  1072. return;
  1073. }
  1074. length = header.extended_long.length;
  1075. } else {
  1076. length = header.extended_short.length;
  1077. }
  1078. vsec_id = header.extended_short.vsec_id;
  1079. } else {
  1080. if (header.basic.cap == TB_SWITCH_CAP_TMU) {
  1081. length = SWITCH_CAP_TMU_LEN;
  1082. } else {
  1083. seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
  1084. cap, header.basic.cap);
  1085. return;
  1086. }
  1087. }
  1088. cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
  1089. }
  1090. static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
  1091. {
  1092. int cap;
  1093. cap = tb_switch_next_cap(sw, 0);
  1094. while (cap > 0) {
  1095. switch_cap_show(sw, s, cap);
  1096. cap = tb_switch_next_cap(sw, cap);
  1097. }
  1098. }
  1099. static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
  1100. {
  1101. u32 data[SWITCH_CAP_BASIC_LEN];
  1102. size_t dwords;
  1103. int ret, i;
  1104. /* Only USB4 has the additional registers */
  1105. if (tb_switch_is_usb4(sw))
  1106. dwords = ARRAY_SIZE(data);
  1107. else
  1108. dwords = 7;
  1109. ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
  1110. if (ret)
  1111. return ret;
  1112. for (i = 0; i < dwords; i++)
  1113. seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
  1114. return 0;
  1115. }
  1116. static int switch_regs_show(struct seq_file *s, void *not_used)
  1117. {
  1118. struct tb_switch *sw = s->private;
  1119. struct tb *tb = sw->tb;
  1120. int ret;
  1121. pm_runtime_get_sync(&sw->dev);
  1122. if (mutex_lock_interruptible(&tb->lock)) {
  1123. ret = -ERESTARTSYS;
  1124. goto out_rpm_put;
  1125. }
  1126. seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
  1127. ret = switch_basic_regs_show(sw, s);
  1128. if (ret)
  1129. goto out_unlock;
  1130. switch_caps_show(sw, s);
  1131. out_unlock:
  1132. mutex_unlock(&tb->lock);
  1133. out_rpm_put:
  1134. pm_runtime_mark_last_busy(&sw->dev);
  1135. pm_runtime_put_autosuspend(&sw->dev);
  1136. return ret;
  1137. }
  1138. DEBUGFS_ATTR_RW(switch_regs);
  1139. static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
  1140. {
  1141. u32 data[PATH_LEN];
  1142. int ret, i;
  1143. ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
  1144. ARRAY_SIZE(data));
  1145. if (ret) {
  1146. seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
  1147. return ret;
  1148. }
  1149. for (i = 0; i < ARRAY_SIZE(data); i++) {
  1150. seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
  1151. hopid * PATH_LEN + i, i, hopid, data[i]);
  1152. }
  1153. return 0;
  1154. }
  1155. static int path_show(struct seq_file *s, void *not_used)
  1156. {
  1157. struct tb_port *port = s->private;
  1158. struct tb_switch *sw = port->sw;
  1159. struct tb *tb = sw->tb;
  1160. int start, i, ret = 0;
  1161. pm_runtime_get_sync(&sw->dev);
  1162. if (mutex_lock_interruptible(&tb->lock)) {
  1163. ret = -ERESTARTSYS;
  1164. goto out_rpm_put;
  1165. }
  1166. seq_puts(s, "# offset relative_offset in_hop_id value\n");
  1167. /* NHI and lane adapters have entry for path 0 */
  1168. if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
  1169. ret = path_show_one(port, s, 0);
  1170. if (ret)
  1171. goto out_unlock;
  1172. }
  1173. start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
  1174. for (i = start; i <= port->config.max_in_hop_id; i++) {
  1175. ret = path_show_one(port, s, i);
  1176. if (ret)
  1177. break;
  1178. }
  1179. out_unlock:
  1180. mutex_unlock(&tb->lock);
  1181. out_rpm_put:
  1182. pm_runtime_mark_last_busy(&sw->dev);
  1183. pm_runtime_put_autosuspend(&sw->dev);
  1184. return ret;
  1185. }
  1186. DEBUGFS_ATTR_RO(path);
  1187. static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
  1188. int counter)
  1189. {
  1190. u32 data[COUNTER_SET_LEN];
  1191. int ret, i;
  1192. ret = tb_port_read(port, data, TB_CFG_COUNTERS,
  1193. counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
  1194. if (ret) {
  1195. seq_printf(s, "0x%04x <not accessible>\n",
  1196. counter * COUNTER_SET_LEN);
  1197. return ret;
  1198. }
  1199. for (i = 0; i < ARRAY_SIZE(data); i++) {
  1200. seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
  1201. counter * COUNTER_SET_LEN + i, i, counter, data[i]);
  1202. }
  1203. return 0;
  1204. }
  1205. static int counters_show(struct seq_file *s, void *not_used)
  1206. {
  1207. struct tb_port *port = s->private;
  1208. struct tb_switch *sw = port->sw;
  1209. struct tb *tb = sw->tb;
  1210. int i, ret = 0;
  1211. pm_runtime_get_sync(&sw->dev);
  1212. if (mutex_lock_interruptible(&tb->lock)) {
  1213. ret = -ERESTARTSYS;
  1214. goto out;
  1215. }
  1216. seq_puts(s, "# offset relative_offset counter_id value\n");
  1217. for (i = 0; i < port->config.max_counters; i++) {
  1218. ret = counter_set_regs_show(port, s, i);
  1219. if (ret)
  1220. break;
  1221. }
  1222. mutex_unlock(&tb->lock);
  1223. out:
  1224. pm_runtime_mark_last_busy(&sw->dev);
  1225. pm_runtime_put_autosuspend(&sw->dev);
  1226. return ret;
  1227. }
  1228. DEBUGFS_ATTR_RW(counters);
  1229. /**
  1230. * tb_switch_debugfs_init() - Add debugfs entries for router
  1231. * @sw: Pointer to the router
  1232. *
  1233. * Adds debugfs directories and files for given router.
  1234. */
  1235. void tb_switch_debugfs_init(struct tb_switch *sw)
  1236. {
  1237. struct dentry *debugfs_dir;
  1238. struct tb_port *port;
  1239. debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
  1240. sw->debugfs_dir = debugfs_dir;
  1241. debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
  1242. &switch_regs_fops);
  1243. tb_switch_for_each_port(sw, port) {
  1244. struct dentry *debugfs_dir;
  1245. char dir_name[10];
  1246. if (port->disabled)
  1247. continue;
  1248. if (port->config.type == TB_TYPE_INACTIVE)
  1249. continue;
  1250. snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
  1251. debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
  1252. debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
  1253. port, &port_regs_fops);
  1254. debugfs_create_file("path", 0400, debugfs_dir, port,
  1255. &path_fops);
  1256. if (port->config.counters_support)
  1257. debugfs_create_file("counters", 0600, debugfs_dir, port,
  1258. &counters_fops);
  1259. }
  1260. margining_switch_init(sw);
  1261. }
  1262. /**
  1263. * tb_switch_debugfs_remove() - Remove all router debugfs entries
  1264. * @sw: Pointer to the router
  1265. *
  1266. * Removes all previously added debugfs entries under this router.
  1267. */
  1268. void tb_switch_debugfs_remove(struct tb_switch *sw)
  1269. {
  1270. margining_switch_remove(sw);
  1271. debugfs_remove_recursive(sw->debugfs_dir);
  1272. }
  1273. void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
  1274. {
  1275. margining_xdomain_init(xd);
  1276. }
  1277. void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
  1278. {
  1279. margining_xdomain_remove(xd);
  1280. }
  1281. /**
  1282. * tb_service_debugfs_init() - Add debugfs directory for service
  1283. * @svc: Thunderbolt service pointer
  1284. *
  1285. * Adds debugfs directory for service.
  1286. */
  1287. void tb_service_debugfs_init(struct tb_service *svc)
  1288. {
  1289. svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
  1290. tb_debugfs_root);
  1291. }
  1292. /**
  1293. * tb_service_debugfs_remove() - Remove service debugfs directory
  1294. * @svc: Thunderbolt service pointer
  1295. *
  1296. * Removes the previously created debugfs directory for @svc.
  1297. */
  1298. void tb_service_debugfs_remove(struct tb_service *svc)
  1299. {
  1300. debugfs_remove_recursive(svc->debugfs_dir);
  1301. svc->debugfs_dir = NULL;
  1302. }
  1303. void tb_debugfs_init(void)
  1304. {
  1305. tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
  1306. }
  1307. void tb_debugfs_exit(void)
  1308. {
  1309. debugfs_remove_recursive(tb_debugfs_root);
  1310. }