summaryrefslogtreecommitdiff
path: root/drivers/infiniband/hw/usnic/usnic_fwd.c
blob: 7875883621f4eb165b50178a316dc46dd0383739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#include <linux/netdevice.h>
#include <linux/pci.h>

#include "enic_api.h"
#include "usnic_common_pkt_hdr.h"
#include "usnic_fwd.h"
#include "usnic_log.h"

static int usnic_fwd_devcmd_locked(struct usnic_fwd_dev *ufdev, int vnic_idx,
					enum vnic_devcmd_cmd cmd, u64 *a0,
					u64 *a1)
{
	int status;
	struct net_device *netdev = ufdev->netdev;

	lockdep_assert_held(&ufdev->lock);

	status = enic_api_devcmd_proxy_by_index(netdev,
			vnic_idx,
			cmd,
			a0, a1,
			1000);
	if (status) {
		if (status == ERR_EINVAL && cmd == CMD_DEL_FILTER) {
			usnic_dbg("Dev %s vnic idx %u cmd %u already deleted",
					ufdev->name, vnic_idx, cmd);
		} else {
			usnic_err("Dev %s vnic idx %u cmd %u failed with status %d\n",
					ufdev->name, vnic_idx, cmd,
					status);
		}
	} else {
		usnic_dbg("Dev %s vnic idx %u cmd %u success",
				ufdev->name, vnic_idx, cmd);
	}

	return status;
}

static int usnic_fwd_devcmd(struct usnic_fwd_dev *ufdev, int vnic_idx,
				enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1)
{
	int status;

	spin_lock(&ufdev->lock);
	status = usnic_fwd_devcmd_locked(ufdev, vnic_idx, cmd, a0, a1);
	spin_unlock(&ufdev->lock);

	return status;
}

struct usnic_fwd_dev *usnic_fwd_dev_alloc(struct pci_dev *pdev)
{
	struct usnic_fwd_dev *ufdev;

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

	ufdev->pdev = pdev;
	ufdev->netdev = pci_get_drvdata(pdev);
	spin_lock_init(&ufdev->lock);
	BUILD_BUG_ON(sizeof(ufdev->name) != sizeof(ufdev->netdev->name));
	strcpy(ufdev->name, ufdev->netdev->name);

	return ufdev;
}

void usnic_fwd_dev_free(struct usnic_fwd_dev *ufdev)
{
	kfree(ufdev);
}

void usnic_fwd_set_mac(struct usnic_fwd_dev *ufdev, char mac[ETH_ALEN])
{
	spin_lock(&ufdev->lock);
	memcpy(&ufdev->mac, mac, sizeof(ufdev->mac));
	spin_unlock(&ufdev->lock);
}

void usnic_fwd_add_ipaddr(struct usnic_fwd_dev *ufdev, __be32 inaddr)
{
	spin_lock(&ufdev->lock);
	if (!ufdev->inaddr)
		ufdev->inaddr = inaddr;
	spin_unlock(&ufdev->lock);
}

void usnic_fwd_del_ipaddr(struct usnic_fwd_dev *ufdev)
{
	spin_lock(&ufdev->lock);
	ufdev->inaddr = 0;
	spin_unlock(&ufdev->lock);
}

void usnic_fwd_carrier_up(struct usnic_fwd_dev *ufdev)
{
	spin_lock(&ufdev->lock);
	ufdev->link_up = 1;
	spin_unlock(&ufdev->lock);
}

void usnic_fwd_carrier_down(struct usnic_fwd_dev *ufdev)
{
	spin_lock(&ufdev->lock);
	ufdev->link_up = 0;
	spin_unlock(&ufdev->lock);
}

void usnic_fwd_set_mtu(struct usnic_fwd_dev *ufdev, unsigned int mtu)
{
	spin_lock(&ufdev->lock);
	ufdev->mtu = mtu;
	spin_unlock(&ufdev->lock);
}

static int usnic_fwd_dev_ready_locked(struct usnic_fwd_dev *ufdev)
{
	lockdep_assert_held(&ufdev->lock);

	if (!ufdev->link_up)
		return -EPERM;

	return 0;
}

static int validate_filter_locked(struct usnic_fwd_dev *ufdev,
					struct filter *filter)
{

	lockdep_assert_held(&ufdev->lock);

	if (filter->type == FILTER_IPV4_5TUPLE) {
		if (!(filter->u.ipv4.flags & FILTER_FIELD_5TUP_DST_AD))
			return -EACCES;
		if (!(filter->u.ipv4.flags & FILTER_FIELD_5TUP_DST_PT))
			return -EBUSY;
		else if (ufdev->inaddr == 0)
			return -EINVAL;
		else if (filter->u.ipv4.dst_port == 0)
			return -ERANGE;
		else if (ntohl(ufdev->inaddr) != filter->u.ipv4.dst_addr)
			return -EFAULT;
		else
			return 0;
	}

	return 0;
}

static void fill_tlv(struct filter_tlv *tlv, struct filter *filter,
		struct filter_action *action)
{
	tlv->type = CLSF_TLV_FILTER;
	tlv->length = sizeof(struct filter);
	*((struct filter *)&tlv->val) = *filter;

	tlv = (struct filter_tlv *)((char *)tlv + sizeof(struct filter_tlv) +
			sizeof(struct filter));
	tlv->type = CLSF_TLV_ACTION;
	tlv->length = sizeof(struct filter_action);
	*((struct filter_action *)&tlv->val) = *action;
}

struct usnic_fwd_flow*
usnic_fwd_alloc_flow(struct usnic_fwd_dev *ufdev, struct filter *filter,
				struct usnic_filter_action *uaction)
{
	struct filter_tlv *tlv;
	struct pci_dev *pdev;
	struct usnic_fwd_flow *flow;
	uint64_t a0, a1;
	uint64_t tlv_size;
	dma_addr_t tlv_pa;
	int status;

	pdev = ufdev->pdev;
	tlv_size = (2*sizeof(struct filter_tlv) + sizeof(struct filter) +
			sizeof(struct filter_action));

	flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
	if (!flow)
		return ERR_PTR(-ENOMEM);

	tlv = pci_alloc_consistent(pdev, tlv_size, &tlv_pa);
	if (!tlv) {
		usnic_err("Failed to allocate memory\n");
		status = -ENOMEM;
		goto out_free_flow;
	}

	fill_tlv(tlv, filter, &uaction->action);

	spin_lock(&ufdev->lock);
	status = usnic_fwd_dev_ready_locked(ufdev);
	if (status) {
		usnic_err("Forwarding dev %s not ready with status %d\n",
				ufdev->name, status);
		goto out_free_tlv;
	}

	status = validate_filter_locked(ufdev, filter);
	if (status) {
		usnic_err("Failed to validate filter with status %d\n",
				status);
		goto out_free_tlv;
	}

	/* Issue Devcmd */
	a0 = tlv_pa;
	a1 = tlv_size;
	status = usnic_fwd_devcmd_locked(ufdev, uaction->vnic_idx,
						CMD_ADD_FILTER, &a0, &a1);
	if (status) {
		usnic_err("VF %s Filter add failed with status:%d",
				ufdev->name, status);
		status = -EFAULT;
		goto out_free_tlv;
	} else {
		usnic_dbg("VF %s FILTER ID:%llu", ufdev->name, a0);
	}

	flow->flow_id = (uint32_t) a0;
	flow->vnic_idx = uaction->vnic_idx;
	flow->ufdev = ufdev;

out_free_tlv:
	spin_unlock(&ufdev->lock);
	pci_free_consistent(pdev, tlv_size, tlv, tlv_pa);
	if (!status)
		return flow;
out_free_flow:
	kfree(flow);
	return ERR_PTR(status);
}

int usnic_fwd_dealloc_flow(struct usnic_fwd_flow *flow)
{
	int status;
	u64 a0, a1;

	a0 = flow->flow_id;

	status = usnic_fwd_devcmd(flow->ufdev, flow->vnic_idx,
					CMD_DEL_FILTER, &a0, &a1);
	if (status) {
		if (status == ERR_EINVAL) {
			usnic_dbg("Filter %u already deleted for VF Idx %u pf: %s status: %d",
					flow->flow_id, flow->vnic_idx,
					flow->ufdev->name, status);
		} else {
			usnic_err("PF %s VF Idx %u Filter: %u FILTER DELETE failed with status %d",
					flow->ufdev->name, flow->vnic_idx,
					flow->flow_id, status);
		}
		status = 0;
		/*
		 * Log the error and fake success to the caller because if
		 * a flow fails to be deleted in the firmware, it is an
		 * unrecoverable error.
		 */
	} else {
		usnic_dbg("PF %s VF Idx %u Filter: %u FILTER DELETED",
				flow->ufdev->name, flow->vnic_idx,
				flow->flow_id);
	}

	kfree(flow);
	return status;
}

int usnic_fwd_enable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx)
{
	int status;
	struct net_device *pf_netdev;
	u64 a0, a1;

	pf_netdev = ufdev->netdev;
	a0 = qp_idx;
	a1 = CMD_QP_RQWQ;

	status = usnic_fwd_devcmd(ufdev, vnic_idx, CMD_QP_ENABLE,
						&a0, &a1);
	if (status) {
		usnic_err("PF %s VNIC Index %u RQ Index: %u ENABLE Failed with status %d",
				netdev_name(pf_netdev),
				vnic_idx,
				qp_idx,
				status);
	} else {
		usnic_dbg("PF %s VNIC Index %u RQ Index: %u ENABLED",
				netdev_name(pf_netdev),
				vnic_idx, qp_idx);
	}

	return status;
}

int usnic_fwd_disable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx)
{
	int status;
	u64 a0, a1;
	struct net_device *pf_netdev;

	pf_netdev = ufdev->netdev;
	a0 = qp_idx;
	a1 = CMD_QP_RQWQ;

	status = usnic_fwd_devcmd(ufdev, vnic_idx, CMD_QP_DISABLE,
			&a0, &a1);
	if (status) {
		usnic_err("PF %s VNIC Index %u RQ Index: %u DISABLE Failed with status %d",
				netdev_name(pf_netdev),
				vnic_idx,
				qp_idx,
				status);
	} else {
		usnic_dbg("PF %s VNIC Index %u RQ Index: %u DISABLED",
				netdev_name(pf_netdev),
				vnic_idx,
				qp_idx);
	}

	return status;
}