path.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - path/tunnel functionality
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2019, Intel Corporation
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/errno.h>
  10. #include <linux/delay.h>
  11. #include <linux/ktime.h>
  12. #include "tb.h"
  13. static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop *regs)
  14. {
  15. const struct tb_port *port = hop->in_port;
  16. tb_port_dbg(port, " In HopID: %d => Out port: %d Out HopID: %d\n",
  17. hop->in_hop_index, regs->out_port, regs->next_hop);
  18. tb_port_dbg(port, " Weight: %d Priority: %d Credits: %d Drop: %d PM: %d\n",
  19. regs->weight, regs->priority, regs->initial_credits,
  20. regs->drop_packages, regs->pmps);
  21. tb_port_dbg(port, " Counter enabled: %d Counter index: %d\n",
  22. regs->counter_enable, regs->counter);
  23. tb_port_dbg(port, " Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
  24. regs->ingress_fc, regs->egress_fc,
  25. regs->ingress_shared_buffer, regs->egress_shared_buffer);
  26. tb_port_dbg(port, " Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
  27. regs->unknown1, regs->unknown2, regs->unknown3);
  28. }
  29. static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid,
  30. int dst_hopid)
  31. {
  32. struct tb_port *port, *out_port = NULL;
  33. struct tb_regs_hop hop;
  34. struct tb_switch *sw;
  35. int i, ret, hopid;
  36. hopid = src_hopid;
  37. port = src;
  38. for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) {
  39. sw = port->sw;
  40. ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2);
  41. if (ret) {
  42. tb_port_warn(port, "failed to read path at %d\n", hopid);
  43. return NULL;
  44. }
  45. if (!hop.enable)
  46. return NULL;
  47. out_port = &sw->ports[hop.out_port];
  48. hopid = hop.next_hop;
  49. port = out_port->remote;
  50. }
  51. return out_port && hopid == dst_hopid ? out_port : NULL;
  52. }
  53. static int tb_path_find_src_hopid(struct tb_port *src,
  54. const struct tb_port *dst, int dst_hopid)
  55. {
  56. struct tb_port *out;
  57. int i;
  58. for (i = TB_PATH_MIN_HOPID; i <= src->config.max_in_hop_id; i++) {
  59. out = tb_path_find_dst_port(src, i, dst_hopid);
  60. if (out == dst)
  61. return i;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * tb_path_discover() - Discover a path
  67. * @src: First input port of a path
  68. * @src_hopid: Starting HopID of a path (%-1 if don't care)
  69. * @dst: Expected destination port of the path (%NULL if don't care)
  70. * @dst_hopid: HopID to the @dst (%-1 if don't care)
  71. * @last: Last port is filled here if not %NULL
  72. * @name: Name of the path
  73. * @alloc_hopid: Allocate HopIDs for the ports
  74. *
  75. * Follows a path starting from @src and @src_hopid to the last output
  76. * port of the path. Allocates HopIDs for the visited ports (if
  77. * @alloc_hopid is true). Call tb_path_free() to release the path and
  78. * allocated HopIDs when the path is not needed anymore.
  79. *
  80. * Note function discovers also incomplete paths so caller should check
  81. * that the @dst port is the expected one. If it is not, the path can be
  82. * cleaned up by calling tb_path_deactivate() before tb_path_free().
  83. *
  84. * Return: Discovered path on success, %NULL in case of failure
  85. */
  86. struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
  87. struct tb_port *dst, int dst_hopid,
  88. struct tb_port **last, const char *name,
  89. bool alloc_hopid)
  90. {
  91. struct tb_port *out_port;
  92. struct tb_regs_hop hop;
  93. struct tb_path *path;
  94. struct tb_switch *sw;
  95. struct tb_port *p;
  96. size_t num_hops;
  97. int ret, i, h;
  98. if (src_hopid < 0 && dst) {
  99. /*
  100. * For incomplete paths the intermediate HopID can be
  101. * different from the one used by the protocol adapter
  102. * so in that case find a path that ends on @dst with
  103. * matching @dst_hopid. That should give us the correct
  104. * HopID for the @src.
  105. */
  106. src_hopid = tb_path_find_src_hopid(src, dst, dst_hopid);
  107. if (!src_hopid)
  108. return NULL;
  109. }
  110. p = src;
  111. h = src_hopid;
  112. num_hops = 0;
  113. for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) {
  114. sw = p->sw;
  115. ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
  116. if (ret) {
  117. tb_port_warn(p, "failed to read path at %d\n", h);
  118. return NULL;
  119. }
  120. /* If the hop is not enabled we got an incomplete path */
  121. if (!hop.enable)
  122. break;
  123. out_port = &sw->ports[hop.out_port];
  124. if (last)
  125. *last = out_port;
  126. h = hop.next_hop;
  127. p = out_port->remote;
  128. num_hops++;
  129. }
  130. path = kzalloc(sizeof(*path), GFP_KERNEL);
  131. if (!path)
  132. return NULL;
  133. path->name = name;
  134. path->tb = src->sw->tb;
  135. path->path_length = num_hops;
  136. path->activated = true;
  137. path->alloc_hopid = alloc_hopid;
  138. path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
  139. if (!path->hops) {
  140. kfree(path);
  141. return NULL;
  142. }
  143. tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n",
  144. path->name, tb_route(src->sw), src->port);
  145. p = src;
  146. h = src_hopid;
  147. for (i = 0; i < num_hops; i++) {
  148. int next_hop;
  149. sw = p->sw;
  150. ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
  151. if (ret) {
  152. tb_port_warn(p, "failed to read path at %d\n", h);
  153. goto err;
  154. }
  155. if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0)
  156. goto err;
  157. out_port = &sw->ports[hop.out_port];
  158. next_hop = hop.next_hop;
  159. if (alloc_hopid &&
  160. tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
  161. tb_port_release_in_hopid(p, h);
  162. goto err;
  163. }
  164. path->hops[i].in_port = p;
  165. path->hops[i].in_hop_index = h;
  166. path->hops[i].in_counter_index = -1;
  167. path->hops[i].out_port = out_port;
  168. path->hops[i].next_hop_index = next_hop;
  169. tb_dump_hop(&path->hops[i], &hop);
  170. h = next_hop;
  171. p = out_port->remote;
  172. }
  173. tb_dbg(path->tb, "path discovery complete\n");
  174. return path;
  175. err:
  176. tb_port_warn(src, "failed to discover path starting at HopID %d\n",
  177. src_hopid);
  178. tb_path_free(path);
  179. return NULL;
  180. }
  181. /**
  182. * tb_path_alloc() - allocate a thunderbolt path between two ports
  183. * @tb: Domain pointer
  184. * @src: Source port of the path
  185. * @src_hopid: HopID used for the first ingress port in the path
  186. * @dst: Destination port of the path
  187. * @dst_hopid: HopID used for the last egress port in the path
  188. * @link_nr: Preferred link if there are dual links on the path
  189. * @name: Name of the path
  190. *
  191. * Creates path between two ports starting with given @src_hopid. Reserves
  192. * HopIDs for each port (they can be different from @src_hopid depending on
  193. * how many HopIDs each port already have reserved). If there are dual
  194. * links on the path, prioritizes using @link_nr but takes into account
  195. * that the lanes may be bonded.
  196. *
  197. * Return: Returns a tb_path on success or NULL on failure.
  198. */
  199. struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
  200. struct tb_port *dst, int dst_hopid, int link_nr,
  201. const char *name)
  202. {
  203. struct tb_port *in_port, *out_port, *first_port, *last_port;
  204. int in_hopid, out_hopid;
  205. struct tb_path *path;
  206. size_t num_hops;
  207. int i, ret;
  208. path = kzalloc(sizeof(*path), GFP_KERNEL);
  209. if (!path)
  210. return NULL;
  211. first_port = last_port = NULL;
  212. i = 0;
  213. tb_for_each_port_on_path(src, dst, in_port) {
  214. if (!first_port)
  215. first_port = in_port;
  216. last_port = in_port;
  217. i++;
  218. }
  219. /* Check that src and dst are reachable */
  220. if (first_port != src || last_port != dst) {
  221. kfree(path);
  222. return NULL;
  223. }
  224. /* Each hop takes two ports */
  225. num_hops = i / 2;
  226. path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
  227. if (!path->hops) {
  228. kfree(path);
  229. return NULL;
  230. }
  231. path->alloc_hopid = true;
  232. in_hopid = src_hopid;
  233. out_port = NULL;
  234. for (i = 0; i < num_hops; i++) {
  235. in_port = tb_next_port_on_path(src, dst, out_port);
  236. if (!in_port)
  237. goto err;
  238. /* When lanes are bonded primary link must be used */
  239. if (!in_port->bonded && in_port->dual_link_port &&
  240. in_port->link_nr != link_nr)
  241. in_port = in_port->dual_link_port;
  242. ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
  243. if (ret < 0)
  244. goto err;
  245. in_hopid = ret;
  246. out_port = tb_next_port_on_path(src, dst, in_port);
  247. if (!out_port)
  248. goto err;
  249. /*
  250. * Pick up right port when going from non-bonded to
  251. * bonded or from bonded to non-bonded.
  252. */
  253. if (out_port->dual_link_port) {
  254. if (!in_port->bonded && out_port->bonded &&
  255. out_port->link_nr) {
  256. /*
  257. * Use primary link when going from
  258. * non-bonded to bonded.
  259. */
  260. out_port = out_port->dual_link_port;
  261. } else if (!out_port->bonded &&
  262. out_port->link_nr != link_nr) {
  263. /*
  264. * If out port is not bonded follow
  265. * link_nr.
  266. */
  267. out_port = out_port->dual_link_port;
  268. }
  269. }
  270. if (i == num_hops - 1)
  271. ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
  272. dst_hopid);
  273. else
  274. ret = tb_port_alloc_out_hopid(out_port, -1, -1);
  275. if (ret < 0)
  276. goto err;
  277. out_hopid = ret;
  278. path->hops[i].in_hop_index = in_hopid;
  279. path->hops[i].in_port = in_port;
  280. path->hops[i].in_counter_index = -1;
  281. path->hops[i].out_port = out_port;
  282. path->hops[i].next_hop_index = out_hopid;
  283. in_hopid = out_hopid;
  284. }
  285. path->tb = tb;
  286. path->path_length = num_hops;
  287. path->name = name;
  288. return path;
  289. err:
  290. tb_path_free(path);
  291. return NULL;
  292. }
  293. /**
  294. * tb_path_free() - free a path
  295. * @path: Path to free
  296. *
  297. * Frees a path. The path does not need to be deactivated.
  298. */
  299. void tb_path_free(struct tb_path *path)
  300. {
  301. if (path->alloc_hopid) {
  302. int i;
  303. for (i = 0; i < path->path_length; i++) {
  304. const struct tb_path_hop *hop = &path->hops[i];
  305. if (hop->in_port)
  306. tb_port_release_in_hopid(hop->in_port,
  307. hop->in_hop_index);
  308. if (hop->out_port)
  309. tb_port_release_out_hopid(hop->out_port,
  310. hop->next_hop_index);
  311. }
  312. }
  313. kfree(path->hops);
  314. kfree(path);
  315. }
  316. static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
  317. {
  318. int i, res;
  319. for (i = first_hop; i < path->path_length; i++) {
  320. res = tb_port_add_nfc_credits(path->hops[i].in_port,
  321. -path->hops[i].nfc_credits);
  322. if (res)
  323. tb_port_warn(path->hops[i].in_port,
  324. "nfc credits deallocation failed for hop %d\n",
  325. i);
  326. }
  327. }
  328. static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
  329. bool clear_fc)
  330. {
  331. struct tb_regs_hop hop;
  332. ktime_t timeout;
  333. int ret;
  334. /* Disable the path */
  335. ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
  336. if (ret)
  337. return ret;
  338. /* Already disabled */
  339. if (!hop.enable)
  340. return 0;
  341. hop.enable = 0;
  342. ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
  343. if (ret)
  344. return ret;
  345. /* Wait until it is drained */
  346. timeout = ktime_add_ms(ktime_get(), 500);
  347. do {
  348. ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
  349. if (ret)
  350. return ret;
  351. if (!hop.pending) {
  352. if (clear_fc) {
  353. /*
  354. * Clear flow control. Protocol adapters
  355. * IFC and ISE bits are vendor defined
  356. * in the USB4 spec so we clear them
  357. * only for pre-USB4 adapters.
  358. */
  359. if (!tb_switch_is_usb4(port->sw)) {
  360. hop.ingress_fc = 0;
  361. hop.ingress_shared_buffer = 0;
  362. }
  363. hop.egress_fc = 0;
  364. hop.egress_shared_buffer = 0;
  365. return tb_port_write(port, &hop, TB_CFG_HOPS,
  366. 2 * hop_index, 2);
  367. }
  368. return 0;
  369. }
  370. usleep_range(10, 20);
  371. } while (ktime_before(ktime_get(), timeout));
  372. return -ETIMEDOUT;
  373. }
  374. /**
  375. * tb_path_deactivate_hop() - Deactivate one path in path config space
  376. * @port: Lane or protocol adapter
  377. * @hop_index: HopID of the path to be cleared
  378. *
  379. * This deactivates or clears a single path config space entry at
  380. * @hop_index. Returns %0 in success and negative errno otherwise.
  381. */
  382. int tb_path_deactivate_hop(struct tb_port *port, int hop_index)
  383. {
  384. return __tb_path_deactivate_hop(port, hop_index, true);
  385. }
  386. static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
  387. {
  388. int i, res;
  389. for (i = first_hop; i < path->path_length; i++) {
  390. res = __tb_path_deactivate_hop(path->hops[i].in_port,
  391. path->hops[i].in_hop_index,
  392. path->clear_fc);
  393. if (res && res != -ENODEV)
  394. tb_port_warn(path->hops[i].in_port,
  395. "hop deactivation failed for hop %d, index %d\n",
  396. i, path->hops[i].in_hop_index);
  397. }
  398. }
  399. void tb_path_deactivate(struct tb_path *path)
  400. {
  401. if (!path->activated) {
  402. tb_WARN(path->tb, "trying to deactivate an inactive path\n");
  403. return;
  404. }
  405. tb_dbg(path->tb,
  406. "deactivating %s path from %llx:%u to %llx:%u\n",
  407. path->name, tb_route(path->hops[0].in_port->sw),
  408. path->hops[0].in_port->port,
  409. tb_route(path->hops[path->path_length - 1].out_port->sw),
  410. path->hops[path->path_length - 1].out_port->port);
  411. __tb_path_deactivate_hops(path, 0);
  412. __tb_path_deallocate_nfc(path, 0);
  413. path->activated = false;
  414. }
  415. /**
  416. * tb_path_activate() - activate a path
  417. * @path: Path to activate
  418. *
  419. * Activate a path starting with the last hop and iterating backwards. The
  420. * caller must fill path->hops before calling tb_path_activate().
  421. *
  422. * Return: Returns 0 on success or an error code on failure.
  423. */
  424. int tb_path_activate(struct tb_path *path)
  425. {
  426. int i, res;
  427. enum tb_path_port out_mask, in_mask;
  428. if (path->activated) {
  429. tb_WARN(path->tb, "trying to activate already activated path\n");
  430. return -EINVAL;
  431. }
  432. tb_dbg(path->tb,
  433. "activating %s path from %llx:%u to %llx:%u\n",
  434. path->name, tb_route(path->hops[0].in_port->sw),
  435. path->hops[0].in_port->port,
  436. tb_route(path->hops[path->path_length - 1].out_port->sw),
  437. path->hops[path->path_length - 1].out_port->port);
  438. /* Clear counters. */
  439. for (i = path->path_length - 1; i >= 0; i--) {
  440. if (path->hops[i].in_counter_index == -1)
  441. continue;
  442. res = tb_port_clear_counter(path->hops[i].in_port,
  443. path->hops[i].in_counter_index);
  444. if (res)
  445. goto err;
  446. }
  447. /* Add non flow controlled credits. */
  448. for (i = path->path_length - 1; i >= 0; i--) {
  449. res = tb_port_add_nfc_credits(path->hops[i].in_port,
  450. path->hops[i].nfc_credits);
  451. if (res) {
  452. __tb_path_deallocate_nfc(path, i);
  453. goto err;
  454. }
  455. }
  456. /* Activate hops. */
  457. for (i = path->path_length - 1; i >= 0; i--) {
  458. struct tb_regs_hop hop = { 0 };
  459. /* If it is left active deactivate it first */
  460. __tb_path_deactivate_hop(path->hops[i].in_port,
  461. path->hops[i].in_hop_index, path->clear_fc);
  462. /* dword 0 */
  463. hop.next_hop = path->hops[i].next_hop_index;
  464. hop.out_port = path->hops[i].out_port->port;
  465. hop.initial_credits = path->hops[i].initial_credits;
  466. hop.pmps = path->hops[i].pm_support;
  467. hop.unknown1 = 0;
  468. hop.enable = 1;
  469. /* dword 1 */
  470. out_mask = (i == path->path_length - 1) ?
  471. TB_PATH_DESTINATION : TB_PATH_INTERNAL;
  472. in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
  473. hop.weight = path->weight;
  474. hop.unknown2 = 0;
  475. hop.priority = path->priority;
  476. hop.drop_packages = path->drop_packages;
  477. hop.counter = path->hops[i].in_counter_index;
  478. hop.counter_enable = path->hops[i].in_counter_index != -1;
  479. hop.ingress_fc = path->ingress_fc_enable & in_mask;
  480. hop.egress_fc = path->egress_fc_enable & out_mask;
  481. hop.ingress_shared_buffer = path->ingress_shared_buffer
  482. & in_mask;
  483. hop.egress_shared_buffer = path->egress_shared_buffer
  484. & out_mask;
  485. hop.unknown3 = 0;
  486. tb_port_dbg(path->hops[i].in_port, "Writing hop %d\n", i);
  487. tb_dump_hop(&path->hops[i], &hop);
  488. res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
  489. 2 * path->hops[i].in_hop_index, 2);
  490. if (res) {
  491. __tb_path_deactivate_hops(path, i);
  492. __tb_path_deallocate_nfc(path, 0);
  493. goto err;
  494. }
  495. }
  496. path->activated = true;
  497. tb_dbg(path->tb, "path activation complete\n");
  498. return 0;
  499. err:
  500. tb_WARN(path->tb, "path activation failed\n");
  501. return res;
  502. }
  503. /**
  504. * tb_path_is_invalid() - check whether any ports on the path are invalid
  505. * @path: Path to check
  506. *
  507. * Return: Returns true if the path is invalid, false otherwise.
  508. */
  509. bool tb_path_is_invalid(struct tb_path *path)
  510. {
  511. int i = 0;
  512. for (i = 0; i < path->path_length; i++) {
  513. if (path->hops[i].in_port->sw->is_unplugged)
  514. return true;
  515. if (path->hops[i].out_port->sw->is_unplugged)
  516. return true;
  517. }
  518. return false;
  519. }
  520. /**
  521. * tb_path_port_on_path() - Does the path go through certain port
  522. * @path: Path to check
  523. * @port: Switch to check
  524. *
  525. * Goes over all hops on path and checks if @port is any of them.
  526. * Direction does not matter.
  527. */
  528. bool tb_path_port_on_path(const struct tb_path *path, const struct tb_port *port)
  529. {
  530. int i;
  531. for (i = 0; i < path->path_length; i++) {
  532. if (path->hops[i].in_port == port ||
  533. path->hops[i].out_port == port)
  534. return true;
  535. }
  536. return false;
  537. }