Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 3 | * Thunderbolt driver - Tunneling support |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 6 | * Copyright (C) 2019, Intel Corporation |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/list.h> |
| 11 | |
Mika Westerberg | 1752b9f | 2017-02-19 10:58:35 +0200 | [diff] [blame] | 12 | #include "tunnel.h" |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 13 | #include "tb.h" |
| 14 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 15 | /* PCIe adapters use always HopID of 8 for both directions */ |
| 16 | #define TB_PCI_HOPID 8 |
| 17 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 18 | #define TB_PCI_PATH_DOWN 0 |
| 19 | #define TB_PCI_PATH_UP 1 |
| 20 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 21 | #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \ |
| 22 | do { \ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 23 | struct tb_tunnel *__tunnel = (tunnel); \ |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 24 | level(__tunnel->tb, "%llx:%x <-> %llx:%x (PCI): " fmt, \ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 25 | tb_route(__tunnel->src_port->sw), \ |
| 26 | __tunnel->src_port->port, \ |
| 27 | tb_route(__tunnel->dst_port->sw), \ |
| 28 | __tunnel->dst_port->port, \ |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 29 | ## arg); \ |
| 30 | } while (0) |
| 31 | |
| 32 | #define tb_tunnel_WARN(tunnel, fmt, arg...) \ |
| 33 | __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg) |
| 34 | #define tb_tunnel_warn(tunnel, fmt, arg...) \ |
| 35 | __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg) |
| 36 | #define tb_tunnel_info(tunnel, fmt, arg...) \ |
| 37 | __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg) |
| 38 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 39 | static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths) |
| 40 | { |
| 41 | struct tb_tunnel *tunnel; |
| 42 | |
| 43 | tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL); |
| 44 | if (!tunnel) |
| 45 | return NULL; |
| 46 | |
| 47 | tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL); |
| 48 | if (!tunnel->paths) { |
| 49 | tb_tunnel_free(tunnel); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | INIT_LIST_HEAD(&tunnel->list); |
| 54 | tunnel->tb = tb; |
| 55 | tunnel->npaths = npaths; |
| 56 | |
| 57 | return tunnel; |
| 58 | } |
| 59 | |
| 60 | static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate) |
| 61 | { |
| 62 | int res; |
| 63 | |
| 64 | res = tb_pci_port_enable(tunnel->src_port, activate); |
| 65 | if (res) |
| 66 | return res; |
| 67 | |
| 68 | return tb_pci_port_enable(tunnel->dst_port, activate); |
| 69 | } |
| 70 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 71 | static void tb_pci_init_path(struct tb_path *path) |
| 72 | { |
| 73 | path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL; |
| 74 | path->egress_shared_buffer = TB_PATH_NONE; |
| 75 | path->ingress_fc_enable = TB_PATH_ALL; |
| 76 | path->ingress_shared_buffer = TB_PATH_NONE; |
| 77 | path->priority = 3; |
| 78 | path->weight = 1; |
| 79 | path->drop_packages = 0; |
| 80 | path->nfc_credits = 0; |
| 81 | } |
| 82 | |
| 83 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 84 | * tb_tunnel_alloc_pci() - allocate a pci tunnel |
| 85 | * @tb: Pointer to the domain structure |
| 86 | * @up: PCIe upstream adapter port |
| 87 | * @down: PCIe downstream adapter port |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 88 | * |
| 89 | * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and |
| 90 | * TB_TYPE_PCIE_DOWN. |
| 91 | * |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 92 | * Return: Returns a tb_tunnel on success or NULL on failure. |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 93 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 94 | struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up, |
| 95 | struct tb_port *down) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 96 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 97 | struct tb_tunnel *tunnel; |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 98 | struct tb_path *path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 99 | |
| 100 | tunnel = tb_tunnel_alloc(tb, 2); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 101 | if (!tunnel) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 102 | return NULL; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 103 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 104 | tunnel->activate = tb_pci_activate; |
| 105 | tunnel->src_port = down; |
| 106 | tunnel->dst_port = up; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 107 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 108 | path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0, |
| 109 | "PCIe Down"); |
| 110 | if (!path) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 111 | tb_tunnel_free(tunnel); |
| 112 | return NULL; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 113 | } |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 114 | tb_pci_init_path(path); |
| 115 | tunnel->paths[TB_PCI_PATH_UP] = path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 116 | |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 117 | path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0, |
| 118 | "PCIe Up"); |
| 119 | if (!path) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 120 | tb_tunnel_free(tunnel); |
| 121 | return NULL; |
| 122 | } |
Mika Westerberg | 8c7acaaf | 2017-02-19 22:11:41 +0200 | [diff] [blame] | 123 | tb_pci_init_path(path); |
| 124 | tunnel->paths[TB_PCI_PATH_DOWN] = path; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 125 | |
| 126 | return tunnel; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 130 | * tb_tunnel_free() - free a tunnel |
| 131 | * @tunnel: Tunnel to be freed |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 132 | * |
| 133 | * The tunnel must have been deactivated. |
| 134 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 135 | void tb_tunnel_free(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 136 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 137 | int i; |
| 138 | |
| 139 | if (!tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 140 | return; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 141 | |
| 142 | for (i = 0; i < tunnel->npaths; i++) { |
| 143 | if (tunnel->paths[i] && tunnel->paths[i]->activated) { |
| 144 | tb_tunnel_WARN(tunnel, |
| 145 | "trying to free an activated tunnel\n"); |
| 146 | return; |
| 147 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 148 | } |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 149 | |
| 150 | for (i = 0; i < tunnel->npaths; i++) { |
| 151 | if (tunnel->paths[i]) |
| 152 | tb_path_free(tunnel->paths[i]); |
| 153 | } |
| 154 | |
| 155 | kfree(tunnel->paths); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 156 | kfree(tunnel); |
| 157 | } |
| 158 | |
| 159 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 160 | * tb_tunnel_is_invalid - check whether an activated path is still valid |
| 161 | * @tunnel: Tunnel to check |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 162 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 163 | bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 164 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 165 | int i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 166 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 167 | for (i = 0; i < tunnel->npaths; i++) { |
| 168 | WARN_ON(!tunnel->paths[i]->activated); |
| 169 | if (tb_path_is_invalid(tunnel->paths[i])) |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 177 | * tb_tunnel_restart() - activate a tunnel after a hardware reset |
| 178 | * @tunnel: Tunnel to restart |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 179 | * |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 180 | * Return: 0 on success and negative errno in case if failure |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 181 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 182 | int tb_tunnel_restart(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 183 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 184 | int res, i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 185 | |
| 186 | tb_tunnel_info(tunnel, "activating\n"); |
| 187 | |
Mika Westerberg | aae9e27 | 2017-02-19 23:37:35 +0200 | [diff] [blame^] | 188 | /* |
| 189 | * Make sure all paths are properly disabled before enabling |
| 190 | * them again. |
| 191 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 192 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | aae9e27 | 2017-02-19 23:37:35 +0200 | [diff] [blame^] | 193 | if (tunnel->paths[i]->activated) { |
| 194 | tb_path_deactivate(tunnel->paths[i]); |
| 195 | tunnel->paths[i]->activated = false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | for (i = 0; i < tunnel->npaths; i++) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 200 | res = tb_path_activate(tunnel->paths[i]); |
| 201 | if (res) |
| 202 | goto err; |
| 203 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 204 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 205 | if (tunnel->activate) { |
| 206 | res = tunnel->activate(tunnel, true); |
| 207 | if (res) |
| 208 | goto err; |
| 209 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 210 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 211 | return 0; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 212 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 213 | err: |
| 214 | tb_tunnel_warn(tunnel, "activation failed\n"); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 215 | tb_tunnel_deactivate(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 216 | return res; |
| 217 | } |
| 218 | |
| 219 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 220 | * tb_tunnel_activate() - activate a tunnel |
| 221 | * @tunnel: Tunnel to activate |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 222 | * |
| 223 | * Return: Returns 0 on success or an error code on failure. |
| 224 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 225 | int tb_tunnel_activate(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 226 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 227 | int i; |
| 228 | |
| 229 | tb_tunnel_info(tunnel, "activating\n"); |
| 230 | |
| 231 | for (i = 0; i < tunnel->npaths; i++) { |
| 232 | if (tunnel->paths[i]->activated) { |
| 233 | tb_tunnel_WARN(tunnel, |
| 234 | "trying to activate an already activated tunnel\n"); |
| 235 | return -EINVAL; |
| 236 | } |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 239 | return tb_tunnel_restart(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 242 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 243 | * tb_tunnel_deactivate() - deactivate a tunnel |
| 244 | * @tunnel: Tunnel to deactivate |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 245 | */ |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 246 | void tb_tunnel_deactivate(struct tb_tunnel *tunnel) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 247 | { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 248 | int i; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 249 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 250 | tb_tunnel_info(tunnel, "deactivating\n"); |
| 251 | |
| 252 | if (tunnel->activate) |
| 253 | tunnel->activate(tunnel, false); |
| 254 | |
| 255 | for (i = 0; i < tunnel->npaths; i++) { |
| 256 | if (tunnel->paths[i]->activated) |
| 257 | tb_path_deactivate(tunnel->paths[i]); |
| 258 | } |
| 259 | } |