switch.c 94 KB

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