debugfs.c 64 KB

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