debugfs.c 56 KB

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