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 9 #ifndef TB_TUNNEL_H_ 10 #define TB_TUNNEL_H_ 11 12 #include "tb.h" 13 14 enum tb_tunnel_type { 15 TB_TUNNEL_PCI, 16 TB_TUNNEL_DP, 17 TB_TUNNEL_DMA, 18 TB_TUNNEL_USB3, 19 }; 20 21 /** 22 * enum tb_tunnel_state - State of a tunnel 23 * @TB_TUNNEL_INACTIVE: tb_tunnel_activate() is not called for the tunnel 24 * @TB_TUNNEL_ACTIVATING: tb_tunnel_activate() returned successfully for the tunnel 25 * @TB_TUNNEL_ACTIVE: The tunnel is fully active 26 */ 27 enum tb_tunnel_state { 28 TB_TUNNEL_INACTIVE, 29 TB_TUNNEL_ACTIVATING, 30 TB_TUNNEL_ACTIVE, 31 }; 32 33 /** 34 * struct tb_tunnel - Tunnel between two ports 35 * @kref: Reference count 36 * @tb: Pointer to the domain 37 * @src_port: Source port of the tunnel 38 * @dst_port: Destination port of the tunnel. For discovered incomplete 39 * tunnels may be %NULL or null adapter port instead. 40 * @npaths: Number of paths in @paths 41 * @pre_activate: Optional tunnel specific initialization called before 42 * activation. Can touch hardware. 43 * @activate: Optional tunnel specific activation/deactivation 44 * @post_deactivate: Optional tunnel specific de-initialization called 45 * after deactivation. Can touch hardware. 46 * @destroy: Optional tunnel specific callback called when the tunnel 47 * memory is being released. Should not touch hardware. 48 * @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel 49 * @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel 50 * @alloc_bandwidth: Change tunnel bandwidth allocation 51 * @consumed_bandwidth: Return how much bandwidth the tunnel consumes 52 * @release_unused_bandwidth: Release all unused bandwidth 53 * @reclaim_available_bandwidth: Reclaim back available bandwidth 54 * @list: Tunnels are linked using this field 55 * @type: Type of the tunnel 56 * @state: Current state of the tunnel 57 * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel. 58 * Only set if the bandwidth needs to be limited. 59 * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel. 60 * Only set if the bandwidth needs to be limited. 61 * @allocated_up: Allocated upstream bandwidth (only for USB3) 62 * @allocated_down: Allocated downstream bandwidth (only for USB3) 63 * @bw_mode: DP bandwidth allocation mode registers can be used to 64 * determine consumed and allocated bandwidth 65 * @dprx_started: DPRX negotiation was started (tb_dp_dprx_start() was called for it) 66 * @dprx_canceled: Was DPRX capabilities read poll canceled 67 * @dprx_timeout: If set DPRX capabilities read poll work will timeout after this passes 68 * @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read 69 * @callback: Callback called when DP tunnel is fully activated 70 * @callback_data: Data for @callback 71 * @paths: All paths required by the tunnel 72 */ 73 struct tb_tunnel { 74 struct kref kref; 75 struct tb *tb; 76 struct tb_port *src_port; 77 struct tb_port *dst_port; 78 size_t npaths; 79 int (*pre_activate)(struct tb_tunnel *tunnel); 80 int (*activate)(struct tb_tunnel *tunnel, bool activate); 81 void (*post_deactivate)(struct tb_tunnel *tunnel); 82 void (*destroy)(struct tb_tunnel *tunnel); 83 int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up, 84 int *max_down); 85 int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up, 86 int *allocated_down); 87 int (*alloc_bandwidth)(struct tb_tunnel *tunnel, int *alloc_up, 88 int *alloc_down); 89 int (*consumed_bandwidth)(struct tb_tunnel *tunnel, int *consumed_up, 90 int *consumed_down); 91 int (*release_unused_bandwidth)(struct tb_tunnel *tunnel); 92 void (*reclaim_available_bandwidth)(struct tb_tunnel *tunnel, 93 int *available_up, 94 int *available_down); 95 struct list_head list; 96 enum tb_tunnel_type type; 97 enum tb_tunnel_state state; 98 int max_up; 99 int max_down; 100 int allocated_up; 101 int allocated_down; 102 bool bw_mode; 103 bool dprx_started; 104 bool dprx_canceled; 105 ktime_t dprx_timeout; 106 struct delayed_work dprx_work; 107 void (*callback)(struct tb_tunnel *tunnel, void *data); 108 void *callback_data; 109 110 struct tb_path *paths[] __counted_by(npaths); 111 }; 112 113 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down, 114 bool alloc_hopid); 115 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up, 116 struct tb_port *down); 117 bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up, 118 int *reserved_down); 119 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in, 120 bool alloc_hopid, 121 void (*callback)(struct tb_tunnel *, void *), 122 void *callback_data); 123 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in, 124 struct tb_port *out, int link_nr, 125 int max_up, int max_down, 126 void (*callback)(struct tb_tunnel *, void *), 127 void *callback_data); 128 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi, 129 struct tb_port *dst, int transmit_path, 130 int transmit_ring, int receive_path, 131 int receive_ring); 132 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, 133 int transmit_ring, int receive_path, int receive_ring); 134 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down, 135 bool alloc_hopid); 136 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up, 137 struct tb_port *down, int max_up, 138 int max_down); 139 140 void tb_tunnel_put(struct tb_tunnel *tunnel); 141 int tb_tunnel_activate(struct tb_tunnel *tunnel); 142 void tb_tunnel_deactivate(struct tb_tunnel *tunnel); 143 144 /** 145 * tb_tunnel_is_active() - Is tunnel fully activated 146 * @tunnel: Tunnel to check 147 * 148 * Return: %true if @tunnel is fully activated. 149 * 150 * Note for DP tunnels this returns %true only once the DPRX capabilities 151 * read has been issued successfully. For other tunnels, this function 152 * returns %true pretty much once tb_tunnel_activate() returns successfully. 153 */ 154 static inline bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) 155 { 156 return tunnel->state == TB_TUNNEL_ACTIVE; 157 } 158 159 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel); 160 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, 161 const struct tb_port *port); 162 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, 163 int *max_down); 164 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, 165 int *allocated_down); 166 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, 167 int *alloc_down); 168 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, 169 int *consumed_down); 170 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel); 171 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, 172 int *available_up, 173 int *available_down); 174 175 static inline bool tb_tunnel_is_pci(const struct tb_tunnel *tunnel) 176 { 177 return tunnel->type == TB_TUNNEL_PCI; 178 } 179 180 static inline bool tb_tunnel_is_dp(const struct tb_tunnel *tunnel) 181 { 182 return tunnel->type == TB_TUNNEL_DP; 183 } 184 185 static inline bool tb_tunnel_is_dma(const struct tb_tunnel *tunnel) 186 { 187 return tunnel->type == TB_TUNNEL_DMA; 188 } 189 190 static inline bool tb_tunnel_is_usb3(const struct tb_tunnel *tunnel) 191 { 192 return tunnel->type == TB_TUNNEL_USB3; 193 } 194 195 static inline bool tb_tunnel_direction_downstream(const struct tb_tunnel *tunnel) 196 { 197 return tb_port_path_direction_downstream(tunnel->src_port, 198 tunnel->dst_port); 199 } 200 201 /** 202 * enum tb_tunnel_event - Tunnel related events 203 * @TB_TUNNEL_ACTIVATED: A tunnel was activated 204 * @TB_TUNNEL_CHANGED: There is a tunneling change in the domain. Includes 205 * full %TUNNEL_DETAILS if the tunnel in question is known 206 * (ICM does not provide that information). 207 * @TB_TUNNEL_DEACTIVATED: A tunnel was torn down 208 * @TB_TUNNEL_LOW_BANDWIDTH: Tunnel bandwidth is not optimal 209 * @TB_TUNNEL_NO_BANDWIDTH: There is not enough bandwidth for a tunnel 210 */ 211 enum tb_tunnel_event { 212 TB_TUNNEL_ACTIVATED, 213 TB_TUNNEL_CHANGED, 214 TB_TUNNEL_DEACTIVATED, 215 TB_TUNNEL_LOW_BANDWIDTH, 216 TB_TUNNEL_NO_BANDWIDTH, 217 }; 218 219 void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event, 220 enum tb_tunnel_type type, 221 const struct tb_port *src_port, 222 const struct tb_port *dst_port); 223 224 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel); 225 226 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \ 227 do { \ 228 struct tb_tunnel *__tunnel = (tunnel); \ 229 level(__tunnel->tb, "%llx:%u <-> %llx:%u (%s): " fmt, \ 230 tb_route(__tunnel->src_port->sw), \ 231 __tunnel->src_port->port, \ 232 tb_route(__tunnel->dst_port->sw), \ 233 __tunnel->dst_port->port, \ 234 tb_tunnel_type_name(__tunnel), \ 235 ## arg); \ 236 } while (0) 237 238 #define tb_tunnel_WARN(tunnel, fmt, arg...) \ 239 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg) 240 #define tb_tunnel_warn(tunnel, fmt, arg...) \ 241 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg) 242 #define tb_tunnel_info(tunnel, fmt, arg...) \ 243 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg) 244 #define tb_tunnel_dbg(tunnel, fmt, arg...) \ 245 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg) 246 247 #endif 248