Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 3 | * Thunderbolt driver - Tunneling support |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 6 | * Copyright (C) 2019, Intel Corporation |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Mika Westerberg | de718ac | 2019-02-15 18:18:47 +0200 | [diff] [blame] | 9 | #include <linux/delay.h> |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/list.h> |
| 12 | |
Mika Westerberg | 1752b9f | 2017-02-19 10:58:35 +0200 | [diff] [blame] | 13 | #include "tunnel.h" |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 14 | #include "tb.h" |
| 15 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 16 | /* PCIe adapters use always HopID of 8 for both directions */ |
| 17 | #define TB_PCI_HOPID 8 |
| 18 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 19 | #define TB_PCI_PATH_DOWN 0 |
| 20 | #define TB_PCI_PATH_UP 1 |
| 21 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 22 | /* USB3 adapters use always HopID of 8 for both directions */ |
| 23 | #define TB_USB3_HOPID 8 |
| 24 | |
| 25 | #define TB_USB3_PATH_DOWN 0 |
| 26 | #define TB_USB3_PATH_UP 1 |
| 27 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 28 | /* DP adapters use HopID 8 for AUX and 9 for Video */ |
| 29 | #define TB_DP_AUX_TX_HOPID 8 |
| 30 | #define TB_DP_AUX_RX_HOPID 8 |
| 31 | #define TB_DP_VIDEO_HOPID 9 |
| 32 | |
| 33 | #define TB_DP_VIDEO_PATH_OUT 0 |
| 34 | #define TB_DP_AUX_PATH_OUT 1 |
| 35 | #define TB_DP_AUX_PATH_IN 2 |
| 36 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 37 | static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" }; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 38 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 39 | #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \ |
| 40 | do { \ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 41 | struct tb_tunnel *__tunnel = (tunnel); \ |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 42 | level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt, \ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 43 | tb_route(__tunnel->src_port->sw), \ |
| 44 | __tunnel->src_port->port, \ |
| 45 | tb_route(__tunnel->dst_port->sw), \ |
| 46 | __tunnel->dst_port->port, \ |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 47 | tb_tunnel_names[__tunnel->type], \ |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 48 | ## arg); \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define tb_tunnel_WARN(tunnel, fmt, arg...) \ |
| 52 | __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg) |
| 53 | #define tb_tunnel_warn(tunnel, fmt, arg...) \ |
| 54 | __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg) |
| 55 | #define tb_tunnel_info(tunnel, fmt, arg...) \ |
| 56 | __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg) |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 57 | #define tb_tunnel_dbg(tunnel, fmt, arg...) \ |
| 58 | __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 59 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 60 | static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths, |
| 61 | enum tb_tunnel_type type) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 62 | { |
| 63 | struct tb_tunnel *tunnel; |
| 64 | |
| 65 | tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); |
| 66 | if (!tunnel) |
| 67 | return NULL; |
| 68 | |
| 69 | tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); |
| 70 | if (!tunnel->paths) { |
| 71 | tb_tunnel_free(tunnel); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | INIT_LIST_HEAD(&tunnel->list); |
| 76 | tunnel->tb = tb; |
| 77 | tunnel->npaths = npaths; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 78 | tunnel->type = type; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 79 | |
| 80 | return tunnel; |
| 81 | } |
| 82 | |
| 83 | static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) |
| 84 | { |
| 85 | int res; |
| 86 | |
| 87 | res = tb_pci_port_enable(tunnel->src_port, activate); |
| 88 | if (res) |
| 89 | return res; |
| 90 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 91 | if (tb_port_is_pcie_up(tunnel->dst_port)) |
| 92 | return tb_pci_port_enable(tunnel->dst_port, activate); |
| 93 | |
| 94 | return 0; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 97 | static int tb_initial_credits(const struct tb_switch *sw) |
| 98 | { |
| 99 | /* If the path is complete sw is not NULL */ |
| 100 | if (sw) { |
| 101 | /* More credits for faster link */ |
| 102 | switch (sw->link_speed * sw->link_width) { |
| 103 | case 40: |
| 104 | return 32; |
| 105 | case 20: |
| 106 | return 24; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return 16; |
| 111 | } |
| 112 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 113 | static void tb_pci_init_path(struct tb_path *path) |
| 114 | { |
| 115 | path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; |
| 116 | path->egress_shared_buffer = TB_PATH_NONE; |
| 117 | path->ingress_fc_enable = TB_PATH_ALL; |
| 118 | path->ingress_shared_buffer = TB_PATH_NONE; |
| 119 | path->priority = 3; |
| 120 | path->weight = 1; |
| 121 | path->drop_packages = 0; |
| 122 | path->nfc_credits = 0; |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 123 | path->hops[0].initial_credits = 7; |
Mika Westerberg | 75ab3f0 | 2020-05-08 11:55:03 +0300 | [diff] [blame] | 124 | if (path->path_length > 1) |
| 125 | path->hops[1].initial_credits = |
| 126 | tb_initial_credits(path->hops[1].in_port->sw); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * tb_tunnel_discover_pci() - Discover existing PCIe tunnels |
| 131 | * @tb: Pointer to the domain structure |
| 132 | * @down: PCIe downstream adapter |
| 133 | * |
| 134 | * If @down adapter is active, follows the tunnel to the PCIe upstream |
| 135 | * adapter and back. Returns the discovered tunnel or %NULL if there was |
| 136 | * no tunnel. |
| 137 | */ |
| 138 | struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down) |
| 139 | { |
| 140 | struct tb_tunnel *tunnel; |
| 141 | struct tb_path *path; |
| 142 | |
| 143 | if (!tb_pci_port_is_enabled(down)) |
| 144 | return NULL; |
| 145 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 146 | tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 147 | if (!tunnel) |
| 148 | return NULL; |
| 149 | |
| 150 | tunnel->activate = tb_pci_activate; |
| 151 | tunnel->src_port = down; |
| 152 | |
| 153 | /* |
| 154 | * Discover both paths even if they are not complete. We will |
| 155 | * clean them up by calling tb_tunnel_deactivate() below in that |
| 156 | * case. |
| 157 | */ |
| 158 | path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1, |
| 159 | &tunnel->dst_port, "PCIe Up"); |
| 160 | if (!path) { |
| 161 | /* Just disable the downstream port */ |
| 162 | tb_pci_port_enable(down, false); |
| 163 | goto err_free; |
| 164 | } |
| 165 | tunnel->paths[TB_PCI_PATH_UP] = path; |
| 166 | tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]); |
| 167 | |
| 168 | path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL, |
| 169 | "PCIe Down"); |
| 170 | if (!path) |
| 171 | goto err_deactivate; |
| 172 | tunnel->paths[TB_PCI_PATH_DOWN] = path; |
| 173 | tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]); |
| 174 | |
| 175 | /* Validate that the tunnel is complete */ |
| 176 | if (!tb_port_is_pcie_up(tunnel->dst_port)) { |
| 177 | tb_port_warn(tunnel->dst_port, |
| 178 | "path does not end on a PCIe adapter, cleaning up\n"); |
| 179 | goto err_deactivate; |
| 180 | } |
| 181 | |
| 182 | if (down != tunnel->src_port) { |
| 183 | tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); |
| 184 | goto err_deactivate; |
| 185 | } |
| 186 | |
| 187 | if (!tb_pci_port_is_enabled(tunnel->dst_port)) { |
| 188 | tb_tunnel_warn(tunnel, |
| 189 | "tunnel is not fully activated, cleaning up\n"); |
| 190 | goto err_deactivate; |
| 191 | } |
| 192 | |
| 193 | tb_tunnel_dbg(tunnel, "discovered\n"); |
| 194 | return tunnel; |
| 195 | |
| 196 | err_deactivate: |
| 197 | tb_tunnel_deactivate(tunnel); |
| 198 | err_free: |
| 199 | tb_tunnel_free(tunnel); |
| 200 | |
| 201 | return NULL; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 205 | * tb_tunnel_alloc_pci() - allocate a pci tunnel |
| 206 | * @tb: Pointer to the domain structure |
| 207 | * @up: PCIe upstream adapter port |
| 208 | * @down: PCIe downstream adapter port |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 209 | * |
| 210 | * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and |
| 211 | * TB_TYPE_PCIE_DOWN. |
| 212 | * |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 213 | * Return: Returns a tb_tunnel on success or NULL on failure. |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 214 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 215 | struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up, |
| 216 | struct tb_port *down) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 217 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 218 | struct tb_tunnel *tunnel; |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 219 | struct tb_path *path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 220 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 221 | tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 222 | if (!tunnel) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 223 | return NULL; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 224 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 225 | tunnel->activate = tb_pci_activate; |
| 226 | tunnel->src_port = down; |
| 227 | tunnel->dst_port = up; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 228 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 229 | path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0, |
| 230 | "PCIe Down"); |
| 231 | if (!path) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 232 | tb_tunnel_free(tunnel); |
| 233 | return NULL; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 234 | } |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 235 | tb_pci_init_path(path); |
Mika Westerberg | ce19f91 | 2019-06-11 19:31:26 +0300 | [diff] [blame] | 236 | tunnel->paths[TB_PCI_PATH_DOWN] = path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 237 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 238 | path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0, |
| 239 | "PCIe Up"); |
| 240 | if (!path) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 241 | tb_tunnel_free(tunnel); |
| 242 | return NULL; |
| 243 | } |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 244 | tb_pci_init_path(path); |
Mika Westerberg | ce19f91 | 2019-06-11 19:31:26 +0300 | [diff] [blame] | 245 | tunnel->paths[TB_PCI_PATH_UP] = path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 246 | |
| 247 | return tunnel; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 250 | static bool tb_dp_is_usb4(const struct tb_switch *sw) |
| 251 | { |
| 252 | /* Titan Ridge DP adapters need the same treatment as USB4 */ |
| 253 | return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw); |
| 254 | } |
| 255 | |
Mika Westerberg | de718ac | 2019-02-15 18:18:47 +0200 | [diff] [blame] | 256 | static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out) |
| 257 | { |
| 258 | int timeout = 10; |
| 259 | u32 val; |
| 260 | int ret; |
| 261 | |
| 262 | /* Both ends need to support this */ |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 263 | if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw)) |
Mika Westerberg | de718ac | 2019-02-15 18:18:47 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | ret = tb_port_read(out, &val, TB_CFG_PORT, |
| 267 | out->cap_adap + DP_STATUS_CTRL, 1); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
| 271 | val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS; |
| 272 | |
| 273 | ret = tb_port_write(out, &val, TB_CFG_PORT, |
| 274 | out->cap_adap + DP_STATUS_CTRL, 1); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
| 278 | do { |
| 279 | ret = tb_port_read(out, &val, TB_CFG_PORT, |
| 280 | out->cap_adap + DP_STATUS_CTRL, 1); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | if (!(val & DP_STATUS_CTRL_CMHS)) |
| 284 | return 0; |
| 285 | usleep_range(10, 100); |
| 286 | } while (timeout--); |
| 287 | |
| 288 | return -ETIMEDOUT; |
| 289 | } |
| 290 | |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 291 | static inline u32 tb_dp_cap_get_rate(u32 val) |
| 292 | { |
| 293 | u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT; |
| 294 | |
| 295 | switch (rate) { |
| 296 | case DP_COMMON_CAP_RATE_RBR: |
| 297 | return 1620; |
| 298 | case DP_COMMON_CAP_RATE_HBR: |
| 299 | return 2700; |
| 300 | case DP_COMMON_CAP_RATE_HBR2: |
| 301 | return 5400; |
| 302 | case DP_COMMON_CAP_RATE_HBR3: |
| 303 | return 8100; |
| 304 | default: |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate) |
| 310 | { |
| 311 | val &= ~DP_COMMON_CAP_RATE_MASK; |
| 312 | switch (rate) { |
| 313 | default: |
| 314 | WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 315 | fallthrough; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 316 | case 1620: |
| 317 | val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT; |
| 318 | break; |
| 319 | case 2700: |
| 320 | val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT; |
| 321 | break; |
| 322 | case 5400: |
| 323 | val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT; |
| 324 | break; |
| 325 | case 8100: |
| 326 | val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT; |
| 327 | break; |
| 328 | } |
| 329 | return val; |
| 330 | } |
| 331 | |
| 332 | static inline u32 tb_dp_cap_get_lanes(u32 val) |
| 333 | { |
| 334 | u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT; |
| 335 | |
| 336 | switch (lanes) { |
| 337 | case DP_COMMON_CAP_1_LANE: |
| 338 | return 1; |
| 339 | case DP_COMMON_CAP_2_LANES: |
| 340 | return 2; |
| 341 | case DP_COMMON_CAP_4_LANES: |
| 342 | return 4; |
| 343 | default: |
| 344 | return 0; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes) |
| 349 | { |
| 350 | val &= ~DP_COMMON_CAP_LANES_MASK; |
| 351 | switch (lanes) { |
| 352 | default: |
| 353 | WARN(1, "invalid number of lanes %u passed, defaulting to 1\n", |
| 354 | lanes); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 355 | fallthrough; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 356 | case 1: |
| 357 | val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT; |
| 358 | break; |
| 359 | case 2: |
| 360 | val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT; |
| 361 | break; |
| 362 | case 4: |
| 363 | val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT; |
| 364 | break; |
| 365 | } |
| 366 | return val; |
| 367 | } |
| 368 | |
| 369 | static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes) |
| 370 | { |
| 371 | /* Tunneling removes the DP 8b/10b encoding */ |
| 372 | return rate * lanes * 8 / 10; |
| 373 | } |
| 374 | |
| 375 | static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes, |
| 376 | u32 out_rate, u32 out_lanes, u32 *new_rate, |
| 377 | u32 *new_lanes) |
| 378 | { |
| 379 | static const u32 dp_bw[][2] = { |
| 380 | /* Mb/s, lanes */ |
| 381 | { 8100, 4 }, /* 25920 Mb/s */ |
| 382 | { 5400, 4 }, /* 17280 Mb/s */ |
| 383 | { 8100, 2 }, /* 12960 Mb/s */ |
| 384 | { 2700, 4 }, /* 8640 Mb/s */ |
| 385 | { 5400, 2 }, /* 8640 Mb/s */ |
| 386 | { 8100, 1 }, /* 6480 Mb/s */ |
| 387 | { 1620, 4 }, /* 5184 Mb/s */ |
| 388 | { 5400, 1 }, /* 4320 Mb/s */ |
| 389 | { 2700, 2 }, /* 4320 Mb/s */ |
| 390 | { 1620, 2 }, /* 2592 Mb/s */ |
| 391 | { 2700, 1 }, /* 2160 Mb/s */ |
| 392 | { 1620, 1 }, /* 1296 Mb/s */ |
| 393 | }; |
| 394 | unsigned int i; |
| 395 | |
| 396 | /* |
| 397 | * Find a combination that can fit into max_bw and does not |
| 398 | * exceed the maximum rate and lanes supported by the DP OUT and |
| 399 | * DP IN adapters. |
| 400 | */ |
| 401 | for (i = 0; i < ARRAY_SIZE(dp_bw); i++) { |
| 402 | if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes) |
| 403 | continue; |
| 404 | |
| 405 | if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes) |
| 406 | continue; |
| 407 | |
| 408 | if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) { |
| 409 | *new_rate = dp_bw[i][0]; |
| 410 | *new_lanes = dp_bw[i][1]; |
| 411 | return 0; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return -ENOSR; |
| 416 | } |
| 417 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 418 | static int tb_dp_xchg_caps(struct tb_tunnel *tunnel) |
| 419 | { |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 420 | u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 421 | struct tb_port *out = tunnel->dst_port; |
| 422 | struct tb_port *in = tunnel->src_port; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 423 | int ret, max_bw; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for |
| 427 | * newer generation hardware. |
| 428 | */ |
| 429 | if (in->sw->generation < 2 || out->sw->generation < 2) |
| 430 | return 0; |
| 431 | |
Mika Westerberg | de718ac | 2019-02-15 18:18:47 +0200 | [diff] [blame] | 432 | /* |
| 433 | * Perform connection manager handshake between IN and OUT ports |
| 434 | * before capabilities exchange can take place. |
| 435 | */ |
| 436 | ret = tb_dp_cm_handshake(in, out); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 440 | /* Read both DP_LOCAL_CAP registers */ |
| 441 | ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT, |
Mika Westerberg | 9817638 | 2019-09-06 11:32:15 +0300 | [diff] [blame] | 442 | in->cap_adap + DP_LOCAL_CAP, 1); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 443 | if (ret) |
| 444 | return ret; |
| 445 | |
| 446 | ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT, |
Mika Westerberg | 9817638 | 2019-09-06 11:32:15 +0300 | [diff] [blame] | 447 | out->cap_adap + DP_LOCAL_CAP, 1); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 448 | if (ret) |
| 449 | return ret; |
| 450 | |
| 451 | /* Write IN local caps to OUT remote caps */ |
| 452 | ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT, |
Mika Westerberg | 9817638 | 2019-09-06 11:32:15 +0300 | [diff] [blame] | 453 | out->cap_adap + DP_REMOTE_CAP, 1); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 454 | if (ret) |
| 455 | return ret; |
| 456 | |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 457 | in_rate = tb_dp_cap_get_rate(in_dp_cap); |
| 458 | in_lanes = tb_dp_cap_get_lanes(in_dp_cap); |
| 459 | tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", |
| 460 | in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes)); |
| 461 | |
| 462 | /* |
| 463 | * If the tunnel bandwidth is limited (max_bw is set) then see |
| 464 | * if we need to reduce bandwidth to fit there. |
| 465 | */ |
| 466 | out_rate = tb_dp_cap_get_rate(out_dp_cap); |
| 467 | out_lanes = tb_dp_cap_get_lanes(out_dp_cap); |
| 468 | bw = tb_dp_bandwidth(out_rate, out_lanes); |
| 469 | tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", |
| 470 | out_rate, out_lanes, bw); |
| 471 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 472 | if (in->sw->config.depth < out->sw->config.depth) |
| 473 | max_bw = tunnel->max_down; |
| 474 | else |
| 475 | max_bw = tunnel->max_up; |
| 476 | |
| 477 | if (max_bw && bw > max_bw) { |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 478 | u32 new_rate, new_lanes, new_bw; |
| 479 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 480 | ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes, |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 481 | out_rate, out_lanes, &new_rate, |
| 482 | &new_lanes); |
| 483 | if (ret) { |
| 484 | tb_port_info(out, "not enough bandwidth for DP tunnel\n"); |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | new_bw = tb_dp_bandwidth(new_rate, new_lanes); |
| 489 | tb_port_dbg(out, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n", |
| 490 | new_rate, new_lanes, new_bw); |
| 491 | |
| 492 | /* |
| 493 | * Set new rate and number of lanes before writing it to |
| 494 | * the IN port remote caps. |
| 495 | */ |
| 496 | out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate); |
| 497 | out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes); |
| 498 | } |
| 499 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 500 | return tb_port_write(in, &out_dp_cap, TB_CFG_PORT, |
Mika Westerberg | 9817638 | 2019-09-06 11:32:15 +0300 | [diff] [blame] | 501 | in->cap_adap + DP_REMOTE_CAP, 1); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static int tb_dp_activate(struct tb_tunnel *tunnel, bool active) |
| 505 | { |
| 506 | int ret; |
| 507 | |
| 508 | if (active) { |
| 509 | struct tb_path **paths; |
| 510 | int last; |
| 511 | |
| 512 | paths = tunnel->paths; |
| 513 | last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1; |
| 514 | |
| 515 | tb_dp_port_set_hops(tunnel->src_port, |
| 516 | paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index, |
| 517 | paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index, |
| 518 | paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index); |
| 519 | |
| 520 | tb_dp_port_set_hops(tunnel->dst_port, |
| 521 | paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index, |
| 522 | paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index, |
| 523 | paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index); |
| 524 | } else { |
| 525 | tb_dp_port_hpd_clear(tunnel->src_port); |
| 526 | tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0); |
| 527 | if (tb_port_is_dpout(tunnel->dst_port)) |
| 528 | tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0); |
| 529 | } |
| 530 | |
| 531 | ret = tb_dp_port_enable(tunnel->src_port, active); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | |
| 535 | if (tb_port_is_dpout(tunnel->dst_port)) |
| 536 | return tb_dp_port_enable(tunnel->dst_port, active); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 541 | static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, |
| 542 | int *consumed_down) |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 543 | { |
| 544 | struct tb_port *in = tunnel->src_port; |
| 545 | const struct tb_switch *sw = in->sw; |
| 546 | u32 val, rate = 0, lanes = 0; |
| 547 | int ret; |
| 548 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 549 | if (tb_dp_is_usb4(sw)) { |
Mika Westerberg | acf815b | 2020-05-16 12:32:33 +0300 | [diff] [blame] | 550 | int timeout = 20; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | * Wait for DPRX done. Normally it should be already set |
| 554 | * for active tunnel. |
| 555 | */ |
| 556 | do { |
| 557 | ret = tb_port_read(in, &val, TB_CFG_PORT, |
| 558 | in->cap_adap + DP_COMMON_CAP, 1); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
| 562 | if (val & DP_COMMON_CAP_DPRX_DONE) { |
| 563 | rate = tb_dp_cap_get_rate(val); |
| 564 | lanes = tb_dp_cap_get_lanes(val); |
| 565 | break; |
| 566 | } |
| 567 | msleep(250); |
| 568 | } while (timeout--); |
| 569 | |
| 570 | if (!timeout) |
| 571 | return -ETIMEDOUT; |
| 572 | } else if (sw->generation >= 2) { |
| 573 | /* |
| 574 | * Read from the copied remote cap so that we take into |
| 575 | * account if capabilities were reduced during exchange. |
| 576 | */ |
| 577 | ret = tb_port_read(in, &val, TB_CFG_PORT, |
| 578 | in->cap_adap + DP_REMOTE_CAP, 1); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | |
| 582 | rate = tb_dp_cap_get_rate(val); |
| 583 | lanes = tb_dp_cap_get_lanes(val); |
| 584 | } else { |
| 585 | /* No bandwidth management for legacy devices */ |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 586 | *consumed_up = 0; |
| 587 | *consumed_down = 0; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 591 | if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) { |
| 592 | *consumed_up = 0; |
| 593 | *consumed_down = tb_dp_bandwidth(rate, lanes); |
| 594 | } else { |
| 595 | *consumed_up = tb_dp_bandwidth(rate, lanes); |
| 596 | *consumed_down = 0; |
| 597 | } |
| 598 | |
| 599 | return 0; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 600 | } |
| 601 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 602 | static void tb_dp_init_aux_path(struct tb_path *path) |
| 603 | { |
| 604 | int i; |
| 605 | |
| 606 | path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; |
| 607 | path->egress_shared_buffer = TB_PATH_NONE; |
| 608 | path->ingress_fc_enable = TB_PATH_ALL; |
| 609 | path->ingress_shared_buffer = TB_PATH_NONE; |
| 610 | path->priority = 2; |
| 611 | path->weight = 1; |
| 612 | |
| 613 | for (i = 0; i < path->path_length; i++) |
| 614 | path->hops[i].initial_credits = 1; |
| 615 | } |
| 616 | |
| 617 | static void tb_dp_init_video_path(struct tb_path *path, bool discover) |
| 618 | { |
| 619 | u32 nfc_credits = path->hops[0].in_port->config.nfc_credits; |
| 620 | |
| 621 | path->egress_fc_enable = TB_PATH_NONE; |
| 622 | path->egress_shared_buffer = TB_PATH_NONE; |
| 623 | path->ingress_fc_enable = TB_PATH_NONE; |
| 624 | path->ingress_shared_buffer = TB_PATH_NONE; |
| 625 | path->priority = 1; |
| 626 | path->weight = 1; |
| 627 | |
| 628 | if (discover) { |
Mika Westerberg | 8f57d47 | 2019-09-06 11:59:00 +0300 | [diff] [blame] | 629 | path->nfc_credits = nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 630 | } else { |
| 631 | u32 max_credits; |
| 632 | |
Mika Westerberg | 8f57d47 | 2019-09-06 11:59:00 +0300 | [diff] [blame] | 633 | max_credits = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> |
| 634 | ADP_CS_4_TOTAL_BUFFERS_SHIFT; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 635 | /* Leave some credits for AUX path */ |
| 636 | path->nfc_credits = min(max_credits - 2, 12U); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * tb_tunnel_discover_dp() - Discover existing Display Port tunnels |
| 642 | * @tb: Pointer to the domain structure |
| 643 | * @in: DP in adapter |
| 644 | * |
| 645 | * If @in adapter is active, follows the tunnel to the DP out adapter |
| 646 | * and back. Returns the discovered tunnel or %NULL if there was no |
| 647 | * tunnel. |
| 648 | * |
| 649 | * Return: DP tunnel or %NULL if no tunnel found. |
| 650 | */ |
| 651 | struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in) |
| 652 | { |
| 653 | struct tb_tunnel *tunnel; |
| 654 | struct tb_port *port; |
| 655 | struct tb_path *path; |
| 656 | |
| 657 | if (!tb_dp_port_is_enabled(in)) |
| 658 | return NULL; |
| 659 | |
| 660 | tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); |
| 661 | if (!tunnel) |
| 662 | return NULL; |
| 663 | |
| 664 | tunnel->init = tb_dp_xchg_caps; |
| 665 | tunnel->activate = tb_dp_activate; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 666 | tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 667 | tunnel->src_port = in; |
| 668 | |
| 669 | path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1, |
| 670 | &tunnel->dst_port, "Video"); |
| 671 | if (!path) { |
| 672 | /* Just disable the DP IN port */ |
| 673 | tb_dp_port_enable(in, false); |
| 674 | goto err_free; |
| 675 | } |
| 676 | tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path; |
| 677 | tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], true); |
| 678 | |
| 679 | path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX"); |
| 680 | if (!path) |
| 681 | goto err_deactivate; |
| 682 | tunnel->paths[TB_DP_AUX_PATH_OUT] = path; |
| 683 | tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]); |
| 684 | |
| 685 | path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID, |
| 686 | &port, "AUX RX"); |
| 687 | if (!path) |
| 688 | goto err_deactivate; |
| 689 | tunnel->paths[TB_DP_AUX_PATH_IN] = path; |
| 690 | tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]); |
| 691 | |
| 692 | /* Validate that the tunnel is complete */ |
| 693 | if (!tb_port_is_dpout(tunnel->dst_port)) { |
| 694 | tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n"); |
| 695 | goto err_deactivate; |
| 696 | } |
| 697 | |
| 698 | if (!tb_dp_port_is_enabled(tunnel->dst_port)) |
| 699 | goto err_deactivate; |
| 700 | |
| 701 | if (!tb_dp_port_hpd_is_active(tunnel->dst_port)) |
| 702 | goto err_deactivate; |
| 703 | |
| 704 | if (port != tunnel->src_port) { |
| 705 | tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); |
| 706 | goto err_deactivate; |
| 707 | } |
| 708 | |
| 709 | tb_tunnel_dbg(tunnel, "discovered\n"); |
| 710 | return tunnel; |
| 711 | |
| 712 | err_deactivate: |
| 713 | tb_tunnel_deactivate(tunnel); |
| 714 | err_free: |
| 715 | tb_tunnel_free(tunnel); |
| 716 | |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * tb_tunnel_alloc_dp() - allocate a Display Port tunnel |
| 722 | * @tb: Pointer to the domain structure |
| 723 | * @in: DP in adapter port |
| 724 | * @out: DP out adapter port |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 725 | * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0 |
| 726 | * if not limited) |
| 727 | * @max_down: Maximum available downstream bandwidth for the DP tunnel |
| 728 | * (%0 if not limited) |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 729 | * |
| 730 | * Allocates a tunnel between @in and @out that is capable of tunneling |
| 731 | * Display Port traffic. |
| 732 | * |
| 733 | * Return: Returns a tb_tunnel on success or NULL on failure. |
| 734 | */ |
| 735 | struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in, |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 736 | struct tb_port *out, int max_up, |
| 737 | int max_down) |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 738 | { |
| 739 | struct tb_tunnel *tunnel; |
| 740 | struct tb_path **paths; |
| 741 | struct tb_path *path; |
| 742 | |
| 743 | if (WARN_ON(!in->cap_adap || !out->cap_adap)) |
| 744 | return NULL; |
| 745 | |
| 746 | tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); |
| 747 | if (!tunnel) |
| 748 | return NULL; |
| 749 | |
| 750 | tunnel->init = tb_dp_xchg_caps; |
| 751 | tunnel->activate = tb_dp_activate; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 752 | tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 753 | tunnel->src_port = in; |
| 754 | tunnel->dst_port = out; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 755 | tunnel->max_up = max_up; |
| 756 | tunnel->max_down = max_down; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 757 | |
| 758 | paths = tunnel->paths; |
| 759 | |
| 760 | path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID, |
| 761 | 1, "Video"); |
| 762 | if (!path) |
| 763 | goto err_free; |
| 764 | tb_dp_init_video_path(path, false); |
| 765 | paths[TB_DP_VIDEO_PATH_OUT] = path; |
| 766 | |
| 767 | path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out, |
| 768 | TB_DP_AUX_TX_HOPID, 1, "AUX TX"); |
| 769 | if (!path) |
| 770 | goto err_free; |
| 771 | tb_dp_init_aux_path(path); |
| 772 | paths[TB_DP_AUX_PATH_OUT] = path; |
| 773 | |
| 774 | path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in, |
| 775 | TB_DP_AUX_RX_HOPID, 1, "AUX RX"); |
| 776 | if (!path) |
| 777 | goto err_free; |
| 778 | tb_dp_init_aux_path(path); |
| 779 | paths[TB_DP_AUX_PATH_IN] = path; |
| 780 | |
| 781 | return tunnel; |
| 782 | |
| 783 | err_free: |
| 784 | tb_tunnel_free(tunnel); |
| 785 | return NULL; |
| 786 | } |
| 787 | |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 788 | static u32 tb_dma_credits(struct tb_port *nhi) |
| 789 | { |
| 790 | u32 max_credits; |
| 791 | |
Mika Westerberg | 8f57d47 | 2019-09-06 11:59:00 +0300 | [diff] [blame] | 792 | max_credits = (nhi->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> |
| 793 | ADP_CS_4_TOTAL_BUFFERS_SHIFT; |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 794 | return min(max_credits, 13U); |
| 795 | } |
| 796 | |
Mika Westerberg | e587655 | 2021-01-14 20:27:58 +0300 | [diff] [blame] | 797 | static void tb_dma_init_path(struct tb_path *path, unsigned int efc, u32 credits) |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 798 | { |
| 799 | int i; |
| 800 | |
| 801 | path->egress_fc_enable = efc; |
| 802 | path->ingress_fc_enable = TB_PATH_ALL; |
| 803 | path->egress_shared_buffer = TB_PATH_NONE; |
Mika Westerberg | e587655 | 2021-01-14 20:27:58 +0300 | [diff] [blame] | 804 | path->ingress_shared_buffer = TB_PATH_NONE; |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 805 | path->priority = 5; |
| 806 | path->weight = 1; |
| 807 | path->clear_fc = true; |
| 808 | |
| 809 | for (i = 0; i < path->path_length; i++) |
| 810 | path->hops[i].initial_credits = credits; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * tb_tunnel_alloc_dma() - allocate a DMA tunnel |
| 815 | * @tb: Pointer to the domain structure |
| 816 | * @nhi: Host controller port |
| 817 | * @dst: Destination null port which the other domain is connected to |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 818 | * @transmit_path: HopID used for transmitting packets |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 819 | * @transmit_ring: NHI ring number used to send packets towards the |
| 820 | * other domain. Set to %-1 if TX path is not needed. |
Lee Jones | a27ea0d | 2021-01-27 11:25:53 +0000 | [diff] [blame] | 821 | * @receive_path: HopID used for receiving packets |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 822 | * @receive_ring: NHI ring number used to receive packets from the |
| 823 | * other domain. Set to %-1 if RX path is not needed. |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 824 | * |
| 825 | * Return: Returns a tb_tunnel on success or NULL on failure. |
| 826 | */ |
| 827 | struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi, |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 828 | struct tb_port *dst, int transmit_path, |
| 829 | int transmit_ring, int receive_path, |
| 830 | int receive_ring) |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 831 | { |
| 832 | struct tb_tunnel *tunnel; |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 833 | size_t npaths = 0, i = 0; |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 834 | struct tb_path *path; |
| 835 | u32 credits; |
| 836 | |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 837 | if (receive_ring > 0) |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 838 | npaths++; |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 839 | if (transmit_ring > 0) |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 840 | npaths++; |
| 841 | |
| 842 | if (WARN_ON(!npaths)) |
| 843 | return NULL; |
| 844 | |
| 845 | tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA); |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 846 | if (!tunnel) |
| 847 | return NULL; |
| 848 | |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 849 | tunnel->src_port = nhi; |
| 850 | tunnel->dst_port = dst; |
| 851 | |
| 852 | credits = tb_dma_credits(nhi); |
| 853 | |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 854 | if (receive_ring > 0) { |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 855 | path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0, |
| 856 | "DMA RX"); |
| 857 | if (!path) { |
| 858 | tb_tunnel_free(tunnel); |
| 859 | return NULL; |
| 860 | } |
Mika Westerberg | e587655 | 2021-01-14 20:27:58 +0300 | [diff] [blame] | 861 | tb_dma_init_path(path, TB_PATH_SOURCE | TB_PATH_INTERNAL, credits); |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 862 | tunnel->paths[i++] = path; |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 863 | } |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 864 | |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 865 | if (transmit_ring > 0) { |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 866 | path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0, |
| 867 | "DMA TX"); |
| 868 | if (!path) { |
| 869 | tb_tunnel_free(tunnel); |
| 870 | return NULL; |
| 871 | } |
Mika Westerberg | e587655 | 2021-01-14 20:27:58 +0300 | [diff] [blame] | 872 | tb_dma_init_path(path, TB_PATH_ALL, credits); |
Mika Westerberg | 5bf722d | 2020-10-07 18:17:12 +0300 | [diff] [blame] | 873 | tunnel->paths[i++] = path; |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 874 | } |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 875 | |
| 876 | return tunnel; |
| 877 | } |
| 878 | |
Mika Westerberg | 180b068 | 2021-01-08 16:25:39 +0200 | [diff] [blame^] | 879 | /** |
| 880 | * tb_tunnel_match_dma() - Match DMA tunnel |
| 881 | * @tunnel: Tunnel to match |
| 882 | * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore. |
| 883 | * @transmit_ring: NHI ring number used to send packets towards the |
| 884 | * other domain. Pass %-1 to ignore. |
| 885 | * @receive_path: HopID used for receiving packets. Pass %-1 to ignore. |
| 886 | * @receive_ring: NHI ring number used to receive packets from the |
| 887 | * other domain. Pass %-1 to ignore. |
| 888 | * |
| 889 | * This function can be used to match specific DMA tunnel, if there are |
| 890 | * multiple DMA tunnels going through the same XDomain connection. |
| 891 | * Returns true if there is match and false otherwise. |
| 892 | */ |
| 893 | bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, |
| 894 | int transmit_ring, int receive_path, int receive_ring) |
| 895 | { |
| 896 | const struct tb_path *tx_path = NULL, *rx_path = NULL; |
| 897 | int i; |
| 898 | |
| 899 | if (!receive_ring || !transmit_ring) |
| 900 | return false; |
| 901 | |
| 902 | for (i = 0; i < tunnel->npaths; i++) { |
| 903 | const struct tb_path *path = tunnel->paths[i]; |
| 904 | |
| 905 | if (!path) |
| 906 | continue; |
| 907 | |
| 908 | if (tb_port_is_nhi(path->hops[0].in_port)) |
| 909 | tx_path = path; |
| 910 | else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port)) |
| 911 | rx_path = path; |
| 912 | } |
| 913 | |
| 914 | if (transmit_ring > 0 || transmit_path > 0) { |
| 915 | if (!tx_path) |
| 916 | return false; |
| 917 | if (transmit_ring > 0 && |
| 918 | (tx_path->hops[0].in_hop_index != transmit_ring)) |
| 919 | return false; |
| 920 | if (transmit_path > 0 && |
| 921 | (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path)) |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | if (receive_ring > 0 || receive_path > 0) { |
| 926 | if (!rx_path) |
| 927 | return false; |
| 928 | if (receive_path > 0 && |
| 929 | (rx_path->hops[0].in_hop_index != receive_path)) |
| 930 | return false; |
| 931 | if (receive_ring > 0 && |
| 932 | (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring)) |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | return true; |
| 937 | } |
| 938 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 939 | static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down) |
| 940 | { |
| 941 | int ret, up_max_rate, down_max_rate; |
| 942 | |
| 943 | ret = usb4_usb3_port_max_link_rate(up); |
| 944 | if (ret < 0) |
| 945 | return ret; |
| 946 | up_max_rate = ret; |
| 947 | |
| 948 | ret = usb4_usb3_port_max_link_rate(down); |
| 949 | if (ret < 0) |
| 950 | return ret; |
| 951 | down_max_rate = ret; |
| 952 | |
| 953 | return min(up_max_rate, down_max_rate); |
| 954 | } |
| 955 | |
| 956 | static int tb_usb3_init(struct tb_tunnel *tunnel) |
| 957 | { |
| 958 | tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", |
| 959 | tunnel->allocated_up, tunnel->allocated_down); |
| 960 | |
| 961 | return usb4_usb3_port_allocate_bandwidth(tunnel->src_port, |
| 962 | &tunnel->allocated_up, |
| 963 | &tunnel->allocated_down); |
| 964 | } |
| 965 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 966 | static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate) |
| 967 | { |
| 968 | int res; |
| 969 | |
| 970 | res = tb_usb3_port_enable(tunnel->src_port, activate); |
| 971 | if (res) |
| 972 | return res; |
| 973 | |
| 974 | if (tb_port_is_usb3_up(tunnel->dst_port)) |
| 975 | return tb_usb3_port_enable(tunnel->dst_port, activate); |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 980 | static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel, |
| 981 | int *consumed_up, int *consumed_down) |
| 982 | { |
Mika Westerberg | c6da62a | 2020-02-18 16:14:42 +0200 | [diff] [blame] | 983 | int pcie_enabled = tb_acpi_may_tunnel_pcie(); |
| 984 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 985 | /* |
Mika Westerberg | c6da62a | 2020-02-18 16:14:42 +0200 | [diff] [blame] | 986 | * PCIe tunneling, if enabled, affects the USB3 bandwidth so |
| 987 | * take that it into account here. |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 988 | */ |
Mika Westerberg | c6da62a | 2020-02-18 16:14:42 +0200 | [diff] [blame] | 989 | *consumed_up = tunnel->allocated_up * (3 + pcie_enabled) / 3; |
| 990 | *consumed_down = tunnel->allocated_down * (3 + pcie_enabled) / 3; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel) |
| 995 | { |
| 996 | int ret; |
| 997 | |
| 998 | ret = usb4_usb3_port_release_bandwidth(tunnel->src_port, |
| 999 | &tunnel->allocated_up, |
| 1000 | &tunnel->allocated_down); |
| 1001 | if (ret) |
| 1002 | return ret; |
| 1003 | |
| 1004 | tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n", |
| 1005 | tunnel->allocated_up, tunnel->allocated_down); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel, |
| 1010 | int *available_up, |
| 1011 | int *available_down) |
| 1012 | { |
| 1013 | int ret, max_rate, allocate_up, allocate_down; |
| 1014 | |
| 1015 | ret = usb4_usb3_port_actual_link_rate(tunnel->src_port); |
Mika Westerberg | 813050e | 2020-06-24 21:45:03 +0300 | [diff] [blame] | 1016 | if (ret < 0) { |
| 1017 | tb_tunnel_warn(tunnel, "failed to read actual link rate\n"); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1018 | return; |
Mika Westerberg | 813050e | 2020-06-24 21:45:03 +0300 | [diff] [blame] | 1019 | } else if (!ret) { |
| 1020 | /* Use maximum link rate if the link valid is not set */ |
| 1021 | ret = usb4_usb3_port_max_link_rate(tunnel->src_port); |
| 1022 | if (ret < 0) { |
| 1023 | tb_tunnel_warn(tunnel, "failed to read maximum link rate\n"); |
| 1024 | return; |
| 1025 | } |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1026 | } |
Mika Westerberg | 813050e | 2020-06-24 21:45:03 +0300 | [diff] [blame] | 1027 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1028 | /* |
| 1029 | * 90% of the max rate can be allocated for isochronous |
| 1030 | * transfers. |
| 1031 | */ |
| 1032 | max_rate = ret * 90 / 100; |
| 1033 | |
| 1034 | /* No need to reclaim if already at maximum */ |
| 1035 | if (tunnel->allocated_up >= max_rate && |
| 1036 | tunnel->allocated_down >= max_rate) |
| 1037 | return; |
| 1038 | |
| 1039 | /* Don't go lower than what is already allocated */ |
| 1040 | allocate_up = min(max_rate, *available_up); |
| 1041 | if (allocate_up < tunnel->allocated_up) |
| 1042 | allocate_up = tunnel->allocated_up; |
| 1043 | |
| 1044 | allocate_down = min(max_rate, *available_down); |
| 1045 | if (allocate_down < tunnel->allocated_down) |
| 1046 | allocate_down = tunnel->allocated_down; |
| 1047 | |
| 1048 | /* If no changes no need to do more */ |
| 1049 | if (allocate_up == tunnel->allocated_up && |
| 1050 | allocate_down == tunnel->allocated_down) |
| 1051 | return; |
| 1052 | |
| 1053 | ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up, |
| 1054 | &allocate_down); |
| 1055 | if (ret) { |
| 1056 | tb_tunnel_info(tunnel, "failed to allocate bandwidth\n"); |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | tunnel->allocated_up = allocate_up; |
| 1061 | *available_up -= tunnel->allocated_up; |
| 1062 | |
| 1063 | tunnel->allocated_down = allocate_down; |
| 1064 | *available_down -= tunnel->allocated_down; |
| 1065 | |
| 1066 | tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n", |
| 1067 | tunnel->allocated_up, tunnel->allocated_down); |
| 1068 | } |
| 1069 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1070 | static void tb_usb3_init_path(struct tb_path *path) |
| 1071 | { |
| 1072 | path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; |
| 1073 | path->egress_shared_buffer = TB_PATH_NONE; |
| 1074 | path->ingress_fc_enable = TB_PATH_ALL; |
| 1075 | path->ingress_shared_buffer = TB_PATH_NONE; |
| 1076 | path->priority = 3; |
| 1077 | path->weight = 3; |
| 1078 | path->drop_packages = 0; |
| 1079 | path->nfc_credits = 0; |
| 1080 | path->hops[0].initial_credits = 7; |
Mika Westerberg | 75ab3f0 | 2020-05-08 11:55:03 +0300 | [diff] [blame] | 1081 | if (path->path_length > 1) |
| 1082 | path->hops[1].initial_credits = |
| 1083 | tb_initial_credits(path->hops[1].in_port->sw); |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels |
| 1088 | * @tb: Pointer to the domain structure |
| 1089 | * @down: USB3 downstream adapter |
| 1090 | * |
| 1091 | * If @down adapter is active, follows the tunnel to the USB3 upstream |
| 1092 | * adapter and back. Returns the discovered tunnel or %NULL if there was |
| 1093 | * no tunnel. |
| 1094 | */ |
| 1095 | struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down) |
| 1096 | { |
| 1097 | struct tb_tunnel *tunnel; |
| 1098 | struct tb_path *path; |
| 1099 | |
| 1100 | if (!tb_usb3_port_is_enabled(down)) |
| 1101 | return NULL; |
| 1102 | |
| 1103 | tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); |
| 1104 | if (!tunnel) |
| 1105 | return NULL; |
| 1106 | |
| 1107 | tunnel->activate = tb_usb3_activate; |
| 1108 | tunnel->src_port = down; |
| 1109 | |
| 1110 | /* |
| 1111 | * Discover both paths even if they are not complete. We will |
| 1112 | * clean them up by calling tb_tunnel_deactivate() below in that |
| 1113 | * case. |
| 1114 | */ |
| 1115 | path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1, |
Mika Westerberg | 783735f | 2020-04-02 12:45:34 +0300 | [diff] [blame] | 1116 | &tunnel->dst_port, "USB3 Down"); |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1117 | if (!path) { |
| 1118 | /* Just disable the downstream port */ |
| 1119 | tb_usb3_port_enable(down, false); |
| 1120 | goto err_free; |
| 1121 | } |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1122 | tunnel->paths[TB_USB3_PATH_DOWN] = path; |
| 1123 | tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]); |
| 1124 | |
Mika Westerberg | 783735f | 2020-04-02 12:45:34 +0300 | [diff] [blame] | 1125 | path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL, |
| 1126 | "USB3 Up"); |
| 1127 | if (!path) |
| 1128 | goto err_deactivate; |
| 1129 | tunnel->paths[TB_USB3_PATH_UP] = path; |
| 1130 | tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]); |
| 1131 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1132 | /* Validate that the tunnel is complete */ |
| 1133 | if (!tb_port_is_usb3_up(tunnel->dst_port)) { |
| 1134 | tb_port_warn(tunnel->dst_port, |
| 1135 | "path does not end on an USB3 adapter, cleaning up\n"); |
| 1136 | goto err_deactivate; |
| 1137 | } |
| 1138 | |
| 1139 | if (down != tunnel->src_port) { |
| 1140 | tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); |
| 1141 | goto err_deactivate; |
| 1142 | } |
| 1143 | |
| 1144 | if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { |
| 1145 | tb_tunnel_warn(tunnel, |
| 1146 | "tunnel is not fully activated, cleaning up\n"); |
| 1147 | goto err_deactivate; |
| 1148 | } |
| 1149 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1150 | if (!tb_route(down->sw)) { |
| 1151 | int ret; |
| 1152 | |
| 1153 | /* |
| 1154 | * Read the initial bandwidth allocation for the first |
| 1155 | * hop tunnel. |
| 1156 | */ |
| 1157 | ret = usb4_usb3_port_allocated_bandwidth(down, |
| 1158 | &tunnel->allocated_up, &tunnel->allocated_down); |
| 1159 | if (ret) |
| 1160 | goto err_deactivate; |
| 1161 | |
| 1162 | tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", |
| 1163 | tunnel->allocated_up, tunnel->allocated_down); |
| 1164 | |
| 1165 | tunnel->init = tb_usb3_init; |
| 1166 | tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; |
| 1167 | tunnel->release_unused_bandwidth = |
| 1168 | tb_usb3_release_unused_bandwidth; |
| 1169 | tunnel->reclaim_available_bandwidth = |
| 1170 | tb_usb3_reclaim_available_bandwidth; |
| 1171 | } |
| 1172 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1173 | tb_tunnel_dbg(tunnel, "discovered\n"); |
| 1174 | return tunnel; |
| 1175 | |
| 1176 | err_deactivate: |
| 1177 | tb_tunnel_deactivate(tunnel); |
| 1178 | err_free: |
| 1179 | tb_tunnel_free(tunnel); |
| 1180 | |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel |
| 1186 | * @tb: Pointer to the domain structure |
| 1187 | * @up: USB3 upstream adapter port |
| 1188 | * @down: USB3 downstream adapter port |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1189 | * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0 |
| 1190 | * if not limited). |
| 1191 | * @max_down: Maximum available downstream bandwidth for the USB3 tunnel |
| 1192 | * (%0 if not limited). |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1193 | * |
| 1194 | * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and |
| 1195 | * @TB_TYPE_USB3_DOWN. |
| 1196 | * |
| 1197 | * Return: Returns a tb_tunnel on success or %NULL on failure. |
| 1198 | */ |
| 1199 | struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up, |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1200 | struct tb_port *down, int max_up, |
| 1201 | int max_down) |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1202 | { |
| 1203 | struct tb_tunnel *tunnel; |
| 1204 | struct tb_path *path; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1205 | int max_rate = 0; |
| 1206 | |
| 1207 | /* |
| 1208 | * Check that we have enough bandwidth available for the new |
| 1209 | * USB3 tunnel. |
| 1210 | */ |
| 1211 | if (max_up > 0 || max_down > 0) { |
| 1212 | max_rate = tb_usb3_max_link_rate(down, up); |
| 1213 | if (max_rate < 0) |
| 1214 | return NULL; |
| 1215 | |
| 1216 | /* Only 90% can be allocated for USB3 isochronous transfers */ |
| 1217 | max_rate = max_rate * 90 / 100; |
| 1218 | tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n", |
| 1219 | max_rate); |
| 1220 | |
| 1221 | if (max_rate > max_up || max_rate > max_down) { |
| 1222 | tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n"); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | } |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1226 | |
| 1227 | tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); |
| 1228 | if (!tunnel) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | tunnel->activate = tb_usb3_activate; |
| 1232 | tunnel->src_port = down; |
| 1233 | tunnel->dst_port = up; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1234 | tunnel->max_up = max_up; |
| 1235 | tunnel->max_down = max_down; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1236 | |
| 1237 | path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0, |
| 1238 | "USB3 Down"); |
| 1239 | if (!path) { |
| 1240 | tb_tunnel_free(tunnel); |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | tb_usb3_init_path(path); |
| 1244 | tunnel->paths[TB_USB3_PATH_DOWN] = path; |
| 1245 | |
| 1246 | path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0, |
| 1247 | "USB3 Up"); |
| 1248 | if (!path) { |
| 1249 | tb_tunnel_free(tunnel); |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | tb_usb3_init_path(path); |
| 1253 | tunnel->paths[TB_USB3_PATH_UP] = path; |
| 1254 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1255 | if (!tb_route(down->sw)) { |
| 1256 | tunnel->allocated_up = max_rate; |
| 1257 | tunnel->allocated_down = max_rate; |
| 1258 | |
| 1259 | tunnel->init = tb_usb3_init; |
| 1260 | tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; |
| 1261 | tunnel->release_unused_bandwidth = |
| 1262 | tb_usb3_release_unused_bandwidth; |
| 1263 | tunnel->reclaim_available_bandwidth = |
| 1264 | tb_usb3_reclaim_available_bandwidth; |
| 1265 | } |
| 1266 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1267 | return tunnel; |
| 1268 | } |
| 1269 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1270 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1271 | * tb_tunnel_free() - free a tunnel |
| 1272 | * @tunnel: Tunnel to be freed |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1273 | * |
Mika Westerberg | ab9f31c | 2019-03-06 18:21:08 +0200 | [diff] [blame] | 1274 | * Frees a tunnel. The tunnel does not need to be deactivated. |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1275 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1276 | void tb_tunnel_free(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1277 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1278 | int i; |
| 1279 | |
| 1280 | if (!tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1281 | return; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1282 | |
| 1283 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1284 | if (tunnel->paths[i]) |
| 1285 | tb_path_free(tunnel->paths[i]); |
| 1286 | } |
| 1287 | |
| 1288 | kfree(tunnel->paths); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1289 | kfree(tunnel); |
| 1290 | } |
| 1291 | |
| 1292 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1293 | * tb_tunnel_is_invalid - check whether an activated path is still valid |
| 1294 | * @tunnel: Tunnel to check |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1295 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1296 | bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1297 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1298 | int i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1299 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1300 | for (i = 0; i < tunnel->npaths; i++) { |
| 1301 | WARN_ON(!tunnel->paths[i]->activated); |
| 1302 | if (tb_path_is_invalid(tunnel->paths[i])) |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | return false; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1310 | * tb_tunnel_restart() - activate a tunnel after a hardware reset |
| 1311 | * @tunnel: Tunnel to restart |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1312 | * |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1313 | * Return: 0 on success and negative errno in case if failure |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1314 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1315 | int tb_tunnel_restart(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1316 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1317 | int res, i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1318 | |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1319 | tb_tunnel_dbg(tunnel, "activating\n"); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1320 | |
Mika Westerberg | aae9e27 | 2017-02-19 23:37:35 +0200 | [diff] [blame] | 1321 | /* |
| 1322 | * Make sure all paths are properly disabled before enabling |
| 1323 | * them again. |
| 1324 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1325 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | aae9e27 | 2017-02-19 23:37:35 +0200 | [diff] [blame] | 1326 | if (tunnel->paths[i]->activated) { |
| 1327 | tb_path_deactivate(tunnel->paths[i]); |
| 1328 | tunnel->paths[i]->activated = false; |
| 1329 | } |
| 1330 | } |
| 1331 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 1332 | if (tunnel->init) { |
| 1333 | res = tunnel->init(tunnel); |
| 1334 | if (res) |
| 1335 | return res; |
| 1336 | } |
| 1337 | |
Mika Westerberg | aae9e27 | 2017-02-19 23:37:35 +0200 | [diff] [blame] | 1338 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1339 | res = tb_path_activate(tunnel->paths[i]); |
| 1340 | if (res) |
| 1341 | goto err; |
| 1342 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1343 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1344 | if (tunnel->activate) { |
| 1345 | res = tunnel->activate(tunnel, true); |
| 1346 | if (res) |
| 1347 | goto err; |
| 1348 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1349 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1350 | return 0; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1351 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1352 | err: |
| 1353 | tb_tunnel_warn(tunnel, "activation failed\n"); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1354 | tb_tunnel_deactivate(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1355 | return res; |
| 1356 | } |
| 1357 | |
| 1358 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1359 | * tb_tunnel_activate() - activate a tunnel |
| 1360 | * @tunnel: Tunnel to activate |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1361 | * |
| 1362 | * Return: Returns 0 on success or an error code on failure. |
| 1363 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1364 | int tb_tunnel_activate(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1365 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1366 | int i; |
| 1367 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1368 | for (i = 0; i < tunnel->npaths; i++) { |
| 1369 | if (tunnel->paths[i]->activated) { |
| 1370 | tb_tunnel_WARN(tunnel, |
| 1371 | "trying to activate an already activated tunnel\n"); |
| 1372 | return -EINVAL; |
| 1373 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1376 | return tb_tunnel_restart(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1379 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1380 | * tb_tunnel_deactivate() - deactivate a tunnel |
| 1381 | * @tunnel: Tunnel to deactivate |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1382 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1383 | void tb_tunnel_deactivate(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1384 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1385 | int i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1386 | |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1387 | tb_tunnel_dbg(tunnel, "deactivating\n"); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1388 | |
| 1389 | if (tunnel->activate) |
| 1390 | tunnel->activate(tunnel, false); |
| 1391 | |
| 1392 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 1393 | if (tunnel->paths[i] && tunnel->paths[i]->activated) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1394 | tb_path_deactivate(tunnel->paths[i]); |
| 1395 | } |
| 1396 | } |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1397 | |
| 1398 | /** |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1399 | * tb_tunnel_port_on_path() - Does the tunnel go through port |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1400 | * @tunnel: Tunnel to check |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1401 | * @port: Port to check |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1402 | * |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1403 | * Returns true if @tunnel goes through @port (direction does not matter), |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1404 | * false otherwise. |
| 1405 | */ |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1406 | bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, |
| 1407 | const struct tb_port *port) |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1408 | { |
| 1409 | int i; |
| 1410 | |
| 1411 | for (i = 0; i < tunnel->npaths; i++) { |
| 1412 | if (!tunnel->paths[i]) |
| 1413 | continue; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1414 | |
| 1415 | if (tb_path_port_on_path(tunnel->paths[i], port)) |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1416 | return true; |
| 1417 | } |
| 1418 | |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) |
| 1423 | { |
| 1424 | int i; |
| 1425 | |
| 1426 | for (i = 0; i < tunnel->npaths; i++) { |
| 1427 | if (!tunnel->paths[i]) |
| 1428 | return false; |
| 1429 | if (!tunnel->paths[i]->activated) |
| 1430 | return false; |
| 1431 | } |
| 1432 | |
| 1433 | return true; |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel |
| 1438 | * @tunnel: Tunnel to check |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1439 | * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port. |
| 1440 | * Can be %NULL. |
| 1441 | * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port. |
| 1442 | * Can be %NULL. |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1443 | * |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1444 | * Stores the amount of isochronous bandwidth @tunnel consumes in |
| 1445 | * @consumed_up and @consumed_down. In case of success returns %0, |
| 1446 | * negative errno otherwise. |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1447 | */ |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1448 | int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, |
| 1449 | int *consumed_down) |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1450 | { |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1451 | int up_bw = 0, down_bw = 0; |
| 1452 | |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1453 | if (!tb_tunnel_is_active(tunnel)) |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1454 | goto out; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1455 | |
| 1456 | if (tunnel->consumed_bandwidth) { |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1457 | int ret; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1458 | |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1459 | ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); |
| 1460 | if (ret) |
| 1461 | return ret; |
| 1462 | |
| 1463 | tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, |
| 1464 | down_bw); |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1465 | } |
| 1466 | |
Mika Westerberg | 7c0ee8f | 2020-03-28 12:52:31 +0200 | [diff] [blame] | 1467 | out: |
| 1468 | if (consumed_up) |
| 1469 | *consumed_up = up_bw; |
| 1470 | if (consumed_down) |
| 1471 | *consumed_down = down_bw; |
| 1472 | |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 1475 | |
| 1476 | /** |
| 1477 | * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth |
| 1478 | * @tunnel: Tunnel whose unused bandwidth to release |
| 1479 | * |
| 1480 | * If tunnel supports dynamic bandwidth management (USB3 tunnels at the |
| 1481 | * moment) this function makes it to release all the unused bandwidth. |
| 1482 | * |
| 1483 | * Returns %0 in case of success and negative errno otherwise. |
| 1484 | */ |
| 1485 | int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) |
| 1486 | { |
| 1487 | if (!tb_tunnel_is_active(tunnel)) |
| 1488 | return 0; |
| 1489 | |
| 1490 | if (tunnel->release_unused_bandwidth) { |
| 1491 | int ret; |
| 1492 | |
| 1493 | ret = tunnel->release_unused_bandwidth(tunnel); |
| 1494 | if (ret) |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | /** |
| 1502 | * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth |
| 1503 | * @tunnel: Tunnel reclaiming available bandwidth |
| 1504 | * @available_up: Available upstream bandwidth (in Mb/s) |
| 1505 | * @available_down: Available downstream bandwidth (in Mb/s) |
| 1506 | * |
| 1507 | * Reclaims bandwidth from @available_up and @available_down and updates |
| 1508 | * the variables accordingly (e.g decreases both according to what was |
| 1509 | * reclaimed by the tunnel). If nothing was reclaimed the values are |
| 1510 | * kept as is. |
| 1511 | */ |
| 1512 | void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, |
| 1513 | int *available_up, |
| 1514 | int *available_down) |
| 1515 | { |
| 1516 | if (!tb_tunnel_is_active(tunnel)) |
| 1517 | return; |
| 1518 | |
| 1519 | if (tunnel->reclaim_available_bandwidth) |
| 1520 | tunnel->reclaim_available_bandwidth(tunnel, available_up, |
| 1521 | available_down); |
| 1522 | } |