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()
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()
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()
930 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_maximum_bandwidth() argument
933 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_maximum_bandwidth()
978 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_consumed_bandwidth() argument
982 struct tb_port *out = tunnel->dst_port; in tb_dp_bandwidth_mode_consumed_bandwidth()
983 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_consumed_bandwidth()
989 if (!tunnel->bw_mode) in tb_dp_bandwidth_mode_consumed_bandwidth()
998 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_bandwidth_mode_consumed_bandwidth()
1015 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_dp_allocated_bandwidth() argument
1018 struct tb_port *out = tunnel->dst_port; in tb_dp_allocated_bandwidth()
1019 struct tb_port *in = tunnel->src_port; in tb_dp_allocated_bandwidth()
1025 if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) { in tb_dp_allocated_bandwidth()
1033 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_allocated_bandwidth()
1049 return tunnel->consumed_bandwidth(tunnel, allocated_up, in tb_dp_allocated_bandwidth()
1053 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_dp_alloc_bandwidth() argument
1056 struct tb_port *out = tunnel->dst_port; in tb_dp_alloc_bandwidth()
1057 struct tb_port *in = tunnel->src_port; in tb_dp_alloc_bandwidth()
1063 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_alloc_bandwidth()
1085 tunnel->bw_mode = true; in tb_dp_alloc_bandwidth()
1089 static int tb_dp_wait_dprx(struct tb_tunnel *tunnel, int timeout_msec) in tb_dp_wait_dprx() argument
1092 struct tb_port *in = tunnel->src_port; in tb_dp_wait_dprx()
1096 * active tunnel. in tb_dp_wait_dprx()
1108 tb_tunnel_dbg(tunnel, "DPRX read done\n"); in tb_dp_wait_dprx()
1114 tb_tunnel_dbg(tunnel, "DPRX read timeout\n"); in tb_dp_wait_dprx()
1118 /* Read cap from tunnel DP IN */
1119 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate, in tb_dp_read_cap() argument
1122 struct tb_port *in = tunnel->src_port; in tb_dp_read_cap()
1133 tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap); in tb_dp_read_cap()
1150 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_dp_maximum_bandwidth() argument
1153 struct tb_port *in = tunnel->src_port; in tb_dp_maximum_bandwidth()
1159 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL); in tb_dp_maximum_bandwidth()
1163 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_maximum_bandwidth()
1174 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_dp_consumed_bandwidth() argument
1177 struct tb_port *in = tunnel->src_port; in tb_dp_consumed_bandwidth()
1188 ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, in tb_dp_consumed_bandwidth()
1202 ret = tb_dp_wait_dprx(tunnel, 150); in tb_dp_consumed_bandwidth()
1205 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, in tb_dp_consumed_bandwidth()
1210 ret = tb_dp_read_cap(tunnel, DP_COMMON_CAP, &rate, &lanes); in tb_dp_consumed_bandwidth()
1214 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes); in tb_dp_consumed_bandwidth()
1224 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_consumed_bandwidth()
1317 static void tb_dp_dump(struct tb_tunnel *tunnel) in tb_dp_dump() argument
1322 in = tunnel->src_port; in tb_dp_dump()
1323 out = tunnel->dst_port; in tb_dp_dump()
1332 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1343 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1354 tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n", in tb_dp_dump()
1364 * If @in adapter is active, follows the tunnel to the DP out adapter
1365 * and back. Returns the discovered tunnel or %NULL if there was no
1366 * tunnel.
1368 * Return: DP tunnel or %NULL if no tunnel found.
1373 struct tb_tunnel *tunnel; in tb_tunnel_discover_dp() local
1380 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_discover_dp()
1381 if (!tunnel) in tb_tunnel_discover_dp()
1384 tunnel->init = tb_dp_init; in tb_tunnel_discover_dp()
1385 tunnel->deinit = tb_dp_deinit; in tb_tunnel_discover_dp()
1386 tunnel->activate = tb_dp_activate; in tb_tunnel_discover_dp()
1387 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_discover_dp()
1388 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_discover_dp()
1389 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_discover_dp()
1390 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_discover_dp()
1391 tunnel->src_port = in; in tb_tunnel_discover_dp()
1394 &tunnel->dst_port, "Video", alloc_hopid); in tb_tunnel_discover_dp()
1400 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path; in tb_tunnel_discover_dp()
1401 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], false)) in tb_tunnel_discover_dp()
1408 tunnel->paths[TB_DP_AUX_PATH_OUT] = path; in tb_tunnel_discover_dp()
1409 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT], false); in tb_tunnel_discover_dp()
1411 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID, in tb_tunnel_discover_dp()
1415 tunnel->paths[TB_DP_AUX_PATH_IN] = path; in tb_tunnel_discover_dp()
1416 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN], false); in tb_tunnel_discover_dp()
1418 /* Validate that the tunnel is complete */ in tb_tunnel_discover_dp()
1419 if (!tb_port_is_dpout(tunnel->dst_port)) { in tb_tunnel_discover_dp()
1424 if (!tb_dp_port_is_enabled(tunnel->dst_port)) in tb_tunnel_discover_dp()
1427 if (!tb_dp_port_hpd_is_active(tunnel->dst_port)) in tb_tunnel_discover_dp()
1430 if (port != tunnel->src_port) { in tb_tunnel_discover_dp()
1431 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_dp()
1435 tb_dp_dump(tunnel); in tb_tunnel_discover_dp()
1437 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_dp()
1438 return tunnel; in tb_tunnel_discover_dp()
1441 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_dp()
1443 tb_tunnel_free(tunnel); in tb_tunnel_discover_dp()
1449 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1454 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
1456 * @max_down: Maximum available downstream bandwidth for the DP tunnel
1459 * Allocates a tunnel between @in and @out that is capable of tunneling
1468 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dp() local
1476 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_alloc_dp()
1477 if (!tunnel) in tb_tunnel_alloc_dp()
1480 tunnel->init = tb_dp_init; in tb_tunnel_alloc_dp()
1481 tunnel->deinit = tb_dp_deinit; in tb_tunnel_alloc_dp()
1482 tunnel->activate = tb_dp_activate; in tb_tunnel_alloc_dp()
1483 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_alloc_dp()
1484 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_alloc_dp()
1485 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_alloc_dp()
1486 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_alloc_dp()
1487 tunnel->src_port = in; in tb_tunnel_alloc_dp()
1488 tunnel->dst_port = out; in tb_tunnel_alloc_dp()
1489 tunnel->max_up = max_up; in tb_tunnel_alloc_dp()
1490 tunnel->max_down = max_down; in tb_tunnel_alloc_dp()
1492 paths = tunnel->paths; in tb_tunnel_alloc_dp()
1516 return tunnel; in tb_tunnel_alloc_dp()
1519 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dp()
1584 * We don't tunnel other traffic over this link so can use all in tb_dma_init_rx_path()
1647 static void tb_dma_deinit(struct tb_tunnel *tunnel) in tb_dma_deinit() argument
1651 for (i = 0; i < tunnel->npaths; i++) { in tb_dma_deinit()
1652 if (!tunnel->paths[i]) in tb_dma_deinit()
1654 tb_dma_deinit_path(tunnel->paths[i]); in tb_dma_deinit()
1659 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1677 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dma() local
1694 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA); in tb_tunnel_alloc_dma()
1695 if (!tunnel) in tb_tunnel_alloc_dma()
1698 tunnel->src_port = nhi; in tb_tunnel_alloc_dma()
1699 tunnel->dst_port = dst; in tb_tunnel_alloc_dma()
1700 tunnel->deinit = tb_dma_deinit; in tb_tunnel_alloc_dma()
1709 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1711 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n"); in tb_tunnel_alloc_dma()
1721 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1723 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n"); in tb_tunnel_alloc_dma()
1728 return tunnel; in tb_tunnel_alloc_dma()
1731 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dma()
1736 * tb_tunnel_match_dma() - Match DMA tunnel
1737 * @tunnel: Tunnel to match
1745 * This function can be used to match specific DMA tunnel, if there are
1749 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, in tb_tunnel_match_dma() argument
1758 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_match_dma()
1759 const struct tb_path *path = tunnel->paths[i]; in tb_tunnel_match_dma()
1812 static int tb_usb3_init(struct tb_tunnel *tunnel) in tb_usb3_init() argument
1814 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", in tb_usb3_init()
1815 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_init()
1817 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port, in tb_usb3_init()
1818 &tunnel->allocated_up, in tb_usb3_init()
1819 &tunnel->allocated_down); in tb_usb3_init()
1822 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate) in tb_usb3_activate() argument
1826 res = tb_usb3_port_enable(tunnel->src_port, activate); in tb_usb3_activate()
1830 if (tb_port_is_usb3_up(tunnel->dst_port)) in tb_usb3_activate()
1831 return tb_usb3_port_enable(tunnel->dst_port, activate); in tb_usb3_activate()
1836 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_consumed_bandwidth() argument
1839 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); in tb_usb3_consumed_bandwidth()
1846 *consumed_up = tunnel->allocated_up * in tb_usb3_consumed_bandwidth()
1848 *consumed_down = tunnel->allocated_down * in tb_usb3_consumed_bandwidth()
1859 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_usb3_release_unused_bandwidth() argument
1863 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port, in tb_usb3_release_unused_bandwidth()
1864 &tunnel->allocated_up, in tb_usb3_release_unused_bandwidth()
1865 &tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1869 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_release_unused_bandwidth()
1870 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1874 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_reclaim_available_bandwidth() argument
1880 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port); in tb_usb3_reclaim_available_bandwidth()
1882 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n"); in tb_usb3_reclaim_available_bandwidth()
1893 if (tunnel->allocated_up >= max_rate && in tb_usb3_reclaim_available_bandwidth()
1894 tunnel->allocated_down >= max_rate) in tb_usb3_reclaim_available_bandwidth()
1899 if (allocate_up < tunnel->allocated_up) in tb_usb3_reclaim_available_bandwidth()
1900 allocate_up = tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1903 if (allocate_down < tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1904 allocate_down = tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1907 if (allocate_up == tunnel->allocated_up && in tb_usb3_reclaim_available_bandwidth()
1908 allocate_down == tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1911 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up, in tb_usb3_reclaim_available_bandwidth()
1914 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n"); in tb_usb3_reclaim_available_bandwidth()
1918 tunnel->allocated_up = allocate_up; in tb_usb3_reclaim_available_bandwidth()
1919 *available_up -= tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1921 tunnel->allocated_down = allocate_down; in tb_usb3_reclaim_available_bandwidth()
1922 *available_down -= tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1924 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_reclaim_available_bandwidth()
1925 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_reclaim_available_bandwidth()
1968 * If @down adapter is active, follows the tunnel to the USB3 upstream
1969 * adapter and back. Returns the discovered tunnel or %NULL if there was
1970 * no tunnel.
1975 struct tb_tunnel *tunnel; in tb_tunnel_discover_usb3() local
1981 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_discover_usb3()
1982 if (!tunnel) in tb_tunnel_discover_usb3()
1985 tunnel->activate = tb_usb3_activate; in tb_tunnel_discover_usb3()
1986 tunnel->src_port = down; in tb_tunnel_discover_usb3()
1994 &tunnel->dst_port, "USB3 Down", alloc_hopid); in tb_tunnel_discover_usb3()
2000 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_discover_usb3()
2001 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]); in tb_tunnel_discover_usb3()
2003 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL, in tb_tunnel_discover_usb3()
2007 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_discover_usb3()
2008 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]); in tb_tunnel_discover_usb3()
2010 /* Validate that the tunnel is complete */ in tb_tunnel_discover_usb3()
2011 if (!tb_port_is_usb3_up(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
2012 tb_port_warn(tunnel->dst_port, in tb_tunnel_discover_usb3()
2017 if (down != tunnel->src_port) { in tb_tunnel_discover_usb3()
2018 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_usb3()
2022 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
2023 tb_tunnel_warn(tunnel, in tb_tunnel_discover_usb3()
2024 "tunnel is not fully activated, cleaning up\n"); in tb_tunnel_discover_usb3()
2033 * hop tunnel. in tb_tunnel_discover_usb3()
2036 &tunnel->allocated_up, &tunnel->allocated_down); in tb_tunnel_discover_usb3()
2040 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", in tb_tunnel_discover_usb3()
2041 tunnel->allocated_up, tunnel->allocated_down); in tb_tunnel_discover_usb3()
2043 tunnel->init = tb_usb3_init; in tb_tunnel_discover_usb3()
2044 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_discover_usb3()
2045 tunnel->release_unused_bandwidth = in tb_tunnel_discover_usb3()
2047 tunnel->reclaim_available_bandwidth = in tb_tunnel_discover_usb3()
2051 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_usb3()
2052 return tunnel; in tb_tunnel_discover_usb3()
2055 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_usb3()
2057 tb_tunnel_free(tunnel); in tb_tunnel_discover_usb3()
2063 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
2067 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
2069 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
2072 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
2081 struct tb_tunnel *tunnel; in tb_tunnel_alloc_usb3() local
2087 * USB3 tunnel. in tb_tunnel_alloc_usb3()
2096 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n", in tb_tunnel_alloc_usb3()
2100 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n"); in tb_tunnel_alloc_usb3()
2105 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_alloc_usb3()
2106 if (!tunnel) in tb_tunnel_alloc_usb3()
2109 tunnel->activate = tb_usb3_activate; in tb_tunnel_alloc_usb3()
2110 tunnel->src_port = down; in tb_tunnel_alloc_usb3()
2111 tunnel->dst_port = up; in tb_tunnel_alloc_usb3()
2112 tunnel->max_up = max_up; in tb_tunnel_alloc_usb3()
2113 tunnel->max_down = max_down; in tb_tunnel_alloc_usb3()
2118 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2122 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_alloc_usb3()
2127 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2131 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_alloc_usb3()
2134 tunnel->allocated_up = max_rate; in tb_tunnel_alloc_usb3()
2135 tunnel->allocated_down = max_rate; in tb_tunnel_alloc_usb3()
2137 tunnel->init = tb_usb3_init; in tb_tunnel_alloc_usb3()
2138 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_alloc_usb3()
2139 tunnel->release_unused_bandwidth = in tb_tunnel_alloc_usb3()
2141 tunnel->reclaim_available_bandwidth = in tb_tunnel_alloc_usb3()
2145 return tunnel; in tb_tunnel_alloc_usb3()
2149 * tb_tunnel_free() - free a tunnel
2150 * @tunnel: Tunnel to be freed
2152 * Frees a tunnel. The tunnel does not need to be deactivated.
2154 void tb_tunnel_free(struct tb_tunnel *tunnel) in tb_tunnel_free() argument
2158 if (!tunnel) in tb_tunnel_free()
2161 if (tunnel->deinit) in tb_tunnel_free()
2162 tunnel->deinit(tunnel); in tb_tunnel_free()
2164 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_free()
2165 if (tunnel->paths[i]) in tb_tunnel_free()
2166 tb_path_free(tunnel->paths[i]); in tb_tunnel_free()
2169 kfree(tunnel->paths); in tb_tunnel_free()
2170 kfree(tunnel); in tb_tunnel_free()
2175 * @tunnel: Tunnel to check
2177 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) in tb_tunnel_is_invalid() argument
2181 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_invalid()
2182 WARN_ON(!tunnel->paths[i]->activated); in tb_tunnel_is_invalid()
2183 if (tb_path_is_invalid(tunnel->paths[i])) in tb_tunnel_is_invalid()
2191 * tb_tunnel_restart() - activate a tunnel after a hardware reset
2192 * @tunnel: Tunnel to restart
2196 int tb_tunnel_restart(struct tb_tunnel *tunnel) in tb_tunnel_restart() argument
2200 tb_tunnel_dbg(tunnel, "activating\n"); in tb_tunnel_restart()
2206 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2207 if (tunnel->paths[i]->activated) { in tb_tunnel_restart()
2208 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_restart()
2209 tunnel->paths[i]->activated = false; in tb_tunnel_restart()
2213 if (tunnel->init) { in tb_tunnel_restart()
2214 res = tunnel->init(tunnel); in tb_tunnel_restart()
2219 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2220 res = tb_path_activate(tunnel->paths[i]); in tb_tunnel_restart()
2225 if (tunnel->activate) { in tb_tunnel_restart()
2226 res = tunnel->activate(tunnel, true); in tb_tunnel_restart()
2234 tb_tunnel_warn(tunnel, "activation failed\n"); in tb_tunnel_restart()
2235 tb_tunnel_deactivate(tunnel); in tb_tunnel_restart()
2240 * tb_tunnel_activate() - activate a tunnel
2241 * @tunnel: Tunnel to activate
2245 int tb_tunnel_activate(struct tb_tunnel *tunnel) in tb_tunnel_activate() argument
2249 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_activate()
2250 if (tunnel->paths[i]->activated) { in tb_tunnel_activate()
2251 tb_tunnel_WARN(tunnel, in tb_tunnel_activate()
2252 "trying to activate an already activated tunnel\n"); in tb_tunnel_activate()
2257 return tb_tunnel_restart(tunnel); in tb_tunnel_activate()
2261 * tb_tunnel_deactivate() - deactivate a tunnel
2262 * @tunnel: Tunnel to deactivate
2264 void tb_tunnel_deactivate(struct tb_tunnel *tunnel) in tb_tunnel_deactivate() argument
2268 tb_tunnel_dbg(tunnel, "deactivating\n"); in tb_tunnel_deactivate()
2270 if (tunnel->activate) in tb_tunnel_deactivate()
2271 tunnel->activate(tunnel, false); in tb_tunnel_deactivate()
2273 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_deactivate()
2274 if (tunnel->paths[i] && tunnel->paths[i]->activated) in tb_tunnel_deactivate()
2275 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_deactivate()
2280 * tb_tunnel_port_on_path() - Does the tunnel go through port
2281 * @tunnel: Tunnel to check
2284 * Returns true if @tunnel goes through @port (direction does not matter),
2287 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, in tb_tunnel_port_on_path() argument
2292 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_port_on_path()
2293 if (!tunnel->paths[i]) in tb_tunnel_port_on_path()
2296 if (tb_path_port_on_path(tunnel->paths[i], port)) in tb_tunnel_port_on_path()
2303 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) in tb_tunnel_is_active() argument
2307 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_active()
2308 if (!tunnel->paths[i]) in tb_tunnel_is_active()
2310 if (!tunnel->paths[i]->activated) in tb_tunnel_is_active()
2319 * @tunnel: Tunnel to check
2323 * Returns maximum possible bandwidth this tunnel can go if not limited
2324 * by other bandwidth clients. If the tunnel does not support this
2327 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_tunnel_maximum_bandwidth() argument
2330 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_maximum_bandwidth()
2333 if (tunnel->maximum_bandwidth) in tb_tunnel_maximum_bandwidth()
2334 return tunnel->maximum_bandwidth(tunnel, max_up, max_down); in tb_tunnel_maximum_bandwidth()
2339 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2340 * @tunnel: Tunnel to check
2345 * Returns the bandwidth allocated for the tunnel. This may be higher
2346 * than what the tunnel actually consumes.
2348 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_tunnel_allocated_bandwidth() argument
2351 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_allocated_bandwidth()
2354 if (tunnel->allocated_bandwidth) in tb_tunnel_allocated_bandwidth()
2355 return tunnel->allocated_bandwidth(tunnel, allocated_up, in tb_tunnel_allocated_bandwidth()
2361 * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
2362 * @tunnel: Tunnel whose bandwidth allocation to change
2366 * Tries to change tunnel bandwidth allocation. If succeeds returns %0
2371 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_tunnel_alloc_bandwidth() argument
2374 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_alloc_bandwidth()
2377 if (tunnel->alloc_bandwidth) in tb_tunnel_alloc_bandwidth()
2378 return tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down); in tb_tunnel_alloc_bandwidth()
2384 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2385 * @tunnel: Tunnel to check
2391 * Stores the amount of isochronous bandwidth @tunnel consumes in
2395 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_tunnel_consumed_bandwidth() argument
2400 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_consumed_bandwidth()
2403 if (tunnel->consumed_bandwidth) { in tb_tunnel_consumed_bandwidth()
2406 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); in tb_tunnel_consumed_bandwidth()
2410 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, in tb_tunnel_consumed_bandwidth()
2425 * @tunnel: Tunnel whose unused bandwidth to release
2427 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
2432 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_tunnel_release_unused_bandwidth() argument
2434 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_release_unused_bandwidth()
2437 if (tunnel->release_unused_bandwidth) { in tb_tunnel_release_unused_bandwidth()
2440 ret = tunnel->release_unused_bandwidth(tunnel); in tb_tunnel_release_unused_bandwidth()
2450 * @tunnel: Tunnel reclaiming available bandwidth
2456 * reclaimed by the tunnel). If nothing was reclaimed the values are
2459 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_tunnel_reclaim_available_bandwidth() argument
2463 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_reclaim_available_bandwidth()
2466 if (tunnel->reclaim_available_bandwidth) in tb_tunnel_reclaim_available_bandwidth()
2467 tunnel->reclaim_available_bandwidth(tunnel, available_up, in tb_tunnel_reclaim_available_bandwidth()
2471 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel) in tb_tunnel_type_name() argument
2473 return tb_tunnel_names[tunnel->type]; in tb_tunnel_type_name()