switch.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - switch/port utility functions
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/idr.h>
  10. #include <linux/module.h>
  11. #include <linux/nvmem-provider.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/sizes.h>
  15. #include <linux/slab.h>
  16. #include <linux/string_helpers.h>
  17. #include "tb.h"
  18. /* Switch NVM support */
  19. struct nvm_auth_status {
  20. struct list_head list;
  21. uuid_t uuid;
  22. u32 status;
  23. };
  24. /*
  25. * Hold NVM authentication failure status per switch This information
  26. * needs to stay around even when the switch gets power cycled so we
  27. * keep it separately.
  28. */
  29. static LIST_HEAD(nvm_auth_status_cache);
  30. static DEFINE_MUTEX(nvm_auth_status_lock);
  31. static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
  32. {
  33. struct nvm_auth_status *st;
  34. list_for_each_entry(st, &nvm_auth_status_cache, list) {
  35. if (uuid_equal(&st->uuid, sw->uuid))
  36. return st;
  37. }
  38. return NULL;
  39. }
  40. static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
  41. {
  42. struct nvm_auth_status *st;
  43. mutex_lock(&nvm_auth_status_lock);
  44. st = __nvm_get_auth_status(sw);
  45. mutex_unlock(&nvm_auth_status_lock);
  46. *status = st ? st->status : 0;
  47. }
  48. static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
  49. {
  50. struct nvm_auth_status *st;
  51. if (WARN_ON(!sw->uuid))
  52. return;
  53. mutex_lock(&nvm_auth_status_lock);
  54. st = __nvm_get_auth_status(sw);
  55. if (!st) {
  56. st = kzalloc(sizeof(*st), GFP_KERNEL);
  57. if (!st)
  58. goto unlock;
  59. memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
  60. INIT_LIST_HEAD(&st->list);
  61. list_add_tail(&st->list, &nvm_auth_status_cache);
  62. }
  63. st->status = status;
  64. unlock:
  65. mutex_unlock(&nvm_auth_status_lock);
  66. }
  67. static void nvm_clear_auth_status(const struct tb_switch *sw)
  68. {
  69. struct nvm_auth_status *st;
  70. mutex_lock(&nvm_auth_status_lock);
  71. st = __nvm_get_auth_status(sw);
  72. if (st) {
  73. list_del(&st->list);
  74. kfree(st);
  75. }
  76. mutex_unlock(&nvm_auth_status_lock);
  77. }
  78. static int nvm_validate_and_write(struct tb_switch *sw)
  79. {
  80. unsigned int image_size;
  81. const u8 *buf;
  82. int ret;
  83. ret = tb_nvm_validate(sw->nvm);
  84. if (ret)
  85. return ret;
  86. ret = tb_nvm_write_headers(sw->nvm);
  87. if (ret)
  88. return ret;
  89. buf = sw->nvm->buf_data_start;
  90. image_size = sw->nvm->buf_data_size;
  91. if (tb_switch_is_usb4(sw))
  92. ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
  93. else
  94. ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
  95. if (ret)
  96. return ret;
  97. sw->nvm->flushed = true;
  98. return 0;
  99. }
  100. static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
  101. {
  102. int ret = 0;
  103. /*
  104. * Root switch NVM upgrade requires that we disconnect the
  105. * existing paths first (in case it is not in safe mode
  106. * already).
  107. */
  108. if (!sw->safe_mode) {
  109. u32 status;
  110. ret = tb_domain_disconnect_all_paths(sw->tb);
  111. if (ret)
  112. return ret;
  113. /*
  114. * The host controller goes away pretty soon after this if
  115. * everything goes well so getting timeout is expected.
  116. */
  117. ret = dma_port_flash_update_auth(sw->dma_port);
  118. if (!ret || ret == -ETIMEDOUT)
  119. return 0;
  120. /*
  121. * Any error from update auth operation requires power
  122. * cycling of the host router.
  123. */
  124. tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
  125. if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
  126. nvm_set_auth_status(sw, status);
  127. }
  128. /*
  129. * From safe mode we can get out by just power cycling the
  130. * switch.
  131. */
  132. dma_port_power_cycle(sw->dma_port);
  133. return ret;
  134. }
  135. static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
  136. {
  137. int ret, retries = 10;
  138. ret = dma_port_flash_update_auth(sw->dma_port);
  139. switch (ret) {
  140. case 0:
  141. case -ETIMEDOUT:
  142. case -EACCES:
  143. case -EINVAL:
  144. /* Power cycle is required */
  145. break;
  146. default:
  147. return ret;
  148. }
  149. /*
  150. * Poll here for the authentication status. It takes some time
  151. * for the device to respond (we get timeout for a while). Once
  152. * we get response the device needs to be power cycled in order
  153. * to the new NVM to be taken into use.
  154. */
  155. do {
  156. u32 status;
  157. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  158. if (ret < 0 && ret != -ETIMEDOUT)
  159. return ret;
  160. if (ret > 0) {
  161. if (status) {
  162. tb_sw_warn(sw, "failed to authenticate NVM\n");
  163. nvm_set_auth_status(sw, status);
  164. }
  165. tb_sw_info(sw, "power cycling the switch now\n");
  166. dma_port_power_cycle(sw->dma_port);
  167. return 0;
  168. }
  169. msleep(500);
  170. } while (--retries);
  171. return -ETIMEDOUT;
  172. }
  173. static void nvm_authenticate_start_dma_port(struct tb_switch *sw)
  174. {
  175. struct pci_dev *root_port;
  176. /*
  177. * During host router NVM upgrade we should not allow root port to
  178. * go into D3cold because some root ports cannot trigger PME
  179. * itself. To be on the safe side keep the root port in D0 during
  180. * the whole upgrade process.
  181. */
  182. root_port = pcie_find_root_port(sw->tb->nhi->pdev);
  183. if (root_port)
  184. pm_runtime_get_noresume(&root_port->dev);
  185. }
  186. static void nvm_authenticate_complete_dma_port(struct tb_switch *sw)
  187. {
  188. struct pci_dev *root_port;
  189. root_port = pcie_find_root_port(sw->tb->nhi->pdev);
  190. if (root_port)
  191. pm_runtime_put(&root_port->dev);
  192. }
  193. static inline bool nvm_readable(struct tb_switch *sw)
  194. {
  195. if (tb_switch_is_usb4(sw)) {
  196. /*
  197. * USB4 devices must support NVM operations but it is
  198. * optional for hosts. Therefore we query the NVM sector
  199. * size here and if it is supported assume NVM
  200. * operations are implemented.
  201. */
  202. return usb4_switch_nvm_sector_size(sw) > 0;
  203. }
  204. /* Thunderbolt 2 and 3 devices support NVM through DMA port */
  205. return !!sw->dma_port;
  206. }
  207. static inline bool nvm_upgradeable(struct tb_switch *sw)
  208. {
  209. if (sw->no_nvm_upgrade)
  210. return false;
  211. return nvm_readable(sw);
  212. }
  213. static int nvm_authenticate(struct tb_switch *sw, bool auth_only)
  214. {
  215. int ret;
  216. if (tb_switch_is_usb4(sw)) {
  217. if (auth_only) {
  218. ret = usb4_switch_nvm_set_offset(sw, 0);
  219. if (ret)
  220. return ret;
  221. }
  222. sw->nvm->authenticating = true;
  223. return usb4_switch_nvm_authenticate(sw);
  224. }
  225. if (auth_only)
  226. return -EOPNOTSUPP;
  227. sw->nvm->authenticating = true;
  228. if (!tb_route(sw)) {
  229. nvm_authenticate_start_dma_port(sw);
  230. ret = nvm_authenticate_host_dma_port(sw);
  231. } else {
  232. ret = nvm_authenticate_device_dma_port(sw);
  233. }
  234. return ret;
  235. }
  236. /**
  237. * tb_switch_nvm_read() - Read router NVM
  238. * @sw: Router whose NVM to read
  239. * @address: Start address on the NVM
  240. * @buf: Buffer where the read data is copied
  241. * @size: Size of the buffer in bytes
  242. *
  243. * Reads from router NVM and returns the requested data in @buf. Locking
  244. * is up to the caller. Returns %0 in success and negative errno in case
  245. * of failure.
  246. */
  247. int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
  248. size_t size)
  249. {
  250. if (tb_switch_is_usb4(sw))
  251. return usb4_switch_nvm_read(sw, address, buf, size);
  252. return dma_port_flash_read(sw->dma_port, address, buf, size);
  253. }
  254. static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
  255. {
  256. struct tb_nvm *nvm = priv;
  257. struct tb_switch *sw = tb_to_switch(nvm->dev);
  258. int ret;
  259. pm_runtime_get_sync(&sw->dev);
  260. if (!mutex_trylock(&sw->tb->lock)) {
  261. ret = restart_syscall();
  262. goto out;
  263. }
  264. ret = tb_switch_nvm_read(sw, offset, val, bytes);
  265. mutex_unlock(&sw->tb->lock);
  266. out:
  267. pm_runtime_mark_last_busy(&sw->dev);
  268. pm_runtime_put_autosuspend(&sw->dev);
  269. return ret;
  270. }
  271. static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
  272. {
  273. struct tb_nvm *nvm = priv;
  274. struct tb_switch *sw = tb_to_switch(nvm->dev);
  275. int ret;
  276. if (!mutex_trylock(&sw->tb->lock))
  277. return restart_syscall();
  278. /*
  279. * Since writing the NVM image might require some special steps,
  280. * for example when CSS headers are written, we cache the image
  281. * locally here and handle the special cases when the user asks
  282. * us to authenticate the image.
  283. */
  284. ret = tb_nvm_write_buf(nvm, offset, val, bytes);
  285. mutex_unlock(&sw->tb->lock);
  286. return ret;
  287. }
  288. static int tb_switch_nvm_add(struct tb_switch *sw)
  289. {
  290. struct tb_nvm *nvm;
  291. int ret;
  292. if (!nvm_readable(sw))
  293. return 0;
  294. nvm = tb_nvm_alloc(&sw->dev);
  295. if (IS_ERR(nvm)) {
  296. ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
  297. goto err_nvm;
  298. }
  299. ret = tb_nvm_read_version(nvm);
  300. if (ret)
  301. goto err_nvm;
  302. /*
  303. * If the switch is in safe-mode the only accessible portion of
  304. * the NVM is the non-active one where userspace is expected to
  305. * write new functional NVM.
  306. */
  307. if (!sw->safe_mode) {
  308. ret = tb_nvm_add_active(nvm, nvm_read);
  309. if (ret)
  310. goto err_nvm;
  311. tb_sw_dbg(sw, "NVM version %x.%x\n", nvm->major, nvm->minor);
  312. }
  313. if (!sw->no_nvm_upgrade) {
  314. ret = tb_nvm_add_non_active(nvm, nvm_write);
  315. if (ret)
  316. goto err_nvm;
  317. }
  318. sw->nvm = nvm;
  319. return 0;
  320. err_nvm:
  321. tb_sw_dbg(sw, "NVM upgrade disabled\n");
  322. sw->no_nvm_upgrade = true;
  323. if (!IS_ERR(nvm))
  324. tb_nvm_free(nvm);
  325. return ret;
  326. }
  327. static void tb_switch_nvm_remove(struct tb_switch *sw)
  328. {
  329. struct tb_nvm *nvm;
  330. nvm = sw->nvm;
  331. sw->nvm = NULL;
  332. if (!nvm)
  333. return;
  334. /* Remove authentication status in case the switch is unplugged */
  335. if (!nvm->authenticating)
  336. nvm_clear_auth_status(sw);
  337. tb_nvm_free(nvm);
  338. }
  339. /* port utility functions */
  340. static const char *tb_port_type(const struct tb_regs_port_header *port)
  341. {
  342. switch (port->type >> 16) {
  343. case 0:
  344. switch ((u8) port->type) {
  345. case 0:
  346. return "Inactive";
  347. case 1:
  348. return "Port";
  349. case 2:
  350. return "NHI";
  351. default:
  352. return "unknown";
  353. }
  354. case 0x2:
  355. return "Ethernet";
  356. case 0x8:
  357. return "SATA";
  358. case 0xe:
  359. return "DP/HDMI";
  360. case 0x10:
  361. return "PCIe";
  362. case 0x20:
  363. return "USB";
  364. default:
  365. return "unknown";
  366. }
  367. }
  368. static void tb_dump_port(struct tb *tb, const struct tb_port *port)
  369. {
  370. const struct tb_regs_port_header *regs = &port->config;
  371. tb_dbg(tb,
  372. " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
  373. regs->port_number, regs->vendor_id, regs->device_id,
  374. regs->revision, regs->thunderbolt_version, tb_port_type(regs),
  375. regs->type);
  376. tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
  377. regs->max_in_hop_id, regs->max_out_hop_id);
  378. tb_dbg(tb, " Max counters: %d\n", regs->max_counters);
  379. tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits);
  380. tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits,
  381. port->ctl_credits);
  382. }
  383. /**
  384. * tb_port_state() - get connectedness state of a port
  385. * @port: the port to check
  386. *
  387. * The port must have a TB_CAP_PHY (i.e. it should be a real port).
  388. *
  389. * Return: Returns an enum tb_port_state on success or an error code on failure.
  390. */
  391. int tb_port_state(struct tb_port *port)
  392. {
  393. struct tb_cap_phy phy;
  394. int res;
  395. if (port->cap_phy == 0) {
  396. tb_port_WARN(port, "does not have a PHY\n");
  397. return -EINVAL;
  398. }
  399. res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
  400. if (res)
  401. return res;
  402. return phy.state;
  403. }
  404. /**
  405. * tb_wait_for_port() - wait for a port to become ready
  406. * @port: Port to wait
  407. * @wait_if_unplugged: Wait also when port is unplugged
  408. *
  409. * Wait up to 1 second for a port to reach state TB_PORT_UP. If
  410. * wait_if_unplugged is set then we also wait if the port is in state
  411. * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
  412. * switch resume). Otherwise we only wait if a device is registered but the link
  413. * has not yet been established.
  414. *
  415. * Return: Returns an error code on failure. Returns 0 if the port is not
  416. * connected or failed to reach state TB_PORT_UP within one second. Returns 1
  417. * if the port is connected and in state TB_PORT_UP.
  418. */
  419. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  420. {
  421. int retries = 10;
  422. int state;
  423. if (!port->cap_phy) {
  424. tb_port_WARN(port, "does not have PHY\n");
  425. return -EINVAL;
  426. }
  427. if (tb_is_upstream_port(port)) {
  428. tb_port_WARN(port, "is the upstream port\n");
  429. return -EINVAL;
  430. }
  431. while (retries--) {
  432. state = tb_port_state(port);
  433. switch (state) {
  434. case TB_PORT_DISABLED:
  435. tb_port_dbg(port, "is disabled (state: 0)\n");
  436. return 0;
  437. case TB_PORT_UNPLUGGED:
  438. if (wait_if_unplugged) {
  439. /* used during resume */
  440. tb_port_dbg(port,
  441. "is unplugged (state: 7), retrying...\n");
  442. msleep(100);
  443. break;
  444. }
  445. tb_port_dbg(port, "is unplugged (state: 7)\n");
  446. return 0;
  447. case TB_PORT_UP:
  448. case TB_PORT_TX_CL0S:
  449. case TB_PORT_RX_CL0S:
  450. case TB_PORT_CL1:
  451. case TB_PORT_CL2:
  452. tb_port_dbg(port, "is connected, link is up (state: %d)\n", state);
  453. return 1;
  454. default:
  455. if (state < 0)
  456. return state;
  457. /*
  458. * After plug-in the state is TB_PORT_CONNECTING. Give it some
  459. * time.
  460. */
  461. tb_port_dbg(port,
  462. "is connected, link is not up (state: %d), retrying...\n",
  463. state);
  464. msleep(100);
  465. }
  466. }
  467. tb_port_warn(port,
  468. "failed to reach state TB_PORT_UP. Ignoring port...\n");
  469. return 0;
  470. }
  471. /**
  472. * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
  473. * @port: Port to add/remove NFC credits
  474. * @credits: Credits to add/remove
  475. *
  476. * Change the number of NFC credits allocated to @port by @credits. To remove
  477. * NFC credits pass a negative amount of credits.
  478. *
  479. * Return: Returns 0 on success or an error code on failure.
  480. */
  481. int tb_port_add_nfc_credits(struct tb_port *port, int credits)
  482. {
  483. u32 nfc_credits;
  484. if (credits == 0 || port->sw->is_unplugged)
  485. return 0;
  486. /*
  487. * USB4 restricts programming NFC buffers to lane adapters only
  488. * so skip other ports.
  489. */
  490. if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
  491. return 0;
  492. nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
  493. if (credits < 0)
  494. credits = max_t(int, -nfc_credits, credits);
  495. nfc_credits += credits;
  496. tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
  497. port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
  498. port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
  499. port->config.nfc_credits |= nfc_credits;
  500. return tb_port_write(port, &port->config.nfc_credits,
  501. TB_CFG_PORT, ADP_CS_4, 1);
  502. }
  503. /**
  504. * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
  505. * @port: Port whose counters to clear
  506. * @counter: Counter index to clear
  507. *
  508. * Return: Returns 0 on success or an error code on failure.
  509. */
  510. int tb_port_clear_counter(struct tb_port *port, int counter)
  511. {
  512. u32 zero[3] = { 0, 0, 0 };
  513. tb_port_dbg(port, "clearing counter %d\n", counter);
  514. return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
  515. }
  516. /**
  517. * tb_port_unlock() - Unlock downstream port
  518. * @port: Port to unlock
  519. *
  520. * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the
  521. * downstream router accessible for CM.
  522. */
  523. int tb_port_unlock(struct tb_port *port)
  524. {
  525. if (tb_switch_is_icm(port->sw))
  526. return 0;
  527. if (!tb_port_is_null(port))
  528. return -EINVAL;
  529. if (tb_switch_is_usb4(port->sw))
  530. return usb4_port_unlock(port);
  531. return 0;
  532. }
  533. static int __tb_port_enable(struct tb_port *port, bool enable)
  534. {
  535. int ret;
  536. u32 phy;
  537. if (!tb_port_is_null(port))
  538. return -EINVAL;
  539. ret = tb_port_read(port, &phy, TB_CFG_PORT,
  540. port->cap_phy + LANE_ADP_CS_1, 1);
  541. if (ret)
  542. return ret;
  543. if (enable)
  544. phy &= ~LANE_ADP_CS_1_LD;
  545. else
  546. phy |= LANE_ADP_CS_1_LD;
  547. ret = tb_port_write(port, &phy, TB_CFG_PORT,
  548. port->cap_phy + LANE_ADP_CS_1, 1);
  549. if (ret)
  550. return ret;
  551. tb_port_dbg(port, "lane %s\n", str_enabled_disabled(enable));
  552. return 0;
  553. }
  554. /**
  555. * tb_port_enable() - Enable lane adapter
  556. * @port: Port to enable (can be %NULL)
  557. *
  558. * This is used for lane 0 and 1 adapters to enable it.
  559. */
  560. int tb_port_enable(struct tb_port *port)
  561. {
  562. return __tb_port_enable(port, true);
  563. }
  564. /**
  565. * tb_port_disable() - Disable lane adapter
  566. * @port: Port to disable (can be %NULL)
  567. *
  568. * This is used for lane 0 and 1 adapters to disable it.
  569. */
  570. int tb_port_disable(struct tb_port *port)
  571. {
  572. return __tb_port_enable(port, false);
  573. }
  574. static int tb_port_reset(struct tb_port *port)
  575. {
  576. if (tb_switch_is_usb4(port->sw))
  577. return port->cap_usb4 ? usb4_port_reset(port) : 0;
  578. return tb_lc_reset_port(port);
  579. }
  580. /*
  581. * tb_init_port() - initialize a port
  582. *
  583. * This is a helper method for tb_switch_alloc. Does not check or initialize
  584. * any downstream switches.
  585. *
  586. * Return: Returns 0 on success or an error code on failure.
  587. */
  588. static int tb_init_port(struct tb_port *port)
  589. {
  590. int res;
  591. int cap;
  592. INIT_LIST_HEAD(&port->list);
  593. /* Control adapter does not have configuration space */
  594. if (!port->port)
  595. return 0;
  596. res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
  597. if (res) {
  598. if (res == -ENODEV) {
  599. tb_dbg(port->sw->tb, " Port %d: not implemented\n",
  600. port->port);
  601. port->disabled = true;
  602. return 0;
  603. }
  604. return res;
  605. }
  606. /* Port 0 is the switch itself and has no PHY. */
  607. if (port->config.type == TB_TYPE_PORT) {
  608. cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
  609. if (cap > 0)
  610. port->cap_phy = cap;
  611. else
  612. tb_port_WARN(port, "non switch port without a PHY\n");
  613. cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
  614. if (cap > 0)
  615. port->cap_usb4 = cap;
  616. /*
  617. * USB4 ports the buffers allocated for the control path
  618. * can be read from the path config space. Legacy
  619. * devices we use hard-coded value.
  620. */
  621. if (port->cap_usb4) {
  622. struct tb_regs_hop hop;
  623. if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2))
  624. port->ctl_credits = hop.initial_credits;
  625. }
  626. if (!port->ctl_credits)
  627. port->ctl_credits = 2;
  628. } else {
  629. cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
  630. if (cap > 0)
  631. port->cap_adap = cap;
  632. }
  633. port->total_credits =
  634. (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
  635. ADP_CS_4_TOTAL_BUFFERS_SHIFT;
  636. tb_dump_port(port->sw->tb, port);
  637. return 0;
  638. }
  639. static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
  640. int max_hopid)
  641. {
  642. int port_max_hopid;
  643. struct ida *ida;
  644. if (in) {
  645. port_max_hopid = port->config.max_in_hop_id;
  646. ida = &port->in_hopids;
  647. } else {
  648. port_max_hopid = port->config.max_out_hop_id;
  649. ida = &port->out_hopids;
  650. }
  651. /*
  652. * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are
  653. * reserved.
  654. */
  655. if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
  656. min_hopid = TB_PATH_MIN_HOPID;
  657. if (max_hopid < 0 || max_hopid > port_max_hopid)
  658. max_hopid = port_max_hopid;
  659. return ida_alloc_range(ida, min_hopid, max_hopid, GFP_KERNEL);
  660. }
  661. /**
  662. * tb_port_alloc_in_hopid() - Allocate input HopID from port
  663. * @port: Port to allocate HopID for
  664. * @min_hopid: Minimum acceptable input HopID
  665. * @max_hopid: Maximum acceptable input HopID
  666. *
  667. * Return: HopID between @min_hopid and @max_hopid or negative errno in
  668. * case of error.
  669. */
  670. int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
  671. {
  672. return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
  673. }
  674. /**
  675. * tb_port_alloc_out_hopid() - Allocate output HopID from port
  676. * @port: Port to allocate HopID for
  677. * @min_hopid: Minimum acceptable output HopID
  678. * @max_hopid: Maximum acceptable output HopID
  679. *
  680. * Return: HopID between @min_hopid and @max_hopid or negative errno in
  681. * case of error.
  682. */
  683. int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
  684. {
  685. return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
  686. }
  687. /**
  688. * tb_port_release_in_hopid() - Release allocated input HopID from port
  689. * @port: Port whose HopID to release
  690. * @hopid: HopID to release
  691. */
  692. void tb_port_release_in_hopid(struct tb_port *port, int hopid)
  693. {
  694. ida_free(&port->in_hopids, hopid);
  695. }
  696. /**
  697. * tb_port_release_out_hopid() - Release allocated output HopID from port
  698. * @port: Port whose HopID to release
  699. * @hopid: HopID to release
  700. */
  701. void tb_port_release_out_hopid(struct tb_port *port, int hopid)
  702. {
  703. ida_free(&port->out_hopids, hopid);
  704. }
  705. static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
  706. const struct tb_switch *sw)
  707. {
  708. u64 mask = (1ULL << parent->config.depth * 8) - 1;
  709. return (tb_route(parent) & mask) == (tb_route(sw) & mask);
  710. }
  711. /**
  712. * tb_next_port_on_path() - Return next port for given port on a path
  713. * @start: Start port of the walk
  714. * @end: End port of the walk
  715. * @prev: Previous port (%NULL if this is the first)
  716. *
  717. * This function can be used to walk from one port to another if they
  718. * are connected through zero or more switches. If the @prev is dual
  719. * link port, the function follows that link and returns another end on
  720. * that same link.
  721. *
  722. * If the @end port has been reached, return %NULL.
  723. *
  724. * Domain tb->lock must be held when this function is called.
  725. */
  726. struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
  727. struct tb_port *prev)
  728. {
  729. struct tb_port *next;
  730. if (!prev)
  731. return start;
  732. if (prev->sw == end->sw) {
  733. if (prev == end)
  734. return NULL;
  735. return end;
  736. }
  737. if (tb_switch_is_reachable(prev->sw, end->sw)) {
  738. next = tb_port_at(tb_route(end->sw), prev->sw);
  739. /* Walk down the topology if next == prev */
  740. if (prev->remote &&
  741. (next == prev || next->dual_link_port == prev))
  742. next = prev->remote;
  743. } else {
  744. if (tb_is_upstream_port(prev)) {
  745. next = prev->remote;
  746. } else {
  747. next = tb_upstream_port(prev->sw);
  748. /*
  749. * Keep the same link if prev and next are both
  750. * dual link ports.
  751. */
  752. if (next->dual_link_port &&
  753. next->link_nr != prev->link_nr) {
  754. next = next->dual_link_port;
  755. }
  756. }
  757. }
  758. return next != prev ? next : NULL;
  759. }
  760. /**
  761. * tb_port_get_link_speed() - Get current link speed
  762. * @port: Port to check (USB4 or CIO)
  763. *
  764. * Returns link speed in Gb/s or negative errno in case of failure.
  765. */
  766. int tb_port_get_link_speed(struct tb_port *port)
  767. {
  768. u32 val, speed;
  769. int ret;
  770. if (!port->cap_phy)
  771. return -EINVAL;
  772. ret = tb_port_read(port, &val, TB_CFG_PORT,
  773. port->cap_phy + LANE_ADP_CS_1, 1);
  774. if (ret)
  775. return ret;
  776. speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >>
  777. LANE_ADP_CS_1_CURRENT_SPEED_SHIFT;
  778. switch (speed) {
  779. case LANE_ADP_CS_1_CURRENT_SPEED_GEN4:
  780. return 40;
  781. case LANE_ADP_CS_1_CURRENT_SPEED_GEN3:
  782. return 20;
  783. default:
  784. return 10;
  785. }
  786. }
  787. /**
  788. * tb_port_get_link_generation() - Returns link generation
  789. * @port: Lane adapter
  790. *
  791. * Returns link generation as number or negative errno in case of
  792. * failure. Does not distinguish between Thunderbolt 1 and Thunderbolt 2
  793. * links so for those always returns 2.
  794. */
  795. int tb_port_get_link_generation(struct tb_port *port)
  796. {
  797. int ret;
  798. ret = tb_port_get_link_speed(port);
  799. if (ret < 0)
  800. return ret;
  801. switch (ret) {
  802. case 40:
  803. return 4;
  804. case 20:
  805. return 3;
  806. default:
  807. return 2;
  808. }
  809. }
  810. /**
  811. * tb_port_get_link_width() - Get current link width
  812. * @port: Port to check (USB4 or CIO)
  813. *
  814. * Returns link width. Return the link width as encoded in &enum
  815. * tb_link_width or negative errno in case of failure.
  816. */
  817. int tb_port_get_link_width(struct tb_port *port)
  818. {
  819. u32 val;
  820. int ret;
  821. if (!port->cap_phy)
  822. return -EINVAL;
  823. ret = tb_port_read(port, &val, TB_CFG_PORT,
  824. port->cap_phy + LANE_ADP_CS_1, 1);
  825. if (ret)
  826. return ret;
  827. /* Matches the values in enum tb_link_width */
  828. return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
  829. LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
  830. }
  831. /**
  832. * tb_port_width_supported() - Is the given link width supported
  833. * @port: Port to check
  834. * @width: Widths to check (bitmask)
  835. *
  836. * Can be called to any lane adapter. Checks if given @width is
  837. * supported by the hardware and returns %true if it is.
  838. */
  839. bool tb_port_width_supported(struct tb_port *port, unsigned int width)
  840. {
  841. u32 phy, widths;
  842. int ret;
  843. if (!port->cap_phy)
  844. return false;
  845. if (width & (TB_LINK_WIDTH_ASYM_TX | TB_LINK_WIDTH_ASYM_RX)) {
  846. if (tb_port_get_link_generation(port) < 4 ||
  847. !usb4_port_asym_supported(port))
  848. return false;
  849. }
  850. ret = tb_port_read(port, &phy, TB_CFG_PORT,
  851. port->cap_phy + LANE_ADP_CS_0, 1);
  852. if (ret)
  853. return false;
  854. /*
  855. * The field encoding is the same as &enum tb_link_width (which is
  856. * passed to @width).
  857. */
  858. widths = FIELD_GET(LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK, phy);
  859. return widths & width;
  860. }
  861. /**
  862. * tb_port_set_link_width() - Set target link width of the lane adapter
  863. * @port: Lane adapter
  864. * @width: Target link width
  865. *
  866. * Sets the target link width of the lane adapter to @width. Does not
  867. * enable/disable lane bonding. For that call tb_port_set_lane_bonding().
  868. *
  869. * Return: %0 in case of success and negative errno in case of error
  870. */
  871. int tb_port_set_link_width(struct tb_port *port, enum tb_link_width width)
  872. {
  873. u32 val;
  874. int ret;
  875. if (!port->cap_phy)
  876. return -EINVAL;
  877. ret = tb_port_read(port, &val, TB_CFG_PORT,
  878. port->cap_phy + LANE_ADP_CS_1, 1);
  879. if (ret)
  880. return ret;
  881. val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK;
  882. switch (width) {
  883. case TB_LINK_WIDTH_SINGLE:
  884. /* Gen 4 link cannot be single */
  885. if (tb_port_get_link_generation(port) >= 4)
  886. return -EOPNOTSUPP;
  887. val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE <<
  888. LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
  889. break;
  890. case TB_LINK_WIDTH_DUAL:
  891. if (tb_port_get_link_generation(port) >= 4)
  892. return usb4_port_asym_set_link_width(port, width);
  893. val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL <<
  894. LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
  895. break;
  896. case TB_LINK_WIDTH_ASYM_TX:
  897. case TB_LINK_WIDTH_ASYM_RX:
  898. return usb4_port_asym_set_link_width(port, width);
  899. default:
  900. return -EINVAL;
  901. }
  902. return tb_port_write(port, &val, TB_CFG_PORT,
  903. port->cap_phy + LANE_ADP_CS_1, 1);
  904. }
  905. /**
  906. * tb_port_set_lane_bonding() - Enable/disable lane bonding
  907. * @port: Lane adapter
  908. * @bonding: enable/disable bonding
  909. *
  910. * Enables or disables lane bonding. This should be called after target
  911. * link width has been set (tb_port_set_link_width()). Note in most
  912. * cases one should use tb_port_lane_bonding_enable() instead to enable
  913. * lane bonding.
  914. *
  915. * Return: %0 in case of success and negative errno in case of error
  916. */
  917. static int tb_port_set_lane_bonding(struct tb_port *port, bool bonding)
  918. {
  919. u32 val;
  920. int ret;
  921. if (!port->cap_phy)
  922. return -EINVAL;
  923. ret = tb_port_read(port, &val, TB_CFG_PORT,
  924. port->cap_phy + LANE_ADP_CS_1, 1);
  925. if (ret)
  926. return ret;
  927. if (bonding)
  928. val |= LANE_ADP_CS_1_LB;
  929. else
  930. val &= ~LANE_ADP_CS_1_LB;
  931. return tb_port_write(port, &val, TB_CFG_PORT,
  932. port->cap_phy + LANE_ADP_CS_1, 1);
  933. }
  934. /**
  935. * tb_port_lane_bonding_enable() - Enable bonding on port
  936. * @port: port to enable
  937. *
  938. * Enable bonding by setting the link width of the port and the other
  939. * port in case of dual link port. Does not wait for the link to
  940. * actually reach the bonded state so caller needs to call
  941. * tb_port_wait_for_link_width() before enabling any paths through the
  942. * link to make sure the link is in expected state.
  943. *
  944. * Return: %0 in case of success and negative errno in case of error
  945. */
  946. int tb_port_lane_bonding_enable(struct tb_port *port)
  947. {
  948. enum tb_link_width width;
  949. int ret;
  950. /*
  951. * Enable lane bonding for both links if not already enabled by
  952. * for example the boot firmware.
  953. */
  954. width = tb_port_get_link_width(port);
  955. if (width == TB_LINK_WIDTH_SINGLE) {
  956. ret = tb_port_set_link_width(port, TB_LINK_WIDTH_DUAL);
  957. if (ret)
  958. goto err_lane0;
  959. }
  960. width = tb_port_get_link_width(port->dual_link_port);
  961. if (width == TB_LINK_WIDTH_SINGLE) {
  962. ret = tb_port_set_link_width(port->dual_link_port,
  963. TB_LINK_WIDTH_DUAL);
  964. if (ret)
  965. goto err_lane1;
  966. }
  967. /*
  968. * Only set bonding if the link was not already bonded. This
  969. * avoids the lane adapter to re-enter bonding state.
  970. */
  971. if (width == TB_LINK_WIDTH_SINGLE && !tb_is_upstream_port(port)) {
  972. ret = tb_port_set_lane_bonding(port, true);
  973. if (ret)
  974. goto err_lane1;
  975. }
  976. /*
  977. * When lane 0 bonding is set it will affect lane 1 too so
  978. * update both.
  979. */
  980. port->bonded = true;
  981. port->dual_link_port->bonded = true;
  982. return 0;
  983. err_lane1:
  984. tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE);
  985. err_lane0:
  986. tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE);
  987. return ret;
  988. }
  989. /**
  990. * tb_port_lane_bonding_disable() - Disable bonding on port
  991. * @port: port to disable
  992. *
  993. * Disable bonding by setting the link width of the port and the
  994. * other port in case of dual link port.
  995. */
  996. void tb_port_lane_bonding_disable(struct tb_port *port)
  997. {
  998. tb_port_set_lane_bonding(port, false);
  999. tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE);
  1000. tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE);
  1001. port->dual_link_port->bonded = false;
  1002. port->bonded = false;
  1003. }
  1004. /**
  1005. * tb_port_wait_for_link_width() - Wait until link reaches specific width
  1006. * @port: Port to wait for
  1007. * @width: Expected link width (bitmask)
  1008. * @timeout_msec: Timeout in ms how long to wait
  1009. *
  1010. * Should be used after both ends of the link have been bonded (or
  1011. * bonding has been disabled) to wait until the link actually reaches
  1012. * the expected state. Returns %-ETIMEDOUT if the width was not reached
  1013. * within the given timeout, %0 if it did. Can be passed a mask of
  1014. * expected widths and succeeds if any of the widths is reached.
  1015. */
  1016. int tb_port_wait_for_link_width(struct tb_port *port, unsigned int width,
  1017. int timeout_msec)
  1018. {
  1019. ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
  1020. int ret;
  1021. /* Gen 4 link does not support single lane */
  1022. if ((width & TB_LINK_WIDTH_SINGLE) &&
  1023. tb_port_get_link_generation(port) >= 4)
  1024. return -EOPNOTSUPP;
  1025. do {
  1026. ret = tb_port_get_link_width(port);
  1027. if (ret < 0) {
  1028. /*
  1029. * Sometimes we get port locked error when
  1030. * polling the lanes so we can ignore it and
  1031. * retry.
  1032. */
  1033. if (ret != -EACCES)
  1034. return ret;
  1035. } else if (ret & width) {
  1036. return 0;
  1037. }
  1038. usleep_range(1000, 2000);
  1039. } while (ktime_before(ktime_get(), timeout));
  1040. return -ETIMEDOUT;
  1041. }
  1042. static int tb_port_do_update_credits(struct tb_port *port)
  1043. {
  1044. u32 nfc_credits;
  1045. int ret;
  1046. ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1);
  1047. if (ret)
  1048. return ret;
  1049. if (nfc_credits != port->config.nfc_credits) {
  1050. u32 total;
  1051. total = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
  1052. ADP_CS_4_TOTAL_BUFFERS_SHIFT;
  1053. tb_port_dbg(port, "total credits changed %u -> %u\n",
  1054. port->total_credits, total);
  1055. port->config.nfc_credits = nfc_credits;
  1056. port->total_credits = total;
  1057. }
  1058. return 0;
  1059. }
  1060. /**
  1061. * tb_port_update_credits() - Re-read port total credits
  1062. * @port: Port to update
  1063. *
  1064. * After the link is bonded (or bonding was disabled) the port total
  1065. * credits may change, so this function needs to be called to re-read
  1066. * the credits. Updates also the second lane adapter.
  1067. */
  1068. int tb_port_update_credits(struct tb_port *port)
  1069. {
  1070. int ret;
  1071. ret = tb_port_do_update_credits(port);
  1072. if (ret)
  1073. return ret;
  1074. if (!port->dual_link_port)
  1075. return 0;
  1076. return tb_port_do_update_credits(port->dual_link_port);
  1077. }
  1078. static int tb_port_start_lane_initialization(struct tb_port *port)
  1079. {
  1080. int ret;
  1081. if (tb_switch_is_usb4(port->sw))
  1082. return 0;
  1083. ret = tb_lc_start_lane_initialization(port);
  1084. return ret == -EINVAL ? 0 : ret;
  1085. }
  1086. /*
  1087. * Returns true if the port had something (router, XDomain) connected
  1088. * before suspend.
  1089. */
  1090. static bool tb_port_resume(struct tb_port *port)
  1091. {
  1092. bool has_remote = tb_port_has_remote(port);
  1093. if (port->usb4) {
  1094. usb4_port_device_resume(port->usb4);
  1095. } else if (!has_remote) {
  1096. /*
  1097. * For disconnected downstream lane adapters start lane
  1098. * initialization now so we detect future connects.
  1099. *
  1100. * For XDomain start the lane initialzation now so the
  1101. * link gets re-established.
  1102. *
  1103. * This is only needed for non-USB4 ports.
  1104. */
  1105. if (!tb_is_upstream_port(port) || port->xdomain)
  1106. tb_port_start_lane_initialization(port);
  1107. }
  1108. return has_remote || port->xdomain;
  1109. }
  1110. /**
  1111. * tb_port_is_enabled() - Is the adapter port enabled
  1112. * @port: Port to check
  1113. */
  1114. bool tb_port_is_enabled(struct tb_port *port)
  1115. {
  1116. switch (port->config.type) {
  1117. case TB_TYPE_PCIE_UP:
  1118. case TB_TYPE_PCIE_DOWN:
  1119. return tb_pci_port_is_enabled(port);
  1120. case TB_TYPE_DP_HDMI_IN:
  1121. case TB_TYPE_DP_HDMI_OUT:
  1122. return tb_dp_port_is_enabled(port);
  1123. case TB_TYPE_USB3_UP:
  1124. case TB_TYPE_USB3_DOWN:
  1125. return tb_usb3_port_is_enabled(port);
  1126. default:
  1127. return false;
  1128. }
  1129. }
  1130. /**
  1131. * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
  1132. * @port: USB3 adapter port to check
  1133. */
  1134. bool tb_usb3_port_is_enabled(struct tb_port *port)
  1135. {
  1136. u32 data;
  1137. if (tb_port_read(port, &data, TB_CFG_PORT,
  1138. port->cap_adap + ADP_USB3_CS_0, 1))
  1139. return false;
  1140. return !!(data & ADP_USB3_CS_0_PE);
  1141. }
  1142. /**
  1143. * tb_usb3_port_enable() - Enable USB3 adapter port
  1144. * @port: USB3 adapter port to enable
  1145. * @enable: Enable/disable the USB3 adapter
  1146. */
  1147. int tb_usb3_port_enable(struct tb_port *port, bool enable)
  1148. {
  1149. u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V)
  1150. : ADP_USB3_CS_0_V;
  1151. if (!port->cap_adap)
  1152. return -ENXIO;
  1153. return tb_port_write(port, &word, TB_CFG_PORT,
  1154. port->cap_adap + ADP_USB3_CS_0, 1);
  1155. }
  1156. /**
  1157. * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
  1158. * @port: PCIe port to check
  1159. */
  1160. bool tb_pci_port_is_enabled(struct tb_port *port)
  1161. {
  1162. u32 data;
  1163. if (tb_port_read(port, &data, TB_CFG_PORT,
  1164. port->cap_adap + ADP_PCIE_CS_0, 1))
  1165. return false;
  1166. return !!(data & ADP_PCIE_CS_0_PE);
  1167. }
  1168. /**
  1169. * tb_pci_port_enable() - Enable PCIe adapter port
  1170. * @port: PCIe port to enable
  1171. * @enable: Enable/disable the PCIe adapter
  1172. */
  1173. int tb_pci_port_enable(struct tb_port *port, bool enable)
  1174. {
  1175. u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
  1176. if (!port->cap_adap)
  1177. return -ENXIO;
  1178. return tb_port_write(port, &word, TB_CFG_PORT,
  1179. port->cap_adap + ADP_PCIE_CS_0, 1);
  1180. }
  1181. /**
  1182. * tb_dp_port_hpd_is_active() - Is HPD already active
  1183. * @port: DP out port to check
  1184. *
  1185. * Checks if the DP OUT adapter port has HPD bit already set.
  1186. */
  1187. int tb_dp_port_hpd_is_active(struct tb_port *port)
  1188. {
  1189. u32 data;
  1190. int ret;
  1191. ret = tb_port_read(port, &data, TB_CFG_PORT,
  1192. port->cap_adap + ADP_DP_CS_2, 1);
  1193. if (ret)
  1194. return ret;
  1195. return !!(data & ADP_DP_CS_2_HPD);
  1196. }
  1197. /**
  1198. * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
  1199. * @port: Port to clear HPD
  1200. *
  1201. * If the DP IN port has HPD set, this function can be used to clear it.
  1202. */
  1203. int tb_dp_port_hpd_clear(struct tb_port *port)
  1204. {
  1205. u32 data;
  1206. int ret;
  1207. ret = tb_port_read(port, &data, TB_CFG_PORT,
  1208. port->cap_adap + ADP_DP_CS_3, 1);
  1209. if (ret)
  1210. return ret;
  1211. data |= ADP_DP_CS_3_HPDC;
  1212. return tb_port_write(port, &data, TB_CFG_PORT,
  1213. port->cap_adap + ADP_DP_CS_3, 1);
  1214. }
  1215. /**
  1216. * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
  1217. * @port: DP IN/OUT port to set hops
  1218. * @video: Video Hop ID
  1219. * @aux_tx: AUX TX Hop ID
  1220. * @aux_rx: AUX RX Hop ID
  1221. *
  1222. * Programs specified Hop IDs for DP IN/OUT port. Can be called for USB4
  1223. * router DP adapters too but does not program the values as the fields
  1224. * are read-only.
  1225. */
  1226. int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
  1227. unsigned int aux_tx, unsigned int aux_rx)
  1228. {
  1229. u32 data[2];
  1230. int ret;
  1231. if (tb_switch_is_usb4(port->sw))
  1232. return 0;
  1233. ret = tb_port_read(port, data, TB_CFG_PORT,
  1234. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1235. if (ret)
  1236. return ret;
  1237. data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
  1238. data[1] &= ~ADP_DP_CS_1_AUX_TX_HOPID_MASK;
  1239. data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
  1240. data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
  1241. ADP_DP_CS_0_VIDEO_HOPID_MASK;
  1242. data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
  1243. data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
  1244. ADP_DP_CS_1_AUX_RX_HOPID_MASK;
  1245. return tb_port_write(port, data, TB_CFG_PORT,
  1246. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1247. }
  1248. /**
  1249. * tb_dp_port_is_enabled() - Is DP adapter port enabled
  1250. * @port: DP adapter port to check
  1251. */
  1252. bool tb_dp_port_is_enabled(struct tb_port *port)
  1253. {
  1254. u32 data[2];
  1255. if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
  1256. ARRAY_SIZE(data)))
  1257. return false;
  1258. return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
  1259. }
  1260. /**
  1261. * tb_dp_port_enable() - Enables/disables DP paths of a port
  1262. * @port: DP IN/OUT port
  1263. * @enable: Enable/disable DP path
  1264. *
  1265. * Once Hop IDs are programmed DP paths can be enabled or disabled by
  1266. * calling this function.
  1267. */
  1268. int tb_dp_port_enable(struct tb_port *port, bool enable)
  1269. {
  1270. u32 data[2];
  1271. int ret;
  1272. ret = tb_port_read(port, data, TB_CFG_PORT,
  1273. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1274. if (ret)
  1275. return ret;
  1276. if (enable)
  1277. data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
  1278. else
  1279. data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
  1280. return tb_port_write(port, data, TB_CFG_PORT,
  1281. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1282. }
  1283. /* switch utility functions */
  1284. static const char *tb_switch_generation_name(const struct tb_switch *sw)
  1285. {
  1286. switch (sw->generation) {
  1287. case 1:
  1288. return "Thunderbolt 1";
  1289. case 2:
  1290. return "Thunderbolt 2";
  1291. case 3:
  1292. return "Thunderbolt 3";
  1293. case 4:
  1294. return "USB4";
  1295. default:
  1296. return "Unknown";
  1297. }
  1298. }
  1299. static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
  1300. {
  1301. const struct tb_regs_switch_header *regs = &sw->config;
  1302. tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n",
  1303. tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
  1304. regs->revision, regs->thunderbolt_version);
  1305. tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number);
  1306. tb_dbg(tb, " Config:\n");
  1307. tb_dbg(tb,
  1308. " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
  1309. regs->upstream_port_number, regs->depth,
  1310. (((u64) regs->route_hi) << 32) | regs->route_lo,
  1311. regs->enabled, regs->plug_events_delay);
  1312. tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
  1313. regs->__unknown1, regs->__unknown4);
  1314. }
  1315. static int tb_switch_reset_host(struct tb_switch *sw)
  1316. {
  1317. if (sw->generation > 1) {
  1318. struct tb_port *port;
  1319. tb_switch_for_each_port(sw, port) {
  1320. int i, ret;
  1321. /*
  1322. * For lane adapters we issue downstream port
  1323. * reset and clear up path config spaces.
  1324. *
  1325. * For protocol adapters we disable the path and
  1326. * clear path config space one by one (from 8 to
  1327. * Max Input HopID of the adapter).
  1328. */
  1329. if (tb_port_is_null(port) && !tb_is_upstream_port(port)) {
  1330. ret = tb_port_reset(port);
  1331. if (ret)
  1332. return ret;
  1333. } else if (tb_port_is_usb3_down(port) ||
  1334. tb_port_is_usb3_up(port)) {
  1335. tb_usb3_port_enable(port, false);
  1336. } else if (tb_port_is_dpin(port) ||
  1337. tb_port_is_dpout(port)) {
  1338. tb_dp_port_enable(port, false);
  1339. } else if (tb_port_is_pcie_down(port) ||
  1340. tb_port_is_pcie_up(port)) {
  1341. tb_pci_port_enable(port, false);
  1342. } else {
  1343. continue;
  1344. }
  1345. /* Cleanup path config space of protocol adapter */
  1346. for (i = TB_PATH_MIN_HOPID;
  1347. i <= port->config.max_in_hop_id; i++) {
  1348. ret = tb_path_deactivate_hop(port, i);
  1349. if (ret)
  1350. return ret;
  1351. }
  1352. }
  1353. } else {
  1354. struct tb_cfg_result res;
  1355. /* Thunderbolt 1 uses the "reset" config space packet */
  1356. res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
  1357. TB_CFG_SWITCH, 2, 2);
  1358. if (res.err)
  1359. return res.err;
  1360. res = tb_cfg_reset(sw->tb->ctl, tb_route(sw));
  1361. if (res.err > 0)
  1362. return -EIO;
  1363. else if (res.err < 0)
  1364. return res.err;
  1365. }
  1366. return 0;
  1367. }
  1368. static int tb_switch_reset_device(struct tb_switch *sw)
  1369. {
  1370. return tb_port_reset(tb_switch_downstream_port(sw));
  1371. }
  1372. static bool tb_switch_enumerated(struct tb_switch *sw)
  1373. {
  1374. u32 val;
  1375. int ret;
  1376. /*
  1377. * Read directly from the hardware because we use this also
  1378. * during system sleep where sw->config.enabled is already set
  1379. * by us.
  1380. */
  1381. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_3, 1);
  1382. if (ret)
  1383. return false;
  1384. return !!(val & ROUTER_CS_3_V);
  1385. }
  1386. /**
  1387. * tb_switch_reset() - Perform reset to the router
  1388. * @sw: Router to reset
  1389. *
  1390. * Issues reset to the router @sw. Can be used for any router. For host
  1391. * routers, resets all the downstream ports and cleans up path config
  1392. * spaces accordingly. For device routers issues downstream port reset
  1393. * through the parent router, so as side effect there will be unplug
  1394. * soon after this is finished.
  1395. *
  1396. * If the router is not enumerated does nothing.
  1397. *
  1398. * Returns %0 on success or negative errno in case of failure.
  1399. */
  1400. int tb_switch_reset(struct tb_switch *sw)
  1401. {
  1402. int ret;
  1403. /*
  1404. * We cannot access the port config spaces unless the router is
  1405. * already enumerated. If the router is not enumerated it is
  1406. * equal to being reset so we can skip that here.
  1407. */
  1408. if (!tb_switch_enumerated(sw))
  1409. return 0;
  1410. tb_sw_dbg(sw, "resetting\n");
  1411. if (tb_route(sw))
  1412. ret = tb_switch_reset_device(sw);
  1413. else
  1414. ret = tb_switch_reset_host(sw);
  1415. if (ret)
  1416. tb_sw_warn(sw, "failed to reset\n");
  1417. return ret;
  1418. }
  1419. /**
  1420. * tb_switch_wait_for_bit() - Wait for specified value of bits in offset
  1421. * @sw: Router to read the offset value from
  1422. * @offset: Offset in the router config space to read from
  1423. * @bit: Bit mask in the offset to wait for
  1424. * @value: Value of the bits to wait for
  1425. * @timeout_msec: Timeout in ms how long to wait
  1426. *
  1427. * Wait till the specified bits in specified offset reach specified value.
  1428. * Returns %0 in case of success, %-ETIMEDOUT if the @value was not reached
  1429. * within the given timeout or a negative errno in case of failure.
  1430. */
  1431. int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
  1432. u32 value, int timeout_msec)
  1433. {
  1434. ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
  1435. do {
  1436. u32 val;
  1437. int ret;
  1438. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
  1439. if (ret)
  1440. return ret;
  1441. if ((val & bit) == value)
  1442. return 0;
  1443. usleep_range(50, 100);
  1444. } while (ktime_before(ktime_get(), timeout));
  1445. return -ETIMEDOUT;
  1446. }
  1447. /*
  1448. * tb_plug_events_active() - enable/disable plug events on a switch
  1449. *
  1450. * Also configures a sane plug_events_delay of 255ms.
  1451. *
  1452. * Return: Returns 0 on success or an error code on failure.
  1453. */
  1454. static int tb_plug_events_active(struct tb_switch *sw, bool active)
  1455. {
  1456. u32 data;
  1457. int res;
  1458. if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
  1459. return 0;
  1460. sw->config.plug_events_delay = 0xff;
  1461. res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
  1462. if (res)
  1463. return res;
  1464. res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
  1465. if (res)
  1466. return res;
  1467. if (active) {
  1468. data = data & 0xFFFFFF83;
  1469. switch (sw->config.device_id) {
  1470. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  1471. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  1472. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  1473. break;
  1474. default:
  1475. /*
  1476. * Skip Alpine Ridge, it needs to have vendor
  1477. * specific USB hotplug event enabled for the
  1478. * internal xHCI to work.
  1479. */
  1480. if (!tb_switch_is_alpine_ridge(sw))
  1481. data |= TB_PLUG_EVENTS_USB_DISABLE;
  1482. }
  1483. } else {
  1484. data = data | 0x7c;
  1485. }
  1486. return tb_sw_write(sw, &data, TB_CFG_SWITCH,
  1487. sw->cap_plug_events + 1, 1);
  1488. }
  1489. static ssize_t authorized_show(struct device *dev,
  1490. struct device_attribute *attr,
  1491. char *buf)
  1492. {
  1493. struct tb_switch *sw = tb_to_switch(dev);
  1494. return sysfs_emit(buf, "%u\n", sw->authorized);
  1495. }
  1496. static int disapprove_switch(struct device *dev, void *not_used)
  1497. {
  1498. char *envp[] = { "AUTHORIZED=0", NULL };
  1499. struct tb_switch *sw;
  1500. sw = tb_to_switch(dev);
  1501. if (sw && sw->authorized) {
  1502. int ret;
  1503. /* First children */
  1504. ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch);
  1505. if (ret)
  1506. return ret;
  1507. ret = tb_domain_disapprove_switch(sw->tb, sw);
  1508. if (ret)
  1509. return ret;
  1510. sw->authorized = 0;
  1511. kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
  1512. }
  1513. return 0;
  1514. }
  1515. static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
  1516. {
  1517. char envp_string[13];
  1518. int ret = -EINVAL;
  1519. char *envp[] = { envp_string, NULL };
  1520. if (!mutex_trylock(&sw->tb->lock))
  1521. return restart_syscall();
  1522. if (!!sw->authorized == !!val)
  1523. goto unlock;
  1524. switch (val) {
  1525. /* Disapprove switch */
  1526. case 0:
  1527. if (tb_route(sw)) {
  1528. ret = disapprove_switch(&sw->dev, NULL);
  1529. goto unlock;
  1530. }
  1531. break;
  1532. /* Approve switch */
  1533. case 1:
  1534. if (sw->key)
  1535. ret = tb_domain_approve_switch_key(sw->tb, sw);
  1536. else
  1537. ret = tb_domain_approve_switch(sw->tb, sw);
  1538. break;
  1539. /* Challenge switch */
  1540. case 2:
  1541. if (sw->key)
  1542. ret = tb_domain_challenge_switch_key(sw->tb, sw);
  1543. break;
  1544. default:
  1545. break;
  1546. }
  1547. if (!ret) {
  1548. sw->authorized = val;
  1549. /*
  1550. * Notify status change to the userspace, informing the new
  1551. * value of /sys/bus/thunderbolt/devices/.../authorized.
  1552. */
  1553. sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
  1554. kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
  1555. }
  1556. unlock:
  1557. mutex_unlock(&sw->tb->lock);
  1558. return ret;
  1559. }
  1560. static ssize_t authorized_store(struct device *dev,
  1561. struct device_attribute *attr,
  1562. const char *buf, size_t count)
  1563. {
  1564. struct tb_switch *sw = tb_to_switch(dev);
  1565. unsigned int val;
  1566. ssize_t ret;
  1567. ret = kstrtouint(buf, 0, &val);
  1568. if (ret)
  1569. return ret;
  1570. if (val > 2)
  1571. return -EINVAL;
  1572. pm_runtime_get_sync(&sw->dev);
  1573. ret = tb_switch_set_authorized(sw, val);
  1574. pm_runtime_mark_last_busy(&sw->dev);
  1575. pm_runtime_put_autosuspend(&sw->dev);
  1576. return ret ? ret : count;
  1577. }
  1578. static DEVICE_ATTR_RW(authorized);
  1579. static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
  1580. char *buf)
  1581. {
  1582. struct tb_switch *sw = tb_to_switch(dev);
  1583. return sysfs_emit(buf, "%u\n", sw->boot);
  1584. }
  1585. static DEVICE_ATTR_RO(boot);
  1586. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  1587. char *buf)
  1588. {
  1589. struct tb_switch *sw = tb_to_switch(dev);
  1590. return sysfs_emit(buf, "%#x\n", sw->device);
  1591. }
  1592. static DEVICE_ATTR_RO(device);
  1593. static ssize_t
  1594. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1595. {
  1596. struct tb_switch *sw = tb_to_switch(dev);
  1597. return sysfs_emit(buf, "%s\n", sw->device_name ?: "");
  1598. }
  1599. static DEVICE_ATTR_RO(device_name);
  1600. static ssize_t
  1601. generation_show(struct device *dev, struct device_attribute *attr, char *buf)
  1602. {
  1603. struct tb_switch *sw = tb_to_switch(dev);
  1604. return sysfs_emit(buf, "%u\n", sw->generation);
  1605. }
  1606. static DEVICE_ATTR_RO(generation);
  1607. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  1608. char *buf)
  1609. {
  1610. struct tb_switch *sw = tb_to_switch(dev);
  1611. ssize_t ret;
  1612. if (!mutex_trylock(&sw->tb->lock))
  1613. return restart_syscall();
  1614. if (sw->key)
  1615. ret = sysfs_emit(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
  1616. else
  1617. ret = sysfs_emit(buf, "\n");
  1618. mutex_unlock(&sw->tb->lock);
  1619. return ret;
  1620. }
  1621. static ssize_t key_store(struct device *dev, struct device_attribute *attr,
  1622. const char *buf, size_t count)
  1623. {
  1624. struct tb_switch *sw = tb_to_switch(dev);
  1625. u8 key[TB_SWITCH_KEY_SIZE];
  1626. ssize_t ret = count;
  1627. bool clear = false;
  1628. if (!strcmp(buf, "\n"))
  1629. clear = true;
  1630. else if (hex2bin(key, buf, sizeof(key)))
  1631. return -EINVAL;
  1632. if (!mutex_trylock(&sw->tb->lock))
  1633. return restart_syscall();
  1634. if (sw->authorized) {
  1635. ret = -EBUSY;
  1636. } else {
  1637. kfree(sw->key);
  1638. if (clear) {
  1639. sw->key = NULL;
  1640. } else {
  1641. sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
  1642. if (!sw->key)
  1643. ret = -ENOMEM;
  1644. }
  1645. }
  1646. mutex_unlock(&sw->tb->lock);
  1647. return ret;
  1648. }
  1649. static DEVICE_ATTR(key, 0600, key_show, key_store);
  1650. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  1651. char *buf)
  1652. {
  1653. struct tb_switch *sw = tb_to_switch(dev);
  1654. return sysfs_emit(buf, "%u.0 Gb/s\n", sw->link_speed);
  1655. }
  1656. /*
  1657. * Currently all lanes must run at the same speed but we expose here
  1658. * both directions to allow possible asymmetric links in the future.
  1659. */
  1660. static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
  1661. static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
  1662. static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
  1663. char *buf)
  1664. {
  1665. struct tb_switch *sw = tb_to_switch(dev);
  1666. unsigned int width;
  1667. switch (sw->link_width) {
  1668. case TB_LINK_WIDTH_SINGLE:
  1669. case TB_LINK_WIDTH_ASYM_TX:
  1670. width = 1;
  1671. break;
  1672. case TB_LINK_WIDTH_DUAL:
  1673. width = 2;
  1674. break;
  1675. case TB_LINK_WIDTH_ASYM_RX:
  1676. width = 3;
  1677. break;
  1678. default:
  1679. WARN_ON_ONCE(1);
  1680. return -EINVAL;
  1681. }
  1682. return sysfs_emit(buf, "%u\n", width);
  1683. }
  1684. static DEVICE_ATTR(rx_lanes, 0444, rx_lanes_show, NULL);
  1685. static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
  1686. char *buf)
  1687. {
  1688. struct tb_switch *sw = tb_to_switch(dev);
  1689. unsigned int width;
  1690. switch (sw->link_width) {
  1691. case TB_LINK_WIDTH_SINGLE:
  1692. case TB_LINK_WIDTH_ASYM_RX:
  1693. width = 1;
  1694. break;
  1695. case TB_LINK_WIDTH_DUAL:
  1696. width = 2;
  1697. break;
  1698. case TB_LINK_WIDTH_ASYM_TX:
  1699. width = 3;
  1700. break;
  1701. default:
  1702. WARN_ON_ONCE(1);
  1703. return -EINVAL;
  1704. }
  1705. return sysfs_emit(buf, "%u\n", width);
  1706. }
  1707. static DEVICE_ATTR(tx_lanes, 0444, tx_lanes_show, NULL);
  1708. static ssize_t nvm_authenticate_show(struct device *dev,
  1709. struct device_attribute *attr, char *buf)
  1710. {
  1711. struct tb_switch *sw = tb_to_switch(dev);
  1712. u32 status;
  1713. nvm_get_auth_status(sw, &status);
  1714. return sysfs_emit(buf, "%#x\n", status);
  1715. }
  1716. static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf,
  1717. bool disconnect)
  1718. {
  1719. struct tb_switch *sw = tb_to_switch(dev);
  1720. int val, ret;
  1721. pm_runtime_get_sync(&sw->dev);
  1722. if (!mutex_trylock(&sw->tb->lock)) {
  1723. ret = restart_syscall();
  1724. goto exit_rpm;
  1725. }
  1726. if (sw->no_nvm_upgrade) {
  1727. ret = -EOPNOTSUPP;
  1728. goto exit_unlock;
  1729. }
  1730. /* If NVMem devices are not yet added */
  1731. if (!sw->nvm) {
  1732. ret = -EAGAIN;
  1733. goto exit_unlock;
  1734. }
  1735. ret = kstrtoint(buf, 10, &val);
  1736. if (ret)
  1737. goto exit_unlock;
  1738. /* Always clear the authentication status */
  1739. nvm_clear_auth_status(sw);
  1740. if (val > 0) {
  1741. if (val == AUTHENTICATE_ONLY) {
  1742. if (disconnect)
  1743. ret = -EINVAL;
  1744. else
  1745. ret = nvm_authenticate(sw, true);
  1746. } else {
  1747. if (!sw->nvm->flushed) {
  1748. if (!sw->nvm->buf) {
  1749. ret = -EINVAL;
  1750. goto exit_unlock;
  1751. }
  1752. ret = nvm_validate_and_write(sw);
  1753. if (ret || val == WRITE_ONLY)
  1754. goto exit_unlock;
  1755. }
  1756. if (val == WRITE_AND_AUTHENTICATE) {
  1757. if (disconnect)
  1758. ret = tb_lc_force_power(sw);
  1759. else
  1760. ret = nvm_authenticate(sw, false);
  1761. }
  1762. }
  1763. }
  1764. exit_unlock:
  1765. mutex_unlock(&sw->tb->lock);
  1766. exit_rpm:
  1767. pm_runtime_mark_last_busy(&sw->dev);
  1768. pm_runtime_put_autosuspend(&sw->dev);
  1769. return ret;
  1770. }
  1771. static ssize_t nvm_authenticate_store(struct device *dev,
  1772. struct device_attribute *attr, const char *buf, size_t count)
  1773. {
  1774. int ret = nvm_authenticate_sysfs(dev, buf, false);
  1775. if (ret)
  1776. return ret;
  1777. return count;
  1778. }
  1779. static DEVICE_ATTR_RW(nvm_authenticate);
  1780. static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev,
  1781. struct device_attribute *attr, char *buf)
  1782. {
  1783. return nvm_authenticate_show(dev, attr, buf);
  1784. }
  1785. static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev,
  1786. struct device_attribute *attr, const char *buf, size_t count)
  1787. {
  1788. int ret;
  1789. ret = nvm_authenticate_sysfs(dev, buf, true);
  1790. return ret ? ret : count;
  1791. }
  1792. static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect);
  1793. static ssize_t nvm_version_show(struct device *dev,
  1794. struct device_attribute *attr, char *buf)
  1795. {
  1796. struct tb_switch *sw = tb_to_switch(dev);
  1797. int ret;
  1798. if (!mutex_trylock(&sw->tb->lock))
  1799. return restart_syscall();
  1800. if (sw->safe_mode)
  1801. ret = -ENODATA;
  1802. else if (!sw->nvm)
  1803. ret = -EAGAIN;
  1804. else
  1805. ret = sysfs_emit(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
  1806. mutex_unlock(&sw->tb->lock);
  1807. return ret;
  1808. }
  1809. static DEVICE_ATTR_RO(nvm_version);
  1810. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  1811. char *buf)
  1812. {
  1813. struct tb_switch *sw = tb_to_switch(dev);
  1814. return sysfs_emit(buf, "%#x\n", sw->vendor);
  1815. }
  1816. static DEVICE_ATTR_RO(vendor);
  1817. static ssize_t
  1818. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1819. {
  1820. struct tb_switch *sw = tb_to_switch(dev);
  1821. return sysfs_emit(buf, "%s\n", sw->vendor_name ?: "");
  1822. }
  1823. static DEVICE_ATTR_RO(vendor_name);
  1824. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  1825. char *buf)
  1826. {
  1827. struct tb_switch *sw = tb_to_switch(dev);
  1828. return sysfs_emit(buf, "%pUb\n", sw->uuid);
  1829. }
  1830. static DEVICE_ATTR_RO(unique_id);
  1831. static struct attribute *switch_attrs[] = {
  1832. &dev_attr_authorized.attr,
  1833. &dev_attr_boot.attr,
  1834. &dev_attr_device.attr,
  1835. &dev_attr_device_name.attr,
  1836. &dev_attr_generation.attr,
  1837. &dev_attr_key.attr,
  1838. &dev_attr_nvm_authenticate.attr,
  1839. &dev_attr_nvm_authenticate_on_disconnect.attr,
  1840. &dev_attr_nvm_version.attr,
  1841. &dev_attr_rx_speed.attr,
  1842. &dev_attr_rx_lanes.attr,
  1843. &dev_attr_tx_speed.attr,
  1844. &dev_attr_tx_lanes.attr,
  1845. &dev_attr_vendor.attr,
  1846. &dev_attr_vendor_name.attr,
  1847. &dev_attr_unique_id.attr,
  1848. NULL,
  1849. };
  1850. static umode_t switch_attr_is_visible(struct kobject *kobj,
  1851. struct attribute *attr, int n)
  1852. {
  1853. struct device *dev = kobj_to_dev(kobj);
  1854. struct tb_switch *sw = tb_to_switch(dev);
  1855. if (attr == &dev_attr_authorized.attr) {
  1856. if (sw->tb->security_level == TB_SECURITY_NOPCIE ||
  1857. sw->tb->security_level == TB_SECURITY_DPONLY)
  1858. return 0;
  1859. } else if (attr == &dev_attr_device.attr) {
  1860. if (!sw->device)
  1861. return 0;
  1862. } else if (attr == &dev_attr_device_name.attr) {
  1863. if (!sw->device_name)
  1864. return 0;
  1865. } else if (attr == &dev_attr_vendor.attr) {
  1866. if (!sw->vendor)
  1867. return 0;
  1868. } else if (attr == &dev_attr_vendor_name.attr) {
  1869. if (!sw->vendor_name)
  1870. return 0;
  1871. } else if (attr == &dev_attr_key.attr) {
  1872. if (tb_route(sw) &&
  1873. sw->tb->security_level == TB_SECURITY_SECURE &&
  1874. sw->security_level == TB_SECURITY_SECURE)
  1875. return attr->mode;
  1876. return 0;
  1877. } else if (attr == &dev_attr_rx_speed.attr ||
  1878. attr == &dev_attr_rx_lanes.attr ||
  1879. attr == &dev_attr_tx_speed.attr ||
  1880. attr == &dev_attr_tx_lanes.attr) {
  1881. if (tb_route(sw))
  1882. return attr->mode;
  1883. return 0;
  1884. } else if (attr == &dev_attr_nvm_authenticate.attr) {
  1885. if (nvm_upgradeable(sw))
  1886. return attr->mode;
  1887. return 0;
  1888. } else if (attr == &dev_attr_nvm_version.attr) {
  1889. if (nvm_readable(sw))
  1890. return attr->mode;
  1891. return 0;
  1892. } else if (attr == &dev_attr_boot.attr) {
  1893. if (tb_route(sw))
  1894. return attr->mode;
  1895. return 0;
  1896. } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) {
  1897. if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
  1898. return attr->mode;
  1899. return 0;
  1900. }
  1901. return sw->safe_mode ? 0 : attr->mode;
  1902. }
  1903. static const struct attribute_group switch_group = {
  1904. .is_visible = switch_attr_is_visible,
  1905. .attrs = switch_attrs,
  1906. };
  1907. static const struct attribute_group *switch_groups[] = {
  1908. &switch_group,
  1909. NULL,
  1910. };
  1911. static void tb_switch_release(struct device *dev)
  1912. {
  1913. struct tb_switch *sw = tb_to_switch(dev);
  1914. struct tb_port *port;
  1915. dma_port_free(sw->dma_port);
  1916. tb_switch_for_each_port(sw, port) {
  1917. ida_destroy(&port->in_hopids);
  1918. ida_destroy(&port->out_hopids);
  1919. }
  1920. kfree(sw->uuid);
  1921. kfree(sw->device_name);
  1922. kfree(sw->vendor_name);
  1923. kfree(sw->ports);
  1924. kfree(sw->drom);
  1925. kfree(sw->key);
  1926. kfree(sw);
  1927. }
  1928. static int tb_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1929. {
  1930. const struct tb_switch *sw = tb_to_switch(dev);
  1931. const char *type;
  1932. if (tb_switch_is_usb4(sw)) {
  1933. if (add_uevent_var(env, "USB4_VERSION=%u.0",
  1934. usb4_switch_version(sw)))
  1935. return -ENOMEM;
  1936. }
  1937. if (!tb_route(sw)) {
  1938. type = "host";
  1939. } else {
  1940. const struct tb_port *port;
  1941. bool hub = false;
  1942. /* Device is hub if it has any downstream ports */
  1943. tb_switch_for_each_port(sw, port) {
  1944. if (!port->disabled && !tb_is_upstream_port(port) &&
  1945. tb_port_is_null(port)) {
  1946. hub = true;
  1947. break;
  1948. }
  1949. }
  1950. type = hub ? "hub" : "device";
  1951. }
  1952. if (add_uevent_var(env, "USB4_TYPE=%s", type))
  1953. return -ENOMEM;
  1954. return 0;
  1955. }
  1956. /*
  1957. * Currently only need to provide the callbacks. Everything else is handled
  1958. * in the connection manager.
  1959. */
  1960. static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
  1961. {
  1962. struct tb_switch *sw = tb_to_switch(dev);
  1963. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  1964. if (cm_ops->runtime_suspend_switch)
  1965. return cm_ops->runtime_suspend_switch(sw);
  1966. return 0;
  1967. }
  1968. static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
  1969. {
  1970. struct tb_switch *sw = tb_to_switch(dev);
  1971. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  1972. if (cm_ops->runtime_resume_switch)
  1973. return cm_ops->runtime_resume_switch(sw);
  1974. return 0;
  1975. }
  1976. static const struct dev_pm_ops tb_switch_pm_ops = {
  1977. SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
  1978. NULL)
  1979. };
  1980. const struct device_type tb_switch_type = {
  1981. .name = "thunderbolt_device",
  1982. .release = tb_switch_release,
  1983. .uevent = tb_switch_uevent,
  1984. .pm = &tb_switch_pm_ops,
  1985. };
  1986. static int tb_switch_get_generation(struct tb_switch *sw)
  1987. {
  1988. if (tb_switch_is_usb4(sw))
  1989. return 4;
  1990. if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
  1991. switch (sw->config.device_id) {
  1992. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  1993. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  1994. case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
  1995. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
  1996. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  1997. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  1998. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
  1999. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
  2000. return 1;
  2001. case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
  2002. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
  2003. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
  2004. return 2;
  2005. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  2006. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  2007. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  2008. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  2009. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  2010. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  2011. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  2012. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
  2013. case PCI_DEVICE_ID_INTEL_ICL_NHI0:
  2014. case PCI_DEVICE_ID_INTEL_ICL_NHI1:
  2015. return 3;
  2016. }
  2017. }
  2018. /*
  2019. * For unknown switches assume generation to be 1 to be on the
  2020. * safe side.
  2021. */
  2022. tb_sw_warn(sw, "unsupported switch device id %#x\n",
  2023. sw->config.device_id);
  2024. return 1;
  2025. }
  2026. static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
  2027. {
  2028. int max_depth;
  2029. if (tb_switch_is_usb4(sw) ||
  2030. (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
  2031. max_depth = USB4_SWITCH_MAX_DEPTH;
  2032. else
  2033. max_depth = TB_SWITCH_MAX_DEPTH;
  2034. return depth > max_depth;
  2035. }
  2036. /**
  2037. * tb_switch_alloc() - allocate a switch
  2038. * @tb: Pointer to the owning domain
  2039. * @parent: Parent device for this switch
  2040. * @route: Route string for this switch
  2041. *
  2042. * Allocates and initializes a switch. Will not upload configuration to
  2043. * the switch. For that you need to call tb_switch_configure()
  2044. * separately. The returned switch should be released by calling
  2045. * tb_switch_put().
  2046. *
  2047. * Return: Pointer to the allocated switch or ERR_PTR() in case of
  2048. * failure.
  2049. */
  2050. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  2051. u64 route)
  2052. {
  2053. struct tb_switch *sw;
  2054. int upstream_port;
  2055. int i, ret, depth;
  2056. /* Unlock the downstream port so we can access the switch below */
  2057. if (route) {
  2058. struct tb_switch *parent_sw = tb_to_switch(parent);
  2059. struct tb_port *down;
  2060. down = tb_port_at(route, parent_sw);
  2061. tb_port_unlock(down);
  2062. }
  2063. depth = tb_route_length(route);
  2064. upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
  2065. if (upstream_port < 0)
  2066. return ERR_PTR(upstream_port);
  2067. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  2068. if (!sw)
  2069. return ERR_PTR(-ENOMEM);
  2070. sw->tb = tb;
  2071. ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
  2072. if (ret)
  2073. goto err_free_sw_ports;
  2074. sw->generation = tb_switch_get_generation(sw);
  2075. tb_dbg(tb, "current switch config:\n");
  2076. tb_dump_switch(tb, sw);
  2077. /* configure switch */
  2078. sw->config.upstream_port_number = upstream_port;
  2079. sw->config.depth = depth;
  2080. sw->config.route_hi = upper_32_bits(route);
  2081. sw->config.route_lo = lower_32_bits(route);
  2082. sw->config.enabled = 0;
  2083. /* Make sure we do not exceed maximum topology limit */
  2084. if (tb_switch_exceeds_max_depth(sw, depth)) {
  2085. ret = -EADDRNOTAVAIL;
  2086. goto err_free_sw_ports;
  2087. }
  2088. /* initialize ports */
  2089. sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
  2090. GFP_KERNEL);
  2091. if (!sw->ports) {
  2092. ret = -ENOMEM;
  2093. goto err_free_sw_ports;
  2094. }
  2095. for (i = 0; i <= sw->config.max_port_number; i++) {
  2096. /* minimum setup for tb_find_cap and tb_drom_read to work */
  2097. sw->ports[i].sw = sw;
  2098. sw->ports[i].port = i;
  2099. /* Control port does not need HopID allocation */
  2100. if (i) {
  2101. ida_init(&sw->ports[i].in_hopids);
  2102. ida_init(&sw->ports[i].out_hopids);
  2103. }
  2104. }
  2105. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
  2106. if (ret > 0)
  2107. sw->cap_plug_events = ret;
  2108. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_TIME2);
  2109. if (ret > 0)
  2110. sw->cap_vsec_tmu = ret;
  2111. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
  2112. if (ret > 0)
  2113. sw->cap_lc = ret;
  2114. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_CP_LP);
  2115. if (ret > 0)
  2116. sw->cap_lp = ret;
  2117. /* Root switch is always authorized */
  2118. if (!route)
  2119. sw->authorized = true;
  2120. device_initialize(&sw->dev);
  2121. sw->dev.parent = parent;
  2122. sw->dev.bus = &tb_bus_type;
  2123. sw->dev.type = &tb_switch_type;
  2124. sw->dev.groups = switch_groups;
  2125. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  2126. return sw;
  2127. err_free_sw_ports:
  2128. kfree(sw->ports);
  2129. kfree(sw);
  2130. return ERR_PTR(ret);
  2131. }
  2132. /**
  2133. * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
  2134. * @tb: Pointer to the owning domain
  2135. * @parent: Parent device for this switch
  2136. * @route: Route string for this switch
  2137. *
  2138. * This creates a switch in safe mode. This means the switch pretty much
  2139. * lacks all capabilities except DMA configuration port before it is
  2140. * flashed with a valid NVM firmware.
  2141. *
  2142. * The returned switch must be released by calling tb_switch_put().
  2143. *
  2144. * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
  2145. */
  2146. struct tb_switch *
  2147. tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
  2148. {
  2149. struct tb_switch *sw;
  2150. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  2151. if (!sw)
  2152. return ERR_PTR(-ENOMEM);
  2153. sw->tb = tb;
  2154. sw->config.depth = tb_route_length(route);
  2155. sw->config.route_hi = upper_32_bits(route);
  2156. sw->config.route_lo = lower_32_bits(route);
  2157. sw->safe_mode = true;
  2158. device_initialize(&sw->dev);
  2159. sw->dev.parent = parent;
  2160. sw->dev.bus = &tb_bus_type;
  2161. sw->dev.type = &tb_switch_type;
  2162. sw->dev.groups = switch_groups;
  2163. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  2164. return sw;
  2165. }
  2166. /**
  2167. * tb_switch_configure() - Uploads configuration to the switch
  2168. * @sw: Switch to configure
  2169. *
  2170. * Call this function before the switch is added to the system. It will
  2171. * upload configuration to the switch and makes it available for the
  2172. * connection manager to use. Can be called to the switch again after
  2173. * resume from low power states to re-initialize it.
  2174. *
  2175. * Return: %0 in case of success and negative errno in case of failure
  2176. */
  2177. int tb_switch_configure(struct tb_switch *sw)
  2178. {
  2179. struct tb *tb = sw->tb;
  2180. u64 route;
  2181. int ret;
  2182. route = tb_route(sw);
  2183. tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
  2184. sw->config.enabled ? "restoring" : "initializing", route,
  2185. tb_route_length(route), sw->config.upstream_port_number);
  2186. sw->config.enabled = 1;
  2187. if (tb_switch_is_usb4(sw)) {
  2188. /*
  2189. * For USB4 devices, we need to program the CM version
  2190. * accordingly so that it knows to expose all the
  2191. * additional capabilities. Program it according to USB4
  2192. * version to avoid changing existing (v1) routers behaviour.
  2193. */
  2194. if (usb4_switch_version(sw) < 2)
  2195. sw->config.cmuv = ROUTER_CS_4_CMUV_V1;
  2196. else
  2197. sw->config.cmuv = ROUTER_CS_4_CMUV_V2;
  2198. sw->config.plug_events_delay = 0xa;
  2199. /* Enumerate the switch */
  2200. ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
  2201. ROUTER_CS_1, 4);
  2202. if (ret)
  2203. return ret;
  2204. ret = usb4_switch_setup(sw);
  2205. } else {
  2206. if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
  2207. tb_sw_warn(sw, "unknown switch vendor id %#x\n",
  2208. sw->config.vendor_id);
  2209. if (!sw->cap_plug_events) {
  2210. tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
  2211. return -ENODEV;
  2212. }
  2213. /* Enumerate the switch */
  2214. ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
  2215. ROUTER_CS_1, 3);
  2216. }
  2217. if (ret)
  2218. return ret;
  2219. return tb_plug_events_active(sw, true);
  2220. }
  2221. /**
  2222. * tb_switch_configuration_valid() - Set the tunneling configuration to be valid
  2223. * @sw: Router to configure
  2224. *
  2225. * Needs to be called before any tunnels can be setup through the
  2226. * router. Can be called to any router.
  2227. *
  2228. * Returns %0 in success and negative errno otherwise.
  2229. */
  2230. int tb_switch_configuration_valid(struct tb_switch *sw)
  2231. {
  2232. if (tb_switch_is_usb4(sw))
  2233. return usb4_switch_configuration_valid(sw);
  2234. return 0;
  2235. }
  2236. static int tb_switch_set_uuid(struct tb_switch *sw)
  2237. {
  2238. bool uid = false;
  2239. u32 uuid[4];
  2240. int ret;
  2241. if (sw->uuid)
  2242. return 0;
  2243. if (tb_switch_is_usb4(sw)) {
  2244. ret = usb4_switch_read_uid(sw, &sw->uid);
  2245. if (ret)
  2246. return ret;
  2247. uid = true;
  2248. } else {
  2249. /*
  2250. * The newer controllers include fused UUID as part of
  2251. * link controller specific registers
  2252. */
  2253. ret = tb_lc_read_uuid(sw, uuid);
  2254. if (ret) {
  2255. if (ret != -EINVAL)
  2256. return ret;
  2257. uid = true;
  2258. }
  2259. }
  2260. if (uid) {
  2261. /*
  2262. * ICM generates UUID based on UID and fills the upper
  2263. * two words with ones. This is not strictly following
  2264. * UUID format but we want to be compatible with it so
  2265. * we do the same here.
  2266. */
  2267. uuid[0] = sw->uid & 0xffffffff;
  2268. uuid[1] = (sw->uid >> 32) & 0xffffffff;
  2269. uuid[2] = 0xffffffff;
  2270. uuid[3] = 0xffffffff;
  2271. }
  2272. sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  2273. if (!sw->uuid)
  2274. return -ENOMEM;
  2275. return 0;
  2276. }
  2277. static int tb_switch_add_dma_port(struct tb_switch *sw)
  2278. {
  2279. u32 status;
  2280. int ret;
  2281. switch (sw->generation) {
  2282. case 2:
  2283. /* Only root switch can be upgraded */
  2284. if (tb_route(sw))
  2285. return 0;
  2286. fallthrough;
  2287. case 3:
  2288. case 4:
  2289. ret = tb_switch_set_uuid(sw);
  2290. if (ret)
  2291. return ret;
  2292. break;
  2293. default:
  2294. /*
  2295. * DMA port is the only thing available when the switch
  2296. * is in safe mode.
  2297. */
  2298. if (!sw->safe_mode)
  2299. return 0;
  2300. break;
  2301. }
  2302. if (sw->no_nvm_upgrade)
  2303. return 0;
  2304. if (tb_switch_is_usb4(sw)) {
  2305. ret = usb4_switch_nvm_authenticate_status(sw, &status);
  2306. if (ret)
  2307. return ret;
  2308. if (status) {
  2309. tb_sw_info(sw, "switch flash authentication failed\n");
  2310. nvm_set_auth_status(sw, status);
  2311. }
  2312. return 0;
  2313. }
  2314. /* Root switch DMA port requires running firmware */
  2315. if (!tb_route(sw) && !tb_switch_is_icm(sw))
  2316. return 0;
  2317. sw->dma_port = dma_port_alloc(sw);
  2318. if (!sw->dma_port)
  2319. return 0;
  2320. /*
  2321. * If there is status already set then authentication failed
  2322. * when the dma_port_flash_update_auth() returned. Power cycling
  2323. * is not needed (it was done already) so only thing we do here
  2324. * is to unblock runtime PM of the root port.
  2325. */
  2326. nvm_get_auth_status(sw, &status);
  2327. if (status) {
  2328. if (!tb_route(sw))
  2329. nvm_authenticate_complete_dma_port(sw);
  2330. return 0;
  2331. }
  2332. /*
  2333. * Check status of the previous flash authentication. If there
  2334. * is one we need to power cycle the switch in any case to make
  2335. * it functional again.
  2336. */
  2337. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  2338. if (ret <= 0)
  2339. return ret;
  2340. /* Now we can allow root port to suspend again */
  2341. if (!tb_route(sw))
  2342. nvm_authenticate_complete_dma_port(sw);
  2343. if (status) {
  2344. tb_sw_info(sw, "switch flash authentication failed\n");
  2345. nvm_set_auth_status(sw, status);
  2346. }
  2347. tb_sw_info(sw, "power cycling the switch now\n");
  2348. dma_port_power_cycle(sw->dma_port);
  2349. /*
  2350. * We return error here which causes the switch adding failure.
  2351. * It should appear back after power cycle is complete.
  2352. */
  2353. return -ESHUTDOWN;
  2354. }
  2355. static void tb_switch_default_link_ports(struct tb_switch *sw)
  2356. {
  2357. int i;
  2358. for (i = 1; i <= sw->config.max_port_number; i++) {
  2359. struct tb_port *port = &sw->ports[i];
  2360. struct tb_port *subordinate;
  2361. if (!tb_port_is_null(port))
  2362. continue;
  2363. /* Check for the subordinate port */
  2364. if (i == sw->config.max_port_number ||
  2365. !tb_port_is_null(&sw->ports[i + 1]))
  2366. continue;
  2367. /* Link them if not already done so (by DROM) */
  2368. subordinate = &sw->ports[i + 1];
  2369. if (!port->dual_link_port && !subordinate->dual_link_port) {
  2370. port->link_nr = 0;
  2371. port->dual_link_port = subordinate;
  2372. subordinate->link_nr = 1;
  2373. subordinate->dual_link_port = port;
  2374. tb_sw_dbg(sw, "linked ports %d <-> %d\n",
  2375. port->port, subordinate->port);
  2376. }
  2377. }
  2378. }
  2379. static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
  2380. {
  2381. const struct tb_port *up = tb_upstream_port(sw);
  2382. if (!up->dual_link_port || !up->dual_link_port->remote)
  2383. return false;
  2384. if (tb_switch_is_usb4(sw))
  2385. return usb4_switch_lane_bonding_possible(sw);
  2386. return tb_lc_lane_bonding_possible(sw);
  2387. }
  2388. static int tb_switch_update_link_attributes(struct tb_switch *sw)
  2389. {
  2390. struct tb_port *up;
  2391. bool change = false;
  2392. int ret;
  2393. if (!tb_route(sw) || tb_switch_is_icm(sw))
  2394. return 0;
  2395. up = tb_upstream_port(sw);
  2396. ret = tb_port_get_link_speed(up);
  2397. if (ret < 0)
  2398. return ret;
  2399. if (sw->link_speed != ret)
  2400. change = true;
  2401. sw->link_speed = ret;
  2402. ret = tb_port_get_link_width(up);
  2403. if (ret < 0)
  2404. return ret;
  2405. if (sw->link_width != ret)
  2406. change = true;
  2407. sw->link_width = ret;
  2408. /* Notify userspace that there is possible link attribute change */
  2409. if (device_is_registered(&sw->dev) && change)
  2410. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  2411. return 0;
  2412. }
  2413. /* Must be called after tb_switch_update_link_attributes() */
  2414. static void tb_switch_link_init(struct tb_switch *sw)
  2415. {
  2416. struct tb_port *up, *down;
  2417. bool bonded;
  2418. if (!tb_route(sw) || tb_switch_is_icm(sw))
  2419. return;
  2420. tb_sw_dbg(sw, "current link speed %u.0 Gb/s\n", sw->link_speed);
  2421. tb_sw_dbg(sw, "current link width %s\n", tb_width_name(sw->link_width));
  2422. bonded = sw->link_width >= TB_LINK_WIDTH_DUAL;
  2423. /*
  2424. * Gen 4 links come up as bonded so update the port structures
  2425. * accordingly.
  2426. */
  2427. up = tb_upstream_port(sw);
  2428. down = tb_switch_downstream_port(sw);
  2429. up->bonded = bonded;
  2430. if (up->dual_link_port)
  2431. up->dual_link_port->bonded = bonded;
  2432. tb_port_update_credits(up);
  2433. down->bonded = bonded;
  2434. if (down->dual_link_port)
  2435. down->dual_link_port->bonded = bonded;
  2436. tb_port_update_credits(down);
  2437. if (tb_port_get_link_generation(up) < 4)
  2438. return;
  2439. /*
  2440. * Set the Gen 4 preferred link width. This is what the router
  2441. * prefers when the link is brought up. If the router does not
  2442. * support asymmetric link configuration, this also will be set
  2443. * to TB_LINK_WIDTH_DUAL.
  2444. */
  2445. sw->preferred_link_width = sw->link_width;
  2446. tb_sw_dbg(sw, "preferred link width %s\n",
  2447. tb_width_name(sw->preferred_link_width));
  2448. }
  2449. /**
  2450. * tb_switch_lane_bonding_enable() - Enable lane bonding
  2451. * @sw: Switch to enable lane bonding
  2452. *
  2453. * Connection manager can call this function to enable lane bonding of a
  2454. * switch. If conditions are correct and both switches support the feature,
  2455. * lanes are bonded. It is safe to call this to any switch.
  2456. */
  2457. static int tb_switch_lane_bonding_enable(struct tb_switch *sw)
  2458. {
  2459. struct tb_port *up, *down;
  2460. unsigned int width;
  2461. int ret;
  2462. if (!tb_switch_lane_bonding_possible(sw))
  2463. return 0;
  2464. up = tb_upstream_port(sw);
  2465. down = tb_switch_downstream_port(sw);
  2466. if (!tb_port_width_supported(up, TB_LINK_WIDTH_DUAL) ||
  2467. !tb_port_width_supported(down, TB_LINK_WIDTH_DUAL))
  2468. return 0;
  2469. /*
  2470. * Both lanes need to be in CL0. Here we assume lane 0 already be in
  2471. * CL0 and check just for lane 1.
  2472. */
  2473. if (tb_wait_for_port(down->dual_link_port, false) <= 0)
  2474. return -ENOTCONN;
  2475. ret = tb_port_lane_bonding_enable(up);
  2476. if (ret) {
  2477. tb_port_warn(up, "failed to enable lane bonding\n");
  2478. return ret;
  2479. }
  2480. ret = tb_port_lane_bonding_enable(down);
  2481. if (ret) {
  2482. tb_port_warn(down, "failed to enable lane bonding\n");
  2483. tb_port_lane_bonding_disable(up);
  2484. return ret;
  2485. }
  2486. /* Any of the widths are all bonded */
  2487. width = TB_LINK_WIDTH_DUAL | TB_LINK_WIDTH_ASYM_TX |
  2488. TB_LINK_WIDTH_ASYM_RX;
  2489. return tb_port_wait_for_link_width(down, width, 100);
  2490. }
  2491. /**
  2492. * tb_switch_lane_bonding_disable() - Disable lane bonding
  2493. * @sw: Switch whose lane bonding to disable
  2494. *
  2495. * Disables lane bonding between @sw and parent. This can be called even
  2496. * if lanes were not bonded originally.
  2497. */
  2498. static int tb_switch_lane_bonding_disable(struct tb_switch *sw)
  2499. {
  2500. struct tb_port *up, *down;
  2501. int ret;
  2502. up = tb_upstream_port(sw);
  2503. if (!up->bonded)
  2504. return 0;
  2505. /*
  2506. * If the link is Gen 4 there is no way to switch the link to
  2507. * two single lane links so avoid that here. Also don't bother
  2508. * if the link is not up anymore (sw is unplugged).
  2509. */
  2510. ret = tb_port_get_link_generation(up);
  2511. if (ret < 0)
  2512. return ret;
  2513. if (ret >= 4)
  2514. return -EOPNOTSUPP;
  2515. down = tb_switch_downstream_port(sw);
  2516. tb_port_lane_bonding_disable(up);
  2517. tb_port_lane_bonding_disable(down);
  2518. /*
  2519. * It is fine if we get other errors as the router might have
  2520. * been unplugged.
  2521. */
  2522. return tb_port_wait_for_link_width(down, TB_LINK_WIDTH_SINGLE, 100);
  2523. }
  2524. /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
  2525. static int tb_switch_asym_enable(struct tb_switch *sw, enum tb_link_width width)
  2526. {
  2527. struct tb_port *up, *down, *port;
  2528. enum tb_link_width down_width;
  2529. int ret;
  2530. up = tb_upstream_port(sw);
  2531. down = tb_switch_downstream_port(sw);
  2532. if (width == TB_LINK_WIDTH_ASYM_TX) {
  2533. down_width = TB_LINK_WIDTH_ASYM_RX;
  2534. port = down;
  2535. } else {
  2536. down_width = TB_LINK_WIDTH_ASYM_TX;
  2537. port = up;
  2538. }
  2539. ret = tb_port_set_link_width(up, width);
  2540. if (ret)
  2541. return ret;
  2542. ret = tb_port_set_link_width(down, down_width);
  2543. if (ret)
  2544. return ret;
  2545. /*
  2546. * Initiate the change in the router that one of its TX lanes is
  2547. * changing to RX but do so only if there is an actual change.
  2548. */
  2549. if (sw->link_width != width) {
  2550. ret = usb4_port_asym_start(port);
  2551. if (ret)
  2552. return ret;
  2553. ret = tb_port_wait_for_link_width(up, width, 100);
  2554. if (ret)
  2555. return ret;
  2556. }
  2557. return 0;
  2558. }
  2559. /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
  2560. static int tb_switch_asym_disable(struct tb_switch *sw)
  2561. {
  2562. struct tb_port *up, *down;
  2563. int ret;
  2564. up = tb_upstream_port(sw);
  2565. down = tb_switch_downstream_port(sw);
  2566. ret = tb_port_set_link_width(up, TB_LINK_WIDTH_DUAL);
  2567. if (ret)
  2568. return ret;
  2569. ret = tb_port_set_link_width(down, TB_LINK_WIDTH_DUAL);
  2570. if (ret)
  2571. return ret;
  2572. /*
  2573. * Initiate the change in the router that has three TX lanes and
  2574. * is changing one of its TX lanes to RX but only if there is a
  2575. * change in the link width.
  2576. */
  2577. if (sw->link_width > TB_LINK_WIDTH_DUAL) {
  2578. if (sw->link_width == TB_LINK_WIDTH_ASYM_TX)
  2579. ret = usb4_port_asym_start(up);
  2580. else
  2581. ret = usb4_port_asym_start(down);
  2582. if (ret)
  2583. return ret;
  2584. ret = tb_port_wait_for_link_width(up, TB_LINK_WIDTH_DUAL, 100);
  2585. if (ret)
  2586. return ret;
  2587. }
  2588. return 0;
  2589. }
  2590. /**
  2591. * tb_switch_set_link_width() - Configure router link width
  2592. * @sw: Router to configure
  2593. * @width: The new link width
  2594. *
  2595. * Set device router link width to @width from router upstream port
  2596. * perspective. Supports also asymmetric links if the routers boths side
  2597. * of the link supports it.
  2598. *
  2599. * Does nothing for host router.
  2600. *
  2601. * Returns %0 in case of success, negative errno otherwise.
  2602. */
  2603. int tb_switch_set_link_width(struct tb_switch *sw, enum tb_link_width width)
  2604. {
  2605. struct tb_port *up, *down;
  2606. int ret = 0;
  2607. if (!tb_route(sw))
  2608. return 0;
  2609. up = tb_upstream_port(sw);
  2610. down = tb_switch_downstream_port(sw);
  2611. switch (width) {
  2612. case TB_LINK_WIDTH_SINGLE:
  2613. ret = tb_switch_lane_bonding_disable(sw);
  2614. break;
  2615. case TB_LINK_WIDTH_DUAL:
  2616. if (sw->link_width == TB_LINK_WIDTH_ASYM_TX ||
  2617. sw->link_width == TB_LINK_WIDTH_ASYM_RX) {
  2618. ret = tb_switch_asym_disable(sw);
  2619. if (ret)
  2620. break;
  2621. }
  2622. ret = tb_switch_lane_bonding_enable(sw);
  2623. break;
  2624. case TB_LINK_WIDTH_ASYM_TX:
  2625. case TB_LINK_WIDTH_ASYM_RX:
  2626. ret = tb_switch_asym_enable(sw, width);
  2627. break;
  2628. }
  2629. switch (ret) {
  2630. case 0:
  2631. break;
  2632. case -ETIMEDOUT:
  2633. tb_sw_warn(sw, "timeout changing link width\n");
  2634. return ret;
  2635. case -ENOTCONN:
  2636. case -EOPNOTSUPP:
  2637. case -ENODEV:
  2638. return ret;
  2639. default:
  2640. tb_sw_dbg(sw, "failed to change link width: %d\n", ret);
  2641. return ret;
  2642. }
  2643. tb_port_update_credits(down);
  2644. tb_port_update_credits(up);
  2645. tb_switch_update_link_attributes(sw);
  2646. tb_sw_dbg(sw, "link width set to %s\n", tb_width_name(width));
  2647. return ret;
  2648. }
  2649. /**
  2650. * tb_switch_configure_link() - Set link configured
  2651. * @sw: Switch whose link is configured
  2652. *
  2653. * Sets the link upstream from @sw configured (from both ends) so that
  2654. * it will not be disconnected when the domain exits sleep. Can be
  2655. * called for any switch.
  2656. *
  2657. * It is recommended that this is called after lane bonding is enabled.
  2658. *
  2659. * Returns %0 on success and negative errno in case of error.
  2660. */
  2661. int tb_switch_configure_link(struct tb_switch *sw)
  2662. {
  2663. struct tb_port *up, *down;
  2664. int ret;
  2665. if (!tb_route(sw) || tb_switch_is_icm(sw))
  2666. return 0;
  2667. up = tb_upstream_port(sw);
  2668. if (tb_switch_is_usb4(up->sw))
  2669. ret = usb4_port_configure(up);
  2670. else
  2671. ret = tb_lc_configure_port(up);
  2672. if (ret)
  2673. return ret;
  2674. down = up->remote;
  2675. if (tb_switch_is_usb4(down->sw))
  2676. return usb4_port_configure(down);
  2677. return tb_lc_configure_port(down);
  2678. }
  2679. /**
  2680. * tb_switch_unconfigure_link() - Unconfigure link
  2681. * @sw: Switch whose link is unconfigured
  2682. *
  2683. * Sets the link unconfigured so the @sw will be disconnected if the
  2684. * domain exists sleep.
  2685. */
  2686. void tb_switch_unconfigure_link(struct tb_switch *sw)
  2687. {
  2688. struct tb_port *up, *down;
  2689. if (!tb_route(sw) || tb_switch_is_icm(sw))
  2690. return;
  2691. /*
  2692. * Unconfigure downstream port so that wake-on-connect can be
  2693. * configured after router unplug. No need to unconfigure upstream port
  2694. * since its router is unplugged.
  2695. */
  2696. up = tb_upstream_port(sw);
  2697. down = up->remote;
  2698. if (tb_switch_is_usb4(down->sw))
  2699. usb4_port_unconfigure(down);
  2700. else
  2701. tb_lc_unconfigure_port(down);
  2702. if (sw->is_unplugged)
  2703. return;
  2704. up = tb_upstream_port(sw);
  2705. if (tb_switch_is_usb4(up->sw))
  2706. usb4_port_unconfigure(up);
  2707. else
  2708. tb_lc_unconfigure_port(up);
  2709. }
  2710. static void tb_switch_credits_init(struct tb_switch *sw)
  2711. {
  2712. if (tb_switch_is_icm(sw))
  2713. return;
  2714. if (!tb_switch_is_usb4(sw))
  2715. return;
  2716. if (usb4_switch_credits_init(sw))
  2717. tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n");
  2718. }
  2719. static int tb_switch_port_hotplug_enable(struct tb_switch *sw)
  2720. {
  2721. struct tb_port *port;
  2722. if (tb_switch_is_icm(sw))
  2723. return 0;
  2724. tb_switch_for_each_port(sw, port) {
  2725. int res;
  2726. if (!port->cap_usb4)
  2727. continue;
  2728. res = usb4_port_hotplug_enable(port);
  2729. if (res)
  2730. return res;
  2731. }
  2732. return 0;
  2733. }
  2734. /**
  2735. * tb_switch_add() - Add a switch to the domain
  2736. * @sw: Switch to add
  2737. *
  2738. * This is the last step in adding switch to the domain. It will read
  2739. * identification information from DROM and initializes ports so that
  2740. * they can be used to connect other switches. The switch will be
  2741. * exposed to the userspace when this function successfully returns. To
  2742. * remove and release the switch, call tb_switch_remove().
  2743. *
  2744. * Return: %0 in case of success and negative errno in case of failure
  2745. */
  2746. int tb_switch_add(struct tb_switch *sw)
  2747. {
  2748. int i, ret;
  2749. /*
  2750. * Initialize DMA control port now before we read DROM. Recent
  2751. * host controllers have more complete DROM on NVM that includes
  2752. * vendor and model identification strings which we then expose
  2753. * to the userspace. NVM can be accessed through DMA
  2754. * configuration based mailbox.
  2755. */
  2756. ret = tb_switch_add_dma_port(sw);
  2757. if (ret) {
  2758. dev_err(&sw->dev, "failed to add DMA port\n");
  2759. return ret;
  2760. }
  2761. if (!sw->safe_mode) {
  2762. tb_switch_credits_init(sw);
  2763. /* read drom */
  2764. ret = tb_drom_read(sw);
  2765. if (ret)
  2766. dev_warn(&sw->dev, "reading DROM failed: %d\n", ret);
  2767. tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
  2768. ret = tb_switch_set_uuid(sw);
  2769. if (ret) {
  2770. dev_err(&sw->dev, "failed to set UUID\n");
  2771. return ret;
  2772. }
  2773. for (i = 0; i <= sw->config.max_port_number; i++) {
  2774. if (sw->ports[i].disabled) {
  2775. tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
  2776. continue;
  2777. }
  2778. ret = tb_init_port(&sw->ports[i]);
  2779. if (ret) {
  2780. dev_err(&sw->dev, "failed to initialize port %d\n", i);
  2781. return ret;
  2782. }
  2783. }
  2784. tb_check_quirks(sw);
  2785. tb_switch_default_link_ports(sw);
  2786. ret = tb_switch_update_link_attributes(sw);
  2787. if (ret)
  2788. return ret;
  2789. tb_switch_link_init(sw);
  2790. ret = tb_switch_clx_init(sw);
  2791. if (ret)
  2792. return ret;
  2793. ret = tb_switch_tmu_init(sw);
  2794. if (ret)
  2795. return ret;
  2796. }
  2797. ret = tb_switch_port_hotplug_enable(sw);
  2798. if (ret)
  2799. return ret;
  2800. ret = device_add(&sw->dev);
  2801. if (ret) {
  2802. dev_err(&sw->dev, "failed to add device: %d\n", ret);
  2803. return ret;
  2804. }
  2805. if (tb_route(sw)) {
  2806. dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
  2807. sw->vendor, sw->device);
  2808. if (sw->vendor_name && sw->device_name)
  2809. dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
  2810. sw->device_name);
  2811. }
  2812. ret = usb4_switch_add_ports(sw);
  2813. if (ret) {
  2814. dev_err(&sw->dev, "failed to add USB4 ports\n");
  2815. goto err_del;
  2816. }
  2817. ret = tb_switch_nvm_add(sw);
  2818. if (ret) {
  2819. dev_err(&sw->dev, "failed to add NVM devices\n");
  2820. goto err_ports;
  2821. }
  2822. /*
  2823. * Thunderbolt routers do not generate wakeups themselves but
  2824. * they forward wakeups from tunneled protocols, so enable it
  2825. * here.
  2826. */
  2827. device_init_wakeup(&sw->dev, true);
  2828. pm_runtime_set_active(&sw->dev);
  2829. if (sw->rpm) {
  2830. pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
  2831. pm_runtime_use_autosuspend(&sw->dev);
  2832. pm_runtime_mark_last_busy(&sw->dev);
  2833. pm_runtime_enable(&sw->dev);
  2834. pm_request_autosuspend(&sw->dev);
  2835. }
  2836. tb_switch_debugfs_init(sw);
  2837. return 0;
  2838. err_ports:
  2839. usb4_switch_remove_ports(sw);
  2840. err_del:
  2841. device_del(&sw->dev);
  2842. return ret;
  2843. }
  2844. /**
  2845. * tb_switch_remove() - Remove and release a switch
  2846. * @sw: Switch to remove
  2847. *
  2848. * This will remove the switch from the domain and release it after last
  2849. * reference count drops to zero. If there are switches connected below
  2850. * this switch, they will be removed as well.
  2851. */
  2852. void tb_switch_remove(struct tb_switch *sw)
  2853. {
  2854. struct tb_port *port;
  2855. tb_switch_debugfs_remove(sw);
  2856. if (sw->rpm) {
  2857. pm_runtime_get_sync(&sw->dev);
  2858. pm_runtime_disable(&sw->dev);
  2859. }
  2860. /* port 0 is the switch itself and never has a remote */
  2861. tb_switch_for_each_port(sw, port) {
  2862. if (tb_port_has_remote(port)) {
  2863. tb_switch_remove(port->remote->sw);
  2864. port->remote = NULL;
  2865. } else if (port->xdomain) {
  2866. port->xdomain->is_unplugged = true;
  2867. tb_xdomain_remove(port->xdomain);
  2868. port->xdomain = NULL;
  2869. }
  2870. /* Remove any downstream retimers */
  2871. tb_retimer_remove_all(port);
  2872. }
  2873. if (!sw->is_unplugged)
  2874. tb_plug_events_active(sw, false);
  2875. tb_switch_nvm_remove(sw);
  2876. usb4_switch_remove_ports(sw);
  2877. if (tb_route(sw))
  2878. dev_info(&sw->dev, "device disconnected\n");
  2879. device_unregister(&sw->dev);
  2880. }
  2881. /**
  2882. * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
  2883. * @sw: Router to mark unplugged
  2884. */
  2885. void tb_sw_set_unplugged(struct tb_switch *sw)
  2886. {
  2887. struct tb_port *port;
  2888. if (sw == sw->tb->root_switch) {
  2889. tb_sw_WARN(sw, "cannot unplug root switch\n");
  2890. return;
  2891. }
  2892. if (sw->is_unplugged) {
  2893. tb_sw_WARN(sw, "is_unplugged already set\n");
  2894. return;
  2895. }
  2896. sw->is_unplugged = true;
  2897. tb_switch_for_each_port(sw, port) {
  2898. if (tb_port_has_remote(port))
  2899. tb_sw_set_unplugged(port->remote->sw);
  2900. else if (port->xdomain)
  2901. port->xdomain->is_unplugged = true;
  2902. }
  2903. }
  2904. static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags, bool runtime)
  2905. {
  2906. if (flags)
  2907. tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
  2908. else
  2909. tb_sw_dbg(sw, "disabling wakeup\n");
  2910. if (tb_switch_is_usb4(sw))
  2911. return usb4_switch_set_wake(sw, flags, runtime);
  2912. return tb_lc_set_wake(sw, flags);
  2913. }
  2914. static void tb_switch_check_wakes(struct tb_switch *sw)
  2915. {
  2916. if (device_may_wakeup(&sw->dev)) {
  2917. if (tb_switch_is_usb4(sw))
  2918. usb4_switch_check_wakes(sw);
  2919. }
  2920. }
  2921. /**
  2922. * tb_switch_resume() - Resume a switch after sleep
  2923. * @sw: Switch to resume
  2924. * @runtime: Is this resume from runtime suspend or system sleep
  2925. *
  2926. * Resumes and re-enumerates router (and all its children), if still plugged
  2927. * after suspend. Don't enumerate device router whose UID was changed during
  2928. * suspend. If this is resume from system sleep, notifies PM core about the
  2929. * wakes occurred during suspend. Disables all wakes, except USB4 wake of
  2930. * upstream port for USB4 routers that shall be always enabled.
  2931. */
  2932. int tb_switch_resume(struct tb_switch *sw, bool runtime)
  2933. {
  2934. struct tb_port *port;
  2935. int err;
  2936. tb_sw_dbg(sw, "resuming switch\n");
  2937. /*
  2938. * Check for UID of the connected switches except for root
  2939. * switch which we assume cannot be removed.
  2940. */
  2941. if (tb_route(sw)) {
  2942. u64 uid;
  2943. /*
  2944. * Check first that we can still read the switch config
  2945. * space. It may be that there is now another domain
  2946. * connected.
  2947. */
  2948. err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
  2949. if (err < 0) {
  2950. tb_sw_info(sw, "switch not present anymore\n");
  2951. return err;
  2952. }
  2953. /* We don't have any way to confirm this was the same device */
  2954. if (!sw->uid)
  2955. return -ENODEV;
  2956. if (tb_switch_is_usb4(sw))
  2957. err = usb4_switch_read_uid(sw, &uid);
  2958. else
  2959. err = tb_drom_read_uid_only(sw, &uid);
  2960. if (err) {
  2961. tb_sw_warn(sw, "uid read failed\n");
  2962. return err;
  2963. }
  2964. if (sw->uid != uid) {
  2965. tb_sw_info(sw,
  2966. "changed while suspended (uid %#llx -> %#llx)\n",
  2967. sw->uid, uid);
  2968. return -ENODEV;
  2969. }
  2970. }
  2971. err = tb_switch_configure(sw);
  2972. if (err)
  2973. return err;
  2974. if (!runtime)
  2975. tb_switch_check_wakes(sw);
  2976. /* Disable wakes */
  2977. tb_switch_set_wake(sw, 0, true);
  2978. err = tb_switch_tmu_init(sw);
  2979. if (err)
  2980. return err;
  2981. /* check for surviving downstream switches */
  2982. tb_switch_for_each_port(sw, port) {
  2983. if (!tb_port_is_null(port))
  2984. continue;
  2985. if (!tb_port_resume(port))
  2986. continue;
  2987. if (tb_wait_for_port(port, true) <= 0) {
  2988. tb_port_warn(port,
  2989. "lost during suspend, disconnecting\n");
  2990. if (tb_port_has_remote(port))
  2991. tb_sw_set_unplugged(port->remote->sw);
  2992. else if (port->xdomain)
  2993. port->xdomain->is_unplugged = true;
  2994. } else {
  2995. /*
  2996. * Always unlock the port so the downstream
  2997. * switch/domain is accessible.
  2998. */
  2999. if (tb_port_unlock(port))
  3000. tb_port_warn(port, "failed to unlock port\n");
  3001. if (port->remote &&
  3002. tb_switch_resume(port->remote->sw, runtime)) {
  3003. tb_port_warn(port,
  3004. "lost during suspend, disconnecting\n");
  3005. tb_sw_set_unplugged(port->remote->sw);
  3006. }
  3007. }
  3008. }
  3009. return 0;
  3010. }
  3011. /**
  3012. * tb_switch_suspend() - Put a switch to sleep
  3013. * @sw: Switch to suspend
  3014. * @runtime: Is this runtime suspend or system sleep
  3015. *
  3016. * Suspends router and all its children. Enables wakes according to
  3017. * value of @runtime and then sets sleep bit for the router. If @sw is
  3018. * host router the domain is ready to go to sleep once this function
  3019. * returns.
  3020. */
  3021. void tb_switch_suspend(struct tb_switch *sw, bool runtime)
  3022. {
  3023. unsigned int flags = 0;
  3024. struct tb_port *port;
  3025. int err;
  3026. tb_sw_dbg(sw, "suspending switch\n");
  3027. /*
  3028. * Actually only needed for Titan Ridge but for simplicity can be
  3029. * done for USB4 device too as CLx is re-enabled at resume.
  3030. */
  3031. tb_switch_clx_disable(sw);
  3032. err = tb_plug_events_active(sw, false);
  3033. if (err)
  3034. return;
  3035. tb_switch_for_each_port(sw, port) {
  3036. if (tb_port_has_remote(port))
  3037. tb_switch_suspend(port->remote->sw, runtime);
  3038. }
  3039. if (runtime) {
  3040. /* Trigger wake when something is plugged in/out */
  3041. flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT;
  3042. flags |= TB_WAKE_ON_USB4;
  3043. flags |= TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE | TB_WAKE_ON_DP;
  3044. } else if (device_may_wakeup(&sw->dev)) {
  3045. flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
  3046. }
  3047. tb_switch_set_wake(sw, flags, runtime);
  3048. if (tb_switch_is_usb4(sw))
  3049. usb4_switch_set_sleep(sw);
  3050. else
  3051. tb_lc_set_sleep(sw);
  3052. }
  3053. /**
  3054. * tb_switch_query_dp_resource() - Query availability of DP resource
  3055. * @sw: Switch whose DP resource is queried
  3056. * @in: DP IN port
  3057. *
  3058. * Queries availability of DP resource for DP tunneling using switch
  3059. * specific means. Returns %true if resource is available.
  3060. */
  3061. bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
  3062. {
  3063. if (tb_switch_is_usb4(sw))
  3064. return usb4_switch_query_dp_resource(sw, in);
  3065. return tb_lc_dp_sink_query(sw, in);
  3066. }
  3067. /**
  3068. * tb_switch_alloc_dp_resource() - Allocate available DP resource
  3069. * @sw: Switch whose DP resource is allocated
  3070. * @in: DP IN port
  3071. *
  3072. * Allocates DP resource for DP tunneling. The resource must be
  3073. * available for this to succeed (see tb_switch_query_dp_resource()).
  3074. * Returns %0 in success and negative errno otherwise.
  3075. */
  3076. int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  3077. {
  3078. int ret;
  3079. if (tb_switch_is_usb4(sw))
  3080. ret = usb4_switch_alloc_dp_resource(sw, in);
  3081. else
  3082. ret = tb_lc_dp_sink_alloc(sw, in);
  3083. if (ret)
  3084. tb_sw_warn(sw, "failed to allocate DP resource for port %d\n",
  3085. in->port);
  3086. else
  3087. tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port);
  3088. return ret;
  3089. }
  3090. /**
  3091. * tb_switch_dealloc_dp_resource() - De-allocate DP resource
  3092. * @sw: Switch whose DP resource is de-allocated
  3093. * @in: DP IN port
  3094. *
  3095. * De-allocates DP resource that was previously allocated for DP
  3096. * tunneling.
  3097. */
  3098. void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  3099. {
  3100. int ret;
  3101. if (tb_switch_is_usb4(sw))
  3102. ret = usb4_switch_dealloc_dp_resource(sw, in);
  3103. else
  3104. ret = tb_lc_dp_sink_dealloc(sw, in);
  3105. if (ret)
  3106. tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
  3107. in->port);
  3108. else
  3109. tb_sw_dbg(sw, "released DP resource for port %d\n", in->port);
  3110. }
  3111. struct tb_sw_lookup {
  3112. struct tb *tb;
  3113. u8 link;
  3114. u8 depth;
  3115. const uuid_t *uuid;
  3116. u64 route;
  3117. };
  3118. static int tb_switch_match(struct device *dev, const void *data)
  3119. {
  3120. struct tb_switch *sw = tb_to_switch(dev);
  3121. const struct tb_sw_lookup *lookup = data;
  3122. if (!sw)
  3123. return 0;
  3124. if (sw->tb != lookup->tb)
  3125. return 0;
  3126. if (lookup->uuid)
  3127. return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
  3128. if (lookup->route) {
  3129. return sw->config.route_lo == lower_32_bits(lookup->route) &&
  3130. sw->config.route_hi == upper_32_bits(lookup->route);
  3131. }
  3132. /* Root switch is matched only by depth */
  3133. if (!lookup->depth)
  3134. return !sw->depth;
  3135. return sw->link == lookup->link && sw->depth == lookup->depth;
  3136. }
  3137. /**
  3138. * tb_switch_find_by_link_depth() - Find switch by link and depth
  3139. * @tb: Domain the switch belongs
  3140. * @link: Link number the switch is connected
  3141. * @depth: Depth of the switch in link
  3142. *
  3143. * Returned switch has reference count increased so the caller needs to
  3144. * call tb_switch_put() when done with the switch.
  3145. */
  3146. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
  3147. {
  3148. struct tb_sw_lookup lookup;
  3149. struct device *dev;
  3150. memset(&lookup, 0, sizeof(lookup));
  3151. lookup.tb = tb;
  3152. lookup.link = link;
  3153. lookup.depth = depth;
  3154. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  3155. if (dev)
  3156. return tb_to_switch(dev);
  3157. return NULL;
  3158. }
  3159. /**
  3160. * tb_switch_find_by_uuid() - Find switch by UUID
  3161. * @tb: Domain the switch belongs
  3162. * @uuid: UUID to look for
  3163. *
  3164. * Returned switch has reference count increased so the caller needs to
  3165. * call tb_switch_put() when done with the switch.
  3166. */
  3167. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  3168. {
  3169. struct tb_sw_lookup lookup;
  3170. struct device *dev;
  3171. memset(&lookup, 0, sizeof(lookup));
  3172. lookup.tb = tb;
  3173. lookup.uuid = uuid;
  3174. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  3175. if (dev)
  3176. return tb_to_switch(dev);
  3177. return NULL;
  3178. }
  3179. /**
  3180. * tb_switch_find_by_route() - Find switch by route string
  3181. * @tb: Domain the switch belongs
  3182. * @route: Route string to look for
  3183. *
  3184. * Returned switch has reference count increased so the caller needs to
  3185. * call tb_switch_put() when done with the switch.
  3186. */
  3187. struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
  3188. {
  3189. struct tb_sw_lookup lookup;
  3190. struct device *dev;
  3191. if (!route)
  3192. return tb_switch_get(tb->root_switch);
  3193. memset(&lookup, 0, sizeof(lookup));
  3194. lookup.tb = tb;
  3195. lookup.route = route;
  3196. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  3197. if (dev)
  3198. return tb_to_switch(dev);
  3199. return NULL;
  3200. }
  3201. /**
  3202. * tb_switch_find_port() - return the first port of @type on @sw or NULL
  3203. * @sw: Switch to find the port from
  3204. * @type: Port type to look for
  3205. */
  3206. struct tb_port *tb_switch_find_port(struct tb_switch *sw,
  3207. enum tb_port_type type)
  3208. {
  3209. struct tb_port *port;
  3210. tb_switch_for_each_port(sw, port) {
  3211. if (port->config.type == type)
  3212. return port;
  3213. }
  3214. return NULL;
  3215. }
  3216. /*
  3217. * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
  3218. * device. For now used only for Titan Ridge.
  3219. */
  3220. static int tb_switch_pcie_bridge_write(struct tb_switch *sw, unsigned int bridge,
  3221. unsigned int pcie_offset, u32 value)
  3222. {
  3223. u32 offset, command, val;
  3224. int ret;
  3225. if (sw->generation != 3)
  3226. return -EOPNOTSUPP;
  3227. offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_WR_DATA;
  3228. ret = tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
  3229. if (ret)
  3230. return ret;
  3231. command = pcie_offset & TB_PLUG_EVENTS_PCIE_CMD_DW_OFFSET_MASK;
  3232. command |= BIT(bridge + TB_PLUG_EVENTS_PCIE_CMD_BR_SHIFT);
  3233. command |= TB_PLUG_EVENTS_PCIE_CMD_RD_WR_MASK;
  3234. command |= TB_PLUG_EVENTS_PCIE_CMD_COMMAND_VAL
  3235. << TB_PLUG_EVENTS_PCIE_CMD_COMMAND_SHIFT;
  3236. command |= TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK;
  3237. offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_CMD;
  3238. ret = tb_sw_write(sw, &command, TB_CFG_SWITCH, offset, 1);
  3239. if (ret)
  3240. return ret;
  3241. ret = tb_switch_wait_for_bit(sw, offset,
  3242. TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK, 0, 100);
  3243. if (ret)
  3244. return ret;
  3245. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
  3246. if (ret)
  3247. return ret;
  3248. if (val & TB_PLUG_EVENTS_PCIE_CMD_TIMEOUT_MASK)
  3249. return -ETIMEDOUT;
  3250. return 0;
  3251. }
  3252. /**
  3253. * tb_switch_pcie_l1_enable() - Enable PCIe link to enter L1 state
  3254. * @sw: Router to enable PCIe L1
  3255. *
  3256. * For Titan Ridge switch to enter CLx state, its PCIe bridges shall enable
  3257. * entry to PCIe L1 state. Shall be called after the upstream PCIe tunnel
  3258. * was configured. Due to Intel platforms limitation, shall be called only
  3259. * for first hop switch.
  3260. */
  3261. int tb_switch_pcie_l1_enable(struct tb_switch *sw)
  3262. {
  3263. struct tb_switch *parent = tb_switch_parent(sw);
  3264. int ret;
  3265. if (!tb_route(sw))
  3266. return 0;
  3267. if (!tb_switch_is_titan_ridge(sw))
  3268. return 0;
  3269. /* Enable PCIe L1 enable only for first hop router (depth = 1) */
  3270. if (tb_route(parent))
  3271. return 0;
  3272. /* Write to downstream PCIe bridge #5 aka Dn4 */
  3273. ret = tb_switch_pcie_bridge_write(sw, 5, 0x143, 0x0c7806b1);
  3274. if (ret)
  3275. return ret;
  3276. /* Write to Upstream PCIe bridge #0 aka Up0 */
  3277. return tb_switch_pcie_bridge_write(sw, 0, 0x143, 0x0c5806b1);
  3278. }
  3279. /**
  3280. * tb_switch_xhci_connect() - Connect internal xHCI
  3281. * @sw: Router whose xHCI to connect
  3282. *
  3283. * Can be called to any router. For Alpine Ridge and Titan Ridge
  3284. * performs special flows that bring the xHCI functional for any device
  3285. * connected to the type-C port. Call only after PCIe tunnel has been
  3286. * established. The function only does the connect if not done already
  3287. * so can be called several times for the same router.
  3288. */
  3289. int tb_switch_xhci_connect(struct tb_switch *sw)
  3290. {
  3291. struct tb_port *port1, *port3;
  3292. int ret;
  3293. if (sw->generation != 3)
  3294. return 0;
  3295. port1 = &sw->ports[1];
  3296. port3 = &sw->ports[3];
  3297. if (tb_switch_is_alpine_ridge(sw)) {
  3298. bool usb_port1, usb_port3, xhci_port1, xhci_port3;
  3299. usb_port1 = tb_lc_is_usb_plugged(port1);
  3300. usb_port3 = tb_lc_is_usb_plugged(port3);
  3301. xhci_port1 = tb_lc_is_xhci_connected(port1);
  3302. xhci_port3 = tb_lc_is_xhci_connected(port3);
  3303. /* Figure out correct USB port to connect */
  3304. if (usb_port1 && !xhci_port1) {
  3305. ret = tb_lc_xhci_connect(port1);
  3306. if (ret)
  3307. return ret;
  3308. }
  3309. if (usb_port3 && !xhci_port3)
  3310. return tb_lc_xhci_connect(port3);
  3311. } else if (tb_switch_is_titan_ridge(sw)) {
  3312. ret = tb_lc_xhci_connect(port1);
  3313. if (ret)
  3314. return ret;
  3315. return tb_lc_xhci_connect(port3);
  3316. }
  3317. return 0;
  3318. }
  3319. /**
  3320. * tb_switch_xhci_disconnect() - Disconnect internal xHCI
  3321. * @sw: Router whose xHCI to disconnect
  3322. *
  3323. * The opposite of tb_switch_xhci_connect(). Disconnects xHCI on both
  3324. * ports.
  3325. */
  3326. void tb_switch_xhci_disconnect(struct tb_switch *sw)
  3327. {
  3328. if (sw->generation == 3) {
  3329. struct tb_port *port1 = &sw->ports[1];
  3330. struct tb_port *port3 = &sw->ports[3];
  3331. tb_lc_xhci_disconnect(port1);
  3332. tb_port_dbg(port1, "disconnected xHCI\n");
  3333. tb_lc_xhci_disconnect(port3);
  3334. tb_port_dbg(port3, "disconnected xHCI\n");
  3335. }
  3336. }