Lines Matching full:tunnel

15 #include "tunnel.h"
108 /* Add some credits for potential second DMA tunnel */ in tb_available_credits()
150 struct tb_tunnel *tunnel; in tb_tunnel_alloc() local
152 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); in tb_tunnel_alloc()
153 if (!tunnel) in tb_tunnel_alloc()
156 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); in tb_tunnel_alloc()
157 if (!tunnel->paths) { in tb_tunnel_alloc()
158 tb_tunnel_free(tunnel); in tb_tunnel_alloc()
162 INIT_LIST_HEAD(&tunnel->list); in tb_tunnel_alloc()
163 tunnel->tb = tb; in tb_tunnel_alloc()
164 tunnel->npaths = npaths; in tb_tunnel_alloc()
165 tunnel->type = type; in tb_tunnel_alloc()
167 return tunnel; in tb_tunnel_alloc()
170 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable) in tb_pci_set_ext_encapsulation() argument
172 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); in tb_pci_set_ext_encapsulation()
176 if ((usb4_switch_version(tunnel->src_port->sw) < 2) || in tb_pci_set_ext_encapsulation()
177 (usb4_switch_version(tunnel->dst_port->sw) < 2)) in tb_pci_set_ext_encapsulation()
183 ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable); in tb_pci_set_ext_encapsulation()
191 ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable); in tb_pci_set_ext_encapsulation()
199 tb_tunnel_dbg(tunnel, "extended encapsulation %s\n", in tb_pci_set_ext_encapsulation()
204 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) in tb_pci_activate() argument
209 res = tb_pci_set_ext_encapsulation(tunnel, activate); in tb_pci_activate()
215 res = tb_pci_port_enable(tunnel->dst_port, activate); in tb_pci_activate()
217 res = tb_pci_port_enable(tunnel->src_port, activate); in tb_pci_activate()
223 res = tb_pci_port_enable(tunnel->src_port, activate); in tb_pci_activate()
228 tb_pci_port_enable(tunnel->dst_port, activate); in tb_pci_activate()
231 return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate); in tb_pci_activate()
290 * If @down adapter is active, follows the tunnel to the PCIe upstream
291 * adapter and back. Returns the discovered tunnel or %NULL if there was
292 * no tunnel.
297 struct tb_tunnel *tunnel; in tb_tunnel_discover_pci() local
303 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); in tb_tunnel_discover_pci()
304 if (!tunnel) in tb_tunnel_discover_pci()
307 tunnel->activate = tb_pci_activate; in tb_tunnel_discover_pci()
308 tunnel->src_port = down; in tb_tunnel_discover_pci()
316 &tunnel->dst_port, "PCIe Up", alloc_hopid); in tb_tunnel_discover_pci()
322 tunnel->paths[TB_PCI_PATH_UP] = path; in tb_tunnel_discover_pci()
323 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP])) in tb_tunnel_discover_pci()
326 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL, in tb_tunnel_discover_pci()
330 tunnel->paths[TB_PCI_PATH_DOWN] = path; in tb_tunnel_discover_pci()
331 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN])) in tb_tunnel_discover_pci()
334 /* Validate that the tunnel is complete */ in tb_tunnel_discover_pci()
335 if (!tb_port_is_pcie_up(tunnel->dst_port)) { in tb_tunnel_discover_pci()
336 tb_port_warn(tunnel->dst_port, in tb_tunnel_discover_pci()
341 if (down != tunnel->src_port) { in tb_tunnel_discover_pci()
342 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_pci()
346 if (!tb_pci_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_pci()
347 tb_tunnel_warn(tunnel, in tb_tunnel_discover_pci()
348 "tunnel is not fully activated, cleaning up\n"); in tb_tunnel_discover_pci()
352 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_pci()
353 return tunnel; in tb_tunnel_discover_pci()
356 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_pci()
358 tb_tunnel_free(tunnel); in tb_tunnel_discover_pci()
364 * tb_tunnel_alloc_pci() - allocate a pci tunnel
369 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
377 struct tb_tunnel *tunnel; in tb_tunnel_alloc_pci() local
380 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); in tb_tunnel_alloc_pci()
381 if (!tunnel) in tb_tunnel_alloc_pci()
384 tunnel->activate = tb_pci_activate; in tb_tunnel_alloc_pci()
385 tunnel->src_port = down; in tb_tunnel_alloc_pci()
386 tunnel->dst_port = up; in tb_tunnel_alloc_pci()
392 tunnel->paths[TB_PCI_PATH_DOWN] = path; in tb_tunnel_alloc_pci()
400 tunnel->paths[TB_PCI_PATH_UP] = path; in tb_tunnel_alloc_pci()
404 return tunnel; in tb_tunnel_alloc_pci()
407 tb_tunnel_free(tunnel); in tb_tunnel_alloc_pci()
653 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel) in tb_dp_xchg_caps() argument
656 struct tb_port *out = tunnel->dst_port; in tb_dp_xchg_caps()
657 struct tb_port *in = tunnel->src_port; in tb_dp_xchg_caps()
694 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
699 * If the tunnel bandwidth is limited (max_bw is set) then see in tb_dp_xchg_caps()
705 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
709 if (tb_tunnel_direction_downstream(tunnel)) in tb_dp_xchg_caps()
710 max_bw = tunnel->max_down; in tb_dp_xchg_caps()
712 max_bw = tunnel->max_up; in tb_dp_xchg_caps()
721 tb_tunnel_info(tunnel, "not enough bandwidth\n"); in tb_dp_xchg_caps()
726 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
745 tb_tunnel_dbg(tunnel, "disabling LTTPR\n"); in tb_dp_xchg_caps()
752 static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel) in tb_dp_bandwidth_alloc_mode_enable() argument
755 struct tb_port *out = tunnel->dst_port; in tb_dp_bandwidth_alloc_mode_enable()
756 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_alloc_mode_enable()
795 tb_tunnel_dbg(tunnel, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n", in tb_dp_bandwidth_alloc_mode_enable()
811 tb_tunnel_dbg(tunnel, in tb_dp_bandwidth_alloc_mode_enable()
819 tb_tunnel_dbg(tunnel, "granularity %d Mb/s\n", granularity); in tb_dp_bandwidth_alloc_mode_enable()
834 if (tb_tunnel_direction_downstream(tunnel)) in tb_dp_bandwidth_alloc_mode_enable()
835 estimated_bw = tunnel->max_down; in tb_dp_bandwidth_alloc_mode_enable()
837 estimated_bw = tunnel->max_up; in tb_dp_bandwidth_alloc_mode_enable()
839 tb_tunnel_dbg(tunnel, "estimated bandwidth %d Mb/s\n", estimated_bw); in tb_dp_bandwidth_alloc_mode_enable()
850 tb_tunnel_dbg(tunnel, "bandwidth allocation mode enabled\n"); in tb_dp_bandwidth_alloc_mode_enable()
854 static int tb_dp_init(struct tb_tunnel *tunnel) in tb_dp_init() argument
856 struct tb_port *in = tunnel->src_port; in tb_dp_init()
861 ret = tb_dp_xchg_caps(tunnel); in tb_dp_init()
871 tb_tunnel_dbg(tunnel, "bandwidth allocation mode supported\n"); in tb_dp_init()
877 return tb_dp_bandwidth_alloc_mode_enable(tunnel); in tb_dp_init()
880 static void tb_dp_deinit(struct tb_tunnel *tunnel) in tb_dp_deinit() argument
882 struct tb_port *in = tunnel->src_port; in tb_dp_deinit()
888 tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n"); in tb_dp_deinit()
892 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active) in tb_dp_activate() argument
900 paths = tunnel->paths; in tb_dp_activate()
903 tb_dp_port_set_hops(tunnel->src_port, in tb_dp_activate()
908 tb_dp_port_set_hops(tunnel->dst_port, in tb_dp_activate()
913 tb_dp_port_hpd_clear(tunnel->src_port); in tb_dp_activate()
914 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0); in tb_dp_activate()
915 if (tb_port_is_dpout(tunnel->dst_port)) in tb_dp_activate()
916 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0); in tb_dp_activate()
919 ret = tb_dp_port_enable(tunnel->src_port, active); in tb_dp_activate()
923 if (tb_port_is_dpout(tunnel->dst_port)) in tb_dp_activate()
924 return tb_dp_port_enable(tunnel->dst_port, active); in tb_dp_activate()
931 * @tunnel: DP tunnel to check
934 * Returns maximum possible bandwidth for this tunnel in Mb/s.
936 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_maximum_bandwidth() argument
939 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_maximum_bandwidth()
970 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_consumed_bandwidth() argument
974 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_consumed_bandwidth()
980 if (!tunnel->bw_mode) in tb_dp_bandwidth_mode_consumed_bandwidth()
989 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded); in tb_dp_bandwidth_mode_consumed_bandwidth()
995 if (tb_tunnel_direction_downstream(tunnel)) { in tb_dp_bandwidth_mode_consumed_bandwidth()
1006 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_dp_allocated_bandwidth() argument
1009 struct tb_port *in = tunnel->src_port; in tb_dp_allocated_bandwidth()
1015 if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) { in tb_dp_allocated_bandwidth()
1023 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, in tb_dp_allocated_bandwidth()
1030 if (tb_tunnel_direction_downstream(tunnel)) { in tb_dp_allocated_bandwidth()
1040 return tunnel->consumed_bandwidth(tunnel, allocated_up, in tb_dp_allocated_bandwidth()
1044 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_dp_alloc_bandwidth() argument
1047 struct tb_port *in = tunnel->src_port; in tb_dp_alloc_bandwidth()
1053 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw_rounded); in tb_dp_alloc_bandwidth()
1057 if (tb_tunnel_direction_downstream(tunnel)) { in tb_dp_alloc_bandwidth()
1075 tunnel->bw_mode = true; in tb_dp_alloc_bandwidth()
1079 static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec) in tb_dp_wait_dprx() argument
1082 struct tb_port *in = tunnel->src_port; in tb_dp_wait_dprx()
1086 * active tunnel. in tb_dp_wait_dprx()
1098 tb_tunnel_dbg(tunnel, "DPRX read done\n"); in tb_dp_wait_dprx()
1104 tb_tunnel_dbg(tunnel, "DPRX read timeout\n"); in tb_dp_wait_dprx()
1108 /* Read cap from tunnel DP IN */
1109 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate, in tb_dp_read_cap() argument
1112 struct tb_port *in = tunnel->src_port; in tb_dp_read_cap()
1123 tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap); in tb_dp_read_cap()
1140 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_dp_maximum_bandwidth() argument
1145 if (!usb4_dp_port_bandwidth_mode_enabled(tunnel->src_port)) in tb_dp_maximum_bandwidth()
1148 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL); in tb_dp_maximum_bandwidth()
1152 if (tb_tunnel_direction_downstream(tunnel)) { in tb_dp_maximum_bandwidth()
1163 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_dp_consumed_bandwidth() argument
1166 const struct tb_switch *sw = tunnel->src_port->sw; in tb_dp_consumed_bandwidth()
1176 ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, in tb_dp_consumed_bandwidth()
1188 * negotiation shall compete in 5 seconds after tunnel in tb_dp_consumed_bandwidth()
1191 ret = tb_dp_wait_dprx(tunnel, 5100); in tb_dp_consumed_bandwidth()
1194 ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes); in tb_dp_consumed_bandwidth()
1198 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes); in tb_dp_consumed_bandwidth()
1208 if (tb_tunnel_direction_downstream(tunnel)) { in tb_dp_consumed_bandwidth()
1301 static void tb_dp_dump(struct tb_tunnel *tunnel) in tb_dp_dump() argument
1306 in = tunnel->src_port; in tb_dp_dump()
1307 out = tunnel->dst_port; in tb_dp_dump()
1316 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1327 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1338 tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n", in tb_dp_dump()
1348 * If @in adapter is active, follows the tunnel to the DP out adapter
1349 * and back. Returns the discovered tunnel or %NULL if there was no
1350 * tunnel.
1352 * Return: DP tunnel or %NULL if no tunnel found.
1357 struct tb_tunnel *tunnel; in tb_tunnel_discover_dp() local
1364 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_discover_dp()
1365 if (!tunnel) in tb_tunnel_discover_dp()
1368 tunnel->init = tb_dp_init; in tb_tunnel_discover_dp()
1369 tunnel->deinit = tb_dp_deinit; in tb_tunnel_discover_dp()
1370 tunnel->activate = tb_dp_activate; in tb_tunnel_discover_dp()
1371 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_discover_dp()
1372 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_discover_dp()
1373 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_discover_dp()
1374 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_discover_dp()
1375 tunnel->src_port = in; in tb_tunnel_discover_dp()
1378 &tunnel->dst_port, "Video", alloc_hopid); in tb_tunnel_discover_dp()
1384 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path; in tb_tunnel_discover_dp()
1385 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false)) in tb_tunnel_discover_dp()
1392 tunnel->paths[TB_DP_AUX_PATH_OUT] = path; in tb_tunnel_discover_dp()
1393 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false); in tb_tunnel_discover_dp()
1395 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID, in tb_tunnel_discover_dp()
1399 tunnel->paths[TB_DP_AUX_PATH_IN] = path; in tb_tunnel_discover_dp()
1400 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false); in tb_tunnel_discover_dp()
1402 /* Validate that the tunnel is complete */ in tb_tunnel_discover_dp()
1403 if (!tb_port_is_dpout(tunnel->dst_port)) { in tb_tunnel_discover_dp()
1408 if (!tb_dp_port_is_enabled(tunnel->dst_port)) in tb_tunnel_discover_dp()
1411 if (!tb_dp_port_hpd_is_active(tunnel->dst_port)) in tb_tunnel_discover_dp()
1414 if (port != tunnel->src_port) { in tb_tunnel_discover_dp()
1415 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_dp()
1419 tb_dp_dump(tunnel); in tb_tunnel_discover_dp()
1421 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_dp()
1422 return tunnel; in tb_tunnel_discover_dp()
1425 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_dp()
1427 tb_tunnel_free(tunnel); in tb_tunnel_discover_dp()
1433 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1438 * @max_up: Maximum available upstream bandwidth for the DP tunnel.
1440 * @max_down: Maximum available downstream bandwidth for the DP tunnel.
1443 * Allocates a tunnel between @in and @out that is capable of tunneling
1452 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dp() local
1460 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_alloc_dp()
1461 if (!tunnel) in tb_tunnel_alloc_dp()
1464 tunnel->init = tb_dp_init; in tb_tunnel_alloc_dp()
1465 tunnel->deinit = tb_dp_deinit; in tb_tunnel_alloc_dp()
1466 tunnel->activate = tb_dp_activate; in tb_tunnel_alloc_dp()
1467 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_alloc_dp()
1468 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_alloc_dp()
1469 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_alloc_dp()
1470 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_alloc_dp()
1471 tunnel->src_port = in; in tb_tunnel_alloc_dp()
1472 tunnel->dst_port = out; in tb_tunnel_alloc_dp()
1473 tunnel->max_up = max_up; in tb_tunnel_alloc_dp()
1474 tunnel->max_down = max_down; in tb_tunnel_alloc_dp()
1476 paths = tunnel->paths; in tb_tunnel_alloc_dp()
1500 return tunnel; in tb_tunnel_alloc_dp()
1503 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dp()
1568 * We don't tunnel other traffic over this link so can use all in tb_dma_init_rx_path()
1631 static void tb_dma_deinit(struct tb_tunnel *tunnel) in tb_dma_deinit() argument
1635 for (i = 0; i < tunnel->npaths; i++) { in tb_dma_deinit()
1636 if (!tunnel->paths[i]) in tb_dma_deinit()
1638 tb_dma_deinit_path(tunnel->paths[i]); in tb_dma_deinit()
1643 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1661 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dma() local
1678 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA); in tb_tunnel_alloc_dma()
1679 if (!tunnel) in tb_tunnel_alloc_dma()
1682 tunnel->src_port = nhi; in tb_tunnel_alloc_dma()
1683 tunnel->dst_port = dst; in tb_tunnel_alloc_dma()
1684 tunnel->deinit = tb_dma_deinit; in tb_tunnel_alloc_dma()
1693 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1695 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n"); in tb_tunnel_alloc_dma()
1705 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1707 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n"); in tb_tunnel_alloc_dma()
1712 return tunnel; in tb_tunnel_alloc_dma()
1715 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dma()
1720 * tb_tunnel_match_dma() - Match DMA tunnel
1721 * @tunnel: Tunnel to match
1729 * This function can be used to match specific DMA tunnel, if there are
1733 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, in tb_tunnel_match_dma() argument
1742 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_match_dma()
1743 const struct tb_path *path = tunnel->paths[i]; in tb_tunnel_match_dma()
1796 static int tb_usb3_init(struct tb_tunnel *tunnel) in tb_usb3_init() argument
1798 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", in tb_usb3_init()
1799 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_init()
1801 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port, in tb_usb3_init()
1802 &tunnel->allocated_up, in tb_usb3_init()
1803 &tunnel->allocated_down); in tb_usb3_init()
1806 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate) in tb_usb3_activate() argument
1810 res = tb_usb3_port_enable(tunnel->src_port, activate); in tb_usb3_activate()
1814 if (tb_port_is_usb3_up(tunnel->dst_port)) in tb_usb3_activate()
1815 return tb_usb3_port_enable(tunnel->dst_port, activate); in tb_usb3_activate()
1820 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_consumed_bandwidth() argument
1823 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); in tb_usb3_consumed_bandwidth()
1830 *consumed_up = tunnel->allocated_up * in tb_usb3_consumed_bandwidth()
1832 *consumed_down = tunnel->allocated_down * in tb_usb3_consumed_bandwidth()
1843 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_usb3_release_unused_bandwidth() argument
1847 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port, in tb_usb3_release_unused_bandwidth()
1848 &tunnel->allocated_up, in tb_usb3_release_unused_bandwidth()
1849 &tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1853 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_release_unused_bandwidth()
1854 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1858 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_reclaim_available_bandwidth() argument
1864 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port); in tb_usb3_reclaim_available_bandwidth()
1866 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n"); in tb_usb3_reclaim_available_bandwidth()
1877 if (tunnel->allocated_up >= max_rate && in tb_usb3_reclaim_available_bandwidth()
1878 tunnel->allocated_down >= max_rate) in tb_usb3_reclaim_available_bandwidth()
1883 if (allocate_up < tunnel->allocated_up) in tb_usb3_reclaim_available_bandwidth()
1884 allocate_up = tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1887 if (allocate_down < tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1888 allocate_down = tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1891 if (allocate_up == tunnel->allocated_up && in tb_usb3_reclaim_available_bandwidth()
1892 allocate_down == tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1895 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up, in tb_usb3_reclaim_available_bandwidth()
1898 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n"); in tb_usb3_reclaim_available_bandwidth()
1902 tunnel->allocated_up = allocate_up; in tb_usb3_reclaim_available_bandwidth()
1903 *available_up -= tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1905 tunnel->allocated_down = allocate_down; in tb_usb3_reclaim_available_bandwidth()
1906 *available_down -= tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1908 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_reclaim_available_bandwidth()
1909 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_reclaim_available_bandwidth()
1952 * If @down adapter is active, follows the tunnel to the USB3 upstream
1953 * adapter and back. Returns the discovered tunnel or %NULL if there was
1954 * no tunnel.
1959 struct tb_tunnel *tunnel; in tb_tunnel_discover_usb3() local
1965 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_discover_usb3()
1966 if (!tunnel) in tb_tunnel_discover_usb3()
1969 tunnel->activate = tb_usb3_activate; in tb_tunnel_discover_usb3()
1970 tunnel->src_port = down; in tb_tunnel_discover_usb3()
1978 &tunnel->dst_port, "USB3 Down", alloc_hopid); in tb_tunnel_discover_usb3()
1984 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_discover_usb3()
1985 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]); in tb_tunnel_discover_usb3()
1987 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL, in tb_tunnel_discover_usb3()
1991 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_discover_usb3()
1992 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]); in tb_tunnel_discover_usb3()
1994 /* Validate that the tunnel is complete */ in tb_tunnel_discover_usb3()
1995 if (!tb_port_is_usb3_up(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
1996 tb_port_warn(tunnel->dst_port, in tb_tunnel_discover_usb3()
2001 if (down != tunnel->src_port) { in tb_tunnel_discover_usb3()
2002 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_usb3()
2006 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
2007 tb_tunnel_warn(tunnel, in tb_tunnel_discover_usb3()
2008 "tunnel is not fully activated, cleaning up\n"); in tb_tunnel_discover_usb3()
2017 * hop tunnel. in tb_tunnel_discover_usb3()
2020 &tunnel->allocated_up, &tunnel->allocated_down); in tb_tunnel_discover_usb3()
2024 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", in tb_tunnel_discover_usb3()
2025 tunnel->allocated_up, tunnel->allocated_down); in tb_tunnel_discover_usb3()
2027 tunnel->init = tb_usb3_init; in tb_tunnel_discover_usb3()
2028 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_discover_usb3()
2029 tunnel->release_unused_bandwidth = in tb_tunnel_discover_usb3()
2031 tunnel->reclaim_available_bandwidth = in tb_tunnel_discover_usb3()
2035 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_usb3()
2036 return tunnel; in tb_tunnel_discover_usb3()
2039 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_usb3()
2041 tb_tunnel_free(tunnel); in tb_tunnel_discover_usb3()
2047 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
2051 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel.
2053 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel.
2056 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
2065 struct tb_tunnel *tunnel; in tb_tunnel_alloc_usb3() local
2080 tb_port_dbg(up, "maximum required bandwidth for USB3 tunnel %d Mb/s\n", in tb_tunnel_alloc_usb3()
2084 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_alloc_usb3()
2085 if (!tunnel) in tb_tunnel_alloc_usb3()
2088 tunnel->activate = tb_usb3_activate; in tb_tunnel_alloc_usb3()
2089 tunnel->src_port = down; in tb_tunnel_alloc_usb3()
2090 tunnel->dst_port = up; in tb_tunnel_alloc_usb3()
2091 tunnel->max_up = max_up; in tb_tunnel_alloc_usb3()
2092 tunnel->max_down = max_down; in tb_tunnel_alloc_usb3()
2097 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2101 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_alloc_usb3()
2106 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2110 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_alloc_usb3()
2113 tunnel->allocated_up = min(max_rate, max_up); in tb_tunnel_alloc_usb3()
2114 tunnel->allocated_down = min(max_rate, max_down); in tb_tunnel_alloc_usb3()
2116 tunnel->init = tb_usb3_init; in tb_tunnel_alloc_usb3()
2117 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_alloc_usb3()
2118 tunnel->release_unused_bandwidth = in tb_tunnel_alloc_usb3()
2120 tunnel->reclaim_available_bandwidth = in tb_tunnel_alloc_usb3()
2124 return tunnel; in tb_tunnel_alloc_usb3()
2128 * tb_tunnel_free() - free a tunnel
2129 * @tunnel: Tunnel to be freed
2131 * Frees a tunnel. The tunnel does not need to be deactivated.
2133 void tb_tunnel_free(struct tb_tunnel *tunnel) in tb_tunnel_free() argument
2137 if (!tunnel) in tb_tunnel_free()
2140 if (tunnel->deinit) in tb_tunnel_free()
2141 tunnel->deinit(tunnel); in tb_tunnel_free()
2143 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_free()
2144 if (tunnel->paths[i]) in tb_tunnel_free()
2145 tb_path_free(tunnel->paths[i]); in tb_tunnel_free()
2148 kfree(tunnel->paths); in tb_tunnel_free()
2149 kfree(tunnel); in tb_tunnel_free()
2154 * @tunnel: Tunnel to check
2156 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) in tb_tunnel_is_invalid() argument
2160 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_invalid()
2161 WARN_ON(!tunnel->paths[i]->activated); in tb_tunnel_is_invalid()
2162 if (tb_path_is_invalid(tunnel->paths[i])) in tb_tunnel_is_invalid()
2170 * tb_tunnel_restart() - activate a tunnel after a hardware reset
2171 * @tunnel: Tunnel to restart
2175 int tb_tunnel_restart(struct tb_tunnel *tunnel) in tb_tunnel_restart() argument
2179 tb_tunnel_dbg(tunnel, "activating\n"); in tb_tunnel_restart()
2185 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2186 if (tunnel->paths[i]->activated) { in tb_tunnel_restart()
2187 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_restart()
2188 tunnel->paths[i]->activated = false; in tb_tunnel_restart()
2192 if (tunnel->init) { in tb_tunnel_restart()
2193 res = tunnel->init(tunnel); in tb_tunnel_restart()
2198 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2199 res = tb_path_activate(tunnel->paths[i]); in tb_tunnel_restart()
2204 if (tunnel->activate) { in tb_tunnel_restart()
2205 res = tunnel->activate(tunnel, true); in tb_tunnel_restart()
2213 tb_tunnel_warn(tunnel, "activation failed\n"); in tb_tunnel_restart()
2214 tb_tunnel_deactivate(tunnel); in tb_tunnel_restart()
2219 * tb_tunnel_activate() - activate a tunnel
2220 * @tunnel: Tunnel to activate
2224 int tb_tunnel_activate(struct tb_tunnel *tunnel) in tb_tunnel_activate() argument
2228 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_activate()
2229 if (tunnel->paths[i]->activated) { in tb_tunnel_activate()
2230 tb_tunnel_WARN(tunnel, in tb_tunnel_activate()
2231 "trying to activate an already activated tunnel\n"); in tb_tunnel_activate()
2236 return tb_tunnel_restart(tunnel); in tb_tunnel_activate()
2240 * tb_tunnel_deactivate() - deactivate a tunnel
2241 * @tunnel: Tunnel to deactivate
2243 void tb_tunnel_deactivate(struct tb_tunnel *tunnel) in tb_tunnel_deactivate() argument
2247 tb_tunnel_dbg(tunnel, "deactivating\n"); in tb_tunnel_deactivate()
2249 if (tunnel->activate) in tb_tunnel_deactivate()
2250 tunnel->activate(tunnel, false); in tb_tunnel_deactivate()
2252 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_deactivate()
2253 if (tunnel->paths[i] && tunnel->paths[i]->activated) in tb_tunnel_deactivate()
2254 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_deactivate()
2259 * tb_tunnel_port_on_path() - Does the tunnel go through port
2260 * @tunnel: Tunnel to check
2263 * Returns true if @tunnel goes through @port (direction does not matter),
2266 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, in tb_tunnel_port_on_path() argument
2271 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_port_on_path()
2272 if (!tunnel->paths[i]) in tb_tunnel_port_on_path()
2275 if (tb_path_port_on_path(tunnel->paths[i], port)) in tb_tunnel_port_on_path()
2282 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) in tb_tunnel_is_active() argument
2286 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_active()
2287 if (!tunnel->paths[i]) in tb_tunnel_is_active()
2289 if (!tunnel->paths[i]->activated) in tb_tunnel_is_active()
2298 * @tunnel: Tunnel to check
2302 * Returns maximum possible bandwidth this tunnel can go if not limited
2303 * by other bandwidth clients. If the tunnel does not support this
2306 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_tunnel_maximum_bandwidth() argument
2309 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_maximum_bandwidth()
2312 if (tunnel->maximum_bandwidth) in tb_tunnel_maximum_bandwidth()
2313 return tunnel->maximum_bandwidth(tunnel, max_up, max_down); in tb_tunnel_maximum_bandwidth()
2318 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2319 * @tunnel: Tunnel to check
2324 * Returns the bandwidth allocated for the tunnel. This may be higher
2325 * than what the tunnel actually consumes.
2327 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_tunnel_allocated_bandwidth() argument
2330 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_allocated_bandwidth()
2333 if (tunnel->allocated_bandwidth) in tb_tunnel_allocated_bandwidth()
2334 return tunnel->allocated_bandwidth(tunnel, allocated_up, in tb_tunnel_allocated_bandwidth()
2340 * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
2341 * @tunnel: Tunnel whose bandwidth allocation to change
2345 * Tries to change tunnel bandwidth allocation. If succeeds returns %0
2350 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_tunnel_alloc_bandwidth() argument
2353 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_alloc_bandwidth()
2356 if (tunnel->alloc_bandwidth) in tb_tunnel_alloc_bandwidth()
2357 return tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down); in tb_tunnel_alloc_bandwidth()
2363 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2364 * @tunnel: Tunnel to check
2370 * Stores the amount of isochronous bandwidth @tunnel consumes in
2374 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_tunnel_consumed_bandwidth() argument
2379 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_consumed_bandwidth()
2382 if (tunnel->consumed_bandwidth) { in tb_tunnel_consumed_bandwidth()
2385 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); in tb_tunnel_consumed_bandwidth()
2389 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, in tb_tunnel_consumed_bandwidth()
2404 * @tunnel: Tunnel whose unused bandwidth to release
2406 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
2411 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_tunnel_release_unused_bandwidth() argument
2413 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_release_unused_bandwidth()
2416 if (tunnel->release_unused_bandwidth) { in tb_tunnel_release_unused_bandwidth()
2419 ret = tunnel->release_unused_bandwidth(tunnel); in tb_tunnel_release_unused_bandwidth()
2429 * @tunnel: Tunnel reclaiming available bandwidth
2435 * reclaimed by the tunnel). If nothing was reclaimed the values are
2438 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_tunnel_reclaim_available_bandwidth() argument
2442 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_reclaim_available_bandwidth()
2445 if (tunnel->reclaim_available_bandwidth) in tb_tunnel_reclaim_available_bandwidth()
2446 tunnel->reclaim_available_bandwidth(tunnel, available_up, in tb_tunnel_reclaim_available_bandwidth()
2450 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel) in tb_tunnel_type_name() argument
2452 return tb_tunnel_names[tunnel->type]; in tb_tunnel_type_name()