Lines Matching +full:activate +full:- +full:to +full:- +full:activate
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - Tunneling support
54 * Number of credits we try to allocate for each DMA path if not limited
66 * according to USB4 v2 Connection Manager guide. This ends up reserving
87 return port->total_credits - port->ctl_credits; in tb_usable_credits()
91 * tb_available_credits() - Available credits for PCIe and DMA
92 * @port: Lane adapter to check
93 * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
99 const struct tb_switch *sw = port->sw; in tb_available_credits()
103 usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0; in tb_available_credits()
104 pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0; in tb_available_credits()
107 spare = min_not_zero(sw->max_dma_credits, dma_credits); in tb_available_credits()
120 if (sw->min_dp_aux_credits + sw->min_dp_main_credits) in tb_available_credits()
121 ndp = (credits - (usb3 + pcie + spare)) / in tb_available_credits()
122 (sw->min_dp_aux_credits + sw->min_dp_main_credits); in tb_available_credits()
128 credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits); in tb_available_credits()
129 credits -= usb3; in tb_available_credits()
139 struct tb_port *out_port = hop->out_port; in tb_init_pm_support()
140 struct tb_port *in_port = hop->in_port; in tb_init_pm_support()
143 usb4_switch_version(in_port->sw) >= 2) in tb_init_pm_support()
144 hop->pm_support = true; in tb_init_pm_support()
156 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); in tb_tunnel_alloc()
157 if (!tunnel->paths) { 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()
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()
195 if (ret != -ENODEV) in tb_pci_set_ext_encapsulation()
204 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) in tb_pci_activate() argument
208 if (activate) { in tb_pci_activate()
209 res = tb_pci_set_ext_encapsulation(tunnel, activate); in tb_pci_activate()
214 if (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()
222 if (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()
236 struct tb_port *port = hop->in_port; in tb_pci_init_credits()
237 struct tb_switch *sw = port->sw; in tb_pci_init_credits()
244 credits = min(sw->max_pcie_credits, available); in tb_pci_init_credits()
247 return -ENOSPC; in tb_pci_init_credits()
252 credits = port->bonded ? 32 : 16; in tb_pci_init_credits()
257 hop->initial_credits = credits; in tb_pci_init_credits()
265 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; in tb_pci_init_path()
266 path->egress_shared_buffer = TB_PATH_NONE; in tb_pci_init_path()
267 path->ingress_fc_enable = TB_PATH_ALL; in tb_pci_init_path()
268 path->ingress_shared_buffer = TB_PATH_NONE; in tb_pci_init_path()
269 path->priority = TB_PCI_PRIORITY; in tb_pci_init_path()
270 path->weight = TB_PCI_WEIGHT; in tb_pci_init_path()
271 path->drop_packages = 0; in tb_pci_init_path()
285 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
286 * @tb: Pointer to the domain structure
290 * If @down adapter is active, follows the tunnel to the PCIe upstream
307 tunnel->activate = tb_pci_activate; in tb_tunnel_discover_pci()
308 tunnel->src_port = down; in tb_tunnel_discover_pci()
315 path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1, 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()
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()
346 if (!tb_pci_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_pci()
364 * tb_tunnel_alloc_pci() - allocate a pci tunnel
365 * @tb: Pointer to the domain structure
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()
412 * tb_tunnel_reserved_pci() - Amount of bandwidth to reserve for PCIe
414 * @reserved_up: Upstream bandwidth in Mb/s to reserve
415 * @reserved_down: Downstream bandwidth in Mb/s to reserve
417 * Can be called to any connected lane 0 adapter to find out how much
418 * bandwidth needs to be left in reserve for possible PCIe bulk traffic.
419 * Returns true if there is something to be reserved and writes the
420 * amount to @reserved_down/@reserved_up. Otherwise returns false and
426 if (WARN_ON_ONCE(!port->remote)) in tb_tunnel_reserved_pci()
437 if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_UP)) in tb_tunnel_reserved_pci()
439 if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_DOWN)) in tb_tunnel_reserved_pci()
442 if (!tb_switch_find_port(port->sw, TB_TYPE_PCIE_DOWN)) in tb_tunnel_reserved_pci()
444 if (!tb_switch_find_port(port->remote->sw, TB_TYPE_PCIE_UP)) in tb_tunnel_reserved_pci()
469 /* Both ends need to support this */ in tb_dp_cm_handshake()
470 if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw)) in tb_dp_cm_handshake()
474 out->cap_adap + DP_STATUS_CTRL, 1); in tb_dp_cm_handshake()
481 out->cap_adap + DP_STATUS_CTRL, 1); in tb_dp_cm_handshake()
487 out->cap_adap + DP_STATUS_CTRL, 1); in tb_dp_cm_handshake()
495 return -ETIMEDOUT; in tb_dp_cm_handshake()
547 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate); in tb_dp_cap_set_rate()
586 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n", in tb_dp_cap_set_lanes()
650 return -ENOSR; in tb_dp_reduce_bandwidth()
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()
661 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for in tb_dp_xchg_caps()
664 if (in->sw->generation < 2 || out->sw->generation < 2) in tb_dp_xchg_caps()
677 in->cap_adap + DP_LOCAL_CAP, 1); in tb_dp_xchg_caps()
682 out->cap_adap + DP_LOCAL_CAP, 1); in tb_dp_xchg_caps()
686 /* Write IN local caps to OUT remote caps */ in tb_dp_xchg_caps()
688 out->cap_adap + DP_REMOTE_CAP, 1); in tb_dp_xchg_caps()
700 * if we need to reduce bandwidth to fit there. 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()
727 "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n", in tb_dp_xchg_caps()
731 * Set new rate and number of lanes before writing it to in tb_dp_xchg_caps()
743 if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) { in tb_dp_xchg_caps()
749 in->cap_adap + DP_REMOTE_CAP, 1); in tb_dp_xchg_caps()
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()
768 ret = usb4_dp_port_set_group_id(in, in->group->index); in tb_dp_bandwidth_alloc_mode_enable()
773 * Get the non-reduced rate and lanes based on the lowest in tb_dp_bandwidth_alloc_mode_enable()
777 in->cap_adap + DP_LOCAL_CAP, 1); in tb_dp_bandwidth_alloc_mode_enable()
782 out->cap_adap + DP_LOCAL_CAP, 1); 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()
822 * Returns -EINVAL if granularity above is outside of the in tb_dp_bandwidth_alloc_mode_enable()
832 * estimation was set to. 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()
856 struct tb_port *in = tunnel->src_port; in tb_dp_init()
857 struct tb_switch *sw = in->sw; in tb_dp_init()
858 struct tb *tb = in->sw->tb; in tb_dp_init()
873 ret = usb4_dp_port_set_cm_id(in, tb->index); in tb_dp_init()
882 struct tb_port *in = tunnel->src_port; in tb_dp_deinit()
900 paths = tunnel->paths; in tb_dp_activate()
901 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1; in tb_dp_activate()
903 tb_dp_port_set_hops(tunnel->src_port, in tb_dp_activate()
904 paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index, in tb_dp_activate()
905 paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index, in tb_dp_activate()
906 paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index); in tb_dp_activate()
908 tb_dp_port_set_hops(tunnel->dst_port, in tb_dp_activate()
909 paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index, in tb_dp_activate()
910 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index, in tb_dp_activate()
911 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index); 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()
929 /* max_bw is rounded up to next granularity */
933 struct tb_port *in = tunnel->src_port; in tb_dp_bandwidth_mode_maximum_bandwidth()
938 * DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX in tb_dp_bandwidth_mode_maximum_bandwidth()
939 * read parameter values so this so we can use this to determine in tb_dp_bandwidth_mode_maximum_bandwidth()
945 in->cap_adap + DP_LOCAL_CAP, 1); in tb_dp_bandwidth_mode_maximum_bandwidth()
959 * non-reduced rate and lanes. in tb_dp_bandwidth_mode_maximum_bandwidth()
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()
987 return -EOPNOTSUPP; in tb_dp_bandwidth_mode_consumed_bandwidth()
989 if (!tunnel->bw_mode) in tb_dp_bandwidth_mode_consumed_bandwidth()
990 return -EOPNOTSUPP; in tb_dp_bandwidth_mode_consumed_bandwidth()
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()
1049 return tunnel->consumed_bandwidth(tunnel, allocated_up, in tb_dp_allocated_bandwidth()
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()
1061 return -EOPNOTSUPP; in tb_dp_alloc_bandwidth()
1083 /* Now we can use BW mode registers to figure out the bandwidth */ in tb_dp_alloc_bandwidth()
1084 /* TODO: need to handle discovery too */ in tb_dp_alloc_bandwidth()
1085 tunnel->bw_mode = true; in tb_dp_alloc_bandwidth()
1092 struct tb_port *in = tunnel->src_port; in tb_dp_wait_dprx()
1103 in->cap_adap + DP_COMMON_CAP, 1); in tb_dp_wait_dprx()
1115 return -ETIMEDOUT; in tb_dp_wait_dprx()
1122 struct tb_port *in = tunnel->src_port; in tb_dp_read_cap()
1134 return -EINVAL; in tb_dp_read_cap()
1141 ret = tb_port_read(in, &val, TB_CFG_PORT, in->cap_adap + cap, 1); in tb_dp_read_cap()
1153 struct tb_port *in = tunnel->src_port; in tb_dp_maximum_bandwidth()
1157 return -EOPNOTSUPP; in tb_dp_maximum_bandwidth()
1163 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_maximum_bandwidth()
1177 struct tb_port *in = tunnel->src_port; in tb_dp_consumed_bandwidth()
1178 const struct tb_switch *sw = in->sw; in tb_dp_consumed_bandwidth()
1191 if (ret != -EOPNOTSUPP) in tb_dp_consumed_bandwidth()
1204 if (ret == -ETIMEDOUT) in tb_dp_consumed_bandwidth()
1213 } else if (sw->generation >= 2) { in tb_dp_consumed_bandwidth()
1224 if (tb_port_path_direction_downstream(in, tunnel->dst_port)) { in tb_dp_consumed_bandwidth()
1237 struct tb_port *port = hop->in_port; in tb_dp_init_aux_credits()
1238 struct tb_switch *sw = port->sw; in tb_dp_init_aux_credits()
1241 hop->initial_credits = sw->min_dp_aux_credits; in tb_dp_init_aux_credits()
1243 hop->initial_credits = 1; in tb_dp_init_aux_credits()
1250 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; in tb_dp_init_aux_path()
1251 path->egress_shared_buffer = TB_PATH_NONE; in tb_dp_init_aux_path()
1252 path->ingress_fc_enable = TB_PATH_ALL; in tb_dp_init_aux_path()
1253 path->ingress_shared_buffer = TB_PATH_NONE; in tb_dp_init_aux_path()
1254 path->priority = TB_DP_AUX_PRIORITY; in tb_dp_init_aux_path()
1255 path->weight = TB_DP_AUX_WEIGHT; in tb_dp_init_aux_path()
1266 struct tb_port *port = hop->in_port; in tb_dp_init_video_credits()
1267 struct tb_switch *sw = port->sw; in tb_dp_init_video_credits()
1277 * tunneling we can use that to figure out how many DP in tb_dp_init_video_credits()
1280 nfc_credits = port->config.nfc_credits & in tb_dp_init_video_credits()
1282 if (nfc_credits / sw->min_dp_main_credits > max_dp_streams) in tb_dp_init_video_credits()
1283 return -ENOSPC; in tb_dp_init_video_credits()
1285 hop->nfc_credits = sw->min_dp_main_credits; in tb_dp_init_video_credits()
1287 hop->nfc_credits = min(port->total_credits - 2, 12U); in tb_dp_init_video_credits()
1297 path->egress_fc_enable = TB_PATH_NONE; in tb_dp_init_video_path()
1298 path->egress_shared_buffer = TB_PATH_NONE; in tb_dp_init_video_path()
1299 path->ingress_fc_enable = TB_PATH_NONE; in tb_dp_init_video_path()
1300 path->ingress_shared_buffer = TB_PATH_NONE; in tb_dp_init_video_path()
1301 path->priority = TB_DP_VIDEO_PRIORITY; in tb_dp_init_video_path()
1302 path->weight = TB_DP_VIDEO_WEIGHT; in tb_dp_init_video_path()
1322 in = tunnel->src_port; in tb_dp_dump()
1323 out = tunnel->dst_port; in tb_dp_dump()
1326 in->cap_adap + DP_LOCAL_CAP, 1)) in tb_dp_dump()
1337 out->cap_adap + DP_LOCAL_CAP, 1)) in tb_dp_dump()
1348 in->cap_adap + DP_REMOTE_CAP, 1)) in tb_dp_dump()
1359 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
1360 * @tb: Pointer to the domain structure
1364 * If @in adapter is active, follows the tunnel to the DP out adapter
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()
1393 path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1, 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()
1404 path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX", 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()
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()
1449 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1450 * @tb: Pointer to the domain structure
1473 if (WARN_ON(!in->cap_adap || !out->cap_adap)) 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()
1493 pm_support = usb4_switch_version(in->sw) >= 2; in tb_tunnel_alloc_dp()
1525 const struct tb_switch *sw = port->sw; in tb_dma_available_credits()
1530 credits -= sw->max_pcie_credits; in tb_dma_available_credits()
1531 credits -= port->dma_credits; in tb_dma_available_credits()
1538 struct tb_port *port = hop->in_port; in tb_dma_reserve_credits()
1544 * Need to have at least TB_MIN_DMA_CREDITS, otherwise in tb_dma_reserve_credits()
1548 return -ENOSPC; in tb_dma_reserve_credits()
1551 credits--; in tb_dma_reserve_credits()
1556 port->dma_credits += credits; in tb_dma_reserve_credits()
1559 credits = port->bonded ? 14 : 6; in tb_dma_reserve_credits()
1561 credits = min(port->total_credits, credits); in tb_dma_reserve_credits()
1564 hop->initial_credits = credits; in tb_dma_reserve_credits()
1568 /* Path from lane adapter to NHI */
1574 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; in tb_dma_init_rx_path()
1575 path->ingress_fc_enable = TB_PATH_ALL; in tb_dma_init_rx_path()
1576 path->egress_shared_buffer = TB_PATH_NONE; in tb_dma_init_rx_path()
1577 path->ingress_shared_buffer = TB_PATH_NONE; in tb_dma_init_rx_path()
1578 path->priority = TB_DMA_PRIORITY; in tb_dma_init_rx_path()
1579 path->weight = TB_DMA_WEIGHT; in tb_dma_init_rx_path()
1580 path->clear_fc = true; in tb_dma_init_rx_path()
1583 * First lane adapter is the one connected to the remote host. in tb_dma_init_rx_path()
1587 hop = &path->hops[0]; in tb_dma_init_rx_path()
1588 tmp = min(tb_usable_credits(hop->in_port), credits); in tb_dma_init_rx_path()
1589 hop->initial_credits = tmp; in tb_dma_init_rx_path()
1590 hop->in_port->dma_credits += tmp; in tb_dma_init_rx_path()
1592 for (i = 1; i < path->path_length; i++) { in tb_dma_init_rx_path()
1595 ret = tb_dma_reserve_credits(&path->hops[i], credits); in tb_dma_init_rx_path()
1603 /* Path from NHI to lane adapter */
1608 path->egress_fc_enable = TB_PATH_ALL; in tb_dma_init_tx_path()
1609 path->ingress_fc_enable = TB_PATH_ALL; in tb_dma_init_tx_path()
1610 path->egress_shared_buffer = TB_PATH_NONE; in tb_dma_init_tx_path()
1611 path->ingress_shared_buffer = TB_PATH_NONE; in tb_dma_init_tx_path()
1612 path->priority = TB_DMA_PRIORITY; in tb_dma_init_tx_path()
1613 path->weight = TB_DMA_WEIGHT; in tb_dma_init_tx_path()
1614 path->clear_fc = true; in tb_dma_init_tx_path()
1629 struct tb_port *port = hop->in_port; in tb_dma_release_credits()
1632 port->dma_credits -= hop->initial_credits; in tb_dma_release_credits()
1635 hop->initial_credits); in tb_dma_release_credits()
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
1660 * @tb: Pointer to the domain structure
1662 * @dst: Destination null port which the other domain is connected to
1664 * @transmit_ring: NHI ring number used to send packets towards the
1665 * other domain. Set to %-1 if TX path is not needed.
1667 * @receive_ring: NHI ring number used to receive packets from the
1668 * other domain. Set to %-1 if RX path is not needed.
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()
1702 credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits); in tb_tunnel_alloc_dma()
1709 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1721 tunnel->paths[i++] = path; in tb_tunnel_alloc_dma()
1736 * tb_tunnel_match_dma() - Match DMA tunnel
1737 * @tunnel: Tunnel to match
1738 * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
1739 * @transmit_ring: NHI ring number used to send packets towards the
1740 * other domain. Pass %-1 to ignore.
1741 * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
1742 * @receive_ring: NHI ring number used to receive packets from the
1743 * other domain. Pass %-1 to ignore.
1745 * This function can be used to match specific DMA tunnel, if there are
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()
1764 if (tb_port_is_nhi(path->hops[0].in_port)) in tb_tunnel_match_dma()
1766 else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port)) in tb_tunnel_match_dma()
1774 (tx_path->hops[0].in_hop_index != transmit_ring)) in tb_tunnel_match_dma()
1777 (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path)) in tb_tunnel_match_dma()
1785 (rx_path->hops[0].in_hop_index != receive_path)) in tb_tunnel_match_dma()
1788 (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring)) in tb_tunnel_match_dma()
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()
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()
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()
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()
1892 /* No need to reclaim if already at maximum */ 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()
1906 /* If no changes no need to do more */ 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()
1930 struct tb_port *port = hop->in_port; in tb_usb3_init_credits()
1931 struct tb_switch *sw = port->sw; in tb_usb3_init_credits()
1935 credits = sw->max_usb3_credits; in tb_usb3_init_credits()
1938 credits = port->bonded ? 32 : 16; in tb_usb3_init_credits()
1943 hop->initial_credits = credits; in tb_usb3_init_credits()
1950 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; in tb_usb3_init_path()
1951 path->egress_shared_buffer = TB_PATH_NONE; in tb_usb3_init_path()
1952 path->ingress_fc_enable = TB_PATH_ALL; in tb_usb3_init_path()
1953 path->ingress_shared_buffer = TB_PATH_NONE; in tb_usb3_init_path()
1954 path->priority = TB_USB3_PRIORITY; in tb_usb3_init_path()
1955 path->weight = TB_USB3_WEIGHT; in tb_usb3_init_path()
1956 path->drop_packages = 0; in tb_usb3_init_path()
1963 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
1964 * @tb: Pointer to the domain structure
1968 * If @down adapter is active, follows the tunnel to the USB3 upstream
1985 tunnel->activate = tb_usb3_activate; in tb_tunnel_discover_usb3()
1986 tunnel->src_port = down; in tb_tunnel_discover_usb3()
1993 path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1, 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()
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()
2022 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) { in tb_tunnel_discover_usb3()
2028 if (!tb_route(down->sw)) { in tb_tunnel_discover_usb3()
2036 &tunnel->allocated_up, &tunnel->allocated_down); 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()
2063 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
2064 * @tb: Pointer to the domain structure
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()
2122 tunnel->paths[TB_USB3_PATH_DOWN] = path; in tb_tunnel_alloc_usb3()
2131 tunnel->paths[TB_USB3_PATH_UP] = path; in tb_tunnel_alloc_usb3()
2133 if (!tb_route(down->sw)) { 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()
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.
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()
2174 * tb_tunnel_is_invalid - check whether an activated path is still valid
2175 * @tunnel: Tunnel to check
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
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()
2240 * tb_tunnel_activate() - activate a tunnel
2241 * @tunnel: Tunnel to activate
2249 for (i = 0; i < tunnel->npaths; i++) { in tb_tunnel_activate()
2250 if (tunnel->paths[i]->activated) { in tb_tunnel_activate()
2252 "trying to activate an already activated tunnel\n"); in tb_tunnel_activate()
2253 return -EINVAL; in tb_tunnel_activate()
2261 * tb_tunnel_deactivate() - deactivate a tunnel
2262 * @tunnel: Tunnel to 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
2282 * @port: Port to check
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()
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()
2318 * tb_tunnel_maximum_bandwidth() - Return maximum possible bandwidth
2319 * @tunnel: Tunnel to check
2325 * returns %-EOPNOTSUPP.
2331 return -EINVAL; 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()
2335 return -EOPNOTSUPP; in tb_tunnel_maximum_bandwidth()
2339 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2340 * @tunnel: Tunnel to check
2352 return -EINVAL; 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()
2357 return -EOPNOTSUPP; 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
2367 * and updates @alloc_up and @alloc_down to that was actually allocated
2375 return -EINVAL; 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()
2380 return -EOPNOTSUPP; in tb_tunnel_alloc_bandwidth()
2384 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2385 * @tunnel: Tunnel to check
2386 * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
2388 * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
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()
2424 * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
2425 * @tunnel: Tunnel whose unused bandwidth to release
2428 * moment) this function makes it to release all the 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()
2449 * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
2455 * the variables accordingly (e.g decreases both according to what was
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()
2473 return tb_tunnel_names[tunnel->type]; in tb_tunnel_type_name()