path.c 6.7 KB

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