tunnel.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - Tunneling support
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2019, Intel Corporation
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/list.h>
  11. #include <linux/ktime.h>
  12. #include <linux/string_helpers.h>
  13. #include "tunnel.h"
  14. #include "tb.h"
  15. /* PCIe adapters use always HopID of 8 for both directions */
  16. #define TB_PCI_HOPID 8
  17. #define TB_PCI_PATH_DOWN 0
  18. #define TB_PCI_PATH_UP 1
  19. #define TB_PCI_PRIORITY 3
  20. #define TB_PCI_WEIGHT 1
  21. /* USB3 adapters use always HopID of 8 for both directions */
  22. #define TB_USB3_HOPID 8
  23. #define TB_USB3_PATH_DOWN 0
  24. #define TB_USB3_PATH_UP 1
  25. #define TB_USB3_PRIORITY 3
  26. #define TB_USB3_WEIGHT 2
  27. /* DP adapters use HopID 8 for AUX and 9 for Video */
  28. #define TB_DP_AUX_TX_HOPID 8
  29. #define TB_DP_AUX_RX_HOPID 8
  30. #define TB_DP_VIDEO_HOPID 9
  31. #define TB_DP_VIDEO_PATH_OUT 0
  32. #define TB_DP_AUX_PATH_OUT 1
  33. #define TB_DP_AUX_PATH_IN 2
  34. #define TB_DP_VIDEO_PRIORITY 1
  35. #define TB_DP_VIDEO_WEIGHT 1
  36. #define TB_DP_AUX_PRIORITY 2
  37. #define TB_DP_AUX_WEIGHT 1
  38. /* Minimum number of credits needed for PCIe path */
  39. #define TB_MIN_PCIE_CREDITS 6U
  40. /*
  41. * Number of credits we try to allocate for each DMA path if not limited
  42. * by the host router baMaxHI.
  43. */
  44. #define TB_DMA_CREDITS 14
  45. /* Minimum number of credits for DMA path */
  46. #define TB_MIN_DMA_CREDITS 1
  47. #define TB_DMA_PRIORITY 5
  48. #define TB_DMA_WEIGHT 1
  49. /*
  50. * Reserve additional bandwidth for USB 3.x and PCIe bulk traffic
  51. * according to USB4 v2 Connection Manager guide. This ends up reserving
  52. * 1500 Mb/s for PCIe and 3000 Mb/s for USB 3.x taking weights into
  53. * account.
  54. */
  55. #define USB4_V2_PCI_MIN_BANDWIDTH (1500 * TB_PCI_WEIGHT)
  56. #define USB4_V2_USB3_MIN_BANDWIDTH (1500 * TB_USB3_WEIGHT)
  57. /*
  58. * According to VESA spec, the DPRX negotiation shall compete in 5
  59. * seconds after tunnel is established. Since at least i915 can runtime
  60. * suspend if there is nothing connected, and that it polls any new
  61. * connections every 10 seconds, we use 12 seconds here.
  62. *
  63. * These are in ms.
  64. */
  65. #define TB_DPRX_TIMEOUT 12000
  66. #define TB_DPRX_WAIT_TIMEOUT 25
  67. #define TB_DPRX_POLL_DELAY 50
  68. static int dprx_timeout = TB_DPRX_TIMEOUT;
  69. module_param(dprx_timeout, int, 0444);
  70. MODULE_PARM_DESC(dprx_timeout,
  71. "DPRX capability read timeout in ms, -1 waits forever (default: "
  72. __MODULE_STRING(TB_DPRX_TIMEOUT) ")");
  73. static unsigned int dma_credits = TB_DMA_CREDITS;
  74. module_param(dma_credits, uint, 0444);
  75. MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: "
  76. __MODULE_STRING(TB_DMA_CREDITS) ")");
  77. static bool bw_alloc_mode = true;
  78. module_param(bw_alloc_mode, bool, 0444);
  79. MODULE_PARM_DESC(bw_alloc_mode,
  80. "enable bandwidth allocation mode if supported (default: true)");
  81. static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
  82. static const char * const tb_event_names[] = {
  83. [TB_TUNNEL_ACTIVATED] = "activated",
  84. [TB_TUNNEL_CHANGED] = "changed",
  85. [TB_TUNNEL_DEACTIVATED] = "deactivated",
  86. [TB_TUNNEL_LOW_BANDWIDTH] = "low bandwidth",
  87. [TB_TUNNEL_NO_BANDWIDTH] = "insufficient bandwidth",
  88. };
  89. /* Synchronizes kref_get()/put() of struct tb_tunnel */
  90. static DEFINE_MUTEX(tb_tunnel_lock);
  91. static inline unsigned int tb_usable_credits(const struct tb_port *port)
  92. {
  93. return port->total_credits - port->ctl_credits;
  94. }
  95. /**
  96. * tb_available_credits() - Available credits for PCIe and DMA
  97. * @port: Lane adapter to check
  98. * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
  99. * streams possible through this lane adapter
  100. *
  101. * Return: Number of available credits.
  102. */
  103. static unsigned int tb_available_credits(const struct tb_port *port,
  104. size_t *max_dp_streams)
  105. {
  106. const struct tb_switch *sw = port->sw;
  107. int credits, usb3, pcie, spare;
  108. size_t ndp;
  109. usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
  110. pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
  111. if (tb_acpi_is_xdomain_allowed()) {
  112. spare = min_not_zero(sw->max_dma_credits, dma_credits);
  113. /* Add some credits for potential second DMA tunnel */
  114. spare += TB_MIN_DMA_CREDITS;
  115. } else {
  116. spare = 0;
  117. }
  118. credits = tb_usable_credits(port);
  119. if (tb_acpi_may_tunnel_dp()) {
  120. /*
  121. * Maximum number of DP streams possible through the
  122. * lane adapter.
  123. */
  124. if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
  125. ndp = (credits - (usb3 + pcie + spare)) /
  126. (sw->min_dp_aux_credits + sw->min_dp_main_credits);
  127. else
  128. ndp = 0;
  129. } else {
  130. ndp = 0;
  131. }
  132. credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
  133. credits -= usb3;
  134. if (max_dp_streams)
  135. *max_dp_streams = ndp;
  136. return credits > 0 ? credits : 0;
  137. }
  138. static void tb_init_pm_support(struct tb_path_hop *hop)
  139. {
  140. struct tb_port *out_port = hop->out_port;
  141. struct tb_port *in_port = hop->in_port;
  142. if (tb_port_is_null(in_port) && tb_port_is_null(out_port) &&
  143. usb4_switch_version(in_port->sw) >= 2)
  144. hop->pm_support = true;
  145. }
  146. static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
  147. enum tb_tunnel_type type)
  148. {
  149. struct tb_tunnel *tunnel;
  150. tunnel = kzalloc_obj(*tunnel);
  151. if (!tunnel)
  152. return NULL;
  153. tunnel->paths = kzalloc_objs(tunnel->paths[0], npaths);
  154. if (!tunnel->paths) {
  155. kfree(tunnel);
  156. return NULL;
  157. }
  158. INIT_LIST_HEAD(&tunnel->list);
  159. tunnel->tb = tb;
  160. tunnel->npaths = npaths;
  161. tunnel->type = type;
  162. kref_init(&tunnel->kref);
  163. return tunnel;
  164. }
  165. static void tb_tunnel_get(struct tb_tunnel *tunnel)
  166. {
  167. mutex_lock(&tb_tunnel_lock);
  168. kref_get(&tunnel->kref);
  169. mutex_unlock(&tb_tunnel_lock);
  170. }
  171. static void tb_tunnel_destroy(struct kref *kref)
  172. {
  173. struct tb_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
  174. int i;
  175. if (tunnel->destroy)
  176. tunnel->destroy(tunnel);
  177. for (i = 0; i < tunnel->npaths; i++) {
  178. if (tunnel->paths[i])
  179. tb_path_free(tunnel->paths[i]);
  180. }
  181. kfree(tunnel->paths);
  182. kfree(tunnel);
  183. }
  184. void tb_tunnel_put(struct tb_tunnel *tunnel)
  185. {
  186. mutex_lock(&tb_tunnel_lock);
  187. kref_put(&tunnel->kref, tb_tunnel_destroy);
  188. mutex_unlock(&tb_tunnel_lock);
  189. }
  190. /**
  191. * tb_tunnel_event() - Notify userspace about tunneling event
  192. * @tb: Domain where the event occurred
  193. * @event: Event that happened
  194. * @type: Type of the tunnel in question
  195. * @src_port: Tunnel source port (can be %NULL)
  196. * @dst_port: Tunnel destination port (can be %NULL)
  197. *
  198. * Notifies userspace about tunneling @event in the domain. The tunnel
  199. * does not need to exist (e.g the tunnel was not activated because
  200. * there is not enough bandwidth). If the @src_port and @dst_port are
  201. * given fill in full %TUNNEL_DETAILS environment variable. Otherwise
  202. * uses the shorter one (just the tunnel type).
  203. */
  204. void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event,
  205. enum tb_tunnel_type type,
  206. const struct tb_port *src_port,
  207. const struct tb_port *dst_port)
  208. {
  209. char *envp[3] = { NULL };
  210. if (WARN_ON_ONCE(event >= ARRAY_SIZE(tb_event_names)))
  211. return;
  212. if (WARN_ON_ONCE(type >= ARRAY_SIZE(tb_tunnel_names)))
  213. return;
  214. envp[0] = kasprintf(GFP_KERNEL, "TUNNEL_EVENT=%s", tb_event_names[event]);
  215. if (!envp[0])
  216. return;
  217. if (src_port != NULL && dst_port != NULL) {
  218. envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=%llx:%u <-> %llx:%u (%s)",
  219. tb_route(src_port->sw), src_port->port,
  220. tb_route(dst_port->sw), dst_port->port,
  221. tb_tunnel_names[type]);
  222. } else {
  223. envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=(%s)",
  224. tb_tunnel_names[type]);
  225. }
  226. if (envp[1])
  227. tb_domain_event(tb, envp);
  228. kfree(envp[1]);
  229. kfree(envp[0]);
  230. }
  231. static inline void tb_tunnel_set_active(struct tb_tunnel *tunnel, bool active)
  232. {
  233. if (active) {
  234. tunnel->state = TB_TUNNEL_ACTIVE;
  235. tb_tunnel_event(tunnel->tb, TB_TUNNEL_ACTIVATED, tunnel->type,
  236. tunnel->src_port, tunnel->dst_port);
  237. } else {
  238. tunnel->state = TB_TUNNEL_INACTIVE;
  239. tb_tunnel_event(tunnel->tb, TB_TUNNEL_DEACTIVATED, tunnel->type,
  240. tunnel->src_port, tunnel->dst_port);
  241. }
  242. }
  243. static inline void tb_tunnel_changed(struct tb_tunnel *tunnel)
  244. {
  245. tb_tunnel_event(tunnel->tb, TB_TUNNEL_CHANGED, tunnel->type,
  246. tunnel->src_port, tunnel->dst_port);
  247. }
  248. static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable)
  249. {
  250. struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw);
  251. int ret;
  252. /* Only supported if both routers are at least USB4 v2 */
  253. if ((usb4_switch_version(tunnel->src_port->sw) < 2) ||
  254. (usb4_switch_version(tunnel->dst_port->sw) < 2))
  255. return 0;
  256. if (enable && tb_port_get_link_generation(port) < 4)
  257. return 0;
  258. ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable);
  259. if (ret)
  260. return ret;
  261. /*
  262. * Downstream router could be unplugged so disable of encapsulation
  263. * in upstream router is still possible.
  264. */
  265. ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable);
  266. if (ret) {
  267. if (enable)
  268. return ret;
  269. if (ret != -ENODEV)
  270. return ret;
  271. }
  272. tb_tunnel_dbg(tunnel, "extended encapsulation %s\n",
  273. str_enabled_disabled(enable));
  274. return 0;
  275. }
  276. static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
  277. {
  278. int res;
  279. if (activate) {
  280. res = tb_pci_set_ext_encapsulation(tunnel, activate);
  281. if (res)
  282. return res;
  283. }
  284. if (activate)
  285. res = tb_pci_port_enable(tunnel->dst_port, activate);
  286. else
  287. res = tb_pci_port_enable(tunnel->src_port, activate);
  288. if (res)
  289. return res;
  290. if (activate) {
  291. res = tb_pci_port_enable(tunnel->src_port, activate);
  292. if (res)
  293. return res;
  294. } else {
  295. /* Downstream router could be unplugged */
  296. tb_pci_port_enable(tunnel->dst_port, activate);
  297. }
  298. return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate);
  299. }
  300. static int tb_pci_init_credits(struct tb_path_hop *hop)
  301. {
  302. struct tb_port *port = hop->in_port;
  303. struct tb_switch *sw = port->sw;
  304. unsigned int credits;
  305. if (tb_port_use_credit_allocation(port)) {
  306. unsigned int available;
  307. available = tb_available_credits(port, NULL);
  308. credits = min(sw->max_pcie_credits, available);
  309. if (credits < TB_MIN_PCIE_CREDITS)
  310. return -ENOSPC;
  311. credits = max(TB_MIN_PCIE_CREDITS, credits);
  312. } else {
  313. if (tb_port_is_null(port))
  314. credits = port->bonded ? 32 : 16;
  315. else
  316. credits = 7;
  317. }
  318. hop->initial_credits = credits;
  319. return 0;
  320. }
  321. static int tb_pci_init_path(struct tb_path *path)
  322. {
  323. struct tb_path_hop *hop;
  324. path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
  325. path->egress_shared_buffer = TB_PATH_NONE;
  326. path->ingress_fc_enable = TB_PATH_ALL;
  327. path->ingress_shared_buffer = TB_PATH_NONE;
  328. path->priority = TB_PCI_PRIORITY;
  329. path->weight = TB_PCI_WEIGHT;
  330. path->drop_packages = 0;
  331. tb_path_for_each_hop(path, hop) {
  332. int ret;
  333. ret = tb_pci_init_credits(hop);
  334. if (ret)
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
  341. * @tb: Pointer to the domain structure
  342. * @down: PCIe downstream adapter
  343. * @alloc_hopid: Allocate HopIDs from visited ports
  344. *
  345. * If @down adapter is active, follows the tunnel to the PCIe upstream
  346. * adapter and back.
  347. *
  348. * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
  349. */
  350. struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
  351. bool alloc_hopid)
  352. {
  353. struct tb_tunnel *tunnel;
  354. struct tb_path *path;
  355. if (!tb_pci_port_is_enabled(down))
  356. return NULL;
  357. tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
  358. if (!tunnel)
  359. return NULL;
  360. tunnel->activate = tb_pci_activate;
  361. tunnel->src_port = down;
  362. /*
  363. * Discover both paths even if they are not complete. We will
  364. * clean them up by calling tb_tunnel_deactivate() below in that
  365. * case.
  366. */
  367. path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
  368. &tunnel->dst_port, "PCIe Up", alloc_hopid);
  369. if (!path) {
  370. /* Just disable the downstream port */
  371. tb_pci_port_enable(down, false);
  372. goto err_free;
  373. }
  374. tunnel->paths[TB_PCI_PATH_UP] = path;
  375. if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
  376. goto err_free;
  377. path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
  378. "PCIe Down", alloc_hopid);
  379. if (!path)
  380. goto err_deactivate;
  381. tunnel->paths[TB_PCI_PATH_DOWN] = path;
  382. if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]))
  383. goto err_deactivate;
  384. /* Validate that the tunnel is complete */
  385. if (!tb_port_is_pcie_up(tunnel->dst_port)) {
  386. tb_port_warn(tunnel->dst_port,
  387. "path does not end on a PCIe adapter, cleaning up\n");
  388. goto err_deactivate;
  389. }
  390. if (down != tunnel->src_port) {
  391. tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
  392. goto err_deactivate;
  393. }
  394. if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
  395. tb_tunnel_warn(tunnel,
  396. "tunnel is not fully activated, cleaning up\n");
  397. goto err_deactivate;
  398. }
  399. tb_tunnel_dbg(tunnel, "discovered\n");
  400. return tunnel;
  401. err_deactivate:
  402. tb_tunnel_deactivate(tunnel);
  403. err_free:
  404. tb_tunnel_put(tunnel);
  405. return NULL;
  406. }
  407. /**
  408. * tb_tunnel_alloc_pci() - allocate a pci tunnel
  409. * @tb: Pointer to the domain structure
  410. * @up: PCIe upstream adapter port
  411. * @down: PCIe downstream adapter port
  412. *
  413. * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
  414. * TB_TYPE_PCIE_DOWN.
  415. *
  416. * Return: Pointer to @struct tb_tunnel or %NULL on failure.
  417. */
  418. struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
  419. struct tb_port *down)
  420. {
  421. struct tb_tunnel *tunnel;
  422. struct tb_path *path;
  423. tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
  424. if (!tunnel)
  425. return NULL;
  426. tunnel->activate = tb_pci_activate;
  427. tunnel->src_port = down;
  428. tunnel->dst_port = up;
  429. path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
  430. "PCIe Down");
  431. if (!path)
  432. goto err_free;
  433. tunnel->paths[TB_PCI_PATH_DOWN] = path;
  434. if (tb_pci_init_path(path))
  435. goto err_free;
  436. path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
  437. "PCIe Up");
  438. if (!path)
  439. goto err_free;
  440. tunnel->paths[TB_PCI_PATH_UP] = path;
  441. if (tb_pci_init_path(path))
  442. goto err_free;
  443. return tunnel;
  444. err_free:
  445. tb_tunnel_put(tunnel);
  446. return NULL;
  447. }
  448. /**
  449. * tb_tunnel_reserved_pci() - Amount of bandwidth to reserve for PCIe
  450. * @port: Lane 0 adapter
  451. * @reserved_up: Upstream bandwidth in Mb/s to reserve
  452. * @reserved_down: Downstream bandwidth in Mb/s to reserve
  453. *
  454. * Can be called to any connected lane 0 adapter to find out how much
  455. * bandwidth needs to be left in reserve for possible PCIe bulk traffic.
  456. *
  457. * Return:
  458. * * %true - If there is something to be reserved. Writes the amount to
  459. * @reserved_down/@reserved_up.
  460. * * %false - Nothing to be reserved. Leaves @reserved_down/@reserved_up
  461. * unmodified.
  462. */
  463. bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
  464. int *reserved_down)
  465. {
  466. if (WARN_ON_ONCE(!port->remote))
  467. return false;
  468. if (!tb_acpi_may_tunnel_pcie())
  469. return false;
  470. if (tb_port_get_link_generation(port) < 4)
  471. return false;
  472. /* Must have PCIe adapters */
  473. if (tb_is_upstream_port(port)) {
  474. if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_UP))
  475. return false;
  476. if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_DOWN))
  477. return false;
  478. } else {
  479. if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_DOWN))
  480. return false;
  481. if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_UP))
  482. return false;
  483. }
  484. *reserved_up = USB4_V2_PCI_MIN_BANDWIDTH;
  485. *reserved_down = USB4_V2_PCI_MIN_BANDWIDTH;
  486. tb_port_dbg(port, "reserving %u/%u Mb/s for PCIe\n", *reserved_up,
  487. *reserved_down);
  488. return true;
  489. }
  490. static bool tb_dp_is_usb4(const struct tb_switch *sw)
  491. {
  492. /* Titan Ridge DP adapters need the same treatment as USB4 */
  493. return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
  494. }
  495. static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out,
  496. int timeout_msec)
  497. {
  498. ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
  499. u32 val;
  500. int ret;
  501. /* Both ends need to support this */
  502. if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
  503. return 0;
  504. ret = tb_port_read(out, &val, TB_CFG_PORT,
  505. out->cap_adap + DP_STATUS_CTRL, 1);
  506. if (ret)
  507. return ret;
  508. val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
  509. ret = tb_port_write(out, &val, TB_CFG_PORT,
  510. out->cap_adap + DP_STATUS_CTRL, 1);
  511. if (ret)
  512. return ret;
  513. do {
  514. ret = tb_port_read(out, &val, TB_CFG_PORT,
  515. out->cap_adap + DP_STATUS_CTRL, 1);
  516. if (ret)
  517. return ret;
  518. if (!(val & DP_STATUS_CTRL_CMHS))
  519. return 0;
  520. usleep_range(100, 150);
  521. } while (ktime_before(ktime_get(), timeout));
  522. return -ETIMEDOUT;
  523. }
  524. /*
  525. * Returns maximum possible rate from capability supporting only DP 2.0
  526. * and below. Used when DP BW allocation mode is not enabled.
  527. */
  528. static inline u32 tb_dp_cap_get_rate(u32 val)
  529. {
  530. u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
  531. switch (rate) {
  532. case DP_COMMON_CAP_RATE_RBR:
  533. return 1620;
  534. case DP_COMMON_CAP_RATE_HBR:
  535. return 2700;
  536. case DP_COMMON_CAP_RATE_HBR2:
  537. return 5400;
  538. case DP_COMMON_CAP_RATE_HBR3:
  539. return 8100;
  540. default:
  541. return 0;
  542. }
  543. }
  544. /*
  545. * Returns maximum possible rate from capability supporting DP 2.1
  546. * UHBR20, 13.5 and 10 rates as well. Use only when DP BW allocation
  547. * mode is enabled.
  548. */
  549. static inline u32 tb_dp_cap_get_rate_ext(u32 val)
  550. {
  551. if (val & DP_COMMON_CAP_UHBR20)
  552. return 20000;
  553. else if (val & DP_COMMON_CAP_UHBR13_5)
  554. return 13500;
  555. else if (val & DP_COMMON_CAP_UHBR10)
  556. return 10000;
  557. return tb_dp_cap_get_rate(val);
  558. }
  559. static inline bool tb_dp_is_uhbr_rate(unsigned int rate)
  560. {
  561. return rate >= 10000;
  562. }
  563. static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
  564. {
  565. val &= ~DP_COMMON_CAP_RATE_MASK;
  566. switch (rate) {
  567. default:
  568. WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
  569. fallthrough;
  570. case 1620:
  571. val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
  572. break;
  573. case 2700:
  574. val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
  575. break;
  576. case 5400:
  577. val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
  578. break;
  579. case 8100:
  580. val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
  581. break;
  582. }
  583. return val;
  584. }
  585. static inline u32 tb_dp_cap_get_lanes(u32 val)
  586. {
  587. u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
  588. switch (lanes) {
  589. case DP_COMMON_CAP_1_LANE:
  590. return 1;
  591. case DP_COMMON_CAP_2_LANES:
  592. return 2;
  593. case DP_COMMON_CAP_4_LANES:
  594. return 4;
  595. default:
  596. return 0;
  597. }
  598. }
  599. static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
  600. {
  601. val &= ~DP_COMMON_CAP_LANES_MASK;
  602. switch (lanes) {
  603. default:
  604. WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
  605. lanes);
  606. fallthrough;
  607. case 1:
  608. val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
  609. break;
  610. case 2:
  611. val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
  612. break;
  613. case 4:
  614. val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
  615. break;
  616. }
  617. return val;
  618. }
  619. static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
  620. {
  621. /* Tunneling removes the DP 8b/10b 128/132b encoding */
  622. if (tb_dp_is_uhbr_rate(rate))
  623. return rate * lanes * 128 / 132;
  624. return rate * lanes * 8 / 10;
  625. }
  626. static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
  627. u32 out_rate, u32 out_lanes, u32 *new_rate,
  628. u32 *new_lanes)
  629. {
  630. static const u32 dp_bw[][2] = {
  631. /* Mb/s, lanes */
  632. { 8100, 4 }, /* 25920 Mb/s */
  633. { 5400, 4 }, /* 17280 Mb/s */
  634. { 8100, 2 }, /* 12960 Mb/s */
  635. { 2700, 4 }, /* 8640 Mb/s */
  636. { 5400, 2 }, /* 8640 Mb/s */
  637. { 8100, 1 }, /* 6480 Mb/s */
  638. { 1620, 4 }, /* 5184 Mb/s */
  639. { 5400, 1 }, /* 4320 Mb/s */
  640. { 2700, 2 }, /* 4320 Mb/s */
  641. { 1620, 2 }, /* 2592 Mb/s */
  642. { 2700, 1 }, /* 2160 Mb/s */
  643. { 1620, 1 }, /* 1296 Mb/s */
  644. };
  645. unsigned int i;
  646. /*
  647. * Find a combination that can fit into max_bw and does not
  648. * exceed the maximum rate and lanes supported by the DP OUT and
  649. * DP IN adapters.
  650. */
  651. for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
  652. if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
  653. continue;
  654. if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
  655. continue;
  656. if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
  657. *new_rate = dp_bw[i][0];
  658. *new_lanes = dp_bw[i][1];
  659. return 0;
  660. }
  661. }
  662. return -ENOSR;
  663. }
  664. static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
  665. {
  666. u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
  667. struct tb_port *out = tunnel->dst_port;
  668. struct tb_port *in = tunnel->src_port;
  669. int ret, max_bw;
  670. /*
  671. * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
  672. * newer generation hardware.
  673. */
  674. if (in->sw->generation < 2 || out->sw->generation < 2)
  675. return 0;
  676. /*
  677. * Perform connection manager handshake between IN and OUT ports
  678. * before capabilities exchange can take place.
  679. */
  680. ret = tb_dp_cm_handshake(in, out, 3000);
  681. if (ret)
  682. return ret;
  683. /* Read both DP_LOCAL_CAP registers */
  684. ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
  685. in->cap_adap + DP_LOCAL_CAP, 1);
  686. if (ret)
  687. return ret;
  688. ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
  689. out->cap_adap + DP_LOCAL_CAP, 1);
  690. if (ret)
  691. return ret;
  692. /* Write IN local caps to OUT remote caps */
  693. ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
  694. out->cap_adap + DP_REMOTE_CAP, 1);
  695. if (ret)
  696. return ret;
  697. in_rate = tb_dp_cap_get_rate(in_dp_cap);
  698. in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
  699. tb_tunnel_dbg(tunnel,
  700. "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
  701. in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
  702. /*
  703. * If the tunnel bandwidth is limited (max_bw is set) then see
  704. * if we need to reduce bandwidth to fit there.
  705. */
  706. out_rate = tb_dp_cap_get_rate(out_dp_cap);
  707. out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
  708. bw = tb_dp_bandwidth(out_rate, out_lanes);
  709. tb_tunnel_dbg(tunnel,
  710. "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
  711. out_rate, out_lanes, bw);
  712. if (tb_tunnel_direction_downstream(tunnel))
  713. max_bw = tunnel->max_down;
  714. else
  715. max_bw = tunnel->max_up;
  716. if (max_bw && bw > max_bw) {
  717. u32 new_rate, new_lanes, new_bw;
  718. ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
  719. out_rate, out_lanes, &new_rate,
  720. &new_lanes);
  721. if (ret) {
  722. tb_tunnel_info(tunnel, "not enough bandwidth\n");
  723. return ret;
  724. }
  725. new_bw = tb_dp_bandwidth(new_rate, new_lanes);
  726. tb_tunnel_dbg(tunnel,
  727. "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
  728. new_rate, new_lanes, new_bw);
  729. /*
  730. * Set new rate and number of lanes before writing it to
  731. * the IN port remote caps.
  732. */
  733. out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
  734. out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
  735. }
  736. /*
  737. * Titan Ridge does not disable AUX timers when it gets
  738. * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with
  739. * DP tunneling.
  740. */
  741. if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) {
  742. out_dp_cap |= DP_COMMON_CAP_LTTPR_NS;
  743. tb_tunnel_dbg(tunnel, "disabling LTTPR\n");
  744. }
  745. return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
  746. in->cap_adap + DP_REMOTE_CAP, 1);
  747. }
  748. static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel)
  749. {
  750. int ret, estimated_bw, granularity, tmp;
  751. struct tb_port *out = tunnel->dst_port;
  752. struct tb_port *in = tunnel->src_port;
  753. u32 out_dp_cap, out_rate, out_lanes;
  754. u32 in_dp_cap, in_rate, in_lanes;
  755. u32 rate, lanes;
  756. if (!bw_alloc_mode)
  757. return 0;
  758. ret = usb4_dp_port_set_cm_bandwidth_mode_supported(in, true);
  759. if (ret)
  760. return ret;
  761. ret = usb4_dp_port_set_group_id(in, in->group->index);
  762. if (ret)
  763. return ret;
  764. /*
  765. * Get the non-reduced rate and lanes based on the lowest
  766. * capability of both adapters.
  767. */
  768. ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
  769. in->cap_adap + DP_LOCAL_CAP, 1);
  770. if (ret)
  771. return ret;
  772. ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
  773. out->cap_adap + DP_LOCAL_CAP, 1);
  774. if (ret)
  775. return ret;
  776. in_rate = tb_dp_cap_get_rate(in_dp_cap);
  777. in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
  778. out_rate = tb_dp_cap_get_rate(out_dp_cap);
  779. out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
  780. rate = min(in_rate, out_rate);
  781. lanes = min(in_lanes, out_lanes);
  782. tmp = tb_dp_bandwidth(rate, lanes);
  783. tb_tunnel_dbg(tunnel, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n",
  784. rate, lanes, tmp);
  785. ret = usb4_dp_port_set_nrd(in, rate, lanes);
  786. if (ret)
  787. return ret;
  788. /*
  789. * Pick up granularity that supports maximum possible bandwidth.
  790. * For that we use the UHBR rates too.
  791. */
  792. in_rate = tb_dp_cap_get_rate_ext(in_dp_cap);
  793. out_rate = tb_dp_cap_get_rate_ext(out_dp_cap);
  794. rate = min(in_rate, out_rate);
  795. tmp = tb_dp_bandwidth(rate, lanes);
  796. tb_tunnel_dbg(tunnel,
  797. "maximum bandwidth through allocation mode %u Mb/s x%u = %u Mb/s\n",
  798. rate, lanes, tmp);
  799. for (granularity = 250; tmp / granularity > 255 && granularity <= 1000;
  800. granularity *= 2)
  801. ;
  802. tb_tunnel_dbg(tunnel, "granularity %d Mb/s\n", granularity);
  803. /*
  804. * Returns -EINVAL if granularity above is outside of the
  805. * accepted ranges.
  806. */
  807. ret = usb4_dp_port_set_granularity(in, granularity);
  808. if (ret)
  809. return ret;
  810. /*
  811. * Bandwidth estimation is pretty much what we have in
  812. * max_up/down fields. For discovery we just read what the
  813. * estimation was set to.
  814. */
  815. if (tb_tunnel_direction_downstream(tunnel))
  816. estimated_bw = tunnel->max_down;
  817. else
  818. estimated_bw = tunnel->max_up;
  819. tb_tunnel_dbg(tunnel, "estimated bandwidth %d Mb/s\n", estimated_bw);
  820. ret = usb4_dp_port_set_estimated_bandwidth(in, estimated_bw);
  821. if (ret)
  822. return ret;
  823. /* Initial allocation should be 0 according the spec */
  824. ret = usb4_dp_port_allocate_bandwidth(in, 0);
  825. if (ret)
  826. return ret;
  827. tb_tunnel_dbg(tunnel, "bandwidth allocation mode enabled\n");
  828. return 0;
  829. }
  830. static int tb_dp_pre_activate(struct tb_tunnel *tunnel)
  831. {
  832. struct tb_port *in = tunnel->src_port;
  833. struct tb_switch *sw = in->sw;
  834. struct tb *tb = in->sw->tb;
  835. int ret;
  836. ret = tb_dp_xchg_caps(tunnel);
  837. if (ret)
  838. return ret;
  839. if (!tb_switch_is_usb4(sw))
  840. return 0;
  841. if (!usb4_dp_port_bandwidth_mode_supported(in))
  842. return 0;
  843. tb_tunnel_dbg(tunnel, "bandwidth allocation mode supported\n");
  844. ret = usb4_dp_port_set_cm_id(in, tb->index);
  845. if (ret)
  846. return ret;
  847. return tb_dp_bandwidth_alloc_mode_enable(tunnel);
  848. }
  849. static void tb_dp_post_deactivate(struct tb_tunnel *tunnel)
  850. {
  851. struct tb_port *in = tunnel->src_port;
  852. if (!usb4_dp_port_bandwidth_mode_supported(in))
  853. return;
  854. if (usb4_dp_port_bandwidth_mode_enabled(in)) {
  855. usb4_dp_port_set_cm_bandwidth_mode_supported(in, false);
  856. tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n");
  857. }
  858. }
  859. static ktime_t dprx_timeout_to_ktime(int timeout_msec)
  860. {
  861. return timeout_msec >= 0 ?
  862. ktime_add_ms(ktime_get(), timeout_msec) : KTIME_MAX;
  863. }
  864. static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec)
  865. {
  866. ktime_t timeout = dprx_timeout_to_ktime(timeout_msec);
  867. struct tb_port *in = tunnel->src_port;
  868. /*
  869. * Wait for DPRX done. Normally it should be already set for
  870. * active tunnel.
  871. */
  872. do {
  873. u32 val;
  874. int ret;
  875. ret = tb_port_read(in, &val, TB_CFG_PORT,
  876. in->cap_adap + DP_COMMON_CAP, 1);
  877. if (ret)
  878. return ret;
  879. if (val & DP_COMMON_CAP_DPRX_DONE)
  880. return 0;
  881. usleep_range(100, 150);
  882. } while (ktime_before(ktime_get(), timeout));
  883. tb_tunnel_dbg(tunnel, "DPRX read timeout\n");
  884. return -ETIMEDOUT;
  885. }
  886. static void tb_dp_dprx_work(struct work_struct *work)
  887. {
  888. struct tb_tunnel *tunnel = container_of(work, typeof(*tunnel), dprx_work.work);
  889. struct tb *tb = tunnel->tb;
  890. if (!tunnel->dprx_canceled) {
  891. mutex_lock(&tb->lock);
  892. if (tb_dp_is_usb4(tunnel->src_port->sw) &&
  893. tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT)) {
  894. if (ktime_before(ktime_get(), tunnel->dprx_timeout)) {
  895. queue_delayed_work(tb->wq, &tunnel->dprx_work,
  896. msecs_to_jiffies(TB_DPRX_POLL_DELAY));
  897. mutex_unlock(&tb->lock);
  898. return;
  899. }
  900. } else {
  901. tb_tunnel_set_active(tunnel, true);
  902. }
  903. mutex_unlock(&tb->lock);
  904. }
  905. if (tunnel->callback)
  906. tunnel->callback(tunnel, tunnel->callback_data);
  907. tb_tunnel_put(tunnel);
  908. }
  909. static int tb_dp_dprx_start(struct tb_tunnel *tunnel)
  910. {
  911. /*
  912. * Bump up the reference to keep the tunnel around. It will be
  913. * dropped in tb_dp_dprx_stop() once the tunnel is deactivated.
  914. */
  915. tb_tunnel_get(tunnel);
  916. tunnel->dprx_started = true;
  917. if (tunnel->callback) {
  918. tunnel->dprx_timeout = dprx_timeout_to_ktime(dprx_timeout);
  919. queue_delayed_work(tunnel->tb->wq, &tunnel->dprx_work, 0);
  920. return -EINPROGRESS;
  921. }
  922. return tb_dp_is_usb4(tunnel->src_port->sw) ?
  923. tb_dp_wait_dprx(tunnel, dprx_timeout) : 0;
  924. }
  925. static void tb_dp_dprx_stop(struct tb_tunnel *tunnel)
  926. {
  927. if (tunnel->dprx_started) {
  928. tunnel->dprx_started = false;
  929. tunnel->dprx_canceled = true;
  930. if (cancel_delayed_work(&tunnel->dprx_work))
  931. tb_tunnel_put(tunnel);
  932. }
  933. }
  934. static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
  935. {
  936. int ret;
  937. if (active) {
  938. struct tb_path **paths;
  939. int last;
  940. paths = tunnel->paths;
  941. last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
  942. tb_dp_port_set_hops(tunnel->src_port,
  943. paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
  944. paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
  945. paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
  946. tb_dp_port_set_hops(tunnel->dst_port,
  947. paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
  948. paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
  949. paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
  950. } else {
  951. tb_dp_dprx_stop(tunnel);
  952. tb_dp_port_hpd_clear(tunnel->src_port);
  953. tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
  954. if (tb_port_is_dpout(tunnel->dst_port))
  955. tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
  956. }
  957. ret = tb_dp_port_enable(tunnel->src_port, active);
  958. if (ret)
  959. return ret;
  960. if (tb_port_is_dpout(tunnel->dst_port)) {
  961. ret = tb_dp_port_enable(tunnel->dst_port, active);
  962. if (ret)
  963. return ret;
  964. }
  965. return active ? tb_dp_dprx_start(tunnel) : 0;
  966. }
  967. /**
  968. * tb_dp_bandwidth_mode_maximum_bandwidth() - Maximum possible bandwidth
  969. * @tunnel: DP tunnel to check
  970. * @max_bw_rounded: Maximum bandwidth in Mb/s rounded up to the next granularity
  971. *
  972. * Return: Maximum possible bandwidth for this tunnel in Mb/s, negative errno
  973. * in case of failure.
  974. */
  975. static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel,
  976. int *max_bw_rounded)
  977. {
  978. struct tb_port *in = tunnel->src_port;
  979. int ret, rate, lanes, max_bw;
  980. u32 cap;
  981. /*
  982. * DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX
  983. * read parameter values so we can use this to determine the
  984. * maximum possible bandwidth over this link.
  985. *
  986. * See USB4 v2 spec 1.0 10.4.4.5.
  987. */
  988. ret = tb_port_read(in, &cap, TB_CFG_PORT,
  989. in->cap_adap + DP_LOCAL_CAP, 1);
  990. if (ret)
  991. return ret;
  992. rate = tb_dp_cap_get_rate_ext(cap);
  993. lanes = tb_dp_cap_get_lanes(cap);
  994. max_bw = tb_dp_bandwidth(rate, lanes);
  995. if (max_bw_rounded) {
  996. ret = usb4_dp_port_granularity(in);
  997. if (ret < 0)
  998. return ret;
  999. *max_bw_rounded = roundup(max_bw, ret);
  1000. }
  1001. return max_bw;
  1002. }
  1003. static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel,
  1004. int *consumed_up,
  1005. int *consumed_down)
  1006. {
  1007. struct tb_port *in = tunnel->src_port;
  1008. int ret, allocated_bw, max_bw_rounded;
  1009. if (!usb4_dp_port_bandwidth_mode_enabled(in))
  1010. return -EOPNOTSUPP;
  1011. if (!tunnel->bw_mode)
  1012. return -EOPNOTSUPP;
  1013. /* Read what was allocated previously if any */
  1014. ret = usb4_dp_port_allocated_bandwidth(in);
  1015. if (ret < 0)
  1016. return ret;
  1017. allocated_bw = ret;
  1018. ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded);
  1019. if (ret < 0)
  1020. return ret;
  1021. if (allocated_bw == max_bw_rounded)
  1022. allocated_bw = ret;
  1023. if (tb_tunnel_direction_downstream(tunnel)) {
  1024. *consumed_up = 0;
  1025. *consumed_down = allocated_bw;
  1026. } else {
  1027. *consumed_up = allocated_bw;
  1028. *consumed_down = 0;
  1029. }
  1030. return 0;
  1031. }
  1032. static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
  1033. int *allocated_down)
  1034. {
  1035. struct tb_port *in = tunnel->src_port;
  1036. /*
  1037. * If we have already set the allocated bandwidth then use that.
  1038. * Otherwise we read it from the DPRX.
  1039. */
  1040. if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) {
  1041. int ret, allocated_bw, max_bw_rounded;
  1042. ret = usb4_dp_port_allocated_bandwidth(in);
  1043. if (ret < 0)
  1044. return ret;
  1045. allocated_bw = ret;
  1046. ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel,
  1047. &max_bw_rounded);
  1048. if (ret < 0)
  1049. return ret;
  1050. if (allocated_bw == max_bw_rounded)
  1051. allocated_bw = ret;
  1052. if (tb_tunnel_direction_downstream(tunnel)) {
  1053. *allocated_up = 0;
  1054. *allocated_down = allocated_bw;
  1055. } else {
  1056. *allocated_up = allocated_bw;
  1057. *allocated_down = 0;
  1058. }
  1059. return 0;
  1060. }
  1061. return tunnel->consumed_bandwidth(tunnel, allocated_up,
  1062. allocated_down);
  1063. }
  1064. static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
  1065. int *alloc_down)
  1066. {
  1067. struct tb_port *in = tunnel->src_port;
  1068. int max_bw_rounded, ret, tmp;
  1069. if (!usb4_dp_port_bandwidth_mode_enabled(in))
  1070. return -EOPNOTSUPP;
  1071. ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded);
  1072. if (ret < 0)
  1073. return ret;
  1074. if (tb_tunnel_direction_downstream(tunnel)) {
  1075. tmp = min(*alloc_down, max_bw_rounded);
  1076. ret = usb4_dp_port_allocate_bandwidth(in, tmp);
  1077. if (ret)
  1078. return ret;
  1079. *alloc_down = tmp;
  1080. *alloc_up = 0;
  1081. } else {
  1082. tmp = min(*alloc_up, max_bw_rounded);
  1083. ret = usb4_dp_port_allocate_bandwidth(in, tmp);
  1084. if (ret)
  1085. return ret;
  1086. *alloc_down = 0;
  1087. *alloc_up = tmp;
  1088. }
  1089. /* Now we can use BW mode registers to figure out the bandwidth */
  1090. /* TODO: need to handle discovery too */
  1091. tunnel->bw_mode = true;
  1092. return 0;
  1093. }
  1094. /* Read cap from tunnel DP IN */
  1095. static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate,
  1096. u32 *lanes)
  1097. {
  1098. struct tb_port *in = tunnel->src_port;
  1099. u32 val;
  1100. int ret;
  1101. switch (cap) {
  1102. case DP_LOCAL_CAP:
  1103. case DP_REMOTE_CAP:
  1104. case DP_COMMON_CAP:
  1105. break;
  1106. default:
  1107. tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap);
  1108. return -EINVAL;
  1109. }
  1110. /*
  1111. * Read from the copied remote cap so that we take into account
  1112. * if capabilities were reduced during exchange.
  1113. */
  1114. ret = tb_port_read(in, &val, TB_CFG_PORT, in->cap_adap + cap, 1);
  1115. if (ret)
  1116. return ret;
  1117. *rate = tb_dp_cap_get_rate(val);
  1118. *lanes = tb_dp_cap_get_lanes(val);
  1119. return 0;
  1120. }
  1121. static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
  1122. int *max_down)
  1123. {
  1124. int ret;
  1125. if (!usb4_dp_port_bandwidth_mode_enabled(tunnel->src_port))
  1126. return -EOPNOTSUPP;
  1127. ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL);
  1128. if (ret < 0)
  1129. return ret;
  1130. if (tb_tunnel_direction_downstream(tunnel)) {
  1131. *max_up = 0;
  1132. *max_down = ret;
  1133. } else {
  1134. *max_up = ret;
  1135. *max_down = 0;
  1136. }
  1137. return 0;
  1138. }
  1139. static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
  1140. int *consumed_down)
  1141. {
  1142. const struct tb_switch *sw = tunnel->src_port->sw;
  1143. u32 rate = 0, lanes = 0;
  1144. int ret;
  1145. if (tb_dp_is_usb4(sw)) {
  1146. ret = tb_dp_wait_dprx(tunnel, 0);
  1147. if (ret) {
  1148. if (ret == -ETIMEDOUT) {
  1149. /*
  1150. * While we wait for DPRX complete the
  1151. * tunnel consumes as much as it had
  1152. * been reserved initially.
  1153. */
  1154. ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP,
  1155. &rate, &lanes);
  1156. if (ret)
  1157. return ret;
  1158. } else {
  1159. return ret;
  1160. }
  1161. } else {
  1162. /*
  1163. * On USB4 routers check if the bandwidth allocation
  1164. * mode is enabled first and then read the bandwidth
  1165. * through those registers.
  1166. */
  1167. ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up,
  1168. consumed_down);
  1169. if (ret < 0) {
  1170. if (ret != -EOPNOTSUPP)
  1171. return ret;
  1172. } else if (!ret) {
  1173. return 0;
  1174. }
  1175. ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes);
  1176. if (ret)
  1177. return ret;
  1178. }
  1179. } else if (sw->generation >= 2) {
  1180. ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes);
  1181. if (ret)
  1182. return ret;
  1183. } else {
  1184. /* No bandwidth management for legacy devices */
  1185. *consumed_up = 0;
  1186. *consumed_down = 0;
  1187. return 0;
  1188. }
  1189. if (tb_tunnel_direction_downstream(tunnel)) {
  1190. *consumed_up = 0;
  1191. *consumed_down = tb_dp_bandwidth(rate, lanes);
  1192. } else {
  1193. *consumed_up = tb_dp_bandwidth(rate, lanes);
  1194. *consumed_down = 0;
  1195. }
  1196. return 0;
  1197. }
  1198. static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
  1199. {
  1200. struct tb_port *port = hop->in_port;
  1201. struct tb_switch *sw = port->sw;
  1202. if (tb_port_use_credit_allocation(port))
  1203. hop->initial_credits = sw->min_dp_aux_credits;
  1204. else
  1205. hop->initial_credits = 1;
  1206. }
  1207. static void tb_dp_init_aux_path(struct tb_path *path, bool pm_support)
  1208. {
  1209. struct tb_path_hop *hop;
  1210. path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
  1211. path->egress_shared_buffer = TB_PATH_NONE;
  1212. path->ingress_fc_enable = TB_PATH_ALL;
  1213. path->ingress_shared_buffer = TB_PATH_NONE;
  1214. path->priority = TB_DP_AUX_PRIORITY;
  1215. path->weight = TB_DP_AUX_WEIGHT;
  1216. tb_path_for_each_hop(path, hop) {
  1217. tb_dp_init_aux_credits(hop);
  1218. if (pm_support)
  1219. tb_init_pm_support(hop);
  1220. }
  1221. }
  1222. static int tb_dp_init_video_credits(struct tb_path_hop *hop)
  1223. {
  1224. struct tb_port *port = hop->in_port;
  1225. struct tb_switch *sw = port->sw;
  1226. if (tb_port_use_credit_allocation(port)) {
  1227. unsigned int nfc_credits;
  1228. size_t max_dp_streams;
  1229. tb_available_credits(port, &max_dp_streams);
  1230. /*
  1231. * Read the number of currently allocated NFC credits
  1232. * from the lane adapter. Since we only use them for DP
  1233. * tunneling we can use that to figure out how many DP
  1234. * tunnels already go through the lane adapter.
  1235. */
  1236. nfc_credits = port->config.nfc_credits &
  1237. ADP_CS_4_NFC_BUFFERS_MASK;
  1238. if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
  1239. return -ENOSPC;
  1240. hop->nfc_credits = sw->min_dp_main_credits;
  1241. } else {
  1242. hop->nfc_credits = min(port->total_credits - 2, 12U);
  1243. }
  1244. return 0;
  1245. }
  1246. static int tb_dp_init_video_path(struct tb_path *path, bool pm_support)
  1247. {
  1248. struct tb_path_hop *hop;
  1249. path->egress_fc_enable = TB_PATH_NONE;
  1250. path->egress_shared_buffer = TB_PATH_NONE;
  1251. path->ingress_fc_enable = TB_PATH_NONE;
  1252. path->ingress_shared_buffer = TB_PATH_NONE;
  1253. path->priority = TB_DP_VIDEO_PRIORITY;
  1254. path->weight = TB_DP_VIDEO_WEIGHT;
  1255. tb_path_for_each_hop(path, hop) {
  1256. int ret;
  1257. ret = tb_dp_init_video_credits(hop);
  1258. if (ret)
  1259. return ret;
  1260. if (pm_support)
  1261. tb_init_pm_support(hop);
  1262. }
  1263. return 0;
  1264. }
  1265. static void tb_dp_dump(struct tb_tunnel *tunnel)
  1266. {
  1267. struct tb_port *in, *out;
  1268. u32 dp_cap, rate, lanes;
  1269. in = tunnel->src_port;
  1270. out = tunnel->dst_port;
  1271. if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
  1272. in->cap_adap + DP_LOCAL_CAP, 1))
  1273. return;
  1274. rate = tb_dp_cap_get_rate(dp_cap);
  1275. lanes = tb_dp_cap_get_lanes(dp_cap);
  1276. tb_tunnel_dbg(tunnel,
  1277. "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
  1278. rate, lanes, tb_dp_bandwidth(rate, lanes));
  1279. if (tb_port_read(out, &dp_cap, TB_CFG_PORT,
  1280. out->cap_adap + DP_LOCAL_CAP, 1))
  1281. return;
  1282. rate = tb_dp_cap_get_rate(dp_cap);
  1283. lanes = tb_dp_cap_get_lanes(dp_cap);
  1284. tb_tunnel_dbg(tunnel,
  1285. "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
  1286. rate, lanes, tb_dp_bandwidth(rate, lanes));
  1287. if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
  1288. in->cap_adap + DP_REMOTE_CAP, 1))
  1289. return;
  1290. rate = tb_dp_cap_get_rate(dp_cap);
  1291. lanes = tb_dp_cap_get_lanes(dp_cap);
  1292. tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n",
  1293. rate, lanes, tb_dp_bandwidth(rate, lanes));
  1294. }
  1295. /**
  1296. * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
  1297. * @tb: Pointer to the domain structure
  1298. * @in: DP in adapter
  1299. * @alloc_hopid: Allocate HopIDs from visited ports
  1300. *
  1301. * If @in adapter is active, follows the tunnel to the DP out adapter
  1302. * and back. Returns the discovered tunnel or %NULL if there was no
  1303. * tunnel.
  1304. *
  1305. * Return: Pointer to &struct tb_tunnel or %NULL if no tunnel found.
  1306. */
  1307. struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
  1308. bool alloc_hopid)
  1309. {
  1310. struct tb_tunnel *tunnel;
  1311. struct tb_port *port;
  1312. struct tb_path *path;
  1313. if (!tb_dp_port_is_enabled(in))
  1314. return NULL;
  1315. tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
  1316. if (!tunnel)
  1317. return NULL;
  1318. tunnel->pre_activate = tb_dp_pre_activate;
  1319. tunnel->activate = tb_dp_activate;
  1320. tunnel->post_deactivate = tb_dp_post_deactivate;
  1321. tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
  1322. tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
  1323. tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
  1324. tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
  1325. tunnel->src_port = in;
  1326. path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
  1327. &tunnel->dst_port, "Video", alloc_hopid);
  1328. if (!path) {
  1329. /* Just disable the DP IN port */
  1330. tb_dp_port_enable(in, false);
  1331. goto err_free;
  1332. }
  1333. tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
  1334. if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false))
  1335. goto err_free;
  1336. path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
  1337. alloc_hopid);
  1338. if (!path)
  1339. goto err_deactivate;
  1340. tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
  1341. tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false);
  1342. path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
  1343. &port, "AUX RX", alloc_hopid);
  1344. if (!path)
  1345. goto err_deactivate;
  1346. tunnel->paths[TB_DP_AUX_PATH_IN] = path;
  1347. tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false);
  1348. /* Validate that the tunnel is complete */
  1349. if (!tb_port_is_dpout(tunnel->dst_port)) {
  1350. tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
  1351. goto err_deactivate;
  1352. }
  1353. if (!tb_dp_port_is_enabled(tunnel->dst_port))
  1354. goto err_deactivate;
  1355. if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
  1356. goto err_deactivate;
  1357. if (port != tunnel->src_port) {
  1358. tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
  1359. goto err_deactivate;
  1360. }
  1361. tb_dp_dump(tunnel);
  1362. tb_tunnel_dbg(tunnel, "discovered\n");
  1363. return tunnel;
  1364. err_deactivate:
  1365. tb_tunnel_deactivate(tunnel);
  1366. err_free:
  1367. tb_tunnel_put(tunnel);
  1368. return NULL;
  1369. }
  1370. /**
  1371. * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
  1372. * @tb: Pointer to the domain structure
  1373. * @in: DP in adapter port
  1374. * @out: DP out adapter port
  1375. * @link_nr: Preferred lane adapter when the link is not bonded
  1376. * @max_up: Maximum available upstream bandwidth for the DP tunnel.
  1377. * %0 if no available bandwidth.
  1378. * @max_down: Maximum available downstream bandwidth for the DP tunnel.
  1379. * %0 if no available bandwidth.
  1380. * @callback: Optional callback that is called when the DP tunnel is
  1381. * fully activated (or there is an error)
  1382. * @callback_data: Optional data for @callback
  1383. *
  1384. * Allocates a tunnel between @in and @out that is capable of tunneling
  1385. * Display Port traffic. If @callback is not %NULL it will be called
  1386. * after tb_tunnel_activate() once the tunnel has been fully activated.
  1387. * It can call tb_tunnel_is_active() to check if activation was
  1388. * successful (or if it returns %false there was some sort of issue).
  1389. * The @callback is called without @tb->lock held.
  1390. *
  1391. * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
  1392. */
  1393. struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
  1394. struct tb_port *out, int link_nr,
  1395. int max_up, int max_down,
  1396. void (*callback)(struct tb_tunnel *, void *),
  1397. void *callback_data)
  1398. {
  1399. struct tb_tunnel *tunnel;
  1400. struct tb_path **paths;
  1401. struct tb_path *path;
  1402. bool pm_support;
  1403. if (WARN_ON(!in->cap_adap || !out->cap_adap))
  1404. return NULL;
  1405. tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
  1406. if (!tunnel)
  1407. return NULL;
  1408. tunnel->pre_activate = tb_dp_pre_activate;
  1409. tunnel->activate = tb_dp_activate;
  1410. tunnel->post_deactivate = tb_dp_post_deactivate;
  1411. tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
  1412. tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
  1413. tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
  1414. tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
  1415. tunnel->src_port = in;
  1416. tunnel->dst_port = out;
  1417. tunnel->max_up = max_up;
  1418. tunnel->max_down = max_down;
  1419. tunnel->callback = callback;
  1420. tunnel->callback_data = callback_data;
  1421. INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work);
  1422. paths = tunnel->paths;
  1423. pm_support = usb4_switch_version(in->sw) >= 2;
  1424. path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
  1425. link_nr, "Video");
  1426. if (!path)
  1427. goto err_free;
  1428. tb_dp_init_video_path(path, pm_support);
  1429. paths[TB_DP_VIDEO_PATH_OUT] = path;
  1430. path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
  1431. TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
  1432. if (!path)
  1433. goto err_free;
  1434. tb_dp_init_aux_path(path, pm_support);
  1435. paths[TB_DP_AUX_PATH_OUT] = path;
  1436. path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
  1437. TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
  1438. if (!path)
  1439. goto err_free;
  1440. tb_dp_init_aux_path(path, pm_support);
  1441. paths[TB_DP_AUX_PATH_IN] = path;
  1442. return tunnel;
  1443. err_free:
  1444. tb_tunnel_put(tunnel);
  1445. return NULL;
  1446. }
  1447. static unsigned int tb_dma_available_credits(const struct tb_port *port)
  1448. {
  1449. const struct tb_switch *sw = port->sw;
  1450. int credits;
  1451. credits = tb_available_credits(port, NULL);
  1452. if (tb_acpi_may_tunnel_pcie())
  1453. credits -= sw->max_pcie_credits;
  1454. credits -= port->dma_credits;
  1455. return credits > 0 ? credits : 0;
  1456. }
  1457. static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
  1458. {
  1459. struct tb_port *port = hop->in_port;
  1460. if (tb_port_use_credit_allocation(port)) {
  1461. unsigned int available = tb_dma_available_credits(port);
  1462. /*
  1463. * Need to have at least TB_MIN_DMA_CREDITS, otherwise
  1464. * DMA path cannot be established.
  1465. */
  1466. if (available < TB_MIN_DMA_CREDITS)
  1467. return -ENOSPC;
  1468. while (credits > available)
  1469. credits--;
  1470. tb_port_dbg(port, "reserving %u credits for DMA path\n",
  1471. credits);
  1472. port->dma_credits += credits;
  1473. } else {
  1474. if (tb_port_is_null(port))
  1475. credits = port->bonded ? 14 : 6;
  1476. else
  1477. credits = min(port->total_credits, credits);
  1478. }
  1479. hop->initial_credits = credits;
  1480. return 0;
  1481. }
  1482. /* Path from lane adapter to NHI */
  1483. static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
  1484. {
  1485. struct tb_path_hop *hop;
  1486. unsigned int i, tmp;
  1487. path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
  1488. path->ingress_fc_enable = TB_PATH_ALL;
  1489. path->egress_shared_buffer = TB_PATH_NONE;
  1490. path->ingress_shared_buffer = TB_PATH_NONE;
  1491. path->priority = TB_DMA_PRIORITY;
  1492. path->weight = TB_DMA_WEIGHT;
  1493. path->clear_fc = true;
  1494. /*
  1495. * First lane adapter is the one connected to the remote host.
  1496. * We don't tunnel other traffic over this link so we can use
  1497. * all the credits (except the ones reserved for control traffic).
  1498. */
  1499. hop = &path->hops[0];
  1500. tmp = min(tb_usable_credits(hop->in_port), credits);
  1501. hop->initial_credits = tmp;
  1502. hop->in_port->dma_credits += tmp;
  1503. for (i = 1; i < path->path_length; i++) {
  1504. int ret;
  1505. ret = tb_dma_reserve_credits(&path->hops[i], credits);
  1506. if (ret)
  1507. return ret;
  1508. }
  1509. return 0;
  1510. }
  1511. /* Path from NHI to lane adapter */
  1512. static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
  1513. {
  1514. struct tb_path_hop *hop;
  1515. path->egress_fc_enable = TB_PATH_ALL;
  1516. path->ingress_fc_enable = TB_PATH_ALL;
  1517. path->egress_shared_buffer = TB_PATH_NONE;
  1518. path->ingress_shared_buffer = TB_PATH_NONE;
  1519. path->priority = TB_DMA_PRIORITY;
  1520. path->weight = TB_DMA_WEIGHT;
  1521. path->clear_fc = true;
  1522. tb_path_for_each_hop(path, hop) {
  1523. int ret;
  1524. ret = tb_dma_reserve_credits(hop, credits);
  1525. if (ret)
  1526. return ret;
  1527. }
  1528. return 0;
  1529. }
  1530. static void tb_dma_release_credits(struct tb_path_hop *hop)
  1531. {
  1532. struct tb_port *port = hop->in_port;
  1533. if (tb_port_use_credit_allocation(port)) {
  1534. port->dma_credits -= hop->initial_credits;
  1535. tb_port_dbg(port, "released %u DMA path credits\n",
  1536. hop->initial_credits);
  1537. }
  1538. }
  1539. static void tb_dma_destroy_path(struct tb_path *path)
  1540. {
  1541. struct tb_path_hop *hop;
  1542. tb_path_for_each_hop(path, hop)
  1543. tb_dma_release_credits(hop);
  1544. }
  1545. static void tb_dma_destroy(struct tb_tunnel *tunnel)
  1546. {
  1547. int i;
  1548. for (i = 0; i < tunnel->npaths; i++) {
  1549. if (!tunnel->paths[i])
  1550. continue;
  1551. tb_dma_destroy_path(tunnel->paths[i]);
  1552. }
  1553. }
  1554. /**
  1555. * tb_tunnel_alloc_dma() - allocate a DMA tunnel
  1556. * @tb: Pointer to the domain structure
  1557. * @nhi: Host controller port
  1558. * @dst: Destination null port which the other domain is connected to
  1559. * @transmit_path: HopID used for transmitting packets
  1560. * @transmit_ring: NHI ring number used to send packets towards the
  1561. * other domain. Set to %-1 if TX path is not needed.
  1562. * @receive_path: HopID used for receiving packets
  1563. * @receive_ring: NHI ring number used to receive packets from the
  1564. * other domain. Set to %-1 if RX path is not needed.
  1565. *
  1566. * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
  1567. */
  1568. struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
  1569. struct tb_port *dst, int transmit_path,
  1570. int transmit_ring, int receive_path,
  1571. int receive_ring)
  1572. {
  1573. struct tb_tunnel *tunnel;
  1574. size_t npaths = 0, i = 0;
  1575. struct tb_path *path;
  1576. int credits;
  1577. /* Ring 0 is reserved for control channel */
  1578. if (WARN_ON(!receive_ring || !transmit_ring))
  1579. return NULL;
  1580. if (receive_ring > 0)
  1581. npaths++;
  1582. if (transmit_ring > 0)
  1583. npaths++;
  1584. if (WARN_ON(!npaths))
  1585. return NULL;
  1586. tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
  1587. if (!tunnel)
  1588. return NULL;
  1589. tunnel->src_port = nhi;
  1590. tunnel->dst_port = dst;
  1591. tunnel->destroy = tb_dma_destroy;
  1592. credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits);
  1593. if (receive_ring > 0) {
  1594. path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
  1595. "DMA RX");
  1596. if (!path)
  1597. goto err_free;
  1598. tunnel->paths[i++] = path;
  1599. if (tb_dma_init_rx_path(path, credits)) {
  1600. tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
  1601. goto err_free;
  1602. }
  1603. }
  1604. if (transmit_ring > 0) {
  1605. path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
  1606. "DMA TX");
  1607. if (!path)
  1608. goto err_free;
  1609. tunnel->paths[i++] = path;
  1610. if (tb_dma_init_tx_path(path, credits)) {
  1611. tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
  1612. goto err_free;
  1613. }
  1614. }
  1615. return tunnel;
  1616. err_free:
  1617. tb_tunnel_put(tunnel);
  1618. return NULL;
  1619. }
  1620. /**
  1621. * tb_tunnel_match_dma() - Match DMA tunnel
  1622. * @tunnel: Tunnel to match
  1623. * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
  1624. * @transmit_ring: NHI ring number used to send packets towards the
  1625. * other domain. Pass %-1 to ignore.
  1626. * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
  1627. * @receive_ring: NHI ring number used to receive packets from the
  1628. * other domain. Pass %-1 to ignore.
  1629. *
  1630. * This function can be used to match specific DMA tunnel, if there are
  1631. * multiple DMA tunnels going through the same XDomain connection.
  1632. *
  1633. * Return: %true if there is a match, %false otherwise.
  1634. */
  1635. bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
  1636. int transmit_ring, int receive_path, int receive_ring)
  1637. {
  1638. const struct tb_path *tx_path = NULL, *rx_path = NULL;
  1639. int i;
  1640. if (!receive_ring || !transmit_ring)
  1641. return false;
  1642. for (i = 0; i < tunnel->npaths; i++) {
  1643. const struct tb_path *path = tunnel->paths[i];
  1644. if (!path)
  1645. continue;
  1646. if (tb_port_is_nhi(path->hops[0].in_port))
  1647. tx_path = path;
  1648. else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
  1649. rx_path = path;
  1650. }
  1651. if (transmit_ring > 0 || transmit_path > 0) {
  1652. if (!tx_path)
  1653. return false;
  1654. if (transmit_ring > 0 &&
  1655. (tx_path->hops[0].in_hop_index != transmit_ring))
  1656. return false;
  1657. if (transmit_path > 0 &&
  1658. (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
  1659. return false;
  1660. }
  1661. if (receive_ring > 0 || receive_path > 0) {
  1662. if (!rx_path)
  1663. return false;
  1664. if (receive_path > 0 &&
  1665. (rx_path->hops[0].in_hop_index != receive_path))
  1666. return false;
  1667. if (receive_ring > 0 &&
  1668. (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
  1669. return false;
  1670. }
  1671. return true;
  1672. }
  1673. static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
  1674. {
  1675. int ret, up_max_rate, down_max_rate;
  1676. ret = usb4_usb3_port_max_link_rate(up);
  1677. if (ret < 0)
  1678. return ret;
  1679. up_max_rate = ret;
  1680. ret = usb4_usb3_port_max_link_rate(down);
  1681. if (ret < 0)
  1682. return ret;
  1683. down_max_rate = ret;
  1684. return min(up_max_rate, down_max_rate);
  1685. }
  1686. static int tb_usb3_pre_activate(struct tb_tunnel *tunnel)
  1687. {
  1688. tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
  1689. tunnel->allocated_up, tunnel->allocated_down);
  1690. return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
  1691. &tunnel->allocated_up,
  1692. &tunnel->allocated_down);
  1693. }
  1694. static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
  1695. {
  1696. int res;
  1697. res = tb_usb3_port_enable(tunnel->src_port, activate);
  1698. if (res)
  1699. return res;
  1700. if (tb_port_is_usb3_up(tunnel->dst_port))
  1701. return tb_usb3_port_enable(tunnel->dst_port, activate);
  1702. return 0;
  1703. }
  1704. static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
  1705. int *consumed_up, int *consumed_down)
  1706. {
  1707. struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw);
  1708. int pcie_weight = tb_acpi_may_tunnel_pcie() ? TB_PCI_WEIGHT : 0;
  1709. /*
  1710. * PCIe tunneling, if enabled, affects the USB3 bandwidth so
  1711. * take that into account here.
  1712. */
  1713. *consumed_up = tunnel->allocated_up *
  1714. (TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT;
  1715. *consumed_down = tunnel->allocated_down *
  1716. (TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT;
  1717. if (tb_port_get_link_generation(port) >= 4) {
  1718. *consumed_up = max(*consumed_up, USB4_V2_USB3_MIN_BANDWIDTH);
  1719. *consumed_down = max(*consumed_down, USB4_V2_USB3_MIN_BANDWIDTH);
  1720. }
  1721. return 0;
  1722. }
  1723. static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
  1724. {
  1725. int ret;
  1726. ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
  1727. &tunnel->allocated_up,
  1728. &tunnel->allocated_down);
  1729. if (ret)
  1730. return ret;
  1731. tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
  1732. tunnel->allocated_up, tunnel->allocated_down);
  1733. return 0;
  1734. }
  1735. static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
  1736. int *available_up,
  1737. int *available_down)
  1738. {
  1739. int ret, max_rate, allocate_up, allocate_down;
  1740. ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port);
  1741. if (ret < 0) {
  1742. tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
  1743. return;
  1744. }
  1745. /*
  1746. * 90% of the max rate can be allocated for isochronous
  1747. * transfers.
  1748. */
  1749. max_rate = ret * 90 / 100;
  1750. /* No need to reclaim if already at maximum */
  1751. if (tunnel->allocated_up >= max_rate &&
  1752. tunnel->allocated_down >= max_rate)
  1753. return;
  1754. /* Don't go lower than what is already allocated */
  1755. allocate_up = min(max_rate, *available_up);
  1756. if (allocate_up < tunnel->allocated_up)
  1757. allocate_up = tunnel->allocated_up;
  1758. allocate_down = min(max_rate, *available_down);
  1759. if (allocate_down < tunnel->allocated_down)
  1760. allocate_down = tunnel->allocated_down;
  1761. /* If no changes no need to do more */
  1762. if (allocate_up == tunnel->allocated_up &&
  1763. allocate_down == tunnel->allocated_down)
  1764. return;
  1765. ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
  1766. &allocate_down);
  1767. if (ret) {
  1768. tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
  1769. return;
  1770. }
  1771. tunnel->allocated_up = allocate_up;
  1772. *available_up -= tunnel->allocated_up;
  1773. tunnel->allocated_down = allocate_down;
  1774. *available_down -= tunnel->allocated_down;
  1775. tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
  1776. tunnel->allocated_up, tunnel->allocated_down);
  1777. }
  1778. static void tb_usb3_init_credits(struct tb_path_hop *hop)
  1779. {
  1780. struct tb_port *port = hop->in_port;
  1781. struct tb_switch *sw = port->sw;
  1782. unsigned int credits;
  1783. if (tb_port_use_credit_allocation(port)) {
  1784. credits = sw->max_usb3_credits;
  1785. } else {
  1786. if (tb_port_is_null(port))
  1787. credits = port->bonded ? 32 : 16;
  1788. else
  1789. credits = 7;
  1790. }
  1791. hop->initial_credits = credits;
  1792. }
  1793. static void tb_usb3_init_path(struct tb_path *path)
  1794. {
  1795. struct tb_path_hop *hop;
  1796. path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
  1797. path->egress_shared_buffer = TB_PATH_NONE;
  1798. path->ingress_fc_enable = TB_PATH_ALL;
  1799. path->ingress_shared_buffer = TB_PATH_NONE;
  1800. path->priority = TB_USB3_PRIORITY;
  1801. path->weight = TB_USB3_WEIGHT;
  1802. path->drop_packages = 0;
  1803. tb_path_for_each_hop(path, hop)
  1804. tb_usb3_init_credits(hop);
  1805. }
  1806. /**
  1807. * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
  1808. * @tb: Pointer to the domain structure
  1809. * @down: USB3 downstream adapter
  1810. * @alloc_hopid: Allocate HopIDs from visited ports
  1811. *
  1812. * If @down adapter is active, follows the tunnel to the USB3 upstream
  1813. * adapter and back.
  1814. *
  1815. * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel.
  1816. */
  1817. struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
  1818. bool alloc_hopid)
  1819. {
  1820. struct tb_tunnel *tunnel;
  1821. struct tb_path *path;
  1822. if (!tb_usb3_port_is_enabled(down))
  1823. return NULL;
  1824. tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
  1825. if (!tunnel)
  1826. return NULL;
  1827. tunnel->activate = tb_usb3_activate;
  1828. tunnel->src_port = down;
  1829. /*
  1830. * Discover both paths even if they are not complete. We will
  1831. * clean them up by calling tb_tunnel_deactivate() below in that
  1832. * case.
  1833. */
  1834. path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
  1835. &tunnel->dst_port, "USB3 Down", alloc_hopid);
  1836. if (!path) {
  1837. /* Just disable the downstream port */
  1838. tb_usb3_port_enable(down, false);
  1839. goto err_free;
  1840. }
  1841. tunnel->paths[TB_USB3_PATH_DOWN] = path;
  1842. tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
  1843. path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
  1844. "USB3 Up", alloc_hopid);
  1845. if (!path)
  1846. goto err_deactivate;
  1847. tunnel->paths[TB_USB3_PATH_UP] = path;
  1848. tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
  1849. /* Validate that the tunnel is complete */
  1850. if (!tb_port_is_usb3_up(tunnel->dst_port)) {
  1851. tb_port_warn(tunnel->dst_port,
  1852. "path does not end on an USB3 adapter, cleaning up\n");
  1853. goto err_deactivate;
  1854. }
  1855. if (down != tunnel->src_port) {
  1856. tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
  1857. goto err_deactivate;
  1858. }
  1859. if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
  1860. tb_tunnel_warn(tunnel,
  1861. "tunnel is not fully activated, cleaning up\n");
  1862. goto err_deactivate;
  1863. }
  1864. if (!tb_route(down->sw)) {
  1865. int ret;
  1866. /*
  1867. * Read the initial bandwidth allocation for the first
  1868. * hop tunnel.
  1869. */
  1870. ret = usb4_usb3_port_allocated_bandwidth(down,
  1871. &tunnel->allocated_up, &tunnel->allocated_down);
  1872. if (ret)
  1873. goto err_deactivate;
  1874. tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
  1875. tunnel->allocated_up, tunnel->allocated_down);
  1876. tunnel->pre_activate = tb_usb3_pre_activate;
  1877. tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
  1878. tunnel->release_unused_bandwidth =
  1879. tb_usb3_release_unused_bandwidth;
  1880. tunnel->reclaim_available_bandwidth =
  1881. tb_usb3_reclaim_available_bandwidth;
  1882. }
  1883. tb_tunnel_dbg(tunnel, "discovered\n");
  1884. return tunnel;
  1885. err_deactivate:
  1886. tb_tunnel_deactivate(tunnel);
  1887. err_free:
  1888. tb_tunnel_put(tunnel);
  1889. return NULL;
  1890. }
  1891. /**
  1892. * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
  1893. * @tb: Pointer to the domain structure
  1894. * @up: USB3 upstream adapter port
  1895. * @down: USB3 downstream adapter port
  1896. * @max_up: Maximum available upstream bandwidth for the USB3 tunnel.
  1897. * %0 if no available bandwidth.
  1898. * @max_down: Maximum available downstream bandwidth for the USB3 tunnel.
  1899. * %0 if no available bandwidth.
  1900. *
  1901. * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
  1902. * @TB_TYPE_USB3_DOWN.
  1903. *
  1904. * Return: Pointer to @struct tb_tunnel or %NULL in case of failure.
  1905. */
  1906. struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
  1907. struct tb_port *down, int max_up,
  1908. int max_down)
  1909. {
  1910. struct tb_tunnel *tunnel;
  1911. struct tb_path *path;
  1912. int max_rate = 0;
  1913. if (!tb_route(down->sw) && (max_up > 0 || max_down > 0)) {
  1914. /*
  1915. * For USB3 isochronous transfers, we allow bandwidth which is
  1916. * not higher than 90% of maximum supported bandwidth by USB3
  1917. * adapters.
  1918. */
  1919. max_rate = tb_usb3_max_link_rate(down, up);
  1920. if (max_rate < 0)
  1921. return NULL;
  1922. max_rate = max_rate * 90 / 100;
  1923. tb_port_dbg(up, "maximum required bandwidth for USB3 tunnel %d Mb/s\n",
  1924. max_rate);
  1925. }
  1926. tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
  1927. if (!tunnel)
  1928. return NULL;
  1929. tunnel->activate = tb_usb3_activate;
  1930. tunnel->src_port = down;
  1931. tunnel->dst_port = up;
  1932. tunnel->max_up = max_up;
  1933. tunnel->max_down = max_down;
  1934. path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
  1935. "USB3 Down");
  1936. if (!path)
  1937. goto err_free;
  1938. tb_usb3_init_path(path);
  1939. tunnel->paths[TB_USB3_PATH_DOWN] = path;
  1940. path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
  1941. "USB3 Up");
  1942. if (!path)
  1943. goto err_free;
  1944. tb_usb3_init_path(path);
  1945. tunnel->paths[TB_USB3_PATH_UP] = path;
  1946. if (!tb_route(down->sw)) {
  1947. tunnel->allocated_up = min(max_rate, max_up);
  1948. tunnel->allocated_down = min(max_rate, max_down);
  1949. tunnel->pre_activate = tb_usb3_pre_activate;
  1950. tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
  1951. tunnel->release_unused_bandwidth =
  1952. tb_usb3_release_unused_bandwidth;
  1953. tunnel->reclaim_available_bandwidth =
  1954. tb_usb3_reclaim_available_bandwidth;
  1955. }
  1956. return tunnel;
  1957. err_free:
  1958. tb_tunnel_put(tunnel);
  1959. return NULL;
  1960. }
  1961. /**
  1962. * tb_tunnel_is_invalid - check whether an activated path is still valid
  1963. * @tunnel: Tunnel to check
  1964. *
  1965. * Return: %true if path is valid, %false otherwise.
  1966. */
  1967. bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
  1968. {
  1969. int i;
  1970. for (i = 0; i < tunnel->npaths; i++) {
  1971. WARN_ON(!tunnel->paths[i]->activated);
  1972. if (tb_path_is_invalid(tunnel->paths[i]))
  1973. return true;
  1974. }
  1975. return false;
  1976. }
  1977. /**
  1978. * tb_tunnel_activate() - activate a tunnel
  1979. * @tunnel: Tunnel to activate
  1980. *
  1981. * Return:
  1982. * * %0 - On success.
  1983. * * %-EINPROGRESS - If the tunnel activation is still in progress (that's
  1984. * for DP tunnels to complete DPRX capabilities read).
  1985. * * Negative errno - Another error occurred.
  1986. */
  1987. int tb_tunnel_activate(struct tb_tunnel *tunnel)
  1988. {
  1989. int res, i;
  1990. tb_tunnel_dbg(tunnel, "activating\n");
  1991. /*
  1992. * Make sure all paths are properly disabled before enabling
  1993. * them again.
  1994. */
  1995. for (i = 0; i < tunnel->npaths; i++) {
  1996. if (tunnel->paths[i]->activated) {
  1997. tb_path_deactivate(tunnel->paths[i]);
  1998. tunnel->paths[i]->activated = false;
  1999. }
  2000. }
  2001. tunnel->state = TB_TUNNEL_ACTIVATING;
  2002. if (tunnel->pre_activate) {
  2003. res = tunnel->pre_activate(tunnel);
  2004. if (res)
  2005. return res;
  2006. }
  2007. for (i = 0; i < tunnel->npaths; i++) {
  2008. res = tb_path_activate(tunnel->paths[i]);
  2009. if (res)
  2010. goto err;
  2011. }
  2012. if (tunnel->activate) {
  2013. res = tunnel->activate(tunnel, true);
  2014. if (res) {
  2015. if (res == -EINPROGRESS)
  2016. return res;
  2017. goto err;
  2018. }
  2019. }
  2020. tb_tunnel_set_active(tunnel, true);
  2021. return 0;
  2022. err:
  2023. tb_tunnel_warn(tunnel, "activation failed\n");
  2024. tb_tunnel_deactivate(tunnel);
  2025. return res;
  2026. }
  2027. /**
  2028. * tb_tunnel_deactivate() - deactivate a tunnel
  2029. * @tunnel: Tunnel to deactivate
  2030. */
  2031. void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
  2032. {
  2033. int i;
  2034. tb_tunnel_dbg(tunnel, "deactivating\n");
  2035. if (tunnel->activate)
  2036. tunnel->activate(tunnel, false);
  2037. for (i = 0; i < tunnel->npaths; i++) {
  2038. if (tunnel->paths[i] && tunnel->paths[i]->activated)
  2039. tb_path_deactivate(tunnel->paths[i]);
  2040. }
  2041. if (tunnel->post_deactivate)
  2042. tunnel->post_deactivate(tunnel);
  2043. tb_tunnel_set_active(tunnel, false);
  2044. }
  2045. /**
  2046. * tb_tunnel_port_on_path() - Does the tunnel go through port
  2047. * @tunnel: Tunnel to check
  2048. * @port: Port to check
  2049. *
  2050. * Return: %true if @tunnel goes through @port (direction does not matter),
  2051. * %false otherwise.
  2052. */
  2053. bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
  2054. const struct tb_port *port)
  2055. {
  2056. int i;
  2057. for (i = 0; i < tunnel->npaths; i++) {
  2058. if (!tunnel->paths[i])
  2059. continue;
  2060. if (tb_path_port_on_path(tunnel->paths[i], port))
  2061. return true;
  2062. }
  2063. return false;
  2064. }
  2065. // Is tb_tunnel_activate() called for the tunnel
  2066. static bool tb_tunnel_is_activated(const struct tb_tunnel *tunnel)
  2067. {
  2068. return tunnel->state == TB_TUNNEL_ACTIVATING || tb_tunnel_is_active(tunnel);
  2069. }
  2070. /**
  2071. * tb_tunnel_maximum_bandwidth() - Return maximum possible bandwidth
  2072. * @tunnel: Tunnel to check
  2073. * @max_up: Maximum upstream bandwidth in Mb/s
  2074. * @max_down: Maximum downstream bandwidth in Mb/s
  2075. *
  2076. * Return:
  2077. * * Maximum possible bandwidth this tunnel can support if not
  2078. * limited by other bandwidth clients.
  2079. * * %-EOPNOTSUPP - If the tunnel does not support this function.
  2080. * * %-ENOTCONN - If the tunnel is not active.
  2081. */
  2082. int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
  2083. int *max_down)
  2084. {
  2085. if (!tb_tunnel_is_active(tunnel))
  2086. return -ENOTCONN;
  2087. if (tunnel->maximum_bandwidth)
  2088. return tunnel->maximum_bandwidth(tunnel, max_up, max_down);
  2089. return -EOPNOTSUPP;
  2090. }
  2091. /**
  2092. * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
  2093. * @tunnel: Tunnel to check
  2094. * @allocated_up: Currently allocated upstream bandwidth in Mb/s is stored here
  2095. * @allocated_down: Currently allocated downstream bandwidth in Mb/s is
  2096. * stored here
  2097. *
  2098. * Return:
  2099. * * Bandwidth allocated for the tunnel. This may be higher than what the
  2100. * tunnel actually consumes.
  2101. * * %-EOPNOTSUPP - If the tunnel does not support this function.
  2102. * * %-ENOTCONN - If the tunnel is not active.
  2103. * * Negative errno - Another error occurred.
  2104. */
  2105. int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
  2106. int *allocated_down)
  2107. {
  2108. if (!tb_tunnel_is_active(tunnel))
  2109. return -ENOTCONN;
  2110. if (tunnel->allocated_bandwidth)
  2111. return tunnel->allocated_bandwidth(tunnel, allocated_up,
  2112. allocated_down);
  2113. return -EOPNOTSUPP;
  2114. }
  2115. /**
  2116. * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
  2117. * @tunnel: Tunnel whose bandwidth allocation to change
  2118. * @alloc_up: New upstream bandwidth in Mb/s
  2119. * @alloc_down: New downstream bandwidth in Mb/s
  2120. *
  2121. * Tries to change tunnel bandwidth allocation.
  2122. *
  2123. * Return:
  2124. * * %0 - On success. Updates @alloc_up and @alloc_down to values that were
  2125. * actually allocated (it may not be the same as passed originally).
  2126. * * Negative errno - In case of failure.
  2127. */
  2128. int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
  2129. int *alloc_down)
  2130. {
  2131. if (!tb_tunnel_is_active(tunnel))
  2132. return -ENOTCONN;
  2133. if (tunnel->alloc_bandwidth) {
  2134. int ret;
  2135. ret = tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down);
  2136. if (ret)
  2137. return ret;
  2138. tb_tunnel_changed(tunnel);
  2139. return 0;
  2140. }
  2141. return -EOPNOTSUPP;
  2142. }
  2143. /**
  2144. * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
  2145. * @tunnel: Tunnel to check
  2146. * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
  2147. * Can be %NULL.
  2148. * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
  2149. * Can be %NULL.
  2150. *
  2151. * Stores the amount of isochronous bandwidth @tunnel consumes in
  2152. * @consumed_up and @consumed_down.
  2153. *
  2154. * Return: %0 on success, negative errno otherwise.
  2155. */
  2156. int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
  2157. int *consumed_down)
  2158. {
  2159. int up_bw = 0, down_bw = 0;
  2160. /*
  2161. * Here we need to distinguish between not active tunnel from
  2162. * tunnels that are either fully active or activation started.
  2163. * The latter is true for DP tunnels where we must report the
  2164. * consumed to be the maximum we gave it until DPRX capabilities
  2165. * read is done by the graphics driver.
  2166. */
  2167. if (tb_tunnel_is_activated(tunnel) && tunnel->consumed_bandwidth) {
  2168. int ret;
  2169. ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
  2170. if (ret)
  2171. return ret;
  2172. }
  2173. if (consumed_up)
  2174. *consumed_up = up_bw;
  2175. if (consumed_down)
  2176. *consumed_down = down_bw;
  2177. tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, down_bw);
  2178. return 0;
  2179. }
  2180. /**
  2181. * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
  2182. * @tunnel: Tunnel whose unused bandwidth to release
  2183. *
  2184. * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
  2185. * moment) this function makes it release all the unused bandwidth.
  2186. *
  2187. * Return: %0 on success, negative errno otherwise.
  2188. */
  2189. int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
  2190. {
  2191. if (!tb_tunnel_is_active(tunnel))
  2192. return -ENOTCONN;
  2193. if (tunnel->release_unused_bandwidth) {
  2194. int ret;
  2195. ret = tunnel->release_unused_bandwidth(tunnel);
  2196. if (ret)
  2197. return ret;
  2198. }
  2199. return 0;
  2200. }
  2201. /**
  2202. * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
  2203. * @tunnel: Tunnel reclaiming available bandwidth
  2204. * @available_up: Available upstream bandwidth (in Mb/s)
  2205. * @available_down: Available downstream bandwidth (in Mb/s)
  2206. *
  2207. * Reclaims bandwidth from @available_up and @available_down and updates
  2208. * the variables accordingly (e.g decreases both according to what was
  2209. * reclaimed by the tunnel). If nothing was reclaimed the values are
  2210. * kept as is.
  2211. */
  2212. void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
  2213. int *available_up,
  2214. int *available_down)
  2215. {
  2216. if (!tb_tunnel_is_active(tunnel))
  2217. return;
  2218. if (tunnel->reclaim_available_bandwidth)
  2219. tunnel->reclaim_available_bandwidth(tunnel, available_up,
  2220. available_down);
  2221. }
  2222. const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel)
  2223. {
  2224. return tb_tunnel_names[tunnel->type];
  2225. }