test.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit tests
  4. *
  5. * Copyright (C) 2020, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #include <kunit/test.h>
  9. #include <linux/idr.h>
  10. #include "tb.h"
  11. #include "tunnel.h"
  12. static int __ida_init(struct kunit_resource *res, void *context)
  13. {
  14. struct ida *ida = context;
  15. ida_init(ida);
  16. res->data = ida;
  17. return 0;
  18. }
  19. static void __ida_destroy(struct kunit_resource *res)
  20. {
  21. struct ida *ida = res->data;
  22. ida_destroy(ida);
  23. }
  24. static void kunit_ida_init(struct kunit *test, struct ida *ida)
  25. {
  26. kunit_alloc_resource(test, __ida_init, __ida_destroy, GFP_KERNEL, ida);
  27. }
  28. static struct tb_switch *alloc_switch(struct kunit *test, u64 route,
  29. u8 upstream_port, u8 max_port_number)
  30. {
  31. struct tb_switch *sw;
  32. size_t size;
  33. int i;
  34. sw = kunit_kzalloc(test, sizeof(*sw), GFP_KERNEL);
  35. if (!sw)
  36. return NULL;
  37. sw->config.upstream_port_number = upstream_port;
  38. sw->config.depth = tb_route_length(route);
  39. sw->config.route_hi = upper_32_bits(route);
  40. sw->config.route_lo = lower_32_bits(route);
  41. sw->config.enabled = 0;
  42. sw->config.max_port_number = max_port_number;
  43. size = (sw->config.max_port_number + 1) * sizeof(*sw->ports);
  44. sw->ports = kunit_kzalloc(test, size, GFP_KERNEL);
  45. if (!sw->ports)
  46. return NULL;
  47. for (i = 0; i <= sw->config.max_port_number; i++) {
  48. sw->ports[i].sw = sw;
  49. sw->ports[i].port = i;
  50. sw->ports[i].config.port_number = i;
  51. if (i) {
  52. kunit_ida_init(test, &sw->ports[i].in_hopids);
  53. kunit_ida_init(test, &sw->ports[i].out_hopids);
  54. }
  55. }
  56. return sw;
  57. }
  58. static struct tb_switch *alloc_host(struct kunit *test)
  59. {
  60. struct tb_switch *sw;
  61. sw = alloc_switch(test, 0, 7, 13);
  62. if (!sw)
  63. return NULL;
  64. sw->config.vendor_id = 0x8086;
  65. sw->config.device_id = 0x9a1b;
  66. sw->ports[0].config.type = TB_TYPE_PORT;
  67. sw->ports[0].config.max_in_hop_id = 7;
  68. sw->ports[0].config.max_out_hop_id = 7;
  69. sw->ports[1].config.type = TB_TYPE_PORT;
  70. sw->ports[1].config.max_in_hop_id = 19;
  71. sw->ports[1].config.max_out_hop_id = 19;
  72. sw->ports[1].dual_link_port = &sw->ports[2];
  73. sw->ports[2].config.type = TB_TYPE_PORT;
  74. sw->ports[2].config.max_in_hop_id = 19;
  75. sw->ports[2].config.max_out_hop_id = 19;
  76. sw->ports[2].dual_link_port = &sw->ports[1];
  77. sw->ports[2].link_nr = 1;
  78. sw->ports[3].config.type = TB_TYPE_PORT;
  79. sw->ports[3].config.max_in_hop_id = 19;
  80. sw->ports[3].config.max_out_hop_id = 19;
  81. sw->ports[3].dual_link_port = &sw->ports[4];
  82. sw->ports[4].config.type = TB_TYPE_PORT;
  83. sw->ports[4].config.max_in_hop_id = 19;
  84. sw->ports[4].config.max_out_hop_id = 19;
  85. sw->ports[4].dual_link_port = &sw->ports[3];
  86. sw->ports[4].link_nr = 1;
  87. sw->ports[5].config.type = TB_TYPE_DP_HDMI_IN;
  88. sw->ports[5].config.max_in_hop_id = 9;
  89. sw->ports[5].config.max_out_hop_id = 9;
  90. sw->ports[5].cap_adap = -1;
  91. sw->ports[6].config.type = TB_TYPE_DP_HDMI_IN;
  92. sw->ports[6].config.max_in_hop_id = 9;
  93. sw->ports[6].config.max_out_hop_id = 9;
  94. sw->ports[6].cap_adap = -1;
  95. sw->ports[7].config.type = TB_TYPE_NHI;
  96. sw->ports[7].config.max_in_hop_id = 11;
  97. sw->ports[7].config.max_out_hop_id = 11;
  98. sw->ports[8].config.type = TB_TYPE_PCIE_DOWN;
  99. sw->ports[8].config.max_in_hop_id = 8;
  100. sw->ports[8].config.max_out_hop_id = 8;
  101. sw->ports[9].config.type = TB_TYPE_PCIE_DOWN;
  102. sw->ports[9].config.max_in_hop_id = 8;
  103. sw->ports[9].config.max_out_hop_id = 8;
  104. sw->ports[10].disabled = true;
  105. sw->ports[11].disabled = true;
  106. sw->ports[12].config.type = TB_TYPE_USB3_DOWN;
  107. sw->ports[12].config.max_in_hop_id = 8;
  108. sw->ports[12].config.max_out_hop_id = 8;
  109. sw->ports[13].config.type = TB_TYPE_USB3_DOWN;
  110. sw->ports[13].config.max_in_hop_id = 8;
  111. sw->ports[13].config.max_out_hop_id = 8;
  112. return sw;
  113. }
  114. static struct tb_switch *alloc_dev_default(struct kunit *test,
  115. struct tb_switch *parent,
  116. u64 route, bool bonded)
  117. {
  118. struct tb_port *port, *upstream_port;
  119. struct tb_switch *sw;
  120. sw = alloc_switch(test, route, 1, 19);
  121. if (!sw)
  122. return NULL;
  123. sw->config.vendor_id = 0x8086;
  124. sw->config.device_id = 0x15ef;
  125. sw->ports[0].config.type = TB_TYPE_PORT;
  126. sw->ports[0].config.max_in_hop_id = 8;
  127. sw->ports[0].config.max_out_hop_id = 8;
  128. sw->ports[1].config.type = TB_TYPE_PORT;
  129. sw->ports[1].config.max_in_hop_id = 19;
  130. sw->ports[1].config.max_out_hop_id = 19;
  131. sw->ports[1].dual_link_port = &sw->ports[2];
  132. sw->ports[2].config.type = TB_TYPE_PORT;
  133. sw->ports[2].config.max_in_hop_id = 19;
  134. sw->ports[2].config.max_out_hop_id = 19;
  135. sw->ports[2].dual_link_port = &sw->ports[1];
  136. sw->ports[2].link_nr = 1;
  137. sw->ports[3].config.type = TB_TYPE_PORT;
  138. sw->ports[3].config.max_in_hop_id = 19;
  139. sw->ports[3].config.max_out_hop_id = 19;
  140. sw->ports[3].dual_link_port = &sw->ports[4];
  141. sw->ports[4].config.type = TB_TYPE_PORT;
  142. sw->ports[4].config.max_in_hop_id = 19;
  143. sw->ports[4].config.max_out_hop_id = 19;
  144. sw->ports[4].dual_link_port = &sw->ports[3];
  145. sw->ports[4].link_nr = 1;
  146. sw->ports[5].config.type = TB_TYPE_PORT;
  147. sw->ports[5].config.max_in_hop_id = 19;
  148. sw->ports[5].config.max_out_hop_id = 19;
  149. sw->ports[5].dual_link_port = &sw->ports[6];
  150. sw->ports[6].config.type = TB_TYPE_PORT;
  151. sw->ports[6].config.max_in_hop_id = 19;
  152. sw->ports[6].config.max_out_hop_id = 19;
  153. sw->ports[6].dual_link_port = &sw->ports[5];
  154. sw->ports[6].link_nr = 1;
  155. sw->ports[7].config.type = TB_TYPE_PORT;
  156. sw->ports[7].config.max_in_hop_id = 19;
  157. sw->ports[7].config.max_out_hop_id = 19;
  158. sw->ports[7].dual_link_port = &sw->ports[8];
  159. sw->ports[8].config.type = TB_TYPE_PORT;
  160. sw->ports[8].config.max_in_hop_id = 19;
  161. sw->ports[8].config.max_out_hop_id = 19;
  162. sw->ports[8].dual_link_port = &sw->ports[7];
  163. sw->ports[8].link_nr = 1;
  164. sw->ports[9].config.type = TB_TYPE_PCIE_UP;
  165. sw->ports[9].config.max_in_hop_id = 8;
  166. sw->ports[9].config.max_out_hop_id = 8;
  167. sw->ports[10].config.type = TB_TYPE_PCIE_DOWN;
  168. sw->ports[10].config.max_in_hop_id = 8;
  169. sw->ports[10].config.max_out_hop_id = 8;
  170. sw->ports[11].config.type = TB_TYPE_PCIE_DOWN;
  171. sw->ports[11].config.max_in_hop_id = 8;
  172. sw->ports[11].config.max_out_hop_id = 8;
  173. sw->ports[12].config.type = TB_TYPE_PCIE_DOWN;
  174. sw->ports[12].config.max_in_hop_id = 8;
  175. sw->ports[12].config.max_out_hop_id = 8;
  176. sw->ports[13].config.type = TB_TYPE_DP_HDMI_OUT;
  177. sw->ports[13].config.max_in_hop_id = 9;
  178. sw->ports[13].config.max_out_hop_id = 9;
  179. sw->ports[13].cap_adap = -1;
  180. sw->ports[14].config.type = TB_TYPE_DP_HDMI_OUT;
  181. sw->ports[14].config.max_in_hop_id = 9;
  182. sw->ports[14].config.max_out_hop_id = 9;
  183. sw->ports[14].cap_adap = -1;
  184. sw->ports[15].disabled = true;
  185. sw->ports[16].config.type = TB_TYPE_USB3_UP;
  186. sw->ports[16].config.max_in_hop_id = 8;
  187. sw->ports[16].config.max_out_hop_id = 8;
  188. sw->ports[17].config.type = TB_TYPE_USB3_DOWN;
  189. sw->ports[17].config.max_in_hop_id = 8;
  190. sw->ports[17].config.max_out_hop_id = 8;
  191. sw->ports[18].config.type = TB_TYPE_USB3_DOWN;
  192. sw->ports[18].config.max_in_hop_id = 8;
  193. sw->ports[18].config.max_out_hop_id = 8;
  194. sw->ports[19].config.type = TB_TYPE_USB3_DOWN;
  195. sw->ports[19].config.max_in_hop_id = 8;
  196. sw->ports[19].config.max_out_hop_id = 8;
  197. if (!parent)
  198. return sw;
  199. /* Link them */
  200. upstream_port = tb_upstream_port(sw);
  201. port = tb_port_at(route, parent);
  202. port->remote = upstream_port;
  203. upstream_port->remote = port;
  204. if (port->dual_link_port && upstream_port->dual_link_port) {
  205. port->dual_link_port->remote = upstream_port->dual_link_port;
  206. upstream_port->dual_link_port->remote = port->dual_link_port;
  207. if (bonded) {
  208. /* Bonding is used */
  209. port->bonded = true;
  210. port->dual_link_port->bonded = true;
  211. upstream_port->bonded = true;
  212. upstream_port->dual_link_port->bonded = true;
  213. }
  214. }
  215. return sw;
  216. }
  217. static struct tb_switch *alloc_dev_with_dpin(struct kunit *test,
  218. struct tb_switch *parent,
  219. u64 route, bool bonded)
  220. {
  221. struct tb_switch *sw;
  222. sw = alloc_dev_default(test, parent, route, bonded);
  223. if (!sw)
  224. return NULL;
  225. sw->ports[13].config.type = TB_TYPE_DP_HDMI_IN;
  226. sw->ports[13].config.max_in_hop_id = 9;
  227. sw->ports[13].config.max_out_hop_id = 9;
  228. sw->ports[14].config.type = TB_TYPE_DP_HDMI_IN;
  229. sw->ports[14].config.max_in_hop_id = 9;
  230. sw->ports[14].config.max_out_hop_id = 9;
  231. return sw;
  232. }
  233. static void tb_test_path_basic(struct kunit *test)
  234. {
  235. struct tb_port *src_port, *dst_port, *p;
  236. struct tb_switch *host;
  237. host = alloc_host(test);
  238. src_port = &host->ports[5];
  239. dst_port = src_port;
  240. p = tb_next_port_on_path(src_port, dst_port, NULL);
  241. KUNIT_EXPECT_PTR_EQ(test, p, dst_port);
  242. p = tb_next_port_on_path(src_port, dst_port, p);
  243. KUNIT_EXPECT_TRUE(test, !p);
  244. }
  245. static void tb_test_path_not_connected_walk(struct kunit *test)
  246. {
  247. struct tb_port *src_port, *dst_port, *p;
  248. struct tb_switch *host, *dev;
  249. host = alloc_host(test);
  250. /* No connection between host and dev */
  251. dev = alloc_dev_default(test, NULL, 3, true);
  252. src_port = &host->ports[12];
  253. dst_port = &dev->ports[16];
  254. p = tb_next_port_on_path(src_port, dst_port, NULL);
  255. KUNIT_EXPECT_PTR_EQ(test, p, src_port);
  256. p = tb_next_port_on_path(src_port, dst_port, p);
  257. KUNIT_EXPECT_PTR_EQ(test, p, &host->ports[3]);
  258. p = tb_next_port_on_path(src_port, dst_port, p);
  259. KUNIT_EXPECT_TRUE(test, !p);
  260. /* Other direction */
  261. p = tb_next_port_on_path(dst_port, src_port, NULL);
  262. KUNIT_EXPECT_PTR_EQ(test, p, dst_port);
  263. p = tb_next_port_on_path(dst_port, src_port, p);
  264. KUNIT_EXPECT_PTR_EQ(test, p, &dev->ports[1]);
  265. p = tb_next_port_on_path(dst_port, src_port, p);
  266. KUNIT_EXPECT_TRUE(test, !p);
  267. }
  268. struct port_expectation {
  269. u64 route;
  270. u8 port;
  271. enum tb_port_type type;
  272. };
  273. static void tb_test_path_single_hop_walk(struct kunit *test)
  274. {
  275. /*
  276. * Walks from Host PCIe downstream port to Device #1 PCIe
  277. * upstream port.
  278. *
  279. * [Host]
  280. * 1 |
  281. * 1 |
  282. * [Device]
  283. */
  284. static const struct port_expectation test_data[] = {
  285. { .route = 0x0, .port = 8, .type = TB_TYPE_PCIE_DOWN },
  286. { .route = 0x0, .port = 1, .type = TB_TYPE_PORT },
  287. { .route = 0x1, .port = 1, .type = TB_TYPE_PORT },
  288. { .route = 0x1, .port = 9, .type = TB_TYPE_PCIE_UP },
  289. };
  290. struct tb_port *src_port, *dst_port, *p;
  291. struct tb_switch *host, *dev;
  292. int i;
  293. host = alloc_host(test);
  294. dev = alloc_dev_default(test, host, 1, true);
  295. src_port = &host->ports[8];
  296. dst_port = &dev->ports[9];
  297. /* Walk both directions */
  298. i = 0;
  299. tb_for_each_port_on_path(src_port, dst_port, p) {
  300. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  301. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  302. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  303. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  304. test_data[i].type);
  305. i++;
  306. }
  307. KUNIT_EXPECT_EQ(test, i, (int)ARRAY_SIZE(test_data));
  308. i = ARRAY_SIZE(test_data) - 1;
  309. tb_for_each_port_on_path(dst_port, src_port, p) {
  310. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  311. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  312. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  313. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  314. test_data[i].type);
  315. i--;
  316. }
  317. KUNIT_EXPECT_EQ(test, i, -1);
  318. }
  319. static void tb_test_path_daisy_chain_walk(struct kunit *test)
  320. {
  321. /*
  322. * Walks from Host DP IN to Device #2 DP OUT.
  323. *
  324. * [Host]
  325. * 1 |
  326. * 1 |
  327. * [Device #1]
  328. * 3 /
  329. * 1 /
  330. * [Device #2]
  331. */
  332. static const struct port_expectation test_data[] = {
  333. { .route = 0x0, .port = 5, .type = TB_TYPE_DP_HDMI_IN },
  334. { .route = 0x0, .port = 1, .type = TB_TYPE_PORT },
  335. { .route = 0x1, .port = 1, .type = TB_TYPE_PORT },
  336. { .route = 0x1, .port = 3, .type = TB_TYPE_PORT },
  337. { .route = 0x301, .port = 1, .type = TB_TYPE_PORT },
  338. { .route = 0x301, .port = 13, .type = TB_TYPE_DP_HDMI_OUT },
  339. };
  340. struct tb_port *src_port, *dst_port, *p;
  341. struct tb_switch *host, *dev1, *dev2;
  342. int i;
  343. host = alloc_host(test);
  344. dev1 = alloc_dev_default(test, host, 0x1, true);
  345. dev2 = alloc_dev_default(test, dev1, 0x301, true);
  346. src_port = &host->ports[5];
  347. dst_port = &dev2->ports[13];
  348. /* Walk both directions */
  349. i = 0;
  350. tb_for_each_port_on_path(src_port, dst_port, p) {
  351. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  352. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  353. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  354. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  355. test_data[i].type);
  356. i++;
  357. }
  358. KUNIT_EXPECT_EQ(test, i, (int)ARRAY_SIZE(test_data));
  359. i = ARRAY_SIZE(test_data) - 1;
  360. tb_for_each_port_on_path(dst_port, src_port, p) {
  361. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  362. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  363. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  364. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  365. test_data[i].type);
  366. i--;
  367. }
  368. KUNIT_EXPECT_EQ(test, i, -1);
  369. }
  370. static void tb_test_path_simple_tree_walk(struct kunit *test)
  371. {
  372. /*
  373. * Walks from Host DP IN to Device #3 DP OUT.
  374. *
  375. * [Host]
  376. * 1 |
  377. * 1 |
  378. * [Device #1]
  379. * 3 / | 5 \ 7
  380. * 1 / | \ 1
  381. * [Device #2] | [Device #4]
  382. * | 1
  383. * [Device #3]
  384. */
  385. static const struct port_expectation test_data[] = {
  386. { .route = 0x0, .port = 5, .type = TB_TYPE_DP_HDMI_IN },
  387. { .route = 0x0, .port = 1, .type = TB_TYPE_PORT },
  388. { .route = 0x1, .port = 1, .type = TB_TYPE_PORT },
  389. { .route = 0x1, .port = 5, .type = TB_TYPE_PORT },
  390. { .route = 0x501, .port = 1, .type = TB_TYPE_PORT },
  391. { .route = 0x501, .port = 13, .type = TB_TYPE_DP_HDMI_OUT },
  392. };
  393. struct tb_port *src_port, *dst_port, *p;
  394. struct tb_switch *host, *dev1, *dev3;
  395. int i;
  396. host = alloc_host(test);
  397. dev1 = alloc_dev_default(test, host, 0x1, true);
  398. alloc_dev_default(test, dev1, 0x301, true);
  399. dev3 = alloc_dev_default(test, dev1, 0x501, true);
  400. alloc_dev_default(test, dev1, 0x701, true);
  401. src_port = &host->ports[5];
  402. dst_port = &dev3->ports[13];
  403. /* Walk both directions */
  404. i = 0;
  405. tb_for_each_port_on_path(src_port, dst_port, p) {
  406. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  407. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  408. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  409. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  410. test_data[i].type);
  411. i++;
  412. }
  413. KUNIT_EXPECT_EQ(test, i, (int)ARRAY_SIZE(test_data));
  414. i = ARRAY_SIZE(test_data) - 1;
  415. tb_for_each_port_on_path(dst_port, src_port, p) {
  416. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  417. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  418. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  419. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  420. test_data[i].type);
  421. i--;
  422. }
  423. KUNIT_EXPECT_EQ(test, i, -1);
  424. }
  425. static void tb_test_path_complex_tree_walk(struct kunit *test)
  426. {
  427. /*
  428. * Walks from Device #3 DP IN to Device #9 DP OUT.
  429. *
  430. * [Host]
  431. * 1 |
  432. * 1 |
  433. * [Device #1]
  434. * 3 / | 5 \ 7
  435. * 1 / | \ 1
  436. * [Device #2] | [Device #5]
  437. * 5 | | 1 \ 7
  438. * 1 | [Device #4] \ 1
  439. * [Device #3] [Device #6]
  440. * 3 /
  441. * 1 /
  442. * [Device #7]
  443. * 3 / | 5
  444. * 1 / |
  445. * [Device #8] | 1
  446. * [Device #9]
  447. */
  448. static const struct port_expectation test_data[] = {
  449. { .route = 0x50301, .port = 13, .type = TB_TYPE_DP_HDMI_IN },
  450. { .route = 0x50301, .port = 1, .type = TB_TYPE_PORT },
  451. { .route = 0x301, .port = 5, .type = TB_TYPE_PORT },
  452. { .route = 0x301, .port = 1, .type = TB_TYPE_PORT },
  453. { .route = 0x1, .port = 3, .type = TB_TYPE_PORT },
  454. { .route = 0x1, .port = 7, .type = TB_TYPE_PORT },
  455. { .route = 0x701, .port = 1, .type = TB_TYPE_PORT },
  456. { .route = 0x701, .port = 7, .type = TB_TYPE_PORT },
  457. { .route = 0x70701, .port = 1, .type = TB_TYPE_PORT },
  458. { .route = 0x70701, .port = 3, .type = TB_TYPE_PORT },
  459. { .route = 0x3070701, .port = 1, .type = TB_TYPE_PORT },
  460. { .route = 0x3070701, .port = 5, .type = TB_TYPE_PORT },
  461. { .route = 0x503070701, .port = 1, .type = TB_TYPE_PORT },
  462. { .route = 0x503070701, .port = 14, .type = TB_TYPE_DP_HDMI_OUT },
  463. };
  464. struct tb_switch *host, *dev1, *dev2, *dev3, *dev5, *dev6, *dev7, *dev9;
  465. struct tb_port *src_port, *dst_port, *p;
  466. int i;
  467. host = alloc_host(test);
  468. dev1 = alloc_dev_default(test, host, 0x1, true);
  469. dev2 = alloc_dev_default(test, dev1, 0x301, true);
  470. dev3 = alloc_dev_with_dpin(test, dev2, 0x50301, true);
  471. alloc_dev_default(test, dev1, 0x501, true);
  472. dev5 = alloc_dev_default(test, dev1, 0x701, true);
  473. dev6 = alloc_dev_default(test, dev5, 0x70701, true);
  474. dev7 = alloc_dev_default(test, dev6, 0x3070701, true);
  475. alloc_dev_default(test, dev7, 0x303070701, true);
  476. dev9 = alloc_dev_default(test, dev7, 0x503070701, true);
  477. src_port = &dev3->ports[13];
  478. dst_port = &dev9->ports[14];
  479. /* Walk both directions */
  480. i = 0;
  481. tb_for_each_port_on_path(src_port, dst_port, p) {
  482. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  483. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  484. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  485. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  486. test_data[i].type);
  487. i++;
  488. }
  489. KUNIT_EXPECT_EQ(test, i, (int)ARRAY_SIZE(test_data));
  490. i = ARRAY_SIZE(test_data) - 1;
  491. tb_for_each_port_on_path(dst_port, src_port, p) {
  492. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  493. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  494. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  495. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  496. test_data[i].type);
  497. i--;
  498. }
  499. KUNIT_EXPECT_EQ(test, i, -1);
  500. }
  501. static void tb_test_path_max_length_walk(struct kunit *test)
  502. {
  503. struct tb_switch *host, *dev1, *dev2, *dev3, *dev4, *dev5, *dev6;
  504. struct tb_switch *dev7, *dev8, *dev9, *dev10, *dev11, *dev12;
  505. struct tb_port *src_port, *dst_port, *p;
  506. int i;
  507. /*
  508. * Walks from Device #6 DP IN to Device #12 DP OUT.
  509. *
  510. * [Host]
  511. * 1 / \ 3
  512. * 1 / \ 1
  513. * [Device #1] [Device #7]
  514. * 3 | | 3
  515. * 1 | | 1
  516. * [Device #2] [Device #8]
  517. * 3 | | 3
  518. * 1 | | 1
  519. * [Device #3] [Device #9]
  520. * 3 | | 3
  521. * 1 | | 1
  522. * [Device #4] [Device #10]
  523. * 3 | | 3
  524. * 1 | | 1
  525. * [Device #5] [Device #11]
  526. * 3 | | 3
  527. * 1 | | 1
  528. * [Device #6] [Device #12]
  529. */
  530. static const struct port_expectation test_data[] = {
  531. { .route = 0x30303030301, .port = 13, .type = TB_TYPE_DP_HDMI_IN },
  532. { .route = 0x30303030301, .port = 1, .type = TB_TYPE_PORT },
  533. { .route = 0x303030301, .port = 3, .type = TB_TYPE_PORT },
  534. { .route = 0x303030301, .port = 1, .type = TB_TYPE_PORT },
  535. { .route = 0x3030301, .port = 3, .type = TB_TYPE_PORT },
  536. { .route = 0x3030301, .port = 1, .type = TB_TYPE_PORT },
  537. { .route = 0x30301, .port = 3, .type = TB_TYPE_PORT },
  538. { .route = 0x30301, .port = 1, .type = TB_TYPE_PORT },
  539. { .route = 0x301, .port = 3, .type = TB_TYPE_PORT },
  540. { .route = 0x301, .port = 1, .type = TB_TYPE_PORT },
  541. { .route = 0x1, .port = 3, .type = TB_TYPE_PORT },
  542. { .route = 0x1, .port = 1, .type = TB_TYPE_PORT },
  543. { .route = 0x0, .port = 1, .type = TB_TYPE_PORT },
  544. { .route = 0x0, .port = 3, .type = TB_TYPE_PORT },
  545. { .route = 0x3, .port = 1, .type = TB_TYPE_PORT },
  546. { .route = 0x3, .port = 3, .type = TB_TYPE_PORT },
  547. { .route = 0x303, .port = 1, .type = TB_TYPE_PORT },
  548. { .route = 0x303, .port = 3, .type = TB_TYPE_PORT },
  549. { .route = 0x30303, .port = 1, .type = TB_TYPE_PORT },
  550. { .route = 0x30303, .port = 3, .type = TB_TYPE_PORT },
  551. { .route = 0x3030303, .port = 1, .type = TB_TYPE_PORT },
  552. { .route = 0x3030303, .port = 3, .type = TB_TYPE_PORT },
  553. { .route = 0x303030303, .port = 1, .type = TB_TYPE_PORT },
  554. { .route = 0x303030303, .port = 3, .type = TB_TYPE_PORT },
  555. { .route = 0x30303030303, .port = 1, .type = TB_TYPE_PORT },
  556. { .route = 0x30303030303, .port = 13, .type = TB_TYPE_DP_HDMI_OUT },
  557. };
  558. host = alloc_host(test);
  559. dev1 = alloc_dev_default(test, host, 0x1, true);
  560. dev2 = alloc_dev_default(test, dev1, 0x301, true);
  561. dev3 = alloc_dev_default(test, dev2, 0x30301, true);
  562. dev4 = alloc_dev_default(test, dev3, 0x3030301, true);
  563. dev5 = alloc_dev_default(test, dev4, 0x303030301, true);
  564. dev6 = alloc_dev_with_dpin(test, dev5, 0x30303030301, true);
  565. dev7 = alloc_dev_default(test, host, 0x3, true);
  566. dev8 = alloc_dev_default(test, dev7, 0x303, true);
  567. dev9 = alloc_dev_default(test, dev8, 0x30303, true);
  568. dev10 = alloc_dev_default(test, dev9, 0x3030303, true);
  569. dev11 = alloc_dev_default(test, dev10, 0x303030303, true);
  570. dev12 = alloc_dev_default(test, dev11, 0x30303030303, true);
  571. src_port = &dev6->ports[13];
  572. dst_port = &dev12->ports[13];
  573. /* Walk both directions */
  574. i = 0;
  575. tb_for_each_port_on_path(src_port, dst_port, p) {
  576. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  577. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  578. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  579. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  580. test_data[i].type);
  581. i++;
  582. }
  583. KUNIT_EXPECT_EQ(test, i, (int)ARRAY_SIZE(test_data));
  584. i = ARRAY_SIZE(test_data) - 1;
  585. tb_for_each_port_on_path(dst_port, src_port, p) {
  586. KUNIT_EXPECT_TRUE(test, i < ARRAY_SIZE(test_data));
  587. KUNIT_EXPECT_EQ(test, tb_route(p->sw), test_data[i].route);
  588. KUNIT_EXPECT_EQ(test, p->port, test_data[i].port);
  589. KUNIT_EXPECT_EQ(test, (enum tb_port_type)p->config.type,
  590. test_data[i].type);
  591. i--;
  592. }
  593. KUNIT_EXPECT_EQ(test, i, -1);
  594. }
  595. static void tb_test_path_not_connected(struct kunit *test)
  596. {
  597. struct tb_switch *host, *dev1, *dev2;
  598. struct tb_port *down, *up;
  599. struct tb_path *path;
  600. host = alloc_host(test);
  601. dev1 = alloc_dev_default(test, host, 0x3, false);
  602. /* Not connected to anything */
  603. dev2 = alloc_dev_default(test, NULL, 0x303, false);
  604. down = &dev1->ports[10];
  605. up = &dev2->ports[9];
  606. path = tb_path_alloc(NULL, down, 8, up, 8, 0, "PCIe Down");
  607. KUNIT_ASSERT_TRUE(test, path == NULL);
  608. path = tb_path_alloc(NULL, down, 8, up, 8, 1, "PCIe Down");
  609. KUNIT_ASSERT_TRUE(test, path == NULL);
  610. }
  611. struct hop_expectation {
  612. u64 route;
  613. u8 in_port;
  614. enum tb_port_type in_type;
  615. u8 out_port;
  616. enum tb_port_type out_type;
  617. };
  618. static void tb_test_path_not_bonded_lane0(struct kunit *test)
  619. {
  620. /*
  621. * PCIe path from host to device using lane 0.
  622. *
  623. * [Host]
  624. * 3 |: 4
  625. * 1 |: 2
  626. * [Device]
  627. */
  628. static const struct hop_expectation test_data[] = {
  629. {
  630. .route = 0x0,
  631. .in_port = 9,
  632. .in_type = TB_TYPE_PCIE_DOWN,
  633. .out_port = 3,
  634. .out_type = TB_TYPE_PORT,
  635. },
  636. {
  637. .route = 0x3,
  638. .in_port = 1,
  639. .in_type = TB_TYPE_PORT,
  640. .out_port = 9,
  641. .out_type = TB_TYPE_PCIE_UP,
  642. },
  643. };
  644. struct tb_switch *host, *dev;
  645. struct tb_port *down, *up;
  646. struct tb_path *path;
  647. int i;
  648. host = alloc_host(test);
  649. dev = alloc_dev_default(test, host, 0x3, false);
  650. down = &host->ports[9];
  651. up = &dev->ports[9];
  652. path = tb_path_alloc(NULL, down, 8, up, 8, 0, "PCIe Down");
  653. KUNIT_ASSERT_TRUE(test, path != NULL);
  654. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  655. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  656. const struct tb_port *in_port, *out_port;
  657. in_port = path->hops[i].in_port;
  658. out_port = path->hops[i].out_port;
  659. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  660. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  661. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  662. test_data[i].in_type);
  663. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  664. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  665. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  666. test_data[i].out_type);
  667. }
  668. tb_path_free(path);
  669. }
  670. static void tb_test_path_not_bonded_lane1(struct kunit *test)
  671. {
  672. /*
  673. * DP Video path from host to device using lane 1. Paths like
  674. * these are only used with Thunderbolt 1 devices where lane
  675. * bonding is not possible. USB4 specifically does not allow
  676. * paths like this (you either use lane 0 where lane 1 is
  677. * disabled or both lanes are bonded).
  678. *
  679. * [Host]
  680. * 1 :| 2
  681. * 1 :| 2
  682. * [Device]
  683. */
  684. static const struct hop_expectation test_data[] = {
  685. {
  686. .route = 0x0,
  687. .in_port = 5,
  688. .in_type = TB_TYPE_DP_HDMI_IN,
  689. .out_port = 2,
  690. .out_type = TB_TYPE_PORT,
  691. },
  692. {
  693. .route = 0x1,
  694. .in_port = 2,
  695. .in_type = TB_TYPE_PORT,
  696. .out_port = 13,
  697. .out_type = TB_TYPE_DP_HDMI_OUT,
  698. },
  699. };
  700. struct tb_switch *host, *dev;
  701. struct tb_port *in, *out;
  702. struct tb_path *path;
  703. int i;
  704. host = alloc_host(test);
  705. dev = alloc_dev_default(test, host, 0x1, false);
  706. in = &host->ports[5];
  707. out = &dev->ports[13];
  708. path = tb_path_alloc(NULL, in, 9, out, 9, 1, "Video");
  709. KUNIT_ASSERT_TRUE(test, path != NULL);
  710. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  711. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  712. const struct tb_port *in_port, *out_port;
  713. in_port = path->hops[i].in_port;
  714. out_port = path->hops[i].out_port;
  715. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  716. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  717. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  718. test_data[i].in_type);
  719. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  720. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  721. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  722. test_data[i].out_type);
  723. }
  724. tb_path_free(path);
  725. }
  726. static void tb_test_path_not_bonded_lane1_chain(struct kunit *test)
  727. {
  728. /*
  729. * DP Video path from host to device 3 using lane 1.
  730. *
  731. * [Host]
  732. * 1 :| 2
  733. * 1 :| 2
  734. * [Device #1]
  735. * 7 :| 8
  736. * 1 :| 2
  737. * [Device #2]
  738. * 5 :| 6
  739. * 1 :| 2
  740. * [Device #3]
  741. */
  742. static const struct hop_expectation test_data[] = {
  743. {
  744. .route = 0x0,
  745. .in_port = 5,
  746. .in_type = TB_TYPE_DP_HDMI_IN,
  747. .out_port = 2,
  748. .out_type = TB_TYPE_PORT,
  749. },
  750. {
  751. .route = 0x1,
  752. .in_port = 2,
  753. .in_type = TB_TYPE_PORT,
  754. .out_port = 8,
  755. .out_type = TB_TYPE_PORT,
  756. },
  757. {
  758. .route = 0x701,
  759. .in_port = 2,
  760. .in_type = TB_TYPE_PORT,
  761. .out_port = 6,
  762. .out_type = TB_TYPE_PORT,
  763. },
  764. {
  765. .route = 0x50701,
  766. .in_port = 2,
  767. .in_type = TB_TYPE_PORT,
  768. .out_port = 13,
  769. .out_type = TB_TYPE_DP_HDMI_OUT,
  770. },
  771. };
  772. struct tb_switch *host, *dev1, *dev2, *dev3;
  773. struct tb_port *in, *out;
  774. struct tb_path *path;
  775. int i;
  776. host = alloc_host(test);
  777. dev1 = alloc_dev_default(test, host, 0x1, false);
  778. dev2 = alloc_dev_default(test, dev1, 0x701, false);
  779. dev3 = alloc_dev_default(test, dev2, 0x50701, false);
  780. in = &host->ports[5];
  781. out = &dev3->ports[13];
  782. path = tb_path_alloc(NULL, in, 9, out, 9, 1, "Video");
  783. KUNIT_ASSERT_TRUE(test, path != NULL);
  784. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  785. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  786. const struct tb_port *in_port, *out_port;
  787. in_port = path->hops[i].in_port;
  788. out_port = path->hops[i].out_port;
  789. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  790. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  791. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  792. test_data[i].in_type);
  793. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  794. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  795. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  796. test_data[i].out_type);
  797. }
  798. tb_path_free(path);
  799. }
  800. static void tb_test_path_not_bonded_lane1_chain_reverse(struct kunit *test)
  801. {
  802. /*
  803. * DP Video path from device 3 to host using lane 1.
  804. *
  805. * [Host]
  806. * 1 :| 2
  807. * 1 :| 2
  808. * [Device #1]
  809. * 7 :| 8
  810. * 1 :| 2
  811. * [Device #2]
  812. * 5 :| 6
  813. * 1 :| 2
  814. * [Device #3]
  815. */
  816. static const struct hop_expectation test_data[] = {
  817. {
  818. .route = 0x50701,
  819. .in_port = 13,
  820. .in_type = TB_TYPE_DP_HDMI_IN,
  821. .out_port = 2,
  822. .out_type = TB_TYPE_PORT,
  823. },
  824. {
  825. .route = 0x701,
  826. .in_port = 6,
  827. .in_type = TB_TYPE_PORT,
  828. .out_port = 2,
  829. .out_type = TB_TYPE_PORT,
  830. },
  831. {
  832. .route = 0x1,
  833. .in_port = 8,
  834. .in_type = TB_TYPE_PORT,
  835. .out_port = 2,
  836. .out_type = TB_TYPE_PORT,
  837. },
  838. {
  839. .route = 0x0,
  840. .in_port = 2,
  841. .in_type = TB_TYPE_PORT,
  842. .out_port = 5,
  843. .out_type = TB_TYPE_DP_HDMI_IN,
  844. },
  845. };
  846. struct tb_switch *host, *dev1, *dev2, *dev3;
  847. struct tb_port *in, *out;
  848. struct tb_path *path;
  849. int i;
  850. host = alloc_host(test);
  851. dev1 = alloc_dev_default(test, host, 0x1, false);
  852. dev2 = alloc_dev_default(test, dev1, 0x701, false);
  853. dev3 = alloc_dev_with_dpin(test, dev2, 0x50701, false);
  854. in = &dev3->ports[13];
  855. out = &host->ports[5];
  856. path = tb_path_alloc(NULL, in, 9, out, 9, 1, "Video");
  857. KUNIT_ASSERT_TRUE(test, path != NULL);
  858. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  859. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  860. const struct tb_port *in_port, *out_port;
  861. in_port = path->hops[i].in_port;
  862. out_port = path->hops[i].out_port;
  863. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  864. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  865. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  866. test_data[i].in_type);
  867. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  868. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  869. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  870. test_data[i].out_type);
  871. }
  872. tb_path_free(path);
  873. }
  874. static void tb_test_path_mixed_chain(struct kunit *test)
  875. {
  876. /*
  877. * DP Video path from host to device 4 where first and last link
  878. * is bonded.
  879. *
  880. * [Host]
  881. * 1 |
  882. * 1 |
  883. * [Device #1]
  884. * 7 :| 8
  885. * 1 :| 2
  886. * [Device #2]
  887. * 5 :| 6
  888. * 1 :| 2
  889. * [Device #3]
  890. * 3 |
  891. * 1 |
  892. * [Device #4]
  893. */
  894. static const struct hop_expectation test_data[] = {
  895. {
  896. .route = 0x0,
  897. .in_port = 5,
  898. .in_type = TB_TYPE_DP_HDMI_IN,
  899. .out_port = 1,
  900. .out_type = TB_TYPE_PORT,
  901. },
  902. {
  903. .route = 0x1,
  904. .in_port = 1,
  905. .in_type = TB_TYPE_PORT,
  906. .out_port = 8,
  907. .out_type = TB_TYPE_PORT,
  908. },
  909. {
  910. .route = 0x701,
  911. .in_port = 2,
  912. .in_type = TB_TYPE_PORT,
  913. .out_port = 6,
  914. .out_type = TB_TYPE_PORT,
  915. },
  916. {
  917. .route = 0x50701,
  918. .in_port = 2,
  919. .in_type = TB_TYPE_PORT,
  920. .out_port = 3,
  921. .out_type = TB_TYPE_PORT,
  922. },
  923. {
  924. .route = 0x3050701,
  925. .in_port = 1,
  926. .in_type = TB_TYPE_PORT,
  927. .out_port = 13,
  928. .out_type = TB_TYPE_DP_HDMI_OUT,
  929. },
  930. };
  931. struct tb_switch *host, *dev1, *dev2, *dev3, *dev4;
  932. struct tb_port *in, *out;
  933. struct tb_path *path;
  934. int i;
  935. host = alloc_host(test);
  936. dev1 = alloc_dev_default(test, host, 0x1, true);
  937. dev2 = alloc_dev_default(test, dev1, 0x701, false);
  938. dev3 = alloc_dev_default(test, dev2, 0x50701, false);
  939. dev4 = alloc_dev_default(test, dev3, 0x3050701, true);
  940. in = &host->ports[5];
  941. out = &dev4->ports[13];
  942. path = tb_path_alloc(NULL, in, 9, out, 9, 1, "Video");
  943. KUNIT_ASSERT_TRUE(test, path != NULL);
  944. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  945. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  946. const struct tb_port *in_port, *out_port;
  947. in_port = path->hops[i].in_port;
  948. out_port = path->hops[i].out_port;
  949. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  950. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  951. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  952. test_data[i].in_type);
  953. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  954. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  955. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  956. test_data[i].out_type);
  957. }
  958. tb_path_free(path);
  959. }
  960. static void tb_test_path_mixed_chain_reverse(struct kunit *test)
  961. {
  962. /*
  963. * DP Video path from device 4 to host where first and last link
  964. * is bonded.
  965. *
  966. * [Host]
  967. * 1 |
  968. * 1 |
  969. * [Device #1]
  970. * 7 :| 8
  971. * 1 :| 2
  972. * [Device #2]
  973. * 5 :| 6
  974. * 1 :| 2
  975. * [Device #3]
  976. * 3 |
  977. * 1 |
  978. * [Device #4]
  979. */
  980. static const struct hop_expectation test_data[] = {
  981. {
  982. .route = 0x3050701,
  983. .in_port = 13,
  984. .in_type = TB_TYPE_DP_HDMI_OUT,
  985. .out_port = 1,
  986. .out_type = TB_TYPE_PORT,
  987. },
  988. {
  989. .route = 0x50701,
  990. .in_port = 3,
  991. .in_type = TB_TYPE_PORT,
  992. .out_port = 2,
  993. .out_type = TB_TYPE_PORT,
  994. },
  995. {
  996. .route = 0x701,
  997. .in_port = 6,
  998. .in_type = TB_TYPE_PORT,
  999. .out_port = 2,
  1000. .out_type = TB_TYPE_PORT,
  1001. },
  1002. {
  1003. .route = 0x1,
  1004. .in_port = 8,
  1005. .in_type = TB_TYPE_PORT,
  1006. .out_port = 1,
  1007. .out_type = TB_TYPE_PORT,
  1008. },
  1009. {
  1010. .route = 0x0,
  1011. .in_port = 1,
  1012. .in_type = TB_TYPE_PORT,
  1013. .out_port = 5,
  1014. .out_type = TB_TYPE_DP_HDMI_IN,
  1015. },
  1016. };
  1017. struct tb_switch *host, *dev1, *dev2, *dev3, *dev4;
  1018. struct tb_port *in, *out;
  1019. struct tb_path *path;
  1020. int i;
  1021. host = alloc_host(test);
  1022. dev1 = alloc_dev_default(test, host, 0x1, true);
  1023. dev2 = alloc_dev_default(test, dev1, 0x701, false);
  1024. dev3 = alloc_dev_default(test, dev2, 0x50701, false);
  1025. dev4 = alloc_dev_default(test, dev3, 0x3050701, true);
  1026. in = &dev4->ports[13];
  1027. out = &host->ports[5];
  1028. path = tb_path_alloc(NULL, in, 9, out, 9, 1, "Video");
  1029. KUNIT_ASSERT_TRUE(test, path != NULL);
  1030. KUNIT_ASSERT_EQ(test, path->path_length, (int)ARRAY_SIZE(test_data));
  1031. for (i = 0; i < ARRAY_SIZE(test_data); i++) {
  1032. const struct tb_port *in_port, *out_port;
  1033. in_port = path->hops[i].in_port;
  1034. out_port = path->hops[i].out_port;
  1035. KUNIT_EXPECT_EQ(test, tb_route(in_port->sw), test_data[i].route);
  1036. KUNIT_EXPECT_EQ(test, in_port->port, test_data[i].in_port);
  1037. KUNIT_EXPECT_EQ(test, (enum tb_port_type)in_port->config.type,
  1038. test_data[i].in_type);
  1039. KUNIT_EXPECT_EQ(test, tb_route(out_port->sw), test_data[i].route);
  1040. KUNIT_EXPECT_EQ(test, out_port->port, test_data[i].out_port);
  1041. KUNIT_EXPECT_EQ(test, (enum tb_port_type)out_port->config.type,
  1042. test_data[i].out_type);
  1043. }
  1044. tb_path_free(path);
  1045. }
  1046. static void tb_test_tunnel_pcie(struct kunit *test)
  1047. {
  1048. struct tb_switch *host, *dev1, *dev2;
  1049. struct tb_tunnel *tunnel1, *tunnel2;
  1050. struct tb_port *down, *up;
  1051. /*
  1052. * Create PCIe tunnel between host and two devices.
  1053. *
  1054. * [Host]
  1055. * 1 |
  1056. * 1 |
  1057. * [Device #1]
  1058. * 5 |
  1059. * 1 |
  1060. * [Device #2]
  1061. */
  1062. host = alloc_host(test);
  1063. dev1 = alloc_dev_default(test, host, 0x1, true);
  1064. dev2 = alloc_dev_default(test, dev1, 0x501, true);
  1065. down = &host->ports[8];
  1066. up = &dev1->ports[9];
  1067. tunnel1 = tb_tunnel_alloc_pci(NULL, up, down);
  1068. KUNIT_ASSERT_TRUE(test, tunnel1 != NULL);
  1069. KUNIT_EXPECT_EQ(test, tunnel1->type, (enum tb_tunnel_type)TB_TUNNEL_PCI);
  1070. KUNIT_EXPECT_PTR_EQ(test, tunnel1->src_port, down);
  1071. KUNIT_EXPECT_PTR_EQ(test, tunnel1->dst_port, up);
  1072. KUNIT_ASSERT_EQ(test, tunnel1->npaths, (size_t)2);
  1073. KUNIT_ASSERT_EQ(test, tunnel1->paths[0]->path_length, 2);
  1074. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[0]->hops[0].in_port, down);
  1075. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[0]->hops[1].out_port, up);
  1076. KUNIT_ASSERT_EQ(test, tunnel1->paths[1]->path_length, 2);
  1077. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[1]->hops[0].in_port, up);
  1078. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[1]->hops[1].out_port, down);
  1079. down = &dev1->ports[10];
  1080. up = &dev2->ports[9];
  1081. tunnel2 = tb_tunnel_alloc_pci(NULL, up, down);
  1082. KUNIT_ASSERT_TRUE(test, tunnel2 != NULL);
  1083. KUNIT_EXPECT_EQ(test, tunnel2->type, (enum tb_tunnel_type)TB_TUNNEL_PCI);
  1084. KUNIT_EXPECT_PTR_EQ(test, tunnel2->src_port, down);
  1085. KUNIT_EXPECT_PTR_EQ(test, tunnel2->dst_port, up);
  1086. KUNIT_ASSERT_EQ(test, tunnel2->npaths, (size_t)2);
  1087. KUNIT_ASSERT_EQ(test, tunnel2->paths[0]->path_length, 2);
  1088. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[0]->hops[0].in_port, down);
  1089. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[0]->hops[1].out_port, up);
  1090. KUNIT_ASSERT_EQ(test, tunnel2->paths[1]->path_length, 2);
  1091. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[0].in_port, up);
  1092. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[1].out_port, down);
  1093. tb_tunnel_free(tunnel2);
  1094. tb_tunnel_free(tunnel1);
  1095. }
  1096. static void tb_test_tunnel_dp(struct kunit *test)
  1097. {
  1098. struct tb_switch *host, *dev;
  1099. struct tb_port *in, *out;
  1100. struct tb_tunnel *tunnel;
  1101. /*
  1102. * Create DP tunnel between Host and Device
  1103. *
  1104. * [Host]
  1105. * 1 |
  1106. * 1 |
  1107. * [Device]
  1108. */
  1109. host = alloc_host(test);
  1110. dev = alloc_dev_default(test, host, 0x3, true);
  1111. in = &host->ports[5];
  1112. out = &dev->ports[13];
  1113. tunnel = tb_tunnel_alloc_dp(NULL, in, out, 0, 0);
  1114. KUNIT_ASSERT_TRUE(test, tunnel != NULL);
  1115. KUNIT_EXPECT_EQ(test, tunnel->type, (enum tb_tunnel_type)TB_TUNNEL_DP);
  1116. KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in);
  1117. KUNIT_EXPECT_PTR_EQ(test, tunnel->dst_port, out);
  1118. KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)3);
  1119. KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 2);
  1120. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, in);
  1121. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[1].out_port, out);
  1122. KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 2);
  1123. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, in);
  1124. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[1].out_port, out);
  1125. KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 2);
  1126. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out);
  1127. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[1].out_port, in);
  1128. tb_tunnel_free(tunnel);
  1129. }
  1130. static void tb_test_tunnel_dp_chain(struct kunit *test)
  1131. {
  1132. struct tb_switch *host, *dev1, *dev4;
  1133. struct tb_port *in, *out;
  1134. struct tb_tunnel *tunnel;
  1135. /*
  1136. * Create DP tunnel from Host DP IN to Device #4 DP OUT.
  1137. *
  1138. * [Host]
  1139. * 1 |
  1140. * 1 |
  1141. * [Device #1]
  1142. * 3 / | 5 \ 7
  1143. * 1 / | \ 1
  1144. * [Device #2] | [Device #4]
  1145. * | 1
  1146. * [Device #3]
  1147. */
  1148. host = alloc_host(test);
  1149. dev1 = alloc_dev_default(test, host, 0x1, true);
  1150. alloc_dev_default(test, dev1, 0x301, true);
  1151. alloc_dev_default(test, dev1, 0x501, true);
  1152. dev4 = alloc_dev_default(test, dev1, 0x701, true);
  1153. in = &host->ports[5];
  1154. out = &dev4->ports[14];
  1155. tunnel = tb_tunnel_alloc_dp(NULL, in, out, 0, 0);
  1156. KUNIT_ASSERT_TRUE(test, tunnel != NULL);
  1157. KUNIT_EXPECT_EQ(test, tunnel->type, (enum tb_tunnel_type)TB_TUNNEL_DP);
  1158. KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in);
  1159. KUNIT_EXPECT_PTR_EQ(test, tunnel->dst_port, out);
  1160. KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)3);
  1161. KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 3);
  1162. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, in);
  1163. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[2].out_port, out);
  1164. KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 3);
  1165. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, in);
  1166. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[2].out_port, out);
  1167. KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 3);
  1168. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out);
  1169. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[2].out_port, in);
  1170. tb_tunnel_free(tunnel);
  1171. }
  1172. static void tb_test_tunnel_dp_tree(struct kunit *test)
  1173. {
  1174. struct tb_switch *host, *dev1, *dev2, *dev3, *dev5;
  1175. struct tb_port *in, *out;
  1176. struct tb_tunnel *tunnel;
  1177. /*
  1178. * Create DP tunnel from Device #2 DP IN to Device #5 DP OUT.
  1179. *
  1180. * [Host]
  1181. * 3 |
  1182. * 1 |
  1183. * [Device #1]
  1184. * 3 / | 5 \ 7
  1185. * 1 / | \ 1
  1186. * [Device #2] | [Device #4]
  1187. * | 1
  1188. * [Device #3]
  1189. * | 5
  1190. * | 1
  1191. * [Device #5]
  1192. */
  1193. host = alloc_host(test);
  1194. dev1 = alloc_dev_default(test, host, 0x3, true);
  1195. dev2 = alloc_dev_with_dpin(test, dev1, 0x303, true);
  1196. dev3 = alloc_dev_default(test, dev1, 0x503, true);
  1197. alloc_dev_default(test, dev1, 0x703, true);
  1198. dev5 = alloc_dev_default(test, dev3, 0x50503, true);
  1199. in = &dev2->ports[13];
  1200. out = &dev5->ports[13];
  1201. tunnel = tb_tunnel_alloc_dp(NULL, in, out, 0, 0);
  1202. KUNIT_ASSERT_TRUE(test, tunnel != NULL);
  1203. KUNIT_EXPECT_EQ(test, tunnel->type, (enum tb_tunnel_type)TB_TUNNEL_DP);
  1204. KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in);
  1205. KUNIT_EXPECT_PTR_EQ(test, tunnel->dst_port, out);
  1206. KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)3);
  1207. KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 4);
  1208. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, in);
  1209. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[3].out_port, out);
  1210. KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 4);
  1211. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, in);
  1212. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[3].out_port, out);
  1213. KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 4);
  1214. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out);
  1215. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[3].out_port, in);
  1216. tb_tunnel_free(tunnel);
  1217. }
  1218. static void tb_test_tunnel_dp_max_length(struct kunit *test)
  1219. {
  1220. struct tb_switch *host, *dev1, *dev2, *dev3, *dev4, *dev5, *dev6;
  1221. struct tb_switch *dev7, *dev8, *dev9, *dev10, *dev11, *dev12;
  1222. struct tb_port *in, *out;
  1223. struct tb_tunnel *tunnel;
  1224. /*
  1225. * Creates DP tunnel from Device #6 to Device #12.
  1226. *
  1227. * [Host]
  1228. * 1 / \ 3
  1229. * 1 / \ 1
  1230. * [Device #1] [Device #7]
  1231. * 3 | | 3
  1232. * 1 | | 1
  1233. * [Device #2] [Device #8]
  1234. * 3 | | 3
  1235. * 1 | | 1
  1236. * [Device #3] [Device #9]
  1237. * 3 | | 3
  1238. * 1 | | 1
  1239. * [Device #4] [Device #10]
  1240. * 3 | | 3
  1241. * 1 | | 1
  1242. * [Device #5] [Device #11]
  1243. * 3 | | 3
  1244. * 1 | | 1
  1245. * [Device #6] [Device #12]
  1246. */
  1247. host = alloc_host(test);
  1248. dev1 = alloc_dev_default(test, host, 0x1, true);
  1249. dev2 = alloc_dev_default(test, dev1, 0x301, true);
  1250. dev3 = alloc_dev_default(test, dev2, 0x30301, true);
  1251. dev4 = alloc_dev_default(test, dev3, 0x3030301, true);
  1252. dev5 = alloc_dev_default(test, dev4, 0x303030301, true);
  1253. dev6 = alloc_dev_with_dpin(test, dev5, 0x30303030301, true);
  1254. dev7 = alloc_dev_default(test, host, 0x3, true);
  1255. dev8 = alloc_dev_default(test, dev7, 0x303, true);
  1256. dev9 = alloc_dev_default(test, dev8, 0x30303, true);
  1257. dev10 = alloc_dev_default(test, dev9, 0x3030303, true);
  1258. dev11 = alloc_dev_default(test, dev10, 0x303030303, true);
  1259. dev12 = alloc_dev_default(test, dev11, 0x30303030303, true);
  1260. in = &dev6->ports[13];
  1261. out = &dev12->ports[13];
  1262. tunnel = tb_tunnel_alloc_dp(NULL, in, out, 0, 0);
  1263. KUNIT_ASSERT_TRUE(test, tunnel != NULL);
  1264. KUNIT_EXPECT_EQ(test, tunnel->type, (enum tb_tunnel_type)TB_TUNNEL_DP);
  1265. KUNIT_EXPECT_PTR_EQ(test, tunnel->src_port, in);
  1266. KUNIT_EXPECT_PTR_EQ(test, tunnel->dst_port, out);
  1267. KUNIT_ASSERT_EQ(test, tunnel->npaths, (size_t)3);
  1268. KUNIT_ASSERT_EQ(test, tunnel->paths[0]->path_length, 13);
  1269. /* First hop */
  1270. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[0].in_port, in);
  1271. /* Middle */
  1272. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[6].in_port,
  1273. &host->ports[1]);
  1274. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[6].out_port,
  1275. &host->ports[3]);
  1276. /* Last */
  1277. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[0]->hops[12].out_port, out);
  1278. KUNIT_ASSERT_EQ(test, tunnel->paths[1]->path_length, 13);
  1279. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[0].in_port, in);
  1280. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[6].in_port,
  1281. &host->ports[1]);
  1282. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[6].out_port,
  1283. &host->ports[3]);
  1284. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[1]->hops[12].out_port, out);
  1285. KUNIT_ASSERT_EQ(test, tunnel->paths[2]->path_length, 13);
  1286. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[0].in_port, out);
  1287. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[6].in_port,
  1288. &host->ports[3]);
  1289. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[6].out_port,
  1290. &host->ports[1]);
  1291. KUNIT_EXPECT_PTR_EQ(test, tunnel->paths[2]->hops[12].out_port, in);
  1292. tb_tunnel_free(tunnel);
  1293. }
  1294. static void tb_test_tunnel_usb3(struct kunit *test)
  1295. {
  1296. struct tb_switch *host, *dev1, *dev2;
  1297. struct tb_tunnel *tunnel1, *tunnel2;
  1298. struct tb_port *down, *up;
  1299. /*
  1300. * Create USB3 tunnel between host and two devices.
  1301. *
  1302. * [Host]
  1303. * 1 |
  1304. * 1 |
  1305. * [Device #1]
  1306. * \ 7
  1307. * \ 1
  1308. * [Device #2]
  1309. */
  1310. host = alloc_host(test);
  1311. dev1 = alloc_dev_default(test, host, 0x1, true);
  1312. dev2 = alloc_dev_default(test, dev1, 0x701, true);
  1313. down = &host->ports[12];
  1314. up = &dev1->ports[16];
  1315. tunnel1 = tb_tunnel_alloc_usb3(NULL, up, down, 0, 0);
  1316. KUNIT_ASSERT_TRUE(test, tunnel1 != NULL);
  1317. KUNIT_EXPECT_EQ(test, tunnel1->type, (enum tb_tunnel_type)TB_TUNNEL_USB3);
  1318. KUNIT_EXPECT_PTR_EQ(test, tunnel1->src_port, down);
  1319. KUNIT_EXPECT_PTR_EQ(test, tunnel1->dst_port, up);
  1320. KUNIT_ASSERT_EQ(test, tunnel1->npaths, (size_t)2);
  1321. KUNIT_ASSERT_EQ(test, tunnel1->paths[0]->path_length, 2);
  1322. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[0]->hops[0].in_port, down);
  1323. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[0]->hops[1].out_port, up);
  1324. KUNIT_ASSERT_EQ(test, tunnel1->paths[1]->path_length, 2);
  1325. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[1]->hops[0].in_port, up);
  1326. KUNIT_EXPECT_PTR_EQ(test, tunnel1->paths[1]->hops[1].out_port, down);
  1327. down = &dev1->ports[17];
  1328. up = &dev2->ports[16];
  1329. tunnel2 = tb_tunnel_alloc_usb3(NULL, up, down, 0, 0);
  1330. KUNIT_ASSERT_TRUE(test, tunnel2 != NULL);
  1331. KUNIT_EXPECT_EQ(test, tunnel2->type, (enum tb_tunnel_type)TB_TUNNEL_USB3);
  1332. KUNIT_EXPECT_PTR_EQ(test, tunnel2->src_port, down);
  1333. KUNIT_EXPECT_PTR_EQ(test, tunnel2->dst_port, up);
  1334. KUNIT_ASSERT_EQ(test, tunnel2->npaths, (size_t)2);
  1335. KUNIT_ASSERT_EQ(test, tunnel2->paths[0]->path_length, 2);
  1336. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[0]->hops[0].in_port, down);
  1337. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[0]->hops[1].out_port, up);
  1338. KUNIT_ASSERT_EQ(test, tunnel2->paths[1]->path_length, 2);
  1339. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[0].in_port, up);
  1340. KUNIT_EXPECT_PTR_EQ(test, tunnel2->paths[1]->hops[1].out_port, down);
  1341. tb_tunnel_free(tunnel2);
  1342. tb_tunnel_free(tunnel1);
  1343. }
  1344. static void tb_test_tunnel_port_on_path(struct kunit *test)
  1345. {
  1346. struct tb_switch *host, *dev1, *dev2, *dev3, *dev4, *dev5;
  1347. struct tb_port *in, *out, *port;
  1348. struct tb_tunnel *dp_tunnel;
  1349. /*
  1350. * [Host]
  1351. * 3 |
  1352. * 1 |
  1353. * [Device #1]
  1354. * 3 / | 5 \ 7
  1355. * 1 / | \ 1
  1356. * [Device #2] | [Device #4]
  1357. * | 1
  1358. * [Device #3]
  1359. * | 5
  1360. * | 1
  1361. * [Device #5]
  1362. */
  1363. host = alloc_host(test);
  1364. dev1 = alloc_dev_default(test, host, 0x3, true);
  1365. dev2 = alloc_dev_with_dpin(test, dev1, 0x303, true);
  1366. dev3 = alloc_dev_default(test, dev1, 0x503, true);
  1367. dev4 = alloc_dev_default(test, dev1, 0x703, true);
  1368. dev5 = alloc_dev_default(test, dev3, 0x50503, true);
  1369. in = &dev2->ports[13];
  1370. out = &dev5->ports[13];
  1371. dp_tunnel = tb_tunnel_alloc_dp(NULL, in, out, 0, 0);
  1372. KUNIT_ASSERT_TRUE(test, dp_tunnel != NULL);
  1373. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, in));
  1374. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, out));
  1375. port = &host->ports[8];
  1376. KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1377. port = &host->ports[3];
  1378. KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1379. port = &dev1->ports[1];
  1380. KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1381. port = &dev1->ports[3];
  1382. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1383. port = &dev1->ports[5];
  1384. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1385. port = &dev1->ports[7];
  1386. KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1387. port = &dev3->ports[1];
  1388. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1389. port = &dev5->ports[1];
  1390. KUNIT_EXPECT_TRUE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1391. port = &dev4->ports[1];
  1392. KUNIT_EXPECT_FALSE(test, tb_tunnel_port_on_path(dp_tunnel, port));
  1393. tb_tunnel_free(dp_tunnel);
  1394. }
  1395. static struct kunit_case tb_test_cases[] = {
  1396. KUNIT_CASE(tb_test_path_basic),
  1397. KUNIT_CASE(tb_test_path_not_connected_walk),
  1398. KUNIT_CASE(tb_test_path_single_hop_walk),
  1399. KUNIT_CASE(tb_test_path_daisy_chain_walk),
  1400. KUNIT_CASE(tb_test_path_simple_tree_walk),
  1401. KUNIT_CASE(tb_test_path_complex_tree_walk),
  1402. KUNIT_CASE(tb_test_path_max_length_walk),
  1403. KUNIT_CASE(tb_test_path_not_connected),
  1404. KUNIT_CASE(tb_test_path_not_bonded_lane0),
  1405. KUNIT_CASE(tb_test_path_not_bonded_lane1),
  1406. KUNIT_CASE(tb_test_path_not_bonded_lane1_chain),
  1407. KUNIT_CASE(tb_test_path_not_bonded_lane1_chain_reverse),
  1408. KUNIT_CASE(tb_test_path_mixed_chain),
  1409. KUNIT_CASE(tb_test_path_mixed_chain_reverse),
  1410. KUNIT_CASE(tb_test_tunnel_pcie),
  1411. KUNIT_CASE(tb_test_tunnel_dp),
  1412. KUNIT_CASE(tb_test_tunnel_dp_chain),
  1413. KUNIT_CASE(tb_test_tunnel_dp_tree),
  1414. KUNIT_CASE(tb_test_tunnel_dp_max_length),
  1415. KUNIT_CASE(tb_test_tunnel_port_on_path),
  1416. KUNIT_CASE(tb_test_tunnel_usb3),
  1417. { }
  1418. };
  1419. static struct kunit_suite tb_test_suite = {
  1420. .name = "thunderbolt",
  1421. .test_cases = tb_test_cases,
  1422. };
  1423. static struct kunit_suite *tb_test_suites[] = { &tb_test_suite, NULL };
  1424. int tb_test_init(void)
  1425. {
  1426. return __kunit_test_suites_init(tb_test_suites);
  1427. }
  1428. void tb_test_exit(void)
  1429. {
  1430. return __kunit_test_suites_exit(tb_test_suites);
  1431. }