1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - PCIe tunnel
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/slab.h>
9 #include <linux/list.h>
11 #include "tunnel_pci.h"
14 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
16 struct tb_pci_tunnel *__tunnel = (tunnel); \
17 level(__tunnel->tb, "%llx:%x <-> %llx:%x (PCI): " fmt, \
18 tb_route(__tunnel->down_port->sw), \
19 __tunnel->down_port->port, \
20 tb_route(__tunnel->up_port->sw), \
21 __tunnel->up_port->port, \
25 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
26 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
27 #define tb_tunnel_warn(tunnel, fmt, arg...) \
28 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
29 #define tb_tunnel_info(tunnel, fmt, arg...) \
30 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
32 static void tb_pci_init_path(struct tb_path
*path
)
34 path
->egress_fc_enable
= TB_PATH_SOURCE
| TB_PATH_INTERNAL
;
35 path
->egress_shared_buffer
= TB_PATH_NONE
;
36 path
->ingress_fc_enable
= TB_PATH_ALL
;
37 path
->ingress_shared_buffer
= TB_PATH_NONE
;
40 path
->drop_packages
= 0;
41 path
->nfc_credits
= 0;
45 * tb_pci_alloc() - allocate a pci tunnel
47 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
50 * Currently only paths consisting of two hops are supported (that is the
51 * ports must be on "adjacent" switches).
53 * The paths are hard-coded to use hop 8 (the only working hop id available on
54 * my thunderbolt devices). Therefore at most ONE path per device may be
57 * Return: Returns a tb_pci_tunnel on success or NULL on failure.
59 struct tb_pci_tunnel
*tb_pci_alloc(struct tb
*tb
, struct tb_port
*up
,
62 struct tb_pci_tunnel
*tunnel
= kzalloc(sizeof(*tunnel
), GFP_KERNEL
);
66 tunnel
->down_port
= down
;
68 INIT_LIST_HEAD(&tunnel
->list
);
69 tunnel
->path_to_up
= tb_path_alloc(up
->sw
->tb
, 2);
70 if (!tunnel
->path_to_up
)
72 tunnel
->path_to_down
= tb_path_alloc(up
->sw
->tb
, 2);
73 if (!tunnel
->path_to_down
)
75 tb_pci_init_path(tunnel
->path_to_up
);
76 tb_pci_init_path(tunnel
->path_to_down
);
78 tunnel
->path_to_up
->hops
[0].in_port
= down
;
79 tunnel
->path_to_up
->hops
[0].in_hop_index
= 8;
80 tunnel
->path_to_up
->hops
[0].in_counter_index
= -1;
81 tunnel
->path_to_up
->hops
[0].out_port
= tb_upstream_port(up
->sw
)->remote
;
82 tunnel
->path_to_up
->hops
[0].next_hop_index
= 8;
84 tunnel
->path_to_up
->hops
[1].in_port
= tb_upstream_port(up
->sw
);
85 tunnel
->path_to_up
->hops
[1].in_hop_index
= 8;
86 tunnel
->path_to_up
->hops
[1].in_counter_index
= -1;
87 tunnel
->path_to_up
->hops
[1].out_port
= up
;
88 tunnel
->path_to_up
->hops
[1].next_hop_index
= 8;
90 tunnel
->path_to_down
->hops
[0].in_port
= up
;
91 tunnel
->path_to_down
->hops
[0].in_hop_index
= 8;
92 tunnel
->path_to_down
->hops
[0].in_counter_index
= -1;
93 tunnel
->path_to_down
->hops
[0].out_port
= tb_upstream_port(up
->sw
);
94 tunnel
->path_to_down
->hops
[0].next_hop_index
= 8;
96 tunnel
->path_to_down
->hops
[1].in_port
=
97 tb_upstream_port(up
->sw
)->remote
;
98 tunnel
->path_to_down
->hops
[1].in_hop_index
= 8;
99 tunnel
->path_to_down
->hops
[1].in_counter_index
= -1;
100 tunnel
->path_to_down
->hops
[1].out_port
= down
;
101 tunnel
->path_to_down
->hops
[1].next_hop_index
= 8;
106 if (tunnel
->path_to_down
)
107 tb_path_free(tunnel
->path_to_down
);
108 if (tunnel
->path_to_up
)
109 tb_path_free(tunnel
->path_to_up
);
116 * tb_pci_free() - free a tunnel
118 * The tunnel must have been deactivated.
120 void tb_pci_free(struct tb_pci_tunnel
*tunnel
)
122 if (tunnel
->path_to_up
->activated
|| tunnel
->path_to_down
->activated
) {
123 tb_tunnel_WARN(tunnel
, "trying to free an activated tunnel\n");
126 tb_path_free(tunnel
->path_to_up
);
127 tb_path_free(tunnel
->path_to_down
);
132 * tb_pci_is_invalid - check whether an activated path is still valid
134 bool tb_pci_is_invalid(struct tb_pci_tunnel
*tunnel
)
136 WARN_ON(!tunnel
->path_to_up
->activated
);
137 WARN_ON(!tunnel
->path_to_down
->activated
);
139 return tb_path_is_invalid(tunnel
->path_to_up
)
140 || tb_path_is_invalid(tunnel
->path_to_down
);
144 * tb_pci_port_active() - activate/deactivate PCI capability
146 * Return: Returns 0 on success or an error code on failure.
148 static int tb_pci_port_active(struct tb_port
*port
, bool active
)
150 u32 word
= active
? 0x80000000 : 0x0;
151 int cap
= tb_port_find_cap(port
, TB_PORT_CAP_ADAP
);
153 tb_port_warn(port
, "TB_PORT_CAP_ADAP not found: %d\n", cap
);
156 return tb_port_write(port
, &word
, TB_CFG_PORT
, cap
, 1);
160 * tb_pci_restart() - activate a tunnel after a hardware reset
162 int tb_pci_restart(struct tb_pci_tunnel
*tunnel
)
165 tunnel
->path_to_up
->activated
= false;
166 tunnel
->path_to_down
->activated
= false;
168 tb_tunnel_info(tunnel
, "activating\n");
170 res
= tb_path_activate(tunnel
->path_to_up
);
173 res
= tb_path_activate(tunnel
->path_to_down
);
177 res
= tb_pci_port_active(tunnel
->down_port
, true);
181 res
= tb_pci_port_active(tunnel
->up_port
, true);
186 tb_tunnel_warn(tunnel
, "activation failed\n");
187 tb_pci_deactivate(tunnel
);
192 * tb_pci_activate() - activate a tunnel
194 * Return: Returns 0 on success or an error code on failure.
196 int tb_pci_activate(struct tb_pci_tunnel
*tunnel
)
198 if (tunnel
->path_to_up
->activated
|| tunnel
->path_to_down
->activated
) {
199 tb_tunnel_WARN(tunnel
,
200 "trying to activate an already activated tunnel\n");
204 return tb_pci_restart(tunnel
);
210 * tb_pci_deactivate() - deactivate a tunnel
212 void tb_pci_deactivate(struct tb_pci_tunnel
*tunnel
)
214 tb_tunnel_info(tunnel
, "deactivating\n");
216 * TODO: enable reset by writing 0x04000000 to TB_CAP_PCIE + 1 on up
217 * port. Seems to have no effect?
219 tb_pci_port_active(tunnel
->up_port
, false);
220 tb_pci_port_active(tunnel
->down_port
, false);
221 if (tunnel
->path_to_down
->activated
)
222 tb_path_deactivate(tunnel
->path_to_down
);
223 if (tunnel
->path_to_up
->activated
)
224 tb_path_deactivate(tunnel
->path_to_up
);