1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt driver - Tunneling support 4 * 5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 6 * Copyright (C) 2019, Intel Corporation 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/slab.h> 11 #include <linux/list.h> 12 #include <linux/ktime.h> 13 #include <linux/string_helpers.h> 14 #include <linux/stringify.h> 15 16 #include "tunnel.h" 17 #include "tb.h" 18 19 /* PCIe adapters use always HopID of 8 for both directions */ 20 #define TB_PCI_HOPID 8 21 22 #define TB_PCI_PATH_DOWN 0 23 #define TB_PCI_PATH_UP 1 24 25 #define TB_PCI_PRIORITY 3 26 #define TB_PCI_WEIGHT 1 27 28 /* USB3 adapters use always HopID of 8 for both directions */ 29 #define TB_USB3_HOPID 8 30 31 #define TB_USB3_PATH_DOWN 0 32 #define TB_USB3_PATH_UP 1 33 34 #define TB_USB3_PRIORITY 3 35 #define TB_USB3_WEIGHT 2 36 37 /* DP adapters use HopID 8 for AUX and 9 for Video */ 38 #define TB_DP_AUX_TX_HOPID 8 39 #define TB_DP_AUX_RX_HOPID 8 40 #define TB_DP_VIDEO_HOPID 9 41 42 #define TB_DP_VIDEO_PATH_OUT 0 43 #define TB_DP_AUX_PATH_OUT 1 44 #define TB_DP_AUX_PATH_IN 2 45 46 #define TB_DP_VIDEO_PRIORITY 1 47 #define TB_DP_VIDEO_WEIGHT 1 48 49 #define TB_DP_AUX_PRIORITY 2 50 #define TB_DP_AUX_WEIGHT 1 51 52 /* struct tb_regs_hop::initial_credits is 7 bits wide */ 53 #define TB_MAX_CREDITS 127 54 55 /* Minimum number of credits needed for PCIe path */ 56 #define TB_MIN_PCIE_CREDITS 6U 57 /* 58 * Number of credits we try to allocate for each DMA path if not limited 59 * by the host router baMaxHI. 60 */ 61 #define TB_DMA_CREDITS 14 62 /* Minimum number of credits for DMA path */ 63 #define TB_MIN_DMA_CREDITS 1 64 65 #define TB_DMA_PRIORITY 5 66 #define TB_DMA_WEIGHT 1 67 68 /* 69 * Reserve additional bandwidth for USB 3.x and PCIe bulk traffic 70 * according to USB4 v2 Connection Manager guide. This ends up reserving 71 * 1500 Mb/s for PCIe and 3000 Mb/s for USB 3.x taking weights into 72 * account. 73 */ 74 #define USB4_V2_PCI_MIN_BANDWIDTH (1500 * TB_PCI_WEIGHT) 75 #define USB4_V2_USB3_MIN_BANDWIDTH (1500 * TB_USB3_WEIGHT) 76 77 /* 78 * According to VESA spec, the DPRX negotiation shall compete in 5 79 * seconds after tunnel is established. Since at least i915 can runtime 80 * suspend if there is nothing connected, and that it polls any new 81 * connections every 10 seconds, we use 12 seconds here. 82 * 83 * These are in ms. 84 */ 85 #define TB_DPRX_TIMEOUT 12000 86 #define TB_DPRX_WAIT_TIMEOUT 25 87 #define TB_DPRX_POLL_DELAY 50 88 89 static int dprx_timeout = TB_DPRX_TIMEOUT; 90 module_param(dprx_timeout, int, 0444); 91 MODULE_PARM_DESC(dprx_timeout, 92 "DPRX capability read timeout in ms, -1 waits forever (default: " 93 __stringify(TB_DPRX_TIMEOUT) ")"); 94 95 static unsigned int dma_credits = TB_DMA_CREDITS; 96 module_param(dma_credits, uint, 0444); 97 MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: " 98 __stringify(TB_DMA_CREDITS) ")"); 99 100 static bool bw_alloc_mode = true; 101 module_param(bw_alloc_mode, bool, 0444); 102 MODULE_PARM_DESC(bw_alloc_mode, 103 "enable bandwidth allocation mode if supported (default: true)"); 104 105 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" }; 106 107 static const char * const tb_event_names[] = { 108 [TB_TUNNEL_ACTIVATED] = "activated", 109 [TB_TUNNEL_CHANGED] = "changed", 110 [TB_TUNNEL_DEACTIVATED] = "deactivated", 111 [TB_TUNNEL_LOW_BANDWIDTH] = "low bandwidth", 112 [TB_TUNNEL_NO_BANDWIDTH] = "insufficient bandwidth", 113 }; 114 115 /* Synchronizes kref_get()/put() of struct tb_tunnel */ 116 static DEFINE_MUTEX(tb_tunnel_lock); 117 118 static inline unsigned int tb_usable_credits(const struct tb_port *port) 119 { 120 return port->total_credits - port->ctl_credits; 121 } 122 123 /** 124 * tb_available_credits() - Available credits for PCIe and DMA 125 * @port: Lane adapter to check 126 * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP 127 * streams possible through this lane adapter 128 * 129 * Return: Number of available credits. 130 */ 131 static unsigned int tb_available_credits(const struct tb_port *port, 132 size_t *max_dp_streams) 133 { 134 const struct tb_switch *sw = port->sw; 135 int credits, usb3, pcie, spare; 136 size_t ndp; 137 138 usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0; 139 pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0; 140 141 if (tb_acpi_is_xdomain_allowed()) { 142 spare = min_not_zero(sw->max_dma_credits, dma_credits); 143 /* Add some credits for potential second DMA tunnel */ 144 spare += TB_MIN_DMA_CREDITS; 145 } else { 146 spare = 0; 147 } 148 149 credits = tb_usable_credits(port); 150 if (tb_acpi_may_tunnel_dp()) { 151 /* 152 * Maximum number of DP streams possible through the 153 * lane adapter. 154 */ 155 if (sw->min_dp_aux_credits + sw->min_dp_main_credits) 156 ndp = (credits - (usb3 + pcie + spare)) / 157 (sw->min_dp_aux_credits + sw->min_dp_main_credits); 158 else 159 ndp = 0; 160 } else { 161 ndp = 0; 162 } 163 credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits); 164 credits -= usb3; 165 166 if (max_dp_streams) 167 *max_dp_streams = ndp; 168 169 return credits > 0 ? credits : 0; 170 } 171 172 static void tb_init_pm_support(struct tb_path_hop *hop) 173 { 174 struct tb_port *out_port = hop->out_port; 175 struct tb_port *in_port = hop->in_port; 176 177 if (tb_port_is_null(in_port) && tb_port_is_null(out_port) && 178 usb4_switch_version(in_port->sw) >= 2) 179 hop->pm_support = true; 180 } 181 182 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths, 183 enum tb_tunnel_type type) 184 { 185 struct tb_tunnel *tunnel; 186 187 tunnel = kzalloc_flex(*tunnel, paths, npaths); 188 if (!tunnel) 189 return NULL; 190 191 tunnel->npaths = npaths; 192 193 INIT_LIST_HEAD(&tunnel->list); 194 tunnel->tb = tb; 195 tunnel->type = type; 196 kref_init(&tunnel->kref); 197 198 return tunnel; 199 } 200 201 static void tb_tunnel_get(struct tb_tunnel *tunnel) 202 { 203 mutex_lock(&tb_tunnel_lock); 204 kref_get(&tunnel->kref); 205 mutex_unlock(&tb_tunnel_lock); 206 } 207 208 static void tb_tunnel_destroy(struct kref *kref) 209 { 210 struct tb_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref); 211 int i; 212 213 if (tunnel->destroy) 214 tunnel->destroy(tunnel); 215 216 for (i = 0; i < tunnel->npaths; i++) { 217 if (tunnel->paths[i]) 218 tb_path_free(tunnel->paths[i]); 219 } 220 221 kfree(tunnel); 222 } 223 224 void tb_tunnel_put(struct tb_tunnel *tunnel) 225 { 226 mutex_lock(&tb_tunnel_lock); 227 kref_put(&tunnel->kref, tb_tunnel_destroy); 228 mutex_unlock(&tb_tunnel_lock); 229 } 230 231 /** 232 * tb_tunnel_event() - Notify userspace about tunneling event 233 * @tb: Domain where the event occurred 234 * @event: Event that happened 235 * @type: Type of the tunnel in question 236 * @src_port: Tunnel source port (can be %NULL) 237 * @dst_port: Tunnel destination port (can be %NULL) 238 * 239 * Notifies userspace about tunneling @event in the domain. The tunnel 240 * does not need to exist (e.g the tunnel was not activated because 241 * there is not enough bandwidth). If the @src_port and @dst_port are 242 * given fill in full %TUNNEL_DETAILS environment variable. Otherwise 243 * uses the shorter one (just the tunnel type). 244 */ 245 void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event, 246 enum tb_tunnel_type type, 247 const struct tb_port *src_port, 248 const struct tb_port *dst_port) 249 { 250 char *envp[3] = { NULL }; 251 252 if (WARN_ON_ONCE(event >= ARRAY_SIZE(tb_event_names))) 253 return; 254 if (WARN_ON_ONCE(type >= ARRAY_SIZE(tb_tunnel_names))) 255 return; 256 257 envp[0] = kasprintf(GFP_KERNEL, "TUNNEL_EVENT=%s", tb_event_names[event]); 258 if (!envp[0]) 259 return; 260 261 if (src_port != NULL && dst_port != NULL) { 262 envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=%llx:%u <-> %llx:%u (%s)", 263 tb_route(src_port->sw), src_port->port, 264 tb_route(dst_port->sw), dst_port->port, 265 tb_tunnel_names[type]); 266 } else { 267 envp[1] = kasprintf(GFP_KERNEL, "TUNNEL_DETAILS=(%s)", 268 tb_tunnel_names[type]); 269 } 270 271 if (envp[1]) 272 tb_domain_event(tb, envp); 273 274 kfree(envp[1]); 275 kfree(envp[0]); 276 } 277 278 static inline void tb_tunnel_set_active(struct tb_tunnel *tunnel, bool active) 279 { 280 if (active) { 281 tunnel->state = TB_TUNNEL_ACTIVE; 282 tb_tunnel_event(tunnel->tb, TB_TUNNEL_ACTIVATED, tunnel->type, 283 tunnel->src_port, tunnel->dst_port); 284 } else { 285 tunnel->state = TB_TUNNEL_INACTIVE; 286 tb_tunnel_event(tunnel->tb, TB_TUNNEL_DEACTIVATED, tunnel->type, 287 tunnel->src_port, tunnel->dst_port); 288 } 289 } 290 291 static inline void tb_tunnel_changed(struct tb_tunnel *tunnel) 292 { 293 tb_tunnel_event(tunnel->tb, TB_TUNNEL_CHANGED, tunnel->type, 294 tunnel->src_port, tunnel->dst_port); 295 } 296 297 static int tb_pci_port_ltssm_state_detect(struct tb_port *port) 298 { 299 ktime_t timeout = ktime_add_ms(ktime_get(), 500); 300 301 do { 302 int ret; 303 304 ret = usb4_pci_port_ltssm_state(port); 305 if (ret < 0) 306 return ret; 307 if (ret == USB4_PCIE_LTSSM_DETECT) 308 return 0; 309 310 fsleep(50); 311 } while (ktime_before(ktime_get(), timeout)); 312 313 return -ETIMEDOUT; 314 } 315 316 static int tb_pci_pre_activate(struct tb_tunnel *tunnel) 317 { 318 struct tb_port *down = tunnel->src_port; 319 struct tb_port *up = tunnel->dst_port; 320 int ret; 321 322 ret = tb_switch_is_usb4(down->sw) ? 323 tb_pci_port_ltssm_state_detect(down) : 0; 324 if (ret) 325 return ret; 326 327 return tb_switch_is_usb4(up->sw) ? 328 tb_pci_port_ltssm_state_detect(up) : 0; 329 } 330 331 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable) 332 { 333 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); 334 int ret; 335 336 /* Only supported if both routers are at least USB4 v2 */ 337 if ((usb4_switch_version(tunnel->src_port->sw) < 2) || 338 (usb4_switch_version(tunnel->dst_port->sw) < 2)) 339 return 0; 340 341 if (enable && tb_port_get_link_generation(port) < 4) 342 return 0; 343 344 ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable); 345 if (ret) 346 return ret; 347 348 /* 349 * Downstream router could be unplugged so disable of encapsulation 350 * in upstream router is still possible. 351 */ 352 ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable); 353 if (ret) { 354 if (enable) 355 return ret; 356 if (ret != -ENODEV) 357 return ret; 358 } 359 360 tb_tunnel_dbg(tunnel, "extended encapsulation %s\n", 361 str_enabled_disabled(enable)); 362 return 0; 363 } 364 365 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) 366 { 367 int res; 368 369 if (activate) { 370 res = tb_pci_set_ext_encapsulation(tunnel, activate); 371 if (res) 372 return res; 373 } 374 375 if (activate) 376 res = tb_pci_port_enable(tunnel->dst_port, activate); 377 else 378 res = tb_pci_port_enable(tunnel->src_port, activate); 379 if (res) 380 return res; 381 382 383 if (activate) { 384 res = tb_pci_port_enable(tunnel->src_port, activate); 385 if (res) 386 return res; 387 } else { 388 /* Downstream router could be unplugged */ 389 tb_pci_port_enable(tunnel->dst_port, activate); 390 } 391 392 return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate); 393 } 394 395 static int tb_pci_init_credits(struct tb_path_hop *hop) 396 { 397 struct tb_port *port = hop->in_port; 398 struct tb_switch *sw = port->sw; 399 unsigned int credits; 400 401 if (tb_port_use_credit_allocation(port)) { 402 unsigned int available; 403 404 available = tb_available_credits(port, NULL); 405 credits = min(sw->max_pcie_credits, available); 406 407 if (credits < TB_MIN_PCIE_CREDITS) 408 return -ENOSPC; 409 410 credits = max(TB_MIN_PCIE_CREDITS, credits); 411 } else { 412 if (tb_port_is_null(port)) 413 credits = port->bonded ? 32 : 16; 414 else 415 credits = 7; 416 } 417 418 hop->initial_credits = credits; 419 return 0; 420 } 421 422 static int tb_pci_init_path(struct tb_path *path) 423 { 424 struct tb_path_hop *hop; 425 426 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; 427 path->egress_shared_buffer = TB_PATH_NONE; 428 path->ingress_fc_enable = TB_PATH_ALL; 429 path->ingress_shared_buffer = TB_PATH_NONE; 430 path->priority = TB_PCI_PRIORITY; 431 path->weight = TB_PCI_WEIGHT; 432 path->drop_packages = 0; 433 434 tb_path_for_each_hop(path, hop) { 435 int ret; 436 437 ret = tb_pci_init_credits(hop); 438 if (ret) 439 return ret; 440 } 441 442 return 0; 443 } 444 445 /** 446 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels 447 * @tb: Pointer to the domain structure 448 * @down: PCIe downstream adapter 449 * @alloc_hopid: Allocate HopIDs from visited ports 450 * 451 * If @down adapter is active, follows the tunnel to the PCIe upstream 452 * adapter and back. 453 * 454 * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel. 455 */ 456 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down, 457 bool alloc_hopid) 458 { 459 struct tb_tunnel *tunnel; 460 struct tb_path *path; 461 462 if (!tb_pci_port_is_enabled(down)) 463 return NULL; 464 465 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); 466 if (!tunnel) 467 return NULL; 468 469 tunnel->activate = tb_pci_activate; 470 tunnel->src_port = down; 471 472 /* 473 * Discover both paths even if they are not complete. We will 474 * clean them up by calling tb_tunnel_deactivate() below in that 475 * case. 476 */ 477 path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1, 478 &tunnel->dst_port, "PCIe Up", alloc_hopid); 479 if (!path) { 480 /* Just disable the downstream port */ 481 tb_pci_port_enable(down, false); 482 goto err_free; 483 } 484 tunnel->paths[TB_PCI_PATH_UP] = path; 485 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP])) 486 goto err_free; 487 488 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL, 489 "PCIe Down", alloc_hopid); 490 if (!path) 491 goto err_deactivate; 492 tunnel->paths[TB_PCI_PATH_DOWN] = path; 493 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN])) 494 goto err_deactivate; 495 496 /* Validate that the tunnel is complete */ 497 if (!tb_port_is_pcie_up(tunnel->dst_port)) { 498 tb_port_warn(tunnel->dst_port, 499 "path does not end on a PCIe adapter, cleaning up\n"); 500 goto err_deactivate; 501 } 502 503 if (down != tunnel->src_port) { 504 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); 505 goto err_deactivate; 506 } 507 508 if (!tb_pci_port_is_enabled(tunnel->dst_port)) { 509 tb_tunnel_warn(tunnel, 510 "tunnel is not fully activated, cleaning up\n"); 511 goto err_deactivate; 512 } 513 514 tb_tunnel_set_active(tunnel, true); 515 tb_tunnel_dbg(tunnel, "discovered\n"); 516 return tunnel; 517 518 err_deactivate: 519 tb_tunnel_deactivate(tunnel); 520 err_free: 521 tb_tunnel_put(tunnel); 522 523 return NULL; 524 } 525 526 /** 527 * tb_tunnel_alloc_pci() - allocate a pci tunnel 528 * @tb: Pointer to the domain structure 529 * @up: PCIe upstream adapter port 530 * @down: PCIe downstream adapter port 531 * 532 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and 533 * TB_TYPE_PCIE_DOWN. 534 * 535 * Return: Pointer to @struct tb_tunnel or %NULL on failure. 536 */ 537 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up, 538 struct tb_port *down) 539 { 540 struct tb_tunnel *tunnel; 541 struct tb_path *path; 542 543 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); 544 if (!tunnel) 545 return NULL; 546 547 tunnel->pre_activate = tb_pci_pre_activate; 548 tunnel->activate = tb_pci_activate; 549 tunnel->src_port = down; 550 tunnel->dst_port = up; 551 552 path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0, 553 "PCIe Down"); 554 if (!path) 555 goto err_free; 556 tunnel->paths[TB_PCI_PATH_DOWN] = path; 557 if (tb_pci_init_path(path)) 558 goto err_free; 559 560 path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0, 561 "PCIe Up"); 562 if (!path) 563 goto err_free; 564 tunnel->paths[TB_PCI_PATH_UP] = path; 565 if (tb_pci_init_path(path)) 566 goto err_free; 567 568 return tunnel; 569 570 err_free: 571 tb_tunnel_put(tunnel); 572 return NULL; 573 } 574 575 /** 576 * tb_tunnel_reserved_pci() - Amount of bandwidth to reserve for PCIe 577 * @port: Lane 0 adapter 578 * @reserved_up: Upstream bandwidth in Mb/s to reserve 579 * @reserved_down: Downstream bandwidth in Mb/s to reserve 580 * 581 * Can be called to any connected lane 0 adapter to find out how much 582 * bandwidth needs to be left in reserve for possible PCIe bulk traffic. 583 * 584 * Return: 585 * * %true - If there is something to be reserved. Writes the amount to 586 * @reserved_down/@reserved_up. 587 * * %false - Nothing to be reserved. Leaves @reserved_down/@reserved_up 588 * unmodified. 589 */ 590 bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up, 591 int *reserved_down) 592 { 593 if (WARN_ON_ONCE(!port->remote)) 594 return false; 595 596 if (!tb_acpi_may_tunnel_pcie()) 597 return false; 598 599 if (tb_port_get_link_generation(port) < 4) 600 return false; 601 602 /* Must have PCIe adapters */ 603 if (tb_is_upstream_port(port)) { 604 if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_UP)) 605 return false; 606 if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_DOWN)) 607 return false; 608 } else { 609 if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_DOWN)) 610 return false; 611 if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_UP)) 612 return false; 613 } 614 615 *reserved_up = USB4_V2_PCI_MIN_BANDWIDTH; 616 *reserved_down = USB4_V2_PCI_MIN_BANDWIDTH; 617 618 tb_port_dbg(port, "reserving %u/%u Mb/s for PCIe\n", *reserved_up, 619 *reserved_down); 620 return true; 621 } 622 623 static bool tb_dp_is_usb4(const struct tb_switch *sw) 624 { 625 /* Titan Ridge DP adapters need the same treatment as USB4 */ 626 return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw); 627 } 628 629 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out, 630 int timeout_msec) 631 { 632 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); 633 u32 val; 634 int ret; 635 636 /* Both ends need to support this */ 637 if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw)) 638 return 0; 639 640 ret = tb_port_read(out, &val, TB_CFG_PORT, 641 out->cap_adap + DP_STATUS_CTRL, 1); 642 if (ret) 643 return ret; 644 645 val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS; 646 647 ret = tb_port_write(out, &val, TB_CFG_PORT, 648 out->cap_adap + DP_STATUS_CTRL, 1); 649 if (ret) 650 return ret; 651 652 do { 653 ret = tb_port_read(out, &val, TB_CFG_PORT, 654 out->cap_adap + DP_STATUS_CTRL, 1); 655 if (ret) 656 return ret; 657 if (!(val & DP_STATUS_CTRL_CMHS)) 658 return 0; 659 usleep_range(100, 150); 660 } while (ktime_before(ktime_get(), timeout)); 661 662 return -ETIMEDOUT; 663 } 664 665 /* 666 * Returns maximum possible rate from capability supporting only DP 2.0 667 * and below. Used when DP BW allocation mode is not enabled. 668 */ 669 static inline u32 tb_dp_cap_get_rate(u32 val) 670 { 671 u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT; 672 673 switch (rate) { 674 case DP_COMMON_CAP_RATE_RBR: 675 return 1620; 676 case DP_COMMON_CAP_RATE_HBR: 677 return 2700; 678 case DP_COMMON_CAP_RATE_HBR2: 679 return 5400; 680 case DP_COMMON_CAP_RATE_HBR3: 681 return 8100; 682 default: 683 return 0; 684 } 685 } 686 687 /* 688 * Returns maximum possible rate from capability supporting DP 2.1 689 * UHBR20, 13.5 and 10 rates as well. Use only when DP BW allocation 690 * mode is enabled. 691 */ 692 static inline u32 tb_dp_cap_get_rate_ext(u32 val) 693 { 694 if (val & DP_COMMON_CAP_UHBR20) 695 return 20000; 696 else if (val & DP_COMMON_CAP_UHBR13_5) 697 return 13500; 698 else if (val & DP_COMMON_CAP_UHBR10) 699 return 10000; 700 701 return tb_dp_cap_get_rate(val); 702 } 703 704 static inline bool tb_dp_is_uhbr_rate(unsigned int rate) 705 { 706 return rate >= 10000; 707 } 708 709 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate) 710 { 711 val &= ~DP_COMMON_CAP_RATE_MASK; 712 switch (rate) { 713 default: 714 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate); 715 fallthrough; 716 case 1620: 717 val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT; 718 break; 719 case 2700: 720 val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT; 721 break; 722 case 5400: 723 val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT; 724 break; 725 case 8100: 726 val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT; 727 break; 728 } 729 return val; 730 } 731 732 static inline u32 tb_dp_cap_get_lanes(u32 val) 733 { 734 u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT; 735 736 switch (lanes) { 737 case DP_COMMON_CAP_1_LANE: 738 return 1; 739 case DP_COMMON_CAP_2_LANES: 740 return 2; 741 case DP_COMMON_CAP_4_LANES: 742 return 4; 743 default: 744 return 0; 745 } 746 } 747 748 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes) 749 { 750 val &= ~DP_COMMON_CAP_LANES_MASK; 751 switch (lanes) { 752 default: 753 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n", 754 lanes); 755 fallthrough; 756 case 1: 757 val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT; 758 break; 759 case 2: 760 val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT; 761 break; 762 case 4: 763 val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT; 764 break; 765 } 766 return val; 767 } 768 769 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes) 770 { 771 /* Tunneling removes the DP 8b/10b 128/132b encoding */ 772 if (tb_dp_is_uhbr_rate(rate)) 773 return rate * lanes * 128 / 132; 774 return rate * lanes * 8 / 10; 775 } 776 777 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes, 778 u32 out_rate, u32 out_lanes, u32 *new_rate, 779 u32 *new_lanes) 780 { 781 static const u32 dp_bw[][2] = { 782 /* Mb/s, lanes */ 783 { 8100, 4 }, /* 25920 Mb/s */ 784 { 5400, 4 }, /* 17280 Mb/s */ 785 { 8100, 2 }, /* 12960 Mb/s */ 786 { 2700, 4 }, /* 8640 Mb/s */ 787 { 5400, 2 }, /* 8640 Mb/s */ 788 { 8100, 1 }, /* 6480 Mb/s */ 789 { 1620, 4 }, /* 5184 Mb/s */ 790 { 5400, 1 }, /* 4320 Mb/s */ 791 { 2700, 2 }, /* 4320 Mb/s */ 792 { 1620, 2 }, /* 2592 Mb/s */ 793 { 2700, 1 }, /* 2160 Mb/s */ 794 { 1620, 1 }, /* 1296 Mb/s */ 795 }; 796 unsigned int i; 797 798 /* 799 * Find a combination that can fit into max_bw and does not 800 * exceed the maximum rate and lanes supported by the DP OUT and 801 * DP IN adapters. 802 */ 803 for (i = 0; i < ARRAY_SIZE(dp_bw); i++) { 804 if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes) 805 continue; 806 807 if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes) 808 continue; 809 810 if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) { 811 *new_rate = dp_bw[i][0]; 812 *new_lanes = dp_bw[i][1]; 813 return 0; 814 } 815 } 816 817 return -ENOSR; 818 } 819 820 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel) 821 { 822 u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw; 823 struct tb_port *out = tunnel->dst_port; 824 struct tb_port *in = tunnel->src_port; 825 int ret, max_bw; 826 827 /* 828 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for 829 * newer generation hardware. 830 */ 831 if (in->sw->generation < 2 || out->sw->generation < 2) 832 return 0; 833 834 /* 835 * Perform connection manager handshake between IN and OUT ports 836 * before capabilities exchange can take place. 837 */ 838 ret = tb_dp_cm_handshake(in, out, 3000); 839 if (ret) 840 return ret; 841 842 /* Read both DP_LOCAL_CAP registers */ 843 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT, 844 in->cap_adap + DP_LOCAL_CAP, 1); 845 if (ret) 846 return ret; 847 848 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT, 849 out->cap_adap + DP_LOCAL_CAP, 1); 850 if (ret) 851 return ret; 852 853 /* Write IN local caps to OUT remote caps */ 854 ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT, 855 out->cap_adap + DP_REMOTE_CAP, 1); 856 if (ret) 857 return ret; 858 859 in_rate = tb_dp_cap_get_rate(in_dp_cap); 860 in_lanes = tb_dp_cap_get_lanes(in_dp_cap); 861 tb_tunnel_dbg(tunnel, 862 "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", 863 in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes)); 864 865 /* 866 * If the tunnel bandwidth is limited (max_bw is set) then see 867 * if we need to reduce bandwidth to fit there. 868 */ 869 out_rate = tb_dp_cap_get_rate(out_dp_cap); 870 out_lanes = tb_dp_cap_get_lanes(out_dp_cap); 871 bw = tb_dp_bandwidth(out_rate, out_lanes); 872 tb_tunnel_dbg(tunnel, 873 "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", 874 out_rate, out_lanes, bw); 875 876 if (tb_tunnel_direction_downstream(tunnel)) 877 max_bw = tunnel->max_down; 878 else 879 max_bw = tunnel->max_up; 880 881 if (max_bw && bw > max_bw) { 882 u32 new_rate, new_lanes, new_bw; 883 884 ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes, 885 out_rate, out_lanes, &new_rate, 886 &new_lanes); 887 if (ret) { 888 tb_tunnel_info(tunnel, "not enough bandwidth\n"); 889 return ret; 890 } 891 892 new_bw = tb_dp_bandwidth(new_rate, new_lanes); 893 tb_tunnel_dbg(tunnel, 894 "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n", 895 new_rate, new_lanes, new_bw); 896 897 /* 898 * Set new rate and number of lanes before writing it to 899 * the IN port remote caps. 900 */ 901 out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate); 902 out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes); 903 } 904 905 /* 906 * Titan Ridge does not disable AUX timers when it gets 907 * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with 908 * DP tunneling. 909 */ 910 if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) { 911 out_dp_cap |= DP_COMMON_CAP_LTTPR_NS; 912 tb_tunnel_dbg(tunnel, "disabling LTTPR\n"); 913 } 914 915 return tb_port_write(in, &out_dp_cap, TB_CFG_PORT, 916 in->cap_adap + DP_REMOTE_CAP, 1); 917 } 918 919 static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel) 920 { 921 int ret, estimated_bw, granularity, tmp; 922 struct tb_port *out = tunnel->dst_port; 923 struct tb_port *in = tunnel->src_port; 924 u32 out_dp_cap, out_rate, out_lanes; 925 u32 in_dp_cap, in_rate, in_lanes; 926 u32 rate, lanes; 927 928 if (!bw_alloc_mode) 929 return 0; 930 931 ret = usb4_dp_port_set_cm_bandwidth_mode_supported(in, true); 932 if (ret) 933 return ret; 934 935 ret = usb4_dp_port_set_group_id(in, in->group->index); 936 if (ret) 937 return ret; 938 939 /* 940 * Get the non-reduced rate and lanes based on the lowest 941 * capability of both adapters. 942 */ 943 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT, 944 in->cap_adap + DP_LOCAL_CAP, 1); 945 if (ret) 946 return ret; 947 948 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT, 949 out->cap_adap + DP_LOCAL_CAP, 1); 950 if (ret) 951 return ret; 952 953 in_rate = tb_dp_cap_get_rate(in_dp_cap); 954 in_lanes = tb_dp_cap_get_lanes(in_dp_cap); 955 out_rate = tb_dp_cap_get_rate(out_dp_cap); 956 out_lanes = tb_dp_cap_get_lanes(out_dp_cap); 957 958 rate = min(in_rate, out_rate); 959 lanes = min(in_lanes, out_lanes); 960 tmp = tb_dp_bandwidth(rate, lanes); 961 962 tb_tunnel_dbg(tunnel, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n", 963 rate, lanes, tmp); 964 965 ret = usb4_dp_port_set_nrd(in, rate, lanes); 966 if (ret) 967 return ret; 968 969 /* 970 * Pick up granularity that supports maximum possible bandwidth. 971 * For that we use the UHBR rates too. 972 */ 973 in_rate = tb_dp_cap_get_rate_ext(in_dp_cap); 974 out_rate = tb_dp_cap_get_rate_ext(out_dp_cap); 975 rate = min(in_rate, out_rate); 976 tmp = tb_dp_bandwidth(rate, lanes); 977 978 tb_tunnel_dbg(tunnel, 979 "maximum bandwidth through allocation mode %u Mb/s x%u = %u Mb/s\n", 980 rate, lanes, tmp); 981 982 for (granularity = 250; tmp / granularity > 255 && granularity <= 1000; 983 granularity *= 2) 984 ; 985 986 tb_tunnel_dbg(tunnel, "granularity %d Mb/s\n", granularity); 987 988 /* 989 * Returns -EINVAL if granularity above is outside of the 990 * accepted ranges. 991 */ 992 ret = usb4_dp_port_set_granularity(in, granularity); 993 if (ret) 994 return ret; 995 996 /* 997 * Bandwidth estimation is pretty much what we have in 998 * max_up/down fields. For discovery we just read what the 999 * estimation was set to. 1000 */ 1001 if (tb_tunnel_direction_downstream(tunnel)) 1002 estimated_bw = tunnel->max_down; 1003 else 1004 estimated_bw = tunnel->max_up; 1005 1006 tb_tunnel_dbg(tunnel, "estimated bandwidth %d Mb/s\n", estimated_bw); 1007 1008 ret = usb4_dp_port_set_estimated_bandwidth(in, estimated_bw); 1009 if (ret) 1010 return ret; 1011 1012 /* Initial allocation should be 0 according the spec */ 1013 ret = usb4_dp_port_allocate_bandwidth(in, 0); 1014 if (ret) 1015 return ret; 1016 1017 tb_tunnel_dbg(tunnel, "bandwidth allocation mode enabled\n"); 1018 return 0; 1019 } 1020 1021 static int tb_dp_pre_activate(struct tb_tunnel *tunnel) 1022 { 1023 struct tb_port *in = tunnel->src_port; 1024 struct tb_switch *sw = in->sw; 1025 struct tb *tb = in->sw->tb; 1026 int ret; 1027 1028 ret = tb_dp_xchg_caps(tunnel); 1029 if (ret) 1030 return ret; 1031 1032 if (!tb_switch_is_usb4(sw)) 1033 return 0; 1034 1035 if (!usb4_dp_port_bandwidth_mode_supported(in)) 1036 return 0; 1037 1038 tb_tunnel_dbg(tunnel, "bandwidth allocation mode supported\n"); 1039 1040 ret = usb4_dp_port_set_cm_id(in, tb->index); 1041 if (ret) 1042 return ret; 1043 1044 return tb_dp_bandwidth_alloc_mode_enable(tunnel); 1045 } 1046 1047 static void tb_dp_post_deactivate(struct tb_tunnel *tunnel) 1048 { 1049 struct tb_port *in = tunnel->src_port; 1050 1051 if (!usb4_dp_port_bandwidth_mode_supported(in)) 1052 return; 1053 if (usb4_dp_port_bandwidth_mode_enabled(in)) { 1054 usb4_dp_port_set_cm_bandwidth_mode_supported(in, false); 1055 tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n"); 1056 } 1057 } 1058 1059 static ktime_t dprx_timeout_to_ktime(int timeout_msec) 1060 { 1061 return timeout_msec >= 0 ? 1062 ktime_add_ms(ktime_get(), timeout_msec) : KTIME_MAX; 1063 } 1064 1065 static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec) 1066 { 1067 ktime_t timeout = dprx_timeout_to_ktime(timeout_msec); 1068 struct tb_port *in = tunnel->src_port; 1069 1070 /* 1071 * Wait for DPRX done. Normally it should be already set for 1072 * active tunnel. 1073 */ 1074 do { 1075 u32 val; 1076 int ret; 1077 1078 ret = tb_port_read(in, &val, TB_CFG_PORT, 1079 in->cap_adap + DP_COMMON_CAP, 1); 1080 if (ret) 1081 return ret; 1082 1083 if (val & DP_COMMON_CAP_DPRX_DONE) 1084 return 0; 1085 1086 usleep_range(100, 150); 1087 } while (ktime_before(ktime_get(), timeout)); 1088 1089 tb_tunnel_dbg(tunnel, "DPRX read timeout\n"); 1090 return -ETIMEDOUT; 1091 } 1092 1093 static void tb_dp_dprx_work(struct work_struct *work) 1094 { 1095 struct tb_tunnel *tunnel = container_of(work, typeof(*tunnel), dprx_work.work); 1096 struct tb *tb = tunnel->tb; 1097 1098 /* 1099 * The DPRX read can be canceled while this work is waiting for 1100 * tb->lock. Check the flag only once it is held: while the lock is 1101 * held the tunnel cannot be torn down under us and the adapters are 1102 * safe to access. 1103 */ 1104 mutex_lock(&tb->lock); 1105 if (!tunnel->dprx_canceled) { 1106 if (tb_dp_is_usb4(tunnel->src_port->sw) && 1107 tb_dp_wait_dprx(tunnel, TB_DPRX_WAIT_TIMEOUT)) { 1108 if (ktime_before(ktime_get(), tunnel->dprx_timeout)) { 1109 queue_delayed_work(tb->wq, &tunnel->dprx_work, 1110 msecs_to_jiffies(TB_DPRX_POLL_DELAY)); 1111 mutex_unlock(&tb->lock); 1112 return; 1113 } 1114 } else { 1115 tb_tunnel_set_active(tunnel, true); 1116 } 1117 } 1118 mutex_unlock(&tb->lock); 1119 1120 tunnel->callback(tunnel, tunnel->callback_data); 1121 tb_tunnel_put(tunnel); 1122 tb_domain_put(tb); 1123 } 1124 1125 static int tb_dp_dprx_start(struct tb_tunnel *tunnel) 1126 { 1127 /* 1128 * Bump up the references to keep the tunnel and the domain around 1129 * until the work has run or has been canceled. 1130 */ 1131 tb_tunnel_get(tunnel); 1132 tb_domain_get(tunnel->tb); 1133 1134 tunnel->dprx_started = true; 1135 tunnel->dprx_canceled = false; 1136 tunnel->dprx_timeout = dprx_timeout_to_ktime(dprx_timeout); 1137 queue_delayed_work(tunnel->tb->wq, &tunnel->dprx_work, 0); 1138 1139 return -EINPROGRESS; 1140 } 1141 1142 static void tb_dp_dprx_stop(struct tb_tunnel *tunnel) 1143 { 1144 struct tb *tb = tunnel->tb; 1145 1146 if (tunnel->dprx_started) { 1147 tunnel->dprx_started = false; 1148 tunnel->dprx_canceled = true; 1149 if (cancel_delayed_work(&tunnel->dprx_work)) { 1150 tb_tunnel_put(tunnel); 1151 tb_domain_put(tb); 1152 } 1153 } 1154 } 1155 1156 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active) 1157 { 1158 int ret; 1159 1160 if (active) { 1161 struct tb_path **paths; 1162 int last; 1163 1164 paths = tunnel->paths; 1165 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1; 1166 1167 tb_dp_port_set_hops(tunnel->src_port, 1168 paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index, 1169 paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index, 1170 paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index); 1171 1172 tb_dp_port_set_hops(tunnel->dst_port, 1173 paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index, 1174 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index, 1175 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index); 1176 } else { 1177 tb_dp_dprx_stop(tunnel); 1178 tb_dp_port_hpd_clear(tunnel->src_port); 1179 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0); 1180 if (tb_port_is_dpout(tunnel->dst_port)) 1181 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0); 1182 } 1183 1184 ret = tb_dp_port_enable(tunnel->src_port, active); 1185 if (ret) 1186 return ret; 1187 1188 if (tb_port_is_dpout(tunnel->dst_port)) { 1189 ret = tb_dp_port_enable(tunnel->dst_port, active); 1190 if (ret) 1191 return ret; 1192 } 1193 1194 return active ? tb_dp_dprx_start(tunnel) : 0; 1195 } 1196 1197 /** 1198 * tb_dp_bandwidth_mode_maximum_bandwidth() - Maximum possible bandwidth 1199 * @tunnel: DP tunnel to check 1200 * @max_bw_rounded: Maximum bandwidth in Mb/s rounded up to the next granularity 1201 * 1202 * Return: Maximum possible bandwidth for this tunnel in Mb/s, negative errno 1203 * in case of failure. 1204 */ 1205 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel, 1206 int *max_bw_rounded) 1207 { 1208 struct tb_port *in = tunnel->src_port; 1209 int ret, rate, lanes, max_bw; 1210 u32 cap; 1211 1212 /* 1213 * DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX 1214 * read parameter values so we can use this to determine the 1215 * maximum possible bandwidth over this link. 1216 * 1217 * See USB4 v2 spec 1.0 10.4.4.5. 1218 */ 1219 ret = tb_port_read(in, &cap, TB_CFG_PORT, 1220 in->cap_adap + DP_LOCAL_CAP, 1); 1221 if (ret) 1222 return ret; 1223 1224 rate = tb_dp_cap_get_rate_ext(cap); 1225 lanes = tb_dp_cap_get_lanes(cap); 1226 1227 max_bw = tb_dp_bandwidth(rate, lanes); 1228 1229 if (max_bw_rounded) { 1230 ret = usb4_dp_port_granularity(in); 1231 if (ret < 0) 1232 return ret; 1233 *max_bw_rounded = roundup(max_bw, ret); 1234 } 1235 1236 return max_bw; 1237 } 1238 1239 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel, 1240 int *consumed_up, 1241 int *consumed_down) 1242 { 1243 struct tb_port *in = tunnel->src_port; 1244 int ret, allocated_bw, max_bw_rounded; 1245 1246 if (!usb4_dp_port_bandwidth_mode_enabled(in)) 1247 return -EOPNOTSUPP; 1248 1249 if (!tunnel->bw_mode) 1250 return -EOPNOTSUPP; 1251 1252 /* Read what was allocated previously if any */ 1253 ret = usb4_dp_port_allocated_bandwidth(in); 1254 if (ret < 0) 1255 return ret; 1256 allocated_bw = ret; 1257 1258 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded); 1259 if (ret < 0) 1260 return ret; 1261 if (allocated_bw == max_bw_rounded) 1262 allocated_bw = ret; 1263 1264 if (tb_tunnel_direction_downstream(tunnel)) { 1265 *consumed_up = 0; 1266 *consumed_down = allocated_bw; 1267 } else { 1268 *consumed_up = allocated_bw; 1269 *consumed_down = 0; 1270 } 1271 1272 return 0; 1273 } 1274 1275 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, 1276 int *allocated_down) 1277 { 1278 struct tb_port *in = tunnel->src_port; 1279 1280 /* 1281 * If we have already set the allocated bandwidth then use that. 1282 * Otherwise we read it from the DPRX. 1283 */ 1284 if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) { 1285 int ret, allocated_bw, max_bw_rounded; 1286 1287 ret = usb4_dp_port_allocated_bandwidth(in); 1288 if (ret < 0) 1289 return ret; 1290 allocated_bw = ret; 1291 1292 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, 1293 &max_bw_rounded); 1294 if (ret < 0) 1295 return ret; 1296 if (allocated_bw == max_bw_rounded) 1297 allocated_bw = ret; 1298 1299 if (tb_tunnel_direction_downstream(tunnel)) { 1300 *allocated_up = 0; 1301 *allocated_down = allocated_bw; 1302 } else { 1303 *allocated_up = allocated_bw; 1304 *allocated_down = 0; 1305 } 1306 return 0; 1307 } 1308 1309 return tunnel->consumed_bandwidth(tunnel, allocated_up, 1310 allocated_down); 1311 } 1312 1313 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, 1314 int *alloc_down) 1315 { 1316 struct tb_port *in = tunnel->src_port; 1317 int max_bw_rounded, ret, tmp; 1318 1319 if (!usb4_dp_port_bandwidth_mode_enabled(in)) 1320 return -EOPNOTSUPP; 1321 1322 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded); 1323 if (ret < 0) 1324 return ret; 1325 1326 if (tb_tunnel_direction_downstream(tunnel)) { 1327 tmp = min(*alloc_down, max_bw_rounded); 1328 ret = usb4_dp_port_allocate_bandwidth(in, tmp); 1329 if (ret) 1330 return ret; 1331 *alloc_down = tmp; 1332 *alloc_up = 0; 1333 } else { 1334 tmp = min(*alloc_up, max_bw_rounded); 1335 ret = usb4_dp_port_allocate_bandwidth(in, tmp); 1336 if (ret) 1337 return ret; 1338 *alloc_down = 0; 1339 *alloc_up = tmp; 1340 } 1341 1342 /* Now we can use BW mode registers to figure out the bandwidth */ 1343 /* TODO: need to handle discovery too */ 1344 tunnel->bw_mode = true; 1345 return 0; 1346 } 1347 1348 /* Read cap from tunnel DP IN */ 1349 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate, 1350 u32 *lanes) 1351 { 1352 struct tb_port *in = tunnel->src_port; 1353 u32 val; 1354 int ret; 1355 1356 switch (cap) { 1357 case DP_LOCAL_CAP: 1358 case DP_REMOTE_CAP: 1359 case DP_COMMON_CAP: 1360 break; 1361 1362 default: 1363 tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap); 1364 return -EINVAL; 1365 } 1366 1367 /* 1368 * Read from the copied remote cap so that we take into account 1369 * if capabilities were reduced during exchange. 1370 */ 1371 ret = tb_port_read(in, &val, TB_CFG_PORT, in->cap_adap + cap, 1); 1372 if (ret) 1373 return ret; 1374 1375 *rate = tb_dp_cap_get_rate(val); 1376 *lanes = tb_dp_cap_get_lanes(val); 1377 return 0; 1378 } 1379 1380 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, 1381 int *max_down) 1382 { 1383 int ret; 1384 1385 if (!usb4_dp_port_bandwidth_mode_enabled(tunnel->src_port)) 1386 return -EOPNOTSUPP; 1387 1388 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL); 1389 if (ret < 0) 1390 return ret; 1391 1392 if (tb_tunnel_direction_downstream(tunnel)) { 1393 *max_up = 0; 1394 *max_down = ret; 1395 } else { 1396 *max_up = ret; 1397 *max_down = 0; 1398 } 1399 1400 return 0; 1401 } 1402 1403 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, 1404 int *consumed_down) 1405 { 1406 const struct tb_switch *sw = tunnel->src_port->sw; 1407 u32 rate = 0, lanes = 0; 1408 int ret; 1409 1410 if (tb_dp_is_usb4(sw)) { 1411 ret = tb_dp_wait_dprx(tunnel, 0); 1412 if (ret) { 1413 if (ret == -ETIMEDOUT) { 1414 /* 1415 * While we wait for DPRX complete the 1416 * tunnel consumes as much as it had 1417 * been reserved initially. 1418 */ 1419 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, 1420 &rate, &lanes); 1421 if (ret) 1422 return ret; 1423 } else { 1424 return ret; 1425 } 1426 } else { 1427 /* 1428 * On USB4 routers check if the bandwidth allocation 1429 * mode is enabled first and then read the bandwidth 1430 * through those registers. 1431 */ 1432 ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, 1433 consumed_down); 1434 if (ret < 0) { 1435 if (ret != -EOPNOTSUPP) 1436 return ret; 1437 } else if (!ret) { 1438 return 0; 1439 } 1440 ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes); 1441 if (ret) 1442 return ret; 1443 } 1444 } else if (sw->generation >= 2) { 1445 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes); 1446 if (ret) 1447 return ret; 1448 } else { 1449 /* No bandwidth management for legacy devices */ 1450 *consumed_up = 0; 1451 *consumed_down = 0; 1452 return 0; 1453 } 1454 1455 if (tb_tunnel_direction_downstream(tunnel)) { 1456 *consumed_up = 0; 1457 *consumed_down = tb_dp_bandwidth(rate, lanes); 1458 } else { 1459 *consumed_up = tb_dp_bandwidth(rate, lanes); 1460 *consumed_down = 0; 1461 } 1462 1463 return 0; 1464 } 1465 1466 static void tb_dp_init_aux_credits(struct tb_path_hop *hop) 1467 { 1468 struct tb_port *port = hop->in_port; 1469 struct tb_switch *sw = port->sw; 1470 1471 if (tb_port_use_credit_allocation(port)) 1472 hop->initial_credits = sw->min_dp_aux_credits; 1473 else 1474 hop->initial_credits = 1; 1475 } 1476 1477 static void tb_dp_init_aux_path(struct tb_path *path, bool pm_support) 1478 { 1479 struct tb_path_hop *hop; 1480 1481 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; 1482 path->egress_shared_buffer = TB_PATH_NONE; 1483 path->ingress_fc_enable = TB_PATH_ALL; 1484 path->ingress_shared_buffer = TB_PATH_NONE; 1485 path->priority = TB_DP_AUX_PRIORITY; 1486 path->weight = TB_DP_AUX_WEIGHT; 1487 1488 tb_path_for_each_hop(path, hop) { 1489 tb_dp_init_aux_credits(hop); 1490 if (pm_support) 1491 tb_init_pm_support(hop); 1492 } 1493 } 1494 1495 static int tb_dp_init_video_credits(struct tb_path_hop *hop) 1496 { 1497 struct tb_port *port = hop->in_port; 1498 struct tb_switch *sw = port->sw; 1499 1500 if (tb_port_use_credit_allocation(port)) { 1501 unsigned int nfc_credits; 1502 size_t max_dp_streams; 1503 1504 tb_available_credits(port, &max_dp_streams); 1505 /* 1506 * Read the number of currently allocated NFC credits 1507 * from the lane adapter. Since we only use them for DP 1508 * tunneling we can use that to figure out how many DP 1509 * tunnels already go through the lane adapter. 1510 */ 1511 nfc_credits = port->config.nfc_credits & 1512 ADP_CS_4_NFC_BUFFERS_MASK; 1513 if (nfc_credits / sw->min_dp_main_credits > max_dp_streams) 1514 return -ENOSPC; 1515 1516 hop->nfc_credits = sw->min_dp_main_credits; 1517 } else { 1518 hop->nfc_credits = min(port->total_credits - 2, 12U); 1519 } 1520 1521 return 0; 1522 } 1523 1524 static int tb_dp_init_video_path(struct tb_path *path, bool pm_support) 1525 { 1526 struct tb_path_hop *hop; 1527 1528 path->egress_fc_enable = TB_PATH_NONE; 1529 path->egress_shared_buffer = TB_PATH_NONE; 1530 path->ingress_fc_enable = TB_PATH_NONE; 1531 path->ingress_shared_buffer = TB_PATH_NONE; 1532 path->priority = TB_DP_VIDEO_PRIORITY; 1533 path->weight = TB_DP_VIDEO_WEIGHT; 1534 1535 tb_path_for_each_hop(path, hop) { 1536 int ret; 1537 1538 ret = tb_dp_init_video_credits(hop); 1539 if (ret) 1540 return ret; 1541 if (pm_support) 1542 tb_init_pm_support(hop); 1543 } 1544 1545 return 0; 1546 } 1547 1548 static void tb_dp_dump(struct tb_tunnel *tunnel) 1549 { 1550 struct tb_port *in, *out; 1551 u32 dp_cap, rate, lanes; 1552 1553 in = tunnel->src_port; 1554 out = tunnel->dst_port; 1555 1556 if (tb_port_read(in, &dp_cap, TB_CFG_PORT, 1557 in->cap_adap + DP_LOCAL_CAP, 1)) 1558 return; 1559 1560 rate = tb_dp_cap_get_rate(dp_cap); 1561 lanes = tb_dp_cap_get_lanes(dp_cap); 1562 1563 tb_tunnel_dbg(tunnel, 1564 "DP IN maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", 1565 rate, lanes, tb_dp_bandwidth(rate, lanes)); 1566 1567 if (tb_port_read(out, &dp_cap, TB_CFG_PORT, 1568 out->cap_adap + DP_LOCAL_CAP, 1)) 1569 return; 1570 1571 rate = tb_dp_cap_get_rate(dp_cap); 1572 lanes = tb_dp_cap_get_lanes(dp_cap); 1573 1574 tb_tunnel_dbg(tunnel, 1575 "DP OUT maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n", 1576 rate, lanes, tb_dp_bandwidth(rate, lanes)); 1577 1578 if (tb_port_read(in, &dp_cap, TB_CFG_PORT, 1579 in->cap_adap + DP_REMOTE_CAP, 1)) 1580 return; 1581 1582 rate = tb_dp_cap_get_rate(dp_cap); 1583 lanes = tb_dp_cap_get_lanes(dp_cap); 1584 1585 tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n", 1586 rate, lanes, tb_dp_bandwidth(rate, lanes)); 1587 } 1588 1589 /** 1590 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels 1591 * @tb: Pointer to the domain structure 1592 * @in: DP in adapter 1593 * @alloc_hopid: Allocate HopIDs from visited ports 1594 * @callback: Callback that is called when the DP tunnel is fully 1595 * activated (or there is an error) 1596 * @callback_data: Data for @callback 1597 * 1598 * If @in adapter is active, follows the tunnel to the DP out adapter 1599 * and back. Returns the discovered tunnel or %NULL if there was no 1600 * tunnel. See tb_tunnel_alloc_dp() for @callback. 1601 * 1602 * Return: Pointer to &struct tb_tunnel or %NULL if no tunnel found. 1603 */ 1604 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in, 1605 bool alloc_hopid, 1606 void (*callback)(struct tb_tunnel *, void *), 1607 void *callback_data) 1608 { 1609 struct tb_tunnel *tunnel; 1610 struct tb_port *port; 1611 struct tb_path *path; 1612 1613 if (WARN_ON(!callback)) 1614 return NULL; 1615 1616 if (!tb_dp_port_is_enabled(in)) 1617 return NULL; 1618 1619 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); 1620 if (!tunnel) 1621 return NULL; 1622 1623 tunnel->pre_activate = tb_dp_pre_activate; 1624 tunnel->activate = tb_dp_activate; 1625 tunnel->post_deactivate = tb_dp_post_deactivate; 1626 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; 1627 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; 1628 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; 1629 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; 1630 tunnel->src_port = in; 1631 tunnel->callback = callback; 1632 tunnel->callback_data = callback_data; 1633 INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work); 1634 1635 path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1, 1636 &tunnel->dst_port, "Video", alloc_hopid); 1637 if (!path) { 1638 /* Just disable the DP IN port */ 1639 tb_dp_port_enable(in, false); 1640 goto err_free; 1641 } 1642 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path; 1643 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false)) 1644 goto err_free; 1645 1646 path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX", 1647 alloc_hopid); 1648 if (!path) 1649 goto err_deactivate; 1650 tunnel->paths[TB_DP_AUX_PATH_OUT] = path; 1651 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false); 1652 1653 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID, 1654 &port, "AUX RX", alloc_hopid); 1655 if (!path) 1656 goto err_deactivate; 1657 tunnel->paths[TB_DP_AUX_PATH_IN] = path; 1658 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false); 1659 1660 /* Validate that the tunnel is complete */ 1661 if (!tb_port_is_dpout(tunnel->dst_port)) { 1662 tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n"); 1663 goto err_deactivate; 1664 } 1665 1666 if (!tb_dp_port_is_enabled(tunnel->dst_port)) 1667 goto err_deactivate; 1668 1669 if (!tb_dp_port_hpd_is_active(tunnel->dst_port)) 1670 goto err_deactivate; 1671 1672 if (port != tunnel->src_port) { 1673 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); 1674 goto err_deactivate; 1675 } 1676 1677 tb_dp_dump(tunnel); 1678 1679 tb_tunnel_set_active(tunnel, true); 1680 tb_tunnel_dbg(tunnel, "discovered\n"); 1681 return tunnel; 1682 1683 err_deactivate: 1684 tb_tunnel_deactivate(tunnel); 1685 err_free: 1686 tb_tunnel_put(tunnel); 1687 1688 return NULL; 1689 } 1690 1691 /** 1692 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel 1693 * @tb: Pointer to the domain structure 1694 * @in: DP in adapter port 1695 * @out: DP out adapter port 1696 * @link_nr: Preferred lane adapter when the link is not bonded 1697 * @max_up: Maximum available upstream bandwidth for the DP tunnel. 1698 * %0 if no available bandwidth. 1699 * @max_down: Maximum available downstream bandwidth for the DP tunnel. 1700 * %0 if no available bandwidth. 1701 * @callback: Callback that is called when the DP tunnel is fully 1702 * activated (or there is an error) 1703 * @callback_data: Data for @callback 1704 * 1705 * Allocates a tunnel between @in and @out that is capable of tunneling 1706 * Display Port traffic. The @callback is called after tb_tunnel_activate() 1707 * once the tunnel has been fully activated. It can call 1708 * tb_tunnel_is_active() to check if activation was successful (or if it 1709 * returns %false there was some sort of issue). The @callback is called 1710 * without @tb->lock held. 1711 * 1712 * Return: Pointer to @struct tb_tunnel or %NULL in case of failure. 1713 */ 1714 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in, 1715 struct tb_port *out, int link_nr, 1716 int max_up, int max_down, 1717 void (*callback)(struct tb_tunnel *, void *), 1718 void *callback_data) 1719 { 1720 struct tb_tunnel *tunnel; 1721 struct tb_path **paths; 1722 struct tb_path *path; 1723 bool pm_support; 1724 1725 if (WARN_ON(!in->cap_adap || !out->cap_adap || !callback)) 1726 return NULL; 1727 1728 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); 1729 if (!tunnel) 1730 return NULL; 1731 1732 tunnel->pre_activate = tb_dp_pre_activate; 1733 tunnel->activate = tb_dp_activate; 1734 tunnel->post_deactivate = tb_dp_post_deactivate; 1735 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; 1736 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; 1737 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; 1738 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; 1739 tunnel->src_port = in; 1740 tunnel->dst_port = out; 1741 tunnel->max_up = max_up; 1742 tunnel->max_down = max_down; 1743 tunnel->callback = callback; 1744 tunnel->callback_data = callback_data; 1745 INIT_DELAYED_WORK(&tunnel->dprx_work, tb_dp_dprx_work); 1746 1747 paths = tunnel->paths; 1748 pm_support = usb4_switch_version(in->sw) >= 2; 1749 1750 path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID, 1751 link_nr, "Video"); 1752 if (!path) 1753 goto err_free; 1754 tb_dp_init_video_path(path, pm_support); 1755 paths[TB_DP_VIDEO_PATH_OUT] = path; 1756 1757 path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out, 1758 TB_DP_AUX_TX_HOPID, link_nr, "AUX TX"); 1759 if (!path) 1760 goto err_free; 1761 tb_dp_init_aux_path(path, pm_support); 1762 paths[TB_DP_AUX_PATH_OUT] = path; 1763 1764 path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in, 1765 TB_DP_AUX_RX_HOPID, link_nr, "AUX RX"); 1766 if (!path) 1767 goto err_free; 1768 tb_dp_init_aux_path(path, pm_support); 1769 paths[TB_DP_AUX_PATH_IN] = path; 1770 1771 return tunnel; 1772 1773 err_free: 1774 tb_tunnel_put(tunnel); 1775 return NULL; 1776 } 1777 1778 static unsigned int tb_dma_available_credits(const struct tb_port *port) 1779 { 1780 const struct tb_switch *sw = port->sw; 1781 int credits; 1782 1783 credits = tb_available_credits(port, NULL); 1784 if (tb_acpi_may_tunnel_pcie()) 1785 credits -= sw->max_pcie_credits; 1786 credits -= port->dma_credits; 1787 1788 return credits > 0 ? credits : 0; 1789 } 1790 1791 static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits) 1792 { 1793 struct tb_port *port = hop->in_port; 1794 1795 if (tb_port_use_credit_allocation(port)) { 1796 unsigned int available = tb_dma_available_credits(port); 1797 1798 /* 1799 * Need to have at least TB_MIN_DMA_CREDITS, otherwise 1800 * DMA path cannot be established. 1801 */ 1802 if (available < TB_MIN_DMA_CREDITS) 1803 return -ENOSPC; 1804 1805 credits = min(credits, available); 1806 1807 tb_port_dbg(port, "reserving %u credits for DMA path\n", 1808 credits); 1809 1810 port->dma_credits += credits; 1811 } else { 1812 if (tb_port_is_null(port)) 1813 credits = port->bonded ? 14 : 6; 1814 else 1815 credits = min(port->total_credits, credits); 1816 } 1817 1818 hop->initial_credits = credits; 1819 return 0; 1820 } 1821 1822 /* Path from lane adapter to NHI */ 1823 static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits) 1824 { 1825 struct tb_path_hop *hop; 1826 unsigned int i, tmp; 1827 1828 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; 1829 path->ingress_fc_enable = TB_PATH_ALL; 1830 path->egress_shared_buffer = TB_PATH_NONE; 1831 path->ingress_shared_buffer = TB_PATH_NONE; 1832 path->priority = TB_DMA_PRIORITY; 1833 path->weight = TB_DMA_WEIGHT; 1834 path->clear_fc = true; 1835 1836 /* 1837 * First lane adapter is the one connected to the remote host. 1838 * We don't tunnel other traffic over this link so we can use 1839 * all the credits (except the ones reserved for control traffic). 1840 */ 1841 hop = &path->hops[0]; 1842 tmp = min(tb_usable_credits(hop->in_port), credits); 1843 hop->initial_credits = tmp; 1844 hop->in_port->dma_credits += tmp; 1845 1846 for (i = 1; i < path->path_length; i++) { 1847 int ret; 1848 1849 ret = tb_dma_reserve_credits(&path->hops[i], credits); 1850 if (ret) 1851 return ret; 1852 } 1853 1854 return 0; 1855 } 1856 1857 /* Path from NHI to lane adapter */ 1858 static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits) 1859 { 1860 struct tb_path_hop *hop; 1861 1862 path->egress_fc_enable = TB_PATH_ALL; 1863 path->ingress_fc_enable = TB_PATH_ALL; 1864 path->egress_shared_buffer = TB_PATH_NONE; 1865 path->ingress_shared_buffer = TB_PATH_NONE; 1866 path->priority = TB_DMA_PRIORITY; 1867 path->weight = TB_DMA_WEIGHT; 1868 path->clear_fc = true; 1869 1870 tb_path_for_each_hop(path, hop) { 1871 int ret; 1872 1873 ret = tb_dma_reserve_credits(hop, credits); 1874 if (ret) 1875 return ret; 1876 } 1877 1878 return 0; 1879 } 1880 1881 static void tb_dma_release_credits(struct tb_path_hop *hop) 1882 { 1883 struct tb_port *port = hop->in_port; 1884 1885 if (tb_port_use_credit_allocation(port)) { 1886 port->dma_credits -= hop->initial_credits; 1887 1888 tb_port_dbg(port, "released %u DMA path credits\n", 1889 hop->initial_credits); 1890 } 1891 } 1892 1893 static void tb_dma_destroy_path(struct tb_path *path) 1894 { 1895 struct tb_path_hop *hop; 1896 1897 tb_path_for_each_hop(path, hop) 1898 tb_dma_release_credits(hop); 1899 } 1900 1901 static void tb_dma_destroy(struct tb_tunnel *tunnel) 1902 { 1903 int i; 1904 1905 for (i = 0; i < tunnel->npaths; i++) { 1906 if (!tunnel->paths[i]) 1907 continue; 1908 tb_dma_destroy_path(tunnel->paths[i]); 1909 } 1910 } 1911 1912 /** 1913 * tb_tunnel_alloc_dma() - allocate a DMA tunnel 1914 * @tb: Pointer to the domain structure 1915 * @nhi: Host controller port 1916 * @dst: Destination null port which the other domain is connected to 1917 * @transmit_path: HopID used for transmitting packets 1918 * @transmit_ring: NHI ring number used to send packets towards the 1919 * other domain. Set to %-1 if TX path is not needed. 1920 * @receive_path: HopID used for receiving packets 1921 * @receive_ring: NHI ring number used to receive packets from the 1922 * other domain. Set to %-1 if RX path is not needed. 1923 * 1924 * Return: Pointer to @struct tb_tunnel or %NULL in case of failure. 1925 */ 1926 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi, 1927 struct tb_port *dst, int transmit_path, 1928 int transmit_ring, int receive_path, 1929 int receive_ring) 1930 { 1931 struct tb_tunnel *tunnel; 1932 size_t npaths = 0, i = 0; 1933 struct tb_path *path; 1934 unsigned int credits; 1935 1936 /* Ring 0 is reserved for control channel */ 1937 if (WARN_ON(!receive_ring || !transmit_ring)) 1938 return NULL; 1939 1940 if (receive_ring > 0) 1941 npaths++; 1942 if (transmit_ring > 0) 1943 npaths++; 1944 1945 if (WARN_ON(!npaths)) 1946 return NULL; 1947 1948 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA); 1949 if (!tunnel) 1950 return NULL; 1951 1952 tunnel->src_port = nhi; 1953 tunnel->dst_port = dst; 1954 tunnel->destroy = tb_dma_destroy; 1955 1956 credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits); 1957 if (credits > TB_MAX_CREDITS) { 1958 tb_tunnel_dbg(tunnel, "%u credits do not fit a hop, using %u\n", 1959 credits, TB_MAX_CREDITS); 1960 credits = TB_MAX_CREDITS; 1961 } 1962 1963 if (receive_ring > 0) { 1964 path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0, 1965 "DMA RX"); 1966 if (!path) 1967 goto err_free; 1968 tunnel->paths[i++] = path; 1969 if (tb_dma_init_rx_path(path, credits)) { 1970 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n"); 1971 goto err_free; 1972 } 1973 } 1974 1975 if (transmit_ring > 0) { 1976 path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0, 1977 "DMA TX"); 1978 if (!path) 1979 goto err_free; 1980 tunnel->paths[i++] = path; 1981 if (tb_dma_init_tx_path(path, credits)) { 1982 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n"); 1983 goto err_free; 1984 } 1985 } 1986 1987 return tunnel; 1988 1989 err_free: 1990 tb_tunnel_put(tunnel); 1991 return NULL; 1992 } 1993 1994 /** 1995 * tb_tunnel_match_dma() - Match DMA tunnel 1996 * @tunnel: Tunnel to match 1997 * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore. 1998 * @transmit_ring: NHI ring number used to send packets towards the 1999 * other domain. Pass %-1 to ignore. 2000 * @receive_path: HopID used for receiving packets. Pass %-1 to ignore. 2001 * @receive_ring: NHI ring number used to receive packets from the 2002 * other domain. Pass %-1 to ignore. 2003 * 2004 * This function can be used to match specific DMA tunnel, if there are 2005 * multiple DMA tunnels going through the same XDomain connection. 2006 * 2007 * Return: %true if there is a match, %false otherwise. 2008 */ 2009 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, 2010 int transmit_ring, int receive_path, int receive_ring) 2011 { 2012 const struct tb_path *tx_path = NULL, *rx_path = NULL; 2013 int i; 2014 2015 if (!receive_ring || !transmit_ring) 2016 return false; 2017 2018 for (i = 0; i < tunnel->npaths; i++) { 2019 const struct tb_path *path = tunnel->paths[i]; 2020 2021 if (!path) 2022 continue; 2023 2024 if (tb_port_is_nhi(path->hops[0].in_port)) 2025 tx_path = path; 2026 else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port)) 2027 rx_path = path; 2028 } 2029 2030 if (transmit_ring > 0 || transmit_path > 0) { 2031 if (!tx_path) 2032 return false; 2033 if (transmit_ring > 0 && 2034 (tx_path->hops[0].in_hop_index != transmit_ring)) 2035 return false; 2036 if (transmit_path > 0 && 2037 (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path)) 2038 return false; 2039 } 2040 2041 if (receive_ring > 0 || receive_path > 0) { 2042 if (!rx_path) 2043 return false; 2044 if (receive_path > 0 && 2045 (rx_path->hops[0].in_hop_index != receive_path)) 2046 return false; 2047 if (receive_ring > 0 && 2048 (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring)) 2049 return false; 2050 } 2051 2052 return true; 2053 } 2054 2055 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down) 2056 { 2057 int ret, up_max_rate, down_max_rate; 2058 2059 ret = usb4_usb3_port_max_link_rate(up); 2060 if (ret < 0) 2061 return ret; 2062 up_max_rate = ret; 2063 2064 ret = usb4_usb3_port_max_link_rate(down); 2065 if (ret < 0) 2066 return ret; 2067 down_max_rate = ret; 2068 2069 return min(up_max_rate, down_max_rate); 2070 } 2071 2072 static int tb_usb3_pre_activate(struct tb_tunnel *tunnel) 2073 { 2074 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", 2075 tunnel->allocated_up, tunnel->allocated_down); 2076 2077 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port, 2078 &tunnel->allocated_up, 2079 &tunnel->allocated_down); 2080 } 2081 2082 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate) 2083 { 2084 int res; 2085 2086 res = tb_usb3_port_enable(tunnel->src_port, activate); 2087 if (res) 2088 return res; 2089 2090 if (tb_port_is_usb3_up(tunnel->dst_port)) 2091 return tb_usb3_port_enable(tunnel->dst_port, activate); 2092 2093 return 0; 2094 } 2095 2096 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel, 2097 int *consumed_up, int *consumed_down) 2098 { 2099 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); 2100 int pcie_weight = tb_acpi_may_tunnel_pcie() ? TB_PCI_WEIGHT : 0; 2101 2102 /* 2103 * PCIe tunneling, if enabled, affects the USB3 bandwidth so 2104 * take that into account here. 2105 */ 2106 *consumed_up = tunnel->allocated_up * 2107 (TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT; 2108 *consumed_down = tunnel->allocated_down * 2109 (TB_USB3_WEIGHT + pcie_weight) / TB_USB3_WEIGHT; 2110 2111 if (tb_port_get_link_generation(port) >= 4) { 2112 *consumed_up = max(*consumed_up, USB4_V2_USB3_MIN_BANDWIDTH); 2113 *consumed_down = max(*consumed_down, USB4_V2_USB3_MIN_BANDWIDTH); 2114 } 2115 2116 return 0; 2117 } 2118 2119 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel) 2120 { 2121 int ret; 2122 2123 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port, 2124 &tunnel->allocated_up, 2125 &tunnel->allocated_down); 2126 if (ret) 2127 return ret; 2128 2129 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n", 2130 tunnel->allocated_up, tunnel->allocated_down); 2131 return 0; 2132 } 2133 2134 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel, 2135 int *available_up, 2136 int *available_down) 2137 { 2138 int ret, max_rate, allocate_up, allocate_down; 2139 2140 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port); 2141 if (ret < 0) { 2142 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n"); 2143 return; 2144 } 2145 2146 /* 2147 * 90% of the max rate can be allocated for isochronous 2148 * transfers. 2149 */ 2150 max_rate = ret * 90 / 100; 2151 2152 /* No need to reclaim if already at maximum */ 2153 if (tunnel->allocated_up >= max_rate && 2154 tunnel->allocated_down >= max_rate) 2155 return; 2156 2157 /* Don't go lower than what is already allocated */ 2158 allocate_up = min(max_rate, *available_up); 2159 if (allocate_up < tunnel->allocated_up) 2160 allocate_up = tunnel->allocated_up; 2161 2162 allocate_down = min(max_rate, *available_down); 2163 if (allocate_down < tunnel->allocated_down) 2164 allocate_down = tunnel->allocated_down; 2165 2166 /* If no changes no need to do more */ 2167 if (allocate_up == tunnel->allocated_up && 2168 allocate_down == tunnel->allocated_down) 2169 return; 2170 2171 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up, 2172 &allocate_down); 2173 if (ret) { 2174 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n"); 2175 return; 2176 } 2177 2178 tunnel->allocated_up = allocate_up; 2179 *available_up -= tunnel->allocated_up; 2180 2181 tunnel->allocated_down = allocate_down; 2182 *available_down -= tunnel->allocated_down; 2183 2184 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n", 2185 tunnel->allocated_up, tunnel->allocated_down); 2186 } 2187 2188 static void tb_usb3_init_credits(struct tb_path_hop *hop) 2189 { 2190 struct tb_port *port = hop->in_port; 2191 struct tb_switch *sw = port->sw; 2192 unsigned int credits; 2193 2194 if (tb_port_use_credit_allocation(port)) { 2195 credits = sw->max_usb3_credits; 2196 } else { 2197 if (tb_port_is_null(port)) 2198 credits = port->bonded ? 32 : 16; 2199 else 2200 credits = 7; 2201 } 2202 2203 hop->initial_credits = credits; 2204 } 2205 2206 static void tb_usb3_init_path(struct tb_path *path) 2207 { 2208 struct tb_path_hop *hop; 2209 2210 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; 2211 path->egress_shared_buffer = TB_PATH_NONE; 2212 path->ingress_fc_enable = TB_PATH_ALL; 2213 path->ingress_shared_buffer = TB_PATH_NONE; 2214 path->priority = TB_USB3_PRIORITY; 2215 path->weight = TB_USB3_WEIGHT; 2216 path->drop_packages = 0; 2217 2218 tb_path_for_each_hop(path, hop) 2219 tb_usb3_init_credits(hop); 2220 } 2221 2222 /** 2223 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels 2224 * @tb: Pointer to the domain structure 2225 * @down: USB3 downstream adapter 2226 * @alloc_hopid: Allocate HopIDs from visited ports 2227 * 2228 * If @down adapter is active, follows the tunnel to the USB3 upstream 2229 * adapter and back. 2230 * 2231 * Return: Pointer to &struct tb_tunnel or %NULL if there was no tunnel. 2232 */ 2233 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down, 2234 bool alloc_hopid) 2235 { 2236 struct tb_tunnel *tunnel; 2237 struct tb_path *path; 2238 2239 if (!tb_usb3_port_is_enabled(down)) 2240 return NULL; 2241 2242 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); 2243 if (!tunnel) 2244 return NULL; 2245 2246 tunnel->activate = tb_usb3_activate; 2247 tunnel->src_port = down; 2248 2249 /* 2250 * Discover both paths even if they are not complete. We will 2251 * clean them up by calling tb_tunnel_deactivate() below in that 2252 * case. 2253 */ 2254 path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1, 2255 &tunnel->dst_port, "USB3 Down", alloc_hopid); 2256 if (!path) { 2257 /* Just disable the downstream port */ 2258 tb_usb3_port_enable(down, false); 2259 goto err_free; 2260 } 2261 tunnel->paths[TB_USB3_PATH_DOWN] = path; 2262 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]); 2263 2264 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL, 2265 "USB3 Up", alloc_hopid); 2266 if (!path) 2267 goto err_deactivate; 2268 tunnel->paths[TB_USB3_PATH_UP] = path; 2269 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]); 2270 2271 /* Validate that the tunnel is complete */ 2272 if (!tb_port_is_usb3_up(tunnel->dst_port)) { 2273 tb_port_warn(tunnel->dst_port, 2274 "path does not end on an USB3 adapter, cleaning up\n"); 2275 goto err_deactivate; 2276 } 2277 2278 if (down != tunnel->src_port) { 2279 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); 2280 goto err_deactivate; 2281 } 2282 2283 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { 2284 tb_tunnel_warn(tunnel, 2285 "tunnel is not fully activated, cleaning up\n"); 2286 goto err_deactivate; 2287 } 2288 2289 if (!tb_route(down->sw)) { 2290 int ret; 2291 2292 /* 2293 * Read the initial bandwidth allocation for the first 2294 * hop tunnel. 2295 */ 2296 ret = usb4_usb3_port_allocated_bandwidth(down, 2297 &tunnel->allocated_up, &tunnel->allocated_down); 2298 if (ret) 2299 goto err_deactivate; 2300 2301 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", 2302 tunnel->allocated_up, tunnel->allocated_down); 2303 2304 tunnel->pre_activate = tb_usb3_pre_activate; 2305 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; 2306 tunnel->release_unused_bandwidth = 2307 tb_usb3_release_unused_bandwidth; 2308 tunnel->reclaim_available_bandwidth = 2309 tb_usb3_reclaim_available_bandwidth; 2310 } 2311 2312 tb_tunnel_set_active(tunnel, true); 2313 tb_tunnel_dbg(tunnel, "discovered\n"); 2314 return tunnel; 2315 2316 err_deactivate: 2317 tb_tunnel_deactivate(tunnel); 2318 err_free: 2319 tb_tunnel_put(tunnel); 2320 2321 return NULL; 2322 } 2323 2324 /** 2325 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel 2326 * @tb: Pointer to the domain structure 2327 * @up: USB3 upstream adapter port 2328 * @down: USB3 downstream adapter port 2329 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel. 2330 * %0 if no available bandwidth. 2331 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel. 2332 * %0 if no available bandwidth. 2333 * 2334 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and 2335 * @TB_TYPE_USB3_DOWN. 2336 * 2337 * Return: Pointer to @struct tb_tunnel or %NULL in case of failure. 2338 */ 2339 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up, 2340 struct tb_port *down, int max_up, 2341 int max_down) 2342 { 2343 struct tb_tunnel *tunnel; 2344 struct tb_path *path; 2345 int max_rate = 0; 2346 2347 if (!tb_route(down->sw) && (max_up > 0 || max_down > 0)) { 2348 /* 2349 * For USB3 isochronous transfers, we allow bandwidth which is 2350 * not higher than 90% of maximum supported bandwidth by USB3 2351 * adapters. 2352 */ 2353 max_rate = tb_usb3_max_link_rate(down, up); 2354 if (max_rate < 0) 2355 return NULL; 2356 2357 max_rate = max_rate * 90 / 100; 2358 tb_port_dbg(up, "maximum required bandwidth for USB3 tunnel %d Mb/s\n", 2359 max_rate); 2360 } 2361 2362 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); 2363 if (!tunnel) 2364 return NULL; 2365 2366 tunnel->activate = tb_usb3_activate; 2367 tunnel->src_port = down; 2368 tunnel->dst_port = up; 2369 tunnel->max_up = max_up; 2370 tunnel->max_down = max_down; 2371 2372 path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0, 2373 "USB3 Down"); 2374 if (!path) 2375 goto err_free; 2376 tb_usb3_init_path(path); 2377 tunnel->paths[TB_USB3_PATH_DOWN] = path; 2378 2379 path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0, 2380 "USB3 Up"); 2381 if (!path) 2382 goto err_free; 2383 tb_usb3_init_path(path); 2384 tunnel->paths[TB_USB3_PATH_UP] = path; 2385 2386 if (!tb_route(down->sw)) { 2387 tunnel->allocated_up = min(max_rate, max_up); 2388 tunnel->allocated_down = min(max_rate, max_down); 2389 2390 tunnel->pre_activate = tb_usb3_pre_activate; 2391 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; 2392 tunnel->release_unused_bandwidth = 2393 tb_usb3_release_unused_bandwidth; 2394 tunnel->reclaim_available_bandwidth = 2395 tb_usb3_reclaim_available_bandwidth; 2396 } 2397 2398 return tunnel; 2399 2400 err_free: 2401 tb_tunnel_put(tunnel); 2402 return NULL; 2403 } 2404 2405 /** 2406 * tb_tunnel_is_invalid - check whether an activated path is still valid 2407 * @tunnel: Tunnel to check 2408 * 2409 * Return: %true if path is valid, %false otherwise. 2410 */ 2411 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) 2412 { 2413 int i; 2414 2415 for (i = 0; i < tunnel->npaths; i++) { 2416 WARN_ON(!tunnel->paths[i]->activated); 2417 if (tb_path_is_invalid(tunnel->paths[i])) 2418 return true; 2419 } 2420 2421 return false; 2422 } 2423 2424 /** 2425 * tb_tunnel_activate() - activate a tunnel 2426 * @tunnel: Tunnel to activate 2427 * 2428 * Return: 2429 * * %0 - On success. 2430 * * %-EINPROGRESS - If the tunnel activation is still in progress (that's 2431 * for DP tunnels to complete DPRX capabilities read). 2432 * * Negative errno - Another error occurred. 2433 */ 2434 int tb_tunnel_activate(struct tb_tunnel *tunnel) 2435 { 2436 int res, i; 2437 2438 tb_tunnel_dbg(tunnel, "activating\n"); 2439 2440 /* 2441 * Make sure all paths are properly disabled before enabling 2442 * them again. 2443 */ 2444 for (i = 0; i < tunnel->npaths; i++) { 2445 if (tunnel->paths[i]->activated) { 2446 tb_path_deactivate(tunnel->paths[i]); 2447 tunnel->paths[i]->activated = false; 2448 } 2449 } 2450 2451 tunnel->state = TB_TUNNEL_ACTIVATING; 2452 2453 if (tunnel->pre_activate) { 2454 res = tunnel->pre_activate(tunnel); 2455 if (res) 2456 return res; 2457 } 2458 2459 for (i = 0; i < tunnel->npaths; i++) { 2460 res = tb_path_activate(tunnel->paths[i]); 2461 if (res) 2462 goto err; 2463 } 2464 2465 if (tunnel->activate) { 2466 res = tunnel->activate(tunnel, true); 2467 if (res) { 2468 if (res == -EINPROGRESS) 2469 return res; 2470 goto err; 2471 } 2472 } 2473 2474 tb_tunnel_set_active(tunnel, true); 2475 return 0; 2476 2477 err: 2478 tb_tunnel_warn(tunnel, "activation failed\n"); 2479 tb_tunnel_deactivate(tunnel); 2480 return res; 2481 } 2482 2483 /** 2484 * tb_tunnel_deactivate() - deactivate a tunnel 2485 * @tunnel: Tunnel to deactivate 2486 */ 2487 void tb_tunnel_deactivate(struct tb_tunnel *tunnel) 2488 { 2489 int i; 2490 2491 tb_tunnel_dbg(tunnel, "deactivating\n"); 2492 2493 if (tunnel->activate) 2494 tunnel->activate(tunnel, false); 2495 2496 for (i = 0; i < tunnel->npaths; i++) { 2497 if (tunnel->paths[i] && tunnel->paths[i]->activated) 2498 tb_path_deactivate(tunnel->paths[i]); 2499 } 2500 2501 if (tunnel->post_deactivate) 2502 tunnel->post_deactivate(tunnel); 2503 2504 tb_tunnel_set_active(tunnel, false); 2505 } 2506 2507 /** 2508 * tb_tunnel_port_on_path() - Does the tunnel go through port 2509 * @tunnel: Tunnel to check 2510 * @port: Port to check 2511 * 2512 * Return: %true if @tunnel goes through @port (direction does not matter), 2513 * %false otherwise. 2514 */ 2515 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, 2516 const struct tb_port *port) 2517 { 2518 int i; 2519 2520 for (i = 0; i < tunnel->npaths; i++) { 2521 if (!tunnel->paths[i]) 2522 continue; 2523 2524 if (tb_path_port_on_path(tunnel->paths[i], port)) 2525 return true; 2526 } 2527 2528 return false; 2529 } 2530 2531 // Is tb_tunnel_activate() called for the tunnel 2532 static bool tb_tunnel_is_activated(const struct tb_tunnel *tunnel) 2533 { 2534 return tunnel->state == TB_TUNNEL_ACTIVATING || tb_tunnel_is_active(tunnel); 2535 } 2536 2537 /** 2538 * tb_tunnel_maximum_bandwidth() - Return maximum possible bandwidth 2539 * @tunnel: Tunnel to check 2540 * @max_up: Maximum upstream bandwidth in Mb/s 2541 * @max_down: Maximum downstream bandwidth in Mb/s 2542 * 2543 * Return: 2544 * * Maximum possible bandwidth this tunnel can support if not 2545 * limited by other bandwidth clients. 2546 * * %-EOPNOTSUPP - If the tunnel does not support this function. 2547 * * %-ENOTCONN - If the tunnel is not active. 2548 */ 2549 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, 2550 int *max_down) 2551 { 2552 if (!tb_tunnel_is_active(tunnel)) 2553 return -ENOTCONN; 2554 2555 if (tunnel->maximum_bandwidth) 2556 return tunnel->maximum_bandwidth(tunnel, max_up, max_down); 2557 return -EOPNOTSUPP; 2558 } 2559 2560 /** 2561 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel 2562 * @tunnel: Tunnel to check 2563 * @allocated_up: Currently allocated upstream bandwidth in Mb/s is stored here 2564 * @allocated_down: Currently allocated downstream bandwidth in Mb/s is 2565 * stored here 2566 * 2567 * Return: 2568 * * Bandwidth allocated for the tunnel. This may be higher than what the 2569 * tunnel actually consumes. 2570 * * %-EOPNOTSUPP - If the tunnel does not support this function. 2571 * * %-ENOTCONN - If the tunnel is not active. 2572 * * Negative errno - Another error occurred. 2573 */ 2574 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, 2575 int *allocated_down) 2576 { 2577 if (!tb_tunnel_is_active(tunnel)) 2578 return -ENOTCONN; 2579 2580 if (tunnel->allocated_bandwidth) 2581 return tunnel->allocated_bandwidth(tunnel, allocated_up, 2582 allocated_down); 2583 return -EOPNOTSUPP; 2584 } 2585 2586 /** 2587 * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation 2588 * @tunnel: Tunnel whose bandwidth allocation to change 2589 * @alloc_up: New upstream bandwidth in Mb/s 2590 * @alloc_down: New downstream bandwidth in Mb/s 2591 * 2592 * Tries to change tunnel bandwidth allocation. 2593 * 2594 * Return: 2595 * * %0 - On success. Updates @alloc_up and @alloc_down to values that were 2596 * actually allocated (it may not be the same as passed originally). 2597 * * Negative errno - In case of failure. 2598 */ 2599 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, 2600 int *alloc_down) 2601 { 2602 if (!tb_tunnel_is_active(tunnel)) 2603 return -ENOTCONN; 2604 2605 if (tunnel->alloc_bandwidth) { 2606 int ret; 2607 2608 ret = tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down); 2609 if (ret) 2610 return ret; 2611 2612 tb_tunnel_changed(tunnel); 2613 return 0; 2614 } 2615 2616 return -EOPNOTSUPP; 2617 } 2618 2619 /** 2620 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel 2621 * @tunnel: Tunnel to check 2622 * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port. 2623 * Can be %NULL. 2624 * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port. 2625 * Can be %NULL. 2626 * 2627 * Stores the amount of isochronous bandwidth @tunnel consumes in 2628 * @consumed_up and @consumed_down. 2629 * 2630 * Return: %0 on success, negative errno otherwise. 2631 */ 2632 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, 2633 int *consumed_down) 2634 { 2635 int up_bw = 0, down_bw = 0; 2636 2637 /* 2638 * Here we need to distinguish between not active tunnel from 2639 * tunnels that are either fully active or activation started. 2640 * The latter is true for DP tunnels where we must report the 2641 * consumed to be the maximum we gave it until DPRX capabilities 2642 * read is done by the graphics driver. 2643 */ 2644 if (tb_tunnel_is_activated(tunnel) && tunnel->consumed_bandwidth) { 2645 int ret; 2646 2647 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); 2648 if (ret) 2649 return ret; 2650 } 2651 2652 if (consumed_up) 2653 *consumed_up = up_bw; 2654 if (consumed_down) 2655 *consumed_down = down_bw; 2656 2657 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, down_bw); 2658 return 0; 2659 } 2660 2661 /** 2662 * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth 2663 * @tunnel: Tunnel whose unused bandwidth to release 2664 * 2665 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the 2666 * moment) this function makes it release all the unused bandwidth. 2667 * 2668 * Return: %0 on success, negative errno otherwise. 2669 */ 2670 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) 2671 { 2672 if (!tb_tunnel_is_active(tunnel)) 2673 return -ENOTCONN; 2674 2675 if (tunnel->release_unused_bandwidth) { 2676 int ret; 2677 2678 ret = tunnel->release_unused_bandwidth(tunnel); 2679 if (ret) 2680 return ret; 2681 } 2682 2683 return 0; 2684 } 2685 2686 /** 2687 * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth 2688 * @tunnel: Tunnel reclaiming available bandwidth 2689 * @available_up: Available upstream bandwidth (in Mb/s) 2690 * @available_down: Available downstream bandwidth (in Mb/s) 2691 * 2692 * Reclaims bandwidth from @available_up and @available_down and updates 2693 * the variables accordingly (e.g decreases both according to what was 2694 * reclaimed by the tunnel). If nothing was reclaimed the values are 2695 * kept as is. 2696 */ 2697 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, 2698 int *available_up, 2699 int *available_down) 2700 { 2701 if (!tb_tunnel_is_active(tunnel)) 2702 return; 2703 2704 if (tunnel->reclaim_available_bandwidth) 2705 tunnel->reclaim_available_bandwidth(tunnel, available_up, 2706 available_down); 2707 } 2708 2709 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel) 2710 { 2711 return tb_tunnel_names[tunnel->type]; 2712 } 2713