Lines Matching full:tunnel
15 #include "tunnel.h"
108 /* Add some credits for potential second DMA tunnel */ in tb_available_credits()
140 struct tb_tunnel *tunnel; in tb_tunnel_alloc() local
142 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); in tb_tunnel_alloc()
143 if (!tunnel) in tb_tunnel_alloc()
146 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); in tb_tunnel_alloc()
147 if (!tunnel->paths) { in tb_tunnel_alloc()
148 tb_tunnel_free(tunnel); in tb_tunnel_alloc()
152 INIT_LIST_HEAD(&tunnel->list); in tb_tunnel_alloc()
153 tunnel->tb = tb; in tb_tunnel_alloc()
154 tunnel->npaths = npaths; in tb_tunnel_alloc()
155 tunnel->type = type; in tb_tunnel_alloc()
157 return tunnel; in tb_tunnel_alloc()
160 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable) in tb_pci_set_ext_encapsulation() argument
162 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); in tb_pci_set_ext_encapsulation()
169 ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable); in tb_pci_set_ext_encapsulation()
173 ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable); in tb_pci_set_ext_encapsulation()
177 tb_tunnel_dbg(tunnel, "extended encapsulation %s\n", in tb_pci_set_ext_encapsulation()
182 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) in tb_pci_activate() argument
187 res = tb_pci_set_ext_encapsulation(tunnel, activate); in tb_pci_activate()
192 res = tb_pci_port_enable(tunnel->src_port, activate); in tb_pci_activate()
196 if (tb_port_is_pcie_up(tunnel->dst_port)) { in tb_pci_activate()
197 res = tb_pci_port_enable(tunnel->dst_port, activate); in tb_pci_activate()
202 return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate); in tb_pci_activate()
261 * If @down adapter is active, follows the tunnel to the PCIe upstream
262 * adapter and back. Returns the discovered tunnel or %NULL if there was
263 * no tunnel.
268 struct tb_tunnel *tunnel; in tb_tunnel_discover_pci() local
274 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); in tb_tunnel_discover_pci()
275 if (!tunnel) in tb_tunnel_discover_pci()
278 tunnel->activate = tb_pci_activate; in tb_tunnel_discover_pci()
279 tunnel->src_port = down; in tb_tunnel_discover_pci()
287 &tunnel->dst_port, "PCIe Up", alloc_hopid); in tb_tunnel_discover_pci()
293 tunnel->paths[TB_PCI_PATH_UP] = path; in tb_tunnel_discover_pci()
294 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP])) in tb_tunnel_discover_pci()
297 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL, in tb_tunnel_discover_pci()
301 tunnel->paths[TB_PCI_PATH_DOWN] = path; in tb_tunnel_discover_pci()
302 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN])) in tb_tunnel_discover_pci()
305 /* Validate that the tunnel is complete */ in tb_tunnel_discover_pci()
306 if (!tb_port_is_pcie_up(tunnel->dst_port)) { in tb_tunnel_discover_pci()
307 tb_port_warn(tunnel->dst_port, in tb_tunnel_discover_pci()
312 if (down != tunnel->src_port) { in tb_tunnel_discover_pci()
313 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_pci()
317 if (!tb_pci_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_pci()
318 tb_tunnel_warn(tunnel, in tb_tunnel_discover_pci()
319 "tunnel is not fully activated, cleaning up\n"); in tb_tunnel_discover_pci()
323 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_pci()
324 return tunnel; in tb_tunnel_discover_pci()
327 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_pci()
329 tb_tunnel_free(tunnel); in tb_tunnel_discover_pci()
335 * tb_tunnel_alloc_pci() - allocate a pci tunnel
340 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
348 struct tb_tunnel *tunnel; in tb_tunnel_alloc_pci() local
351 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI); in tb_tunnel_alloc_pci()
352 if (!tunnel) in tb_tunnel_alloc_pci()
355 tunnel->activate = tb_pci_activate; in tb_tunnel_alloc_pci()
356 tunnel->src_port = down; in tb_tunnel_alloc_pci()
357 tunnel->dst_port = up; in tb_tunnel_alloc_pci()
363 tunnel->paths[TB_PCI_PATH_DOWN] = path; in tb_tunnel_alloc_pci()
371 tunnel->paths[TB_PCI_PATH_UP] = path; in tb_tunnel_alloc_pci()
375 return tunnel; in tb_tunnel_alloc_pci()
378 tb_tunnel_free(tunnel); in tb_tunnel_alloc_pci()
624 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel) in tb_dp_xchg_caps() argument
627 struct tb_port *out = tunnel->dst_port; in tb_dp_xchg_caps()
628 struct tb_port *in = tunnel->src_port; in tb_dp_xchg_caps()
665 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
670 * If the tunnel bandwidth is limited (max_bw is set) then see in tb_dp_xchg_caps()
676 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
681 max_bw = tunnel->max_down; in tb_dp_xchg_caps()
683 max_bw = tunnel->max_up; in tb_dp_xchg_caps()
692 tb_tunnel_info(tunnel, "not enough bandwidth\n"); in tb_dp_xchg_caps()
697 tb_tunnel_dbg(tunnel, in tb_dp_xchg_caps()
716 tb_tunnel_dbg(tunnel, "disabling LTTPR\n"); in tb_dp_xchg_caps()
723 static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel) in tb_dp_bandwidth_alloc_mode_enable() argument
726 struct tb_port *out = tunnel->dst_port; in tb_dp_bandwidth_alloc_mode_enable()
727 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_alloc_mode_enable()
766 tb_tunnel_dbg(tunnel, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n", in tb_dp_bandwidth_alloc_mode_enable()
782 tb_tunnel_dbg(tunnel, in tb_dp_bandwidth_alloc_mode_enable()
790 tb_tunnel_dbg(tunnel, "granularity %d Mb/s\n", granularity); in tb_dp_bandwidth_alloc_mode_enable()
806 estimated_bw = tunnel->max_down; in tb_dp_bandwidth_alloc_mode_enable()
808 estimated_bw = tunnel->max_up; in tb_dp_bandwidth_alloc_mode_enable()
810 tb_tunnel_dbg(tunnel, "estimated bandwidth %d Mb/s\n", estimated_bw); in tb_dp_bandwidth_alloc_mode_enable()
821 tb_tunnel_dbg(tunnel, "bandwidth allocation mode enabled\n"); in tb_dp_bandwidth_alloc_mode_enable()
825 static int tb_dp_init(struct tb_tunnel *tunnel) in tb_dp_init() argument
827 struct tb_port *in = tunnel->src_port; in tb_dp_init()
832 ret = tb_dp_xchg_caps(tunnel); in tb_dp_init()
842 tb_tunnel_dbg(tunnel, "bandwidth allocation mode supported\n"); in tb_dp_init()
848 return tb_dp_bandwidth_alloc_mode_enable(tunnel); in tb_dp_init()
851 static void tb_dp_deinit(struct tb_tunnel *tunnel) in tb_dp_deinit() argument
853 struct tb_port *in = tunnel->src_port; in tb_dp_deinit()
859 tb_tunnel_dbg(tunnel, "bandwidth allocation mode disabled\n"); in tb_dp_deinit()
863 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active) in tb_dp_activate() argument
871 paths = tunnel->paths; in tb_dp_activate()
874 tb_dp_port_set_hops(tunnel->src_port, in tb_dp_activate()
879 tb_dp_port_set_hops(tunnel->dst_port, in tb_dp_activate()
884 tb_dp_port_hpd_clear(tunnel->src_port); in tb_dp_activate()
885 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0); in tb_dp_activate()
886 if (tb_port_is_dpout(tunnel->dst_port)) in tb_dp_activate()
887 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0); in tb_dp_activate()
890 ret = tb_dp_port_enable(tunnel->src_port, active); in tb_dp_activate()
894 if (tb_port_is_dpout(tunnel->dst_port)) in tb_dp_activate()
895 return tb_dp_port_enable(tunnel->dst_port, active); in tb_dp_activate()
901 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_maximum_bandwidth() argument
904 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_maximum_bandwidth()
949 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_dp_bandwidth_mode_consumed_bandwidth() argument
953 struct tb_port *out = tunnel->dst_port; in tb_dp_bandwidth_mode_consumed_bandwidth()
954 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_consumed_bandwidth()
960 if (!tunnel->bw_mode) in tb_dp_bandwidth_mode_consumed_bandwidth()
969 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_bandwidth_mode_consumed_bandwidth()
986 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_dp_allocated_bandwidth() argument
989 struct tb_port *out = tunnel->dst_port; in tb_dp_allocated_bandwidth()
990 struct tb_port *in = tunnel->src_port; in tb_dp_allocated_bandwidth()
996 if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) { in tb_dp_allocated_bandwidth()
1004 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_allocated_bandwidth()
1020 return tunnel->consumed_bandwidth(tunnel, allocated_up, in tb_dp_allocated_bandwidth()
1024 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_dp_alloc_bandwidth() argument
1027 struct tb_port *out = tunnel->dst_port; in tb_dp_alloc_bandwidth()
1028 struct tb_port *in = tunnel->src_port; in tb_dp_alloc_bandwidth()
1034 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw); in tb_dp_alloc_bandwidth()
1056 tunnel->bw_mode = true; in tb_dp_alloc_bandwidth()
1060 static int tb_dp_read_dprx(struct tb_tunnel *tunnel, u32 *rate, u32 *lanes, in tb_dp_read_dprx() argument
1064 struct tb_port *in = tunnel->src_port; in tb_dp_read_dprx()
1068 * active tunnel. in tb_dp_read_dprx()
1083 tb_tunnel_dbg(tunnel, "DPRX read done\n"); in tb_dp_read_dprx()
1092 /* Read cap from tunnel DP IN */
1093 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate, in tb_dp_read_cap() argument
1096 struct tb_port *in = tunnel->src_port; in tb_dp_read_cap()
1106 tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap); in tb_dp_read_cap()
1123 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_dp_maximum_bandwidth() argument
1126 struct tb_port *in = tunnel->src_port; in tb_dp_maximum_bandwidth()
1132 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL); in tb_dp_maximum_bandwidth()
1136 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_maximum_bandwidth()
1147 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_dp_consumed_bandwidth() argument
1150 struct tb_port *in = tunnel->src_port; in tb_dp_consumed_bandwidth()
1161 ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up, in tb_dp_consumed_bandwidth()
1175 ret = tb_dp_read_dprx(tunnel, &rate, &lanes, 150); in tb_dp_consumed_bandwidth()
1178 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, in tb_dp_consumed_bandwidth()
1184 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes); in tb_dp_consumed_bandwidth()
1194 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_consumed_bandwidth()
1282 static void tb_dp_dump(struct tb_tunnel *tunnel) in tb_dp_dump() argument
1287 in = tunnel->src_port; in tb_dp_dump()
1288 out = tunnel->dst_port; in tb_dp_dump()
1297 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1301 out = tunnel->dst_port; in tb_dp_dump()
1310 tb_tunnel_dbg(tunnel, in tb_dp_dump()
1321 tb_tunnel_dbg(tunnel, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n", in tb_dp_dump()
1331 * If @in adapter is active, follows the tunnel to the DP out adapter
1332 * and back. Returns the discovered tunnel or %NULL if there was no
1333 * tunnel.
1335 * Return: DP tunnel or %NULL if no tunnel found.
1340 struct tb_tunnel *tunnel; in tb_tunnel_discover_dp() local
1347 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_discover_dp()
1348 if (!tunnel) in tb_tunnel_discover_dp()
1351 tunnel->init = tb_dp_init; in tb_tunnel_discover_dp()
1352 tunnel->deinit = tb_dp_deinit; in tb_tunnel_discover_dp()
1353 tunnel->activate = tb_dp_activate; in tb_tunnel_discover_dp()
1354 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_discover_dp()
1355 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_discover_dp()
1356 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_discover_dp()
1357 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_discover_dp()
1358 tunnel->src_port = in; in tb_tunnel_discover_dp()
1361 &tunnel->dst_port, "Video", alloc_hopid); in tb_tunnel_discover_dp()
1367 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path; in tb_tunnel_discover_dp()
1368 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT])) in tb_tunnel_discover_dp()
1375 tunnel->paths[TB_DP_AUX_PATH_OUT] = path; in tb_tunnel_discover_dp()
1376 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]); in tb_tunnel_discover_dp()
1378 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID, in tb_tunnel_discover_dp()
1382 tunnel->paths[TB_DP_AUX_PATH_IN] = path; in tb_tunnel_discover_dp()
1383 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]); in tb_tunnel_discover_dp()
1385 /* Validate that the tunnel is complete */ in tb_tunnel_discover_dp()
1386 if (!tb_port_is_dpout(tunnel->dst_port)) { in tb_tunnel_discover_dp()
1391 if (!tb_dp_port_is_enabled(tunnel->dst_port)) in tb_tunnel_discover_dp()
1394 if (!tb_dp_port_hpd_is_active(tunnel->dst_port)) in tb_tunnel_discover_dp()
1397 if (port != tunnel->src_port) { in tb_tunnel_discover_dp()
1398 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_dp()
1402 tb_dp_dump(tunnel); in tb_tunnel_discover_dp()
1404 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_dp()
1405 return tunnel; in tb_tunnel_discover_dp()
1408 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_dp()
1410 tb_tunnel_free(tunnel); in tb_tunnel_discover_dp()
1416 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1421 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
1423 * @max_down: Maximum available downstream bandwidth for the DP tunnel
1426 * Allocates a tunnel between @in and @out that is capable of tunneling
1435 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dp() local
1442 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP); in tb_tunnel_alloc_dp()
1443 if (!tunnel) in tb_tunnel_alloc_dp()
1446 tunnel->init = tb_dp_init; in tb_tunnel_alloc_dp()
1447 tunnel->deinit = tb_dp_deinit; in tb_tunnel_alloc_dp()
1448 tunnel->activate = tb_dp_activate; in tb_tunnel_alloc_dp()
1449 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth; in tb_tunnel_alloc_dp()
1450 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth; in tb_tunnel_alloc_dp()
1451 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth; in tb_tunnel_alloc_dp()
1452 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth; in tb_tunnel_alloc_dp()
1453 tunnel->src_port = in; in tb_tunnel_alloc_dp()
1454 tunnel->dst_port = out; in tb_tunnel_alloc_dp()
1455 tunnel->max_up = max_up; in tb_tunnel_alloc_dp()
1456 tunnel->max_down = max_down; in tb_tunnel_alloc_dp()
1458 paths = tunnel->paths; in tb_tunnel_alloc_dp()
1481 return tunnel; in tb_tunnel_alloc_dp()
1484 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dp()
1549 * We don't tunnel other traffic over this link so can use all in tb_dma_init_rx_path()
1612 static void tb_dma_deinit(struct tb_tunnel *tunnel) in tb_dma_deinit() argument
1616 for (i = 0; i < tunnel->npaths; i++) { in tb_dma_deinit()
1617 if (!tunnel->paths[i]) in tb_dma_deinit()
1619 tb_dma_deinit_path(tunnel->paths[i]); in tb_dma_deinit()
1624 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1642 struct tb_tunnel *tunnel; in tb_tunnel_alloc_dma() local
1659 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA); in tb_tunnel_alloc_dma()
1660 if (!tunnel) in tb_tunnel_alloc_dma()
1663 tunnel->src_port = nhi; in tb_tunnel_alloc_dma()
1664 tunnel->dst_port = dst; in tb_tunnel_alloc_dma()
1665 tunnel->deinit = tb_dma_deinit; in tb_tunnel_alloc_dma()
1674 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1676 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n"); in tb_tunnel_alloc_dma()
1686 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1688 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n"); in tb_tunnel_alloc_dma()
1693 return tunnel; in tb_tunnel_alloc_dma()
1696 tb_tunnel_free(tunnel); in tb_tunnel_alloc_dma()
1701 * tb_tunnel_match_dma() - Match DMA tunnel
1702 * @tunnel: Tunnel to match
1710 * This function can be used to match specific DMA tunnel, if there are
1714 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path, in tb_tunnel_match_dma() argument
1723 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_match_dma()
1724 const struct tb_path *path = tunnel->paths[i]; in tb_tunnel_match_dma()
1777 static int tb_usb3_init(struct tb_tunnel *tunnel) in tb_usb3_init() argument
1779 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n", in tb_usb3_init()
1780 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_init()
1782 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port, in tb_usb3_init()
1783 &tunnel->allocated_up, in tb_usb3_init()
1784 &tunnel->allocated_down); in tb_usb3_init()
1787 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate) in tb_usb3_activate() argument
1791 res = tb_usb3_port_enable(tunnel->src_port, activate); in tb_usb3_activate()
1795 if (tb_port_is_usb3_up(tunnel->dst_port)) in tb_usb3_activate()
1796 return tb_usb3_port_enable(tunnel->dst_port, activate); in tb_usb3_activate()
1801 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_consumed_bandwidth() argument
1804 struct tb_port *port = tb_upstream_port(tunnel->dst_port->sw); in tb_usb3_consumed_bandwidth()
1811 *consumed_up = tunnel->allocated_up * in tb_usb3_consumed_bandwidth()
1813 *consumed_down = tunnel->allocated_down * in tb_usb3_consumed_bandwidth()
1824 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_usb3_release_unused_bandwidth() argument
1828 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port, in tb_usb3_release_unused_bandwidth()
1829 &tunnel->allocated_up, in tb_usb3_release_unused_bandwidth()
1830 &tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1834 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_release_unused_bandwidth()
1835 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_release_unused_bandwidth()
1839 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_usb3_reclaim_available_bandwidth() argument
1845 ret = usb4_usb3_port_actual_link_rate(tunnel->src_port); in tb_usb3_reclaim_available_bandwidth()
1847 tb_tunnel_warn(tunnel, "failed to read actual link rate\n"); in tb_usb3_reclaim_available_bandwidth()
1851 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port); in tb_usb3_reclaim_available_bandwidth()
1853 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n"); in tb_usb3_reclaim_available_bandwidth()
1865 if (tunnel->allocated_up >= max_rate && in tb_usb3_reclaim_available_bandwidth()
1866 tunnel->allocated_down >= max_rate) in tb_usb3_reclaim_available_bandwidth()
1871 if (allocate_up < tunnel->allocated_up) in tb_usb3_reclaim_available_bandwidth()
1872 allocate_up = tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1875 if (allocate_down < tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1876 allocate_down = tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1879 if (allocate_up == tunnel->allocated_up && in tb_usb3_reclaim_available_bandwidth()
1880 allocate_down == tunnel->allocated_down) in tb_usb3_reclaim_available_bandwidth()
1883 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up, in tb_usb3_reclaim_available_bandwidth()
1886 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n"); in tb_usb3_reclaim_available_bandwidth()
1890 tunnel->allocated_up = allocate_up; in tb_usb3_reclaim_available_bandwidth()
1891 *available_up -= tunnel->allocated_up; in tb_usb3_reclaim_available_bandwidth()
1893 tunnel->allocated_down = allocate_down; in tb_usb3_reclaim_available_bandwidth()
1894 *available_down -= tunnel->allocated_down; in tb_usb3_reclaim_available_bandwidth()
1896 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n", in tb_usb3_reclaim_available_bandwidth()
1897 tunnel->allocated_up, tunnel->allocated_down); in tb_usb3_reclaim_available_bandwidth()
1940 * If @down adapter is active, follows the tunnel to the USB3 upstream
1941 * adapter and back. Returns the discovered tunnel or %NULL if there was
1942 * no tunnel.
1947 struct tb_tunnel *tunnel; in tb_tunnel_discover_usb3() local
1953 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_discover_usb3()
1954 if (!tunnel) in tb_tunnel_discover_usb3()
1957 tunnel->activate = tb_usb3_activate; in tb_tunnel_discover_usb3()
1958 tunnel->src_port = down; in tb_tunnel_discover_usb3()
1966 &tunnel->dst_port, "USB3 Down", alloc_hopid); in tb_tunnel_discover_usb3()
1972 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_discover_usb3()
1973 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]); in tb_tunnel_discover_usb3()
1975 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL, in tb_tunnel_discover_usb3()
1979 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_discover_usb3()
1980 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]); in tb_tunnel_discover_usb3()
1982 /* Validate that the tunnel is complete */ in tb_tunnel_discover_usb3()
1983 if (!tb_port_is_usb3_up(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
1984 tb_port_warn(tunnel->dst_port, in tb_tunnel_discover_usb3()
1989 if (down != tunnel->src_port) { in tb_tunnel_discover_usb3()
1990 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n"); in tb_tunnel_discover_usb3()
1994 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
1995 tb_tunnel_warn(tunnel, in tb_tunnel_discover_usb3()
1996 "tunnel is not fully activated, cleaning up\n"); in tb_tunnel_discover_usb3()
2005 * hop tunnel. in tb_tunnel_discover_usb3()
2008 &tunnel->allocated_up, &tunnel->allocated_down); in tb_tunnel_discover_usb3()
2012 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n", in tb_tunnel_discover_usb3()
2013 tunnel->allocated_up, tunnel->allocated_down); in tb_tunnel_discover_usb3()
2015 tunnel->init = tb_usb3_init; in tb_tunnel_discover_usb3()
2016 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_discover_usb3()
2017 tunnel->release_unused_bandwidth = in tb_tunnel_discover_usb3()
2019 tunnel->reclaim_available_bandwidth = in tb_tunnel_discover_usb3()
2023 tb_tunnel_dbg(tunnel, "discovered\n"); in tb_tunnel_discover_usb3()
2024 return tunnel; in tb_tunnel_discover_usb3()
2027 tb_tunnel_deactivate(tunnel); in tb_tunnel_discover_usb3()
2029 tb_tunnel_free(tunnel); in tb_tunnel_discover_usb3()
2035 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
2039 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
2041 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
2044 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
2053 struct tb_tunnel *tunnel; in tb_tunnel_alloc_usb3() local
2059 * USB3 tunnel. in tb_tunnel_alloc_usb3()
2068 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n", in tb_tunnel_alloc_usb3()
2072 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n"); in tb_tunnel_alloc_usb3()
2077 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3); in tb_tunnel_alloc_usb3()
2078 if (!tunnel) in tb_tunnel_alloc_usb3()
2081 tunnel->activate = tb_usb3_activate; in tb_tunnel_alloc_usb3()
2082 tunnel->src_port = down; in tb_tunnel_alloc_usb3()
2083 tunnel->dst_port = up; in tb_tunnel_alloc_usb3()
2084 tunnel->max_up = max_up; in tb_tunnel_alloc_usb3()
2085 tunnel->max_down = max_down; in tb_tunnel_alloc_usb3()
2090 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2094 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_alloc_usb3()
2099 tb_tunnel_free(tunnel); in tb_tunnel_alloc_usb3()
2103 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_alloc_usb3()
2106 tunnel->allocated_up = max_rate; in tb_tunnel_alloc_usb3()
2107 tunnel->allocated_down = max_rate; in tb_tunnel_alloc_usb3()
2109 tunnel->init = tb_usb3_init; in tb_tunnel_alloc_usb3()
2110 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth; in tb_tunnel_alloc_usb3()
2111 tunnel->release_unused_bandwidth = in tb_tunnel_alloc_usb3()
2113 tunnel->reclaim_available_bandwidth = in tb_tunnel_alloc_usb3()
2117 return tunnel; in tb_tunnel_alloc_usb3()
2121 * tb_tunnel_free() - free a tunnel
2122 * @tunnel: Tunnel to be freed
2124 * Frees a tunnel. The tunnel does not need to be deactivated.
2126 void tb_tunnel_free(struct tb_tunnel *tunnel) in tb_tunnel_free() argument
2130 if (!tunnel) in tb_tunnel_free()
2133 if (tunnel->deinit) in tb_tunnel_free()
2134 tunnel->deinit(tunnel); in tb_tunnel_free()
2136 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_free()
2137 if (tunnel->paths[i]) in tb_tunnel_free()
2138 tb_path_free(tunnel->paths[i]); in tb_tunnel_free()
2141 kfree(tunnel->paths); in tb_tunnel_free()
2142 kfree(tunnel); in tb_tunnel_free()
2147 * @tunnel: Tunnel to check
2149 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) in tb_tunnel_is_invalid() argument
2153 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_invalid()
2154 WARN_ON(!tunnel->paths[i]->activated); in tb_tunnel_is_invalid()
2155 if (tb_path_is_invalid(tunnel->paths[i])) in tb_tunnel_is_invalid()
2163 * tb_tunnel_restart() - activate a tunnel after a hardware reset
2164 * @tunnel: Tunnel to restart
2168 int tb_tunnel_restart(struct tb_tunnel *tunnel) in tb_tunnel_restart() argument
2172 tb_tunnel_dbg(tunnel, "activating\n"); in tb_tunnel_restart()
2178 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2179 if (tunnel->paths[i]->activated) { in tb_tunnel_restart()
2180 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_restart()
2181 tunnel->paths[i]->activated = false; in tb_tunnel_restart()
2185 if (tunnel->init) { in tb_tunnel_restart()
2186 res = tunnel->init(tunnel); in tb_tunnel_restart()
2191 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_restart()
2192 res = tb_path_activate(tunnel->paths[i]); in tb_tunnel_restart()
2197 if (tunnel->activate) { in tb_tunnel_restart()
2198 res = tunnel->activate(tunnel, true); in tb_tunnel_restart()
2206 tb_tunnel_warn(tunnel, "activation failed\n"); in tb_tunnel_restart()
2207 tb_tunnel_deactivate(tunnel); in tb_tunnel_restart()
2212 * tb_tunnel_activate() - activate a tunnel
2213 * @tunnel: Tunnel to activate
2217 int tb_tunnel_activate(struct tb_tunnel *tunnel) in tb_tunnel_activate() argument
2221 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_activate()
2222 if (tunnel->paths[i]->activated) { in tb_tunnel_activate()
2223 tb_tunnel_WARN(tunnel, in tb_tunnel_activate()
2224 "trying to activate an already activated tunnel\n"); in tb_tunnel_activate()
2229 return tb_tunnel_restart(tunnel); in tb_tunnel_activate()
2233 * tb_tunnel_deactivate() - deactivate a tunnel
2234 * @tunnel: Tunnel to deactivate
2236 void tb_tunnel_deactivate(struct tb_tunnel *tunnel) in tb_tunnel_deactivate() argument
2240 tb_tunnel_dbg(tunnel, "deactivating\n"); in tb_tunnel_deactivate()
2242 if (tunnel->activate) in tb_tunnel_deactivate()
2243 tunnel->activate(tunnel, false); in tb_tunnel_deactivate()
2245 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_deactivate()
2246 if (tunnel->paths[i] && tunnel->paths[i]->activated) in tb_tunnel_deactivate()
2247 tb_path_deactivate(tunnel->paths[i]); in tb_tunnel_deactivate()
2252 * tb_tunnel_port_on_path() - Does the tunnel go through port
2253 * @tunnel: Tunnel to check
2256 * Returns true if @tunnel goes through @port (direction does not matter),
2259 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel, in tb_tunnel_port_on_path() argument
2264 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_port_on_path()
2265 if (!tunnel->paths[i]) in tb_tunnel_port_on_path()
2268 if (tb_path_port_on_path(tunnel->paths[i], port)) in tb_tunnel_port_on_path()
2275 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel) in tb_tunnel_is_active() argument
2279 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_is_active()
2280 if (!tunnel->paths[i]) in tb_tunnel_is_active()
2282 if (!tunnel->paths[i]->activated) in tb_tunnel_is_active()
2291 * @tunnel: Tunnel to check
2295 * Returns maximum possible bandwidth this tunnel can go if not limited
2296 * by other bandwidth clients. If the tunnel does not support this
2299 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up, in tb_tunnel_maximum_bandwidth() argument
2302 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_maximum_bandwidth()
2305 if (tunnel->maximum_bandwidth) in tb_tunnel_maximum_bandwidth()
2306 return tunnel->maximum_bandwidth(tunnel, max_up, max_down); in tb_tunnel_maximum_bandwidth()
2311 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2312 * @tunnel: Tunnel to check
2317 * Returns the bandwidth allocated for the tunnel. This may be higher
2318 * than what the tunnel actually consumes.
2320 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up, in tb_tunnel_allocated_bandwidth() argument
2323 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_allocated_bandwidth()
2326 if (tunnel->allocated_bandwidth) in tb_tunnel_allocated_bandwidth()
2327 return tunnel->allocated_bandwidth(tunnel, allocated_up, in tb_tunnel_allocated_bandwidth()
2333 * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
2334 * @tunnel: Tunnel whose bandwidth allocation to change
2338 * Tries to change tunnel bandwidth allocation. If succeeds returns %0
2343 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up, in tb_tunnel_alloc_bandwidth() argument
2346 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_alloc_bandwidth()
2349 if (tunnel->alloc_bandwidth) in tb_tunnel_alloc_bandwidth()
2350 return tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down); in tb_tunnel_alloc_bandwidth()
2356 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2357 * @tunnel: Tunnel to check
2363 * Stores the amount of isochronous bandwidth @tunnel consumes in
2367 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up, in tb_tunnel_consumed_bandwidth() argument
2372 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_consumed_bandwidth()
2375 if (tunnel->consumed_bandwidth) { in tb_tunnel_consumed_bandwidth()
2378 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw); in tb_tunnel_consumed_bandwidth()
2382 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw, in tb_tunnel_consumed_bandwidth()
2397 * @tunnel: Tunnel whose unused bandwidth to release
2399 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
2404 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel) in tb_tunnel_release_unused_bandwidth() argument
2406 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_release_unused_bandwidth()
2409 if (tunnel->release_unused_bandwidth) { in tb_tunnel_release_unused_bandwidth()
2412 ret = tunnel->release_unused_bandwidth(tunnel); in tb_tunnel_release_unused_bandwidth()
2422 * @tunnel: Tunnel reclaiming available bandwidth
2428 * reclaimed by the tunnel). If nothing was reclaimed the values are
2431 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel, in tb_tunnel_reclaim_available_bandwidth() argument
2435 if (!tb_tunnel_is_active(tunnel)) in tb_tunnel_reclaim_available_bandwidth()
2438 if (tunnel->reclaim_available_bandwidth) in tb_tunnel_reclaim_available_bandwidth()
2439 tunnel->reclaim_available_bandwidth(tunnel, available_up, in tb_tunnel_reclaim_available_bandwidth()
2443 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel) in tb_tunnel_type_name() argument
2445 return tb_tunnel_names[tunnel->type]; in tb_tunnel_type_name()