tunnel.c 5.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * Thunderbolt driver - Tunneling support
4 5
 *
 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6
 * Copyright (C) 2019, Intel Corporation
7 8 9 10 11
 */

#include <linux/slab.h>
#include <linux/list.h>

12
#include "tunnel.h"
13 14
#include "tb.h"

15 16 17
/* PCIe adapters use always HopID of 8 for both directions */
#define TB_PCI_HOPID			8

18 19 20
#define TB_PCI_PATH_DOWN		0
#define TB_PCI_PATH_UP			1

21 22
#define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...)                   \
	do {                                                            \
23
		struct tb_tunnel *__tunnel = (tunnel);                  \
24
		level(__tunnel->tb, "%llx:%x <-> %llx:%x (PCI): " fmt,  \
25 26 27 28
		      tb_route(__tunnel->src_port->sw),                 \
		      __tunnel->src_port->port,                         \
		      tb_route(__tunnel->dst_port->sw),                 \
		      __tunnel->dst_port->port,                         \
29 30 31 32 33 34 35 36 37 38
		      ## arg);                                          \
	} while (0)

#define tb_tunnel_WARN(tunnel, fmt, arg...) \
	__TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
#define tb_tunnel_warn(tunnel, fmt, arg...) \
	__TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
#define tb_tunnel_info(tunnel, fmt, arg...) \
	__TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths)
{
	struct tb_tunnel *tunnel;

	tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
	if (!tunnel)
		return NULL;

	tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
	if (!tunnel->paths) {
		tb_tunnel_free(tunnel);
		return NULL;
	}

	INIT_LIST_HEAD(&tunnel->list);
	tunnel->tb = tb;
	tunnel->npaths = npaths;

	return tunnel;
}

static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
{
	int res;

	res = tb_pci_port_enable(tunnel->src_port, activate);
	if (res)
		return res;

	return tb_pci_port_enable(tunnel->dst_port, activate);
}

71 72 73 74 75 76 77 78 79 80 81 82 83
static void tb_pci_init_path(struct tb_path *path)
{
	path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
	path->egress_shared_buffer = TB_PATH_NONE;
	path->ingress_fc_enable = TB_PATH_ALL;
	path->ingress_shared_buffer = TB_PATH_NONE;
	path->priority = 3;
	path->weight = 1;
	path->drop_packages = 0;
	path->nfc_credits = 0;
}

/**
84 85 86 87
 * tb_tunnel_alloc_pci() - allocate a pci tunnel
 * @tb: Pointer to the domain structure
 * @up: PCIe upstream adapter port
 * @down: PCIe downstream adapter port
88 89 90 91
 *
 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
 * TB_TYPE_PCIE_DOWN.
 *
92
 * Return: Returns a tb_tunnel on success or NULL on failure.
93
 */
94 95
struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
				      struct tb_port *down)
96
{
97
	struct tb_tunnel *tunnel;
98
	struct tb_path *path;
99 100

	tunnel = tb_tunnel_alloc(tb, 2);
101
	if (!tunnel)
102
		return NULL;
103

104 105 106 107
	tunnel->activate = tb_pci_activate;
	tunnel->src_port = down;
	tunnel->dst_port = up;

108 109 110
	path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
			     "PCIe Down");
	if (!path) {
111 112
		tb_tunnel_free(tunnel);
		return NULL;
113
	}
114 115
	tb_pci_init_path(path);
	tunnel->paths[TB_PCI_PATH_UP] = path;
116

117 118 119
	path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
			     "PCIe Up");
	if (!path) {
120 121 122
		tb_tunnel_free(tunnel);
		return NULL;
	}
123 124
	tb_pci_init_path(path);
	tunnel->paths[TB_PCI_PATH_DOWN] = path;
125 126

	return tunnel;
127 128 129
}

/**
130 131
 * tb_tunnel_free() - free a tunnel
 * @tunnel: Tunnel to be freed
132 133 134
 *
 * The tunnel must have been deactivated.
 */
135
void tb_tunnel_free(struct tb_tunnel *tunnel)
136
{
137 138 139
	int i;

	if (!tunnel)
140
		return;
141 142 143 144 145 146 147

	for (i = 0; i < tunnel->npaths; i++) {
		if (tunnel->paths[i] && tunnel->paths[i]->activated) {
			tb_tunnel_WARN(tunnel,
				       "trying to free an activated tunnel\n");
			return;
		}
148
	}
149 150 151 152 153 154 155

	for (i = 0; i < tunnel->npaths; i++) {
		if (tunnel->paths[i])
			tb_path_free(tunnel->paths[i]);
	}

	kfree(tunnel->paths);
156 157 158 159
	kfree(tunnel);
}

/**
160 161
 * tb_tunnel_is_invalid - check whether an activated path is still valid
 * @tunnel: Tunnel to check
162
 */
163
bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
164
{
165
	int i;
166

167 168 169 170 171
	for (i = 0; i < tunnel->npaths; i++) {
		WARN_ON(!tunnel->paths[i]->activated);
		if (tb_path_is_invalid(tunnel->paths[i]))
			return true;
	}
172

173
	return false;
174 175 176
}

/**
177 178 179 180
 * tb_tunnel_restart() - activate a tunnel after a hardware reset
 * @tunnel: Tunnel to restart
 *
 * Return: 0 on success and negative errno in case if failure
181
 */
182
int tb_tunnel_restart(struct tb_tunnel *tunnel)
183
{
184
	int res, i;
185 186 187

	tb_tunnel_info(tunnel, "activating\n");

188 189 190 191 192 193
	for (i = 0; i < tunnel->npaths; i++) {
		tunnel->paths[i]->activated = false;
		res = tb_path_activate(tunnel->paths[i]);
		if (res)
			goto err;
	}
194

195 196 197 198 199
	if (tunnel->activate) {
		res = tunnel->activate(tunnel, true);
		if (res)
			goto err;
	}
200 201

	return 0;
202

203 204
err:
	tb_tunnel_warn(tunnel, "activation failed\n");
205
	tb_tunnel_deactivate(tunnel);
206 207 208 209
	return res;
}

/**
210 211
 * tb_tunnel_activate() - activate a tunnel
 * @tunnel: Tunnel to activate
212 213 214
 *
 * Return: Returns 0 on success or an error code on failure.
 */
215
int tb_tunnel_activate(struct tb_tunnel *tunnel)
216
{
217
	int i;
218

219
	tb_tunnel_info(tunnel, "activating\n");
220

221 222 223 224 225 226 227
	for (i = 0; i < tunnel->npaths; i++) {
		if (tunnel->paths[i]->activated) {
			tb_tunnel_WARN(tunnel,
				       "trying to activate an already activated tunnel\n");
			return -EINVAL;
		}
	}
228

229 230
	return tb_tunnel_restart(tunnel);
}
231 232

/**
233 234
 * tb_tunnel_deactivate() - deactivate a tunnel
 * @tunnel: Tunnel to deactivate
235
 */
236
void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
237
{
238 239
	int i;

240 241
	tb_tunnel_info(tunnel, "deactivating\n");

242 243 244 245 246 247 248 249
	if (tunnel->activate)
		tunnel->activate(tunnel, false);

	for (i = 0; i < tunnel->npaths; i++) {
		if (tunnel->paths[i]->activated)
			tb_path_deactivate(tunnel->paths[i]);
	}
}