blob: bdce5915baae42daec2036491fba6826a67adc20 [file] [log] [blame]
Sunil Goutham4863dea2015-05-26 19:20:15 -07001/*
2 * Copyright (C) 2015 Cavium, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 */
8
9#include <linux/pci.h>
10#include <linux/netdevice.h>
11#include <linux/ip.h>
12#include <linux/etherdevice.h>
13#include <net/ip.h>
14#include <net/tso.h>
15
16#include "nic_reg.h"
17#include "nic.h"
18#include "q_struct.h"
19#include "nicvf_queues.h"
20
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053021static void nicvf_get_page(struct nicvf *nic)
22{
23 if (!nic->rb_pageref || !nic->rb_page)
24 return;
25
Joonsoo Kim6d061f92016-05-19 17:10:46 -070026 page_ref_add(nic->rb_page, nic->rb_pageref);
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053027 nic->rb_pageref = 0;
28}
29
Sunil Goutham4863dea2015-05-26 19:20:15 -070030/* Poll a register for a specific value */
31static int nicvf_poll_reg(struct nicvf *nic, int qidx,
32 u64 reg, int bit_pos, int bits, int val)
33{
34 u64 bit_mask;
35 u64 reg_val;
36 int timeout = 10;
37
38 bit_mask = (1ULL << bits) - 1;
39 bit_mask = (bit_mask << bit_pos);
40
41 while (timeout) {
42 reg_val = nicvf_queue_reg_read(nic, reg, qidx);
43 if (((reg_val & bit_mask) >> bit_pos) == val)
44 return 0;
45 usleep_range(1000, 2000);
46 timeout--;
47 }
48 netdev_err(nic->netdev, "Poll on reg 0x%llx failed\n", reg);
49 return 1;
50}
51
52/* Allocate memory for a queue's descriptors */
53static int nicvf_alloc_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem,
54 int q_len, int desc_size, int align_bytes)
55{
56 dmem->q_len = q_len;
57 dmem->size = (desc_size * q_len) + align_bytes;
58 /* Save address, need it while freeing */
59 dmem->unalign_base = dma_zalloc_coherent(&nic->pdev->dev, dmem->size,
60 &dmem->dma, GFP_KERNEL);
61 if (!dmem->unalign_base)
62 return -ENOMEM;
63
64 /* Align memory address for 'align_bytes' */
65 dmem->phys_base = NICVF_ALIGNED_ADDR((u64)dmem->dma, align_bytes);
Aleksey Makarov39a0dd02015-06-02 11:00:25 -070066 dmem->base = dmem->unalign_base + (dmem->phys_base - dmem->dma);
Sunil Goutham4863dea2015-05-26 19:20:15 -070067 return 0;
68}
69
70/* Free queue's descriptor memory */
71static void nicvf_free_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem)
72{
73 if (!dmem)
74 return;
75
76 dma_free_coherent(&nic->pdev->dev, dmem->size,
77 dmem->unalign_base, dmem->dma);
78 dmem->unalign_base = NULL;
79 dmem->base = NULL;
80}
81
82/* Allocate buffer for packet reception
83 * HW returns memory address where packet is DMA'ed but not a pointer
84 * into RBDR ring, so save buffer address at the start of fragment and
85 * align the start address to a cache aligned address
86 */
87static inline int nicvf_alloc_rcv_buffer(struct nicvf *nic, gfp_t gfp,
88 u32 buf_len, u64 **rbuf)
89{
Sunil Goutham6e4be8d2016-02-11 21:50:26 +053090 int order = (PAGE_SIZE <= 4096) ? PAGE_ALLOC_COSTLY_ORDER : 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -070091
92 /* Check if request can be accomodated in previous allocated page */
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053093 if (nic->rb_page &&
94 ((nic->rb_page_offset + buf_len) < (PAGE_SIZE << order))) {
95 nic->rb_pageref++;
96 goto ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -070097 }
98
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053099 nicvf_get_page(nic);
100 nic->rb_page = NULL;
101
Sunil Goutham4863dea2015-05-26 19:20:15 -0700102 /* Allocate a new page */
103 if (!nic->rb_page) {
Sunil Gouthamf8ce9662015-07-29 16:49:41 +0300104 nic->rb_page = alloc_pages(gfp | __GFP_COMP | __GFP_NOWARN,
105 order);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700106 if (!nic->rb_page) {
Sunil Goutham964cb692016-11-15 17:38:16 +0530107 this_cpu_inc(nic->pnicvf->drv_stats->
108 rcv_buffer_alloc_failures);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700109 return -ENOMEM;
110 }
111 nic->rb_page_offset = 0;
112 }
113
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530114ret:
Sunil Goutham668dda02015-12-07 10:30:33 +0530115 *rbuf = (u64 *)((u64)page_address(nic->rb_page) + nic->rb_page_offset);
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530116 nic->rb_page_offset += buf_len;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700117
Sunil Goutham4863dea2015-05-26 19:20:15 -0700118 return 0;
119}
120
Sunil Goutham668dda02015-12-07 10:30:33 +0530121/* Build skb around receive buffer */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700122static struct sk_buff *nicvf_rb_ptr_to_skb(struct nicvf *nic,
123 u64 rb_ptr, int len)
124{
Sunil Goutham668dda02015-12-07 10:30:33 +0530125 void *data;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700126 struct sk_buff *skb;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700127
Sunil Goutham668dda02015-12-07 10:30:33 +0530128 data = phys_to_virt(rb_ptr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700129
130 /* Now build an skb to give to stack */
Sunil Goutham668dda02015-12-07 10:30:33 +0530131 skb = build_skb(data, RCV_FRAG_LEN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700132 if (!skb) {
Sunil Goutham668dda02015-12-07 10:30:33 +0530133 put_page(virt_to_page(data));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700134 return NULL;
135 }
136
Sunil Goutham668dda02015-12-07 10:30:33 +0530137 prefetch(skb->data);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700138 return skb;
139}
140
141/* Allocate RBDR ring and populate receive buffers */
142static int nicvf_init_rbdr(struct nicvf *nic, struct rbdr *rbdr,
143 int ring_len, int buf_size)
144{
145 int idx;
146 u64 *rbuf;
147 struct rbdr_entry_t *desc;
148 int err;
149
150 err = nicvf_alloc_q_desc_mem(nic, &rbdr->dmem, ring_len,
151 sizeof(struct rbdr_entry_t),
152 NICVF_RCV_BUF_ALIGN_BYTES);
153 if (err)
154 return err;
155
156 rbdr->desc = rbdr->dmem.base;
157 /* Buffer size has to be in multiples of 128 bytes */
158 rbdr->dma_size = buf_size;
159 rbdr->enable = true;
160 rbdr->thresh = RBDR_THRESH;
161
162 nic->rb_page = NULL;
163 for (idx = 0; idx < ring_len; idx++) {
164 err = nicvf_alloc_rcv_buffer(nic, GFP_KERNEL, RCV_FRAG_LEN,
165 &rbuf);
166 if (err)
167 return err;
168
169 desc = GET_RBDR_DESC(rbdr, idx);
170 desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
171 }
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530172
173 nicvf_get_page(nic);
174
Sunil Goutham4863dea2015-05-26 19:20:15 -0700175 return 0;
176}
177
178/* Free RBDR ring and its receive buffers */
179static void nicvf_free_rbdr(struct nicvf *nic, struct rbdr *rbdr)
180{
181 int head, tail;
182 u64 buf_addr;
183 struct rbdr_entry_t *desc;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700184
185 if (!rbdr)
186 return;
187
188 rbdr->enable = false;
189 if (!rbdr->dmem.base)
190 return;
191
192 head = rbdr->head;
193 tail = rbdr->tail;
194
195 /* Free SKBs */
196 while (head != tail) {
197 desc = GET_RBDR_DESC(rbdr, head);
198 buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
Sunil Goutham668dda02015-12-07 10:30:33 +0530199 put_page(virt_to_page(phys_to_virt(buf_addr)));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700200 head++;
201 head &= (rbdr->dmem.q_len - 1);
202 }
203 /* Free SKB of tail desc */
204 desc = GET_RBDR_DESC(rbdr, tail);
205 buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
Sunil Goutham668dda02015-12-07 10:30:33 +0530206 put_page(virt_to_page(phys_to_virt(buf_addr)));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700207
208 /* Free RBDR ring */
209 nicvf_free_q_desc_mem(nic, &rbdr->dmem);
210}
211
212/* Refill receive buffer descriptors with new buffers.
213 */
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700214static void nicvf_refill_rbdr(struct nicvf *nic, gfp_t gfp)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700215{
216 struct queue_set *qs = nic->qs;
217 int rbdr_idx = qs->rbdr_cnt;
218 int tail, qcount;
219 int refill_rb_cnt;
220 struct rbdr *rbdr;
221 struct rbdr_entry_t *desc;
222 u64 *rbuf;
223 int new_rb = 0;
224
225refill:
226 if (!rbdr_idx)
227 return;
228 rbdr_idx--;
229 rbdr = &qs->rbdr[rbdr_idx];
230 /* Check if it's enabled */
231 if (!rbdr->enable)
232 goto next_rbdr;
233
234 /* Get no of desc's to be refilled */
235 qcount = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, rbdr_idx);
236 qcount &= 0x7FFFF;
237 /* Doorbell can be ringed with a max of ring size minus 1 */
238 if (qcount >= (qs->rbdr_len - 1))
239 goto next_rbdr;
240 else
241 refill_rb_cnt = qs->rbdr_len - qcount - 1;
242
243 /* Start filling descs from tail */
244 tail = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, rbdr_idx) >> 3;
245 while (refill_rb_cnt) {
246 tail++;
247 tail &= (rbdr->dmem.q_len - 1);
248
249 if (nicvf_alloc_rcv_buffer(nic, gfp, RCV_FRAG_LEN, &rbuf))
250 break;
251
252 desc = GET_RBDR_DESC(rbdr, tail);
253 desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
254 refill_rb_cnt--;
255 new_rb++;
256 }
257
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530258 nicvf_get_page(nic);
259
Sunil Goutham4863dea2015-05-26 19:20:15 -0700260 /* make sure all memory stores are done before ringing doorbell */
261 smp_wmb();
262
263 /* Check if buffer allocation failed */
264 if (refill_rb_cnt)
265 nic->rb_alloc_fail = true;
266 else
267 nic->rb_alloc_fail = false;
268
269 /* Notify HW */
270 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
271 rbdr_idx, new_rb);
272next_rbdr:
273 /* Re-enable RBDR interrupts only if buffer allocation is success */
274 if (!nic->rb_alloc_fail && rbdr->enable)
275 nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
276
277 if (rbdr_idx)
278 goto refill;
279}
280
281/* Alloc rcv buffers in non-atomic mode for better success */
282void nicvf_rbdr_work(struct work_struct *work)
283{
284 struct nicvf *nic = container_of(work, struct nicvf, rbdr_work.work);
285
286 nicvf_refill_rbdr(nic, GFP_KERNEL);
287 if (nic->rb_alloc_fail)
288 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
289 else
290 nic->rb_work_scheduled = false;
291}
292
293/* In Softirq context, alloc rcv buffers in atomic mode */
294void nicvf_rbdr_task(unsigned long data)
295{
296 struct nicvf *nic = (struct nicvf *)data;
297
298 nicvf_refill_rbdr(nic, GFP_ATOMIC);
299 if (nic->rb_alloc_fail) {
300 nic->rb_work_scheduled = true;
301 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
302 }
303}
304
305/* Initialize completion queue */
306static int nicvf_init_cmp_queue(struct nicvf *nic,
307 struct cmp_queue *cq, int q_len)
308{
309 int err;
310
311 err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
312 NICVF_CQ_BASE_ALIGN_BYTES);
313 if (err)
314 return err;
315
316 cq->desc = cq->dmem.base;
Sunil Gouthamb9687b42015-12-10 13:25:20 +0530317 cq->thresh = pass1_silicon(nic->pdev) ? 0 : CMP_QUEUE_CQE_THRESH;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700318 nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
319
320 return 0;
321}
322
323static void nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
324{
325 if (!cq)
326 return;
327 if (!cq->dmem.base)
328 return;
329
330 nicvf_free_q_desc_mem(nic, &cq->dmem);
331}
332
333/* Initialize transmit queue */
334static int nicvf_init_snd_queue(struct nicvf *nic,
335 struct snd_queue *sq, int q_len)
336{
337 int err;
338
339 err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
340 NICVF_SQ_BASE_ALIGN_BYTES);
341 if (err)
342 return err;
343
344 sq->desc = sq->dmem.base;
Aleksey Makarov86ace692015-06-02 11:00:27 -0700345 sq->skbuff = kcalloc(q_len, sizeof(u64), GFP_KERNEL);
Aleksey Makarovfa1a6c92015-06-02 11:00:26 -0700346 if (!sq->skbuff)
347 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700348 sq->head = 0;
349 sq->tail = 0;
350 atomic_set(&sq->free_cnt, q_len - 1);
351 sq->thresh = SND_QUEUE_THRESH;
352
353 /* Preallocate memory for TSO segment's header */
354 sq->tso_hdrs = dma_alloc_coherent(&nic->pdev->dev,
355 q_len * TSO_HEADER_SIZE,
356 &sq->tso_hdrs_phys, GFP_KERNEL);
357 if (!sq->tso_hdrs)
358 return -ENOMEM;
359
360 return 0;
361}
362
363static void nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
364{
365 if (!sq)
366 return;
367 if (!sq->dmem.base)
368 return;
369
370 if (sq->tso_hdrs)
Sunil Goutham143ceb02015-07-29 16:49:37 +0300371 dma_free_coherent(&nic->pdev->dev,
372 sq->dmem.q_len * TSO_HEADER_SIZE,
Sunil Goutham4863dea2015-05-26 19:20:15 -0700373 sq->tso_hdrs, sq->tso_hdrs_phys);
374
375 kfree(sq->skbuff);
376 nicvf_free_q_desc_mem(nic, &sq->dmem);
377}
378
379static void nicvf_reclaim_snd_queue(struct nicvf *nic,
380 struct queue_set *qs, int qidx)
381{
382 /* Disable send queue */
383 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
384 /* Check if SQ is stopped */
385 if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
386 return;
387 /* Reset send queue */
388 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
389}
390
391static void nicvf_reclaim_rcv_queue(struct nicvf *nic,
392 struct queue_set *qs, int qidx)
393{
394 union nic_mbx mbx = {};
395
396 /* Make sure all packets in the pipeline are written back into mem */
397 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
398 nicvf_send_msg_to_pf(nic, &mbx);
399}
400
401static void nicvf_reclaim_cmp_queue(struct nicvf *nic,
402 struct queue_set *qs, int qidx)
403{
404 /* Disable timer threshold (doesn't get reset upon CQ reset */
405 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
406 /* Disable completion queue */
407 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
408 /* Reset completion queue */
409 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
410}
411
412static void nicvf_reclaim_rbdr(struct nicvf *nic,
413 struct rbdr *rbdr, int qidx)
414{
415 u64 tmp, fifo_state;
416 int timeout = 10;
417
418 /* Save head and tail pointers for feeing up buffers */
419 rbdr->head = nicvf_queue_reg_read(nic,
420 NIC_QSET_RBDR_0_1_HEAD,
421 qidx) >> 3;
422 rbdr->tail = nicvf_queue_reg_read(nic,
423 NIC_QSET_RBDR_0_1_TAIL,
424 qidx) >> 3;
425
426 /* If RBDR FIFO is in 'FAIL' state then do a reset first
427 * before relaiming.
428 */
429 fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
430 if (((fifo_state >> 62) & 0x03) == 0x3)
431 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
432 qidx, NICVF_RBDR_RESET);
433
434 /* Disable RBDR */
435 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
436 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
437 return;
438 while (1) {
439 tmp = nicvf_queue_reg_read(nic,
440 NIC_QSET_RBDR_0_1_PREFETCH_STATUS,
441 qidx);
442 if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
443 break;
444 usleep_range(1000, 2000);
445 timeout--;
446 if (!timeout) {
447 netdev_err(nic->netdev,
448 "Failed polling on prefetch status\n");
449 return;
450 }
451 }
452 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
453 qidx, NICVF_RBDR_RESET);
454
455 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
456 return;
457 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
458 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
459 return;
460}
461
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300462void nicvf_config_vlan_stripping(struct nicvf *nic, netdev_features_t features)
463{
464 u64 rq_cfg;
465 int sqs;
466
467 rq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_RQ_GEN_CFG, 0);
468
469 /* Enable first VLAN stripping */
470 if (features & NETIF_F_HW_VLAN_CTAG_RX)
471 rq_cfg |= (1ULL << 25);
472 else
473 rq_cfg &= ~(1ULL << 25);
474 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
475
476 /* Configure Secondary Qsets, if any */
477 for (sqs = 0; sqs < nic->sqs_count; sqs++)
478 if (nic->snicvf[sqs])
479 nicvf_queue_reg_write(nic->snicvf[sqs],
480 NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
481}
482
Jerin Jacob3458c402016-08-12 16:51:39 +0530483static void nicvf_reset_rcv_queue_stats(struct nicvf *nic)
484{
485 union nic_mbx mbx = {};
486
Sunil Goutham964cb692016-11-15 17:38:16 +0530487 /* Reset all RQ/SQ and VF stats */
Jerin Jacob3458c402016-08-12 16:51:39 +0530488 mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
Sunil Goutham964cb692016-11-15 17:38:16 +0530489 mbx.reset_stat.rx_stat_mask = 0x3FFF;
490 mbx.reset_stat.tx_stat_mask = 0x1F;
Jerin Jacob3458c402016-08-12 16:51:39 +0530491 mbx.reset_stat.rq_stat_mask = 0xFFFF;
Sunil Goutham964cb692016-11-15 17:38:16 +0530492 mbx.reset_stat.sq_stat_mask = 0xFFFF;
Jerin Jacob3458c402016-08-12 16:51:39 +0530493 nicvf_send_msg_to_pf(nic, &mbx);
494}
495
Sunil Goutham4863dea2015-05-26 19:20:15 -0700496/* Configures receive queue */
497static void nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
498 int qidx, bool enable)
499{
500 union nic_mbx mbx = {};
501 struct rcv_queue *rq;
502 struct rq_cfg rq_cfg;
503
504 rq = &qs->rq[qidx];
505 rq->enable = enable;
506
507 /* Disable receive queue */
508 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
509
510 if (!rq->enable) {
511 nicvf_reclaim_rcv_queue(nic, qs, qidx);
512 return;
513 }
514
515 rq->cq_qs = qs->vnic_id;
516 rq->cq_idx = qidx;
517 rq->start_rbdr_qs = qs->vnic_id;
518 rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
519 rq->cont_rbdr_qs = qs->vnic_id;
520 rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
521 /* all writes of RBDR data to be loaded into L2 Cache as well*/
522 rq->caching = 1;
523
524 /* Send a mailbox msg to PF to config RQ */
525 mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
526 mbx.rq.qs_num = qs->vnic_id;
527 mbx.rq.rq_num = qidx;
528 mbx.rq.cfg = (rq->caching << 26) | (rq->cq_qs << 19) |
529 (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
530 (rq->cont_qs_rbdr_idx << 8) |
531 (rq->start_rbdr_qs << 1) | (rq->start_qs_rbdr_idx);
532 nicvf_send_msg_to_pf(nic, &mbx);
533
534 mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
535 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (qs->vnic_id << 0);
536 nicvf_send_msg_to_pf(nic, &mbx);
537
538 /* RQ drop config
539 * Enable CQ drop to reserve sufficient CQEs for all tx packets
540 */
541 mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
542 mbx.rq.cfg = (1ULL << 62) | (RQ_CQ_DROP << 8);
543 nicvf_send_msg_to_pf(nic, &mbx);
544
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530545 if (!nic->sqs_mode && (qidx == 0)) {
546 /* Enable checking L3/L4 length and TCP/UDP checksums */
547 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0,
548 (BIT(24) | BIT(23) | BIT(21)));
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300549 nicvf_config_vlan_stripping(nic, nic->netdev->features);
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530550 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700551
552 /* Enable Receive queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200553 memset(&rq_cfg, 0, sizeof(struct rq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700554 rq_cfg.ena = 1;
555 rq_cfg.tcp_ena = 0;
556 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
557}
558
559/* Configures completion queue */
560void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
561 int qidx, bool enable)
562{
563 struct cmp_queue *cq;
564 struct cq_cfg cq_cfg;
565
566 cq = &qs->cq[qidx];
567 cq->enable = enable;
568
569 if (!cq->enable) {
570 nicvf_reclaim_cmp_queue(nic, qs, qidx);
571 return;
572 }
573
574 /* Reset completion queue */
575 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
576
577 if (!cq->enable)
578 return;
579
580 spin_lock_init(&cq->lock);
581 /* Set completion queue base address */
582 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
583 qidx, (u64)(cq->dmem.phys_base));
584
585 /* Enable Completion queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200586 memset(&cq_cfg, 0, sizeof(struct cq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700587 cq_cfg.ena = 1;
588 cq_cfg.reset = 0;
589 cq_cfg.caching = 0;
590 cq_cfg.qsize = CMP_QSIZE;
591 cq_cfg.avg_con = 0;
592 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
593
594 /* Set threshold value for interrupt generation */
595 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
596 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
Sunil Goutham006394a2015-12-02 15:36:15 +0530597 qidx, CMP_QUEUE_TIMER_THRESH);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700598}
599
600/* Configures transmit queue */
601static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
602 int qidx, bool enable)
603{
604 union nic_mbx mbx = {};
605 struct snd_queue *sq;
606 struct sq_cfg sq_cfg;
607
608 sq = &qs->sq[qidx];
609 sq->enable = enable;
610
611 if (!sq->enable) {
612 nicvf_reclaim_snd_queue(nic, qs, qidx);
613 return;
614 }
615
616 /* Reset send queue */
617 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
618
619 sq->cq_qs = qs->vnic_id;
620 sq->cq_idx = qidx;
621
622 /* Send a mailbox msg to PF to config SQ */
623 mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
624 mbx.sq.qs_num = qs->vnic_id;
625 mbx.sq.sq_num = qidx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300626 mbx.sq.sqs_mode = nic->sqs_mode;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700627 mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
628 nicvf_send_msg_to_pf(nic, &mbx);
629
630 /* Set queue base address */
631 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
632 qidx, (u64)(sq->dmem.phys_base));
633
634 /* Enable send queue & set queue size */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200635 memset(&sq_cfg, 0, sizeof(struct sq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700636 sq_cfg.ena = 1;
637 sq_cfg.reset = 0;
638 sq_cfg.ldwb = 0;
639 sq_cfg.qsize = SND_QSIZE;
640 sq_cfg.tstmp_bgx_intf = 0;
641 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
642
643 /* Set threshold value for interrupt generation */
644 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
645
646 /* Set queue:cpu affinity for better load distribution */
647 if (cpu_online(qidx)) {
648 cpumask_set_cpu(qidx, &sq->affinity_mask);
649 netif_set_xps_queue(nic->netdev,
650 &sq->affinity_mask, qidx);
651 }
652}
653
654/* Configures receive buffer descriptor ring */
655static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
656 int qidx, bool enable)
657{
658 struct rbdr *rbdr;
659 struct rbdr_cfg rbdr_cfg;
660
661 rbdr = &qs->rbdr[qidx];
662 nicvf_reclaim_rbdr(nic, rbdr, qidx);
663 if (!enable)
664 return;
665
666 /* Set descriptor base address */
667 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
668 qidx, (u64)(rbdr->dmem.phys_base));
669
670 /* Enable RBDR & set queue size */
671 /* Buffer size should be in multiples of 128 bytes */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200672 memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700673 rbdr_cfg.ena = 1;
674 rbdr_cfg.reset = 0;
675 rbdr_cfg.ldwb = 0;
676 rbdr_cfg.qsize = RBDR_SIZE;
677 rbdr_cfg.avg_con = 0;
678 rbdr_cfg.lines = rbdr->dma_size / 128;
679 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
680 qidx, *(u64 *)&rbdr_cfg);
681
682 /* Notify HW */
683 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
684 qidx, qs->rbdr_len - 1);
685
686 /* Set threshold value for interrupt generation */
687 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
688 qidx, rbdr->thresh - 1);
689}
690
691/* Requests PF to assign and enable Qset */
692void nicvf_qset_config(struct nicvf *nic, bool enable)
693{
694 union nic_mbx mbx = {};
695 struct queue_set *qs = nic->qs;
696 struct qs_cfg *qs_cfg;
697
698 if (!qs) {
699 netdev_warn(nic->netdev,
700 "Qset is still not allocated, don't init queues\n");
701 return;
702 }
703
704 qs->enable = enable;
705 qs->vnic_id = nic->vf_id;
706
707 /* Send a mailbox msg to PF to config Qset */
708 mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
709 mbx.qs.num = qs->vnic_id;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300710 mbx.qs.sqs_count = nic->sqs_count;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700711
712 mbx.qs.cfg = 0;
713 qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
714 if (qs->enable) {
715 qs_cfg->ena = 1;
716#ifdef __BIG_ENDIAN
717 qs_cfg->be = 1;
718#endif
719 qs_cfg->vnic = qs->vnic_id;
720 }
721 nicvf_send_msg_to_pf(nic, &mbx);
722}
723
724static void nicvf_free_resources(struct nicvf *nic)
725{
726 int qidx;
727 struct queue_set *qs = nic->qs;
728
729 /* Free receive buffer descriptor ring */
730 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
731 nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
732
733 /* Free completion queue */
734 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
735 nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
736
737 /* Free send queue */
738 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
739 nicvf_free_snd_queue(nic, &qs->sq[qidx]);
740}
741
742static int nicvf_alloc_resources(struct nicvf *nic)
743{
744 int qidx;
745 struct queue_set *qs = nic->qs;
746
747 /* Alloc receive buffer descriptor ring */
748 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
749 if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
750 DMA_BUFFER_LEN))
751 goto alloc_fail;
752 }
753
754 /* Alloc send queue */
755 for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
756 if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
757 goto alloc_fail;
758 }
759
760 /* Alloc completion queue */
761 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
762 if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
763 goto alloc_fail;
764 }
765
766 return 0;
767alloc_fail:
768 nicvf_free_resources(nic);
769 return -ENOMEM;
770}
771
772int nicvf_set_qset_resources(struct nicvf *nic)
773{
774 struct queue_set *qs;
775
776 qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
777 if (!qs)
778 return -ENOMEM;
779 nic->qs = qs;
780
781 /* Set count of each queue */
Sunil Goutham3a397eb2016-08-12 16:51:27 +0530782 qs->rbdr_cnt = DEFAULT_RBDR_CNT;
783 qs->rq_cnt = min_t(u8, MAX_RCV_QUEUES_PER_QS, num_online_cpus());
784 qs->sq_cnt = min_t(u8, MAX_SND_QUEUES_PER_QS, num_online_cpus());
785 qs->cq_cnt = max_t(u8, qs->rq_cnt, qs->sq_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700786
787 /* Set queue lengths */
788 qs->rbdr_len = RCV_BUF_COUNT;
789 qs->sq_len = SND_QUEUE_LEN;
790 qs->cq_len = CMP_QUEUE_LEN;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300791
792 nic->rx_queues = qs->rq_cnt;
793 nic->tx_queues = qs->sq_cnt;
794
Sunil Goutham4863dea2015-05-26 19:20:15 -0700795 return 0;
796}
797
798int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
799{
800 bool disable = false;
801 struct queue_set *qs = nic->qs;
802 int qidx;
803
804 if (!qs)
805 return 0;
806
807 if (enable) {
808 if (nicvf_alloc_resources(nic))
809 return -ENOMEM;
810
811 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
812 nicvf_snd_queue_config(nic, qs, qidx, enable);
813 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
814 nicvf_cmp_queue_config(nic, qs, qidx, enable);
815 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
816 nicvf_rbdr_config(nic, qs, qidx, enable);
817 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
818 nicvf_rcv_queue_config(nic, qs, qidx, enable);
819 } else {
820 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
821 nicvf_rcv_queue_config(nic, qs, qidx, disable);
822 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
823 nicvf_rbdr_config(nic, qs, qidx, disable);
824 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
825 nicvf_snd_queue_config(nic, qs, qidx, disable);
826 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
827 nicvf_cmp_queue_config(nic, qs, qidx, disable);
828
829 nicvf_free_resources(nic);
830 }
831
Jerin Jacob3458c402016-08-12 16:51:39 +0530832 /* Reset RXQ's stats.
833 * SQ's stats will get reset automatically once SQ is reset.
834 */
835 nicvf_reset_rcv_queue_stats(nic);
836
Sunil Goutham4863dea2015-05-26 19:20:15 -0700837 return 0;
838}
839
840/* Get a free desc from SQ
841 * returns descriptor ponter & descriptor number
842 */
843static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
844{
845 int qentry;
846
847 qentry = sq->tail;
848 atomic_sub(desc_cnt, &sq->free_cnt);
849 sq->tail += desc_cnt;
850 sq->tail &= (sq->dmem.q_len - 1);
851
852 return qentry;
853}
854
855/* Free descriptor back to SQ for future use */
856void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
857{
858 atomic_add(desc_cnt, &sq->free_cnt);
859 sq->head += desc_cnt;
860 sq->head &= (sq->dmem.q_len - 1);
861}
862
863static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
864{
865 qentry++;
866 qentry &= (sq->dmem.q_len - 1);
867 return qentry;
868}
869
870void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
871{
872 u64 sq_cfg;
873
874 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
875 sq_cfg |= NICVF_SQ_EN;
876 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
877 /* Ring doorbell so that H/W restarts processing SQEs */
878 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
879}
880
881void nicvf_sq_disable(struct nicvf *nic, int qidx)
882{
883 u64 sq_cfg;
884
885 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
886 sq_cfg &= ~NICVF_SQ_EN;
887 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
888}
889
890void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
891 int qidx)
892{
893 u64 head, tail;
894 struct sk_buff *skb;
895 struct nicvf *nic = netdev_priv(netdev);
896 struct sq_hdr_subdesc *hdr;
897
898 head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
899 tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
900 while (sq->head != head) {
901 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
902 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
903 nicvf_put_sq_desc(sq, 1);
904 continue;
905 }
906 skb = (struct sk_buff *)sq->skbuff[sq->head];
Sunil Goutham143ceb02015-07-29 16:49:37 +0300907 if (skb)
908 dev_kfree_skb_any(skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700909 atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
910 atomic64_add(hdr->tot_len,
911 (atomic64_t *)&netdev->stats.tx_bytes);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700912 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
913 }
914}
915
916/* Calculate no of SQ subdescriptors needed to transmit all
917 * segments of this TSO packet.
918 * Taken from 'Tilera network driver' with a minor modification.
919 */
920static int nicvf_tso_count_subdescs(struct sk_buff *skb)
921{
922 struct skb_shared_info *sh = skb_shinfo(skb);
923 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
924 unsigned int data_len = skb->len - sh_len;
925 unsigned int p_len = sh->gso_size;
926 long f_id = -1; /* id of the current fragment */
927 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
928 long f_used = 0; /* bytes used from the current fragment */
929 long n; /* size of the current piece of payload */
930 int num_edescs = 0;
931 int segment;
932
933 for (segment = 0; segment < sh->gso_segs; segment++) {
934 unsigned int p_used = 0;
935
936 /* One edesc for header and for each piece of the payload. */
937 for (num_edescs++; p_used < p_len; num_edescs++) {
938 /* Advance as needed. */
939 while (f_used >= f_size) {
940 f_id++;
941 f_size = skb_frag_size(&sh->frags[f_id]);
942 f_used = 0;
943 }
944
945 /* Use bytes from the current fragment. */
946 n = p_len - p_used;
947 if (n > f_size - f_used)
948 n = f_size - f_used;
949 f_used += n;
950 p_used += n;
951 }
952
953 /* The last segment may be less than gso_size. */
954 data_len -= p_len;
955 if (data_len < p_len)
956 p_len = data_len;
957 }
958
959 /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
960 return num_edescs + sh->gso_segs;
961}
962
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530963#define POST_CQE_DESC_COUNT 2
964
Sunil Goutham4863dea2015-05-26 19:20:15 -0700965/* Get the number of SQ descriptors needed to xmit this skb */
966static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
967{
968 int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
969
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530970 if (skb_shinfo(skb)->gso_size && !nic->hw_tso) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700971 subdesc_cnt = nicvf_tso_count_subdescs(skb);
972 return subdesc_cnt;
973 }
974
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530975 /* Dummy descriptors to get TSO pkt completion notification */
976 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size)
977 subdesc_cnt += POST_CQE_DESC_COUNT;
978
Sunil Goutham4863dea2015-05-26 19:20:15 -0700979 if (skb_shinfo(skb)->nr_frags)
980 subdesc_cnt += skb_shinfo(skb)->nr_frags;
981
982 return subdesc_cnt;
983}
984
985/* Add SQ HEADER subdescriptor.
986 * First subdescriptor for every send descriptor.
987 */
988static inline void
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530989nicvf_sq_add_hdr_subdesc(struct nicvf *nic, struct snd_queue *sq, int qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -0700990 int subdesc_cnt, struct sk_buff *skb, int len)
991{
992 int proto;
993 struct sq_hdr_subdesc *hdr;
994
995 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700996 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
997 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530998
999 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size) {
1000 /* post_cqe = 0, to avoid HW posting a CQE for every TSO
1001 * segment transmitted on 88xx.
1002 */
1003 hdr->subdesc_cnt = subdesc_cnt - POST_CQE_DESC_COUNT;
1004 } else {
1005 sq->skbuff[qentry] = (u64)skb;
1006 /* Enable notification via CQE after processing SQE */
1007 hdr->post_cqe = 1;
1008 /* No of subdescriptors following this */
1009 hdr->subdesc_cnt = subdesc_cnt;
1010 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001011 hdr->tot_len = len;
1012
1013 /* Offload checksum calculation to HW */
1014 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001015 hdr->csum_l3 = 1; /* Enable IP csum calculation */
1016 hdr->l3_offset = skb_network_offset(skb);
1017 hdr->l4_offset = skb_transport_offset(skb);
1018
1019 proto = ip_hdr(skb)->protocol;
1020 switch (proto) {
1021 case IPPROTO_TCP:
1022 hdr->csum_l4 = SEND_L4_CSUM_TCP;
1023 break;
1024 case IPPROTO_UDP:
1025 hdr->csum_l4 = SEND_L4_CSUM_UDP;
1026 break;
1027 case IPPROTO_SCTP:
1028 hdr->csum_l4 = SEND_L4_CSUM_SCTP;
1029 break;
1030 }
1031 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301032
1033 if (nic->hw_tso && skb_shinfo(skb)->gso_size) {
1034 hdr->tso = 1;
1035 hdr->tso_start = skb_transport_offset(skb) + tcp_hdrlen(skb);
1036 hdr->tso_max_paysize = skb_shinfo(skb)->gso_size;
1037 /* For non-tunneled pkts, point this to L2 ethertype */
1038 hdr->inner_l3_offset = skb_network_offset(skb) - 2;
Sunil Goutham964cb692016-11-15 17:38:16 +05301039 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301040 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001041}
1042
1043/* SQ GATHER subdescriptor
1044 * Must follow HDR descriptor
1045 */
1046static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
1047 int size, u64 data)
1048{
1049 struct sq_gather_subdesc *gather;
1050
1051 qentry &= (sq->dmem.q_len - 1);
1052 gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
1053
1054 memset(gather, 0, SND_QUEUE_DESC_SIZE);
1055 gather->subdesc_type = SQ_DESC_TYPE_GATHER;
Sunil Goutham4b561c12015-07-29 16:49:36 +03001056 gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001057 gather->size = size;
1058 gather->addr = data;
1059}
1060
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301061/* Add HDR + IMMEDIATE subdescriptors right after descriptors of a TSO
1062 * packet so that a CQE is posted as a notifation for transmission of
1063 * TSO packet.
1064 */
1065static inline void nicvf_sq_add_cqe_subdesc(struct snd_queue *sq, int qentry,
1066 int tso_sqe, struct sk_buff *skb)
1067{
1068 struct sq_imm_subdesc *imm;
1069 struct sq_hdr_subdesc *hdr;
1070
1071 sq->skbuff[qentry] = (u64)skb;
1072
1073 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
1074 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
1075 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
1076 /* Enable notification via CQE after processing SQE */
1077 hdr->post_cqe = 1;
1078 /* There is no packet to transmit here */
1079 hdr->dont_send = 1;
1080 hdr->subdesc_cnt = POST_CQE_DESC_COUNT - 1;
1081 hdr->tot_len = 1;
1082 /* Actual TSO header SQE index, needed for cleanup */
1083 hdr->rsvd2 = tso_sqe;
1084
1085 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1086 imm = (struct sq_imm_subdesc *)GET_SQ_DESC(sq, qentry);
1087 memset(imm, 0, SND_QUEUE_DESC_SIZE);
1088 imm->subdesc_type = SQ_DESC_TYPE_IMMEDIATE;
1089 imm->len = 1;
1090}
1091
Sunil Goutham2c204c22016-09-23 14:42:28 +05301092static inline void nicvf_sq_doorbell(struct nicvf *nic, struct sk_buff *skb,
1093 int sq_num, int desc_cnt)
1094{
1095 struct netdev_queue *txq;
1096
1097 txq = netdev_get_tx_queue(nic->pnicvf->netdev,
1098 skb_get_queue_mapping(skb));
1099
1100 netdev_tx_sent_queue(txq, skb->len);
1101
1102 /* make sure all memory stores are done before ringing doorbell */
1103 smp_wmb();
1104
1105 /* Inform HW to xmit all TSO segments */
1106 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
1107 sq_num, desc_cnt);
1108}
1109
Sunil Goutham4863dea2015-05-26 19:20:15 -07001110/* Segment a TSO packet into 'gso_size' segments and append
1111 * them to SQ for transfer
1112 */
1113static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
Sunil Goutham92dc8762015-08-30 12:29:15 +03001114 int sq_num, int qentry, struct sk_buff *skb)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001115{
1116 struct tso_t tso;
1117 int seg_subdescs = 0, desc_cnt = 0;
1118 int seg_len, total_len, data_left;
1119 int hdr_qentry = qentry;
1120 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1121
1122 tso_start(skb, &tso);
1123 total_len = skb->len - hdr_len;
1124 while (total_len > 0) {
1125 char *hdr;
1126
1127 /* Save Qentry for adding HDR_SUBDESC at the end */
1128 hdr_qentry = qentry;
1129
1130 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1131 total_len -= data_left;
1132
1133 /* Add segment's header */
1134 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1135 hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
1136 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1137 nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
1138 sq->tso_hdrs_phys +
1139 qentry * TSO_HEADER_SIZE);
1140 /* HDR_SUDESC + GATHER */
1141 seg_subdescs = 2;
1142 seg_len = hdr_len;
1143
1144 /* Add segment's payload fragments */
1145 while (data_left > 0) {
1146 int size;
1147
1148 size = min_t(int, tso.size, data_left);
1149
1150 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1151 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1152 virt_to_phys(tso.data));
1153 seg_subdescs++;
1154 seg_len += size;
1155
1156 data_left -= size;
1157 tso_build_data(skb, &tso, size);
1158 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301159 nicvf_sq_add_hdr_subdesc(nic, sq, hdr_qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001160 seg_subdescs - 1, skb, seg_len);
Sunil Goutham143ceb02015-07-29 16:49:37 +03001161 sq->skbuff[hdr_qentry] = (u64)NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001162 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1163
1164 desc_cnt += seg_subdescs;
1165 }
1166 /* Save SKB in the last segment for freeing */
1167 sq->skbuff[hdr_qentry] = (u64)skb;
1168
Sunil Goutham2c204c22016-09-23 14:42:28 +05301169 nicvf_sq_doorbell(nic, skb, sq_num, desc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001170
Sunil Goutham964cb692016-11-15 17:38:16 +05301171 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001172 return 1;
1173}
1174
1175/* Append an skb to a SQ for packet transfer. */
1176int nicvf_sq_append_skb(struct nicvf *nic, struct sk_buff *skb)
1177{
1178 int i, size;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301179 int subdesc_cnt, tso_sqe = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001180 int sq_num, qentry;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001181 struct queue_set *qs;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001182 struct snd_queue *sq;
1183
1184 sq_num = skb_get_queue_mapping(skb);
Sunil Goutham92dc8762015-08-30 12:29:15 +03001185 if (sq_num >= MAX_SND_QUEUES_PER_QS) {
1186 /* Get secondary Qset's SQ structure */
1187 i = sq_num / MAX_SND_QUEUES_PER_QS;
1188 if (!nic->snicvf[i - 1]) {
1189 netdev_warn(nic->netdev,
1190 "Secondary Qset#%d's ptr not initialized\n",
1191 i - 1);
1192 return 1;
1193 }
1194 nic = (struct nicvf *)nic->snicvf[i - 1];
1195 sq_num = sq_num % MAX_SND_QUEUES_PER_QS;
1196 }
1197
1198 qs = nic->qs;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001199 sq = &qs->sq[sq_num];
1200
1201 subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
1202 if (subdesc_cnt > atomic_read(&sq->free_cnt))
1203 goto append_fail;
1204
1205 qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
1206
1207 /* Check if its a TSO packet */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301208 if (skb_shinfo(skb)->gso_size && !nic->hw_tso)
Sunil Goutham92dc8762015-08-30 12:29:15 +03001209 return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001210
1211 /* Add SQ header subdesc */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301212 nicvf_sq_add_hdr_subdesc(nic, sq, qentry, subdesc_cnt - 1,
1213 skb, skb->len);
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301214 tso_sqe = qentry;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001215
1216 /* Add SQ gather subdescs */
1217 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1218 size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
1219 nicvf_sq_add_gather_subdesc(sq, qentry, size, virt_to_phys(skb->data));
1220
1221 /* Check for scattered buffer */
1222 if (!skb_is_nonlinear(skb))
1223 goto doorbell;
1224
1225 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1226 const struct skb_frag_struct *frag;
1227
1228 frag = &skb_shinfo(skb)->frags[i];
1229
1230 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1231 size = skb_frag_size(frag);
1232 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1233 virt_to_phys(
1234 skb_frag_address(frag)));
1235 }
1236
1237doorbell:
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301238 if (nic->t88 && skb_shinfo(skb)->gso_size) {
1239 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1240 nicvf_sq_add_cqe_subdesc(sq, qentry, tso_sqe, skb);
1241 }
1242
Sunil Goutham2c204c22016-09-23 14:42:28 +05301243 nicvf_sq_doorbell(nic, skb, sq_num, subdesc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001244
Sunil Goutham4863dea2015-05-26 19:20:15 -07001245 return 1;
1246
1247append_fail:
Sunil Goutham92dc8762015-08-30 12:29:15 +03001248 /* Use original PCI dev for debug log */
1249 nic = nic->pnicvf;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001250 netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
1251 return 0;
1252}
1253
1254static inline unsigned frag_num(unsigned i)
1255{
1256#ifdef __BIG_ENDIAN
1257 return (i & ~3) + 3 - (i & 3);
1258#else
1259 return i;
1260#endif
1261}
1262
1263/* Returns SKB for a received packet */
1264struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
1265{
1266 int frag;
1267 int payload_len = 0;
1268 struct sk_buff *skb = NULL;
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301269 struct page *page;
1270 int offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001271 u16 *rb_lens = NULL;
1272 u64 *rb_ptrs = NULL;
1273
1274 rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
Sunil Goutham02a72bd2016-08-12 16:51:28 +05301275 /* Except 88xx pass1 on all other chips CQE_RX2_S is added to
1276 * CQE_RX at word6, hence buffer pointers move by word
1277 *
1278 * Use existing 'hw_tso' flag which will be set for all chips
1279 * except 88xx pass1 instead of a additional cache line
1280 * access (or miss) by using pci dev's revision.
1281 */
1282 if (!nic->hw_tso)
1283 rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
1284 else
1285 rb_ptrs = (void *)cqe_rx + (7 * sizeof(u64));
Sunil Goutham4863dea2015-05-26 19:20:15 -07001286
1287 netdev_dbg(nic->netdev, "%s rb_cnt %d rb0_ptr %llx rb0_sz %d\n",
1288 __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
1289
1290 for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
1291 payload_len = rb_lens[frag_num(frag)];
1292 if (!frag) {
1293 /* First fragment */
1294 skb = nicvf_rb_ptr_to_skb(nic,
1295 *rb_ptrs - cqe_rx->align_pad,
1296 payload_len);
1297 if (!skb)
1298 return NULL;
1299 skb_reserve(skb, cqe_rx->align_pad);
1300 skb_put(skb, payload_len);
1301 } else {
1302 /* Add fragments */
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301303 page = virt_to_page(phys_to_virt(*rb_ptrs));
1304 offset = phys_to_virt(*rb_ptrs) - page_address(page);
1305 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
1306 offset, payload_len, RCV_FRAG_LEN);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001307 }
1308 /* Next buffer pointer */
1309 rb_ptrs++;
1310 }
1311 return skb;
1312}
1313
Yury Norovb45ceb42015-12-07 10:30:32 +05301314static u64 nicvf_int_type_to_mask(int int_type, int q_idx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001315{
1316 u64 reg_val;
1317
Sunil Goutham4863dea2015-05-26 19:20:15 -07001318 switch (int_type) {
1319 case NICVF_INTR_CQ:
1320 reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1321 break;
1322 case NICVF_INTR_SQ:
1323 reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1324 break;
1325 case NICVF_INTR_RBDR:
1326 reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1327 break;
1328 case NICVF_INTR_PKT_DROP:
1329 reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1330 break;
1331 case NICVF_INTR_TCP_TIMER:
1332 reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1333 break;
1334 case NICVF_INTR_MBOX:
1335 reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
1336 break;
1337 case NICVF_INTR_QS_ERR:
Yury Norovb45ceb42015-12-07 10:30:32 +05301338 reg_val = (1ULL << NICVF_INTR_QS_ERR_SHIFT);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001339 break;
1340 default:
Yury Norovb45ceb42015-12-07 10:30:32 +05301341 reg_val = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001342 }
1343
Yury Norovb45ceb42015-12-07 10:30:32 +05301344 return reg_val;
1345}
1346
1347/* Enable interrupt */
1348void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
1349{
1350 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1351
1352 if (!mask) {
1353 netdev_dbg(nic->netdev,
1354 "Failed to enable interrupt: unknown type\n");
1355 return;
1356 }
1357 nicvf_reg_write(nic, NIC_VF_ENA_W1S,
1358 nicvf_reg_read(nic, NIC_VF_ENA_W1S) | mask);
1359}
1360
1361/* Disable interrupt */
1362void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
1363{
1364 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1365
1366 if (!mask) {
1367 netdev_dbg(nic->netdev,
1368 "Failed to disable interrupt: unknown type\n");
1369 return;
1370 }
1371
1372 nicvf_reg_write(nic, NIC_VF_ENA_W1C, mask);
1373}
1374
1375/* Clear interrupt */
1376void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
1377{
1378 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1379
1380 if (!mask) {
1381 netdev_dbg(nic->netdev,
1382 "Failed to clear interrupt: unknown type\n");
1383 return;
1384 }
1385
1386 nicvf_reg_write(nic, NIC_VF_INT, mask);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001387}
1388
1389/* Check if interrupt is enabled */
1390int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
1391{
Yury Norovb45ceb42015-12-07 10:30:32 +05301392 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1393 /* If interrupt type is unknown, we treat it disabled. */
1394 if (!mask) {
1395 netdev_dbg(nic->netdev,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001396 "Failed to check interrupt enable: unknown type\n");
Yury Norovb45ceb42015-12-07 10:30:32 +05301397 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001398 }
1399
Yury Norovb45ceb42015-12-07 10:30:32 +05301400 return mask & nicvf_reg_read(nic, NIC_VF_ENA_W1S);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001401}
1402
1403void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
1404{
1405 struct rcv_queue *rq;
1406
1407#define GET_RQ_STATS(reg) \
1408 nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
1409 (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1410
1411 rq = &nic->qs->rq[rq_idx];
1412 rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
1413 rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
1414}
1415
1416void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
1417{
1418 struct snd_queue *sq;
1419
1420#define GET_SQ_STATS(reg) \
1421 nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
1422 (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1423
1424 sq = &nic->qs->sq[sq_idx];
1425 sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
1426 sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
1427}
1428
1429/* Check for errors in the receive cmp.queue entry */
Sunil Gouthamad2eceb2016-02-16 16:29:51 +05301430int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001431{
Sunil Gouthamad2eceb2016-02-16 16:29:51 +05301432 if (!cqe_rx->err_level && !cqe_rx->err_opcode)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001433 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001434
1435 if (netif_msg_rx_err(nic))
1436 netdev_err(nic->netdev,
1437 "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
1438 nic->netdev->name,
1439 cqe_rx->err_level, cqe_rx->err_opcode);
1440
Sunil Goutham4863dea2015-05-26 19:20:15 -07001441 switch (cqe_rx->err_opcode) {
1442 case CQ_RX_ERROP_RE_PARTIAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301443 this_cpu_inc(nic->drv_stats->rx_bgx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001444 break;
1445 case CQ_RX_ERROP_RE_JABBER:
Sunil Goutham964cb692016-11-15 17:38:16 +05301446 this_cpu_inc(nic->drv_stats->rx_jabber_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001447 break;
1448 case CQ_RX_ERROP_RE_FCS:
Sunil Goutham964cb692016-11-15 17:38:16 +05301449 this_cpu_inc(nic->drv_stats->rx_fcs_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001450 break;
1451 case CQ_RX_ERROP_RE_RX_CTL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301452 this_cpu_inc(nic->drv_stats->rx_bgx_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001453 break;
1454 case CQ_RX_ERROP_PREL2_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301455 this_cpu_inc(nic->drv_stats->rx_prel2_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001456 break;
1457 case CQ_RX_ERROP_L2_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301458 this_cpu_inc(nic->drv_stats->rx_l2_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001459 break;
1460 case CQ_RX_ERROP_L2_OVERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301461 this_cpu_inc(nic->drv_stats->rx_oversize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001462 break;
1463 case CQ_RX_ERROP_L2_UNDERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301464 this_cpu_inc(nic->drv_stats->rx_undersize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001465 break;
1466 case CQ_RX_ERROP_L2_LENMISM:
Sunil Goutham964cb692016-11-15 17:38:16 +05301467 this_cpu_inc(nic->drv_stats->rx_l2_len_mismatch);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001468 break;
1469 case CQ_RX_ERROP_L2_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301470 this_cpu_inc(nic->drv_stats->rx_l2_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001471 break;
1472 case CQ_RX_ERROP_IP_NOT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301473 this_cpu_inc(nic->drv_stats->rx_ip_ver_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001474 break;
1475 case CQ_RX_ERROP_IP_CSUM_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301476 this_cpu_inc(nic->drv_stats->rx_ip_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001477 break;
1478 case CQ_RX_ERROP_IP_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301479 this_cpu_inc(nic->drv_stats->rx_ip_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001480 break;
1481 case CQ_RX_ERROP_IP_MALD:
Sunil Goutham964cb692016-11-15 17:38:16 +05301482 this_cpu_inc(nic->drv_stats->rx_ip_payload_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001483 break;
1484 case CQ_RX_ERROP_IP_HOP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301485 this_cpu_inc(nic->drv_stats->rx_ip_ttl_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001486 break;
1487 case CQ_RX_ERROP_L3_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301488 this_cpu_inc(nic->drv_stats->rx_l3_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001489 break;
1490 case CQ_RX_ERROP_L4_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301491 this_cpu_inc(nic->drv_stats->rx_l4_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001492 break;
1493 case CQ_RX_ERROP_L4_CHK:
Sunil Goutham964cb692016-11-15 17:38:16 +05301494 this_cpu_inc(nic->drv_stats->rx_l4_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001495 break;
1496 case CQ_RX_ERROP_UDP_LEN:
Sunil Goutham964cb692016-11-15 17:38:16 +05301497 this_cpu_inc(nic->drv_stats->rx_udp_len_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001498 break;
1499 case CQ_RX_ERROP_L4_PORT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301500 this_cpu_inc(nic->drv_stats->rx_l4_port_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001501 break;
1502 case CQ_RX_ERROP_TCP_FLAG:
Sunil Goutham964cb692016-11-15 17:38:16 +05301503 this_cpu_inc(nic->drv_stats->rx_tcp_flag_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001504 break;
1505 case CQ_RX_ERROP_TCP_OFFSET:
Sunil Goutham964cb692016-11-15 17:38:16 +05301506 this_cpu_inc(nic->drv_stats->rx_tcp_offset_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001507 break;
1508 case CQ_RX_ERROP_L4_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301509 this_cpu_inc(nic->drv_stats->rx_l4_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001510 break;
1511 case CQ_RX_ERROP_RBDR_TRUNC:
Sunil Goutham964cb692016-11-15 17:38:16 +05301512 this_cpu_inc(nic->drv_stats->rx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001513 break;
1514 }
1515
1516 return 1;
1517}
1518
1519/* Check for errors in the send cmp.queue entry */
Sunil Goutham964cb692016-11-15 17:38:16 +05301520int nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cqe_send_t *cqe_tx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001521{
Sunil Goutham4863dea2015-05-26 19:20:15 -07001522 switch (cqe_tx->send_status) {
1523 case CQ_TX_ERROP_GOOD:
Sunil Goutham4863dea2015-05-26 19:20:15 -07001524 return 0;
1525 case CQ_TX_ERROP_DESC_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301526 this_cpu_inc(nic->drv_stats->tx_desc_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001527 break;
1528 case CQ_TX_ERROP_HDR_CONS_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301529 this_cpu_inc(nic->drv_stats->tx_hdr_cons_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001530 break;
1531 case CQ_TX_ERROP_SUBDC_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301532 this_cpu_inc(nic->drv_stats->tx_subdesc_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001533 break;
Sunil Goutham712c3182016-11-15 17:37:36 +05301534 case CQ_TX_ERROP_MAX_SIZE_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301535 this_cpu_inc(nic->drv_stats->tx_max_size_exceeded);
Sunil Goutham712c3182016-11-15 17:37:36 +05301536 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001537 case CQ_TX_ERROP_IMM_SIZE_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301538 this_cpu_inc(nic->drv_stats->tx_imm_size_oflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001539 break;
1540 case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301541 this_cpu_inc(nic->drv_stats->tx_data_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001542 break;
1543 case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301544 this_cpu_inc(nic->drv_stats->tx_mem_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001545 break;
1546 case CQ_TX_ERROP_LOCK_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301547 this_cpu_inc(nic->drv_stats->tx_lock_viol);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001548 break;
1549 case CQ_TX_ERROP_DATA_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301550 this_cpu_inc(nic->drv_stats->tx_data_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001551 break;
1552 case CQ_TX_ERROP_TSTMP_CONFLICT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301553 this_cpu_inc(nic->drv_stats->tx_tstmp_conflict);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001554 break;
1555 case CQ_TX_ERROP_TSTMP_TIMEOUT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301556 this_cpu_inc(nic->drv_stats->tx_tstmp_timeout);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001557 break;
1558 case CQ_TX_ERROP_MEM_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301559 this_cpu_inc(nic->drv_stats->tx_mem_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001560 break;
1561 case CQ_TX_ERROP_CK_OVERLAP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301562 this_cpu_inc(nic->drv_stats->tx_csum_overlap);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001563 break;
1564 case CQ_TX_ERROP_CK_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301565 this_cpu_inc(nic->drv_stats->tx_csum_overflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001566 break;
1567 }
1568
1569 return 1;
1570}