blob: 747ef08829763db89b8fb1802eab9b47a0eef6b0 [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 */
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530274 if (!nic->rb_alloc_fail && rbdr->enable &&
275 netif_running(nic->pnicvf->netdev))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700276 nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
277
278 if (rbdr_idx)
279 goto refill;
280}
281
282/* Alloc rcv buffers in non-atomic mode for better success */
283void nicvf_rbdr_work(struct work_struct *work)
284{
285 struct nicvf *nic = container_of(work, struct nicvf, rbdr_work.work);
286
287 nicvf_refill_rbdr(nic, GFP_KERNEL);
288 if (nic->rb_alloc_fail)
289 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
290 else
291 nic->rb_work_scheduled = false;
292}
293
294/* In Softirq context, alloc rcv buffers in atomic mode */
295void nicvf_rbdr_task(unsigned long data)
296{
297 struct nicvf *nic = (struct nicvf *)data;
298
299 nicvf_refill_rbdr(nic, GFP_ATOMIC);
300 if (nic->rb_alloc_fail) {
301 nic->rb_work_scheduled = true;
302 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
303 }
304}
305
306/* Initialize completion queue */
307static int nicvf_init_cmp_queue(struct nicvf *nic,
308 struct cmp_queue *cq, int q_len)
309{
310 int err;
311
312 err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
313 NICVF_CQ_BASE_ALIGN_BYTES);
314 if (err)
315 return err;
316
317 cq->desc = cq->dmem.base;
Sunil Gouthamb9687b42015-12-10 13:25:20 +0530318 cq->thresh = pass1_silicon(nic->pdev) ? 0 : CMP_QUEUE_CQE_THRESH;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700319 nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
320
321 return 0;
322}
323
324static void nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
325{
326 if (!cq)
327 return;
328 if (!cq->dmem.base)
329 return;
330
331 nicvf_free_q_desc_mem(nic, &cq->dmem);
332}
333
334/* Initialize transmit queue */
335static int nicvf_init_snd_queue(struct nicvf *nic,
336 struct snd_queue *sq, int q_len)
337{
338 int err;
339
340 err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
341 NICVF_SQ_BASE_ALIGN_BYTES);
342 if (err)
343 return err;
344
345 sq->desc = sq->dmem.base;
Aleksey Makarov86ace692015-06-02 11:00:27 -0700346 sq->skbuff = kcalloc(q_len, sizeof(u64), GFP_KERNEL);
Aleksey Makarovfa1a6c92015-06-02 11:00:26 -0700347 if (!sq->skbuff)
348 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700349 sq->head = 0;
350 sq->tail = 0;
351 atomic_set(&sq->free_cnt, q_len - 1);
352 sq->thresh = SND_QUEUE_THRESH;
353
354 /* Preallocate memory for TSO segment's header */
355 sq->tso_hdrs = dma_alloc_coherent(&nic->pdev->dev,
356 q_len * TSO_HEADER_SIZE,
357 &sq->tso_hdrs_phys, GFP_KERNEL);
358 if (!sq->tso_hdrs)
359 return -ENOMEM;
360
361 return 0;
362}
363
364static void nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
365{
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530366 struct sk_buff *skb;
367
Sunil Goutham4863dea2015-05-26 19:20:15 -0700368 if (!sq)
369 return;
370 if (!sq->dmem.base)
371 return;
372
373 if (sq->tso_hdrs)
Sunil Goutham143ceb02015-07-29 16:49:37 +0300374 dma_free_coherent(&nic->pdev->dev,
375 sq->dmem.q_len * TSO_HEADER_SIZE,
Sunil Goutham4863dea2015-05-26 19:20:15 -0700376 sq->tso_hdrs, sq->tso_hdrs_phys);
377
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530378 /* Free pending skbs in the queue */
379 smp_rmb();
380 while (sq->head != sq->tail) {
381 skb = (struct sk_buff *)sq->skbuff[sq->head];
382 if (skb)
383 dev_kfree_skb_any(skb);
384 sq->head++;
385 sq->head &= (sq->dmem.q_len - 1);
386 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700387 kfree(sq->skbuff);
388 nicvf_free_q_desc_mem(nic, &sq->dmem);
389}
390
391static void nicvf_reclaim_snd_queue(struct nicvf *nic,
392 struct queue_set *qs, int qidx)
393{
394 /* Disable send queue */
395 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
396 /* Check if SQ is stopped */
397 if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
398 return;
399 /* Reset send queue */
400 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
401}
402
403static void nicvf_reclaim_rcv_queue(struct nicvf *nic,
404 struct queue_set *qs, int qidx)
405{
406 union nic_mbx mbx = {};
407
408 /* Make sure all packets in the pipeline are written back into mem */
409 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
410 nicvf_send_msg_to_pf(nic, &mbx);
411}
412
413static void nicvf_reclaim_cmp_queue(struct nicvf *nic,
414 struct queue_set *qs, int qidx)
415{
416 /* Disable timer threshold (doesn't get reset upon CQ reset */
417 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
418 /* Disable completion queue */
419 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
420 /* Reset completion queue */
421 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
422}
423
424static void nicvf_reclaim_rbdr(struct nicvf *nic,
425 struct rbdr *rbdr, int qidx)
426{
427 u64 tmp, fifo_state;
428 int timeout = 10;
429
430 /* Save head and tail pointers for feeing up buffers */
431 rbdr->head = nicvf_queue_reg_read(nic,
432 NIC_QSET_RBDR_0_1_HEAD,
433 qidx) >> 3;
434 rbdr->tail = nicvf_queue_reg_read(nic,
435 NIC_QSET_RBDR_0_1_TAIL,
436 qidx) >> 3;
437
438 /* If RBDR FIFO is in 'FAIL' state then do a reset first
439 * before relaiming.
440 */
441 fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
442 if (((fifo_state >> 62) & 0x03) == 0x3)
443 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
444 qidx, NICVF_RBDR_RESET);
445
446 /* Disable RBDR */
447 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
448 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
449 return;
450 while (1) {
451 tmp = nicvf_queue_reg_read(nic,
452 NIC_QSET_RBDR_0_1_PREFETCH_STATUS,
453 qidx);
454 if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
455 break;
456 usleep_range(1000, 2000);
457 timeout--;
458 if (!timeout) {
459 netdev_err(nic->netdev,
460 "Failed polling on prefetch status\n");
461 return;
462 }
463 }
464 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
465 qidx, NICVF_RBDR_RESET);
466
467 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
468 return;
469 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
470 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
471 return;
472}
473
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300474void nicvf_config_vlan_stripping(struct nicvf *nic, netdev_features_t features)
475{
476 u64 rq_cfg;
477 int sqs;
478
479 rq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_RQ_GEN_CFG, 0);
480
481 /* Enable first VLAN stripping */
482 if (features & NETIF_F_HW_VLAN_CTAG_RX)
483 rq_cfg |= (1ULL << 25);
484 else
485 rq_cfg &= ~(1ULL << 25);
486 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
487
488 /* Configure Secondary Qsets, if any */
489 for (sqs = 0; sqs < nic->sqs_count; sqs++)
490 if (nic->snicvf[sqs])
491 nicvf_queue_reg_write(nic->snicvf[sqs],
492 NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
493}
494
Jerin Jacob3458c402016-08-12 16:51:39 +0530495static void nicvf_reset_rcv_queue_stats(struct nicvf *nic)
496{
497 union nic_mbx mbx = {};
498
Sunil Goutham964cb692016-11-15 17:38:16 +0530499 /* Reset all RQ/SQ and VF stats */
Jerin Jacob3458c402016-08-12 16:51:39 +0530500 mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
Sunil Goutham964cb692016-11-15 17:38:16 +0530501 mbx.reset_stat.rx_stat_mask = 0x3FFF;
502 mbx.reset_stat.tx_stat_mask = 0x1F;
Jerin Jacob3458c402016-08-12 16:51:39 +0530503 mbx.reset_stat.rq_stat_mask = 0xFFFF;
Sunil Goutham964cb692016-11-15 17:38:16 +0530504 mbx.reset_stat.sq_stat_mask = 0xFFFF;
Jerin Jacob3458c402016-08-12 16:51:39 +0530505 nicvf_send_msg_to_pf(nic, &mbx);
506}
507
Sunil Goutham4863dea2015-05-26 19:20:15 -0700508/* Configures receive queue */
509static void nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
510 int qidx, bool enable)
511{
512 union nic_mbx mbx = {};
513 struct rcv_queue *rq;
514 struct rq_cfg rq_cfg;
515
516 rq = &qs->rq[qidx];
517 rq->enable = enable;
518
519 /* Disable receive queue */
520 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
521
522 if (!rq->enable) {
523 nicvf_reclaim_rcv_queue(nic, qs, qidx);
524 return;
525 }
526
527 rq->cq_qs = qs->vnic_id;
528 rq->cq_idx = qidx;
529 rq->start_rbdr_qs = qs->vnic_id;
530 rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
531 rq->cont_rbdr_qs = qs->vnic_id;
532 rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
533 /* all writes of RBDR data to be loaded into L2 Cache as well*/
534 rq->caching = 1;
535
536 /* Send a mailbox msg to PF to config RQ */
537 mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
538 mbx.rq.qs_num = qs->vnic_id;
539 mbx.rq.rq_num = qidx;
540 mbx.rq.cfg = (rq->caching << 26) | (rq->cq_qs << 19) |
541 (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
542 (rq->cont_qs_rbdr_idx << 8) |
543 (rq->start_rbdr_qs << 1) | (rq->start_qs_rbdr_idx);
544 nicvf_send_msg_to_pf(nic, &mbx);
545
546 mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
547 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (qs->vnic_id << 0);
548 nicvf_send_msg_to_pf(nic, &mbx);
549
550 /* RQ drop config
551 * Enable CQ drop to reserve sufficient CQEs for all tx packets
552 */
553 mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
554 mbx.rq.cfg = (1ULL << 62) | (RQ_CQ_DROP << 8);
555 nicvf_send_msg_to_pf(nic, &mbx);
556
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530557 if (!nic->sqs_mode && (qidx == 0)) {
558 /* Enable checking L3/L4 length and TCP/UDP checksums */
559 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0,
560 (BIT(24) | BIT(23) | BIT(21)));
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300561 nicvf_config_vlan_stripping(nic, nic->netdev->features);
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530562 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700563
564 /* Enable Receive queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200565 memset(&rq_cfg, 0, sizeof(struct rq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700566 rq_cfg.ena = 1;
567 rq_cfg.tcp_ena = 0;
568 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
569}
570
571/* Configures completion queue */
572void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
573 int qidx, bool enable)
574{
575 struct cmp_queue *cq;
576 struct cq_cfg cq_cfg;
577
578 cq = &qs->cq[qidx];
579 cq->enable = enable;
580
581 if (!cq->enable) {
582 nicvf_reclaim_cmp_queue(nic, qs, qidx);
583 return;
584 }
585
586 /* Reset completion queue */
587 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
588
589 if (!cq->enable)
590 return;
591
592 spin_lock_init(&cq->lock);
593 /* Set completion queue base address */
594 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
595 qidx, (u64)(cq->dmem.phys_base));
596
597 /* Enable Completion queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200598 memset(&cq_cfg, 0, sizeof(struct cq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700599 cq_cfg.ena = 1;
600 cq_cfg.reset = 0;
601 cq_cfg.caching = 0;
602 cq_cfg.qsize = CMP_QSIZE;
603 cq_cfg.avg_con = 0;
604 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
605
606 /* Set threshold value for interrupt generation */
607 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
608 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
Sunil Goutham006394a2015-12-02 15:36:15 +0530609 qidx, CMP_QUEUE_TIMER_THRESH);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700610}
611
612/* Configures transmit queue */
613static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
614 int qidx, bool enable)
615{
616 union nic_mbx mbx = {};
617 struct snd_queue *sq;
618 struct sq_cfg sq_cfg;
619
620 sq = &qs->sq[qidx];
621 sq->enable = enable;
622
623 if (!sq->enable) {
624 nicvf_reclaim_snd_queue(nic, qs, qidx);
625 return;
626 }
627
628 /* Reset send queue */
629 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
630
631 sq->cq_qs = qs->vnic_id;
632 sq->cq_idx = qidx;
633
634 /* Send a mailbox msg to PF to config SQ */
635 mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
636 mbx.sq.qs_num = qs->vnic_id;
637 mbx.sq.sq_num = qidx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300638 mbx.sq.sqs_mode = nic->sqs_mode;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700639 mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
640 nicvf_send_msg_to_pf(nic, &mbx);
641
642 /* Set queue base address */
643 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
644 qidx, (u64)(sq->dmem.phys_base));
645
646 /* Enable send queue & set queue size */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200647 memset(&sq_cfg, 0, sizeof(struct sq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700648 sq_cfg.ena = 1;
649 sq_cfg.reset = 0;
650 sq_cfg.ldwb = 0;
651 sq_cfg.qsize = SND_QSIZE;
652 sq_cfg.tstmp_bgx_intf = 0;
653 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
654
655 /* Set threshold value for interrupt generation */
656 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
657
658 /* Set queue:cpu affinity for better load distribution */
659 if (cpu_online(qidx)) {
660 cpumask_set_cpu(qidx, &sq->affinity_mask);
661 netif_set_xps_queue(nic->netdev,
662 &sq->affinity_mask, qidx);
663 }
664}
665
666/* Configures receive buffer descriptor ring */
667static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
668 int qidx, bool enable)
669{
670 struct rbdr *rbdr;
671 struct rbdr_cfg rbdr_cfg;
672
673 rbdr = &qs->rbdr[qidx];
674 nicvf_reclaim_rbdr(nic, rbdr, qidx);
675 if (!enable)
676 return;
677
678 /* Set descriptor base address */
679 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
680 qidx, (u64)(rbdr->dmem.phys_base));
681
682 /* Enable RBDR & set queue size */
683 /* Buffer size should be in multiples of 128 bytes */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200684 memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700685 rbdr_cfg.ena = 1;
686 rbdr_cfg.reset = 0;
687 rbdr_cfg.ldwb = 0;
688 rbdr_cfg.qsize = RBDR_SIZE;
689 rbdr_cfg.avg_con = 0;
690 rbdr_cfg.lines = rbdr->dma_size / 128;
691 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
692 qidx, *(u64 *)&rbdr_cfg);
693
694 /* Notify HW */
695 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
696 qidx, qs->rbdr_len - 1);
697
698 /* Set threshold value for interrupt generation */
699 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
700 qidx, rbdr->thresh - 1);
701}
702
703/* Requests PF to assign and enable Qset */
704void nicvf_qset_config(struct nicvf *nic, bool enable)
705{
706 union nic_mbx mbx = {};
707 struct queue_set *qs = nic->qs;
708 struct qs_cfg *qs_cfg;
709
710 if (!qs) {
711 netdev_warn(nic->netdev,
712 "Qset is still not allocated, don't init queues\n");
713 return;
714 }
715
716 qs->enable = enable;
717 qs->vnic_id = nic->vf_id;
718
719 /* Send a mailbox msg to PF to config Qset */
720 mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
721 mbx.qs.num = qs->vnic_id;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300722 mbx.qs.sqs_count = nic->sqs_count;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700723
724 mbx.qs.cfg = 0;
725 qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
726 if (qs->enable) {
727 qs_cfg->ena = 1;
728#ifdef __BIG_ENDIAN
729 qs_cfg->be = 1;
730#endif
731 qs_cfg->vnic = qs->vnic_id;
732 }
733 nicvf_send_msg_to_pf(nic, &mbx);
734}
735
736static void nicvf_free_resources(struct nicvf *nic)
737{
738 int qidx;
739 struct queue_set *qs = nic->qs;
740
741 /* Free receive buffer descriptor ring */
742 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
743 nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
744
745 /* Free completion queue */
746 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
747 nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
748
749 /* Free send queue */
750 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
751 nicvf_free_snd_queue(nic, &qs->sq[qidx]);
752}
753
754static int nicvf_alloc_resources(struct nicvf *nic)
755{
756 int qidx;
757 struct queue_set *qs = nic->qs;
758
759 /* Alloc receive buffer descriptor ring */
760 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
761 if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
762 DMA_BUFFER_LEN))
763 goto alloc_fail;
764 }
765
766 /* Alloc send queue */
767 for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
768 if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
769 goto alloc_fail;
770 }
771
772 /* Alloc completion queue */
773 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
774 if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
775 goto alloc_fail;
776 }
777
778 return 0;
779alloc_fail:
780 nicvf_free_resources(nic);
781 return -ENOMEM;
782}
783
784int nicvf_set_qset_resources(struct nicvf *nic)
785{
786 struct queue_set *qs;
787
788 qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
789 if (!qs)
790 return -ENOMEM;
791 nic->qs = qs;
792
793 /* Set count of each queue */
Sunil Goutham3a397eb2016-08-12 16:51:27 +0530794 qs->rbdr_cnt = DEFAULT_RBDR_CNT;
795 qs->rq_cnt = min_t(u8, MAX_RCV_QUEUES_PER_QS, num_online_cpus());
796 qs->sq_cnt = min_t(u8, MAX_SND_QUEUES_PER_QS, num_online_cpus());
797 qs->cq_cnt = max_t(u8, qs->rq_cnt, qs->sq_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700798
799 /* Set queue lengths */
800 qs->rbdr_len = RCV_BUF_COUNT;
801 qs->sq_len = SND_QUEUE_LEN;
802 qs->cq_len = CMP_QUEUE_LEN;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300803
804 nic->rx_queues = qs->rq_cnt;
805 nic->tx_queues = qs->sq_cnt;
806
Sunil Goutham4863dea2015-05-26 19:20:15 -0700807 return 0;
808}
809
810int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
811{
812 bool disable = false;
813 struct queue_set *qs = nic->qs;
814 int qidx;
815
816 if (!qs)
817 return 0;
818
819 if (enable) {
820 if (nicvf_alloc_resources(nic))
821 return -ENOMEM;
822
823 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
824 nicvf_snd_queue_config(nic, qs, qidx, enable);
825 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
826 nicvf_cmp_queue_config(nic, qs, qidx, enable);
827 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
828 nicvf_rbdr_config(nic, qs, qidx, enable);
829 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
830 nicvf_rcv_queue_config(nic, qs, qidx, enable);
831 } else {
832 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
833 nicvf_rcv_queue_config(nic, qs, qidx, disable);
834 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
835 nicvf_rbdr_config(nic, qs, qidx, disable);
836 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
837 nicvf_snd_queue_config(nic, qs, qidx, disable);
838 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
839 nicvf_cmp_queue_config(nic, qs, qidx, disable);
840
841 nicvf_free_resources(nic);
842 }
843
Jerin Jacob3458c402016-08-12 16:51:39 +0530844 /* Reset RXQ's stats.
845 * SQ's stats will get reset automatically once SQ is reset.
846 */
847 nicvf_reset_rcv_queue_stats(nic);
848
Sunil Goutham4863dea2015-05-26 19:20:15 -0700849 return 0;
850}
851
852/* Get a free desc from SQ
853 * returns descriptor ponter & descriptor number
854 */
855static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
856{
857 int qentry;
858
859 qentry = sq->tail;
860 atomic_sub(desc_cnt, &sq->free_cnt);
861 sq->tail += desc_cnt;
862 sq->tail &= (sq->dmem.q_len - 1);
863
864 return qentry;
865}
866
867/* Free descriptor back to SQ for future use */
868void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
869{
870 atomic_add(desc_cnt, &sq->free_cnt);
871 sq->head += desc_cnt;
872 sq->head &= (sq->dmem.q_len - 1);
873}
874
875static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
876{
877 qentry++;
878 qentry &= (sq->dmem.q_len - 1);
879 return qentry;
880}
881
882void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
883{
884 u64 sq_cfg;
885
886 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
887 sq_cfg |= NICVF_SQ_EN;
888 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
889 /* Ring doorbell so that H/W restarts processing SQEs */
890 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
891}
892
893void nicvf_sq_disable(struct nicvf *nic, int qidx)
894{
895 u64 sq_cfg;
896
897 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
898 sq_cfg &= ~NICVF_SQ_EN;
899 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
900}
901
902void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
903 int qidx)
904{
905 u64 head, tail;
906 struct sk_buff *skb;
907 struct nicvf *nic = netdev_priv(netdev);
908 struct sq_hdr_subdesc *hdr;
909
910 head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
911 tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
912 while (sq->head != head) {
913 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
914 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
915 nicvf_put_sq_desc(sq, 1);
916 continue;
917 }
918 skb = (struct sk_buff *)sq->skbuff[sq->head];
Sunil Goutham143ceb02015-07-29 16:49:37 +0300919 if (skb)
920 dev_kfree_skb_any(skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700921 atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
922 atomic64_add(hdr->tot_len,
923 (atomic64_t *)&netdev->stats.tx_bytes);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700924 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
925 }
926}
927
928/* Calculate no of SQ subdescriptors needed to transmit all
929 * segments of this TSO packet.
930 * Taken from 'Tilera network driver' with a minor modification.
931 */
932static int nicvf_tso_count_subdescs(struct sk_buff *skb)
933{
934 struct skb_shared_info *sh = skb_shinfo(skb);
935 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
936 unsigned int data_len = skb->len - sh_len;
937 unsigned int p_len = sh->gso_size;
938 long f_id = -1; /* id of the current fragment */
939 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
940 long f_used = 0; /* bytes used from the current fragment */
941 long n; /* size of the current piece of payload */
942 int num_edescs = 0;
943 int segment;
944
945 for (segment = 0; segment < sh->gso_segs; segment++) {
946 unsigned int p_used = 0;
947
948 /* One edesc for header and for each piece of the payload. */
949 for (num_edescs++; p_used < p_len; num_edescs++) {
950 /* Advance as needed. */
951 while (f_used >= f_size) {
952 f_id++;
953 f_size = skb_frag_size(&sh->frags[f_id]);
954 f_used = 0;
955 }
956
957 /* Use bytes from the current fragment. */
958 n = p_len - p_used;
959 if (n > f_size - f_used)
960 n = f_size - f_used;
961 f_used += n;
962 p_used += n;
963 }
964
965 /* The last segment may be less than gso_size. */
966 data_len -= p_len;
967 if (data_len < p_len)
968 p_len = data_len;
969 }
970
971 /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
972 return num_edescs + sh->gso_segs;
973}
974
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530975#define POST_CQE_DESC_COUNT 2
976
Sunil Goutham4863dea2015-05-26 19:20:15 -0700977/* Get the number of SQ descriptors needed to xmit this skb */
978static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
979{
980 int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
981
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530982 if (skb_shinfo(skb)->gso_size && !nic->hw_tso) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700983 subdesc_cnt = nicvf_tso_count_subdescs(skb);
984 return subdesc_cnt;
985 }
986
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530987 /* Dummy descriptors to get TSO pkt completion notification */
988 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size)
989 subdesc_cnt += POST_CQE_DESC_COUNT;
990
Sunil Goutham4863dea2015-05-26 19:20:15 -0700991 if (skb_shinfo(skb)->nr_frags)
992 subdesc_cnt += skb_shinfo(skb)->nr_frags;
993
994 return subdesc_cnt;
995}
996
997/* Add SQ HEADER subdescriptor.
998 * First subdescriptor for every send descriptor.
999 */
1000static inline void
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301001nicvf_sq_add_hdr_subdesc(struct nicvf *nic, struct snd_queue *sq, int qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001002 int subdesc_cnt, struct sk_buff *skb, int len)
1003{
1004 int proto;
1005 struct sq_hdr_subdesc *hdr;
1006
1007 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001008 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
1009 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301010
1011 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size) {
1012 /* post_cqe = 0, to avoid HW posting a CQE for every TSO
1013 * segment transmitted on 88xx.
1014 */
1015 hdr->subdesc_cnt = subdesc_cnt - POST_CQE_DESC_COUNT;
1016 } else {
1017 sq->skbuff[qentry] = (u64)skb;
1018 /* Enable notification via CQE after processing SQE */
1019 hdr->post_cqe = 1;
1020 /* No of subdescriptors following this */
1021 hdr->subdesc_cnt = subdesc_cnt;
1022 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001023 hdr->tot_len = len;
1024
1025 /* Offload checksum calculation to HW */
1026 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001027 hdr->csum_l3 = 1; /* Enable IP csum calculation */
1028 hdr->l3_offset = skb_network_offset(skb);
1029 hdr->l4_offset = skb_transport_offset(skb);
1030
1031 proto = ip_hdr(skb)->protocol;
1032 switch (proto) {
1033 case IPPROTO_TCP:
1034 hdr->csum_l4 = SEND_L4_CSUM_TCP;
1035 break;
1036 case IPPROTO_UDP:
1037 hdr->csum_l4 = SEND_L4_CSUM_UDP;
1038 break;
1039 case IPPROTO_SCTP:
1040 hdr->csum_l4 = SEND_L4_CSUM_SCTP;
1041 break;
1042 }
1043 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301044
1045 if (nic->hw_tso && skb_shinfo(skb)->gso_size) {
1046 hdr->tso = 1;
1047 hdr->tso_start = skb_transport_offset(skb) + tcp_hdrlen(skb);
1048 hdr->tso_max_paysize = skb_shinfo(skb)->gso_size;
1049 /* For non-tunneled pkts, point this to L2 ethertype */
1050 hdr->inner_l3_offset = skb_network_offset(skb) - 2;
Sunil Goutham964cb692016-11-15 17:38:16 +05301051 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301052 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001053}
1054
1055/* SQ GATHER subdescriptor
1056 * Must follow HDR descriptor
1057 */
1058static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
1059 int size, u64 data)
1060{
1061 struct sq_gather_subdesc *gather;
1062
1063 qentry &= (sq->dmem.q_len - 1);
1064 gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
1065
1066 memset(gather, 0, SND_QUEUE_DESC_SIZE);
1067 gather->subdesc_type = SQ_DESC_TYPE_GATHER;
Sunil Goutham4b561c12015-07-29 16:49:36 +03001068 gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001069 gather->size = size;
1070 gather->addr = data;
1071}
1072
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301073/* Add HDR + IMMEDIATE subdescriptors right after descriptors of a TSO
1074 * packet so that a CQE is posted as a notifation for transmission of
1075 * TSO packet.
1076 */
1077static inline void nicvf_sq_add_cqe_subdesc(struct snd_queue *sq, int qentry,
1078 int tso_sqe, struct sk_buff *skb)
1079{
1080 struct sq_imm_subdesc *imm;
1081 struct sq_hdr_subdesc *hdr;
1082
1083 sq->skbuff[qentry] = (u64)skb;
1084
1085 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
1086 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
1087 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
1088 /* Enable notification via CQE after processing SQE */
1089 hdr->post_cqe = 1;
1090 /* There is no packet to transmit here */
1091 hdr->dont_send = 1;
1092 hdr->subdesc_cnt = POST_CQE_DESC_COUNT - 1;
1093 hdr->tot_len = 1;
1094 /* Actual TSO header SQE index, needed for cleanup */
1095 hdr->rsvd2 = tso_sqe;
1096
1097 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1098 imm = (struct sq_imm_subdesc *)GET_SQ_DESC(sq, qentry);
1099 memset(imm, 0, SND_QUEUE_DESC_SIZE);
1100 imm->subdesc_type = SQ_DESC_TYPE_IMMEDIATE;
1101 imm->len = 1;
1102}
1103
Sunil Goutham2c204c22016-09-23 14:42:28 +05301104static inline void nicvf_sq_doorbell(struct nicvf *nic, struct sk_buff *skb,
1105 int sq_num, int desc_cnt)
1106{
1107 struct netdev_queue *txq;
1108
1109 txq = netdev_get_tx_queue(nic->pnicvf->netdev,
1110 skb_get_queue_mapping(skb));
1111
1112 netdev_tx_sent_queue(txq, skb->len);
1113
1114 /* make sure all memory stores are done before ringing doorbell */
1115 smp_wmb();
1116
1117 /* Inform HW to xmit all TSO segments */
1118 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
1119 sq_num, desc_cnt);
1120}
1121
Sunil Goutham4863dea2015-05-26 19:20:15 -07001122/* Segment a TSO packet into 'gso_size' segments and append
1123 * them to SQ for transfer
1124 */
1125static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
Sunil Goutham92dc8762015-08-30 12:29:15 +03001126 int sq_num, int qentry, struct sk_buff *skb)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001127{
1128 struct tso_t tso;
1129 int seg_subdescs = 0, desc_cnt = 0;
1130 int seg_len, total_len, data_left;
1131 int hdr_qentry = qentry;
1132 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1133
1134 tso_start(skb, &tso);
1135 total_len = skb->len - hdr_len;
1136 while (total_len > 0) {
1137 char *hdr;
1138
1139 /* Save Qentry for adding HDR_SUBDESC at the end */
1140 hdr_qentry = qentry;
1141
1142 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1143 total_len -= data_left;
1144
1145 /* Add segment's header */
1146 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1147 hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
1148 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1149 nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
1150 sq->tso_hdrs_phys +
1151 qentry * TSO_HEADER_SIZE);
1152 /* HDR_SUDESC + GATHER */
1153 seg_subdescs = 2;
1154 seg_len = hdr_len;
1155
1156 /* Add segment's payload fragments */
1157 while (data_left > 0) {
1158 int size;
1159
1160 size = min_t(int, tso.size, data_left);
1161
1162 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1163 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1164 virt_to_phys(tso.data));
1165 seg_subdescs++;
1166 seg_len += size;
1167
1168 data_left -= size;
1169 tso_build_data(skb, &tso, size);
1170 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301171 nicvf_sq_add_hdr_subdesc(nic, sq, hdr_qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001172 seg_subdescs - 1, skb, seg_len);
Sunil Goutham143ceb02015-07-29 16:49:37 +03001173 sq->skbuff[hdr_qentry] = (u64)NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001174 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1175
1176 desc_cnt += seg_subdescs;
1177 }
1178 /* Save SKB in the last segment for freeing */
1179 sq->skbuff[hdr_qentry] = (u64)skb;
1180
Sunil Goutham2c204c22016-09-23 14:42:28 +05301181 nicvf_sq_doorbell(nic, skb, sq_num, desc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001182
Sunil Goutham964cb692016-11-15 17:38:16 +05301183 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001184 return 1;
1185}
1186
1187/* Append an skb to a SQ for packet transfer. */
1188int nicvf_sq_append_skb(struct nicvf *nic, struct sk_buff *skb)
1189{
1190 int i, size;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301191 int subdesc_cnt, tso_sqe = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001192 int sq_num, qentry;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001193 struct queue_set *qs;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001194 struct snd_queue *sq;
1195
1196 sq_num = skb_get_queue_mapping(skb);
Sunil Goutham92dc8762015-08-30 12:29:15 +03001197 if (sq_num >= MAX_SND_QUEUES_PER_QS) {
1198 /* Get secondary Qset's SQ structure */
1199 i = sq_num / MAX_SND_QUEUES_PER_QS;
1200 if (!nic->snicvf[i - 1]) {
1201 netdev_warn(nic->netdev,
1202 "Secondary Qset#%d's ptr not initialized\n",
1203 i - 1);
1204 return 1;
1205 }
1206 nic = (struct nicvf *)nic->snicvf[i - 1];
1207 sq_num = sq_num % MAX_SND_QUEUES_PER_QS;
1208 }
1209
1210 qs = nic->qs;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001211 sq = &qs->sq[sq_num];
1212
1213 subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
1214 if (subdesc_cnt > atomic_read(&sq->free_cnt))
1215 goto append_fail;
1216
1217 qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
1218
1219 /* Check if its a TSO packet */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301220 if (skb_shinfo(skb)->gso_size && !nic->hw_tso)
Sunil Goutham92dc8762015-08-30 12:29:15 +03001221 return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001222
1223 /* Add SQ header subdesc */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301224 nicvf_sq_add_hdr_subdesc(nic, sq, qentry, subdesc_cnt - 1,
1225 skb, skb->len);
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301226 tso_sqe = qentry;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001227
1228 /* Add SQ gather subdescs */
1229 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1230 size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
1231 nicvf_sq_add_gather_subdesc(sq, qentry, size, virt_to_phys(skb->data));
1232
1233 /* Check for scattered buffer */
1234 if (!skb_is_nonlinear(skb))
1235 goto doorbell;
1236
1237 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1238 const struct skb_frag_struct *frag;
1239
1240 frag = &skb_shinfo(skb)->frags[i];
1241
1242 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1243 size = skb_frag_size(frag);
1244 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1245 virt_to_phys(
1246 skb_frag_address(frag)));
1247 }
1248
1249doorbell:
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301250 if (nic->t88 && skb_shinfo(skb)->gso_size) {
1251 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1252 nicvf_sq_add_cqe_subdesc(sq, qentry, tso_sqe, skb);
1253 }
1254
Sunil Goutham2c204c22016-09-23 14:42:28 +05301255 nicvf_sq_doorbell(nic, skb, sq_num, subdesc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001256
Sunil Goutham4863dea2015-05-26 19:20:15 -07001257 return 1;
1258
1259append_fail:
Sunil Goutham92dc8762015-08-30 12:29:15 +03001260 /* Use original PCI dev for debug log */
1261 nic = nic->pnicvf;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001262 netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
1263 return 0;
1264}
1265
1266static inline unsigned frag_num(unsigned i)
1267{
1268#ifdef __BIG_ENDIAN
1269 return (i & ~3) + 3 - (i & 3);
1270#else
1271 return i;
1272#endif
1273}
1274
1275/* Returns SKB for a received packet */
1276struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
1277{
1278 int frag;
1279 int payload_len = 0;
1280 struct sk_buff *skb = NULL;
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301281 struct page *page;
1282 int offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001283 u16 *rb_lens = NULL;
1284 u64 *rb_ptrs = NULL;
1285
1286 rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
Sunil Goutham02a72bd2016-08-12 16:51:28 +05301287 /* Except 88xx pass1 on all other chips CQE_RX2_S is added to
1288 * CQE_RX at word6, hence buffer pointers move by word
1289 *
1290 * Use existing 'hw_tso' flag which will be set for all chips
1291 * except 88xx pass1 instead of a additional cache line
1292 * access (or miss) by using pci dev's revision.
1293 */
1294 if (!nic->hw_tso)
1295 rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
1296 else
1297 rb_ptrs = (void *)cqe_rx + (7 * sizeof(u64));
Sunil Goutham4863dea2015-05-26 19:20:15 -07001298
1299 netdev_dbg(nic->netdev, "%s rb_cnt %d rb0_ptr %llx rb0_sz %d\n",
1300 __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
1301
1302 for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
1303 payload_len = rb_lens[frag_num(frag)];
1304 if (!frag) {
1305 /* First fragment */
1306 skb = nicvf_rb_ptr_to_skb(nic,
1307 *rb_ptrs - cqe_rx->align_pad,
1308 payload_len);
1309 if (!skb)
1310 return NULL;
1311 skb_reserve(skb, cqe_rx->align_pad);
1312 skb_put(skb, payload_len);
1313 } else {
1314 /* Add fragments */
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301315 page = virt_to_page(phys_to_virt(*rb_ptrs));
1316 offset = phys_to_virt(*rb_ptrs) - page_address(page);
1317 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
1318 offset, payload_len, RCV_FRAG_LEN);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001319 }
1320 /* Next buffer pointer */
1321 rb_ptrs++;
1322 }
1323 return skb;
1324}
1325
Yury Norovb45ceb42015-12-07 10:30:32 +05301326static u64 nicvf_int_type_to_mask(int int_type, int q_idx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001327{
1328 u64 reg_val;
1329
Sunil Goutham4863dea2015-05-26 19:20:15 -07001330 switch (int_type) {
1331 case NICVF_INTR_CQ:
1332 reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1333 break;
1334 case NICVF_INTR_SQ:
1335 reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1336 break;
1337 case NICVF_INTR_RBDR:
1338 reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1339 break;
1340 case NICVF_INTR_PKT_DROP:
1341 reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1342 break;
1343 case NICVF_INTR_TCP_TIMER:
1344 reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1345 break;
1346 case NICVF_INTR_MBOX:
1347 reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
1348 break;
1349 case NICVF_INTR_QS_ERR:
Yury Norovb45ceb42015-12-07 10:30:32 +05301350 reg_val = (1ULL << NICVF_INTR_QS_ERR_SHIFT);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001351 break;
1352 default:
Yury Norovb45ceb42015-12-07 10:30:32 +05301353 reg_val = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001354 }
1355
Yury Norovb45ceb42015-12-07 10:30:32 +05301356 return reg_val;
1357}
1358
1359/* Enable interrupt */
1360void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
1361{
1362 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1363
1364 if (!mask) {
1365 netdev_dbg(nic->netdev,
1366 "Failed to enable interrupt: unknown type\n");
1367 return;
1368 }
1369 nicvf_reg_write(nic, NIC_VF_ENA_W1S,
1370 nicvf_reg_read(nic, NIC_VF_ENA_W1S) | mask);
1371}
1372
1373/* Disable interrupt */
1374void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
1375{
1376 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1377
1378 if (!mask) {
1379 netdev_dbg(nic->netdev,
1380 "Failed to disable interrupt: unknown type\n");
1381 return;
1382 }
1383
1384 nicvf_reg_write(nic, NIC_VF_ENA_W1C, mask);
1385}
1386
1387/* Clear interrupt */
1388void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
1389{
1390 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1391
1392 if (!mask) {
1393 netdev_dbg(nic->netdev,
1394 "Failed to clear interrupt: unknown type\n");
1395 return;
1396 }
1397
1398 nicvf_reg_write(nic, NIC_VF_INT, mask);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001399}
1400
1401/* Check if interrupt is enabled */
1402int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
1403{
Yury Norovb45ceb42015-12-07 10:30:32 +05301404 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1405 /* If interrupt type is unknown, we treat it disabled. */
1406 if (!mask) {
1407 netdev_dbg(nic->netdev,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001408 "Failed to check interrupt enable: unknown type\n");
Yury Norovb45ceb42015-12-07 10:30:32 +05301409 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001410 }
1411
Yury Norovb45ceb42015-12-07 10:30:32 +05301412 return mask & nicvf_reg_read(nic, NIC_VF_ENA_W1S);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001413}
1414
1415void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
1416{
1417 struct rcv_queue *rq;
1418
1419#define GET_RQ_STATS(reg) \
1420 nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
1421 (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1422
1423 rq = &nic->qs->rq[rq_idx];
1424 rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
1425 rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
1426}
1427
1428void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
1429{
1430 struct snd_queue *sq;
1431
1432#define GET_SQ_STATS(reg) \
1433 nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
1434 (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1435
1436 sq = &nic->qs->sq[sq_idx];
1437 sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
1438 sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
1439}
1440
1441/* Check for errors in the receive cmp.queue entry */
Sunil Gouthamad2eceb2016-02-16 16:29:51 +05301442int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001443{
Sunil Gouthamad2eceb2016-02-16 16:29:51 +05301444 if (!cqe_rx->err_level && !cqe_rx->err_opcode)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001445 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001446
1447 if (netif_msg_rx_err(nic))
1448 netdev_err(nic->netdev,
1449 "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
1450 nic->netdev->name,
1451 cqe_rx->err_level, cqe_rx->err_opcode);
1452
Sunil Goutham4863dea2015-05-26 19:20:15 -07001453 switch (cqe_rx->err_opcode) {
1454 case CQ_RX_ERROP_RE_PARTIAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301455 this_cpu_inc(nic->drv_stats->rx_bgx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001456 break;
1457 case CQ_RX_ERROP_RE_JABBER:
Sunil Goutham964cb692016-11-15 17:38:16 +05301458 this_cpu_inc(nic->drv_stats->rx_jabber_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001459 break;
1460 case CQ_RX_ERROP_RE_FCS:
Sunil Goutham964cb692016-11-15 17:38:16 +05301461 this_cpu_inc(nic->drv_stats->rx_fcs_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001462 break;
1463 case CQ_RX_ERROP_RE_RX_CTL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301464 this_cpu_inc(nic->drv_stats->rx_bgx_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001465 break;
1466 case CQ_RX_ERROP_PREL2_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301467 this_cpu_inc(nic->drv_stats->rx_prel2_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001468 break;
1469 case CQ_RX_ERROP_L2_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301470 this_cpu_inc(nic->drv_stats->rx_l2_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001471 break;
1472 case CQ_RX_ERROP_L2_OVERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301473 this_cpu_inc(nic->drv_stats->rx_oversize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001474 break;
1475 case CQ_RX_ERROP_L2_UNDERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301476 this_cpu_inc(nic->drv_stats->rx_undersize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001477 break;
1478 case CQ_RX_ERROP_L2_LENMISM:
Sunil Goutham964cb692016-11-15 17:38:16 +05301479 this_cpu_inc(nic->drv_stats->rx_l2_len_mismatch);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001480 break;
1481 case CQ_RX_ERROP_L2_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301482 this_cpu_inc(nic->drv_stats->rx_l2_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001483 break;
1484 case CQ_RX_ERROP_IP_NOT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301485 this_cpu_inc(nic->drv_stats->rx_ip_ver_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001486 break;
1487 case CQ_RX_ERROP_IP_CSUM_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301488 this_cpu_inc(nic->drv_stats->rx_ip_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001489 break;
1490 case CQ_RX_ERROP_IP_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301491 this_cpu_inc(nic->drv_stats->rx_ip_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001492 break;
1493 case CQ_RX_ERROP_IP_MALD:
Sunil Goutham964cb692016-11-15 17:38:16 +05301494 this_cpu_inc(nic->drv_stats->rx_ip_payload_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001495 break;
1496 case CQ_RX_ERROP_IP_HOP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301497 this_cpu_inc(nic->drv_stats->rx_ip_ttl_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001498 break;
1499 case CQ_RX_ERROP_L3_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301500 this_cpu_inc(nic->drv_stats->rx_l3_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001501 break;
1502 case CQ_RX_ERROP_L4_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301503 this_cpu_inc(nic->drv_stats->rx_l4_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001504 break;
1505 case CQ_RX_ERROP_L4_CHK:
Sunil Goutham964cb692016-11-15 17:38:16 +05301506 this_cpu_inc(nic->drv_stats->rx_l4_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001507 break;
1508 case CQ_RX_ERROP_UDP_LEN:
Sunil Goutham964cb692016-11-15 17:38:16 +05301509 this_cpu_inc(nic->drv_stats->rx_udp_len_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001510 break;
1511 case CQ_RX_ERROP_L4_PORT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301512 this_cpu_inc(nic->drv_stats->rx_l4_port_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001513 break;
1514 case CQ_RX_ERROP_TCP_FLAG:
Sunil Goutham964cb692016-11-15 17:38:16 +05301515 this_cpu_inc(nic->drv_stats->rx_tcp_flag_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001516 break;
1517 case CQ_RX_ERROP_TCP_OFFSET:
Sunil Goutham964cb692016-11-15 17:38:16 +05301518 this_cpu_inc(nic->drv_stats->rx_tcp_offset_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001519 break;
1520 case CQ_RX_ERROP_L4_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301521 this_cpu_inc(nic->drv_stats->rx_l4_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001522 break;
1523 case CQ_RX_ERROP_RBDR_TRUNC:
Sunil Goutham964cb692016-11-15 17:38:16 +05301524 this_cpu_inc(nic->drv_stats->rx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001525 break;
1526 }
1527
1528 return 1;
1529}
1530
1531/* Check for errors in the send cmp.queue entry */
Sunil Goutham964cb692016-11-15 17:38:16 +05301532int nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cqe_send_t *cqe_tx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001533{
Sunil Goutham4863dea2015-05-26 19:20:15 -07001534 switch (cqe_tx->send_status) {
1535 case CQ_TX_ERROP_GOOD:
Sunil Goutham4863dea2015-05-26 19:20:15 -07001536 return 0;
1537 case CQ_TX_ERROP_DESC_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301538 this_cpu_inc(nic->drv_stats->tx_desc_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001539 break;
1540 case CQ_TX_ERROP_HDR_CONS_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301541 this_cpu_inc(nic->drv_stats->tx_hdr_cons_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001542 break;
1543 case CQ_TX_ERROP_SUBDC_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301544 this_cpu_inc(nic->drv_stats->tx_subdesc_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001545 break;
Sunil Goutham712c3182016-11-15 17:37:36 +05301546 case CQ_TX_ERROP_MAX_SIZE_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301547 this_cpu_inc(nic->drv_stats->tx_max_size_exceeded);
Sunil Goutham712c3182016-11-15 17:37:36 +05301548 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001549 case CQ_TX_ERROP_IMM_SIZE_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301550 this_cpu_inc(nic->drv_stats->tx_imm_size_oflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001551 break;
1552 case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301553 this_cpu_inc(nic->drv_stats->tx_data_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001554 break;
1555 case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301556 this_cpu_inc(nic->drv_stats->tx_mem_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001557 break;
1558 case CQ_TX_ERROP_LOCK_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301559 this_cpu_inc(nic->drv_stats->tx_lock_viol);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001560 break;
1561 case CQ_TX_ERROP_DATA_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301562 this_cpu_inc(nic->drv_stats->tx_data_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001563 break;
1564 case CQ_TX_ERROP_TSTMP_CONFLICT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301565 this_cpu_inc(nic->drv_stats->tx_tstmp_conflict);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001566 break;
1567 case CQ_TX_ERROP_TSTMP_TIMEOUT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301568 this_cpu_inc(nic->drv_stats->tx_tstmp_timeout);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001569 break;
1570 case CQ_TX_ERROP_MEM_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301571 this_cpu_inc(nic->drv_stats->tx_mem_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001572 break;
1573 case CQ_TX_ERROP_CK_OVERLAP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301574 this_cpu_inc(nic->drv_stats->tx_csum_overlap);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001575 break;
1576 case CQ_TX_ERROP_CK_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301577 this_cpu_inc(nic->drv_stats->tx_csum_overflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001578 break;
1579 }
1580
1581 return 1;
1582}