switch.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - switch/port utility functions
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/idr.h>
  10. #include <linux/nvmem-provider.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sizes.h>
  14. #include <linux/slab.h>
  15. #include "tb.h"
  16. /* Switch NVM support */
  17. #define NVM_CSS 0x10
  18. struct nvm_auth_status {
  19. struct list_head list;
  20. uuid_t uuid;
  21. u32 status;
  22. };
  23. enum nvm_write_ops {
  24. WRITE_AND_AUTHENTICATE = 1,
  25. WRITE_ONLY = 2,
  26. };
  27. /*
  28. * Hold NVM authentication failure status per switch This information
  29. * needs to stay around even when the switch gets power cycled so we
  30. * keep it separately.
  31. */
  32. static LIST_HEAD(nvm_auth_status_cache);
  33. static DEFINE_MUTEX(nvm_auth_status_lock);
  34. static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
  35. {
  36. struct nvm_auth_status *st;
  37. list_for_each_entry(st, &nvm_auth_status_cache, list) {
  38. if (uuid_equal(&st->uuid, sw->uuid))
  39. return st;
  40. }
  41. return NULL;
  42. }
  43. static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
  44. {
  45. struct nvm_auth_status *st;
  46. mutex_lock(&nvm_auth_status_lock);
  47. st = __nvm_get_auth_status(sw);
  48. mutex_unlock(&nvm_auth_status_lock);
  49. *status = st ? st->status : 0;
  50. }
  51. static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
  52. {
  53. struct nvm_auth_status *st;
  54. if (WARN_ON(!sw->uuid))
  55. return;
  56. mutex_lock(&nvm_auth_status_lock);
  57. st = __nvm_get_auth_status(sw);
  58. if (!st) {
  59. st = kzalloc(sizeof(*st), GFP_KERNEL);
  60. if (!st)
  61. goto unlock;
  62. memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
  63. INIT_LIST_HEAD(&st->list);
  64. list_add_tail(&st->list, &nvm_auth_status_cache);
  65. }
  66. st->status = status;
  67. unlock:
  68. mutex_unlock(&nvm_auth_status_lock);
  69. }
  70. static void nvm_clear_auth_status(const struct tb_switch *sw)
  71. {
  72. struct nvm_auth_status *st;
  73. mutex_lock(&nvm_auth_status_lock);
  74. st = __nvm_get_auth_status(sw);
  75. if (st) {
  76. list_del(&st->list);
  77. kfree(st);
  78. }
  79. mutex_unlock(&nvm_auth_status_lock);
  80. }
  81. static int nvm_validate_and_write(struct tb_switch *sw)
  82. {
  83. unsigned int image_size, hdr_size;
  84. const u8 *buf = sw->nvm->buf;
  85. u16 ds_size;
  86. int ret;
  87. if (!buf)
  88. return -EINVAL;
  89. image_size = sw->nvm->buf_data_size;
  90. if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
  91. return -EINVAL;
  92. /*
  93. * FARB pointer must point inside the image and must at least
  94. * contain parts of the digital section we will be reading here.
  95. */
  96. hdr_size = (*(u32 *)buf) & 0xffffff;
  97. if (hdr_size + NVM_DEVID + 2 >= image_size)
  98. return -EINVAL;
  99. /* Digital section start should be aligned to 4k page */
  100. if (!IS_ALIGNED(hdr_size, SZ_4K))
  101. return -EINVAL;
  102. /*
  103. * Read digital section size and check that it also fits inside
  104. * the image.
  105. */
  106. ds_size = *(u16 *)(buf + hdr_size);
  107. if (ds_size >= image_size)
  108. return -EINVAL;
  109. if (!sw->safe_mode) {
  110. u16 device_id;
  111. /*
  112. * Make sure the device ID in the image matches the one
  113. * we read from the switch config space.
  114. */
  115. device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
  116. if (device_id != sw->config.device_id)
  117. return -EINVAL;
  118. if (sw->generation < 3) {
  119. /* Write CSS headers first */
  120. ret = dma_port_flash_write(sw->dma_port,
  121. DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
  122. DMA_PORT_CSS_MAX_SIZE);
  123. if (ret)
  124. return ret;
  125. }
  126. /* Skip headers in the image */
  127. buf += hdr_size;
  128. image_size -= hdr_size;
  129. }
  130. if (tb_switch_is_usb4(sw))
  131. ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
  132. else
  133. ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
  134. if (!ret)
  135. sw->nvm->flushed = true;
  136. return ret;
  137. }
  138. static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
  139. {
  140. int ret = 0;
  141. /*
  142. * Root switch NVM upgrade requires that we disconnect the
  143. * existing paths first (in case it is not in safe mode
  144. * already).
  145. */
  146. if (!sw->safe_mode) {
  147. u32 status;
  148. ret = tb_domain_disconnect_all_paths(sw->tb);
  149. if (ret)
  150. return ret;
  151. /*
  152. * The host controller goes away pretty soon after this if
  153. * everything goes well so getting timeout is expected.
  154. */
  155. ret = dma_port_flash_update_auth(sw->dma_port);
  156. if (!ret || ret == -ETIMEDOUT)
  157. return 0;
  158. /*
  159. * Any error from update auth operation requires power
  160. * cycling of the host router.
  161. */
  162. tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
  163. if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
  164. nvm_set_auth_status(sw, status);
  165. }
  166. /*
  167. * From safe mode we can get out by just power cycling the
  168. * switch.
  169. */
  170. dma_port_power_cycle(sw->dma_port);
  171. return ret;
  172. }
  173. static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
  174. {
  175. int ret, retries = 10;
  176. ret = dma_port_flash_update_auth(sw->dma_port);
  177. switch (ret) {
  178. case 0:
  179. case -ETIMEDOUT:
  180. case -EACCES:
  181. case -EINVAL:
  182. /* Power cycle is required */
  183. break;
  184. default:
  185. return ret;
  186. }
  187. /*
  188. * Poll here for the authentication status. It takes some time
  189. * for the device to respond (we get timeout for a while). Once
  190. * we get response the device needs to be power cycled in order
  191. * to the new NVM to be taken into use.
  192. */
  193. do {
  194. u32 status;
  195. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  196. if (ret < 0 && ret != -ETIMEDOUT)
  197. return ret;
  198. if (ret > 0) {
  199. if (status) {
  200. tb_sw_warn(sw, "failed to authenticate NVM\n");
  201. nvm_set_auth_status(sw, status);
  202. }
  203. tb_sw_info(sw, "power cycling the switch now\n");
  204. dma_port_power_cycle(sw->dma_port);
  205. return 0;
  206. }
  207. msleep(500);
  208. } while (--retries);
  209. return -ETIMEDOUT;
  210. }
  211. static void nvm_authenticate_start_dma_port(struct tb_switch *sw)
  212. {
  213. struct pci_dev *root_port;
  214. /*
  215. * During host router NVM upgrade we should not allow root port to
  216. * go into D3cold because some root ports cannot trigger PME
  217. * itself. To be on the safe side keep the root port in D0 during
  218. * the whole upgrade process.
  219. */
  220. root_port = pcie_find_root_port(sw->tb->nhi->pdev);
  221. if (root_port)
  222. pm_runtime_get_noresume(&root_port->dev);
  223. }
  224. static void nvm_authenticate_complete_dma_port(struct tb_switch *sw)
  225. {
  226. struct pci_dev *root_port;
  227. root_port = pcie_find_root_port(sw->tb->nhi->pdev);
  228. if (root_port)
  229. pm_runtime_put(&root_port->dev);
  230. }
  231. static inline bool nvm_readable(struct tb_switch *sw)
  232. {
  233. if (tb_switch_is_usb4(sw)) {
  234. /*
  235. * USB4 devices must support NVM operations but it is
  236. * optional for hosts. Therefore we query the NVM sector
  237. * size here and if it is supported assume NVM
  238. * operations are implemented.
  239. */
  240. return usb4_switch_nvm_sector_size(sw) > 0;
  241. }
  242. /* Thunderbolt 2 and 3 devices support NVM through DMA port */
  243. return !!sw->dma_port;
  244. }
  245. static inline bool nvm_upgradeable(struct tb_switch *sw)
  246. {
  247. if (sw->no_nvm_upgrade)
  248. return false;
  249. return nvm_readable(sw);
  250. }
  251. static inline int nvm_read(struct tb_switch *sw, unsigned int address,
  252. void *buf, size_t size)
  253. {
  254. if (tb_switch_is_usb4(sw))
  255. return usb4_switch_nvm_read(sw, address, buf, size);
  256. return dma_port_flash_read(sw->dma_port, address, buf, size);
  257. }
  258. static int nvm_authenticate(struct tb_switch *sw)
  259. {
  260. int ret;
  261. if (tb_switch_is_usb4(sw))
  262. return usb4_switch_nvm_authenticate(sw);
  263. if (!tb_route(sw)) {
  264. nvm_authenticate_start_dma_port(sw);
  265. ret = nvm_authenticate_host_dma_port(sw);
  266. } else {
  267. ret = nvm_authenticate_device_dma_port(sw);
  268. }
  269. return ret;
  270. }
  271. static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
  272. size_t bytes)
  273. {
  274. struct tb_nvm *nvm = priv;
  275. struct tb_switch *sw = tb_to_switch(nvm->dev);
  276. int ret;
  277. pm_runtime_get_sync(&sw->dev);
  278. if (!mutex_trylock(&sw->tb->lock)) {
  279. ret = restart_syscall();
  280. goto out;
  281. }
  282. ret = nvm_read(sw, offset, val, bytes);
  283. mutex_unlock(&sw->tb->lock);
  284. out:
  285. pm_runtime_mark_last_busy(&sw->dev);
  286. pm_runtime_put_autosuspend(&sw->dev);
  287. return ret;
  288. }
  289. static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
  290. size_t bytes)
  291. {
  292. struct tb_nvm *nvm = priv;
  293. struct tb_switch *sw = tb_to_switch(nvm->dev);
  294. int ret;
  295. if (!mutex_trylock(&sw->tb->lock))
  296. return restart_syscall();
  297. /*
  298. * Since writing the NVM image might require some special steps,
  299. * for example when CSS headers are written, we cache the image
  300. * locally here and handle the special cases when the user asks
  301. * us to authenticate the image.
  302. */
  303. ret = tb_nvm_write_buf(nvm, offset, val, bytes);
  304. mutex_unlock(&sw->tb->lock);
  305. return ret;
  306. }
  307. static int tb_switch_nvm_add(struct tb_switch *sw)
  308. {
  309. struct tb_nvm *nvm;
  310. u32 val;
  311. int ret;
  312. if (!nvm_readable(sw))
  313. return 0;
  314. /*
  315. * The NVM format of non-Intel hardware is not known so
  316. * currently restrict NVM upgrade for Intel hardware. We may
  317. * relax this in the future when we learn other NVM formats.
  318. */
  319. if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
  320. sw->config.vendor_id != 0x8087) {
  321. dev_info(&sw->dev,
  322. "NVM format of vendor %#x is not known, disabling NVM upgrade\n",
  323. sw->config.vendor_id);
  324. return 0;
  325. }
  326. nvm = tb_nvm_alloc(&sw->dev);
  327. if (IS_ERR(nvm))
  328. return PTR_ERR(nvm);
  329. /*
  330. * If the switch is in safe-mode the only accessible portion of
  331. * the NVM is the non-active one where userspace is expected to
  332. * write new functional NVM.
  333. */
  334. if (!sw->safe_mode) {
  335. u32 nvm_size, hdr_size;
  336. ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val));
  337. if (ret)
  338. goto err_nvm;
  339. hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
  340. nvm_size = (SZ_1M << (val & 7)) / 8;
  341. nvm_size = (nvm_size - hdr_size) / 2;
  342. ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val));
  343. if (ret)
  344. goto err_nvm;
  345. nvm->major = val >> 16;
  346. nvm->minor = val >> 8;
  347. ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
  348. if (ret)
  349. goto err_nvm;
  350. }
  351. if (!sw->no_nvm_upgrade) {
  352. ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE,
  353. tb_switch_nvm_write);
  354. if (ret)
  355. goto err_nvm;
  356. }
  357. sw->nvm = nvm;
  358. return 0;
  359. err_nvm:
  360. tb_nvm_free(nvm);
  361. return ret;
  362. }
  363. static void tb_switch_nvm_remove(struct tb_switch *sw)
  364. {
  365. struct tb_nvm *nvm;
  366. nvm = sw->nvm;
  367. sw->nvm = NULL;
  368. if (!nvm)
  369. return;
  370. /* Remove authentication status in case the switch is unplugged */
  371. if (!nvm->authenticating)
  372. nvm_clear_auth_status(sw);
  373. tb_nvm_free(nvm);
  374. }
  375. /* port utility functions */
  376. static const char *tb_port_type(struct tb_regs_port_header *port)
  377. {
  378. switch (port->type >> 16) {
  379. case 0:
  380. switch ((u8) port->type) {
  381. case 0:
  382. return "Inactive";
  383. case 1:
  384. return "Port";
  385. case 2:
  386. return "NHI";
  387. default:
  388. return "unknown";
  389. }
  390. case 0x2:
  391. return "Ethernet";
  392. case 0x8:
  393. return "SATA";
  394. case 0xe:
  395. return "DP/HDMI";
  396. case 0x10:
  397. return "PCIe";
  398. case 0x20:
  399. return "USB";
  400. default:
  401. return "unknown";
  402. }
  403. }
  404. static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
  405. {
  406. tb_dbg(tb,
  407. " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
  408. port->port_number, port->vendor_id, port->device_id,
  409. port->revision, port->thunderbolt_version, tb_port_type(port),
  410. port->type);
  411. tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
  412. port->max_in_hop_id, port->max_out_hop_id);
  413. tb_dbg(tb, " Max counters: %d\n", port->max_counters);
  414. tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits);
  415. }
  416. /**
  417. * tb_port_state() - get connectedness state of a port
  418. *
  419. * The port must have a TB_CAP_PHY (i.e. it should be a real port).
  420. *
  421. * Return: Returns an enum tb_port_state on success or an error code on failure.
  422. */
  423. static int tb_port_state(struct tb_port *port)
  424. {
  425. struct tb_cap_phy phy;
  426. int res;
  427. if (port->cap_phy == 0) {
  428. tb_port_WARN(port, "does not have a PHY\n");
  429. return -EINVAL;
  430. }
  431. res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
  432. if (res)
  433. return res;
  434. return phy.state;
  435. }
  436. /**
  437. * tb_wait_for_port() - wait for a port to become ready
  438. *
  439. * Wait up to 1 second for a port to reach state TB_PORT_UP. If
  440. * wait_if_unplugged is set then we also wait if the port is in state
  441. * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
  442. * switch resume). Otherwise we only wait if a device is registered but the link
  443. * has not yet been established.
  444. *
  445. * Return: Returns an error code on failure. Returns 0 if the port is not
  446. * connected or failed to reach state TB_PORT_UP within one second. Returns 1
  447. * if the port is connected and in state TB_PORT_UP.
  448. */
  449. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  450. {
  451. int retries = 10;
  452. int state;
  453. if (!port->cap_phy) {
  454. tb_port_WARN(port, "does not have PHY\n");
  455. return -EINVAL;
  456. }
  457. if (tb_is_upstream_port(port)) {
  458. tb_port_WARN(port, "is the upstream port\n");
  459. return -EINVAL;
  460. }
  461. while (retries--) {
  462. state = tb_port_state(port);
  463. if (state < 0)
  464. return state;
  465. if (state == TB_PORT_DISABLED) {
  466. tb_port_dbg(port, "is disabled (state: 0)\n");
  467. return 0;
  468. }
  469. if (state == TB_PORT_UNPLUGGED) {
  470. if (wait_if_unplugged) {
  471. /* used during resume */
  472. tb_port_dbg(port,
  473. "is unplugged (state: 7), retrying...\n");
  474. msleep(100);
  475. continue;
  476. }
  477. tb_port_dbg(port, "is unplugged (state: 7)\n");
  478. return 0;
  479. }
  480. if (state == TB_PORT_UP) {
  481. tb_port_dbg(port, "is connected, link is up (state: 2)\n");
  482. return 1;
  483. }
  484. /*
  485. * After plug-in the state is TB_PORT_CONNECTING. Give it some
  486. * time.
  487. */
  488. tb_port_dbg(port,
  489. "is connected, link is not up (state: %d), retrying...\n",
  490. state);
  491. msleep(100);
  492. }
  493. tb_port_warn(port,
  494. "failed to reach state TB_PORT_UP. Ignoring port...\n");
  495. return 0;
  496. }
  497. /**
  498. * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
  499. *
  500. * Change the number of NFC credits allocated to @port by @credits. To remove
  501. * NFC credits pass a negative amount of credits.
  502. *
  503. * Return: Returns 0 on success or an error code on failure.
  504. */
  505. int tb_port_add_nfc_credits(struct tb_port *port, int credits)
  506. {
  507. u32 nfc_credits;
  508. if (credits == 0 || port->sw->is_unplugged)
  509. return 0;
  510. /*
  511. * USB4 restricts programming NFC buffers to lane adapters only
  512. * so skip other ports.
  513. */
  514. if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
  515. return 0;
  516. nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
  517. nfc_credits += credits;
  518. tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
  519. port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
  520. port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
  521. port->config.nfc_credits |= nfc_credits;
  522. return tb_port_write(port, &port->config.nfc_credits,
  523. TB_CFG_PORT, ADP_CS_4, 1);
  524. }
  525. /**
  526. * tb_port_set_initial_credits() - Set initial port link credits allocated
  527. * @port: Port to set the initial credits
  528. * @credits: Number of credits to to allocate
  529. *
  530. * Set initial credits value to be used for ingress shared buffering.
  531. */
  532. int tb_port_set_initial_credits(struct tb_port *port, u32 credits)
  533. {
  534. u32 data;
  535. int ret;
  536. ret = tb_port_read(port, &data, TB_CFG_PORT, ADP_CS_5, 1);
  537. if (ret)
  538. return ret;
  539. data &= ~ADP_CS_5_LCA_MASK;
  540. data |= (credits << ADP_CS_5_LCA_SHIFT) & ADP_CS_5_LCA_MASK;
  541. return tb_port_write(port, &data, TB_CFG_PORT, ADP_CS_5, 1);
  542. }
  543. /**
  544. * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
  545. *
  546. * Return: Returns 0 on success or an error code on failure.
  547. */
  548. int tb_port_clear_counter(struct tb_port *port, int counter)
  549. {
  550. u32 zero[3] = { 0, 0, 0 };
  551. tb_port_dbg(port, "clearing counter %d\n", counter);
  552. return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
  553. }
  554. /**
  555. * tb_port_unlock() - Unlock downstream port
  556. * @port: Port to unlock
  557. *
  558. * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the
  559. * downstream router accessible for CM.
  560. */
  561. int tb_port_unlock(struct tb_port *port)
  562. {
  563. if (tb_switch_is_icm(port->sw))
  564. return 0;
  565. if (!tb_port_is_null(port))
  566. return -EINVAL;
  567. if (tb_switch_is_usb4(port->sw))
  568. return usb4_port_unlock(port);
  569. return 0;
  570. }
  571. static int __tb_port_enable(struct tb_port *port, bool enable)
  572. {
  573. int ret;
  574. u32 phy;
  575. if (!tb_port_is_null(port))
  576. return -EINVAL;
  577. ret = tb_port_read(port, &phy, TB_CFG_PORT,
  578. port->cap_phy + LANE_ADP_CS_1, 1);
  579. if (ret)
  580. return ret;
  581. if (enable)
  582. phy &= ~LANE_ADP_CS_1_LD;
  583. else
  584. phy |= LANE_ADP_CS_1_LD;
  585. return tb_port_write(port, &phy, TB_CFG_PORT,
  586. port->cap_phy + LANE_ADP_CS_1, 1);
  587. }
  588. /**
  589. * tb_port_enable() - Enable lane adapter
  590. * @port: Port to enable (can be %NULL)
  591. *
  592. * This is used for lane 0 and 1 adapters to enable it.
  593. */
  594. int tb_port_enable(struct tb_port *port)
  595. {
  596. return __tb_port_enable(port, true);
  597. }
  598. /**
  599. * tb_port_disable() - Disable lane adapter
  600. * @port: Port to disable (can be %NULL)
  601. *
  602. * This is used for lane 0 and 1 adapters to disable it.
  603. */
  604. int tb_port_disable(struct tb_port *port)
  605. {
  606. return __tb_port_enable(port, false);
  607. }
  608. /**
  609. * tb_init_port() - initialize a port
  610. *
  611. * This is a helper method for tb_switch_alloc. Does not check or initialize
  612. * any downstream switches.
  613. *
  614. * Return: Returns 0 on success or an error code on failure.
  615. */
  616. static int tb_init_port(struct tb_port *port)
  617. {
  618. int res;
  619. int cap;
  620. res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
  621. if (res) {
  622. if (res == -ENODEV) {
  623. tb_dbg(port->sw->tb, " Port %d: not implemented\n",
  624. port->port);
  625. port->disabled = true;
  626. return 0;
  627. }
  628. return res;
  629. }
  630. /* Port 0 is the switch itself and has no PHY. */
  631. if (port->config.type == TB_TYPE_PORT && port->port != 0) {
  632. cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
  633. if (cap > 0)
  634. port->cap_phy = cap;
  635. else
  636. tb_port_WARN(port, "non switch port without a PHY\n");
  637. cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
  638. if (cap > 0)
  639. port->cap_usb4 = cap;
  640. } else if (port->port != 0) {
  641. cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
  642. if (cap > 0)
  643. port->cap_adap = cap;
  644. }
  645. tb_dump_port(port->sw->tb, &port->config);
  646. INIT_LIST_HEAD(&port->list);
  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_simple_get(ida, min_hopid, max_hopid + 1, 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_simple_remove(&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_simple_remove(&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. * If the @end port has been reached, return %NULL.
  733. *
  734. * Domain tb->lock must be held when this function is called.
  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. * Returns 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. return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10;
  789. }
  790. static int tb_port_get_link_width(struct tb_port *port)
  791. {
  792. u32 val;
  793. int ret;
  794. if (!port->cap_phy)
  795. return -EINVAL;
  796. ret = tb_port_read(port, &val, TB_CFG_PORT,
  797. port->cap_phy + LANE_ADP_CS_1, 1);
  798. if (ret)
  799. return ret;
  800. return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
  801. LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
  802. }
  803. static bool tb_port_is_width_supported(struct tb_port *port, int width)
  804. {
  805. u32 phy, widths;
  806. int ret;
  807. if (!port->cap_phy)
  808. return false;
  809. ret = tb_port_read(port, &phy, TB_CFG_PORT,
  810. port->cap_phy + LANE_ADP_CS_0, 1);
  811. if (ret)
  812. return false;
  813. widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
  814. LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
  815. return !!(widths & width);
  816. }
  817. static int tb_port_set_link_width(struct tb_port *port, unsigned int width)
  818. {
  819. u32 val;
  820. int ret;
  821. if (!port->cap_phy)
  822. return -EINVAL;
  823. ret = tb_port_read(port, &val, TB_CFG_PORT,
  824. port->cap_phy + LANE_ADP_CS_1, 1);
  825. if (ret)
  826. return ret;
  827. val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK;
  828. switch (width) {
  829. case 1:
  830. val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE <<
  831. LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
  832. break;
  833. case 2:
  834. val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL <<
  835. LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
  836. break;
  837. default:
  838. return -EINVAL;
  839. }
  840. val |= LANE_ADP_CS_1_LB;
  841. return tb_port_write(port, &val, TB_CFG_PORT,
  842. port->cap_phy + LANE_ADP_CS_1, 1);
  843. }
  844. static int tb_port_lane_bonding_enable(struct tb_port *port)
  845. {
  846. int ret;
  847. /*
  848. * Enable lane bonding for both links if not already enabled by
  849. * for example the boot firmware.
  850. */
  851. ret = tb_port_get_link_width(port);
  852. if (ret == 1) {
  853. ret = tb_port_set_link_width(port, 2);
  854. if (ret)
  855. return ret;
  856. }
  857. ret = tb_port_get_link_width(port->dual_link_port);
  858. if (ret == 1) {
  859. ret = tb_port_set_link_width(port->dual_link_port, 2);
  860. if (ret) {
  861. tb_port_set_link_width(port, 1);
  862. return ret;
  863. }
  864. }
  865. port->bonded = true;
  866. port->dual_link_port->bonded = true;
  867. return 0;
  868. }
  869. static void tb_port_lane_bonding_disable(struct tb_port *port)
  870. {
  871. port->dual_link_port->bonded = false;
  872. port->bonded = false;
  873. tb_port_set_link_width(port->dual_link_port, 1);
  874. tb_port_set_link_width(port, 1);
  875. }
  876. /**
  877. * tb_port_is_enabled() - Is the adapter port enabled
  878. * @port: Port to check
  879. */
  880. bool tb_port_is_enabled(struct tb_port *port)
  881. {
  882. switch (port->config.type) {
  883. case TB_TYPE_PCIE_UP:
  884. case TB_TYPE_PCIE_DOWN:
  885. return tb_pci_port_is_enabled(port);
  886. case TB_TYPE_DP_HDMI_IN:
  887. case TB_TYPE_DP_HDMI_OUT:
  888. return tb_dp_port_is_enabled(port);
  889. case TB_TYPE_USB3_UP:
  890. case TB_TYPE_USB3_DOWN:
  891. return tb_usb3_port_is_enabled(port);
  892. default:
  893. return false;
  894. }
  895. }
  896. /**
  897. * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
  898. * @port: USB3 adapter port to check
  899. */
  900. bool tb_usb3_port_is_enabled(struct tb_port *port)
  901. {
  902. u32 data;
  903. if (tb_port_read(port, &data, TB_CFG_PORT,
  904. port->cap_adap + ADP_USB3_CS_0, 1))
  905. return false;
  906. return !!(data & ADP_USB3_CS_0_PE);
  907. }
  908. /**
  909. * tb_usb3_port_enable() - Enable USB3 adapter port
  910. * @port: USB3 adapter port to enable
  911. * @enable: Enable/disable the USB3 adapter
  912. */
  913. int tb_usb3_port_enable(struct tb_port *port, bool enable)
  914. {
  915. u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V)
  916. : ADP_USB3_CS_0_V;
  917. if (!port->cap_adap)
  918. return -ENXIO;
  919. return tb_port_write(port, &word, TB_CFG_PORT,
  920. port->cap_adap + ADP_USB3_CS_0, 1);
  921. }
  922. /**
  923. * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
  924. * @port: PCIe port to check
  925. */
  926. bool tb_pci_port_is_enabled(struct tb_port *port)
  927. {
  928. u32 data;
  929. if (tb_port_read(port, &data, TB_CFG_PORT,
  930. port->cap_adap + ADP_PCIE_CS_0, 1))
  931. return false;
  932. return !!(data & ADP_PCIE_CS_0_PE);
  933. }
  934. /**
  935. * tb_pci_port_enable() - Enable PCIe adapter port
  936. * @port: PCIe port to enable
  937. * @enable: Enable/disable the PCIe adapter
  938. */
  939. int tb_pci_port_enable(struct tb_port *port, bool enable)
  940. {
  941. u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
  942. if (!port->cap_adap)
  943. return -ENXIO;
  944. return tb_port_write(port, &word, TB_CFG_PORT,
  945. port->cap_adap + ADP_PCIE_CS_0, 1);
  946. }
  947. /**
  948. * tb_dp_port_hpd_is_active() - Is HPD already active
  949. * @port: DP out port to check
  950. *
  951. * Checks if the DP OUT adapter port has HDP bit already set.
  952. */
  953. int tb_dp_port_hpd_is_active(struct tb_port *port)
  954. {
  955. u32 data;
  956. int ret;
  957. ret = tb_port_read(port, &data, TB_CFG_PORT,
  958. port->cap_adap + ADP_DP_CS_2, 1);
  959. if (ret)
  960. return ret;
  961. return !!(data & ADP_DP_CS_2_HDP);
  962. }
  963. /**
  964. * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
  965. * @port: Port to clear HPD
  966. *
  967. * If the DP IN port has HDP set, this function can be used to clear it.
  968. */
  969. int tb_dp_port_hpd_clear(struct tb_port *port)
  970. {
  971. u32 data;
  972. int ret;
  973. ret = tb_port_read(port, &data, TB_CFG_PORT,
  974. port->cap_adap + ADP_DP_CS_3, 1);
  975. if (ret)
  976. return ret;
  977. data |= ADP_DP_CS_3_HDPC;
  978. return tb_port_write(port, &data, TB_CFG_PORT,
  979. port->cap_adap + ADP_DP_CS_3, 1);
  980. }
  981. /**
  982. * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
  983. * @port: DP IN/OUT port to set hops
  984. * @video: Video Hop ID
  985. * @aux_tx: AUX TX Hop ID
  986. * @aux_rx: AUX RX Hop ID
  987. *
  988. * Programs specified Hop IDs for DP IN/OUT port.
  989. */
  990. int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
  991. unsigned int aux_tx, unsigned int aux_rx)
  992. {
  993. u32 data[2];
  994. int ret;
  995. ret = tb_port_read(port, data, TB_CFG_PORT,
  996. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  997. if (ret)
  998. return ret;
  999. data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
  1000. data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
  1001. data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
  1002. data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
  1003. ADP_DP_CS_0_VIDEO_HOPID_MASK;
  1004. data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
  1005. data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
  1006. ADP_DP_CS_1_AUX_RX_HOPID_MASK;
  1007. return tb_port_write(port, data, TB_CFG_PORT,
  1008. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1009. }
  1010. /**
  1011. * tb_dp_port_is_enabled() - Is DP adapter port enabled
  1012. * @port: DP adapter port to check
  1013. */
  1014. bool tb_dp_port_is_enabled(struct tb_port *port)
  1015. {
  1016. u32 data[2];
  1017. if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
  1018. ARRAY_SIZE(data)))
  1019. return false;
  1020. return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
  1021. }
  1022. /**
  1023. * tb_dp_port_enable() - Enables/disables DP paths of a port
  1024. * @port: DP IN/OUT port
  1025. * @enable: Enable/disable DP path
  1026. *
  1027. * Once Hop IDs are programmed DP paths can be enabled or disabled by
  1028. * calling this function.
  1029. */
  1030. int tb_dp_port_enable(struct tb_port *port, bool enable)
  1031. {
  1032. u32 data[2];
  1033. int ret;
  1034. ret = tb_port_read(port, data, TB_CFG_PORT,
  1035. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1036. if (ret)
  1037. return ret;
  1038. if (enable)
  1039. data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
  1040. else
  1041. data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
  1042. return tb_port_write(port, data, TB_CFG_PORT,
  1043. port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
  1044. }
  1045. /* switch utility functions */
  1046. static const char *tb_switch_generation_name(const struct tb_switch *sw)
  1047. {
  1048. switch (sw->generation) {
  1049. case 1:
  1050. return "Thunderbolt 1";
  1051. case 2:
  1052. return "Thunderbolt 2";
  1053. case 3:
  1054. return "Thunderbolt 3";
  1055. case 4:
  1056. return "USB4";
  1057. default:
  1058. return "Unknown";
  1059. }
  1060. }
  1061. static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
  1062. {
  1063. const struct tb_regs_switch_header *regs = &sw->config;
  1064. tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n",
  1065. tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
  1066. regs->revision, regs->thunderbolt_version);
  1067. tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number);
  1068. tb_dbg(tb, " Config:\n");
  1069. tb_dbg(tb,
  1070. " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
  1071. regs->upstream_port_number, regs->depth,
  1072. (((u64) regs->route_hi) << 32) | regs->route_lo,
  1073. regs->enabled, regs->plug_events_delay);
  1074. tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
  1075. regs->__unknown1, regs->__unknown4);
  1076. }
  1077. /**
  1078. * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
  1079. * @sw: Switch to reset
  1080. *
  1081. * Return: Returns 0 on success or an error code on failure.
  1082. */
  1083. int tb_switch_reset(struct tb_switch *sw)
  1084. {
  1085. struct tb_cfg_result res;
  1086. if (sw->generation > 1)
  1087. return 0;
  1088. tb_sw_dbg(sw, "resetting switch\n");
  1089. res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
  1090. TB_CFG_SWITCH, 2, 2);
  1091. if (res.err)
  1092. return res.err;
  1093. res = tb_cfg_reset(sw->tb->ctl, tb_route(sw), TB_CFG_DEFAULT_TIMEOUT);
  1094. if (res.err > 0)
  1095. return -EIO;
  1096. return res.err;
  1097. }
  1098. /**
  1099. * tb_plug_events_active() - enable/disable plug events on a switch
  1100. *
  1101. * Also configures a sane plug_events_delay of 255ms.
  1102. *
  1103. * Return: Returns 0 on success or an error code on failure.
  1104. */
  1105. static int tb_plug_events_active(struct tb_switch *sw, bool active)
  1106. {
  1107. u32 data;
  1108. int res;
  1109. if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
  1110. return 0;
  1111. sw->config.plug_events_delay = 0xff;
  1112. res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
  1113. if (res)
  1114. return res;
  1115. res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
  1116. if (res)
  1117. return res;
  1118. if (active) {
  1119. data = data & 0xFFFFFF83;
  1120. switch (sw->config.device_id) {
  1121. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  1122. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  1123. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  1124. break;
  1125. default:
  1126. data |= 4;
  1127. }
  1128. } else {
  1129. data = data | 0x7c;
  1130. }
  1131. return tb_sw_write(sw, &data, TB_CFG_SWITCH,
  1132. sw->cap_plug_events + 1, 1);
  1133. }
  1134. static ssize_t authorized_show(struct device *dev,
  1135. struct device_attribute *attr,
  1136. char *buf)
  1137. {
  1138. struct tb_switch *sw = tb_to_switch(dev);
  1139. return sprintf(buf, "%u\n", sw->authorized);
  1140. }
  1141. static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
  1142. {
  1143. int ret = -EINVAL;
  1144. if (!mutex_trylock(&sw->tb->lock))
  1145. return restart_syscall();
  1146. if (sw->authorized)
  1147. goto unlock;
  1148. switch (val) {
  1149. /* Approve switch */
  1150. case 1:
  1151. if (sw->key)
  1152. ret = tb_domain_approve_switch_key(sw->tb, sw);
  1153. else
  1154. ret = tb_domain_approve_switch(sw->tb, sw);
  1155. break;
  1156. /* Challenge switch */
  1157. case 2:
  1158. if (sw->key)
  1159. ret = tb_domain_challenge_switch_key(sw->tb, sw);
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. if (!ret) {
  1165. sw->authorized = val;
  1166. /* Notify status change to the userspace */
  1167. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  1168. }
  1169. unlock:
  1170. mutex_unlock(&sw->tb->lock);
  1171. return ret;
  1172. }
  1173. static ssize_t authorized_store(struct device *dev,
  1174. struct device_attribute *attr,
  1175. const char *buf, size_t count)
  1176. {
  1177. struct tb_switch *sw = tb_to_switch(dev);
  1178. unsigned int val;
  1179. ssize_t ret;
  1180. ret = kstrtouint(buf, 0, &val);
  1181. if (ret)
  1182. return ret;
  1183. if (val > 2)
  1184. return -EINVAL;
  1185. pm_runtime_get_sync(&sw->dev);
  1186. ret = tb_switch_set_authorized(sw, val);
  1187. pm_runtime_mark_last_busy(&sw->dev);
  1188. pm_runtime_put_autosuspend(&sw->dev);
  1189. return ret ? ret : count;
  1190. }
  1191. static DEVICE_ATTR_RW(authorized);
  1192. static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
  1193. char *buf)
  1194. {
  1195. struct tb_switch *sw = tb_to_switch(dev);
  1196. return sprintf(buf, "%u\n", sw->boot);
  1197. }
  1198. static DEVICE_ATTR_RO(boot);
  1199. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  1200. char *buf)
  1201. {
  1202. struct tb_switch *sw = tb_to_switch(dev);
  1203. return sprintf(buf, "%#x\n", sw->device);
  1204. }
  1205. static DEVICE_ATTR_RO(device);
  1206. static ssize_t
  1207. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1208. {
  1209. struct tb_switch *sw = tb_to_switch(dev);
  1210. return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
  1211. }
  1212. static DEVICE_ATTR_RO(device_name);
  1213. static ssize_t
  1214. generation_show(struct device *dev, struct device_attribute *attr, char *buf)
  1215. {
  1216. struct tb_switch *sw = tb_to_switch(dev);
  1217. return sprintf(buf, "%u\n", sw->generation);
  1218. }
  1219. static DEVICE_ATTR_RO(generation);
  1220. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  1221. char *buf)
  1222. {
  1223. struct tb_switch *sw = tb_to_switch(dev);
  1224. ssize_t ret;
  1225. if (!mutex_trylock(&sw->tb->lock))
  1226. return restart_syscall();
  1227. if (sw->key)
  1228. ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
  1229. else
  1230. ret = sprintf(buf, "\n");
  1231. mutex_unlock(&sw->tb->lock);
  1232. return ret;
  1233. }
  1234. static ssize_t key_store(struct device *dev, struct device_attribute *attr,
  1235. const char *buf, size_t count)
  1236. {
  1237. struct tb_switch *sw = tb_to_switch(dev);
  1238. u8 key[TB_SWITCH_KEY_SIZE];
  1239. ssize_t ret = count;
  1240. bool clear = false;
  1241. if (!strcmp(buf, "\n"))
  1242. clear = true;
  1243. else if (hex2bin(key, buf, sizeof(key)))
  1244. return -EINVAL;
  1245. if (!mutex_trylock(&sw->tb->lock))
  1246. return restart_syscall();
  1247. if (sw->authorized) {
  1248. ret = -EBUSY;
  1249. } else {
  1250. kfree(sw->key);
  1251. if (clear) {
  1252. sw->key = NULL;
  1253. } else {
  1254. sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
  1255. if (!sw->key)
  1256. ret = -ENOMEM;
  1257. }
  1258. }
  1259. mutex_unlock(&sw->tb->lock);
  1260. return ret;
  1261. }
  1262. static DEVICE_ATTR(key, 0600, key_show, key_store);
  1263. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  1264. char *buf)
  1265. {
  1266. struct tb_switch *sw = tb_to_switch(dev);
  1267. return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed);
  1268. }
  1269. /*
  1270. * Currently all lanes must run at the same speed but we expose here
  1271. * both directions to allow possible asymmetric links in the future.
  1272. */
  1273. static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
  1274. static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
  1275. static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
  1276. char *buf)
  1277. {
  1278. struct tb_switch *sw = tb_to_switch(dev);
  1279. return sprintf(buf, "%u\n", sw->link_width);
  1280. }
  1281. /*
  1282. * Currently link has same amount of lanes both directions (1 or 2) but
  1283. * expose them separately to allow possible asymmetric links in the future.
  1284. */
  1285. static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
  1286. static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
  1287. static ssize_t nvm_authenticate_show(struct device *dev,
  1288. struct device_attribute *attr, char *buf)
  1289. {
  1290. struct tb_switch *sw = tb_to_switch(dev);
  1291. u32 status;
  1292. nvm_get_auth_status(sw, &status);
  1293. return sprintf(buf, "%#x\n", status);
  1294. }
  1295. static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf,
  1296. bool disconnect)
  1297. {
  1298. struct tb_switch *sw = tb_to_switch(dev);
  1299. int val;
  1300. int ret;
  1301. pm_runtime_get_sync(&sw->dev);
  1302. if (!mutex_trylock(&sw->tb->lock)) {
  1303. ret = restart_syscall();
  1304. goto exit_rpm;
  1305. }
  1306. /* If NVMem devices are not yet added */
  1307. if (!sw->nvm) {
  1308. ret = -EAGAIN;
  1309. goto exit_unlock;
  1310. }
  1311. ret = kstrtoint(buf, 10, &val);
  1312. if (ret)
  1313. goto exit_unlock;
  1314. /* Always clear the authentication status */
  1315. nvm_clear_auth_status(sw);
  1316. if (val > 0) {
  1317. if (!sw->nvm->flushed) {
  1318. if (!sw->nvm->buf) {
  1319. ret = -EINVAL;
  1320. goto exit_unlock;
  1321. }
  1322. ret = nvm_validate_and_write(sw);
  1323. if (ret || val == WRITE_ONLY)
  1324. goto exit_unlock;
  1325. }
  1326. if (val == WRITE_AND_AUTHENTICATE) {
  1327. if (disconnect) {
  1328. ret = tb_lc_force_power(sw);
  1329. } else {
  1330. sw->nvm->authenticating = true;
  1331. ret = nvm_authenticate(sw);
  1332. }
  1333. }
  1334. }
  1335. exit_unlock:
  1336. mutex_unlock(&sw->tb->lock);
  1337. exit_rpm:
  1338. pm_runtime_mark_last_busy(&sw->dev);
  1339. pm_runtime_put_autosuspend(&sw->dev);
  1340. return ret;
  1341. }
  1342. static ssize_t nvm_authenticate_store(struct device *dev,
  1343. struct device_attribute *attr, const char *buf, size_t count)
  1344. {
  1345. int ret = nvm_authenticate_sysfs(dev, buf, false);
  1346. if (ret)
  1347. return ret;
  1348. return count;
  1349. }
  1350. static DEVICE_ATTR_RW(nvm_authenticate);
  1351. static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev,
  1352. struct device_attribute *attr, char *buf)
  1353. {
  1354. return nvm_authenticate_show(dev, attr, buf);
  1355. }
  1356. static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev,
  1357. struct device_attribute *attr, const char *buf, size_t count)
  1358. {
  1359. int ret;
  1360. ret = nvm_authenticate_sysfs(dev, buf, true);
  1361. return ret ? ret : count;
  1362. }
  1363. static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect);
  1364. static ssize_t nvm_version_show(struct device *dev,
  1365. struct device_attribute *attr, char *buf)
  1366. {
  1367. struct tb_switch *sw = tb_to_switch(dev);
  1368. int ret;
  1369. if (!mutex_trylock(&sw->tb->lock))
  1370. return restart_syscall();
  1371. if (sw->safe_mode)
  1372. ret = -ENODATA;
  1373. else if (!sw->nvm)
  1374. ret = -EAGAIN;
  1375. else
  1376. ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
  1377. mutex_unlock(&sw->tb->lock);
  1378. return ret;
  1379. }
  1380. static DEVICE_ATTR_RO(nvm_version);
  1381. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  1382. char *buf)
  1383. {
  1384. struct tb_switch *sw = tb_to_switch(dev);
  1385. return sprintf(buf, "%#x\n", sw->vendor);
  1386. }
  1387. static DEVICE_ATTR_RO(vendor);
  1388. static ssize_t
  1389. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  1390. {
  1391. struct tb_switch *sw = tb_to_switch(dev);
  1392. return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
  1393. }
  1394. static DEVICE_ATTR_RO(vendor_name);
  1395. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  1396. char *buf)
  1397. {
  1398. struct tb_switch *sw = tb_to_switch(dev);
  1399. return sprintf(buf, "%pUb\n", sw->uuid);
  1400. }
  1401. static DEVICE_ATTR_RO(unique_id);
  1402. static struct attribute *switch_attrs[] = {
  1403. &dev_attr_authorized.attr,
  1404. &dev_attr_boot.attr,
  1405. &dev_attr_device.attr,
  1406. &dev_attr_device_name.attr,
  1407. &dev_attr_generation.attr,
  1408. &dev_attr_key.attr,
  1409. &dev_attr_nvm_authenticate.attr,
  1410. &dev_attr_nvm_authenticate_on_disconnect.attr,
  1411. &dev_attr_nvm_version.attr,
  1412. &dev_attr_rx_speed.attr,
  1413. &dev_attr_rx_lanes.attr,
  1414. &dev_attr_tx_speed.attr,
  1415. &dev_attr_tx_lanes.attr,
  1416. &dev_attr_vendor.attr,
  1417. &dev_attr_vendor_name.attr,
  1418. &dev_attr_unique_id.attr,
  1419. NULL,
  1420. };
  1421. static umode_t switch_attr_is_visible(struct kobject *kobj,
  1422. struct attribute *attr, int n)
  1423. {
  1424. struct device *dev = kobj_to_dev(kobj);
  1425. struct tb_switch *sw = tb_to_switch(dev);
  1426. if (attr == &dev_attr_device.attr) {
  1427. if (!sw->device)
  1428. return 0;
  1429. } else if (attr == &dev_attr_device_name.attr) {
  1430. if (!sw->device_name)
  1431. return 0;
  1432. } else if (attr == &dev_attr_vendor.attr) {
  1433. if (!sw->vendor)
  1434. return 0;
  1435. } else if (attr == &dev_attr_vendor_name.attr) {
  1436. if (!sw->vendor_name)
  1437. return 0;
  1438. } else if (attr == &dev_attr_key.attr) {
  1439. if (tb_route(sw) &&
  1440. sw->tb->security_level == TB_SECURITY_SECURE &&
  1441. sw->security_level == TB_SECURITY_SECURE)
  1442. return attr->mode;
  1443. return 0;
  1444. } else if (attr == &dev_attr_rx_speed.attr ||
  1445. attr == &dev_attr_rx_lanes.attr ||
  1446. attr == &dev_attr_tx_speed.attr ||
  1447. attr == &dev_attr_tx_lanes.attr) {
  1448. if (tb_route(sw))
  1449. return attr->mode;
  1450. return 0;
  1451. } else if (attr == &dev_attr_nvm_authenticate.attr) {
  1452. if (nvm_upgradeable(sw))
  1453. return attr->mode;
  1454. return 0;
  1455. } else if (attr == &dev_attr_nvm_version.attr) {
  1456. if (nvm_readable(sw))
  1457. return attr->mode;
  1458. return 0;
  1459. } else if (attr == &dev_attr_boot.attr) {
  1460. if (tb_route(sw))
  1461. return attr->mode;
  1462. return 0;
  1463. } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) {
  1464. if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
  1465. return attr->mode;
  1466. return 0;
  1467. }
  1468. return sw->safe_mode ? 0 : attr->mode;
  1469. }
  1470. static struct attribute_group switch_group = {
  1471. .is_visible = switch_attr_is_visible,
  1472. .attrs = switch_attrs,
  1473. };
  1474. static const struct attribute_group *switch_groups[] = {
  1475. &switch_group,
  1476. NULL,
  1477. };
  1478. static void tb_switch_release(struct device *dev)
  1479. {
  1480. struct tb_switch *sw = tb_to_switch(dev);
  1481. struct tb_port *port;
  1482. dma_port_free(sw->dma_port);
  1483. tb_switch_for_each_port(sw, port) {
  1484. ida_destroy(&port->in_hopids);
  1485. ida_destroy(&port->out_hopids);
  1486. }
  1487. kfree(sw->uuid);
  1488. kfree(sw->device_name);
  1489. kfree(sw->vendor_name);
  1490. kfree(sw->ports);
  1491. kfree(sw->drom);
  1492. kfree(sw->key);
  1493. kfree(sw);
  1494. }
  1495. /*
  1496. * Currently only need to provide the callbacks. Everything else is handled
  1497. * in the connection manager.
  1498. */
  1499. static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
  1500. {
  1501. struct tb_switch *sw = tb_to_switch(dev);
  1502. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  1503. if (cm_ops->runtime_suspend_switch)
  1504. return cm_ops->runtime_suspend_switch(sw);
  1505. return 0;
  1506. }
  1507. static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
  1508. {
  1509. struct tb_switch *sw = tb_to_switch(dev);
  1510. const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
  1511. if (cm_ops->runtime_resume_switch)
  1512. return cm_ops->runtime_resume_switch(sw);
  1513. return 0;
  1514. }
  1515. static const struct dev_pm_ops tb_switch_pm_ops = {
  1516. SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
  1517. NULL)
  1518. };
  1519. struct device_type tb_switch_type = {
  1520. .name = "thunderbolt_device",
  1521. .release = tb_switch_release,
  1522. .pm = &tb_switch_pm_ops,
  1523. };
  1524. static int tb_switch_get_generation(struct tb_switch *sw)
  1525. {
  1526. switch (sw->config.device_id) {
  1527. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  1528. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  1529. case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
  1530. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
  1531. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  1532. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  1533. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
  1534. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
  1535. return 1;
  1536. case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
  1537. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
  1538. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
  1539. return 2;
  1540. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  1541. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  1542. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  1543. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  1544. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  1545. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  1546. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  1547. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
  1548. case PCI_DEVICE_ID_INTEL_ICL_NHI0:
  1549. case PCI_DEVICE_ID_INTEL_ICL_NHI1:
  1550. return 3;
  1551. default:
  1552. if (tb_switch_is_usb4(sw))
  1553. return 4;
  1554. /*
  1555. * For unknown switches assume generation to be 1 to be
  1556. * on the safe side.
  1557. */
  1558. tb_sw_warn(sw, "unsupported switch device id %#x\n",
  1559. sw->config.device_id);
  1560. return 1;
  1561. }
  1562. }
  1563. static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
  1564. {
  1565. int max_depth;
  1566. if (tb_switch_is_usb4(sw) ||
  1567. (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
  1568. max_depth = USB4_SWITCH_MAX_DEPTH;
  1569. else
  1570. max_depth = TB_SWITCH_MAX_DEPTH;
  1571. return depth > max_depth;
  1572. }
  1573. /**
  1574. * tb_switch_alloc() - allocate a switch
  1575. * @tb: Pointer to the owning domain
  1576. * @parent: Parent device for this switch
  1577. * @route: Route string for this switch
  1578. *
  1579. * Allocates and initializes a switch. Will not upload configuration to
  1580. * the switch. For that you need to call tb_switch_configure()
  1581. * separately. The returned switch should be released by calling
  1582. * tb_switch_put().
  1583. *
  1584. * Return: Pointer to the allocated switch or ERR_PTR() in case of
  1585. * failure.
  1586. */
  1587. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  1588. u64 route)
  1589. {
  1590. struct tb_switch *sw;
  1591. int upstream_port;
  1592. int i, ret, depth;
  1593. /* Unlock the downstream port so we can access the switch below */
  1594. if (route) {
  1595. struct tb_switch *parent_sw = tb_to_switch(parent);
  1596. struct tb_port *down;
  1597. down = tb_port_at(route, parent_sw);
  1598. tb_port_unlock(down);
  1599. }
  1600. depth = tb_route_length(route);
  1601. upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
  1602. if (upstream_port < 0)
  1603. return ERR_PTR(upstream_port);
  1604. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  1605. if (!sw)
  1606. return ERR_PTR(-ENOMEM);
  1607. sw->tb = tb;
  1608. ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
  1609. if (ret)
  1610. goto err_free_sw_ports;
  1611. sw->generation = tb_switch_get_generation(sw);
  1612. tb_dbg(tb, "current switch config:\n");
  1613. tb_dump_switch(tb, sw);
  1614. /* configure switch */
  1615. sw->config.upstream_port_number = upstream_port;
  1616. sw->config.depth = depth;
  1617. sw->config.route_hi = upper_32_bits(route);
  1618. sw->config.route_lo = lower_32_bits(route);
  1619. sw->config.enabled = 0;
  1620. /* Make sure we do not exceed maximum topology limit */
  1621. if (tb_switch_exceeds_max_depth(sw, depth)) {
  1622. ret = -EADDRNOTAVAIL;
  1623. goto err_free_sw_ports;
  1624. }
  1625. /* initialize ports */
  1626. sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
  1627. GFP_KERNEL);
  1628. if (!sw->ports) {
  1629. ret = -ENOMEM;
  1630. goto err_free_sw_ports;
  1631. }
  1632. for (i = 0; i <= sw->config.max_port_number; i++) {
  1633. /* minimum setup for tb_find_cap and tb_drom_read to work */
  1634. sw->ports[i].sw = sw;
  1635. sw->ports[i].port = i;
  1636. /* Control port does not need HopID allocation */
  1637. if (i) {
  1638. ida_init(&sw->ports[i].in_hopids);
  1639. ida_init(&sw->ports[i].out_hopids);
  1640. }
  1641. }
  1642. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
  1643. if (ret > 0)
  1644. sw->cap_plug_events = ret;
  1645. ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
  1646. if (ret > 0)
  1647. sw->cap_lc = ret;
  1648. /* Root switch is always authorized */
  1649. if (!route)
  1650. sw->authorized = true;
  1651. device_initialize(&sw->dev);
  1652. sw->dev.parent = parent;
  1653. sw->dev.bus = &tb_bus_type;
  1654. sw->dev.type = &tb_switch_type;
  1655. sw->dev.groups = switch_groups;
  1656. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  1657. return sw;
  1658. err_free_sw_ports:
  1659. kfree(sw->ports);
  1660. kfree(sw);
  1661. return ERR_PTR(ret);
  1662. }
  1663. /**
  1664. * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
  1665. * @tb: Pointer to the owning domain
  1666. * @parent: Parent device for this switch
  1667. * @route: Route string for this switch
  1668. *
  1669. * This creates a switch in safe mode. This means the switch pretty much
  1670. * lacks all capabilities except DMA configuration port before it is
  1671. * flashed with a valid NVM firmware.
  1672. *
  1673. * The returned switch must be released by calling tb_switch_put().
  1674. *
  1675. * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
  1676. */
  1677. struct tb_switch *
  1678. tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
  1679. {
  1680. struct tb_switch *sw;
  1681. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  1682. if (!sw)
  1683. return ERR_PTR(-ENOMEM);
  1684. sw->tb = tb;
  1685. sw->config.depth = tb_route_length(route);
  1686. sw->config.route_hi = upper_32_bits(route);
  1687. sw->config.route_lo = lower_32_bits(route);
  1688. sw->safe_mode = true;
  1689. device_initialize(&sw->dev);
  1690. sw->dev.parent = parent;
  1691. sw->dev.bus = &tb_bus_type;
  1692. sw->dev.type = &tb_switch_type;
  1693. sw->dev.groups = switch_groups;
  1694. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  1695. return sw;
  1696. }
  1697. /**
  1698. * tb_switch_configure() - Uploads configuration to the switch
  1699. * @sw: Switch to configure
  1700. *
  1701. * Call this function before the switch is added to the system. It will
  1702. * upload configuration to the switch and makes it available for the
  1703. * connection manager to use. Can be called to the switch again after
  1704. * resume from low power states to re-initialize it.
  1705. *
  1706. * Return: %0 in case of success and negative errno in case of failure
  1707. */
  1708. int tb_switch_configure(struct tb_switch *sw)
  1709. {
  1710. struct tb *tb = sw->tb;
  1711. u64 route;
  1712. int ret;
  1713. route = tb_route(sw);
  1714. tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
  1715. sw->config.enabled ? "restoring" : "initializing", route,
  1716. tb_route_length(route), sw->config.upstream_port_number);
  1717. sw->config.enabled = 1;
  1718. if (tb_switch_is_usb4(sw)) {
  1719. /*
  1720. * For USB4 devices, we need to program the CM version
  1721. * accordingly so that it knows to expose all the
  1722. * additional capabilities.
  1723. */
  1724. sw->config.cmuv = USB4_VERSION_1_0;
  1725. /* Enumerate the switch */
  1726. ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
  1727. ROUTER_CS_1, 4);
  1728. if (ret)
  1729. return ret;
  1730. ret = usb4_switch_setup(sw);
  1731. } else {
  1732. if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
  1733. tb_sw_warn(sw, "unknown switch vendor id %#x\n",
  1734. sw->config.vendor_id);
  1735. if (!sw->cap_plug_events) {
  1736. tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
  1737. return -ENODEV;
  1738. }
  1739. /* Enumerate the switch */
  1740. ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
  1741. ROUTER_CS_1, 3);
  1742. }
  1743. if (ret)
  1744. return ret;
  1745. return tb_plug_events_active(sw, true);
  1746. }
  1747. static int tb_switch_set_uuid(struct tb_switch *sw)
  1748. {
  1749. bool uid = false;
  1750. u32 uuid[4];
  1751. int ret;
  1752. if (sw->uuid)
  1753. return 0;
  1754. if (tb_switch_is_usb4(sw)) {
  1755. ret = usb4_switch_read_uid(sw, &sw->uid);
  1756. if (ret)
  1757. return ret;
  1758. uid = true;
  1759. } else {
  1760. /*
  1761. * The newer controllers include fused UUID as part of
  1762. * link controller specific registers
  1763. */
  1764. ret = tb_lc_read_uuid(sw, uuid);
  1765. if (ret) {
  1766. if (ret != -EINVAL)
  1767. return ret;
  1768. uid = true;
  1769. }
  1770. }
  1771. if (uid) {
  1772. /*
  1773. * ICM generates UUID based on UID and fills the upper
  1774. * two words with ones. This is not strictly following
  1775. * UUID format but we want to be compatible with it so
  1776. * we do the same here.
  1777. */
  1778. uuid[0] = sw->uid & 0xffffffff;
  1779. uuid[1] = (sw->uid >> 32) & 0xffffffff;
  1780. uuid[2] = 0xffffffff;
  1781. uuid[3] = 0xffffffff;
  1782. }
  1783. sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  1784. if (!sw->uuid)
  1785. return -ENOMEM;
  1786. return 0;
  1787. }
  1788. static int tb_switch_add_dma_port(struct tb_switch *sw)
  1789. {
  1790. u32 status;
  1791. int ret;
  1792. switch (sw->generation) {
  1793. case 2:
  1794. /* Only root switch can be upgraded */
  1795. if (tb_route(sw))
  1796. return 0;
  1797. fallthrough;
  1798. case 3:
  1799. ret = tb_switch_set_uuid(sw);
  1800. if (ret)
  1801. return ret;
  1802. break;
  1803. default:
  1804. /*
  1805. * DMA port is the only thing available when the switch
  1806. * is in safe mode.
  1807. */
  1808. if (!sw->safe_mode)
  1809. return 0;
  1810. break;
  1811. }
  1812. /* Root switch DMA port requires running firmware */
  1813. if (!tb_route(sw) && !tb_switch_is_icm(sw))
  1814. return 0;
  1815. sw->dma_port = dma_port_alloc(sw);
  1816. if (!sw->dma_port)
  1817. return 0;
  1818. if (sw->no_nvm_upgrade)
  1819. return 0;
  1820. /*
  1821. * If there is status already set then authentication failed
  1822. * when the dma_port_flash_update_auth() returned. Power cycling
  1823. * is not needed (it was done already) so only thing we do here
  1824. * is to unblock runtime PM of the root port.
  1825. */
  1826. nvm_get_auth_status(sw, &status);
  1827. if (status) {
  1828. if (!tb_route(sw))
  1829. nvm_authenticate_complete_dma_port(sw);
  1830. return 0;
  1831. }
  1832. /*
  1833. * Check status of the previous flash authentication. If there
  1834. * is one we need to power cycle the switch in any case to make
  1835. * it functional again.
  1836. */
  1837. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  1838. if (ret <= 0)
  1839. return ret;
  1840. /* Now we can allow root port to suspend again */
  1841. if (!tb_route(sw))
  1842. nvm_authenticate_complete_dma_port(sw);
  1843. if (status) {
  1844. tb_sw_info(sw, "switch flash authentication failed\n");
  1845. nvm_set_auth_status(sw, status);
  1846. }
  1847. tb_sw_info(sw, "power cycling the switch now\n");
  1848. dma_port_power_cycle(sw->dma_port);
  1849. /*
  1850. * We return error here which causes the switch adding failure.
  1851. * It should appear back after power cycle is complete.
  1852. */
  1853. return -ESHUTDOWN;
  1854. }
  1855. static void tb_switch_default_link_ports(struct tb_switch *sw)
  1856. {
  1857. int i;
  1858. for (i = 1; i <= sw->config.max_port_number; i++) {
  1859. struct tb_port *port = &sw->ports[i];
  1860. struct tb_port *subordinate;
  1861. if (!tb_port_is_null(port))
  1862. continue;
  1863. /* Check for the subordinate port */
  1864. if (i == sw->config.max_port_number ||
  1865. !tb_port_is_null(&sw->ports[i + 1]))
  1866. continue;
  1867. /* Link them if not already done so (by DROM) */
  1868. subordinate = &sw->ports[i + 1];
  1869. if (!port->dual_link_port && !subordinate->dual_link_port) {
  1870. port->link_nr = 0;
  1871. port->dual_link_port = subordinate;
  1872. subordinate->link_nr = 1;
  1873. subordinate->dual_link_port = port;
  1874. tb_sw_dbg(sw, "linked ports %d <-> %d\n",
  1875. port->port, subordinate->port);
  1876. }
  1877. }
  1878. }
  1879. static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
  1880. {
  1881. const struct tb_port *up = tb_upstream_port(sw);
  1882. if (!up->dual_link_port || !up->dual_link_port->remote)
  1883. return false;
  1884. if (tb_switch_is_usb4(sw))
  1885. return usb4_switch_lane_bonding_possible(sw);
  1886. return tb_lc_lane_bonding_possible(sw);
  1887. }
  1888. static int tb_switch_update_link_attributes(struct tb_switch *sw)
  1889. {
  1890. struct tb_port *up;
  1891. bool change = false;
  1892. int ret;
  1893. if (!tb_route(sw) || tb_switch_is_icm(sw))
  1894. return 0;
  1895. up = tb_upstream_port(sw);
  1896. ret = tb_port_get_link_speed(up);
  1897. if (ret < 0)
  1898. return ret;
  1899. if (sw->link_speed != ret)
  1900. change = true;
  1901. sw->link_speed = ret;
  1902. ret = tb_port_get_link_width(up);
  1903. if (ret < 0)
  1904. return ret;
  1905. if (sw->link_width != ret)
  1906. change = true;
  1907. sw->link_width = ret;
  1908. /* Notify userspace that there is possible link attribute change */
  1909. if (device_is_registered(&sw->dev) && change)
  1910. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  1911. return 0;
  1912. }
  1913. /**
  1914. * tb_switch_lane_bonding_enable() - Enable lane bonding
  1915. * @sw: Switch to enable lane bonding
  1916. *
  1917. * Connection manager can call this function to enable lane bonding of a
  1918. * switch. If conditions are correct and both switches support the feature,
  1919. * lanes are bonded. It is safe to call this to any switch.
  1920. */
  1921. int tb_switch_lane_bonding_enable(struct tb_switch *sw)
  1922. {
  1923. struct tb_switch *parent = tb_to_switch(sw->dev.parent);
  1924. struct tb_port *up, *down;
  1925. u64 route = tb_route(sw);
  1926. int ret;
  1927. if (!route)
  1928. return 0;
  1929. if (!tb_switch_lane_bonding_possible(sw))
  1930. return 0;
  1931. up = tb_upstream_port(sw);
  1932. down = tb_port_at(route, parent);
  1933. if (!tb_port_is_width_supported(up, 2) ||
  1934. !tb_port_is_width_supported(down, 2))
  1935. return 0;
  1936. ret = tb_port_lane_bonding_enable(up);
  1937. if (ret) {
  1938. tb_port_warn(up, "failed to enable lane bonding\n");
  1939. return ret;
  1940. }
  1941. ret = tb_port_lane_bonding_enable(down);
  1942. if (ret) {
  1943. tb_port_warn(down, "failed to enable lane bonding\n");
  1944. tb_port_lane_bonding_disable(up);
  1945. return ret;
  1946. }
  1947. tb_switch_update_link_attributes(sw);
  1948. tb_sw_dbg(sw, "lane bonding enabled\n");
  1949. return ret;
  1950. }
  1951. /**
  1952. * tb_switch_lane_bonding_disable() - Disable lane bonding
  1953. * @sw: Switch whose lane bonding to disable
  1954. *
  1955. * Disables lane bonding between @sw and parent. This can be called even
  1956. * if lanes were not bonded originally.
  1957. */
  1958. void tb_switch_lane_bonding_disable(struct tb_switch *sw)
  1959. {
  1960. struct tb_switch *parent = tb_to_switch(sw->dev.parent);
  1961. struct tb_port *up, *down;
  1962. if (!tb_route(sw))
  1963. return;
  1964. up = tb_upstream_port(sw);
  1965. if (!up->bonded)
  1966. return;
  1967. down = tb_port_at(tb_route(sw), parent);
  1968. tb_port_lane_bonding_disable(up);
  1969. tb_port_lane_bonding_disable(down);
  1970. tb_switch_update_link_attributes(sw);
  1971. tb_sw_dbg(sw, "lane bonding disabled\n");
  1972. }
  1973. /**
  1974. * tb_switch_configure_link() - Set link configured
  1975. * @sw: Switch whose link is configured
  1976. *
  1977. * Sets the link upstream from @sw configured (from both ends) so that
  1978. * it will not be disconnected when the domain exits sleep. Can be
  1979. * called for any switch.
  1980. *
  1981. * It is recommended that this is called after lane bonding is enabled.
  1982. *
  1983. * Returns %0 on success and negative errno in case of error.
  1984. */
  1985. int tb_switch_configure_link(struct tb_switch *sw)
  1986. {
  1987. struct tb_port *up, *down;
  1988. int ret;
  1989. if (!tb_route(sw) || tb_switch_is_icm(sw))
  1990. return 0;
  1991. up = tb_upstream_port(sw);
  1992. if (tb_switch_is_usb4(up->sw))
  1993. ret = usb4_port_configure(up);
  1994. else
  1995. ret = tb_lc_configure_port(up);
  1996. if (ret)
  1997. return ret;
  1998. down = up->remote;
  1999. if (tb_switch_is_usb4(down->sw))
  2000. return usb4_port_configure(down);
  2001. return tb_lc_configure_port(down);
  2002. }
  2003. /**
  2004. * tb_switch_unconfigure_link() - Unconfigure link
  2005. * @sw: Switch whose link is unconfigured
  2006. *
  2007. * Sets the link unconfigured so the @sw will be disconnected if the
  2008. * domain exists sleep.
  2009. */
  2010. void tb_switch_unconfigure_link(struct tb_switch *sw)
  2011. {
  2012. struct tb_port *up, *down;
  2013. if (sw->is_unplugged)
  2014. return;
  2015. if (!tb_route(sw) || tb_switch_is_icm(sw))
  2016. return;
  2017. up = tb_upstream_port(sw);
  2018. if (tb_switch_is_usb4(up->sw))
  2019. usb4_port_unconfigure(up);
  2020. else
  2021. tb_lc_unconfigure_port(up);
  2022. down = up->remote;
  2023. if (tb_switch_is_usb4(down->sw))
  2024. usb4_port_unconfigure(down);
  2025. else
  2026. tb_lc_unconfigure_port(down);
  2027. }
  2028. /**
  2029. * tb_switch_add() - Add a switch to the domain
  2030. * @sw: Switch to add
  2031. *
  2032. * This is the last step in adding switch to the domain. It will read
  2033. * identification information from DROM and initializes ports so that
  2034. * they can be used to connect other switches. The switch will be
  2035. * exposed to the userspace when this function successfully returns. To
  2036. * remove and release the switch, call tb_switch_remove().
  2037. *
  2038. * Return: %0 in case of success and negative errno in case of failure
  2039. */
  2040. int tb_switch_add(struct tb_switch *sw)
  2041. {
  2042. int i, ret;
  2043. /*
  2044. * Initialize DMA control port now before we read DROM. Recent
  2045. * host controllers have more complete DROM on NVM that includes
  2046. * vendor and model identification strings which we then expose
  2047. * to the userspace. NVM can be accessed through DMA
  2048. * configuration based mailbox.
  2049. */
  2050. ret = tb_switch_add_dma_port(sw);
  2051. if (ret) {
  2052. dev_err(&sw->dev, "failed to add DMA port\n");
  2053. return ret;
  2054. }
  2055. if (!sw->safe_mode) {
  2056. /* read drom */
  2057. ret = tb_drom_read(sw);
  2058. if (ret) {
  2059. dev_err(&sw->dev, "reading DROM failed\n");
  2060. return ret;
  2061. }
  2062. tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
  2063. ret = tb_switch_set_uuid(sw);
  2064. if (ret) {
  2065. dev_err(&sw->dev, "failed to set UUID\n");
  2066. return ret;
  2067. }
  2068. for (i = 0; i <= sw->config.max_port_number; i++) {
  2069. if (sw->ports[i].disabled) {
  2070. tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
  2071. continue;
  2072. }
  2073. ret = tb_init_port(&sw->ports[i]);
  2074. if (ret) {
  2075. dev_err(&sw->dev, "failed to initialize port %d\n", i);
  2076. return ret;
  2077. }
  2078. }
  2079. tb_switch_default_link_ports(sw);
  2080. ret = tb_switch_update_link_attributes(sw);
  2081. if (ret)
  2082. return ret;
  2083. ret = tb_switch_tmu_init(sw);
  2084. if (ret)
  2085. return ret;
  2086. }
  2087. ret = device_add(&sw->dev);
  2088. if (ret) {
  2089. dev_err(&sw->dev, "failed to add device: %d\n", ret);
  2090. return ret;
  2091. }
  2092. if (tb_route(sw)) {
  2093. dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
  2094. sw->vendor, sw->device);
  2095. if (sw->vendor_name && sw->device_name)
  2096. dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
  2097. sw->device_name);
  2098. }
  2099. ret = tb_switch_nvm_add(sw);
  2100. if (ret) {
  2101. dev_err(&sw->dev, "failed to add NVM devices\n");
  2102. device_del(&sw->dev);
  2103. return ret;
  2104. }
  2105. /*
  2106. * Thunderbolt routers do not generate wakeups themselves but
  2107. * they forward wakeups from tunneled protocols, so enable it
  2108. * here.
  2109. */
  2110. device_init_wakeup(&sw->dev, true);
  2111. pm_runtime_set_active(&sw->dev);
  2112. if (sw->rpm) {
  2113. pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
  2114. pm_runtime_use_autosuspend(&sw->dev);
  2115. pm_runtime_mark_last_busy(&sw->dev);
  2116. pm_runtime_enable(&sw->dev);
  2117. pm_request_autosuspend(&sw->dev);
  2118. }
  2119. tb_switch_debugfs_init(sw);
  2120. return 0;
  2121. }
  2122. /**
  2123. * tb_switch_remove() - Remove and release a switch
  2124. * @sw: Switch to remove
  2125. *
  2126. * This will remove the switch from the domain and release it after last
  2127. * reference count drops to zero. If there are switches connected below
  2128. * this switch, they will be removed as well.
  2129. */
  2130. void tb_switch_remove(struct tb_switch *sw)
  2131. {
  2132. struct tb_port *port;
  2133. tb_switch_debugfs_remove(sw);
  2134. if (sw->rpm) {
  2135. pm_runtime_get_sync(&sw->dev);
  2136. pm_runtime_disable(&sw->dev);
  2137. }
  2138. /* port 0 is the switch itself and never has a remote */
  2139. tb_switch_for_each_port(sw, port) {
  2140. if (tb_port_has_remote(port)) {
  2141. tb_switch_remove(port->remote->sw);
  2142. port->remote = NULL;
  2143. } else if (port->xdomain) {
  2144. tb_xdomain_remove(port->xdomain);
  2145. port->xdomain = NULL;
  2146. }
  2147. /* Remove any downstream retimers */
  2148. tb_retimer_remove_all(port);
  2149. }
  2150. if (!sw->is_unplugged)
  2151. tb_plug_events_active(sw, false);
  2152. tb_switch_nvm_remove(sw);
  2153. if (tb_route(sw))
  2154. dev_info(&sw->dev, "device disconnected\n");
  2155. device_unregister(&sw->dev);
  2156. }
  2157. /**
  2158. * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
  2159. */
  2160. void tb_sw_set_unplugged(struct tb_switch *sw)
  2161. {
  2162. struct tb_port *port;
  2163. if (sw == sw->tb->root_switch) {
  2164. tb_sw_WARN(sw, "cannot unplug root switch\n");
  2165. return;
  2166. }
  2167. if (sw->is_unplugged) {
  2168. tb_sw_WARN(sw, "is_unplugged already set\n");
  2169. return;
  2170. }
  2171. sw->is_unplugged = true;
  2172. tb_switch_for_each_port(sw, port) {
  2173. if (tb_port_has_remote(port))
  2174. tb_sw_set_unplugged(port->remote->sw);
  2175. else if (port->xdomain)
  2176. port->xdomain->is_unplugged = true;
  2177. }
  2178. }
  2179. static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags)
  2180. {
  2181. if (flags)
  2182. tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
  2183. else
  2184. tb_sw_dbg(sw, "disabling wakeup\n");
  2185. if (tb_switch_is_usb4(sw))
  2186. return usb4_switch_set_wake(sw, flags);
  2187. return tb_lc_set_wake(sw, flags);
  2188. }
  2189. int tb_switch_resume(struct tb_switch *sw)
  2190. {
  2191. struct tb_port *port;
  2192. int err;
  2193. tb_sw_dbg(sw, "resuming switch\n");
  2194. /*
  2195. * Check for UID of the connected switches except for root
  2196. * switch which we assume cannot be removed.
  2197. */
  2198. if (tb_route(sw)) {
  2199. u64 uid;
  2200. /*
  2201. * Check first that we can still read the switch config
  2202. * space. It may be that there is now another domain
  2203. * connected.
  2204. */
  2205. err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
  2206. if (err < 0) {
  2207. tb_sw_info(sw, "switch not present anymore\n");
  2208. return err;
  2209. }
  2210. if (tb_switch_is_usb4(sw))
  2211. err = usb4_switch_read_uid(sw, &uid);
  2212. else
  2213. err = tb_drom_read_uid_only(sw, &uid);
  2214. if (err) {
  2215. tb_sw_warn(sw, "uid read failed\n");
  2216. return err;
  2217. }
  2218. if (sw->uid != uid) {
  2219. tb_sw_info(sw,
  2220. "changed while suspended (uid %#llx -> %#llx)\n",
  2221. sw->uid, uid);
  2222. return -ENODEV;
  2223. }
  2224. }
  2225. err = tb_switch_configure(sw);
  2226. if (err)
  2227. return err;
  2228. /* Disable wakes */
  2229. tb_switch_set_wake(sw, 0);
  2230. err = tb_switch_tmu_init(sw);
  2231. if (err)
  2232. return err;
  2233. /* check for surviving downstream switches */
  2234. tb_switch_for_each_port(sw, port) {
  2235. if (!tb_port_has_remote(port) && !port->xdomain)
  2236. continue;
  2237. if (tb_wait_for_port(port, true) <= 0) {
  2238. tb_port_warn(port,
  2239. "lost during suspend, disconnecting\n");
  2240. if (tb_port_has_remote(port))
  2241. tb_sw_set_unplugged(port->remote->sw);
  2242. else if (port->xdomain)
  2243. port->xdomain->is_unplugged = true;
  2244. } else if (tb_port_has_remote(port) || port->xdomain) {
  2245. /*
  2246. * Always unlock the port so the downstream
  2247. * switch/domain is accessible.
  2248. */
  2249. if (tb_port_unlock(port))
  2250. tb_port_warn(port, "failed to unlock port\n");
  2251. if (port->remote && tb_switch_resume(port->remote->sw)) {
  2252. tb_port_warn(port,
  2253. "lost during suspend, disconnecting\n");
  2254. tb_sw_set_unplugged(port->remote->sw);
  2255. }
  2256. }
  2257. }
  2258. return 0;
  2259. }
  2260. /**
  2261. * tb_switch_suspend() - Put a switch to sleep
  2262. * @sw: Switch to suspend
  2263. * @runtime: Is this runtime suspend or system sleep
  2264. *
  2265. * Suspends router and all its children. Enables wakes according to
  2266. * value of @runtime and then sets sleep bit for the router. If @sw is
  2267. * host router the domain is ready to go to sleep once this function
  2268. * returns.
  2269. */
  2270. void tb_switch_suspend(struct tb_switch *sw, bool runtime)
  2271. {
  2272. unsigned int flags = 0;
  2273. struct tb_port *port;
  2274. int err;
  2275. tb_sw_dbg(sw, "suspending switch\n");
  2276. err = tb_plug_events_active(sw, false);
  2277. if (err)
  2278. return;
  2279. tb_switch_for_each_port(sw, port) {
  2280. if (tb_port_has_remote(port))
  2281. tb_switch_suspend(port->remote->sw, runtime);
  2282. }
  2283. if (runtime) {
  2284. /* Trigger wake when something is plugged in/out */
  2285. flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT;
  2286. flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
  2287. } else if (device_may_wakeup(&sw->dev)) {
  2288. flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
  2289. }
  2290. tb_switch_set_wake(sw, flags);
  2291. if (tb_switch_is_usb4(sw))
  2292. usb4_switch_set_sleep(sw);
  2293. else
  2294. tb_lc_set_sleep(sw);
  2295. }
  2296. /**
  2297. * tb_switch_query_dp_resource() - Query availability of DP resource
  2298. * @sw: Switch whose DP resource is queried
  2299. * @in: DP IN port
  2300. *
  2301. * Queries availability of DP resource for DP tunneling using switch
  2302. * specific means. Returns %true if resource is available.
  2303. */
  2304. bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
  2305. {
  2306. if (tb_switch_is_usb4(sw))
  2307. return usb4_switch_query_dp_resource(sw, in);
  2308. return tb_lc_dp_sink_query(sw, in);
  2309. }
  2310. /**
  2311. * tb_switch_alloc_dp_resource() - Allocate available DP resource
  2312. * @sw: Switch whose DP resource is allocated
  2313. * @in: DP IN port
  2314. *
  2315. * Allocates DP resource for DP tunneling. The resource must be
  2316. * available for this to succeed (see tb_switch_query_dp_resource()).
  2317. * Returns %0 in success and negative errno otherwise.
  2318. */
  2319. int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  2320. {
  2321. if (tb_switch_is_usb4(sw))
  2322. return usb4_switch_alloc_dp_resource(sw, in);
  2323. return tb_lc_dp_sink_alloc(sw, in);
  2324. }
  2325. /**
  2326. * tb_switch_dealloc_dp_resource() - De-allocate DP resource
  2327. * @sw: Switch whose DP resource is de-allocated
  2328. * @in: DP IN port
  2329. *
  2330. * De-allocates DP resource that was previously allocated for DP
  2331. * tunneling.
  2332. */
  2333. void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
  2334. {
  2335. int ret;
  2336. if (tb_switch_is_usb4(sw))
  2337. ret = usb4_switch_dealloc_dp_resource(sw, in);
  2338. else
  2339. ret = tb_lc_dp_sink_dealloc(sw, in);
  2340. if (ret)
  2341. tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
  2342. in->port);
  2343. }
  2344. struct tb_sw_lookup {
  2345. struct tb *tb;
  2346. u8 link;
  2347. u8 depth;
  2348. const uuid_t *uuid;
  2349. u64 route;
  2350. };
  2351. static int tb_switch_match(struct device *dev, const void *data)
  2352. {
  2353. struct tb_switch *sw = tb_to_switch(dev);
  2354. const struct tb_sw_lookup *lookup = data;
  2355. if (!sw)
  2356. return 0;
  2357. if (sw->tb != lookup->tb)
  2358. return 0;
  2359. if (lookup->uuid)
  2360. return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
  2361. if (lookup->route) {
  2362. return sw->config.route_lo == lower_32_bits(lookup->route) &&
  2363. sw->config.route_hi == upper_32_bits(lookup->route);
  2364. }
  2365. /* Root switch is matched only by depth */
  2366. if (!lookup->depth)
  2367. return !sw->depth;
  2368. return sw->link == lookup->link && sw->depth == lookup->depth;
  2369. }
  2370. /**
  2371. * tb_switch_find_by_link_depth() - Find switch by link and depth
  2372. * @tb: Domain the switch belongs
  2373. * @link: Link number the switch is connected
  2374. * @depth: Depth of the switch in link
  2375. *
  2376. * Returned switch has reference count increased so the caller needs to
  2377. * call tb_switch_put() when done with the switch.
  2378. */
  2379. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
  2380. {
  2381. struct tb_sw_lookup lookup;
  2382. struct device *dev;
  2383. memset(&lookup, 0, sizeof(lookup));
  2384. lookup.tb = tb;
  2385. lookup.link = link;
  2386. lookup.depth = depth;
  2387. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  2388. if (dev)
  2389. return tb_to_switch(dev);
  2390. return NULL;
  2391. }
  2392. /**
  2393. * tb_switch_find_by_uuid() - Find switch by UUID
  2394. * @tb: Domain the switch belongs
  2395. * @uuid: UUID to look for
  2396. *
  2397. * Returned switch has reference count increased so the caller needs to
  2398. * call tb_switch_put() when done with the switch.
  2399. */
  2400. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  2401. {
  2402. struct tb_sw_lookup lookup;
  2403. struct device *dev;
  2404. memset(&lookup, 0, sizeof(lookup));
  2405. lookup.tb = tb;
  2406. lookup.uuid = uuid;
  2407. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  2408. if (dev)
  2409. return tb_to_switch(dev);
  2410. return NULL;
  2411. }
  2412. /**
  2413. * tb_switch_find_by_route() - Find switch by route string
  2414. * @tb: Domain the switch belongs
  2415. * @route: Route string to look for
  2416. *
  2417. * Returned switch has reference count increased so the caller needs to
  2418. * call tb_switch_put() when done with the switch.
  2419. */
  2420. struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
  2421. {
  2422. struct tb_sw_lookup lookup;
  2423. struct device *dev;
  2424. if (!route)
  2425. return tb_switch_get(tb->root_switch);
  2426. memset(&lookup, 0, sizeof(lookup));
  2427. lookup.tb = tb;
  2428. lookup.route = route;
  2429. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  2430. if (dev)
  2431. return tb_to_switch(dev);
  2432. return NULL;
  2433. }
  2434. /**
  2435. * tb_switch_find_port() - return the first port of @type on @sw or NULL
  2436. * @sw: Switch to find the port from
  2437. * @type: Port type to look for
  2438. */
  2439. struct tb_port *tb_switch_find_port(struct tb_switch *sw,
  2440. enum tb_port_type type)
  2441. {
  2442. struct tb_port *port;
  2443. tb_switch_for_each_port(sw, port) {
  2444. if (port->config.type == type)
  2445. return port;
  2446. }
  2447. return NULL;
  2448. }