Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Thunderbolt driver - Tunneling support
0004  *
0005  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
0006  * Copyright (C) 2019, Intel Corporation
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/slab.h>
0011 #include <linux/list.h>
0012 
0013 #include "tunnel.h"
0014 #include "tb.h"
0015 
0016 /* PCIe adapters use always HopID of 8 for both directions */
0017 #define TB_PCI_HOPID            8
0018 
0019 #define TB_PCI_PATH_DOWN        0
0020 #define TB_PCI_PATH_UP          1
0021 
0022 /* USB3 adapters use always HopID of 8 for both directions */
0023 #define TB_USB3_HOPID           8
0024 
0025 #define TB_USB3_PATH_DOWN       0
0026 #define TB_USB3_PATH_UP         1
0027 
0028 /* DP adapters use HopID 8 for AUX and 9 for Video */
0029 #define TB_DP_AUX_TX_HOPID      8
0030 #define TB_DP_AUX_RX_HOPID      8
0031 #define TB_DP_VIDEO_HOPID       9
0032 
0033 #define TB_DP_VIDEO_PATH_OUT        0
0034 #define TB_DP_AUX_PATH_OUT      1
0035 #define TB_DP_AUX_PATH_IN       2
0036 
0037 /* Minimum number of credits needed for PCIe path */
0038 #define TB_MIN_PCIE_CREDITS     6U
0039 /*
0040  * Number of credits we try to allocate for each DMA path if not limited
0041  * by the host router baMaxHI.
0042  */
0043 #define TB_DMA_CREDITS          14U
0044 /* Minimum number of credits for DMA path */
0045 #define TB_MIN_DMA_CREDITS      1U
0046 
0047 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
0048 
0049 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...)                   \
0050     do {                                                            \
0051         struct tb_tunnel *__tunnel = (tunnel);                  \
0052         level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt,   \
0053               tb_route(__tunnel->src_port->sw),                 \
0054               __tunnel->src_port->port,                         \
0055               tb_route(__tunnel->dst_port->sw),                 \
0056               __tunnel->dst_port->port,                         \
0057               tb_tunnel_names[__tunnel->type],          \
0058               ## arg);                                          \
0059     } while (0)
0060 
0061 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
0062     __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
0063 #define tb_tunnel_warn(tunnel, fmt, arg...) \
0064     __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
0065 #define tb_tunnel_info(tunnel, fmt, arg...) \
0066     __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
0067 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
0068     __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
0069 
0070 static inline unsigned int tb_usable_credits(const struct tb_port *port)
0071 {
0072     return port->total_credits - port->ctl_credits;
0073 }
0074 
0075 /**
0076  * tb_available_credits() - Available credits for PCIe and DMA
0077  * @port: Lane adapter to check
0078  * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
0079  *          streams possible through this lane adapter
0080  */
0081 static unsigned int tb_available_credits(const struct tb_port *port,
0082                      size_t *max_dp_streams)
0083 {
0084     const struct tb_switch *sw = port->sw;
0085     int credits, usb3, pcie, spare;
0086     size_t ndp;
0087 
0088     usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
0089     pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
0090 
0091     if (tb_acpi_is_xdomain_allowed()) {
0092         spare = min_not_zero(sw->max_dma_credits, TB_DMA_CREDITS);
0093         /* Add some credits for potential second DMA tunnel */
0094         spare += TB_MIN_DMA_CREDITS;
0095     } else {
0096         spare = 0;
0097     }
0098 
0099     credits = tb_usable_credits(port);
0100     if (tb_acpi_may_tunnel_dp()) {
0101         /*
0102          * Maximum number of DP streams possible through the
0103          * lane adapter.
0104          */
0105         if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
0106             ndp = (credits - (usb3 + pcie + spare)) /
0107                   (sw->min_dp_aux_credits + sw->min_dp_main_credits);
0108         else
0109             ndp = 0;
0110     } else {
0111         ndp = 0;
0112     }
0113     credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
0114     credits -= usb3;
0115 
0116     if (max_dp_streams)
0117         *max_dp_streams = ndp;
0118 
0119     return credits > 0 ? credits : 0;
0120 }
0121 
0122 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
0123                      enum tb_tunnel_type type)
0124 {
0125     struct tb_tunnel *tunnel;
0126 
0127     tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
0128     if (!tunnel)
0129         return NULL;
0130 
0131     tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
0132     if (!tunnel->paths) {
0133         tb_tunnel_free(tunnel);
0134         return NULL;
0135     }
0136 
0137     INIT_LIST_HEAD(&tunnel->list);
0138     tunnel->tb = tb;
0139     tunnel->npaths = npaths;
0140     tunnel->type = type;
0141 
0142     return tunnel;
0143 }
0144 
0145 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
0146 {
0147     int res;
0148 
0149     res = tb_pci_port_enable(tunnel->src_port, activate);
0150     if (res)
0151         return res;
0152 
0153     if (tb_port_is_pcie_up(tunnel->dst_port))
0154         return tb_pci_port_enable(tunnel->dst_port, activate);
0155 
0156     return 0;
0157 }
0158 
0159 static int tb_pci_init_credits(struct tb_path_hop *hop)
0160 {
0161     struct tb_port *port = hop->in_port;
0162     struct tb_switch *sw = port->sw;
0163     unsigned int credits;
0164 
0165     if (tb_port_use_credit_allocation(port)) {
0166         unsigned int available;
0167 
0168         available = tb_available_credits(port, NULL);
0169         credits = min(sw->max_pcie_credits, available);
0170 
0171         if (credits < TB_MIN_PCIE_CREDITS)
0172             return -ENOSPC;
0173 
0174         credits = max(TB_MIN_PCIE_CREDITS, credits);
0175     } else {
0176         if (tb_port_is_null(port))
0177             credits = port->bonded ? 32 : 16;
0178         else
0179             credits = 7;
0180     }
0181 
0182     hop->initial_credits = credits;
0183     return 0;
0184 }
0185 
0186 static int tb_pci_init_path(struct tb_path *path)
0187 {
0188     struct tb_path_hop *hop;
0189 
0190     path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
0191     path->egress_shared_buffer = TB_PATH_NONE;
0192     path->ingress_fc_enable = TB_PATH_ALL;
0193     path->ingress_shared_buffer = TB_PATH_NONE;
0194     path->priority = 3;
0195     path->weight = 1;
0196     path->drop_packages = 0;
0197 
0198     tb_path_for_each_hop(path, hop) {
0199         int ret;
0200 
0201         ret = tb_pci_init_credits(hop);
0202         if (ret)
0203             return ret;
0204     }
0205 
0206     return 0;
0207 }
0208 
0209 /**
0210  * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
0211  * @tb: Pointer to the domain structure
0212  * @down: PCIe downstream adapter
0213  * @alloc_hopid: Allocate HopIDs from visited ports
0214  *
0215  * If @down adapter is active, follows the tunnel to the PCIe upstream
0216  * adapter and back. Returns the discovered tunnel or %NULL if there was
0217  * no tunnel.
0218  */
0219 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
0220                      bool alloc_hopid)
0221 {
0222     struct tb_tunnel *tunnel;
0223     struct tb_path *path;
0224 
0225     if (!tb_pci_port_is_enabled(down))
0226         return NULL;
0227 
0228     tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
0229     if (!tunnel)
0230         return NULL;
0231 
0232     tunnel->activate = tb_pci_activate;
0233     tunnel->src_port = down;
0234 
0235     /*
0236      * Discover both paths even if they are not complete. We will
0237      * clean them up by calling tb_tunnel_deactivate() below in that
0238      * case.
0239      */
0240     path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
0241                 &tunnel->dst_port, "PCIe Up", alloc_hopid);
0242     if (!path) {
0243         /* Just disable the downstream port */
0244         tb_pci_port_enable(down, false);
0245         goto err_free;
0246     }
0247     tunnel->paths[TB_PCI_PATH_UP] = path;
0248     if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
0249         goto err_free;
0250 
0251     path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
0252                 "PCIe Down", alloc_hopid);
0253     if (!path)
0254         goto err_deactivate;
0255     tunnel->paths[TB_PCI_PATH_DOWN] = path;
0256     if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]))
0257         goto err_deactivate;
0258 
0259     /* Validate that the tunnel is complete */
0260     if (!tb_port_is_pcie_up(tunnel->dst_port)) {
0261         tb_port_warn(tunnel->dst_port,
0262                  "path does not end on a PCIe adapter, cleaning up\n");
0263         goto err_deactivate;
0264     }
0265 
0266     if (down != tunnel->src_port) {
0267         tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
0268         goto err_deactivate;
0269     }
0270 
0271     if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
0272         tb_tunnel_warn(tunnel,
0273                    "tunnel is not fully activated, cleaning up\n");
0274         goto err_deactivate;
0275     }
0276 
0277     tb_tunnel_dbg(tunnel, "discovered\n");
0278     return tunnel;
0279 
0280 err_deactivate:
0281     tb_tunnel_deactivate(tunnel);
0282 err_free:
0283     tb_tunnel_free(tunnel);
0284 
0285     return NULL;
0286 }
0287 
0288 /**
0289  * tb_tunnel_alloc_pci() - allocate a pci tunnel
0290  * @tb: Pointer to the domain structure
0291  * @up: PCIe upstream adapter port
0292  * @down: PCIe downstream adapter port
0293  *
0294  * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
0295  * TB_TYPE_PCIE_DOWN.
0296  *
0297  * Return: Returns a tb_tunnel on success or NULL on failure.
0298  */
0299 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
0300                       struct tb_port *down)
0301 {
0302     struct tb_tunnel *tunnel;
0303     struct tb_path *path;
0304 
0305     tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
0306     if (!tunnel)
0307         return NULL;
0308 
0309     tunnel->activate = tb_pci_activate;
0310     tunnel->src_port = down;
0311     tunnel->dst_port = up;
0312 
0313     path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
0314                  "PCIe Down");
0315     if (!path)
0316         goto err_free;
0317     tunnel->paths[TB_PCI_PATH_DOWN] = path;
0318     if (tb_pci_init_path(path))
0319         goto err_free;
0320 
0321     path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
0322                  "PCIe Up");
0323     if (!path)
0324         goto err_free;
0325     tunnel->paths[TB_PCI_PATH_UP] = path;
0326     if (tb_pci_init_path(path))
0327         goto err_free;
0328 
0329     return tunnel;
0330 
0331 err_free:
0332     tb_tunnel_free(tunnel);
0333     return NULL;
0334 }
0335 
0336 static bool tb_dp_is_usb4(const struct tb_switch *sw)
0337 {
0338     /* Titan Ridge DP adapters need the same treatment as USB4 */
0339     return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
0340 }
0341 
0342 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out)
0343 {
0344     int timeout = 10;
0345     u32 val;
0346     int ret;
0347 
0348     /* Both ends need to support this */
0349     if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
0350         return 0;
0351 
0352     ret = tb_port_read(out, &val, TB_CFG_PORT,
0353                out->cap_adap + DP_STATUS_CTRL, 1);
0354     if (ret)
0355         return ret;
0356 
0357     val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
0358 
0359     ret = tb_port_write(out, &val, TB_CFG_PORT,
0360                 out->cap_adap + DP_STATUS_CTRL, 1);
0361     if (ret)
0362         return ret;
0363 
0364     do {
0365         ret = tb_port_read(out, &val, TB_CFG_PORT,
0366                    out->cap_adap + DP_STATUS_CTRL, 1);
0367         if (ret)
0368             return ret;
0369         if (!(val & DP_STATUS_CTRL_CMHS))
0370             return 0;
0371         usleep_range(10, 100);
0372     } while (timeout--);
0373 
0374     return -ETIMEDOUT;
0375 }
0376 
0377 static inline u32 tb_dp_cap_get_rate(u32 val)
0378 {
0379     u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
0380 
0381     switch (rate) {
0382     case DP_COMMON_CAP_RATE_RBR:
0383         return 1620;
0384     case DP_COMMON_CAP_RATE_HBR:
0385         return 2700;
0386     case DP_COMMON_CAP_RATE_HBR2:
0387         return 5400;
0388     case DP_COMMON_CAP_RATE_HBR3:
0389         return 8100;
0390     default:
0391         return 0;
0392     }
0393 }
0394 
0395 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
0396 {
0397     val &= ~DP_COMMON_CAP_RATE_MASK;
0398     switch (rate) {
0399     default:
0400         WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
0401         fallthrough;
0402     case 1620:
0403         val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
0404         break;
0405     case 2700:
0406         val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
0407         break;
0408     case 5400:
0409         val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
0410         break;
0411     case 8100:
0412         val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
0413         break;
0414     }
0415     return val;
0416 }
0417 
0418 static inline u32 tb_dp_cap_get_lanes(u32 val)
0419 {
0420     u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
0421 
0422     switch (lanes) {
0423     case DP_COMMON_CAP_1_LANE:
0424         return 1;
0425     case DP_COMMON_CAP_2_LANES:
0426         return 2;
0427     case DP_COMMON_CAP_4_LANES:
0428         return 4;
0429     default:
0430         return 0;
0431     }
0432 }
0433 
0434 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
0435 {
0436     val &= ~DP_COMMON_CAP_LANES_MASK;
0437     switch (lanes) {
0438     default:
0439         WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
0440              lanes);
0441         fallthrough;
0442     case 1:
0443         val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
0444         break;
0445     case 2:
0446         val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
0447         break;
0448     case 4:
0449         val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
0450         break;
0451     }
0452     return val;
0453 }
0454 
0455 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
0456 {
0457     /* Tunneling removes the DP 8b/10b encoding */
0458     return rate * lanes * 8 / 10;
0459 }
0460 
0461 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
0462                   u32 out_rate, u32 out_lanes, u32 *new_rate,
0463                   u32 *new_lanes)
0464 {
0465     static const u32 dp_bw[][2] = {
0466         /* Mb/s, lanes */
0467         { 8100, 4 }, /* 25920 Mb/s */
0468         { 5400, 4 }, /* 17280 Mb/s */
0469         { 8100, 2 }, /* 12960 Mb/s */
0470         { 2700, 4 }, /* 8640 Mb/s */
0471         { 5400, 2 }, /* 8640 Mb/s */
0472         { 8100, 1 }, /* 6480 Mb/s */
0473         { 1620, 4 }, /* 5184 Mb/s */
0474         { 5400, 1 }, /* 4320 Mb/s */
0475         { 2700, 2 }, /* 4320 Mb/s */
0476         { 1620, 2 }, /* 2592 Mb/s */
0477         { 2700, 1 }, /* 2160 Mb/s */
0478         { 1620, 1 }, /* 1296 Mb/s */
0479     };
0480     unsigned int i;
0481 
0482     /*
0483      * Find a combination that can fit into max_bw and does not
0484      * exceed the maximum rate and lanes supported by the DP OUT and
0485      * DP IN adapters.
0486      */
0487     for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
0488         if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
0489             continue;
0490 
0491         if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
0492             continue;
0493 
0494         if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
0495             *new_rate = dp_bw[i][0];
0496             *new_lanes = dp_bw[i][1];
0497             return 0;
0498         }
0499     }
0500 
0501     return -ENOSR;
0502 }
0503 
0504 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
0505 {
0506     u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
0507     struct tb_port *out = tunnel->dst_port;
0508     struct tb_port *in = tunnel->src_port;
0509     int ret, max_bw;
0510 
0511     /*
0512      * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
0513      * newer generation hardware.
0514      */
0515     if (in->sw->generation < 2 || out->sw->generation < 2)
0516         return 0;
0517 
0518     /*
0519      * Perform connection manager handshake between IN and OUT ports
0520      * before capabilities exchange can take place.
0521      */
0522     ret = tb_dp_cm_handshake(in, out);
0523     if (ret)
0524         return ret;
0525 
0526     /* Read both DP_LOCAL_CAP registers */
0527     ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
0528                in->cap_adap + DP_LOCAL_CAP, 1);
0529     if (ret)
0530         return ret;
0531 
0532     ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
0533                out->cap_adap + DP_LOCAL_CAP, 1);
0534     if (ret)
0535         return ret;
0536 
0537     /* Write IN local caps to OUT remote caps */
0538     ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
0539                 out->cap_adap + DP_REMOTE_CAP, 1);
0540     if (ret)
0541         return ret;
0542 
0543     in_rate = tb_dp_cap_get_rate(in_dp_cap);
0544     in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
0545     tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
0546             in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
0547 
0548     /*
0549      * If the tunnel bandwidth is limited (max_bw is set) then see
0550      * if we need to reduce bandwidth to fit there.
0551      */
0552     out_rate = tb_dp_cap_get_rate(out_dp_cap);
0553     out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
0554     bw = tb_dp_bandwidth(out_rate, out_lanes);
0555     tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
0556             out_rate, out_lanes, bw);
0557 
0558     if (in->sw->config.depth < out->sw->config.depth)
0559         max_bw = tunnel->max_down;
0560     else
0561         max_bw = tunnel->max_up;
0562 
0563     if (max_bw && bw > max_bw) {
0564         u32 new_rate, new_lanes, new_bw;
0565 
0566         ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
0567                          out_rate, out_lanes, &new_rate,
0568                          &new_lanes);
0569         if (ret) {
0570             tb_port_info(out, "not enough bandwidth for DP tunnel\n");
0571             return ret;
0572         }
0573 
0574         new_bw = tb_dp_bandwidth(new_rate, new_lanes);
0575         tb_port_dbg(out, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
0576                 new_rate, new_lanes, new_bw);
0577 
0578         /*
0579          * Set new rate and number of lanes before writing it to
0580          * the IN port remote caps.
0581          */
0582         out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
0583         out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
0584     }
0585 
0586     /*
0587      * Titan Ridge does not disable AUX timers when it gets
0588      * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with
0589      * DP tunneling.
0590      */
0591     if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) {
0592         out_dp_cap |= DP_COMMON_CAP_LTTPR_NS;
0593         tb_port_dbg(out, "disabling LTTPR\n");
0594     }
0595 
0596     return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
0597                  in->cap_adap + DP_REMOTE_CAP, 1);
0598 }
0599 
0600 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
0601 {
0602     int ret;
0603 
0604     if (active) {
0605         struct tb_path **paths;
0606         int last;
0607 
0608         paths = tunnel->paths;
0609         last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
0610 
0611         tb_dp_port_set_hops(tunnel->src_port,
0612             paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
0613             paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
0614             paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
0615 
0616         tb_dp_port_set_hops(tunnel->dst_port,
0617             paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
0618             paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
0619             paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
0620     } else {
0621         tb_dp_port_hpd_clear(tunnel->src_port);
0622         tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
0623         if (tb_port_is_dpout(tunnel->dst_port))
0624             tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
0625     }
0626 
0627     ret = tb_dp_port_enable(tunnel->src_port, active);
0628     if (ret)
0629         return ret;
0630 
0631     if (tb_port_is_dpout(tunnel->dst_port))
0632         return tb_dp_port_enable(tunnel->dst_port, active);
0633 
0634     return 0;
0635 }
0636 
0637 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
0638                     int *consumed_down)
0639 {
0640     struct tb_port *in = tunnel->src_port;
0641     const struct tb_switch *sw = in->sw;
0642     u32 val, rate = 0, lanes = 0;
0643     int ret;
0644 
0645     if (tb_dp_is_usb4(sw)) {
0646         int timeout = 20;
0647 
0648         /*
0649          * Wait for DPRX done. Normally it should be already set
0650          * for active tunnel.
0651          */
0652         do {
0653             ret = tb_port_read(in, &val, TB_CFG_PORT,
0654                        in->cap_adap + DP_COMMON_CAP, 1);
0655             if (ret)
0656                 return ret;
0657 
0658             if (val & DP_COMMON_CAP_DPRX_DONE) {
0659                 rate = tb_dp_cap_get_rate(val);
0660                 lanes = tb_dp_cap_get_lanes(val);
0661                 break;
0662             }
0663             msleep(250);
0664         } while (timeout--);
0665 
0666         if (!timeout)
0667             return -ETIMEDOUT;
0668     } else if (sw->generation >= 2) {
0669         /*
0670          * Read from the copied remote cap so that we take into
0671          * account if capabilities were reduced during exchange.
0672          */
0673         ret = tb_port_read(in, &val, TB_CFG_PORT,
0674                    in->cap_adap + DP_REMOTE_CAP, 1);
0675         if (ret)
0676             return ret;
0677 
0678         rate = tb_dp_cap_get_rate(val);
0679         lanes = tb_dp_cap_get_lanes(val);
0680     } else {
0681         /* No bandwidth management for legacy devices  */
0682         *consumed_up = 0;
0683         *consumed_down = 0;
0684         return 0;
0685     }
0686 
0687     if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
0688         *consumed_up = 0;
0689         *consumed_down = tb_dp_bandwidth(rate, lanes);
0690     } else {
0691         *consumed_up = tb_dp_bandwidth(rate, lanes);
0692         *consumed_down = 0;
0693     }
0694 
0695     return 0;
0696 }
0697 
0698 static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
0699 {
0700     struct tb_port *port = hop->in_port;
0701     struct tb_switch *sw = port->sw;
0702 
0703     if (tb_port_use_credit_allocation(port))
0704         hop->initial_credits = sw->min_dp_aux_credits;
0705     else
0706         hop->initial_credits = 1;
0707 }
0708 
0709 static void tb_dp_init_aux_path(struct tb_path *path)
0710 {
0711     struct tb_path_hop *hop;
0712 
0713     path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
0714     path->egress_shared_buffer = TB_PATH_NONE;
0715     path->ingress_fc_enable = TB_PATH_ALL;
0716     path->ingress_shared_buffer = TB_PATH_NONE;
0717     path->priority = 2;
0718     path->weight = 1;
0719 
0720     tb_path_for_each_hop(path, hop)
0721         tb_dp_init_aux_credits(hop);
0722 }
0723 
0724 static int tb_dp_init_video_credits(struct tb_path_hop *hop)
0725 {
0726     struct tb_port *port = hop->in_port;
0727     struct tb_switch *sw = port->sw;
0728 
0729     if (tb_port_use_credit_allocation(port)) {
0730         unsigned int nfc_credits;
0731         size_t max_dp_streams;
0732 
0733         tb_available_credits(port, &max_dp_streams);
0734         /*
0735          * Read the number of currently allocated NFC credits
0736          * from the lane adapter. Since we only use them for DP
0737          * tunneling we can use that to figure out how many DP
0738          * tunnels already go through the lane adapter.
0739          */
0740         nfc_credits = port->config.nfc_credits &
0741                 ADP_CS_4_NFC_BUFFERS_MASK;
0742         if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
0743             return -ENOSPC;
0744 
0745         hop->nfc_credits = sw->min_dp_main_credits;
0746     } else {
0747         hop->nfc_credits = min(port->total_credits - 2, 12U);
0748     }
0749 
0750     return 0;
0751 }
0752 
0753 static int tb_dp_init_video_path(struct tb_path *path)
0754 {
0755     struct tb_path_hop *hop;
0756 
0757     path->egress_fc_enable = TB_PATH_NONE;
0758     path->egress_shared_buffer = TB_PATH_NONE;
0759     path->ingress_fc_enable = TB_PATH_NONE;
0760     path->ingress_shared_buffer = TB_PATH_NONE;
0761     path->priority = 1;
0762     path->weight = 1;
0763 
0764     tb_path_for_each_hop(path, hop) {
0765         int ret;
0766 
0767         ret = tb_dp_init_video_credits(hop);
0768         if (ret)
0769             return ret;
0770     }
0771 
0772     return 0;
0773 }
0774 
0775 /**
0776  * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
0777  * @tb: Pointer to the domain structure
0778  * @in: DP in adapter
0779  * @alloc_hopid: Allocate HopIDs from visited ports
0780  *
0781  * If @in adapter is active, follows the tunnel to the DP out adapter
0782  * and back. Returns the discovered tunnel or %NULL if there was no
0783  * tunnel.
0784  *
0785  * Return: DP tunnel or %NULL if no tunnel found.
0786  */
0787 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
0788                     bool alloc_hopid)
0789 {
0790     struct tb_tunnel *tunnel;
0791     struct tb_port *port;
0792     struct tb_path *path;
0793 
0794     if (!tb_dp_port_is_enabled(in))
0795         return NULL;
0796 
0797     tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
0798     if (!tunnel)
0799         return NULL;
0800 
0801     tunnel->init = tb_dp_xchg_caps;
0802     tunnel->activate = tb_dp_activate;
0803     tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
0804     tunnel->src_port = in;
0805 
0806     path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
0807                 &tunnel->dst_port, "Video", alloc_hopid);
0808     if (!path) {
0809         /* Just disable the DP IN port */
0810         tb_dp_port_enable(in, false);
0811         goto err_free;
0812     }
0813     tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
0814     if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT]))
0815         goto err_free;
0816 
0817     path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
0818                 alloc_hopid);
0819     if (!path)
0820         goto err_deactivate;
0821     tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
0822     tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
0823 
0824     path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
0825                 &port, "AUX RX", alloc_hopid);
0826     if (!path)
0827         goto err_deactivate;
0828     tunnel->paths[TB_DP_AUX_PATH_IN] = path;
0829     tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
0830 
0831     /* Validate that the tunnel is complete */
0832     if (!tb_port_is_dpout(tunnel->dst_port)) {
0833         tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
0834         goto err_deactivate;
0835     }
0836 
0837     if (!tb_dp_port_is_enabled(tunnel->dst_port))
0838         goto err_deactivate;
0839 
0840     if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
0841         goto err_deactivate;
0842 
0843     if (port != tunnel->src_port) {
0844         tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
0845         goto err_deactivate;
0846     }
0847 
0848     tb_tunnel_dbg(tunnel, "discovered\n");
0849     return tunnel;
0850 
0851 err_deactivate:
0852     tb_tunnel_deactivate(tunnel);
0853 err_free:
0854     tb_tunnel_free(tunnel);
0855 
0856     return NULL;
0857 }
0858 
0859 /**
0860  * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
0861  * @tb: Pointer to the domain structure
0862  * @in: DP in adapter port
0863  * @out: DP out adapter port
0864  * @link_nr: Preferred lane adapter when the link is not bonded
0865  * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
0866  *      if not limited)
0867  * @max_down: Maximum available downstream bandwidth for the DP tunnel
0868  *        (%0 if not limited)
0869  *
0870  * Allocates a tunnel between @in and @out that is capable of tunneling
0871  * Display Port traffic.
0872  *
0873  * Return: Returns a tb_tunnel on success or NULL on failure.
0874  */
0875 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
0876                      struct tb_port *out, int link_nr,
0877                      int max_up, int max_down)
0878 {
0879     struct tb_tunnel *tunnel;
0880     struct tb_path **paths;
0881     struct tb_path *path;
0882 
0883     if (WARN_ON(!in->cap_adap || !out->cap_adap))
0884         return NULL;
0885 
0886     tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
0887     if (!tunnel)
0888         return NULL;
0889 
0890     tunnel->init = tb_dp_xchg_caps;
0891     tunnel->activate = tb_dp_activate;
0892     tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
0893     tunnel->src_port = in;
0894     tunnel->dst_port = out;
0895     tunnel->max_up = max_up;
0896     tunnel->max_down = max_down;
0897 
0898     paths = tunnel->paths;
0899 
0900     path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
0901                  link_nr, "Video");
0902     if (!path)
0903         goto err_free;
0904     tb_dp_init_video_path(path);
0905     paths[TB_DP_VIDEO_PATH_OUT] = path;
0906 
0907     path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
0908                  TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
0909     if (!path)
0910         goto err_free;
0911     tb_dp_init_aux_path(path);
0912     paths[TB_DP_AUX_PATH_OUT] = path;
0913 
0914     path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
0915                  TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
0916     if (!path)
0917         goto err_free;
0918     tb_dp_init_aux_path(path);
0919     paths[TB_DP_AUX_PATH_IN] = path;
0920 
0921     return tunnel;
0922 
0923 err_free:
0924     tb_tunnel_free(tunnel);
0925     return NULL;
0926 }
0927 
0928 static unsigned int tb_dma_available_credits(const struct tb_port *port)
0929 {
0930     const struct tb_switch *sw = port->sw;
0931     int credits;
0932 
0933     credits = tb_available_credits(port, NULL);
0934     if (tb_acpi_may_tunnel_pcie())
0935         credits -= sw->max_pcie_credits;
0936     credits -= port->dma_credits;
0937 
0938     return credits > 0 ? credits : 0;
0939 }
0940 
0941 static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
0942 {
0943     struct tb_port *port = hop->in_port;
0944 
0945     if (tb_port_use_credit_allocation(port)) {
0946         unsigned int available = tb_dma_available_credits(port);
0947 
0948         /*
0949          * Need to have at least TB_MIN_DMA_CREDITS, otherwise
0950          * DMA path cannot be established.
0951          */
0952         if (available < TB_MIN_DMA_CREDITS)
0953             return -ENOSPC;
0954 
0955         while (credits > available)
0956             credits--;
0957 
0958         tb_port_dbg(port, "reserving %u credits for DMA path\n",
0959                 credits);
0960 
0961         port->dma_credits += credits;
0962     } else {
0963         if (tb_port_is_null(port))
0964             credits = port->bonded ? 14 : 6;
0965         else
0966             credits = min(port->total_credits, credits);
0967     }
0968 
0969     hop->initial_credits = credits;
0970     return 0;
0971 }
0972 
0973 /* Path from lane adapter to NHI */
0974 static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
0975 {
0976     struct tb_path_hop *hop;
0977     unsigned int i, tmp;
0978 
0979     path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
0980     path->ingress_fc_enable = TB_PATH_ALL;
0981     path->egress_shared_buffer = TB_PATH_NONE;
0982     path->ingress_shared_buffer = TB_PATH_NONE;
0983     path->priority = 5;
0984     path->weight = 1;
0985     path->clear_fc = true;
0986 
0987     /*
0988      * First lane adapter is the one connected to the remote host.
0989      * We don't tunnel other traffic over this link so can use all
0990      * the credits (except the ones reserved for control traffic).
0991      */
0992     hop = &path->hops[0];
0993     tmp = min(tb_usable_credits(hop->in_port), credits);
0994     hop->initial_credits = tmp;
0995     hop->in_port->dma_credits += tmp;
0996 
0997     for (i = 1; i < path->path_length; i++) {
0998         int ret;
0999 
1000         ret = tb_dma_reserve_credits(&path->hops[i], credits);
1001         if (ret)
1002             return ret;
1003     }
1004 
1005     return 0;
1006 }
1007 
1008 /* Path from NHI to lane adapter */
1009 static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
1010 {
1011     struct tb_path_hop *hop;
1012 
1013     path->egress_fc_enable = TB_PATH_ALL;
1014     path->ingress_fc_enable = TB_PATH_ALL;
1015     path->egress_shared_buffer = TB_PATH_NONE;
1016     path->ingress_shared_buffer = TB_PATH_NONE;
1017     path->priority = 5;
1018     path->weight = 1;
1019     path->clear_fc = true;
1020 
1021     tb_path_for_each_hop(path, hop) {
1022         int ret;
1023 
1024         ret = tb_dma_reserve_credits(hop, credits);
1025         if (ret)
1026             return ret;
1027     }
1028 
1029     return 0;
1030 }
1031 
1032 static void tb_dma_release_credits(struct tb_path_hop *hop)
1033 {
1034     struct tb_port *port = hop->in_port;
1035 
1036     if (tb_port_use_credit_allocation(port)) {
1037         port->dma_credits -= hop->initial_credits;
1038 
1039         tb_port_dbg(port, "released %u DMA path credits\n",
1040                 hop->initial_credits);
1041     }
1042 }
1043 
1044 static void tb_dma_deinit_path(struct tb_path *path)
1045 {
1046     struct tb_path_hop *hop;
1047 
1048     tb_path_for_each_hop(path, hop)
1049         tb_dma_release_credits(hop);
1050 }
1051 
1052 static void tb_dma_deinit(struct tb_tunnel *tunnel)
1053 {
1054     int i;
1055 
1056     for (i = 0; i < tunnel->npaths; i++) {
1057         if (!tunnel->paths[i])
1058             continue;
1059         tb_dma_deinit_path(tunnel->paths[i]);
1060     }
1061 }
1062 
1063 /**
1064  * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1065  * @tb: Pointer to the domain structure
1066  * @nhi: Host controller port
1067  * @dst: Destination null port which the other domain is connected to
1068  * @transmit_path: HopID used for transmitting packets
1069  * @transmit_ring: NHI ring number used to send packets towards the
1070  *         other domain. Set to %-1 if TX path is not needed.
1071  * @receive_path: HopID used for receiving packets
1072  * @receive_ring: NHI ring number used to receive packets from the
1073  *        other domain. Set to %-1 if RX path is not needed.
1074  *
1075  * Return: Returns a tb_tunnel on success or NULL on failure.
1076  */
1077 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
1078                       struct tb_port *dst, int transmit_path,
1079                       int transmit_ring, int receive_path,
1080                       int receive_ring)
1081 {
1082     struct tb_tunnel *tunnel;
1083     size_t npaths = 0, i = 0;
1084     struct tb_path *path;
1085     int credits;
1086 
1087     if (receive_ring > 0)
1088         npaths++;
1089     if (transmit_ring > 0)
1090         npaths++;
1091 
1092     if (WARN_ON(!npaths))
1093         return NULL;
1094 
1095     tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
1096     if (!tunnel)
1097         return NULL;
1098 
1099     tunnel->src_port = nhi;
1100     tunnel->dst_port = dst;
1101     tunnel->deinit = tb_dma_deinit;
1102 
1103     credits = min_not_zero(TB_DMA_CREDITS, nhi->sw->max_dma_credits);
1104 
1105     if (receive_ring > 0) {
1106         path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
1107                      "DMA RX");
1108         if (!path)
1109             goto err_free;
1110         tunnel->paths[i++] = path;
1111         if (tb_dma_init_rx_path(path, credits)) {
1112             tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
1113             goto err_free;
1114         }
1115     }
1116 
1117     if (transmit_ring > 0) {
1118         path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
1119                      "DMA TX");
1120         if (!path)
1121             goto err_free;
1122         tunnel->paths[i++] = path;
1123         if (tb_dma_init_tx_path(path, credits)) {
1124             tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
1125             goto err_free;
1126         }
1127     }
1128 
1129     return tunnel;
1130 
1131 err_free:
1132     tb_tunnel_free(tunnel);
1133     return NULL;
1134 }
1135 
1136 /**
1137  * tb_tunnel_match_dma() - Match DMA tunnel
1138  * @tunnel: Tunnel to match
1139  * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
1140  * @transmit_ring: NHI ring number used to send packets towards the
1141  *         other domain. Pass %-1 to ignore.
1142  * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
1143  * @receive_ring: NHI ring number used to receive packets from the
1144  *        other domain. Pass %-1 to ignore.
1145  *
1146  * This function can be used to match specific DMA tunnel, if there are
1147  * multiple DMA tunnels going through the same XDomain connection.
1148  * Returns true if there is match and false otherwise.
1149  */
1150 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
1151              int transmit_ring, int receive_path, int receive_ring)
1152 {
1153     const struct tb_path *tx_path = NULL, *rx_path = NULL;
1154     int i;
1155 
1156     if (!receive_ring || !transmit_ring)
1157         return false;
1158 
1159     for (i = 0; i < tunnel->npaths; i++) {
1160         const struct tb_path *path = tunnel->paths[i];
1161 
1162         if (!path)
1163             continue;
1164 
1165         if (tb_port_is_nhi(path->hops[0].in_port))
1166             tx_path = path;
1167         else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
1168             rx_path = path;
1169     }
1170 
1171     if (transmit_ring > 0 || transmit_path > 0) {
1172         if (!tx_path)
1173             return false;
1174         if (transmit_ring > 0 &&
1175             (tx_path->hops[0].in_hop_index != transmit_ring))
1176             return false;
1177         if (transmit_path > 0 &&
1178             (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
1179             return false;
1180     }
1181 
1182     if (receive_ring > 0 || receive_path > 0) {
1183         if (!rx_path)
1184             return false;
1185         if (receive_path > 0 &&
1186             (rx_path->hops[0].in_hop_index != receive_path))
1187             return false;
1188         if (receive_ring > 0 &&
1189             (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
1190             return false;
1191     }
1192 
1193     return true;
1194 }
1195 
1196 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
1197 {
1198     int ret, up_max_rate, down_max_rate;
1199 
1200     ret = usb4_usb3_port_max_link_rate(up);
1201     if (ret < 0)
1202         return ret;
1203     up_max_rate = ret;
1204 
1205     ret = usb4_usb3_port_max_link_rate(down);
1206     if (ret < 0)
1207         return ret;
1208     down_max_rate = ret;
1209 
1210     return min(up_max_rate, down_max_rate);
1211 }
1212 
1213 static int tb_usb3_init(struct tb_tunnel *tunnel)
1214 {
1215     tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
1216               tunnel->allocated_up, tunnel->allocated_down);
1217 
1218     return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
1219                          &tunnel->allocated_up,
1220                          &tunnel->allocated_down);
1221 }
1222 
1223 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
1224 {
1225     int res;
1226 
1227     res = tb_usb3_port_enable(tunnel->src_port, activate);
1228     if (res)
1229         return res;
1230 
1231     if (tb_port_is_usb3_up(tunnel->dst_port))
1232         return tb_usb3_port_enable(tunnel->dst_port, activate);
1233 
1234     return 0;
1235 }
1236 
1237 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
1238         int *consumed_up, int *consumed_down)
1239 {
1240     int pcie_enabled = tb_acpi_may_tunnel_pcie();
1241 
1242     /*
1243      * PCIe tunneling, if enabled, affects the USB3 bandwidth so
1244      * take that it into account here.
1245      */
1246     *consumed_up = tunnel->allocated_up * (3 + pcie_enabled) / 3;
1247     *consumed_down = tunnel->allocated_down * (3 + pcie_enabled) / 3;
1248     return 0;
1249 }
1250 
1251 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
1252 {
1253     int ret;
1254 
1255     ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
1256                            &tunnel->allocated_up,
1257                            &tunnel->allocated_down);
1258     if (ret)
1259         return ret;
1260 
1261     tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
1262               tunnel->allocated_up, tunnel->allocated_down);
1263     return 0;
1264 }
1265 
1266 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1267                         int *available_up,
1268                         int *available_down)
1269 {
1270     int ret, max_rate, allocate_up, allocate_down;
1271 
1272     ret = usb4_usb3_port_actual_link_rate(tunnel->src_port);
1273     if (ret < 0) {
1274         tb_tunnel_warn(tunnel, "failed to read actual link rate\n");
1275         return;
1276     } else if (!ret) {
1277         /* Use maximum link rate if the link valid is not set */
1278         ret = usb4_usb3_port_max_link_rate(tunnel->src_port);
1279         if (ret < 0) {
1280             tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
1281             return;
1282         }
1283     }
1284 
1285     /*
1286      * 90% of the max rate can be allocated for isochronous
1287      * transfers.
1288      */
1289     max_rate = ret * 90 / 100;
1290 
1291     /* No need to reclaim if already at maximum */
1292     if (tunnel->allocated_up >= max_rate &&
1293         tunnel->allocated_down >= max_rate)
1294         return;
1295 
1296     /* Don't go lower than what is already allocated */
1297     allocate_up = min(max_rate, *available_up);
1298     if (allocate_up < tunnel->allocated_up)
1299         allocate_up = tunnel->allocated_up;
1300 
1301     allocate_down = min(max_rate, *available_down);
1302     if (allocate_down < tunnel->allocated_down)
1303         allocate_down = tunnel->allocated_down;
1304 
1305     /* If no changes no need to do more */
1306     if (allocate_up == tunnel->allocated_up &&
1307         allocate_down == tunnel->allocated_down)
1308         return;
1309 
1310     ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
1311                         &allocate_down);
1312     if (ret) {
1313         tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
1314         return;
1315     }
1316 
1317     tunnel->allocated_up = allocate_up;
1318     *available_up -= tunnel->allocated_up;
1319 
1320     tunnel->allocated_down = allocate_down;
1321     *available_down -= tunnel->allocated_down;
1322 
1323     tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
1324               tunnel->allocated_up, tunnel->allocated_down);
1325 }
1326 
1327 static void tb_usb3_init_credits(struct tb_path_hop *hop)
1328 {
1329     struct tb_port *port = hop->in_port;
1330     struct tb_switch *sw = port->sw;
1331     unsigned int credits;
1332 
1333     if (tb_port_use_credit_allocation(port)) {
1334         credits = sw->max_usb3_credits;
1335     } else {
1336         if (tb_port_is_null(port))
1337             credits = port->bonded ? 32 : 16;
1338         else
1339             credits = 7;
1340     }
1341 
1342     hop->initial_credits = credits;
1343 }
1344 
1345 static void tb_usb3_init_path(struct tb_path *path)
1346 {
1347     struct tb_path_hop *hop;
1348 
1349     path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1350     path->egress_shared_buffer = TB_PATH_NONE;
1351     path->ingress_fc_enable = TB_PATH_ALL;
1352     path->ingress_shared_buffer = TB_PATH_NONE;
1353     path->priority = 3;
1354     path->weight = 3;
1355     path->drop_packages = 0;
1356 
1357     tb_path_for_each_hop(path, hop)
1358         tb_usb3_init_credits(hop);
1359 }
1360 
1361 /**
1362  * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
1363  * @tb: Pointer to the domain structure
1364  * @down: USB3 downstream adapter
1365  * @alloc_hopid: Allocate HopIDs from visited ports
1366  *
1367  * If @down adapter is active, follows the tunnel to the USB3 upstream
1368  * adapter and back. Returns the discovered tunnel or %NULL if there was
1369  * no tunnel.
1370  */
1371 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
1372                       bool alloc_hopid)
1373 {
1374     struct tb_tunnel *tunnel;
1375     struct tb_path *path;
1376 
1377     if (!tb_usb3_port_is_enabled(down))
1378         return NULL;
1379 
1380     tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1381     if (!tunnel)
1382         return NULL;
1383 
1384     tunnel->activate = tb_usb3_activate;
1385     tunnel->src_port = down;
1386 
1387     /*
1388      * Discover both paths even if they are not complete. We will
1389      * clean them up by calling tb_tunnel_deactivate() below in that
1390      * case.
1391      */
1392     path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
1393                 &tunnel->dst_port, "USB3 Down", alloc_hopid);
1394     if (!path) {
1395         /* Just disable the downstream port */
1396         tb_usb3_port_enable(down, false);
1397         goto err_free;
1398     }
1399     tunnel->paths[TB_USB3_PATH_DOWN] = path;
1400     tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
1401 
1402     path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
1403                 "USB3 Up", alloc_hopid);
1404     if (!path)
1405         goto err_deactivate;
1406     tunnel->paths[TB_USB3_PATH_UP] = path;
1407     tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
1408 
1409     /* Validate that the tunnel is complete */
1410     if (!tb_port_is_usb3_up(tunnel->dst_port)) {
1411         tb_port_warn(tunnel->dst_port,
1412                  "path does not end on an USB3 adapter, cleaning up\n");
1413         goto err_deactivate;
1414     }
1415 
1416     if (down != tunnel->src_port) {
1417         tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1418         goto err_deactivate;
1419     }
1420 
1421     if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
1422         tb_tunnel_warn(tunnel,
1423                    "tunnel is not fully activated, cleaning up\n");
1424         goto err_deactivate;
1425     }
1426 
1427     if (!tb_route(down->sw)) {
1428         int ret;
1429 
1430         /*
1431          * Read the initial bandwidth allocation for the first
1432          * hop tunnel.
1433          */
1434         ret = usb4_usb3_port_allocated_bandwidth(down,
1435             &tunnel->allocated_up, &tunnel->allocated_down);
1436         if (ret)
1437             goto err_deactivate;
1438 
1439         tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
1440                   tunnel->allocated_up, tunnel->allocated_down);
1441 
1442         tunnel->init = tb_usb3_init;
1443         tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1444         tunnel->release_unused_bandwidth =
1445             tb_usb3_release_unused_bandwidth;
1446         tunnel->reclaim_available_bandwidth =
1447             tb_usb3_reclaim_available_bandwidth;
1448     }
1449 
1450     tb_tunnel_dbg(tunnel, "discovered\n");
1451     return tunnel;
1452 
1453 err_deactivate:
1454     tb_tunnel_deactivate(tunnel);
1455 err_free:
1456     tb_tunnel_free(tunnel);
1457 
1458     return NULL;
1459 }
1460 
1461 /**
1462  * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
1463  * @tb: Pointer to the domain structure
1464  * @up: USB3 upstream adapter port
1465  * @down: USB3 downstream adapter port
1466  * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
1467  *      if not limited).
1468  * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
1469  *        (%0 if not limited).
1470  *
1471  * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
1472  * @TB_TYPE_USB3_DOWN.
1473  *
1474  * Return: Returns a tb_tunnel on success or %NULL on failure.
1475  */
1476 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
1477                        struct tb_port *down, int max_up,
1478                        int max_down)
1479 {
1480     struct tb_tunnel *tunnel;
1481     struct tb_path *path;
1482     int max_rate = 0;
1483 
1484     /*
1485      * Check that we have enough bandwidth available for the new
1486      * USB3 tunnel.
1487      */
1488     if (max_up > 0 || max_down > 0) {
1489         max_rate = tb_usb3_max_link_rate(down, up);
1490         if (max_rate < 0)
1491             return NULL;
1492 
1493         /* Only 90% can be allocated for USB3 isochronous transfers */
1494         max_rate = max_rate * 90 / 100;
1495         tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n",
1496                 max_rate);
1497 
1498         if (max_rate > max_up || max_rate > max_down) {
1499             tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n");
1500             return NULL;
1501         }
1502     }
1503 
1504     tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1505     if (!tunnel)
1506         return NULL;
1507 
1508     tunnel->activate = tb_usb3_activate;
1509     tunnel->src_port = down;
1510     tunnel->dst_port = up;
1511     tunnel->max_up = max_up;
1512     tunnel->max_down = max_down;
1513 
1514     path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
1515                  "USB3 Down");
1516     if (!path) {
1517         tb_tunnel_free(tunnel);
1518         return NULL;
1519     }
1520     tb_usb3_init_path(path);
1521     tunnel->paths[TB_USB3_PATH_DOWN] = path;
1522 
1523     path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
1524                  "USB3 Up");
1525     if (!path) {
1526         tb_tunnel_free(tunnel);
1527         return NULL;
1528     }
1529     tb_usb3_init_path(path);
1530     tunnel->paths[TB_USB3_PATH_UP] = path;
1531 
1532     if (!tb_route(down->sw)) {
1533         tunnel->allocated_up = max_rate;
1534         tunnel->allocated_down = max_rate;
1535 
1536         tunnel->init = tb_usb3_init;
1537         tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1538         tunnel->release_unused_bandwidth =
1539             tb_usb3_release_unused_bandwidth;
1540         tunnel->reclaim_available_bandwidth =
1541             tb_usb3_reclaim_available_bandwidth;
1542     }
1543 
1544     return tunnel;
1545 }
1546 
1547 /**
1548  * tb_tunnel_free() - free a tunnel
1549  * @tunnel: Tunnel to be freed
1550  *
1551  * Frees a tunnel. The tunnel does not need to be deactivated.
1552  */
1553 void tb_tunnel_free(struct tb_tunnel *tunnel)
1554 {
1555     int i;
1556 
1557     if (!tunnel)
1558         return;
1559 
1560     if (tunnel->deinit)
1561         tunnel->deinit(tunnel);
1562 
1563     for (i = 0; i < tunnel->npaths; i++) {
1564         if (tunnel->paths[i])
1565             tb_path_free(tunnel->paths[i]);
1566     }
1567 
1568     kfree(tunnel->paths);
1569     kfree(tunnel);
1570 }
1571 
1572 /**
1573  * tb_tunnel_is_invalid - check whether an activated path is still valid
1574  * @tunnel: Tunnel to check
1575  */
1576 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
1577 {
1578     int i;
1579 
1580     for (i = 0; i < tunnel->npaths; i++) {
1581         WARN_ON(!tunnel->paths[i]->activated);
1582         if (tb_path_is_invalid(tunnel->paths[i]))
1583             return true;
1584     }
1585 
1586     return false;
1587 }
1588 
1589 /**
1590  * tb_tunnel_restart() - activate a tunnel after a hardware reset
1591  * @tunnel: Tunnel to restart
1592  *
1593  * Return: 0 on success and negative errno in case if failure
1594  */
1595 int tb_tunnel_restart(struct tb_tunnel *tunnel)
1596 {
1597     int res, i;
1598 
1599     tb_tunnel_dbg(tunnel, "activating\n");
1600 
1601     /*
1602      * Make sure all paths are properly disabled before enabling
1603      * them again.
1604      */
1605     for (i = 0; i < tunnel->npaths; i++) {
1606         if (tunnel->paths[i]->activated) {
1607             tb_path_deactivate(tunnel->paths[i]);
1608             tunnel->paths[i]->activated = false;
1609         }
1610     }
1611 
1612     if (tunnel->init) {
1613         res = tunnel->init(tunnel);
1614         if (res)
1615             return res;
1616     }
1617 
1618     for (i = 0; i < tunnel->npaths; i++) {
1619         res = tb_path_activate(tunnel->paths[i]);
1620         if (res)
1621             goto err;
1622     }
1623 
1624     if (tunnel->activate) {
1625         res = tunnel->activate(tunnel, true);
1626         if (res)
1627             goto err;
1628     }
1629 
1630     return 0;
1631 
1632 err:
1633     tb_tunnel_warn(tunnel, "activation failed\n");
1634     tb_tunnel_deactivate(tunnel);
1635     return res;
1636 }
1637 
1638 /**
1639  * tb_tunnel_activate() - activate a tunnel
1640  * @tunnel: Tunnel to activate
1641  *
1642  * Return: Returns 0 on success or an error code on failure.
1643  */
1644 int tb_tunnel_activate(struct tb_tunnel *tunnel)
1645 {
1646     int i;
1647 
1648     for (i = 0; i < tunnel->npaths; i++) {
1649         if (tunnel->paths[i]->activated) {
1650             tb_tunnel_WARN(tunnel,
1651                        "trying to activate an already activated tunnel\n");
1652             return -EINVAL;
1653         }
1654     }
1655 
1656     return tb_tunnel_restart(tunnel);
1657 }
1658 
1659 /**
1660  * tb_tunnel_deactivate() - deactivate a tunnel
1661  * @tunnel: Tunnel to deactivate
1662  */
1663 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
1664 {
1665     int i;
1666 
1667     tb_tunnel_dbg(tunnel, "deactivating\n");
1668 
1669     if (tunnel->activate)
1670         tunnel->activate(tunnel, false);
1671 
1672     for (i = 0; i < tunnel->npaths; i++) {
1673         if (tunnel->paths[i] && tunnel->paths[i]->activated)
1674             tb_path_deactivate(tunnel->paths[i]);
1675     }
1676 }
1677 
1678 /**
1679  * tb_tunnel_port_on_path() - Does the tunnel go through port
1680  * @tunnel: Tunnel to check
1681  * @port: Port to check
1682  *
1683  * Returns true if @tunnel goes through @port (direction does not matter),
1684  * false otherwise.
1685  */
1686 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
1687                 const struct tb_port *port)
1688 {
1689     int i;
1690 
1691     for (i = 0; i < tunnel->npaths; i++) {
1692         if (!tunnel->paths[i])
1693             continue;
1694 
1695         if (tb_path_port_on_path(tunnel->paths[i], port))
1696             return true;
1697     }
1698 
1699     return false;
1700 }
1701 
1702 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
1703 {
1704     int i;
1705 
1706     for (i = 0; i < tunnel->npaths; i++) {
1707         if (!tunnel->paths[i])
1708             return false;
1709         if (!tunnel->paths[i]->activated)
1710             return false;
1711     }
1712 
1713     return true;
1714 }
1715 
1716 /**
1717  * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
1718  * @tunnel: Tunnel to check
1719  * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
1720  *       Can be %NULL.
1721  * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
1722  *         Can be %NULL.
1723  *
1724  * Stores the amount of isochronous bandwidth @tunnel consumes in
1725  * @consumed_up and @consumed_down. In case of success returns %0,
1726  * negative errno otherwise.
1727  */
1728 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1729                  int *consumed_down)
1730 {
1731     int up_bw = 0, down_bw = 0;
1732 
1733     if (!tb_tunnel_is_active(tunnel))
1734         goto out;
1735 
1736     if (tunnel->consumed_bandwidth) {
1737         int ret;
1738 
1739         ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
1740         if (ret)
1741             return ret;
1742 
1743         tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw,
1744                   down_bw);
1745     }
1746 
1747 out:
1748     if (consumed_up)
1749         *consumed_up = up_bw;
1750     if (consumed_down)
1751         *consumed_down = down_bw;
1752 
1753     return 0;
1754 }
1755 
1756 /**
1757  * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
1758  * @tunnel: Tunnel whose unused bandwidth to release
1759  *
1760  * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
1761  * moment) this function makes it to release all the unused bandwidth.
1762  *
1763  * Returns %0 in case of success and negative errno otherwise.
1764  */
1765 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
1766 {
1767     if (!tb_tunnel_is_active(tunnel))
1768         return 0;
1769 
1770     if (tunnel->release_unused_bandwidth) {
1771         int ret;
1772 
1773         ret = tunnel->release_unused_bandwidth(tunnel);
1774         if (ret)
1775             return ret;
1776     }
1777 
1778     return 0;
1779 }
1780 
1781 /**
1782  * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
1783  * @tunnel: Tunnel reclaiming available bandwidth
1784  * @available_up: Available upstream bandwidth (in Mb/s)
1785  * @available_down: Available downstream bandwidth (in Mb/s)
1786  *
1787  * Reclaims bandwidth from @available_up and @available_down and updates
1788  * the variables accordingly (e.g decreases both according to what was
1789  * reclaimed by the tunnel). If nothing was reclaimed the values are
1790  * kept as is.
1791  */
1792 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1793                        int *available_up,
1794                        int *available_down)
1795 {
1796     if (!tb_tunnel_is_active(tunnel))
1797         return;
1798 
1799     if (tunnel->reclaim_available_bandwidth)
1800         tunnel->reclaim_available_bandwidth(tunnel, available_up,
1801                             available_down);
1802 }