path.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Thunderbolt Cactus Ridge driver - path/tunnel functionality
  3. *
  4. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/errno.h>
  8. #include "tb.h"
  9. static void tb_dump_hop(struct tb_port *port, struct tb_regs_hop *hop)
  10. {
  11. tb_port_info(port, " Hop through port %d to hop %d (%s)\n",
  12. hop->out_port, hop->next_hop,
  13. hop->enable ? "enabled" : "disabled");
  14. tb_port_info(port, " Weight: %d Priority: %d Credits: %d Drop: %d\n",
  15. hop->weight, hop->priority,
  16. hop->initial_credits, hop->drop_packages);
  17. tb_port_info(port, " Counter enabled: %d Counter index: %d\n",
  18. hop->counter_enable, hop->counter);
  19. tb_port_info(port, " Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
  20. hop->ingress_fc, hop->egress_fc,
  21. hop->ingress_shared_buffer, hop->egress_shared_buffer);
  22. tb_port_info(port, " Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
  23. hop->unknown1, hop->unknown2, hop->unknown3);
  24. }
  25. /**
  26. * tb_path_alloc() - allocate a thunderbolt path
  27. *
  28. * Return: Returns a tb_path on success or NULL on failure.
  29. */
  30. struct tb_path *tb_path_alloc(struct tb *tb, int num_hops)
  31. {
  32. struct tb_path *path = kzalloc(sizeof(*path), GFP_KERNEL);
  33. if (!path)
  34. return NULL;
  35. path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
  36. if (!path->hops) {
  37. kfree(path);
  38. return NULL;
  39. }
  40. path->tb = tb;
  41. path->path_length = num_hops;
  42. return path;
  43. }
  44. /**
  45. * tb_path_free() - free a deactivated path
  46. */
  47. void tb_path_free(struct tb_path *path)
  48. {
  49. if (path->activated) {
  50. tb_WARN(path->tb, "trying to free an activated path\n")
  51. return;
  52. }
  53. kfree(path->hops);
  54. kfree(path);
  55. }
  56. static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
  57. {
  58. int i, res;
  59. for (i = first_hop; i < path->path_length; i++) {
  60. res = tb_port_add_nfc_credits(path->hops[i].in_port,
  61. -path->nfc_credits);
  62. if (res)
  63. tb_port_warn(path->hops[i].in_port,
  64. "nfc credits deallocation failed for hop %d\n",
  65. i);
  66. }
  67. }
  68. static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
  69. {
  70. int i, res;
  71. struct tb_regs_hop hop = { };
  72. for (i = first_hop; i < path->path_length; i++) {
  73. res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
  74. 2 * path->hops[i].in_hop_index, 2);
  75. if (res)
  76. tb_port_warn(path->hops[i].in_port,
  77. "hop deactivation failed for hop %d, index %d\n",
  78. i, path->hops[i].in_hop_index);
  79. }
  80. }
  81. void tb_path_deactivate(struct tb_path *path)
  82. {
  83. if (!path->activated) {
  84. tb_WARN(path->tb, "trying to deactivate an inactive path\n");
  85. return;
  86. }
  87. tb_info(path->tb,
  88. "deactivating path from %llx:%x to %llx:%x\n",
  89. tb_route(path->hops[0].in_port->sw),
  90. path->hops[0].in_port->port,
  91. tb_route(path->hops[path->path_length - 1].out_port->sw),
  92. path->hops[path->path_length - 1].out_port->port);
  93. __tb_path_deactivate_hops(path, 0);
  94. __tb_path_deallocate_nfc(path, 0);
  95. path->activated = false;
  96. }
  97. /**
  98. * tb_path_activate() - activate a path
  99. *
  100. * Activate a path starting with the last hop and iterating backwards. The
  101. * caller must fill path->hops before calling tb_path_activate().
  102. *
  103. * Return: Returns 0 on success or an error code on failure.
  104. */
  105. int tb_path_activate(struct tb_path *path)
  106. {
  107. int i, res;
  108. enum tb_path_port out_mask, in_mask;
  109. if (path->activated) {
  110. tb_WARN(path->tb, "trying to activate already activated path\n");
  111. return -EINVAL;
  112. }
  113. tb_info(path->tb,
  114. "activating path from %llx:%x to %llx:%x\n",
  115. tb_route(path->hops[0].in_port->sw),
  116. path->hops[0].in_port->port,
  117. tb_route(path->hops[path->path_length - 1].out_port->sw),
  118. path->hops[path->path_length - 1].out_port->port);
  119. /* Clear counters. */
  120. for (i = path->path_length - 1; i >= 0; i--) {
  121. if (path->hops[i].in_counter_index == -1)
  122. continue;
  123. res = tb_port_clear_counter(path->hops[i].in_port,
  124. path->hops[i].in_counter_index);
  125. if (res)
  126. goto err;
  127. }
  128. /* Add non flow controlled credits. */
  129. for (i = path->path_length - 1; i >= 0; i--) {
  130. res = tb_port_add_nfc_credits(path->hops[i].in_port,
  131. path->nfc_credits);
  132. if (res) {
  133. __tb_path_deallocate_nfc(path, i);
  134. goto err;
  135. }
  136. }
  137. /* Activate hops. */
  138. for (i = path->path_length - 1; i >= 0; i--) {
  139. struct tb_regs_hop hop = { 0 };
  140. /*
  141. * We do (currently) not tear down paths setup by the firmeware.
  142. * If a firmware device is unplugged and plugged in again then
  143. * it can happen that we reuse some of the hops from the (now
  144. * defunct) firmeware path. This causes the hotplug operation to
  145. * fail (the pci device does not show up). Clearing the hop
  146. * before overwriting it fixes the problem.
  147. *
  148. * Should be removed once we discover and tear down firmeware
  149. * paths.
  150. */
  151. res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
  152. 2 * path->hops[i].in_hop_index, 2);
  153. if (res) {
  154. __tb_path_deactivate_hops(path, i);
  155. __tb_path_deallocate_nfc(path, 0);
  156. goto err;
  157. }
  158. /* dword 0 */
  159. hop.next_hop = path->hops[i].next_hop_index;
  160. hop.out_port = path->hops[i].out_port->port;
  161. /* TODO: figure out why these are good values */
  162. hop.initial_credits = (i == path->path_length - 1) ? 16 : 7;
  163. hop.unknown1 = 0;
  164. hop.enable = 1;
  165. /* dword 1 */
  166. out_mask = (i == path->path_length - 1) ?
  167. TB_PATH_DESTINATION : TB_PATH_INTERNAL;
  168. in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
  169. hop.weight = path->weight;
  170. hop.unknown2 = 0;
  171. hop.priority = path->priority;
  172. hop.drop_packages = path->drop_packages;
  173. hop.counter = path->hops[i].in_counter_index;
  174. hop.counter_enable = path->hops[i].in_counter_index != -1;
  175. hop.ingress_fc = path->ingress_fc_enable & in_mask;
  176. hop.egress_fc = path->egress_fc_enable & out_mask;
  177. hop.ingress_shared_buffer = path->ingress_shared_buffer
  178. & in_mask;
  179. hop.egress_shared_buffer = path->egress_shared_buffer
  180. & out_mask;
  181. hop.unknown3 = 0;
  182. tb_port_info(path->hops[i].in_port, "Writing hop %d, index %d",
  183. i, path->hops[i].in_hop_index);
  184. tb_dump_hop(path->hops[i].in_port, &hop);
  185. res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
  186. 2 * path->hops[i].in_hop_index, 2);
  187. if (res) {
  188. __tb_path_deactivate_hops(path, i);
  189. __tb_path_deallocate_nfc(path, 0);
  190. goto err;
  191. }
  192. }
  193. path->activated = true;
  194. tb_info(path->tb, "path activation complete\n");
  195. return 0;
  196. err:
  197. tb_WARN(path->tb, "path activation failed\n");
  198. return res;
  199. }
  200. /**
  201. * tb_path_is_invalid() - check whether any ports on the path are invalid
  202. *
  203. * Return: Returns true if the path is invalid, false otherwise.
  204. */
  205. bool tb_path_is_invalid(struct tb_path *path)
  206. {
  207. int i = 0;
  208. for (i = 0; i < path->path_length; i++) {
  209. if (path->hops[i].in_port->sw->is_unplugged)
  210. return true;
  211. if (path->hops[i].out_port->sw->is_unplugged)
  212. return true;
  213. }
  214. return false;
  215. }