tunnel_pci.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt Cactus Ridge driver - PCIe tunnel
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/list.h>
  9. #include "tunnel_pci.h"
  10. #include "tb.h"
  11. #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
  12. do { \
  13. struct tb_pci_tunnel *__tunnel = (tunnel); \
  14. level(__tunnel->tb, "%llx:%x <-> %llx:%x (PCI): " fmt, \
  15. tb_route(__tunnel->down_port->sw), \
  16. __tunnel->down_port->port, \
  17. tb_route(__tunnel->up_port->sw), \
  18. __tunnel->up_port->port, \
  19. ## arg); \
  20. } while (0)
  21. #define tb_tunnel_WARN(tunnel, fmt, arg...) \
  22. __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
  23. #define tb_tunnel_warn(tunnel, fmt, arg...) \
  24. __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
  25. #define tb_tunnel_info(tunnel, fmt, arg...) \
  26. __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
  27. static void tb_pci_init_path(struct tb_path *path)
  28. {
  29. path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
  30. path->egress_shared_buffer = TB_PATH_NONE;
  31. path->ingress_fc_enable = TB_PATH_ALL;
  32. path->ingress_shared_buffer = TB_PATH_NONE;
  33. path->priority = 3;
  34. path->weight = 1;
  35. path->drop_packages = 0;
  36. path->nfc_credits = 0;
  37. }
  38. /**
  39. * tb_pci_alloc() - allocate a pci tunnel
  40. *
  41. * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
  42. * TB_TYPE_PCIE_DOWN.
  43. *
  44. * Currently only paths consisting of two hops are supported (that is the
  45. * ports must be on "adjacent" switches).
  46. *
  47. * The paths are hard-coded to use hop 8 (the only working hop id available on
  48. * my thunderbolt devices). Therefore at most ONE path per device may be
  49. * activated.
  50. *
  51. * Return: Returns a tb_pci_tunnel on success or NULL on failure.
  52. */
  53. struct tb_pci_tunnel *tb_pci_alloc(struct tb *tb, struct tb_port *up,
  54. struct tb_port *down)
  55. {
  56. struct tb_pci_tunnel *tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
  57. if (!tunnel)
  58. goto err;
  59. tunnel->tb = tb;
  60. tunnel->down_port = down;
  61. tunnel->up_port = up;
  62. INIT_LIST_HEAD(&tunnel->list);
  63. tunnel->path_to_up = tb_path_alloc(up->sw->tb, 2);
  64. if (!tunnel->path_to_up)
  65. goto err;
  66. tunnel->path_to_down = tb_path_alloc(up->sw->tb, 2);
  67. if (!tunnel->path_to_down)
  68. goto err;
  69. tb_pci_init_path(tunnel->path_to_up);
  70. tb_pci_init_path(tunnel->path_to_down);
  71. tunnel->path_to_up->hops[0].in_port = down;
  72. tunnel->path_to_up->hops[0].in_hop_index = 8;
  73. tunnel->path_to_up->hops[0].in_counter_index = -1;
  74. tunnel->path_to_up->hops[0].out_port = tb_upstream_port(up->sw)->remote;
  75. tunnel->path_to_up->hops[0].next_hop_index = 8;
  76. tunnel->path_to_up->hops[1].in_port = tb_upstream_port(up->sw);
  77. tunnel->path_to_up->hops[1].in_hop_index = 8;
  78. tunnel->path_to_up->hops[1].in_counter_index = -1;
  79. tunnel->path_to_up->hops[1].out_port = up;
  80. tunnel->path_to_up->hops[1].next_hop_index = 8;
  81. tunnel->path_to_down->hops[0].in_port = up;
  82. tunnel->path_to_down->hops[0].in_hop_index = 8;
  83. tunnel->path_to_down->hops[0].in_counter_index = -1;
  84. tunnel->path_to_down->hops[0].out_port = tb_upstream_port(up->sw);
  85. tunnel->path_to_down->hops[0].next_hop_index = 8;
  86. tunnel->path_to_down->hops[1].in_port =
  87. tb_upstream_port(up->sw)->remote;
  88. tunnel->path_to_down->hops[1].in_hop_index = 8;
  89. tunnel->path_to_down->hops[1].in_counter_index = -1;
  90. tunnel->path_to_down->hops[1].out_port = down;
  91. tunnel->path_to_down->hops[1].next_hop_index = 8;
  92. return tunnel;
  93. err:
  94. if (tunnel) {
  95. if (tunnel->path_to_down)
  96. tb_path_free(tunnel->path_to_down);
  97. if (tunnel->path_to_up)
  98. tb_path_free(tunnel->path_to_up);
  99. kfree(tunnel);
  100. }
  101. return NULL;
  102. }
  103. /**
  104. * tb_pci_free() - free a tunnel
  105. *
  106. * The tunnel must have been deactivated.
  107. */
  108. void tb_pci_free(struct tb_pci_tunnel *tunnel)
  109. {
  110. if (tunnel->path_to_up->activated || tunnel->path_to_down->activated) {
  111. tb_tunnel_WARN(tunnel, "trying to free an activated tunnel\n");
  112. return;
  113. }
  114. tb_path_free(tunnel->path_to_up);
  115. tb_path_free(tunnel->path_to_down);
  116. kfree(tunnel);
  117. }
  118. /**
  119. * tb_pci_is_invalid - check whether an activated path is still valid
  120. */
  121. bool tb_pci_is_invalid(struct tb_pci_tunnel *tunnel)
  122. {
  123. WARN_ON(!tunnel->path_to_up->activated);
  124. WARN_ON(!tunnel->path_to_down->activated);
  125. return tb_path_is_invalid(tunnel->path_to_up)
  126. || tb_path_is_invalid(tunnel->path_to_down);
  127. }
  128. /**
  129. * tb_pci_port_active() - activate/deactivate PCI capability
  130. *
  131. * Return: Returns 0 on success or an error code on failure.
  132. */
  133. static int tb_pci_port_active(struct tb_port *port, bool active)
  134. {
  135. u32 word = active ? 0x80000000 : 0x0;
  136. int cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
  137. if (cap < 0) {
  138. tb_port_warn(port, "TB_PORT_CAP_ADAP not found: %d\n", cap);
  139. return cap;
  140. }
  141. return tb_port_write(port, &word, TB_CFG_PORT, cap, 1);
  142. }
  143. /**
  144. * tb_pci_restart() - activate a tunnel after a hardware reset
  145. */
  146. int tb_pci_restart(struct tb_pci_tunnel *tunnel)
  147. {
  148. int res;
  149. tunnel->path_to_up->activated = false;
  150. tunnel->path_to_down->activated = false;
  151. tb_tunnel_info(tunnel, "activating\n");
  152. res = tb_path_activate(tunnel->path_to_up);
  153. if (res)
  154. goto err;
  155. res = tb_path_activate(tunnel->path_to_down);
  156. if (res)
  157. goto err;
  158. res = tb_pci_port_active(tunnel->down_port, true);
  159. if (res)
  160. goto err;
  161. res = tb_pci_port_active(tunnel->up_port, true);
  162. if (res)
  163. goto err;
  164. return 0;
  165. err:
  166. tb_tunnel_warn(tunnel, "activation failed\n");
  167. tb_pci_deactivate(tunnel);
  168. return res;
  169. }
  170. /**
  171. * tb_pci_activate() - activate a tunnel
  172. *
  173. * Return: Returns 0 on success or an error code on failure.
  174. */
  175. int tb_pci_activate(struct tb_pci_tunnel *tunnel)
  176. {
  177. if (tunnel->path_to_up->activated || tunnel->path_to_down->activated) {
  178. tb_tunnel_WARN(tunnel,
  179. "trying to activate an already activated tunnel\n");
  180. return -EINVAL;
  181. }
  182. return tb_pci_restart(tunnel);
  183. }
  184. /**
  185. * tb_pci_deactivate() - deactivate a tunnel
  186. */
  187. void tb_pci_deactivate(struct tb_pci_tunnel *tunnel)
  188. {
  189. tb_tunnel_info(tunnel, "deactivating\n");
  190. /*
  191. * TODO: enable reset by writing 0x04000000 to TB_CAP_PCIE + 1 on up
  192. * port. Seems to have no effect?
  193. */
  194. tb_pci_port_active(tunnel->up_port, false);
  195. tb_pci_port_active(tunnel->down_port, false);
  196. if (tunnel->path_to_down->activated)
  197. tb_path_deactivate(tunnel->path_to_down);
  198. if (tunnel->path_to_up->activated)
  199. tb_path_deactivate(tunnel->path_to_up);
  200. }