Lines Matching refs:tunnel

13 #include "tunnel.h"
42 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
44 struct tb_tunnel *__tunnel = (tunnel); \
54 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
55 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
56 #define tb_tunnel_warn(tunnel, fmt, arg...) \
57 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
58 #define tb_tunnel_info(tunnel, fmt, arg...) \
59 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
60 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
61 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
66 struct tb_tunnel *tunnel;
68 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
69 if (!tunnel)
72 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
73 if (!tunnel->paths) {
74 tb_tunnel_free(tunnel);
78 INIT_LIST_HEAD(&tunnel->list);
79 tunnel->tb = tb;
80 tunnel->npaths = npaths;
81 tunnel->type = type;
83 return tunnel;
86 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
90 res = tb_pci_port_enable(tunnel->src_port, activate);
94 if (tb_port_is_pcie_up(tunnel->dst_port))
95 return tb_pci_port_enable(tunnel->dst_port, activate);
137 * If @down adapter is active, follows the tunnel to the PCIe upstream
138 * adapter and back. Returns the discovered tunnel or %NULL if there was
139 * no tunnel.
143 struct tb_tunnel *tunnel;
149 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
150 if (!tunnel)
153 tunnel->activate = tb_pci_activate;
154 tunnel->src_port = down;
162 &tunnel->dst_port, "PCIe Up");
168 tunnel->paths[TB_PCI_PATH_UP] = path;
169 tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]);
171 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
175 tunnel->paths[TB_PCI_PATH_DOWN] = path;
176 tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]);
178 /* Validate that the tunnel is complete */
179 if (!tb_port_is_pcie_up(tunnel->dst_port)) {
180 tb_port_warn(tunnel->dst_port,
185 if (down != tunnel->src_port) {
186 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
190 if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
191 tb_tunnel_warn(tunnel,
192 "tunnel is not fully activated, cleaning up\n");
196 tb_tunnel_dbg(tunnel, "discovered\n");
197 return tunnel;
200 tb_tunnel_deactivate(tunnel);
202 tb_tunnel_free(tunnel);
208 * tb_tunnel_alloc_pci() - allocate a pci tunnel
213 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
221 struct tb_tunnel *tunnel;
224 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
225 if (!tunnel)
228 tunnel->activate = tb_pci_activate;
229 tunnel->src_port = down;
230 tunnel->dst_port = up;
235 tb_tunnel_free(tunnel);
239 tunnel->paths[TB_PCI_PATH_DOWN] = path;
244 tb_tunnel_free(tunnel);
248 tunnel->paths[TB_PCI_PATH_UP] = path;
250 return tunnel;
421 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
424 struct tb_port *out = tunnel->dst_port;
425 struct tb_port *in = tunnel->src_port;
466 * If the tunnel bandwidth is limited (max_bw is set) then see
476 max_bw = tunnel->max_down;
478 max_bw = tunnel->max_up;
487 tb_port_info(out, "not enough bandwidth for DP tunnel\n");
507 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
515 paths = tunnel->paths;
518 tb_dp_port_set_hops(tunnel->src_port,
523 tb_dp_port_set_hops(tunnel->dst_port,
528 tb_dp_port_hpd_clear(tunnel->src_port);
529 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
530 if (tb_port_is_dpout(tunnel->dst_port))
531 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
534 ret = tb_dp_port_enable(tunnel->src_port, active);
538 if (tb_port_is_dpout(tunnel->dst_port))
539 return tb_dp_port_enable(tunnel->dst_port, active);
544 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
547 struct tb_port *in = tunnel->src_port;
557 * for active tunnel.
594 if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
648 * If @in adapter is active, follows the tunnel to the DP out adapter
649 * and back. Returns the discovered tunnel or %NULL if there was no
650 * tunnel.
652 * Return: DP tunnel or %NULL if no tunnel found.
656 struct tb_tunnel *tunnel;
663 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
664 if (!tunnel)
667 tunnel->init = tb_dp_xchg_caps;
668 tunnel->activate = tb_dp_activate;
669 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
670 tunnel->src_port = in;
673 &tunnel->dst_port, "Video");
679 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
680 tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], true);
685 tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
686 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
688 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
692 tunnel->paths[TB_DP_AUX_PATH_IN] = path;
693 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
695 /* Validate that the tunnel is complete */
696 if (!tb_port_is_dpout(tunnel->dst_port)) {
701 if (!tb_dp_port_is_enabled(tunnel->dst_port))
704 if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
707 if (port != tunnel->src_port) {
708 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
712 tb_tunnel_dbg(tunnel, "discovered\n");
713 return tunnel;
716 tb_tunnel_deactivate(tunnel);
718 tb_tunnel_free(tunnel);
724 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
728 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
730 * @max_down: Maximum available downstream bandwidth for the DP tunnel
733 * Allocates a tunnel between @in and @out that is capable of tunneling
742 struct tb_tunnel *tunnel;
749 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
750 if (!tunnel)
753 tunnel->init = tb_dp_xchg_caps;
754 tunnel->activate = tb_dp_activate;
755 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
756 tunnel->src_port = in;
757 tunnel->dst_port = out;
758 tunnel->max_up = max_up;
759 tunnel->max_down = max_down;
761 paths = tunnel->paths;
784 return tunnel;
787 tb_tunnel_free(tunnel);
800 static int tb_dma_activate(struct tb_tunnel *tunnel, bool active)
802 struct tb_port *nhi = tunnel->src_port;
827 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
845 struct tb_tunnel *tunnel;
849 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_DMA);
850 if (!tunnel)
853 tunnel->activate = tb_dma_activate;
854 tunnel->src_port = nhi;
855 tunnel->dst_port = dst;
861 tb_tunnel_free(tunnel);
866 tunnel->paths[TB_DMA_PATH_IN] = path;
870 tb_tunnel_free(tunnel);
874 tunnel->paths[TB_DMA_PATH_OUT] = path;
876 return tunnel;
896 static int tb_usb3_init(struct tb_tunnel *tunnel)
898 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
899 tunnel->allocated_up, tunnel->allocated_down);
901 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
902 &tunnel->allocated_up,
903 &tunnel->allocated_down);
906 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
910 res = tb_usb3_port_enable(tunnel->src_port, activate);
914 if (tb_port_is_usb3_up(tunnel->dst_port))
915 return tb_usb3_port_enable(tunnel->dst_port, activate);
920 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
927 *consumed_up = tunnel->allocated_up * (3 + 1) / 3;
928 *consumed_down = tunnel->allocated_down * (3 + 1) / 3;
932 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
936 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
937 &tunnel->allocated_up,
938 &tunnel->allocated_down);
942 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
943 tunnel->allocated_up, tunnel->allocated_down);
947 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
953 ret = usb4_usb3_port_actual_link_rate(tunnel->src_port);
955 tb_tunnel_warn(tunnel, "failed to read actual link rate\n");
959 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port);
961 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
973 if (tunnel->allocated_up >= max_rate &&
974 tunnel->allocated_down >= max_rate)
979 if (allocate_up < tunnel->allocated_up)
980 allocate_up = tunnel->allocated_up;
983 if (allocate_down < tunnel->allocated_down)
984 allocate_down = tunnel->allocated_down;
987 if (allocate_up == tunnel->allocated_up &&
988 allocate_down == tunnel->allocated_down)
991 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
994 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
998 tunnel->allocated_up = allocate_up;
999 *available_up -= tunnel->allocated_up;
1001 tunnel->allocated_down = allocate_down;
1002 *available_down -= tunnel->allocated_down;
1004 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
1005 tunnel->allocated_up, tunnel->allocated_down);
1029 * If @down adapter is active, follows the tunnel to the USB3 upstream
1030 * adapter and back. Returns the discovered tunnel or %NULL if there was
1031 * no tunnel.
1035 struct tb_tunnel *tunnel;
1041 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1042 if (!tunnel)
1045 tunnel->activate = tb_usb3_activate;
1046 tunnel->src_port = down;
1054 &tunnel->dst_port, "USB3 Down");
1060 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1061 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
1063 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
1067 tunnel->paths[TB_USB3_PATH_UP] = path;
1068 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
1070 /* Validate that the tunnel is complete */
1071 if (!tb_port_is_usb3_up(tunnel->dst_port)) {
1072 tb_port_warn(tunnel->dst_port,
1077 if (down != tunnel->src_port) {
1078 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1082 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
1083 tb_tunnel_warn(tunnel,
1084 "tunnel is not fully activated, cleaning up\n");
1093 * hop tunnel.
1096 &tunnel->allocated_up, &tunnel->allocated_down);
1100 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
1101 tunnel->allocated_up, tunnel->allocated_down);
1103 tunnel->init = tb_usb3_init;
1104 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1105 tunnel->release_unused_bandwidth =
1107 tunnel->reclaim_available_bandwidth =
1111 tb_tunnel_dbg(tunnel, "discovered\n");
1112 return tunnel;
1115 tb_tunnel_deactivate(tunnel);
1117 tb_tunnel_free(tunnel);
1123 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
1127 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
1129 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
1132 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
1141 struct tb_tunnel *tunnel;
1147 * USB3 tunnel.
1156 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n",
1160 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n");
1165 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1166 if (!tunnel)
1169 tunnel->activate = tb_usb3_activate;
1170 tunnel->src_port = down;
1171 tunnel->dst_port = up;
1172 tunnel->max_up = max_up;
1173 tunnel->max_down = max_down;
1178 tb_tunnel_free(tunnel);
1182 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1187 tb_tunnel_free(tunnel);
1191 tunnel->paths[TB_USB3_PATH_UP] = path;
1194 tunnel->allocated_up = max_rate;
1195 tunnel->allocated_down = max_rate;
1197 tunnel->init = tb_usb3_init;
1198 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1199 tunnel->release_unused_bandwidth =
1201 tunnel->reclaim_available_bandwidth =
1205 return tunnel;
1209 * tb_tunnel_free() - free a tunnel
1210 * @tunnel: Tunnel to be freed
1212 * Frees a tunnel. The tunnel does not need to be deactivated.
1214 void tb_tunnel_free(struct tb_tunnel *tunnel)
1218 if (!tunnel)
1221 for (i = 0; i < tunnel->npaths; i++) {
1222 if (tunnel->paths[i])
1223 tb_path_free(tunnel->paths[i]);
1226 kfree(tunnel->paths);
1227 kfree(tunnel);
1232 * @tunnel: Tunnel to check
1234 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
1238 for (i = 0; i < tunnel->npaths; i++) {
1239 WARN_ON(!tunnel->paths[i]->activated);
1240 if (tb_path_is_invalid(tunnel->paths[i]))
1248 * tb_tunnel_restart() - activate a tunnel after a hardware reset
1249 * @tunnel: Tunnel to restart
1253 int tb_tunnel_restart(struct tb_tunnel *tunnel)
1257 tb_tunnel_dbg(tunnel, "activating\n");
1263 for (i = 0; i < tunnel->npaths; i++) {
1264 if (tunnel->paths[i]->activated) {
1265 tb_path_deactivate(tunnel->paths[i]);
1266 tunnel->paths[i]->activated = false;
1270 if (tunnel->init) {
1271 res = tunnel->init(tunnel);
1276 for (i = 0; i < tunnel->npaths; i++) {
1277 res = tb_path_activate(tunnel->paths[i]);
1282 if (tunnel->activate) {
1283 res = tunnel->activate(tunnel, true);
1291 tb_tunnel_warn(tunnel, "activation failed\n");
1292 tb_tunnel_deactivate(tunnel);
1297 * tb_tunnel_activate() - activate a tunnel
1298 * @tunnel: Tunnel to activate
1302 int tb_tunnel_activate(struct tb_tunnel *tunnel)
1306 for (i = 0; i < tunnel->npaths; i++) {
1307 if (tunnel->paths[i]->activated) {
1308 tb_tunnel_WARN(tunnel,
1309 "trying to activate an already activated tunnel\n");
1314 return tb_tunnel_restart(tunnel);
1318 * tb_tunnel_deactivate() - deactivate a tunnel
1319 * @tunnel: Tunnel to deactivate
1321 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
1325 tb_tunnel_dbg(tunnel, "deactivating\n");
1327 if (tunnel->activate)
1328 tunnel->activate(tunnel, false);
1330 for (i = 0; i < tunnel->npaths; i++) {
1331 if (tunnel->paths[i] && tunnel->paths[i]->activated)
1332 tb_path_deactivate(tunnel->paths[i]);
1337 * tb_tunnel_port_on_path() - Does the tunnel go through port
1338 * @tunnel: Tunnel to check
1341 * Returns true if @tunnel goes through @port (direction does not matter),
1344 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
1349 for (i = 0; i < tunnel->npaths; i++) {
1350 if (!tunnel->paths[i])
1353 if (tb_path_port_on_path(tunnel->paths[i], port))
1360 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
1364 for (i = 0; i < tunnel->npaths; i++) {
1365 if (!tunnel->paths[i])
1367 if (!tunnel->paths[i]->activated)
1375 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
1376 * @tunnel: Tunnel to check
1382 * Stores the amount of isochronous bandwidth @tunnel consumes in
1386 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1391 if (!tb_tunnel_is_active(tunnel))
1394 if (tunnel->consumed_bandwidth) {
1397 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
1401 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw,
1416 * @tunnel: Tunnel whose unused bandwidth to release
1418 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
1423 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
1425 if (!tb_tunnel_is_active(tunnel))
1428 if (tunnel->release_unused_bandwidth) {
1431 ret = tunnel->release_unused_bandwidth(tunnel);
1441 * @tunnel: Tunnel reclaiming available bandwidth
1447 * reclaimed by the tunnel). If nothing was reclaimed the values are
1450 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1454 if (!tb_tunnel_is_active(tunnel))
1457 if (tunnel->reclaim_available_bandwidth)
1458 tunnel->reclaim_available_bandwidth(tunnel, available_up,