tunnel.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Thunderbolt driver - Tunneling support
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2019, Intel Corporation
  7. */
  8. #ifndef TB_TUNNEL_H_
  9. #define TB_TUNNEL_H_
  10. #include "tb.h"
  11. enum tb_tunnel_type {
  12. TB_TUNNEL_PCI,
  13. TB_TUNNEL_DP,
  14. TB_TUNNEL_DMA,
  15. TB_TUNNEL_USB3,
  16. };
  17. /**
  18. * struct tb_tunnel - Tunnel between two ports
  19. * @tb: Pointer to the domain
  20. * @src_port: Source port of the tunnel
  21. * @dst_port: Destination port of the tunnel. For discovered incomplete
  22. * tunnels may be %NULL or null adapter port instead.
  23. * @paths: All paths required by the tunnel
  24. * @npaths: Number of paths in @paths
  25. * @init: Optional tunnel specific initialization
  26. * @deinit: Optional tunnel specific de-initialization
  27. * @activate: Optional tunnel specific activation/deactivation
  28. * @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel
  29. * @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel
  30. * @alloc_bandwidth: Change tunnel bandwidth allocation
  31. * @consumed_bandwidth: Return how much bandwidth the tunnel consumes
  32. * @release_unused_bandwidth: Release all unused bandwidth
  33. * @reclaim_available_bandwidth: Reclaim back available bandwidth
  34. * @list: Tunnels are linked using this field
  35. * @type: Type of the tunnel
  36. * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel.
  37. * Only set if the bandwidth needs to be limited.
  38. * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel.
  39. * Only set if the bandwidth needs to be limited.
  40. * @allocated_up: Allocated upstream bandwidth (only for USB3)
  41. * @allocated_down: Allocated downstream bandwidth (only for USB3)
  42. * @bw_mode: DP bandwidth allocation mode registers can be used to
  43. * determine consumed and allocated bandwidth
  44. */
  45. struct tb_tunnel {
  46. struct tb *tb;
  47. struct tb_port *src_port;
  48. struct tb_port *dst_port;
  49. struct tb_path **paths;
  50. size_t npaths;
  51. int (*init)(struct tb_tunnel *tunnel);
  52. void (*deinit)(struct tb_tunnel *tunnel);
  53. int (*activate)(struct tb_tunnel *tunnel, bool activate);
  54. int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up,
  55. int *max_down);
  56. int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up,
  57. int *allocated_down);
  58. int (*alloc_bandwidth)(struct tb_tunnel *tunnel, int *alloc_up,
  59. int *alloc_down);
  60. int (*consumed_bandwidth)(struct tb_tunnel *tunnel, int *consumed_up,
  61. int *consumed_down);
  62. int (*release_unused_bandwidth)(struct tb_tunnel *tunnel);
  63. void (*reclaim_available_bandwidth)(struct tb_tunnel *tunnel,
  64. int *available_up,
  65. int *available_down);
  66. struct list_head list;
  67. enum tb_tunnel_type type;
  68. int max_up;
  69. int max_down;
  70. int allocated_up;
  71. int allocated_down;
  72. bool bw_mode;
  73. };
  74. struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
  75. bool alloc_hopid);
  76. struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
  77. struct tb_port *down);
  78. bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
  79. int *reserved_down);
  80. struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
  81. bool alloc_hopid);
  82. struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
  83. struct tb_port *out, int link_nr,
  84. int max_up, int max_down);
  85. struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
  86. struct tb_port *dst, int transmit_path,
  87. int transmit_ring, int receive_path,
  88. int receive_ring);
  89. bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
  90. int transmit_ring, int receive_path, int receive_ring);
  91. struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
  92. bool alloc_hopid);
  93. struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
  94. struct tb_port *down, int max_up,
  95. int max_down);
  96. void tb_tunnel_free(struct tb_tunnel *tunnel);
  97. int tb_tunnel_activate(struct tb_tunnel *tunnel);
  98. int tb_tunnel_restart(struct tb_tunnel *tunnel);
  99. void tb_tunnel_deactivate(struct tb_tunnel *tunnel);
  100. bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel);
  101. bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
  102. const struct tb_port *port);
  103. int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
  104. int *max_down);
  105. int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
  106. int *allocated_down);
  107. int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
  108. int *alloc_down);
  109. int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
  110. int *consumed_down);
  111. int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel);
  112. void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
  113. int *available_up,
  114. int *available_down);
  115. static inline bool tb_tunnel_is_pci(const struct tb_tunnel *tunnel)
  116. {
  117. return tunnel->type == TB_TUNNEL_PCI;
  118. }
  119. static inline bool tb_tunnel_is_dp(const struct tb_tunnel *tunnel)
  120. {
  121. return tunnel->type == TB_TUNNEL_DP;
  122. }
  123. static inline bool tb_tunnel_is_dma(const struct tb_tunnel *tunnel)
  124. {
  125. return tunnel->type == TB_TUNNEL_DMA;
  126. }
  127. static inline bool tb_tunnel_is_usb3(const struct tb_tunnel *tunnel)
  128. {
  129. return tunnel->type == TB_TUNNEL_USB3;
  130. }
  131. static inline bool tb_tunnel_direction_downstream(const struct tb_tunnel *tunnel)
  132. {
  133. return tb_port_path_direction_downstream(tunnel->src_port,
  134. tunnel->dst_port);
  135. }
  136. const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel);
  137. #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
  138. do { \
  139. struct tb_tunnel *__tunnel = (tunnel); \
  140. level(__tunnel->tb, "%llx:%u <-> %llx:%u (%s): " fmt, \
  141. tb_route(__tunnel->src_port->sw), \
  142. __tunnel->src_port->port, \
  143. tb_route(__tunnel->dst_port->sw), \
  144. __tunnel->dst_port->port, \
  145. tb_tunnel_type_name(__tunnel), \
  146. ## arg); \
  147. } while (0)
  148. #define tb_tunnel_WARN(tunnel, fmt, arg...) \
  149. __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
  150. #define tb_tunnel_warn(tunnel, fmt, arg...) \
  151. __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
  152. #define tb_tunnel_info(tunnel, fmt, arg...) \
  153. __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
  154. #define tb_tunnel_dbg(tunnel, fmt, arg...) \
  155. __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
  156. #endif