usb4.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB4 specific functionality
  4. *
  5. * Copyright (C) 2019, Intel Corporation
  6. * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. * Rajmohan Mani <rajmohan.mani@intel.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/ktime.h>
  11. #include <linux/string_choices.h>
  12. #include <linux/units.h>
  13. #include "sb_regs.h"
  14. #include "tb.h"
  15. #define USB4_DATA_RETRIES 3
  16. #define USB4_DATA_DWORDS 16
  17. #define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2)
  18. #define USB4_NVM_READ_OFFSET_SHIFT 2
  19. #define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24)
  20. #define USB4_NVM_READ_LENGTH_SHIFT 24
  21. #define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK
  22. #define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT
  23. #define USB4_DROM_ADDRESS_MASK GENMASK(14, 2)
  24. #define USB4_DROM_ADDRESS_SHIFT 2
  25. #define USB4_DROM_SIZE_MASK GENMASK(19, 15)
  26. #define USB4_DROM_SIZE_SHIFT 15
  27. #define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0)
  28. #define USB4_BA_LENGTH_MASK GENMASK(7, 0)
  29. #define USB4_BA_INDEX_MASK GENMASK(15, 0)
  30. enum usb4_ba_index {
  31. USB4_BA_MAX_USB3 = 0x1,
  32. USB4_BA_MIN_DP_AUX = 0x2,
  33. USB4_BA_MIN_DP_MAIN = 0x3,
  34. USB4_BA_MAX_PCIE = 0x4,
  35. USB4_BA_MAX_HI = 0x5,
  36. };
  37. #define USB4_BA_VALUE_MASK GENMASK(31, 16)
  38. #define USB4_BA_VALUE_SHIFT 16
  39. /* Delays in us used with usb4_port_wait_for_bit() */
  40. #define USB4_PORT_DELAY 50
  41. #define USB4_PORT_SB_DELAY 1000
  42. static int usb4_native_switch_op(struct tb_switch *sw, u16 opcode,
  43. u32 *metadata, u8 *status,
  44. const void *tx_data, size_t tx_dwords,
  45. void *rx_data, size_t rx_dwords)
  46. {
  47. u32 val;
  48. int ret;
  49. if (metadata) {
  50. ret = tb_sw_write(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
  51. if (ret)
  52. return ret;
  53. }
  54. if (tx_dwords) {
  55. ret = tb_sw_write(sw, tx_data, TB_CFG_SWITCH, ROUTER_CS_9,
  56. tx_dwords);
  57. if (ret)
  58. return ret;
  59. }
  60. val = opcode | ROUTER_CS_26_OV;
  61. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
  62. if (ret)
  63. return ret;
  64. ret = tb_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
  65. if (ret)
  66. return ret;
  67. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
  68. if (ret)
  69. return ret;
  70. if (val & ROUTER_CS_26_ONS)
  71. return -EOPNOTSUPP;
  72. if (status)
  73. *status = (val & ROUTER_CS_26_STATUS_MASK) >>
  74. ROUTER_CS_26_STATUS_SHIFT;
  75. if (metadata) {
  76. ret = tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
  77. if (ret)
  78. return ret;
  79. }
  80. if (rx_dwords) {
  81. ret = tb_sw_read(sw, rx_data, TB_CFG_SWITCH, ROUTER_CS_9,
  82. rx_dwords);
  83. if (ret)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int __usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
  89. u8 *status, const void *tx_data, size_t tx_dwords,
  90. void *rx_data, size_t rx_dwords)
  91. {
  92. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  93. if (tx_dwords > USB4_DATA_DWORDS || rx_dwords > USB4_DATA_DWORDS)
  94. return -EINVAL;
  95. /*
  96. * If the connection manager implementation provides USB4 router
  97. * operation proxy callback, call it here instead of running the
  98. * operation natively.
  99. */
  100. if (cm_ops->usb4_switch_op) {
  101. int ret;
  102. ret = cm_ops->usb4_switch_op(sw, opcode, metadata, status,
  103. tx_data, tx_dwords, rx_data,
  104. rx_dwords);
  105. if (ret != -EOPNOTSUPP)
  106. return ret;
  107. /*
  108. * If the proxy was not supported then run the native
  109. * router operation instead.
  110. */
  111. }
  112. return usb4_native_switch_op(sw, opcode, metadata, status, tx_data,
  113. tx_dwords, rx_data, rx_dwords);
  114. }
  115. static inline int usb4_switch_op(struct tb_switch *sw, u16 opcode,
  116. u32 *metadata, u8 *status)
  117. {
  118. return __usb4_switch_op(sw, opcode, metadata, status, NULL, 0, NULL, 0);
  119. }
  120. static inline int usb4_switch_op_data(struct tb_switch *sw, u16 opcode,
  121. u32 *metadata, u8 *status,
  122. const void *tx_data, size_t tx_dwords,
  123. void *rx_data, size_t rx_dwords)
  124. {
  125. return __usb4_switch_op(sw, opcode, metadata, status, tx_data,
  126. tx_dwords, rx_data, rx_dwords);
  127. }
  128. /**
  129. * usb4_switch_check_wakes() - Check for wakes and notify PM core about them
  130. * @sw: Router whose wakes to check
  131. *
  132. * Checks wakes occurred during suspend and notify the PM core about them.
  133. */
  134. void usb4_switch_check_wakes(struct tb_switch *sw)
  135. {
  136. bool wakeup_usb4 = false;
  137. struct usb4_port *usb4;
  138. struct tb_port *port;
  139. bool wakeup = false;
  140. u32 val;
  141. if (tb_route(sw)) {
  142. if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
  143. return;
  144. tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
  145. str_yes_no(val & ROUTER_CS_6_WOPS),
  146. str_yes_no(val & ROUTER_CS_6_WOUS));
  147. wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
  148. }
  149. /*
  150. * Check for any downstream ports for USB4 wake,
  151. * connection wake and disconnection wake.
  152. */
  153. tb_switch_for_each_port(sw, port) {
  154. if (!port->cap_usb4)
  155. continue;
  156. if (tb_port_read(port, &val, TB_CFG_PORT,
  157. port->cap_usb4 + PORT_CS_18, 1))
  158. break;
  159. tb_port_dbg(port, "USB4 wake: %s, connection wake: %s, disconnection wake: %s\n",
  160. str_yes_no(val & PORT_CS_18_WOU4S),
  161. str_yes_no(val & PORT_CS_18_WOCS),
  162. str_yes_no(val & PORT_CS_18_WODS));
  163. wakeup_usb4 = val & (PORT_CS_18_WOU4S | PORT_CS_18_WOCS |
  164. PORT_CS_18_WODS);
  165. usb4 = port->usb4;
  166. if (device_may_wakeup(&usb4->dev) && wakeup_usb4)
  167. pm_wakeup_event(&usb4->dev, 0);
  168. wakeup |= wakeup_usb4;
  169. }
  170. if (wakeup)
  171. pm_wakeup_event(&sw->dev, 0);
  172. }
  173. static bool link_is_usb4(struct tb_port *port)
  174. {
  175. u32 val;
  176. if (!port->cap_usb4)
  177. return false;
  178. if (tb_port_read(port, &val, TB_CFG_PORT,
  179. port->cap_usb4 + PORT_CS_18, 1))
  180. return false;
  181. return !(val & PORT_CS_18_TCM);
  182. }
  183. /**
  184. * usb4_switch_setup() - Additional setup for USB4 device
  185. * @sw: USB4 router to setup
  186. *
  187. * USB4 routers need additional settings in order to enable all the
  188. * tunneling. This function enables USB and PCIe tunneling if it can be
  189. * enabled (e.g the parent switch also supports them). If USB tunneling
  190. * is not available for some reason (like that there is Thunderbolt 3
  191. * switch upstream) then the internal xHCI controller is enabled
  192. * instead.
  193. *
  194. * This does not set the configuration valid bit of the router. To do
  195. * that call usb4_switch_configuration_valid().
  196. *
  197. * Return: %0 on success, negative errno otherwise.
  198. */
  199. int usb4_switch_setup(struct tb_switch *sw)
  200. {
  201. struct tb_switch *parent = tb_switch_parent(sw);
  202. struct tb_port *down;
  203. bool tbt3, xhci;
  204. u32 val = 0;
  205. int ret;
  206. if (!tb_route(sw))
  207. return 0;
  208. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
  209. if (ret)
  210. return ret;
  211. down = tb_switch_downstream_port(sw);
  212. sw->link_usb4 = link_is_usb4(down);
  213. tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
  214. xhci = val & ROUTER_CS_6_HCI;
  215. tbt3 = !(val & ROUTER_CS_6_TNS);
  216. tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
  217. str_yes_no(tbt3), str_yes_no(xhci));
  218. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  219. if (ret)
  220. return ret;
  221. if (tb_acpi_may_tunnel_usb3() && sw->link_usb4 &&
  222. tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
  223. val |= ROUTER_CS_5_UTO;
  224. xhci = false;
  225. }
  226. /*
  227. * Only enable PCIe tunneling if the parent router supports it
  228. * and it is not disabled.
  229. */
  230. if (tb_acpi_may_tunnel_pcie() &&
  231. tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
  232. val |= ROUTER_CS_5_PTO;
  233. /*
  234. * xHCI can be enabled if PCIe tunneling is supported
  235. * and the parent does not have any USB3 downstream
  236. * adapters (so we cannot do USB 3.x tunneling).
  237. */
  238. if (xhci)
  239. val |= ROUTER_CS_5_HCO;
  240. }
  241. /* TBT3 supported by the CM */
  242. val &= ~ROUTER_CS_5_CNS;
  243. return tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  244. }
  245. /**
  246. * usb4_switch_configuration_valid() - Set tunneling configuration to be valid
  247. * @sw: USB4 router
  248. *
  249. * Sets configuration valid bit for the router. Must be called before
  250. * any tunnels can be set through the router and after
  251. * usb4_switch_setup() has been called. Can be called to host and device
  252. * routers (does nothing for the latter).
  253. *
  254. * Return: %0 on success, negative errno otherwise.
  255. */
  256. int usb4_switch_configuration_valid(struct tb_switch *sw)
  257. {
  258. u32 val;
  259. int ret;
  260. if (!tb_route(sw))
  261. return 0;
  262. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  263. if (ret)
  264. return ret;
  265. val |= ROUTER_CS_5_CV;
  266. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  267. if (ret)
  268. return ret;
  269. return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
  270. ROUTER_CS_6_CR, 50);
  271. }
  272. /**
  273. * usb4_switch_read_uid() - Read UID from USB4 router
  274. * @sw: USB4 router
  275. * @uid: UID is stored here
  276. *
  277. * Reads 64-bit UID from USB4 router config space.
  278. *
  279. * Return: %0 on success, negative errno otherwise.
  280. */
  281. int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
  282. {
  283. return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
  284. }
  285. static int usb4_switch_drom_read_block(void *data,
  286. unsigned int dwaddress, void *buf,
  287. size_t dwords)
  288. {
  289. struct tb_switch *sw = data;
  290. u8 status = 0;
  291. u32 metadata;
  292. int ret;
  293. metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
  294. metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
  295. USB4_DROM_ADDRESS_MASK;
  296. ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_DROM_READ, &metadata,
  297. &status, NULL, 0, buf, dwords);
  298. if (ret)
  299. return ret;
  300. return status ? -EIO : 0;
  301. }
  302. /**
  303. * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
  304. * @sw: USB4 router
  305. * @address: Byte address inside DROM to start reading
  306. * @buf: Buffer where the DROM content is stored
  307. * @size: Number of bytes to read from DROM
  308. *
  309. * Uses USB4 router operations to read router DROM. For devices this
  310. * should always work but for hosts it may return %-EOPNOTSUPP in which
  311. * case the host router does not have DROM.
  312. *
  313. * Return: %0 on success, negative errno otherwise.
  314. */
  315. int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
  316. size_t size)
  317. {
  318. return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
  319. usb4_switch_drom_read_block, sw);
  320. }
  321. /**
  322. * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
  323. * @sw: USB4 router
  324. *
  325. * Checks whether conditions are met so that lane bonding can be
  326. * established with the upstream router. Call only for device routers.
  327. *
  328. * Return: %true if lane bonding is possible, %false otherwise.
  329. */
  330. bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
  331. {
  332. struct tb_port *up;
  333. int ret;
  334. u32 val;
  335. up = tb_upstream_port(sw);
  336. ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
  337. if (ret)
  338. return false;
  339. return !!(val & PORT_CS_18_BE);
  340. }
  341. /**
  342. * usb4_switch_set_wake() - Enabled/disable wake
  343. * @sw: USB4 router
  344. * @flags: Wakeup flags (%0 to disable)
  345. * @runtime: Wake is being programmed during system runtime
  346. *
  347. * Enables/disables router to wake up from sleep.
  348. *
  349. * Return: %0 on success, negative errno otherwise.
  350. */
  351. int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags, bool runtime)
  352. {
  353. struct tb_port *port;
  354. u64 route = tb_route(sw);
  355. u32 val;
  356. int ret;
  357. /*
  358. * Enable wakes coming from all USB4 downstream ports (from
  359. * child routers). For device routers do this also for the
  360. * upstream USB4 port.
  361. */
  362. tb_switch_for_each_port(sw, port) {
  363. if (!tb_port_is_null(port))
  364. continue;
  365. if (!route && tb_is_upstream_port(port))
  366. continue;
  367. if (!port->cap_usb4)
  368. continue;
  369. ret = tb_port_read(port, &val, TB_CFG_PORT,
  370. port->cap_usb4 + PORT_CS_19, 1);
  371. if (ret)
  372. return ret;
  373. val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
  374. if (tb_is_upstream_port(port)) {
  375. val |= PORT_CS_19_WOU4;
  376. } else {
  377. bool configured = val & PORT_CS_19_PC;
  378. bool wakeup = runtime || device_may_wakeup(&port->usb4->dev);
  379. if ((flags & TB_WAKE_ON_CONNECT) && wakeup && !configured)
  380. val |= PORT_CS_19_WOC;
  381. if ((flags & TB_WAKE_ON_DISCONNECT) && wakeup && configured)
  382. val |= PORT_CS_19_WOD;
  383. if ((flags & TB_WAKE_ON_USB4) && configured)
  384. val |= PORT_CS_19_WOU4;
  385. }
  386. ret = tb_port_write(port, &val, TB_CFG_PORT,
  387. port->cap_usb4 + PORT_CS_19, 1);
  388. if (ret)
  389. return ret;
  390. }
  391. /*
  392. * Enable wakes from PCIe, USB 3.x and DP on this router. Only
  393. * needed for device routers.
  394. */
  395. if (route) {
  396. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  397. if (ret)
  398. return ret;
  399. val &= ~(ROUTER_CS_5_WOP | ROUTER_CS_5_WOU | ROUTER_CS_5_WOD);
  400. if (flags & TB_WAKE_ON_USB3)
  401. val |= ROUTER_CS_5_WOU;
  402. if (flags & TB_WAKE_ON_PCIE)
  403. val |= ROUTER_CS_5_WOP;
  404. if (flags & TB_WAKE_ON_DP)
  405. val |= ROUTER_CS_5_WOD;
  406. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  407. if (ret)
  408. return ret;
  409. }
  410. return 0;
  411. }
  412. /**
  413. * usb4_switch_set_sleep() - Prepare the router to enter sleep
  414. * @sw: USB4 router
  415. *
  416. * Sets sleep bit for the router and waits until router sleep ready
  417. * bit has been asserted.
  418. *
  419. * Return: %0 on success, negative errno otherwise.
  420. */
  421. int usb4_switch_set_sleep(struct tb_switch *sw)
  422. {
  423. int ret;
  424. u32 val;
  425. /* Set sleep bit and wait for sleep ready to be asserted */
  426. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  427. if (ret)
  428. return ret;
  429. val |= ROUTER_CS_5_SLP;
  430. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
  431. if (ret)
  432. return ret;
  433. return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
  434. ROUTER_CS_6_SLPR, 500);
  435. }
  436. /**
  437. * usb4_switch_nvm_sector_size() - Return router NVM sector size
  438. * @sw: USB4 router
  439. *
  440. * Return:
  441. * * NVM sector size in bytes if router supports NVM operations.
  442. * * %-EOPNOTSUPP - If router does not support NVM operations.
  443. * * Negative errno - Another error occurred.
  444. */
  445. int usb4_switch_nvm_sector_size(struct tb_switch *sw)
  446. {
  447. u32 metadata;
  448. u8 status;
  449. int ret;
  450. ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &metadata,
  451. &status);
  452. if (ret)
  453. return ret;
  454. if (status)
  455. return status == 0x2 ? -EOPNOTSUPP : -EIO;
  456. return metadata & USB4_NVM_SECTOR_SIZE_MASK;
  457. }
  458. static int usb4_switch_nvm_read_block(void *data,
  459. unsigned int dwaddress, void *buf, size_t dwords)
  460. {
  461. struct tb_switch *sw = data;
  462. u8 status = 0;
  463. u32 metadata;
  464. int ret;
  465. metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
  466. USB4_NVM_READ_LENGTH_MASK;
  467. metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
  468. USB4_NVM_READ_OFFSET_MASK;
  469. ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_READ, &metadata,
  470. &status, NULL, 0, buf, dwords);
  471. if (ret)
  472. return ret;
  473. return status ? -EIO : 0;
  474. }
  475. /**
  476. * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
  477. * @sw: USB4 router
  478. * @address: Starting address in bytes
  479. * @buf: Read data is placed here
  480. * @size: How many bytes to read
  481. *
  482. * Reads NVM contents of the router.
  483. *
  484. * Return:
  485. * * %0 - Read completed successfully.
  486. * * %-EOPNOTSUPP - NVM not supported.
  487. * * Negative errno - Another error occurred.
  488. */
  489. int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
  490. size_t size)
  491. {
  492. return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
  493. usb4_switch_nvm_read_block, sw);
  494. }
  495. /**
  496. * usb4_switch_nvm_set_offset() - Set NVM write offset
  497. * @sw: USB4 router
  498. * @address: Start offset
  499. *
  500. * Explicitly sets NVM write offset. Normally when writing to NVM this
  501. * is done automatically by usb4_switch_nvm_write().
  502. *
  503. * Return: %0 on success, negative errno otherwise.
  504. */
  505. int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address)
  506. {
  507. u32 metadata, dwaddress;
  508. u8 status = 0;
  509. int ret;
  510. dwaddress = address / 4;
  511. metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
  512. USB4_NVM_SET_OFFSET_MASK;
  513. ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &metadata,
  514. &status);
  515. if (ret)
  516. return ret;
  517. return status ? -EIO : 0;
  518. }
  519. static int usb4_switch_nvm_write_next_block(void *data, unsigned int dwaddress,
  520. const void *buf, size_t dwords)
  521. {
  522. struct tb_switch *sw = data;
  523. u8 status;
  524. int ret;
  525. ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_WRITE, NULL, &status,
  526. buf, dwords, NULL, 0);
  527. if (ret)
  528. return ret;
  529. return status ? -EIO : 0;
  530. }
  531. /**
  532. * usb4_switch_nvm_write() - Write to the router NVM
  533. * @sw: USB4 router
  534. * @address: Start address where to write in bytes
  535. * @buf: Pointer to the data to write
  536. * @size: Size of @buf in bytes
  537. *
  538. * Writes @buf to the router NVM using USB4 router operations.
  539. *
  540. * Return:
  541. * * %0 - Write completed successfully.
  542. * * %-EOPNOTSUPP - NVM write not supported.
  543. * * Negative errno - Another error occurred.
  544. */
  545. int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
  546. const void *buf, size_t size)
  547. {
  548. int ret;
  549. ret = usb4_switch_nvm_set_offset(sw, address);
  550. if (ret)
  551. return ret;
  552. return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
  553. usb4_switch_nvm_write_next_block, sw);
  554. }
  555. /**
  556. * usb4_switch_nvm_authenticate() - Authenticate new NVM
  557. * @sw: USB4 router
  558. *
  559. * After the new NVM has been written via usb4_switch_nvm_write(), this
  560. * function triggers NVM authentication process. The router gets power
  561. * cycled and if the authentication is successful the new NVM starts
  562. * running.
  563. *
  564. * The caller should call usb4_switch_nvm_authenticate_status() to read
  565. * the status of the authentication after power cycle. It should be the
  566. * first router operation to avoid the status being lost.
  567. *
  568. * Return: %0 on success, negative errno otherwise.
  569. */
  570. int usb4_switch_nvm_authenticate(struct tb_switch *sw)
  571. {
  572. int ret;
  573. ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, NULL, NULL);
  574. switch (ret) {
  575. /*
  576. * The router is power cycled once NVM_AUTH is started so it is
  577. * expected to get any of the following errors back.
  578. */
  579. case -EACCES:
  580. case -ENOTCONN:
  581. case -ETIMEDOUT:
  582. return 0;
  583. default:
  584. return ret;
  585. }
  586. }
  587. /**
  588. * usb4_switch_nvm_authenticate_status() - Read status of last NVM authenticate
  589. * @sw: USB4 router
  590. * @status: Status code of the operation
  591. *
  592. * The function checks if there is status available from the last NVM
  593. * authenticate router operation.
  594. *
  595. * Must be called before any other router operation.
  596. *
  597. * Return:
  598. * * %0 - If there is status. Status code is placed in @status.
  599. * * Negative errno - Failure occurred.
  600. */
  601. int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status)
  602. {
  603. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  604. u16 opcode;
  605. u32 val;
  606. int ret;
  607. if (cm_ops->usb4_switch_nvm_authenticate_status) {
  608. ret = cm_ops->usb4_switch_nvm_authenticate_status(sw, status);
  609. if (ret != -EOPNOTSUPP)
  610. return ret;
  611. }
  612. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
  613. if (ret)
  614. return ret;
  615. /* Check that the opcode is correct */
  616. opcode = val & ROUTER_CS_26_OPCODE_MASK;
  617. if (opcode == USB4_SWITCH_OP_NVM_AUTH) {
  618. if (val & ROUTER_CS_26_OV)
  619. return -EBUSY;
  620. if (val & ROUTER_CS_26_ONS)
  621. return -EOPNOTSUPP;
  622. *status = (val & ROUTER_CS_26_STATUS_MASK) >>
  623. ROUTER_CS_26_STATUS_SHIFT;
  624. } else {
  625. *status = 0;
  626. }
  627. return 0;
  628. }
  629. /**
  630. * usb4_switch_credits_init() - Read buffer allocation parameters
  631. * @sw: USB4 router
  632. *
  633. * Reads @sw buffer allocation parameters and initializes @sw buffer
  634. * allocation fields accordingly. Specifically @sw->credits_allocation
  635. * is set to %true if these parameters can be used in tunneling.
  636. *
  637. * Return: %0 on success, negative errno otherwise.
  638. */
  639. int usb4_switch_credits_init(struct tb_switch *sw)
  640. {
  641. int max_usb3, min_dp_aux, min_dp_main, max_pcie, max_dma;
  642. int ret, length, i, nports;
  643. const struct tb_port *port;
  644. u32 data[USB4_DATA_DWORDS];
  645. u32 metadata = 0;
  646. u8 status = 0;
  647. memset(data, 0, sizeof(data));
  648. ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_BUFFER_ALLOC, &metadata,
  649. &status, NULL, 0, data, ARRAY_SIZE(data));
  650. if (ret)
  651. return ret;
  652. if (status)
  653. return -EIO;
  654. length = metadata & USB4_BA_LENGTH_MASK;
  655. if (WARN_ON(length > ARRAY_SIZE(data)))
  656. return -EMSGSIZE;
  657. max_usb3 = -1;
  658. min_dp_aux = -1;
  659. min_dp_main = -1;
  660. max_pcie = -1;
  661. max_dma = -1;
  662. tb_sw_dbg(sw, "credit allocation parameters:\n");
  663. for (i = 0; i < length; i++) {
  664. u16 index, value;
  665. index = data[i] & USB4_BA_INDEX_MASK;
  666. value = (data[i] & USB4_BA_VALUE_MASK) >> USB4_BA_VALUE_SHIFT;
  667. switch (index) {
  668. case USB4_BA_MAX_USB3:
  669. tb_sw_dbg(sw, " USB3: %u\n", value);
  670. max_usb3 = value;
  671. break;
  672. case USB4_BA_MIN_DP_AUX:
  673. tb_sw_dbg(sw, " DP AUX: %u\n", value);
  674. min_dp_aux = value;
  675. break;
  676. case USB4_BA_MIN_DP_MAIN:
  677. tb_sw_dbg(sw, " DP main: %u\n", value);
  678. min_dp_main = value;
  679. break;
  680. case USB4_BA_MAX_PCIE:
  681. tb_sw_dbg(sw, " PCIe: %u\n", value);
  682. max_pcie = value;
  683. break;
  684. case USB4_BA_MAX_HI:
  685. tb_sw_dbg(sw, " DMA: %u\n", value);
  686. max_dma = value;
  687. break;
  688. default:
  689. tb_sw_dbg(sw, " unknown credit allocation index %#x, skipping\n",
  690. index);
  691. break;
  692. }
  693. }
  694. /*
  695. * Validate the buffer allocation preferences. If we find
  696. * issues, log a warning and fall back using the hard-coded
  697. * values.
  698. */
  699. /* Host router must report baMaxHI */
  700. if (!tb_route(sw) && max_dma < 0) {
  701. tb_sw_warn(sw, "host router is missing baMaxHI\n");
  702. goto err_invalid;
  703. }
  704. nports = 0;
  705. tb_switch_for_each_port(sw, port) {
  706. if (tb_port_is_null(port))
  707. nports++;
  708. }
  709. /* Must have DP buffer allocation (multiple USB4 ports) */
  710. if (nports > 2 && (min_dp_aux < 0 || min_dp_main < 0)) {
  711. tb_sw_warn(sw, "multiple USB4 ports require baMinDPaux/baMinDPmain\n");
  712. goto err_invalid;
  713. }
  714. tb_switch_for_each_port(sw, port) {
  715. if (tb_port_is_dpout(port) && min_dp_main < 0) {
  716. tb_sw_warn(sw, "missing baMinDPmain");
  717. goto err_invalid;
  718. }
  719. if ((tb_port_is_dpin(port) || tb_port_is_dpout(port)) &&
  720. min_dp_aux < 0) {
  721. tb_sw_warn(sw, "missing baMinDPaux");
  722. goto err_invalid;
  723. }
  724. if ((tb_port_is_usb3_down(port) || tb_port_is_usb3_up(port)) &&
  725. max_usb3 < 0) {
  726. tb_sw_warn(sw, "missing baMaxUSB3");
  727. goto err_invalid;
  728. }
  729. if ((tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) &&
  730. max_pcie < 0) {
  731. tb_sw_warn(sw, "missing baMaxPCIe");
  732. goto err_invalid;
  733. }
  734. }
  735. /*
  736. * Buffer allocation passed the validation so we can use it in
  737. * path creation.
  738. */
  739. sw->credit_allocation = true;
  740. if (max_usb3 > 0)
  741. sw->max_usb3_credits = max_usb3;
  742. if (min_dp_aux > 0)
  743. sw->min_dp_aux_credits = min_dp_aux;
  744. if (min_dp_main > 0)
  745. sw->min_dp_main_credits = min_dp_main;
  746. if (max_pcie > 0)
  747. sw->max_pcie_credits = max_pcie;
  748. if (max_dma > 0)
  749. sw->max_dma_credits = max_dma;
  750. return 0;
  751. err_invalid:
  752. return -EINVAL;
  753. }
  754. /**
  755. * usb4_switch_query_dp_resource() - Query availability of DP IN resource
  756. * @sw: USB4 router
  757. * @in: DP IN adapter
  758. *
  759. * For DP tunneling this function can be used to query availability of
  760. * DP IN resource.
  761. *
  762. * Return: %true if the resource is available for DP tunneling, %false
  763. * otherwise.
  764. */
  765. bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
  766. {
  767. u32 metadata = in->port;
  768. u8 status;
  769. int ret;
  770. ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &metadata,
  771. &status);
  772. /*
  773. * If DP resource allocation is not supported assume it is
  774. * always available.
  775. */
  776. if (ret == -EOPNOTSUPP)
  777. return true;
  778. if (ret)
  779. return false;
  780. return !status;
  781. }
  782. /**
  783. * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
  784. * @sw: USB4 router
  785. * @in: DP IN adapter
  786. *
  787. * Allocates DP IN resource for DP tunneling using USB4 router
  788. * operations.
  789. *
  790. * Return:
  791. * * %0 - Resource allocated successfully.
  792. * * %-EBUSY - Resource is already allocated.
  793. * * Negative errno - Other failure occurred.
  794. */
  795. int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  796. {
  797. u32 metadata = in->port;
  798. u8 status;
  799. int ret;
  800. ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &metadata,
  801. &status);
  802. if (ret == -EOPNOTSUPP)
  803. return 0;
  804. if (ret)
  805. return ret;
  806. return status ? -EBUSY : 0;
  807. }
  808. /**
  809. * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
  810. * @sw: USB4 router
  811. * @in: DP IN adapter
  812. *
  813. * Releases the previously allocated DP IN resource.
  814. *
  815. * Return: %0 on success, negative errno otherwise.
  816. */
  817. int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  818. {
  819. u32 metadata = in->port;
  820. u8 status;
  821. int ret;
  822. ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &metadata,
  823. &status);
  824. if (ret == -EOPNOTSUPP)
  825. return 0;
  826. if (ret)
  827. return ret;
  828. return status ? -EIO : 0;
  829. }
  830. /**
  831. * usb4_port_index() - Finds matching USB4 port index
  832. * @sw: USB4 router
  833. * @port: USB4 protocol or lane adapter
  834. *
  835. * Finds matching USB4 port index (starting from %0) that given @port goes
  836. * through.
  837. */
  838. int usb4_port_index(const struct tb_switch *sw, const struct tb_port *port)
  839. {
  840. struct tb_port *p;
  841. int usb4_idx = 0;
  842. /* Assume port is primary */
  843. tb_switch_for_each_port(sw, p) {
  844. if (!tb_port_is_null(p))
  845. continue;
  846. if (tb_is_upstream_port(p))
  847. continue;
  848. if (!p->link_nr) {
  849. if (p == port)
  850. break;
  851. usb4_idx++;
  852. }
  853. }
  854. return usb4_idx;
  855. }
  856. /**
  857. * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
  858. * @sw: USB4 router
  859. * @port: USB4 port
  860. *
  861. * USB4 routers have direct mapping between USB4 ports and PCIe
  862. * downstream adapters where the PCIe topology is extended. This
  863. * function returns the corresponding downstream PCIe adapter or %NULL
  864. * if no such mapping was possible.
  865. *
  866. * Return: Pointer to &struct tb_port or %NULL if not found.
  867. */
  868. struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
  869. const struct tb_port *port)
  870. {
  871. int usb4_idx = usb4_port_index(sw, port);
  872. struct tb_port *p;
  873. int pcie_idx = 0;
  874. /* Find PCIe down port matching usb4_port */
  875. tb_switch_for_each_port(sw, p) {
  876. if (!tb_port_is_pcie_down(p))
  877. continue;
  878. if (pcie_idx == usb4_idx)
  879. return p;
  880. pcie_idx++;
  881. }
  882. return NULL;
  883. }
  884. /**
  885. * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
  886. * @sw: USB4 router
  887. * @port: USB4 port
  888. *
  889. * USB4 routers have direct mapping between USB4 ports and USB 3.x
  890. * downstream adapters where the USB 3.x topology is extended. This
  891. * function returns the corresponding downstream USB 3.x adapter or
  892. * %NULL if no such mapping was possible.
  893. *
  894. * Return: Pointer to &struct tb_port or %NULL if not found.
  895. */
  896. struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
  897. const struct tb_port *port)
  898. {
  899. int usb4_idx = usb4_port_index(sw, port);
  900. struct tb_port *p;
  901. int usb_idx = 0;
  902. /* Find USB3 down port matching usb4_port */
  903. tb_switch_for_each_port(sw, p) {
  904. if (!tb_port_is_usb3_down(p))
  905. continue;
  906. if (usb_idx == usb4_idx)
  907. return p;
  908. usb_idx++;
  909. }
  910. return NULL;
  911. }
  912. /**
  913. * usb4_switch_add_ports() - Add USB4 ports for this router
  914. * @sw: USB4 router
  915. *
  916. * For USB4 router finds all USB4 ports and registers devices for each.
  917. * Can be called to any router.
  918. *
  919. * Return: %0 on success, negative errno otherwise.
  920. */
  921. int usb4_switch_add_ports(struct tb_switch *sw)
  922. {
  923. struct tb_port *port;
  924. if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
  925. return 0;
  926. tb_switch_for_each_port(sw, port) {
  927. struct usb4_port *usb4;
  928. if (!tb_port_is_null(port))
  929. continue;
  930. if (!port->cap_usb4)
  931. continue;
  932. usb4 = usb4_port_device_add(port);
  933. if (IS_ERR(usb4)) {
  934. usb4_switch_remove_ports(sw);
  935. return PTR_ERR(usb4);
  936. }
  937. port->usb4 = usb4;
  938. }
  939. return 0;
  940. }
  941. /**
  942. * usb4_switch_remove_ports() - Removes USB4 ports from this router
  943. * @sw: USB4 router
  944. *
  945. * Unregisters previously registered USB4 ports.
  946. */
  947. void usb4_switch_remove_ports(struct tb_switch *sw)
  948. {
  949. struct tb_port *port;
  950. tb_switch_for_each_port(sw, port) {
  951. if (port->usb4) {
  952. usb4_port_device_remove(port->usb4);
  953. port->usb4 = NULL;
  954. }
  955. }
  956. }
  957. /**
  958. * usb4_port_unlock() - Unlock USB4 downstream port
  959. * @port: USB4 port to unlock
  960. *
  961. * Unlocks USB4 downstream port so that the connection manager can
  962. * access the router below this port.
  963. *
  964. * Return: %0 on success, negative errno otherwise.
  965. */
  966. int usb4_port_unlock(struct tb_port *port)
  967. {
  968. int ret;
  969. u32 val;
  970. ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
  971. if (ret)
  972. return ret;
  973. val &= ~ADP_CS_4_LCK;
  974. return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
  975. }
  976. /**
  977. * usb4_port_hotplug_enable() - Enables hotplug for a port
  978. * @port: USB4 port to operate on
  979. *
  980. * Enables hot plug events on a given port. This is only intended
  981. * to be used on lane, DP-IN, and DP-OUT adapters.
  982. *
  983. * Return: %0 on success, negative errno otherwise.
  984. */
  985. int usb4_port_hotplug_enable(struct tb_port *port)
  986. {
  987. int ret;
  988. u32 val;
  989. ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
  990. if (ret)
  991. return ret;
  992. val &= ~ADP_CS_5_DHP;
  993. return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
  994. }
  995. /**
  996. * usb4_port_reset() - Issue downstream port reset
  997. * @port: USB4 port to reset
  998. *
  999. * Issues downstream port reset to @port.
  1000. *
  1001. * Return: %0 on success, negative errno otherwise.
  1002. */
  1003. int usb4_port_reset(struct tb_port *port)
  1004. {
  1005. int ret;
  1006. u32 val;
  1007. if (!port->cap_usb4)
  1008. return -EINVAL;
  1009. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1010. port->cap_usb4 + PORT_CS_19, 1);
  1011. if (ret)
  1012. return ret;
  1013. val |= PORT_CS_19_DPR;
  1014. ret = tb_port_write(port, &val, TB_CFG_PORT,
  1015. port->cap_usb4 + PORT_CS_19, 1);
  1016. if (ret)
  1017. return ret;
  1018. fsleep(10000);
  1019. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1020. port->cap_usb4 + PORT_CS_19, 1);
  1021. if (ret)
  1022. return ret;
  1023. val &= ~PORT_CS_19_DPR;
  1024. return tb_port_write(port, &val, TB_CFG_PORT,
  1025. port->cap_usb4 + PORT_CS_19, 1);
  1026. }
  1027. static int usb4_port_set_configured(struct tb_port *port, bool configured)
  1028. {
  1029. int ret;
  1030. u32 val;
  1031. if (!port->cap_usb4)
  1032. return -EINVAL;
  1033. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1034. port->cap_usb4 + PORT_CS_19, 1);
  1035. if (ret)
  1036. return ret;
  1037. if (configured)
  1038. val |= PORT_CS_19_PC;
  1039. else
  1040. val &= ~PORT_CS_19_PC;
  1041. return tb_port_write(port, &val, TB_CFG_PORT,
  1042. port->cap_usb4 + PORT_CS_19, 1);
  1043. }
  1044. /**
  1045. * usb4_port_configure() - Set USB4 port configured
  1046. * @port: USB4 router
  1047. *
  1048. * Sets the USB4 link to be configured for power management purposes.
  1049. *
  1050. * Return: %0 on success, negative errno otherwise.
  1051. */
  1052. int usb4_port_configure(struct tb_port *port)
  1053. {
  1054. return usb4_port_set_configured(port, true);
  1055. }
  1056. /**
  1057. * usb4_port_unconfigure() - Set USB4 port unconfigured
  1058. * @port: USB4 router
  1059. *
  1060. * Sets the USB4 link to be unconfigured for power management purposes.
  1061. *
  1062. * Return: %0 on success, negative errno otherwise.
  1063. */
  1064. void usb4_port_unconfigure(struct tb_port *port)
  1065. {
  1066. usb4_port_set_configured(port, false);
  1067. }
  1068. static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
  1069. {
  1070. int ret;
  1071. u32 val;
  1072. if (!port->cap_usb4)
  1073. return -EINVAL;
  1074. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1075. port->cap_usb4 + PORT_CS_19, 1);
  1076. if (ret)
  1077. return ret;
  1078. if (configured)
  1079. val |= PORT_CS_19_PID;
  1080. else
  1081. val &= ~PORT_CS_19_PID;
  1082. return tb_port_write(port, &val, TB_CFG_PORT,
  1083. port->cap_usb4 + PORT_CS_19, 1);
  1084. }
  1085. /**
  1086. * usb4_port_configure_xdomain() - Configure port for XDomain
  1087. * @port: USB4 port connected to another host
  1088. * @xd: XDomain that is connected to the port
  1089. *
  1090. * Marks the USB4 port as being connected to another host and updates
  1091. * the link type.
  1092. *
  1093. * Return: %0 on success, negative errno otherwise.
  1094. */
  1095. int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
  1096. {
  1097. xd->link_usb4 = link_is_usb4(port);
  1098. return usb4_set_xdomain_configured(port, true);
  1099. }
  1100. /**
  1101. * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
  1102. * @port: USB4 port that was connected to another host
  1103. *
  1104. * Clears USB4 port from being marked as XDomain.
  1105. */
  1106. void usb4_port_unconfigure_xdomain(struct tb_port *port)
  1107. {
  1108. usb4_set_xdomain_configured(port, false);
  1109. }
  1110. static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
  1111. u32 value, int timeout_msec, unsigned long delay_usec)
  1112. {
  1113. ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
  1114. do {
  1115. u32 val;
  1116. int ret;
  1117. ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
  1118. if (ret)
  1119. return ret;
  1120. if ((val & bit) == value)
  1121. return 0;
  1122. fsleep(delay_usec);
  1123. } while (ktime_before(ktime_get(), timeout));
  1124. return -ETIMEDOUT;
  1125. }
  1126. static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
  1127. {
  1128. if (dwords > USB4_DATA_DWORDS)
  1129. return -EINVAL;
  1130. return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
  1131. dwords);
  1132. }
  1133. static int usb4_port_write_data(struct tb_port *port, const void *data,
  1134. size_t dwords)
  1135. {
  1136. if (dwords > USB4_DATA_DWORDS)
  1137. return -EINVAL;
  1138. return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
  1139. dwords);
  1140. }
  1141. /**
  1142. * usb4_port_sb_read() - Read from sideband register
  1143. * @port: USB4 port to read
  1144. * @target: Sideband target
  1145. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1146. * @reg: Sideband register index
  1147. * @buf: Buffer where the sideband data is copied
  1148. * @size: Size of @buf
  1149. *
  1150. * Reads data from sideband register @reg and copies it into @buf.
  1151. *
  1152. * Return: %0 on success, negative errno otherwise.
  1153. */
  1154. int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target, u8 index,
  1155. u8 reg, void *buf, u8 size)
  1156. {
  1157. size_t dwords = DIV_ROUND_UP(size, 4);
  1158. int ret;
  1159. u32 val;
  1160. if (!port->cap_usb4)
  1161. return -EINVAL;
  1162. val = reg;
  1163. val |= size << PORT_CS_1_LENGTH_SHIFT;
  1164. val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
  1165. if (target == USB4_SB_TARGET_RETIMER)
  1166. val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
  1167. val |= PORT_CS_1_PND;
  1168. ret = tb_port_write(port, &val, TB_CFG_PORT,
  1169. port->cap_usb4 + PORT_CS_1, 1);
  1170. if (ret)
  1171. return ret;
  1172. ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
  1173. PORT_CS_1_PND, 0, 500, USB4_PORT_SB_DELAY);
  1174. if (ret)
  1175. return ret;
  1176. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1177. port->cap_usb4 + PORT_CS_1, 1);
  1178. if (ret)
  1179. return ret;
  1180. if (val & PORT_CS_1_NR)
  1181. return -ENODEV;
  1182. if (val & PORT_CS_1_RC)
  1183. return -EIO;
  1184. return buf ? usb4_port_read_data(port, buf, dwords) : 0;
  1185. }
  1186. /**
  1187. * usb4_port_sb_write() - Write to sideband register
  1188. * @port: USB4 port to write
  1189. * @target: Sideband target
  1190. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1191. * @reg: Sideband register index
  1192. * @buf: Data to write
  1193. * @size: Size of @buf
  1194. *
  1195. * Writes @buf to sideband register @reg.
  1196. *
  1197. * Return: %0 on success, negative errno otherwise.
  1198. */
  1199. int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
  1200. u8 index, u8 reg, const void *buf, u8 size)
  1201. {
  1202. size_t dwords = DIV_ROUND_UP(size, 4);
  1203. int ret;
  1204. u32 val;
  1205. if (!port->cap_usb4)
  1206. return -EINVAL;
  1207. if (buf) {
  1208. ret = usb4_port_write_data(port, buf, dwords);
  1209. if (ret)
  1210. return ret;
  1211. }
  1212. val = reg;
  1213. val |= size << PORT_CS_1_LENGTH_SHIFT;
  1214. val |= PORT_CS_1_WNR_WRITE;
  1215. val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
  1216. if (target == USB4_SB_TARGET_RETIMER)
  1217. val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
  1218. val |= PORT_CS_1_PND;
  1219. ret = tb_port_write(port, &val, TB_CFG_PORT,
  1220. port->cap_usb4 + PORT_CS_1, 1);
  1221. if (ret)
  1222. return ret;
  1223. ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
  1224. PORT_CS_1_PND, 0, 500, USB4_PORT_SB_DELAY);
  1225. if (ret)
  1226. return ret;
  1227. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1228. port->cap_usb4 + PORT_CS_1, 1);
  1229. if (ret)
  1230. return ret;
  1231. if (val & PORT_CS_1_NR)
  1232. return -ENODEV;
  1233. if (val & PORT_CS_1_RC)
  1234. return -EIO;
  1235. return 0;
  1236. }
  1237. static int usb4_port_sb_opcode_err_to_errno(u32 val)
  1238. {
  1239. switch (val) {
  1240. case 0:
  1241. return 0;
  1242. case USB4_SB_OPCODE_ERR:
  1243. return -EAGAIN;
  1244. case USB4_SB_OPCODE_ONS:
  1245. return -EOPNOTSUPP;
  1246. default:
  1247. return -EIO;
  1248. }
  1249. }
  1250. static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
  1251. u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
  1252. {
  1253. ktime_t timeout;
  1254. u32 val;
  1255. int ret;
  1256. val = opcode;
  1257. ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
  1258. sizeof(val));
  1259. if (ret)
  1260. return ret;
  1261. timeout = ktime_add_ms(ktime_get(), timeout_msec);
  1262. do {
  1263. /* Check results */
  1264. ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
  1265. &val, sizeof(val));
  1266. if (ret)
  1267. return ret;
  1268. if (val != opcode)
  1269. return usb4_port_sb_opcode_err_to_errno(val);
  1270. fsleep(USB4_PORT_SB_DELAY);
  1271. } while (ktime_before(ktime_get(), timeout));
  1272. return -ETIMEDOUT;
  1273. }
  1274. static int usb4_port_set_router_offline(struct tb_port *port, bool offline)
  1275. {
  1276. u32 val = !offline;
  1277. int ret;
  1278. ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
  1279. USB4_SB_METADATA, &val, sizeof(val));
  1280. if (ret)
  1281. return ret;
  1282. val = USB4_SB_OPCODE_ROUTER_OFFLINE;
  1283. return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
  1284. USB4_SB_OPCODE, &val, sizeof(val));
  1285. }
  1286. /**
  1287. * usb4_port_router_offline() - Put the USB4 port to offline mode
  1288. * @port: USB4 port
  1289. *
  1290. * This function puts the USB4 port into offline mode. In this mode the
  1291. * port does not react on hotplug events anymore. This needs to be
  1292. * called before retimer access is done when the USB4 links is not up.
  1293. *
  1294. * Return: %0 on success, negative errno otherwise.
  1295. */
  1296. int usb4_port_router_offline(struct tb_port *port)
  1297. {
  1298. return usb4_port_set_router_offline(port, true);
  1299. }
  1300. /**
  1301. * usb4_port_router_online() - Put the USB4 port back online
  1302. * @port: USB4 port
  1303. *
  1304. * Makes the USB4 port functional again.
  1305. *
  1306. * Return: %0 on success, negative errno otherwise.
  1307. */
  1308. int usb4_port_router_online(struct tb_port *port)
  1309. {
  1310. return usb4_port_set_router_offline(port, false);
  1311. }
  1312. /**
  1313. * usb4_port_enumerate_retimers() - Send RT broadcast transaction
  1314. * @port: USB4 port
  1315. *
  1316. * This forces the USB4 port to send broadcast RT transaction which
  1317. * makes the retimers on the link assign index to themselves.
  1318. *
  1319. * Return: %0 on success, negative errno otherwise.
  1320. */
  1321. int usb4_port_enumerate_retimers(struct tb_port *port)
  1322. {
  1323. u32 val;
  1324. val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
  1325. return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
  1326. USB4_SB_OPCODE, &val, sizeof(val));
  1327. }
  1328. /**
  1329. * usb4_port_clx_supported() - Check if CLx is supported by the link
  1330. * @port: Port to check for CLx support for
  1331. *
  1332. * PORT_CS_18_CPS bit reflects if the link supports CLx including
  1333. * active cables (if connected on the link).
  1334. *
  1335. * Return: %true if Clx is supported, %false otherwise.
  1336. */
  1337. bool usb4_port_clx_supported(struct tb_port *port)
  1338. {
  1339. int ret;
  1340. u32 val;
  1341. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1342. port->cap_usb4 + PORT_CS_18, 1);
  1343. if (ret)
  1344. return false;
  1345. return !!(val & PORT_CS_18_CPS);
  1346. }
  1347. /**
  1348. * usb4_port_asym_supported() - If the port supports asymmetric link
  1349. * @port: USB4 port
  1350. *
  1351. * Checks if the port and the cable support asymmetric link.
  1352. *
  1353. * Return: %true if asymmetric link is supported, %false otherwise.
  1354. */
  1355. bool usb4_port_asym_supported(struct tb_port *port)
  1356. {
  1357. u32 val;
  1358. if (!port->cap_usb4)
  1359. return false;
  1360. if (tb_port_read(port, &val, TB_CFG_PORT, port->cap_usb4 + PORT_CS_18, 1))
  1361. return false;
  1362. return !!(val & PORT_CS_18_CSA);
  1363. }
  1364. /**
  1365. * usb4_port_asym_set_link_width() - Set link width to asymmetric or symmetric
  1366. * @port: USB4 port
  1367. * @width: Asymmetric width to configure
  1368. *
  1369. * Sets USB4 port link width to @width. Can be called for widths where
  1370. * usb4_port_asym_width_supported() returned @true.
  1371. *
  1372. * Return: %0 on success, negative errno otherwise.
  1373. */
  1374. int usb4_port_asym_set_link_width(struct tb_port *port, enum tb_link_width width)
  1375. {
  1376. u32 val;
  1377. int ret;
  1378. if (!port->cap_phy)
  1379. return -EINVAL;
  1380. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1381. port->cap_phy + LANE_ADP_CS_1, 1);
  1382. if (ret)
  1383. return ret;
  1384. val &= ~LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK;
  1385. switch (width) {
  1386. case TB_LINK_WIDTH_DUAL:
  1387. val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
  1388. LANE_ADP_CS_1_TARGET_WIDTH_ASYM_DUAL);
  1389. break;
  1390. case TB_LINK_WIDTH_ASYM_TX:
  1391. val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
  1392. LANE_ADP_CS_1_TARGET_WIDTH_ASYM_TX);
  1393. break;
  1394. case TB_LINK_WIDTH_ASYM_RX:
  1395. val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
  1396. LANE_ADP_CS_1_TARGET_WIDTH_ASYM_RX);
  1397. break;
  1398. default:
  1399. return -EINVAL;
  1400. }
  1401. return tb_port_write(port, &val, TB_CFG_PORT,
  1402. port->cap_phy + LANE_ADP_CS_1, 1);
  1403. }
  1404. /**
  1405. * usb4_port_asym_start() - Start symmetry change and wait for completion
  1406. * @port: USB4 port
  1407. *
  1408. * Start symmetry change of the link to asymmetric or symmetric
  1409. * (according to what was previously set in tb_port_set_link_width().
  1410. * Wait for completion of the change.
  1411. *
  1412. * Return:
  1413. * * %0 - Symmetry change was successful.
  1414. * * %-ETIMEDOUT - Timeout occurred.
  1415. * * Negative errno - Other failure occurred.
  1416. */
  1417. int usb4_port_asym_start(struct tb_port *port)
  1418. {
  1419. int ret;
  1420. u32 val;
  1421. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1422. port->cap_usb4 + PORT_CS_19, 1);
  1423. if (ret)
  1424. return ret;
  1425. val &= ~PORT_CS_19_START_ASYM;
  1426. val |= FIELD_PREP(PORT_CS_19_START_ASYM, 1);
  1427. ret = tb_port_write(port, &val, TB_CFG_PORT,
  1428. port->cap_usb4 + PORT_CS_19, 1);
  1429. if (ret)
  1430. return ret;
  1431. /*
  1432. * Wait for PORT_CS_19_START_ASYM to be 0. This means the USB4
  1433. * port started the symmetry transition.
  1434. */
  1435. ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_19,
  1436. PORT_CS_19_START_ASYM, 0, 1000,
  1437. USB4_PORT_DELAY);
  1438. if (ret)
  1439. return ret;
  1440. /* Then wait for the transtion to be completed */
  1441. return usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_18,
  1442. PORT_CS_18_TIP, 0, 5000, USB4_PORT_DELAY);
  1443. }
  1444. /**
  1445. * usb4_port_margining_caps() - Read USB4 port margining capabilities
  1446. * @port: USB4 port
  1447. * @target: Sideband target
  1448. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1449. * @caps: Array with at least two elements to hold the results
  1450. * @ncaps: Number of elements in the caps array
  1451. *
  1452. * Reads the USB4 port lane margining capabilities into @caps.
  1453. *
  1454. * Return: %0 on success, negative errno otherwise.
  1455. */
  1456. int usb4_port_margining_caps(struct tb_port *port, enum usb4_sb_target target,
  1457. u8 index, u32 *caps, size_t ncaps)
  1458. {
  1459. int ret;
  1460. ret = usb4_port_sb_op(port, target, index,
  1461. USB4_SB_OPCODE_READ_LANE_MARGINING_CAP, 500);
  1462. if (ret)
  1463. return ret;
  1464. return usb4_port_sb_read(port, target, index, USB4_SB_DATA, caps,
  1465. sizeof(*caps) * ncaps);
  1466. }
  1467. /**
  1468. * usb4_port_hw_margin() - Run hardware lane margining on port
  1469. * @port: USB4 port
  1470. * @target: Sideband target
  1471. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1472. * @params: Parameters for USB4 hardware margining
  1473. * @results: Array to hold the results
  1474. * @nresults: Number of elements in the results array
  1475. *
  1476. * Runs hardware lane margining on USB4 port and returns the result in
  1477. * @results.
  1478. *
  1479. * Return: %0 on success, negative errno otherwise.
  1480. */
  1481. int usb4_port_hw_margin(struct tb_port *port, enum usb4_sb_target target,
  1482. u8 index, const struct usb4_port_margining_params *params,
  1483. u32 *results, size_t nresults)
  1484. {
  1485. u32 val;
  1486. int ret;
  1487. if (WARN_ON_ONCE(!params))
  1488. return -EINVAL;
  1489. val = params->lanes;
  1490. if (params->time)
  1491. val |= USB4_MARGIN_HW_TIME;
  1492. if (params->right_high || params->upper_eye)
  1493. val |= USB4_MARGIN_HW_RHU;
  1494. if (params->ber_level)
  1495. val |= FIELD_PREP(USB4_MARGIN_HW_BER_MASK, params->ber_level);
  1496. if (params->optional_voltage_offset_range)
  1497. val |= USB4_MARGIN_HW_OPT_VOLTAGE;
  1498. ret = usb4_port_sb_write(port, target, index, USB4_SB_METADATA, &val,
  1499. sizeof(val));
  1500. if (ret)
  1501. return ret;
  1502. ret = usb4_port_sb_op(port, target, index,
  1503. USB4_SB_OPCODE_RUN_HW_LANE_MARGINING, 2500);
  1504. if (ret)
  1505. return ret;
  1506. return usb4_port_sb_read(port, target, index, USB4_SB_DATA, results,
  1507. sizeof(*results) * nresults);
  1508. }
  1509. /**
  1510. * usb4_port_sw_margin() - Run software lane margining on port
  1511. * @port: USB4 port
  1512. * @target: Sideband target
  1513. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1514. * @params: Parameters for USB4 software margining
  1515. * @results: Data word for the operation completion data
  1516. *
  1517. * Runs software lane margining on USB4 port. Read back the error
  1518. * counters by calling usb4_port_sw_margin_errors().
  1519. *
  1520. * Return: %0 on success, negative errno otherwise.
  1521. */
  1522. int usb4_port_sw_margin(struct tb_port *port, enum usb4_sb_target target,
  1523. u8 index, const struct usb4_port_margining_params *params,
  1524. u32 *results)
  1525. {
  1526. u32 val;
  1527. int ret;
  1528. if (WARN_ON_ONCE(!params))
  1529. return -EINVAL;
  1530. val = params->lanes;
  1531. if (params->time)
  1532. val |= USB4_MARGIN_SW_TIME;
  1533. if (params->optional_voltage_offset_range)
  1534. val |= USB4_MARGIN_SW_OPT_VOLTAGE;
  1535. if (params->right_high)
  1536. val |= USB4_MARGIN_SW_RH;
  1537. if (params->upper_eye)
  1538. val |= USB4_MARGIN_SW_UPPER_EYE;
  1539. val |= FIELD_PREP(USB4_MARGIN_SW_COUNTER_MASK, params->error_counter);
  1540. val |= FIELD_PREP(USB4_MARGIN_SW_VT_MASK, params->voltage_time_offset);
  1541. ret = usb4_port_sb_write(port, target, index, USB4_SB_METADATA, &val,
  1542. sizeof(val));
  1543. if (ret)
  1544. return ret;
  1545. ret = usb4_port_sb_op(port, target, index,
  1546. USB4_SB_OPCODE_RUN_SW_LANE_MARGINING, 2500);
  1547. if (ret)
  1548. return ret;
  1549. return usb4_port_sb_read(port, target, index, USB4_SB_DATA, results,
  1550. sizeof(*results));
  1551. }
  1552. /**
  1553. * usb4_port_sw_margin_errors() - Read the software margining error counters
  1554. * @port: USB4 port
  1555. * @target: Sideband target
  1556. * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER
  1557. * @errors: Error metadata is copied here.
  1558. *
  1559. * This reads back the software margining error counters from the port.
  1560. *
  1561. * Return: %0 on success, negative errno otherwise.
  1562. */
  1563. int usb4_port_sw_margin_errors(struct tb_port *port, enum usb4_sb_target target,
  1564. u8 index, u32 *errors)
  1565. {
  1566. int ret;
  1567. ret = usb4_port_sb_op(port, target, index,
  1568. USB4_SB_OPCODE_READ_SW_MARGIN_ERR, 150);
  1569. if (ret)
  1570. return ret;
  1571. return usb4_port_sb_read(port, target, index, USB4_SB_METADATA, errors,
  1572. sizeof(*errors));
  1573. }
  1574. static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
  1575. enum usb4_sb_opcode opcode,
  1576. int timeout_msec)
  1577. {
  1578. return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
  1579. timeout_msec);
  1580. }
  1581. /**
  1582. * usb4_port_retimer_set_inbound_sbtx() - Enable sideband channel transactions
  1583. * @port: USB4 port
  1584. * @index: Retimer index
  1585. *
  1586. * Enables sideband channel transactions on SBTX. Can be used when USB4
  1587. * link does not go up, for example if there is no device connected.
  1588. *
  1589. * Return: %0 on success, negative errno otherwise.
  1590. */
  1591. int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index)
  1592. {
  1593. int ret;
  1594. ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
  1595. 500);
  1596. if (ret != -ENODEV)
  1597. return ret;
  1598. /*
  1599. * Per the USB4 retimer spec, the retimer is not required to
  1600. * send an RT (Retimer Transaction) response for the first
  1601. * SET_INBOUND_SBTX command
  1602. */
  1603. return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
  1604. 500);
  1605. }
  1606. /**
  1607. * usb4_port_retimer_unset_inbound_sbtx() - Disable sideband channel transactions
  1608. * @port: USB4 port
  1609. * @index: Retimer index
  1610. *
  1611. * Disables sideband channel transactions on SBTX. The reverse of
  1612. * usb4_port_retimer_set_inbound_sbtx().
  1613. *
  1614. * Return: %0 on success, negative errno otherwise.
  1615. */
  1616. int usb4_port_retimer_unset_inbound_sbtx(struct tb_port *port, u8 index)
  1617. {
  1618. return usb4_port_retimer_op(port, index,
  1619. USB4_SB_OPCODE_UNSET_INBOUND_SBTX, 500);
  1620. }
  1621. /**
  1622. * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
  1623. * @port: USB4 port
  1624. * @index: Retimer index
  1625. *
  1626. * Return:
  1627. * * %1 - Retimer at @index is the last one (connected directly to the
  1628. * Type-C port).
  1629. * * %0 - Retimer at @index is not the last one.
  1630. * * %-ENODEV - Retimer is not present.
  1631. * * Negative errno - Other failure occurred.
  1632. */
  1633. int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
  1634. {
  1635. u32 metadata;
  1636. int ret;
  1637. ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
  1638. 500);
  1639. if (ret)
  1640. return ret;
  1641. ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1642. USB4_SB_METADATA, &metadata, sizeof(metadata));
  1643. return ret ? ret : metadata & 1;
  1644. }
  1645. /**
  1646. * usb4_port_retimer_is_cable() - Is the retimer cable retimer
  1647. * @port: USB4 port
  1648. * @index: Retimer index
  1649. *
  1650. * Return:
  1651. * * %1 - Retimer at @index is the last cable retimer.
  1652. * * %0 - Retimer at @index is on-board retimer.
  1653. * * %-ENODEV - Retimer is not present.
  1654. * * Negative errno - Other failure occurred.
  1655. */
  1656. int usb4_port_retimer_is_cable(struct tb_port *port, u8 index)
  1657. {
  1658. u32 metadata;
  1659. int ret;
  1660. ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_CABLE_RETIMER,
  1661. 500);
  1662. if (ret)
  1663. return ret;
  1664. ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1665. USB4_SB_METADATA, &metadata, sizeof(metadata));
  1666. return ret ? ret : metadata & 1;
  1667. }
  1668. /**
  1669. * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
  1670. * @port: USB4 port
  1671. * @index: Retimer index
  1672. *
  1673. * Reads NVM sector size (in bytes) of a retimer at @index. This
  1674. * operation can be used to determine whether the retimer supports NVM
  1675. * upgrade for example.
  1676. *
  1677. * Return:
  1678. * * Sector size in bytes.
  1679. * * %-ENODEV - If there is no retimer at @index.
  1680. * * Negative errno - In case of an error.
  1681. */
  1682. int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
  1683. {
  1684. u32 metadata;
  1685. int ret;
  1686. ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
  1687. 500);
  1688. if (ret)
  1689. return ret;
  1690. ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1691. USB4_SB_METADATA, &metadata, sizeof(metadata));
  1692. return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
  1693. }
  1694. /**
  1695. * usb4_port_retimer_nvm_set_offset() - Set NVM write offset
  1696. * @port: USB4 port
  1697. * @index: Retimer index
  1698. * @address: Start offset
  1699. *
  1700. * Explicitly sets NVM write offset. Normally when writing to NVM this is
  1701. * done automatically by usb4_port_retimer_nvm_write().
  1702. *
  1703. * Return: %0 on success, negative errno otherwise.
  1704. */
  1705. int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
  1706. unsigned int address)
  1707. {
  1708. u32 metadata, dwaddress;
  1709. int ret;
  1710. dwaddress = address / 4;
  1711. metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
  1712. USB4_NVM_SET_OFFSET_MASK;
  1713. ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
  1714. USB4_SB_METADATA, &metadata, sizeof(metadata));
  1715. if (ret)
  1716. return ret;
  1717. return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
  1718. 500);
  1719. }
  1720. struct retimer_info {
  1721. struct tb_port *port;
  1722. u8 index;
  1723. };
  1724. static int usb4_port_retimer_nvm_write_next_block(void *data,
  1725. unsigned int dwaddress, const void *buf, size_t dwords)
  1726. {
  1727. const struct retimer_info *info = data;
  1728. struct tb_port *port = info->port;
  1729. u8 index = info->index;
  1730. int ret;
  1731. ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
  1732. USB4_SB_DATA, buf, dwords * 4);
  1733. if (ret)
  1734. return ret;
  1735. return usb4_port_retimer_op(port, index,
  1736. USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
  1737. }
  1738. /**
  1739. * usb4_port_retimer_nvm_write() - Write to retimer NVM
  1740. * @port: USB4 port
  1741. * @index: Retimer index
  1742. * @address: Byte address where to start the write
  1743. * @buf: Data to write
  1744. * @size: Size in bytes how much to write
  1745. *
  1746. * Writes @size bytes from @buf to the retimer NVM. Used for NVM
  1747. * upgrade.
  1748. *
  1749. * Return:
  1750. * * %0 - If the data was written successfully.
  1751. * * %-ENODEV - If there is no retimer at @index.
  1752. * * Negative errno - In case of an error.
  1753. */
  1754. int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
  1755. const void *buf, size_t size)
  1756. {
  1757. struct retimer_info info = { .port = port, .index = index };
  1758. int ret;
  1759. ret = usb4_port_retimer_nvm_set_offset(port, index, address);
  1760. if (ret)
  1761. return ret;
  1762. return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
  1763. usb4_port_retimer_nvm_write_next_block, &info);
  1764. }
  1765. /**
  1766. * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
  1767. * @port: USB4 port
  1768. * @index: Retimer index
  1769. *
  1770. * After the new NVM image has been written via usb4_port_retimer_nvm_write()
  1771. * this function can be used to trigger the NVM upgrade process. If
  1772. * successful the retimer restarts with the new NVM and may not have the
  1773. * index set so one needs to call usb4_port_enumerate_retimers() to
  1774. * force index to be assigned.
  1775. *
  1776. * Return: %0 on success, negative errno otherwise.
  1777. */
  1778. int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
  1779. {
  1780. u32 val;
  1781. /*
  1782. * We need to use the raw operation here because once the
  1783. * authentication completes the retimer index is not set anymore
  1784. * so we do not get back the status now.
  1785. */
  1786. val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
  1787. return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
  1788. USB4_SB_OPCODE, &val, sizeof(val));
  1789. }
  1790. /**
  1791. * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
  1792. * @port: USB4 port
  1793. * @index: Retimer index
  1794. * @status: Raw status code read from metadata
  1795. *
  1796. * This can be called after usb4_port_retimer_nvm_authenticate() and
  1797. * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
  1798. *
  1799. * Return: %0 if the authentication status was successfully read. The
  1800. * completion metadata (the result) is then stored into @status. If
  1801. * status read fails, returns negative errno.
  1802. */
  1803. int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
  1804. u32 *status)
  1805. {
  1806. u32 metadata, val;
  1807. int ret;
  1808. ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1809. USB4_SB_OPCODE, &val, sizeof(val));
  1810. if (ret)
  1811. return ret;
  1812. ret = usb4_port_sb_opcode_err_to_errno(val);
  1813. switch (ret) {
  1814. case 0:
  1815. *status = 0;
  1816. return 0;
  1817. case -EAGAIN:
  1818. ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1819. USB4_SB_METADATA, &metadata,
  1820. sizeof(metadata));
  1821. if (ret)
  1822. return ret;
  1823. *status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
  1824. return 0;
  1825. default:
  1826. return ret;
  1827. }
  1828. }
  1829. static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
  1830. void *buf, size_t dwords)
  1831. {
  1832. const struct retimer_info *info = data;
  1833. struct tb_port *port = info->port;
  1834. u8 index = info->index;
  1835. u32 metadata;
  1836. int ret;
  1837. metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
  1838. if (dwords < USB4_DATA_DWORDS)
  1839. metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
  1840. ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
  1841. USB4_SB_METADATA, &metadata, sizeof(metadata));
  1842. if (ret)
  1843. return ret;
  1844. ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
  1845. if (ret)
  1846. return ret;
  1847. return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
  1848. USB4_SB_DATA, buf, dwords * 4);
  1849. }
  1850. /**
  1851. * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
  1852. * @port: USB4 port
  1853. * @index: Retimer index
  1854. * @address: NVM address (in bytes) to start reading
  1855. * @buf: Data read from NVM is stored here
  1856. * @size: Number of bytes to read
  1857. *
  1858. * Reads retimer NVM and copies the contents to @buf.
  1859. *
  1860. * Return:
  1861. * * %0 - If the read was successful.
  1862. * * %-ENODEV - If there is no retimer at @index.
  1863. * * Negative errno - In case of an error.
  1864. */
  1865. int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
  1866. unsigned int address, void *buf, size_t size)
  1867. {
  1868. struct retimer_info info = { .port = port, .index = index };
  1869. return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
  1870. usb4_port_retimer_nvm_read_block, &info);
  1871. }
  1872. static inline unsigned int
  1873. usb4_usb3_port_max_bandwidth(const struct tb_port *port, unsigned int bw)
  1874. {
  1875. /* Take the possible bandwidth limitation into account */
  1876. if (port->max_bw)
  1877. return min(bw, port->max_bw);
  1878. return bw;
  1879. }
  1880. /**
  1881. * usb4_usb3_port_max_link_rate() - Maximum supported USB3 link rate
  1882. * @port: USB3 adapter port
  1883. *
  1884. * Return: Maximum supported link rate of a USB3 adapter in Mb/s.
  1885. * Negative errno in case of an error.
  1886. */
  1887. int usb4_usb3_port_max_link_rate(struct tb_port *port)
  1888. {
  1889. int ret, lr;
  1890. u32 val;
  1891. if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
  1892. return -EINVAL;
  1893. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1894. port->cap_adap + ADP_USB3_CS_4, 1);
  1895. if (ret)
  1896. return ret;
  1897. lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
  1898. ret = lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
  1899. return usb4_usb3_port_max_bandwidth(port, ret);
  1900. }
  1901. static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
  1902. {
  1903. int ret;
  1904. u32 val;
  1905. if (!tb_port_is_usb3_down(port))
  1906. return -EINVAL;
  1907. if (tb_route(port->sw))
  1908. return -EINVAL;
  1909. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1910. port->cap_adap + ADP_USB3_CS_2, 1);
  1911. if (ret)
  1912. return ret;
  1913. if (request)
  1914. val |= ADP_USB3_CS_2_CMR;
  1915. else
  1916. val &= ~ADP_USB3_CS_2_CMR;
  1917. ret = tb_port_write(port, &val, TB_CFG_PORT,
  1918. port->cap_adap + ADP_USB3_CS_2, 1);
  1919. if (ret)
  1920. return ret;
  1921. /*
  1922. * We can use val here directly as the CMR bit is in the same place
  1923. * as HCA. Just mask out others.
  1924. */
  1925. val &= ADP_USB3_CS_2_CMR;
  1926. return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
  1927. ADP_USB3_CS_1_HCA, val, 1500,
  1928. USB4_PORT_DELAY);
  1929. }
  1930. static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
  1931. {
  1932. return usb4_usb3_port_cm_request(port, true);
  1933. }
  1934. static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
  1935. {
  1936. return usb4_usb3_port_cm_request(port, false);
  1937. }
  1938. static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
  1939. {
  1940. unsigned long uframes;
  1941. uframes = bw * 512UL << scale;
  1942. return DIV_ROUND_CLOSEST(uframes * 8000, MEGA);
  1943. }
  1944. static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
  1945. {
  1946. unsigned long uframes;
  1947. /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
  1948. uframes = ((unsigned long)mbps * MEGA) / 8000;
  1949. return DIV_ROUND_UP(uframes, 512UL << scale);
  1950. }
  1951. static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
  1952. int *upstream_bw,
  1953. int *downstream_bw)
  1954. {
  1955. u32 val, bw, scale;
  1956. int ret;
  1957. ret = tb_port_read(port, &val, TB_CFG_PORT,
  1958. port->cap_adap + ADP_USB3_CS_2, 1);
  1959. if (ret)
  1960. return ret;
  1961. ret = tb_port_read(port, &scale, TB_CFG_PORT,
  1962. port->cap_adap + ADP_USB3_CS_3, 1);
  1963. if (ret)
  1964. return ret;
  1965. scale &= ADP_USB3_CS_3_SCALE_MASK;
  1966. bw = val & ADP_USB3_CS_2_AUBW_MASK;
  1967. *upstream_bw = usb3_bw_to_mbps(bw, scale);
  1968. bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
  1969. *downstream_bw = usb3_bw_to_mbps(bw, scale);
  1970. return 0;
  1971. }
  1972. /**
  1973. * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
  1974. * @port: USB3 adapter port
  1975. * @upstream_bw: Allocated upstream bandwidth is stored here
  1976. * @downstream_bw: Allocated downstream bandwidth is stored here
  1977. *
  1978. * Stores currently allocated USB3 bandwidth into @upstream_bw and
  1979. * @downstream_bw in Mb/s.
  1980. *
  1981. * Return: %0 on success, negative errno otherwise.
  1982. */
  1983. int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
  1984. int *downstream_bw)
  1985. {
  1986. int ret;
  1987. ret = usb4_usb3_port_set_cm_request(port);
  1988. if (ret)
  1989. return ret;
  1990. ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
  1991. downstream_bw);
  1992. usb4_usb3_port_clear_cm_request(port);
  1993. return ret;
  1994. }
  1995. static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
  1996. int *upstream_bw,
  1997. int *downstream_bw)
  1998. {
  1999. u32 val, bw, scale;
  2000. int ret;
  2001. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2002. port->cap_adap + ADP_USB3_CS_1, 1);
  2003. if (ret)
  2004. return ret;
  2005. ret = tb_port_read(port, &scale, TB_CFG_PORT,
  2006. port->cap_adap + ADP_USB3_CS_3, 1);
  2007. if (ret)
  2008. return ret;
  2009. scale &= ADP_USB3_CS_3_SCALE_MASK;
  2010. bw = val & ADP_USB3_CS_1_CUBW_MASK;
  2011. *upstream_bw = usb3_bw_to_mbps(bw, scale);
  2012. bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
  2013. *downstream_bw = usb3_bw_to_mbps(bw, scale);
  2014. return 0;
  2015. }
  2016. static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
  2017. int upstream_bw,
  2018. int downstream_bw)
  2019. {
  2020. u32 val, ubw, dbw, scale;
  2021. int ret, max_bw;
  2022. /* Figure out suitable scale */
  2023. scale = 0;
  2024. max_bw = max(upstream_bw, downstream_bw);
  2025. while (scale < 64) {
  2026. if (mbps_to_usb3_bw(max_bw, scale) < 4096)
  2027. break;
  2028. scale++;
  2029. }
  2030. if (WARN_ON(scale >= 64))
  2031. return -EINVAL;
  2032. ret = tb_port_write(port, &scale, TB_CFG_PORT,
  2033. port->cap_adap + ADP_USB3_CS_3, 1);
  2034. if (ret)
  2035. return ret;
  2036. ubw = mbps_to_usb3_bw(upstream_bw, scale);
  2037. dbw = mbps_to_usb3_bw(downstream_bw, scale);
  2038. tb_port_dbg(port, "scaled bandwidth %u/%u, scale %u\n", ubw, dbw, scale);
  2039. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2040. port->cap_adap + ADP_USB3_CS_2, 1);
  2041. if (ret)
  2042. return ret;
  2043. val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
  2044. val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
  2045. val |= ubw;
  2046. return tb_port_write(port, &val, TB_CFG_PORT,
  2047. port->cap_adap + ADP_USB3_CS_2, 1);
  2048. }
  2049. /**
  2050. * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
  2051. * @port: USB3 adapter port
  2052. * @upstream_bw: New upstream bandwidth
  2053. * @downstream_bw: New downstream bandwidth
  2054. *
  2055. * This can be used to set how much bandwidth is allocated for the USB3
  2056. * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
  2057. * new values programmed to the USB3 adapter allocation registers. If
  2058. * the values are lower than what is currently consumed the allocation
  2059. * is set to what is currently consumed instead (consumed bandwidth
  2060. * cannot be taken away by CM). The actual new values are returned in
  2061. * @upstream_bw and @downstream_bw.
  2062. *
  2063. * Return: %0 on success, negative errno otherwise.
  2064. */
  2065. int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
  2066. int *downstream_bw)
  2067. {
  2068. int ret, consumed_up, consumed_down, allocate_up, allocate_down;
  2069. ret = usb4_usb3_port_set_cm_request(port);
  2070. if (ret)
  2071. return ret;
  2072. ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
  2073. &consumed_down);
  2074. if (ret)
  2075. goto err_request;
  2076. /* Don't allow it go lower than what is consumed */
  2077. allocate_up = max(*upstream_bw, consumed_up);
  2078. allocate_down = max(*downstream_bw, consumed_down);
  2079. ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
  2080. allocate_down);
  2081. if (ret)
  2082. goto err_request;
  2083. *upstream_bw = allocate_up;
  2084. *downstream_bw = allocate_down;
  2085. err_request:
  2086. usb4_usb3_port_clear_cm_request(port);
  2087. return ret;
  2088. }
  2089. /**
  2090. * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
  2091. * @port: USB3 adapter port
  2092. * @upstream_bw: New allocated upstream bandwidth
  2093. * @downstream_bw: New allocated downstream bandwidth
  2094. *
  2095. * Releases USB3 allocated bandwidth down to what is actually consumed.
  2096. * The new bandwidth is returned in @upstream_bw and @downstream_bw.
  2097. *
  2098. * Return: %0 on success, negative errno otherwise.
  2099. */
  2100. int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
  2101. int *downstream_bw)
  2102. {
  2103. int ret, consumed_up, consumed_down;
  2104. ret = usb4_usb3_port_set_cm_request(port);
  2105. if (ret)
  2106. return ret;
  2107. ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
  2108. &consumed_down);
  2109. if (ret)
  2110. goto err_request;
  2111. /*
  2112. * Always keep 900 Mb/s to make sure xHCI has at least some
  2113. * bandwidth available for isochronous traffic.
  2114. */
  2115. if (consumed_up < 900)
  2116. consumed_up = 900;
  2117. if (consumed_down < 900)
  2118. consumed_down = 900;
  2119. ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
  2120. consumed_down);
  2121. if (ret)
  2122. goto err_request;
  2123. *upstream_bw = consumed_up;
  2124. *downstream_bw = consumed_down;
  2125. err_request:
  2126. usb4_usb3_port_clear_cm_request(port);
  2127. return ret;
  2128. }
  2129. static bool is_usb4_dpin(const struct tb_port *port)
  2130. {
  2131. if (!tb_port_is_dpin(port))
  2132. return false;
  2133. if (!tb_switch_is_usb4(port->sw))
  2134. return false;
  2135. return true;
  2136. }
  2137. /**
  2138. * usb4_dp_port_set_cm_id() - Assign CM ID to the DP IN adapter
  2139. * @port: DP IN adapter
  2140. * @cm_id: CM ID to assign
  2141. *
  2142. * Sets CM ID for the @port.
  2143. *
  2144. * Return:
  2145. * * %0 - On success.
  2146. * * %-EOPNOTSUPP - If the @port does not support this.
  2147. * * Negative errno - Another error occurred.
  2148. */
  2149. int usb4_dp_port_set_cm_id(struct tb_port *port, int cm_id)
  2150. {
  2151. u32 val;
  2152. int ret;
  2153. if (!is_usb4_dpin(port))
  2154. return -EOPNOTSUPP;
  2155. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2156. port->cap_adap + ADP_DP_CS_2, 1);
  2157. if (ret)
  2158. return ret;
  2159. val &= ~ADP_DP_CS_2_CM_ID_MASK;
  2160. val |= cm_id << ADP_DP_CS_2_CM_ID_SHIFT;
  2161. return tb_port_write(port, &val, TB_CFG_PORT,
  2162. port->cap_adap + ADP_DP_CS_2, 1);
  2163. }
  2164. /**
  2165. * usb4_dp_port_bandwidth_mode_supported() - Is the bandwidth allocation mode
  2166. * supported
  2167. * @port: DP IN adapter to check
  2168. *
  2169. * Can be called to any DP IN adapter.
  2170. *
  2171. * Return: %true if the adapter supports USB4 bandwidth allocation mode,
  2172. * %false otherwise.
  2173. */
  2174. bool usb4_dp_port_bandwidth_mode_supported(struct tb_port *port)
  2175. {
  2176. int ret;
  2177. u32 val;
  2178. if (!is_usb4_dpin(port))
  2179. return false;
  2180. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2181. port->cap_adap + DP_LOCAL_CAP, 1);
  2182. if (ret)
  2183. return false;
  2184. return !!(val & DP_COMMON_CAP_BW_MODE);
  2185. }
  2186. /**
  2187. * usb4_dp_port_bandwidth_mode_enabled() - Is the bandwidth allocation mode
  2188. * enabled
  2189. * @port: DP IN adapter to check
  2190. *
  2191. * Can be called to any DP IN adapter.
  2192. *
  2193. * Return: %true if the bandwidth allocation mode has been enabled,
  2194. * %false otherwise.
  2195. */
  2196. bool usb4_dp_port_bandwidth_mode_enabled(struct tb_port *port)
  2197. {
  2198. int ret;
  2199. u32 val;
  2200. if (!is_usb4_dpin(port))
  2201. return false;
  2202. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2203. port->cap_adap + ADP_DP_CS_8, 1);
  2204. if (ret)
  2205. return false;
  2206. return !!(val & ADP_DP_CS_8_DPME);
  2207. }
  2208. /**
  2209. * usb4_dp_port_set_cm_bandwidth_mode_supported() - Set/clear CM support for
  2210. * bandwidth allocation mode
  2211. * @port: DP IN adapter
  2212. * @supported: Does the CM support bandwidth allocation mode
  2213. *
  2214. * Can be called to any DP IN adapter. Sets or clears the CM support bit
  2215. * of the DP IN adapter.
  2216. *
  2217. * * Return:
  2218. * * %0 - On success.
  2219. * * %-EOPNOTSUPP - If the passed IN adapter does not support this.
  2220. * * Negative errno - Another error occurred.
  2221. */
  2222. int usb4_dp_port_set_cm_bandwidth_mode_supported(struct tb_port *port,
  2223. bool supported)
  2224. {
  2225. u32 val;
  2226. int ret;
  2227. if (!is_usb4_dpin(port))
  2228. return -EOPNOTSUPP;
  2229. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2230. port->cap_adap + ADP_DP_CS_2, 1);
  2231. if (ret)
  2232. return ret;
  2233. if (supported)
  2234. val |= ADP_DP_CS_2_CMMS;
  2235. else
  2236. val &= ~ADP_DP_CS_2_CMMS;
  2237. return tb_port_write(port, &val, TB_CFG_PORT,
  2238. port->cap_adap + ADP_DP_CS_2, 1);
  2239. }
  2240. /**
  2241. * usb4_dp_port_group_id() - Return Group ID assigned for the adapter
  2242. * @port: DP IN adapter
  2243. *
  2244. * Reads bandwidth allocation Group ID from the DP IN adapter and
  2245. * returns it.
  2246. *
  2247. * Return:
  2248. * * Group ID assigned to adapter @port.
  2249. * * %-EOPNOTSUPP - If adapter does not support setting GROUP_ID.
  2250. * * Negative errno - Another error occurred.
  2251. */
  2252. int usb4_dp_port_group_id(struct tb_port *port)
  2253. {
  2254. u32 val;
  2255. int ret;
  2256. if (!is_usb4_dpin(port))
  2257. return -EOPNOTSUPP;
  2258. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2259. port->cap_adap + ADP_DP_CS_2, 1);
  2260. if (ret)
  2261. return ret;
  2262. return (val & ADP_DP_CS_2_GROUP_ID_MASK) >> ADP_DP_CS_2_GROUP_ID_SHIFT;
  2263. }
  2264. /**
  2265. * usb4_dp_port_set_group_id() - Set adapter Group ID
  2266. * @port: DP IN adapter
  2267. * @group_id: Group ID for the adapter
  2268. *
  2269. * Sets bandwidth allocation mode Group ID for the DP IN adapter.
  2270. *
  2271. * Return:
  2272. * * %0 - On success.
  2273. * * %-EOPNOTSUPP - If the adapter does not support this.
  2274. * * Negative errno - Another error occurred.
  2275. */
  2276. int usb4_dp_port_set_group_id(struct tb_port *port, int group_id)
  2277. {
  2278. u32 val;
  2279. int ret;
  2280. if (!is_usb4_dpin(port))
  2281. return -EOPNOTSUPP;
  2282. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2283. port->cap_adap + ADP_DP_CS_2, 1);
  2284. if (ret)
  2285. return ret;
  2286. val &= ~ADP_DP_CS_2_GROUP_ID_MASK;
  2287. val |= group_id << ADP_DP_CS_2_GROUP_ID_SHIFT;
  2288. return tb_port_write(port, &val, TB_CFG_PORT,
  2289. port->cap_adap + ADP_DP_CS_2, 1);
  2290. }
  2291. /**
  2292. * usb4_dp_port_nrd() - Read non-reduced rate and lanes
  2293. * @port: DP IN adapter
  2294. * @rate: Non-reduced rate in Mb/s is placed here
  2295. * @lanes: Non-reduced lanes are placed here
  2296. *
  2297. * Reads the non-reduced rate and lanes from the DP IN adapter.
  2298. *
  2299. * Return:
  2300. * * %0 - On success.
  2301. * * %-EOPNOTSUPP - If the adapter does not support this.
  2302. * * Negative errno - Another error occurred.
  2303. */
  2304. int usb4_dp_port_nrd(struct tb_port *port, int *rate, int *lanes)
  2305. {
  2306. u32 val, tmp;
  2307. int ret;
  2308. if (!is_usb4_dpin(port))
  2309. return -EOPNOTSUPP;
  2310. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2311. port->cap_adap + ADP_DP_CS_2, 1);
  2312. if (ret)
  2313. return ret;
  2314. tmp = (val & ADP_DP_CS_2_NRD_MLR_MASK) >> ADP_DP_CS_2_NRD_MLR_SHIFT;
  2315. switch (tmp) {
  2316. case DP_COMMON_CAP_RATE_RBR:
  2317. *rate = 1620;
  2318. break;
  2319. case DP_COMMON_CAP_RATE_HBR:
  2320. *rate = 2700;
  2321. break;
  2322. case DP_COMMON_CAP_RATE_HBR2:
  2323. *rate = 5400;
  2324. break;
  2325. case DP_COMMON_CAP_RATE_HBR3:
  2326. *rate = 8100;
  2327. break;
  2328. }
  2329. tmp = val & ADP_DP_CS_2_NRD_MLC_MASK;
  2330. switch (tmp) {
  2331. case DP_COMMON_CAP_1_LANE:
  2332. *lanes = 1;
  2333. break;
  2334. case DP_COMMON_CAP_2_LANES:
  2335. *lanes = 2;
  2336. break;
  2337. case DP_COMMON_CAP_4_LANES:
  2338. *lanes = 4;
  2339. break;
  2340. }
  2341. return 0;
  2342. }
  2343. /**
  2344. * usb4_dp_port_set_nrd() - Set non-reduced rate and lanes
  2345. * @port: DP IN adapter
  2346. * @rate: Non-reduced rate in Mb/s
  2347. * @lanes: Non-reduced lanes
  2348. *
  2349. * Before the capabilities reduction, this function can be used to set
  2350. * the non-reduced values for the DP IN adapter.
  2351. *
  2352. * Return:
  2353. * * %0 - On success.
  2354. * * %-EOPNOTSUPP - If the adapter does not support this.
  2355. * * Negative errno - Another error occurred.
  2356. */
  2357. int usb4_dp_port_set_nrd(struct tb_port *port, int rate, int lanes)
  2358. {
  2359. u32 val;
  2360. int ret;
  2361. if (!is_usb4_dpin(port))
  2362. return -EOPNOTSUPP;
  2363. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2364. port->cap_adap + ADP_DP_CS_2, 1);
  2365. if (ret)
  2366. return ret;
  2367. val &= ~ADP_DP_CS_2_NRD_MLR_MASK;
  2368. switch (rate) {
  2369. case 1620:
  2370. break;
  2371. case 2700:
  2372. val |= (DP_COMMON_CAP_RATE_HBR << ADP_DP_CS_2_NRD_MLR_SHIFT)
  2373. & ADP_DP_CS_2_NRD_MLR_MASK;
  2374. break;
  2375. case 5400:
  2376. val |= (DP_COMMON_CAP_RATE_HBR2 << ADP_DP_CS_2_NRD_MLR_SHIFT)
  2377. & ADP_DP_CS_2_NRD_MLR_MASK;
  2378. break;
  2379. case 8100:
  2380. val |= (DP_COMMON_CAP_RATE_HBR3 << ADP_DP_CS_2_NRD_MLR_SHIFT)
  2381. & ADP_DP_CS_2_NRD_MLR_MASK;
  2382. break;
  2383. default:
  2384. return -EINVAL;
  2385. }
  2386. val &= ~ADP_DP_CS_2_NRD_MLC_MASK;
  2387. switch (lanes) {
  2388. case 1:
  2389. break;
  2390. case 2:
  2391. val |= DP_COMMON_CAP_2_LANES;
  2392. break;
  2393. case 4:
  2394. val |= DP_COMMON_CAP_4_LANES;
  2395. break;
  2396. default:
  2397. return -EINVAL;
  2398. }
  2399. return tb_port_write(port, &val, TB_CFG_PORT,
  2400. port->cap_adap + ADP_DP_CS_2, 1);
  2401. }
  2402. /**
  2403. * usb4_dp_port_granularity() - Return granularity for the bandwidth values
  2404. * @port: DP IN adapter
  2405. *
  2406. * Reads the programmed granularity from @port.
  2407. *
  2408. * Return:
  2409. * * Granularity value of a @port.
  2410. * * %-EOPNOTSUPP - If the DP IN adapter does not support bandwidth
  2411. * allocation mode.
  2412. * * Negative errno - Another error occurred.
  2413. */
  2414. int usb4_dp_port_granularity(struct tb_port *port)
  2415. {
  2416. u32 val;
  2417. int ret;
  2418. if (!is_usb4_dpin(port))
  2419. return -EOPNOTSUPP;
  2420. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2421. port->cap_adap + ADP_DP_CS_2, 1);
  2422. if (ret)
  2423. return ret;
  2424. val &= ADP_DP_CS_2_GR_MASK;
  2425. val >>= ADP_DP_CS_2_GR_SHIFT;
  2426. switch (val) {
  2427. case ADP_DP_CS_2_GR_0_25G:
  2428. return 250;
  2429. case ADP_DP_CS_2_GR_0_5G:
  2430. return 500;
  2431. case ADP_DP_CS_2_GR_1G:
  2432. return 1000;
  2433. }
  2434. return -EINVAL;
  2435. }
  2436. /**
  2437. * usb4_dp_port_set_granularity() - Set granularity for the bandwidth values
  2438. * @port: DP IN adapter
  2439. * @granularity: Granularity in Mb/s. Supported values: 1000, 500 and 250.
  2440. *
  2441. * Sets the granularity used with the estimated, allocated and requested
  2442. * bandwidth.
  2443. *
  2444. * Return:
  2445. * * %0 - On success.
  2446. * * %-EOPNOTSUPP - If the adapter does not support this.
  2447. * * Negative errno - Another error occurred.
  2448. */
  2449. int usb4_dp_port_set_granularity(struct tb_port *port, int granularity)
  2450. {
  2451. u32 val;
  2452. int ret;
  2453. if (!is_usb4_dpin(port))
  2454. return -EOPNOTSUPP;
  2455. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2456. port->cap_adap + ADP_DP_CS_2, 1);
  2457. if (ret)
  2458. return ret;
  2459. val &= ~ADP_DP_CS_2_GR_MASK;
  2460. switch (granularity) {
  2461. case 250:
  2462. val |= ADP_DP_CS_2_GR_0_25G << ADP_DP_CS_2_GR_SHIFT;
  2463. break;
  2464. case 500:
  2465. val |= ADP_DP_CS_2_GR_0_5G << ADP_DP_CS_2_GR_SHIFT;
  2466. break;
  2467. case 1000:
  2468. val |= ADP_DP_CS_2_GR_1G << ADP_DP_CS_2_GR_SHIFT;
  2469. break;
  2470. default:
  2471. return -EINVAL;
  2472. }
  2473. return tb_port_write(port, &val, TB_CFG_PORT,
  2474. port->cap_adap + ADP_DP_CS_2, 1);
  2475. }
  2476. /**
  2477. * usb4_dp_port_set_estimated_bandwidth() - Set estimated bandwidth
  2478. * @port: DP IN adapter
  2479. * @bw: Estimated bandwidth in Mb/s.
  2480. *
  2481. * Sets the estimated bandwidth to @bw. Set the granularity by calling
  2482. * usb4_dp_port_set_granularity() before calling this. The @bw is rounded
  2483. * down to the closest granularity multiplier.
  2484. *
  2485. * Return:
  2486. * * %0 - On success.
  2487. * * %-EOPNOTSUPP - If the adapter does not support this.
  2488. * * Negative errno - Another error occurred.
  2489. */
  2490. int usb4_dp_port_set_estimated_bandwidth(struct tb_port *port, int bw)
  2491. {
  2492. u32 val, granularity;
  2493. int ret;
  2494. if (!is_usb4_dpin(port))
  2495. return -EOPNOTSUPP;
  2496. ret = usb4_dp_port_granularity(port);
  2497. if (ret < 0)
  2498. return ret;
  2499. granularity = ret;
  2500. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2501. port->cap_adap + ADP_DP_CS_2, 1);
  2502. if (ret)
  2503. return ret;
  2504. val &= ~ADP_DP_CS_2_ESTIMATED_BW_MASK;
  2505. val |= (bw / granularity) << ADP_DP_CS_2_ESTIMATED_BW_SHIFT;
  2506. return tb_port_write(port, &val, TB_CFG_PORT,
  2507. port->cap_adap + ADP_DP_CS_2, 1);
  2508. }
  2509. /**
  2510. * usb4_dp_port_allocated_bandwidth() - Return allocated bandwidth
  2511. * @port: DP IN adapter
  2512. *
  2513. * Reads the allocated bandwidth for @port in Mb/s (taking into account
  2514. * the programmed granularity).
  2515. *
  2516. * Return: Allocated bandwidth in Mb/s or negative errno in case of an error.
  2517. */
  2518. int usb4_dp_port_allocated_bandwidth(struct tb_port *port)
  2519. {
  2520. u32 val, granularity;
  2521. int ret;
  2522. if (!is_usb4_dpin(port))
  2523. return -EOPNOTSUPP;
  2524. ret = usb4_dp_port_granularity(port);
  2525. if (ret < 0)
  2526. return ret;
  2527. granularity = ret;
  2528. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2529. port->cap_adap + DP_STATUS, 1);
  2530. if (ret)
  2531. return ret;
  2532. val &= DP_STATUS_ALLOCATED_BW_MASK;
  2533. val >>= DP_STATUS_ALLOCATED_BW_SHIFT;
  2534. return val * granularity;
  2535. }
  2536. static int __usb4_dp_port_set_cm_ack(struct tb_port *port, bool ack)
  2537. {
  2538. u32 val;
  2539. int ret;
  2540. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2541. port->cap_adap + ADP_DP_CS_2, 1);
  2542. if (ret)
  2543. return ret;
  2544. if (ack)
  2545. val |= ADP_DP_CS_2_CA;
  2546. else
  2547. val &= ~ADP_DP_CS_2_CA;
  2548. return tb_port_write(port, &val, TB_CFG_PORT,
  2549. port->cap_adap + ADP_DP_CS_2, 1);
  2550. }
  2551. static inline int usb4_dp_port_set_cm_ack(struct tb_port *port)
  2552. {
  2553. return __usb4_dp_port_set_cm_ack(port, true);
  2554. }
  2555. static int usb4_dp_port_wait_and_clear_cm_ack(struct tb_port *port,
  2556. int timeout_msec)
  2557. {
  2558. ktime_t end;
  2559. u32 val;
  2560. int ret;
  2561. ret = __usb4_dp_port_set_cm_ack(port, false);
  2562. if (ret)
  2563. return ret;
  2564. end = ktime_add_ms(ktime_get(), timeout_msec);
  2565. do {
  2566. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2567. port->cap_adap + ADP_DP_CS_8, 1);
  2568. if (ret)
  2569. return ret;
  2570. if (!(val & ADP_DP_CS_8_DR))
  2571. break;
  2572. usleep_range(50, 100);
  2573. } while (ktime_before(ktime_get(), end));
  2574. if (val & ADP_DP_CS_8_DR) {
  2575. tb_port_warn(port, "timeout waiting for DPTX request to clear\n");
  2576. return -ETIMEDOUT;
  2577. }
  2578. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2579. port->cap_adap + ADP_DP_CS_2, 1);
  2580. if (ret)
  2581. return ret;
  2582. val &= ~ADP_DP_CS_2_CA;
  2583. return tb_port_write(port, &val, TB_CFG_PORT,
  2584. port->cap_adap + ADP_DP_CS_2, 1);
  2585. }
  2586. /**
  2587. * usb4_dp_port_allocate_bandwidth() - Set allocated bandwidth
  2588. * @port: DP IN adapter
  2589. * @bw: New allocated bandwidth in Mb/s
  2590. *
  2591. * Communicates the new allocated bandwidth with the DPCD (graphics
  2592. * driver). Takes into account the programmed granularity.
  2593. *
  2594. * Return: %0 on success, negative errno otherwise.
  2595. */
  2596. int usb4_dp_port_allocate_bandwidth(struct tb_port *port, int bw)
  2597. {
  2598. u32 val, granularity;
  2599. int ret;
  2600. if (!is_usb4_dpin(port))
  2601. return -EOPNOTSUPP;
  2602. ret = usb4_dp_port_granularity(port);
  2603. if (ret < 0)
  2604. return ret;
  2605. granularity = ret;
  2606. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2607. port->cap_adap + DP_STATUS, 1);
  2608. if (ret)
  2609. return ret;
  2610. val &= ~DP_STATUS_ALLOCATED_BW_MASK;
  2611. val |= (bw / granularity) << DP_STATUS_ALLOCATED_BW_SHIFT;
  2612. ret = tb_port_write(port, &val, TB_CFG_PORT,
  2613. port->cap_adap + DP_STATUS, 1);
  2614. if (ret)
  2615. return ret;
  2616. ret = usb4_dp_port_set_cm_ack(port);
  2617. if (ret)
  2618. return ret;
  2619. return usb4_dp_port_wait_and_clear_cm_ack(port, 500);
  2620. }
  2621. /**
  2622. * usb4_dp_port_requested_bandwidth() - Read requested bandwidth
  2623. * @port: DP IN adapter
  2624. *
  2625. * Reads the DPCD (graphics driver) requested bandwidth and returns it
  2626. * in Mb/s. Takes the programmed granularity into account.
  2627. *
  2628. * Return:
  2629. * * Requested bandwidth in Mb/s - On success.
  2630. * * %-EOPNOTSUPP - If the adapter does not support bandwidth allocation
  2631. * mode.
  2632. * * %ENODATA - If there is no active bandwidth request from the graphics
  2633. * driver.
  2634. * * Negative errno - On failure.
  2635. */
  2636. int usb4_dp_port_requested_bandwidth(struct tb_port *port)
  2637. {
  2638. u32 val, granularity;
  2639. int ret;
  2640. if (!is_usb4_dpin(port))
  2641. return -EOPNOTSUPP;
  2642. ret = usb4_dp_port_granularity(port);
  2643. if (ret < 0)
  2644. return ret;
  2645. granularity = ret;
  2646. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2647. port->cap_adap + ADP_DP_CS_8, 1);
  2648. if (ret)
  2649. return ret;
  2650. if (!(val & ADP_DP_CS_8_DR))
  2651. return -ENODATA;
  2652. return (val & ADP_DP_CS_8_REQUESTED_BW_MASK) * granularity;
  2653. }
  2654. /**
  2655. * usb4_pci_port_set_ext_encapsulation() - Enable/disable extended encapsulation
  2656. * @port: PCIe adapter
  2657. * @enable: Enable/disable extended encapsulation
  2658. *
  2659. * Enables or disables extended encapsulation used in PCIe tunneling. Caller
  2660. * needs to make sure both adapters support this before enabling.
  2661. *
  2662. * Return: %0 on success, negative errno otherwise.
  2663. */
  2664. int usb4_pci_port_set_ext_encapsulation(struct tb_port *port, bool enable)
  2665. {
  2666. u32 val;
  2667. int ret;
  2668. if (!tb_port_is_pcie_up(port) && !tb_port_is_pcie_down(port))
  2669. return -EINVAL;
  2670. ret = tb_port_read(port, &val, TB_CFG_PORT,
  2671. port->cap_adap + ADP_PCIE_CS_1, 1);
  2672. if (ret)
  2673. return ret;
  2674. if (enable)
  2675. val |= ADP_PCIE_CS_1_EE;
  2676. else
  2677. val &= ~ADP_PCIE_CS_1_EE;
  2678. return tb_port_write(port, &val, TB_CFG_PORT,
  2679. port->cap_adap + ADP_PCIE_CS_1, 1);
  2680. }