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