tunnel_pci.c 6.4 KB

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