aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/marvell/octeontx2/nic/otx2_smqvf.c
blob: 4f5f73f621d44519beaf1da366bace6b988ae668 (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
// SPDX-License-Identifier: GPL-2.0
/* Marvell OcteonTx2 RVU Virtual Function ethernet driver
 *
 * Copyright (C) 2019 Marvell International Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/etherdevice.h>
#include <linux/pci.h>

#include "otx2_common.h"
#include "otx2_reg.h"
#include "otx2_struct.h"
#include "rvu_fixes.h"

/* serialize device removal and xmit */
DEFINE_MUTEX(remove_lock);

static char pkt_data[64] = { 0x00, 0x0f, 0xb7, 0x11, 0xa6, 0x87, 0x02, 0xe0,
			     0x28, 0xa5, 0xf6, 0x00, 0x08, 0x00, 0x45, 0x00,
			     0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x04, 0x11,
			     0xee, 0x53, 0x50, 0x50, 0x50, 0x02, 0x14, 0x14,
			     0x14, 0x02, 0x10, 0x00, 0x10, 0x01, 0x00, 0x1e,
			     0x00, 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
			     0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
			     0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76 };

static struct sk_buff *the_skb;
static struct otx2_nic *the_smqvf;
static u16 drop_entry = 0xFFFF;

static bool is_otx2_smqvf(struct otx2_nic *vf)
{
	if (vf->pcifunc == RVU_SMQVF_PCIFUNC &&
	    (is_96xx_A0(vf->pdev) || is_95xx_A0(vf->pdev)))
		return true;

	return false;
}

static void otx2_sqe_flush(struct otx2_snd_queue *sq, int size)
{
	u64 status;

	/* Packet data stores should finish before SQE is flushed to HW */
	dma_wmb();

	do {
		memcpy(sq->lmt_addr, sq->sqe_base, size);
		status = otx2_lmt_flush(sq->io_addr);
	} while (status == 0);

	sq->head++;
	sq->head &= (sq->sqe_cnt - 1);
}

static int otx2_ctx_update(struct otx2_nic *vf, u16 qidx)
{
	struct nix_aq_enq_req *sq_aq, *rq_aq, *cq_aq;

	/* Do not link CQ for SQ and disable RQ, CQ */
	sq_aq = otx2_mbox_alloc_msg_nix_aq_enq(&vf->mbox);
	if (!sq_aq)
		return -ENOMEM;

	sq_aq->sq.cq_ena = 0;
	sq_aq->sq_mask.cq_ena = 1;
	sq_aq->qidx = qidx;
	sq_aq->ctype = NIX_AQ_CTYPE_SQ;
	sq_aq->op = NIX_AQ_INSTOP_WRITE;

	rq_aq = otx2_mbox_alloc_msg_nix_aq_enq(&vf->mbox);
	if (!rq_aq)
		return -ENOMEM;

	rq_aq->rq.ena = 0;
	rq_aq->rq_mask.ena = 1;
	rq_aq->qidx = qidx;
	rq_aq->ctype = NIX_AQ_CTYPE_RQ;
	rq_aq->op = NIX_AQ_INSTOP_WRITE;

	cq_aq = otx2_mbox_alloc_msg_nix_aq_enq(&vf->mbox);
	if (!cq_aq)
		return -ENOMEM;

	cq_aq->cq.ena = 0;
	cq_aq->cq_mask.ena = 1;
	cq_aq->qidx = qidx;
	cq_aq->ctype = NIX_AQ_CTYPE_CQ;
	cq_aq->op = NIX_AQ_INSTOP_WRITE;

	return otx2_sync_mbox_msg(&vf->mbox);
}

void otx2smqvf_xmit(void)
{
	struct otx2_snd_queue *sq;
	int i, size;

	mutex_lock(&remove_lock);

	if (!the_smqvf) {
		mutex_unlock(&remove_lock);
		return;
	}

	sq = &the_smqvf->qset.sq[0];
	/* Min. set of send descriptors required to send packets */
	size = sizeof(struct nix_sqe_hdr_s) + sizeof(struct nix_sqe_sg_s) +
	       sizeof(struct nix_sqe_ext_s) + sizeof(u64);

	for (i = 0; i < 256; i++)
		otx2_sqe_flush(sq, size);

	mutex_unlock(&remove_lock);
}
EXPORT_SYMBOL(otx2smqvf_xmit);

static int otx2smqvf_install_flow(struct otx2_nic *vf)
{
	struct npc_mcam_alloc_entry_req *alloc_req;
	struct npc_mcam_free_entry_req *free_req;
	struct npc_install_flow_req *install_req;
	struct npc_mcam_alloc_entry_rsp *rsp;
	struct msg_req *msg;
	int err, qid;
	size_t size;
	void *data;

	size = SKB_DATA_ALIGN(64 + OTX2_ALIGN) + NET_SKB_PAD +
		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));

	err = -ENOMEM;

	data = kzalloc(size, GFP_KERNEL);
	if (!data)
		return err;

	memcpy(data, &pkt_data, 64);

	the_skb = build_skb(data, 0);
	the_skb->len = 64;

	for (qid = 0; qid < vf->hw.tx_queues; qid++) {
		err = otx2_ctx_update(vf, qid);
		/* If something wrong with Q0 then treat as error */
		if (err && !qid)
			goto err_free_mem;
	}

	otx2_mbox_lock(&vf->mbox);

	alloc_req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&vf->mbox);
	if (!alloc_req) {
		otx2_mbox_unlock(&vf->mbox);
		goto err_free_mem;
	}
	alloc_req->count = 1;
	alloc_req->contig = true;

	/* Send message to AF */
	if (otx2_sync_mbox_msg(&vf->mbox)) {
		err = -EINVAL;
		otx2_mbox_unlock(&vf->mbox);
		goto err_free_mem;
	}
	otx2_mbox_unlock(&vf->mbox);

	rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
	       (&vf->mbox.mbox, 0, &alloc_req->hdr);
	drop_entry = rsp->entry;

	otx2_mbox_lock(&vf->mbox);

	/* Send messages to drop Tx packets at NPC and stop Rx traffic */
	install_req = otx2_mbox_alloc_msg_npc_install_flow(&vf->mbox);
	if (!install_req) {
		err = -ENOMEM;
		otx2_mbox_unlock(&vf->mbox);
		goto err_free_entry;
	}

	u64_to_ether_addr(0x0ull, install_req->mask.dmac);
	install_req->entry = drop_entry;
	install_req->features = BIT_ULL(NPC_DMAC);
	install_req->intf = NIX_INTF_TX;
	install_req->op = NIX_TX_ACTIONOP_DROP;
	install_req->set_cntr = 1;

	msg = otx2_mbox_alloc_msg_nix_lf_stop_rx(&vf->mbox);
	if (!msg) {
		otx2_mbox_unlock(&vf->mbox);
		goto err_free_entry;
	}

	/* Send message to AF */
	if (otx2_sync_mbox_msg(&vf->mbox)) {
		err = -EINVAL;
		otx2_mbox_unlock(&vf->mbox);
		goto err_free_entry;
	}
	otx2_mbox_unlock(&vf->mbox);

	otx2_sq_append_skb(vf->netdev, &vf->qset.sq[0], the_skb, 0);

	return 0;

err_free_entry:
	otx2_mbox_lock(&vf->mbox);
	free_req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&vf->mbox);
	if (!free_req) {
		dev_err(vf->dev, "Could not allocate msg for freeing entry\n");
	} else {
		free_req->entry = drop_entry;
		WARN_ON(otx2_sync_mbox_msg(&vf->mbox));
	}
	otx2_mbox_unlock(&vf->mbox);
err_free_mem:
	kfree_skb(the_skb);
	drop_entry = 0xFFFF;
	return err;
}

int otx2smqvf_probe(struct otx2_nic *vf)
{
	int err;

	if (!is_otx2_smqvf(vf))
		return -EPERM;

	err = otx2_open(vf->netdev);
	if (err)
		return -EINVAL;

	/* Disable QINT interrupts because we do not use a CQ for SQ and
	 * drop TX packets intentionally
	 */
	otx2_write64(vf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));

	err = otx2smqvf_install_flow(vf);
	if (err) {
		otx2_stop(vf->netdev);
		return -EINVAL;
	}

	the_smqvf = vf;

	return 0;
}

int otx2smqvf_remove(struct otx2_nic *vf)
{
	struct npc_mcam_free_entry_req *free_req;
	struct npc_delete_flow_req *del_req;

	if (!is_otx2_smqvf(vf))
		return -EPERM;

	mutex_lock(&remove_lock);
	kfree_skb(the_skb);
	the_smqvf = NULL;
	the_skb = NULL;
	mutex_unlock(&remove_lock);

	otx2_mbox_lock(&vf->mbox);
	del_req = otx2_mbox_alloc_msg_npc_delete_flow(&vf->mbox);
	free_req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&vf->mbox);
	if (!del_req || !free_req) {
		dev_err(vf->dev, "Could not allocate msg for freeing entry\n");
	} else {
		del_req->entry = drop_entry;
		free_req->entry = drop_entry;
		WARN_ON(otx2_sync_mbox_msg(&vf->mbox));
	}
	otx2_mbox_unlock(&vf->mbox);

	otx2_stop(vf->netdev);
	drop_entry = 0xFFFF;

	return 0;
}