blob: 8c3c571568aa3b1b9dc868e4155f9f5c98a20175 [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>
Sunil Goutham83abb7d2017-03-07 18:09:08 +053013#include <linux/iommu.h>
Sunil Goutham4863dea2015-05-26 19:20:15 -070014#include <net/ip.h>
15#include <net/tso.h>
16
17#include "nic_reg.h"
18#include "nic.h"
19#include "q_struct.h"
20#include "nicvf_queues.h"
21
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053022static void nicvf_get_page(struct nicvf *nic)
23{
24 if (!nic->rb_pageref || !nic->rb_page)
25 return;
26
Joonsoo Kim6d061f92016-05-19 17:10:46 -070027 page_ref_add(nic->rb_page, nic->rb_pageref);
Sunil Goutham5c2e26f2016-03-14 16:36:14 +053028 nic->rb_pageref = 0;
29}
30
Sunil Goutham4863dea2015-05-26 19:20:15 -070031/* Poll a register for a specific value */
32static int nicvf_poll_reg(struct nicvf *nic, int qidx,
33 u64 reg, int bit_pos, int bits, int val)
34{
35 u64 bit_mask;
36 u64 reg_val;
37 int timeout = 10;
38
39 bit_mask = (1ULL << bits) - 1;
40 bit_mask = (bit_mask << bit_pos);
41
42 while (timeout) {
43 reg_val = nicvf_queue_reg_read(nic, reg, qidx);
44 if (((reg_val & bit_mask) >> bit_pos) == val)
45 return 0;
46 usleep_range(1000, 2000);
47 timeout--;
48 }
49 netdev_err(nic->netdev, "Poll on reg 0x%llx failed\n", reg);
50 return 1;
51}
52
53/* Allocate memory for a queue's descriptors */
54static int nicvf_alloc_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem,
55 int q_len, int desc_size, int align_bytes)
56{
57 dmem->q_len = q_len;
58 dmem->size = (desc_size * q_len) + align_bytes;
59 /* Save address, need it while freeing */
60 dmem->unalign_base = dma_zalloc_coherent(&nic->pdev->dev, dmem->size,
61 &dmem->dma, GFP_KERNEL);
62 if (!dmem->unalign_base)
63 return -ENOMEM;
64
65 /* Align memory address for 'align_bytes' */
66 dmem->phys_base = NICVF_ALIGNED_ADDR((u64)dmem->dma, align_bytes);
Aleksey Makarov39a0dd02015-06-02 11:00:25 -070067 dmem->base = dmem->unalign_base + (dmem->phys_base - dmem->dma);
Sunil Goutham4863dea2015-05-26 19:20:15 -070068 return 0;
69}
70
71/* Free queue's descriptor memory */
72static void nicvf_free_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem)
73{
74 if (!dmem)
75 return;
76
77 dma_free_coherent(&nic->pdev->dev, dmem->size,
78 dmem->unalign_base, dmem->dma);
79 dmem->unalign_base = NULL;
80 dmem->base = NULL;
81}
82
Sunil Goutham5836b442017-05-02 18:36:50 +053083/* Allocate a new page or recycle one if possible
84 *
85 * We cannot optimize dma mapping here, since
86 * 1. It's only one RBDR ring for 8 Rx queues.
87 * 2. CQE_RX gives address of the buffer where pkt has been DMA'ed
88 * and not idx into RBDR ring, so can't refer to saved info.
89 * 3. There are multiple receive buffers per page
Sunil Goutham4863dea2015-05-26 19:20:15 -070090 */
Sunil Goutham5836b442017-05-02 18:36:50 +053091static struct pgcache *nicvf_alloc_page(struct nicvf *nic,
92 struct rbdr *rbdr, gfp_t gfp)
Sunil Goutham4863dea2015-05-26 19:20:15 -070093{
Sunil Goutham5836b442017-05-02 18:36:50 +053094 struct page *page = NULL;
95 struct pgcache *pgcache, *next;
96
97 /* Check if page is already allocated */
98 pgcache = &rbdr->pgcache[rbdr->pgidx];
99 page = pgcache->page;
100 /* Check if page can be recycled */
101 if (page && (page_ref_count(page) != 1))
102 page = NULL;
103
104 if (!page) {
105 page = alloc_pages(gfp | __GFP_COMP | __GFP_NOWARN, 0);
106 if (!page)
107 return NULL;
108
109 this_cpu_inc(nic->pnicvf->drv_stats->page_alloc);
110
111 /* Check for space */
112 if (rbdr->pgalloc >= rbdr->pgcnt) {
113 /* Page can still be used */
114 nic->rb_page = page;
115 return NULL;
116 }
117
118 /* Save the page in page cache */
119 pgcache->page = page;
120 rbdr->pgalloc++;
121 }
122
123 /* Take extra page reference for recycling */
124 page_ref_add(page, 1);
125
126 rbdr->pgidx++;
127 rbdr->pgidx &= (rbdr->pgcnt - 1);
128
129 /* Prefetch refcount of next page in page cache */
130 next = &rbdr->pgcache[rbdr->pgidx];
131 page = next->page;
132 if (page)
133 prefetch(&page->_refcount);
134
135 return pgcache;
136}
137
138/* Allocate buffer for packet reception */
139static inline int nicvf_alloc_rcv_buffer(struct nicvf *nic, struct rbdr *rbdr,
Sunil Goutham927987f2017-05-02 18:36:53 +0530140 gfp_t gfp, u32 buf_len, u64 *rbuf)
Sunil Goutham5836b442017-05-02 18:36:50 +0530141{
142 struct pgcache *pgcache = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700143
Sunil Goutham05c773f2017-05-02 18:36:54 +0530144 /* Check if request can be accomodated in previous allocated page.
145 * But in XDP mode only one buffer per page is permitted.
146 */
147 if (!nic->pnicvf->xdp_prog && nic->rb_page &&
Sunil Goutham5836b442017-05-02 18:36:50 +0530148 ((nic->rb_page_offset + buf_len) <= PAGE_SIZE)) {
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530149 nic->rb_pageref++;
150 goto ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700151 }
152
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530153 nicvf_get_page(nic);
Sunil Goutham5836b442017-05-02 18:36:50 +0530154 nic->rb_page = NULL;
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530155
Sunil Goutham5836b442017-05-02 18:36:50 +0530156 /* Get new page, either recycled or new one */
157 pgcache = nicvf_alloc_page(nic, rbdr, gfp);
158 if (!pgcache && !nic->rb_page) {
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530159 this_cpu_inc(nic->pnicvf->drv_stats->rcv_buffer_alloc_failures);
160 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700161 }
Sunil Goutham5836b442017-05-02 18:36:50 +0530162
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530163 nic->rb_page_offset = 0;
Sunil Goutham5836b442017-05-02 18:36:50 +0530164 /* Check if it's recycled */
165 if (pgcache)
166 nic->rb_page = pgcache->page;
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530167ret:
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530168 /* HW will ensure data coherency, CPU sync not required */
Sunil Goutham927987f2017-05-02 18:36:53 +0530169 *rbuf = (u64)dma_map_page_attrs(&nic->pdev->dev, nic->rb_page,
170 nic->rb_page_offset, buf_len,
171 DMA_FROM_DEVICE,
172 DMA_ATTR_SKIP_CPU_SYNC);
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530173 if (dma_mapping_error(&nic->pdev->dev, (dma_addr_t)*rbuf)) {
174 if (!nic->rb_page_offset)
Sunil Goutham5836b442017-05-02 18:36:50 +0530175 __free_pages(nic->rb_page, 0);
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530176 nic->rb_page = NULL;
177 return -ENOMEM;
178 }
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530179 nic->rb_page_offset += buf_len;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700180
Sunil Goutham4863dea2015-05-26 19:20:15 -0700181 return 0;
182}
183
Sunil Goutham668dda02015-12-07 10:30:33 +0530184/* Build skb around receive buffer */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700185static struct sk_buff *nicvf_rb_ptr_to_skb(struct nicvf *nic,
186 u64 rb_ptr, int len)
187{
Sunil Goutham668dda02015-12-07 10:30:33 +0530188 void *data;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700189 struct sk_buff *skb;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700190
Sunil Goutham668dda02015-12-07 10:30:33 +0530191 data = phys_to_virt(rb_ptr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700192
193 /* Now build an skb to give to stack */
Sunil Goutham668dda02015-12-07 10:30:33 +0530194 skb = build_skb(data, RCV_FRAG_LEN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700195 if (!skb) {
Sunil Goutham668dda02015-12-07 10:30:33 +0530196 put_page(virt_to_page(data));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700197 return NULL;
198 }
199
Sunil Goutham668dda02015-12-07 10:30:33 +0530200 prefetch(skb->data);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700201 return skb;
202}
203
204/* Allocate RBDR ring and populate receive buffers */
205static int nicvf_init_rbdr(struct nicvf *nic, struct rbdr *rbdr,
206 int ring_len, int buf_size)
207{
208 int idx;
Sunil Goutham927987f2017-05-02 18:36:53 +0530209 u64 rbuf;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700210 struct rbdr_entry_t *desc;
211 int err;
212
213 err = nicvf_alloc_q_desc_mem(nic, &rbdr->dmem, ring_len,
214 sizeof(struct rbdr_entry_t),
215 NICVF_RCV_BUF_ALIGN_BYTES);
216 if (err)
217 return err;
218
219 rbdr->desc = rbdr->dmem.base;
220 /* Buffer size has to be in multiples of 128 bytes */
221 rbdr->dma_size = buf_size;
222 rbdr->enable = true;
223 rbdr->thresh = RBDR_THRESH;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530224 rbdr->head = 0;
225 rbdr->tail = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700226
Sunil Goutham5836b442017-05-02 18:36:50 +0530227 /* Initialize page recycling stuff.
228 *
229 * Can't use single buffer per page especially with 64K pages.
230 * On embedded platforms i.e 81xx/83xx available memory itself
231 * is low and minimum ring size of RBDR is 8K, that takes away
232 * lots of memory.
233 */
234 rbdr->pgcnt = ring_len / (PAGE_SIZE / buf_size);
235 rbdr->pgcnt = roundup_pow_of_two(rbdr->pgcnt);
236 rbdr->pgcache = kzalloc(sizeof(*rbdr->pgcache) *
237 rbdr->pgcnt, GFP_KERNEL);
238 if (!rbdr->pgcache)
239 return -ENOMEM;
240 rbdr->pgidx = 0;
241 rbdr->pgalloc = 0;
242
Sunil Goutham4863dea2015-05-26 19:20:15 -0700243 nic->rb_page = NULL;
244 for (idx = 0; idx < ring_len; idx++) {
Sunil Goutham5836b442017-05-02 18:36:50 +0530245 err = nicvf_alloc_rcv_buffer(nic, rbdr, GFP_KERNEL,
246 RCV_FRAG_LEN, &rbuf);
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530247 if (err) {
248 /* To free already allocated and mapped ones */
249 rbdr->tail = idx - 1;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700250 return err;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530251 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700252
253 desc = GET_RBDR_DESC(rbdr, idx);
Sunil Goutham927987f2017-05-02 18:36:53 +0530254 desc->buf_addr = rbuf & ~(NICVF_RCV_BUF_ALIGN_BYTES - 1);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700255 }
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530256
257 nicvf_get_page(nic);
258
Sunil Goutham4863dea2015-05-26 19:20:15 -0700259 return 0;
260}
261
262/* Free RBDR ring and its receive buffers */
263static void nicvf_free_rbdr(struct nicvf *nic, struct rbdr *rbdr)
264{
265 int head, tail;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530266 u64 buf_addr, phys_addr;
Sunil Goutham5836b442017-05-02 18:36:50 +0530267 struct pgcache *pgcache;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700268 struct rbdr_entry_t *desc;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700269
270 if (!rbdr)
271 return;
272
273 rbdr->enable = false;
274 if (!rbdr->dmem.base)
275 return;
276
277 head = rbdr->head;
278 tail = rbdr->tail;
279
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530280 /* Release page references */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700281 while (head != tail) {
282 desc = GET_RBDR_DESC(rbdr, head);
Sunil Goutham5e848e42017-05-02 18:36:51 +0530283 buf_addr = desc->buf_addr;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530284 phys_addr = nicvf_iova_to_phys(nic, buf_addr);
285 dma_unmap_page_attrs(&nic->pdev->dev, buf_addr, RCV_FRAG_LEN,
286 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
287 if (phys_addr)
288 put_page(virt_to_page(phys_to_virt(phys_addr)));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700289 head++;
290 head &= (rbdr->dmem.q_len - 1);
291 }
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530292 /* Release buffer of tail desc */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700293 desc = GET_RBDR_DESC(rbdr, tail);
Sunil Goutham5e848e42017-05-02 18:36:51 +0530294 buf_addr = desc->buf_addr;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530295 phys_addr = nicvf_iova_to_phys(nic, buf_addr);
296 dma_unmap_page_attrs(&nic->pdev->dev, buf_addr, RCV_FRAG_LEN,
297 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
298 if (phys_addr)
299 put_page(virt_to_page(phys_to_virt(phys_addr)));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700300
Sunil Goutham5836b442017-05-02 18:36:50 +0530301 /* Sync page cache info */
302 smp_rmb();
303
304 /* Release additional page references held for recycling */
305 head = 0;
306 while (head < rbdr->pgcnt) {
307 pgcache = &rbdr->pgcache[head];
308 if (pgcache->page && page_ref_count(pgcache->page) != 0)
309 put_page(pgcache->page);
310 head++;
311 }
312
Sunil Goutham4863dea2015-05-26 19:20:15 -0700313 /* Free RBDR ring */
314 nicvf_free_q_desc_mem(nic, &rbdr->dmem);
315}
316
317/* Refill receive buffer descriptors with new buffers.
318 */
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700319static void nicvf_refill_rbdr(struct nicvf *nic, gfp_t gfp)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700320{
321 struct queue_set *qs = nic->qs;
322 int rbdr_idx = qs->rbdr_cnt;
323 int tail, qcount;
324 int refill_rb_cnt;
325 struct rbdr *rbdr;
326 struct rbdr_entry_t *desc;
Sunil Goutham927987f2017-05-02 18:36:53 +0530327 u64 rbuf;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700328 int new_rb = 0;
329
330refill:
331 if (!rbdr_idx)
332 return;
333 rbdr_idx--;
334 rbdr = &qs->rbdr[rbdr_idx];
335 /* Check if it's enabled */
336 if (!rbdr->enable)
337 goto next_rbdr;
338
339 /* Get no of desc's to be refilled */
340 qcount = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, rbdr_idx);
341 qcount &= 0x7FFFF;
342 /* Doorbell can be ringed with a max of ring size minus 1 */
343 if (qcount >= (qs->rbdr_len - 1))
344 goto next_rbdr;
345 else
346 refill_rb_cnt = qs->rbdr_len - qcount - 1;
347
Sunil Goutham5836b442017-05-02 18:36:50 +0530348 /* Sync page cache info */
349 smp_rmb();
350
Sunil Goutham4863dea2015-05-26 19:20:15 -0700351 /* Start filling descs from tail */
352 tail = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, rbdr_idx) >> 3;
353 while (refill_rb_cnt) {
354 tail++;
355 tail &= (rbdr->dmem.q_len - 1);
356
Sunil Goutham5836b442017-05-02 18:36:50 +0530357 if (nicvf_alloc_rcv_buffer(nic, rbdr, gfp, RCV_FRAG_LEN, &rbuf))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700358 break;
359
360 desc = GET_RBDR_DESC(rbdr, tail);
Sunil Goutham927987f2017-05-02 18:36:53 +0530361 desc->buf_addr = rbuf & ~(NICVF_RCV_BUF_ALIGN_BYTES - 1);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700362 refill_rb_cnt--;
363 new_rb++;
364 }
365
Sunil Goutham5c2e26f2016-03-14 16:36:14 +0530366 nicvf_get_page(nic);
367
Sunil Goutham4863dea2015-05-26 19:20:15 -0700368 /* make sure all memory stores are done before ringing doorbell */
369 smp_wmb();
370
371 /* Check if buffer allocation failed */
372 if (refill_rb_cnt)
373 nic->rb_alloc_fail = true;
374 else
375 nic->rb_alloc_fail = false;
376
377 /* Notify HW */
378 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
379 rbdr_idx, new_rb);
380next_rbdr:
381 /* Re-enable RBDR interrupts only if buffer allocation is success */
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530382 if (!nic->rb_alloc_fail && rbdr->enable &&
383 netif_running(nic->pnicvf->netdev))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700384 nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
385
386 if (rbdr_idx)
387 goto refill;
388}
389
390/* Alloc rcv buffers in non-atomic mode for better success */
391void nicvf_rbdr_work(struct work_struct *work)
392{
393 struct nicvf *nic = container_of(work, struct nicvf, rbdr_work.work);
394
395 nicvf_refill_rbdr(nic, GFP_KERNEL);
396 if (nic->rb_alloc_fail)
397 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
398 else
399 nic->rb_work_scheduled = false;
400}
401
402/* In Softirq context, alloc rcv buffers in atomic mode */
403void nicvf_rbdr_task(unsigned long data)
404{
405 struct nicvf *nic = (struct nicvf *)data;
406
407 nicvf_refill_rbdr(nic, GFP_ATOMIC);
408 if (nic->rb_alloc_fail) {
409 nic->rb_work_scheduled = true;
410 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
411 }
412}
413
414/* Initialize completion queue */
415static int nicvf_init_cmp_queue(struct nicvf *nic,
416 struct cmp_queue *cq, int q_len)
417{
418 int err;
419
420 err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
421 NICVF_CQ_BASE_ALIGN_BYTES);
422 if (err)
423 return err;
424
425 cq->desc = cq->dmem.base;
Sunil Gouthamb9687b42015-12-10 13:25:20 +0530426 cq->thresh = pass1_silicon(nic->pdev) ? 0 : CMP_QUEUE_CQE_THRESH;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700427 nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
428
429 return 0;
430}
431
432static void nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
433{
434 if (!cq)
435 return;
436 if (!cq->dmem.base)
437 return;
438
439 nicvf_free_q_desc_mem(nic, &cq->dmem);
440}
441
442/* Initialize transmit queue */
443static int nicvf_init_snd_queue(struct nicvf *nic,
444 struct snd_queue *sq, int q_len)
445{
446 int err;
447
448 err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
449 NICVF_SQ_BASE_ALIGN_BYTES);
450 if (err)
451 return err;
452
453 sq->desc = sq->dmem.base;
Aleksey Makarov86ace692015-06-02 11:00:27 -0700454 sq->skbuff = kcalloc(q_len, sizeof(u64), GFP_KERNEL);
Aleksey Makarovfa1a6c92015-06-02 11:00:26 -0700455 if (!sq->skbuff)
456 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700457 sq->head = 0;
458 sq->tail = 0;
459 atomic_set(&sq->free_cnt, q_len - 1);
460 sq->thresh = SND_QUEUE_THRESH;
461
462 /* Preallocate memory for TSO segment's header */
463 sq->tso_hdrs = dma_alloc_coherent(&nic->pdev->dev,
464 q_len * TSO_HEADER_SIZE,
465 &sq->tso_hdrs_phys, GFP_KERNEL);
466 if (!sq->tso_hdrs)
467 return -ENOMEM;
468
469 return 0;
470}
471
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530472void nicvf_unmap_sndq_buffers(struct nicvf *nic, struct snd_queue *sq,
473 int hdr_sqe, u8 subdesc_cnt)
474{
475 u8 idx;
476 struct sq_gather_subdesc *gather;
477
478 /* Unmap DMA mapped skb data buffers */
479 for (idx = 0; idx < subdesc_cnt; idx++) {
480 hdr_sqe++;
481 hdr_sqe &= (sq->dmem.q_len - 1);
482 gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, hdr_sqe);
483 /* HW will ensure data coherency, CPU sync not required */
484 dma_unmap_page_attrs(&nic->pdev->dev, gather->addr,
485 gather->size, DMA_TO_DEVICE,
486 DMA_ATTR_SKIP_CPU_SYNC);
487 }
488}
489
Sunil Goutham4863dea2015-05-26 19:20:15 -0700490static void nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
491{
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530492 struct sk_buff *skb;
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530493 struct sq_hdr_subdesc *hdr;
494 struct sq_hdr_subdesc *tso_sqe;
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530495
Sunil Goutham4863dea2015-05-26 19:20:15 -0700496 if (!sq)
497 return;
498 if (!sq->dmem.base)
499 return;
500
501 if (sq->tso_hdrs)
Sunil Goutham143ceb02015-07-29 16:49:37 +0300502 dma_free_coherent(&nic->pdev->dev,
503 sq->dmem.q_len * TSO_HEADER_SIZE,
Sunil Goutham4863dea2015-05-26 19:20:15 -0700504 sq->tso_hdrs, sq->tso_hdrs_phys);
505
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530506 /* Free pending skbs in the queue */
507 smp_rmb();
508 while (sq->head != sq->tail) {
509 skb = (struct sk_buff *)sq->skbuff[sq->head];
Sunil Goutham83abb7d2017-03-07 18:09:08 +0530510 if (!skb)
511 goto next;
512 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
513 /* Check for dummy descriptor used for HW TSO offload on 88xx */
514 if (hdr->dont_send) {
515 /* Get actual TSO descriptors and unmap them */
516 tso_sqe =
517 (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, hdr->rsvd2);
518 nicvf_unmap_sndq_buffers(nic, sq, hdr->rsvd2,
519 tso_sqe->subdesc_cnt);
520 } else {
521 nicvf_unmap_sndq_buffers(nic, sq, sq->head,
522 hdr->subdesc_cnt);
523 }
524 dev_kfree_skb_any(skb);
525next:
Sunil Gouthamc94acf82016-11-15 17:38:29 +0530526 sq->head++;
527 sq->head &= (sq->dmem.q_len - 1);
528 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700529 kfree(sq->skbuff);
530 nicvf_free_q_desc_mem(nic, &sq->dmem);
531}
532
533static void nicvf_reclaim_snd_queue(struct nicvf *nic,
534 struct queue_set *qs, int qidx)
535{
536 /* Disable send queue */
537 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
538 /* Check if SQ is stopped */
539 if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
540 return;
541 /* Reset send queue */
542 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
543}
544
545static void nicvf_reclaim_rcv_queue(struct nicvf *nic,
546 struct queue_set *qs, int qidx)
547{
548 union nic_mbx mbx = {};
549
550 /* Make sure all packets in the pipeline are written back into mem */
551 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
552 nicvf_send_msg_to_pf(nic, &mbx);
553}
554
555static void nicvf_reclaim_cmp_queue(struct nicvf *nic,
556 struct queue_set *qs, int qidx)
557{
558 /* Disable timer threshold (doesn't get reset upon CQ reset */
559 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
560 /* Disable completion queue */
561 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
562 /* Reset completion queue */
563 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
564}
565
566static void nicvf_reclaim_rbdr(struct nicvf *nic,
567 struct rbdr *rbdr, int qidx)
568{
569 u64 tmp, fifo_state;
570 int timeout = 10;
571
572 /* Save head and tail pointers for feeing up buffers */
573 rbdr->head = nicvf_queue_reg_read(nic,
574 NIC_QSET_RBDR_0_1_HEAD,
575 qidx) >> 3;
576 rbdr->tail = nicvf_queue_reg_read(nic,
577 NIC_QSET_RBDR_0_1_TAIL,
578 qidx) >> 3;
579
580 /* If RBDR FIFO is in 'FAIL' state then do a reset first
581 * before relaiming.
582 */
583 fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
584 if (((fifo_state >> 62) & 0x03) == 0x3)
585 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
586 qidx, NICVF_RBDR_RESET);
587
588 /* Disable RBDR */
589 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
590 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
591 return;
592 while (1) {
593 tmp = nicvf_queue_reg_read(nic,
594 NIC_QSET_RBDR_0_1_PREFETCH_STATUS,
595 qidx);
596 if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
597 break;
598 usleep_range(1000, 2000);
599 timeout--;
600 if (!timeout) {
601 netdev_err(nic->netdev,
602 "Failed polling on prefetch status\n");
603 return;
604 }
605 }
606 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
607 qidx, NICVF_RBDR_RESET);
608
609 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
610 return;
611 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
612 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
613 return;
614}
615
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300616void nicvf_config_vlan_stripping(struct nicvf *nic, netdev_features_t features)
617{
618 u64 rq_cfg;
619 int sqs;
620
621 rq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_RQ_GEN_CFG, 0);
622
623 /* Enable first VLAN stripping */
624 if (features & NETIF_F_HW_VLAN_CTAG_RX)
625 rq_cfg |= (1ULL << 25);
626 else
627 rq_cfg &= ~(1ULL << 25);
628 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
629
630 /* Configure Secondary Qsets, if any */
631 for (sqs = 0; sqs < nic->sqs_count; sqs++)
632 if (nic->snicvf[sqs])
633 nicvf_queue_reg_write(nic->snicvf[sqs],
634 NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
635}
636
Jerin Jacob3458c402016-08-12 16:51:39 +0530637static void nicvf_reset_rcv_queue_stats(struct nicvf *nic)
638{
639 union nic_mbx mbx = {};
640
Sunil Goutham964cb692016-11-15 17:38:16 +0530641 /* Reset all RQ/SQ and VF stats */
Jerin Jacob3458c402016-08-12 16:51:39 +0530642 mbx.reset_stat.msg = NIC_MBOX_MSG_RESET_STAT_COUNTER;
Sunil Goutham964cb692016-11-15 17:38:16 +0530643 mbx.reset_stat.rx_stat_mask = 0x3FFF;
644 mbx.reset_stat.tx_stat_mask = 0x1F;
Jerin Jacob3458c402016-08-12 16:51:39 +0530645 mbx.reset_stat.rq_stat_mask = 0xFFFF;
Sunil Goutham964cb692016-11-15 17:38:16 +0530646 mbx.reset_stat.sq_stat_mask = 0xFFFF;
Jerin Jacob3458c402016-08-12 16:51:39 +0530647 nicvf_send_msg_to_pf(nic, &mbx);
648}
649
Sunil Goutham4863dea2015-05-26 19:20:15 -0700650/* Configures receive queue */
651static void nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
652 int qidx, bool enable)
653{
654 union nic_mbx mbx = {};
655 struct rcv_queue *rq;
656 struct rq_cfg rq_cfg;
657
658 rq = &qs->rq[qidx];
659 rq->enable = enable;
660
661 /* Disable receive queue */
662 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
663
664 if (!rq->enable) {
665 nicvf_reclaim_rcv_queue(nic, qs, qidx);
666 return;
667 }
668
669 rq->cq_qs = qs->vnic_id;
670 rq->cq_idx = qidx;
671 rq->start_rbdr_qs = qs->vnic_id;
672 rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
673 rq->cont_rbdr_qs = qs->vnic_id;
674 rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
675 /* all writes of RBDR data to be loaded into L2 Cache as well*/
676 rq->caching = 1;
677
678 /* Send a mailbox msg to PF to config RQ */
679 mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
680 mbx.rq.qs_num = qs->vnic_id;
681 mbx.rq.rq_num = qidx;
682 mbx.rq.cfg = (rq->caching << 26) | (rq->cq_qs << 19) |
683 (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
684 (rq->cont_qs_rbdr_idx << 8) |
685 (rq->start_rbdr_qs << 1) | (rq->start_qs_rbdr_idx);
686 nicvf_send_msg_to_pf(nic, &mbx);
687
688 mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
Sunil Gouthamd5b2d7a2016-11-24 14:48:02 +0530689 mbx.rq.cfg = BIT_ULL(63) | BIT_ULL(62) |
690 (RQ_PASS_RBDR_LVL << 16) | (RQ_PASS_CQ_LVL << 8) |
691 (qs->vnic_id << 0);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700692 nicvf_send_msg_to_pf(nic, &mbx);
693
694 /* RQ drop config
695 * Enable CQ drop to reserve sufficient CQEs for all tx packets
696 */
697 mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
Sunil Gouthamd5b2d7a2016-11-24 14:48:02 +0530698 mbx.rq.cfg = BIT_ULL(63) | BIT_ULL(62) |
699 (RQ_PASS_RBDR_LVL << 40) | (RQ_DROP_RBDR_LVL << 32) |
700 (RQ_PASS_CQ_LVL << 16) | (RQ_DROP_CQ_LVL << 8);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700701 nicvf_send_msg_to_pf(nic, &mbx);
702
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530703 if (!nic->sqs_mode && (qidx == 0)) {
Thanneeru Srinivasulu36fa35d2017-03-07 18:09:11 +0530704 /* Enable checking L3/L4 length and TCP/UDP checksums
705 * Also allow IPv6 pkts with zero UDP checksum.
706 */
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530707 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0,
Thanneeru Srinivasulu36fa35d2017-03-07 18:09:11 +0530708 (BIT(24) | BIT(23) | BIT(21) | BIT(20)));
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300709 nicvf_config_vlan_stripping(nic, nic->netdev->features);
Sunil Gouthamcadcf952016-11-15 17:37:54 +0530710 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700711
712 /* Enable Receive queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200713 memset(&rq_cfg, 0, sizeof(struct rq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700714 rq_cfg.ena = 1;
715 rq_cfg.tcp_ena = 0;
716 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
717}
718
719/* Configures completion queue */
720void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
721 int qidx, bool enable)
722{
723 struct cmp_queue *cq;
724 struct cq_cfg cq_cfg;
725
726 cq = &qs->cq[qidx];
727 cq->enable = enable;
728
729 if (!cq->enable) {
730 nicvf_reclaim_cmp_queue(nic, qs, qidx);
731 return;
732 }
733
734 /* Reset completion queue */
735 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
736
737 if (!cq->enable)
738 return;
739
740 spin_lock_init(&cq->lock);
741 /* Set completion queue base address */
742 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
743 qidx, (u64)(cq->dmem.phys_base));
744
745 /* Enable Completion queue */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200746 memset(&cq_cfg, 0, sizeof(struct cq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700747 cq_cfg.ena = 1;
748 cq_cfg.reset = 0;
749 cq_cfg.caching = 0;
Sunil Gouthamfff4ffd2017-01-25 17:36:23 +0530750 cq_cfg.qsize = ilog2(qs->cq_len >> 10);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700751 cq_cfg.avg_con = 0;
752 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
753
754 /* Set threshold value for interrupt generation */
755 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
756 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
Sunil Goutham006394a2015-12-02 15:36:15 +0530757 qidx, CMP_QUEUE_TIMER_THRESH);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700758}
759
760/* Configures transmit queue */
761static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
762 int qidx, bool enable)
763{
764 union nic_mbx mbx = {};
765 struct snd_queue *sq;
766 struct sq_cfg sq_cfg;
767
768 sq = &qs->sq[qidx];
769 sq->enable = enable;
770
771 if (!sq->enable) {
772 nicvf_reclaim_snd_queue(nic, qs, qidx);
773 return;
774 }
775
776 /* Reset send queue */
777 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
778
779 sq->cq_qs = qs->vnic_id;
780 sq->cq_idx = qidx;
781
782 /* Send a mailbox msg to PF to config SQ */
783 mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
784 mbx.sq.qs_num = qs->vnic_id;
785 mbx.sq.sq_num = qidx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300786 mbx.sq.sqs_mode = nic->sqs_mode;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700787 mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
788 nicvf_send_msg_to_pf(nic, &mbx);
789
790 /* Set queue base address */
791 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
792 qidx, (u64)(sq->dmem.phys_base));
793
794 /* Enable send queue & set queue size */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200795 memset(&sq_cfg, 0, sizeof(struct sq_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700796 sq_cfg.ena = 1;
797 sq_cfg.reset = 0;
798 sq_cfg.ldwb = 0;
Sunil Gouthamfff4ffd2017-01-25 17:36:23 +0530799 sq_cfg.qsize = ilog2(qs->sq_len >> 10);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700800 sq_cfg.tstmp_bgx_intf = 0;
Sunil Gouthamfff4ffd2017-01-25 17:36:23 +0530801 /* CQ's level at which HW will stop processing SQEs to avoid
802 * transmitting a pkt with no space in CQ to post CQE_TX.
803 */
804 sq_cfg.cq_limit = (CMP_QUEUE_PIPELINE_RSVD * 256) / qs->cq_len;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700805 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
806
807 /* Set threshold value for interrupt generation */
808 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
809
810 /* Set queue:cpu affinity for better load distribution */
811 if (cpu_online(qidx)) {
812 cpumask_set_cpu(qidx, &sq->affinity_mask);
813 netif_set_xps_queue(nic->netdev,
814 &sq->affinity_mask, qidx);
815 }
816}
817
818/* Configures receive buffer descriptor ring */
819static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
820 int qidx, bool enable)
821{
822 struct rbdr *rbdr;
823 struct rbdr_cfg rbdr_cfg;
824
825 rbdr = &qs->rbdr[qidx];
826 nicvf_reclaim_rbdr(nic, rbdr, qidx);
827 if (!enable)
828 return;
829
830 /* Set descriptor base address */
831 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
832 qidx, (u64)(rbdr->dmem.phys_base));
833
834 /* Enable RBDR & set queue size */
835 /* Buffer size should be in multiples of 128 bytes */
xypron.glpk@gmx.de161de2c2016-05-09 00:46:18 +0200836 memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700837 rbdr_cfg.ena = 1;
838 rbdr_cfg.reset = 0;
839 rbdr_cfg.ldwb = 0;
840 rbdr_cfg.qsize = RBDR_SIZE;
841 rbdr_cfg.avg_con = 0;
842 rbdr_cfg.lines = rbdr->dma_size / 128;
843 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
844 qidx, *(u64 *)&rbdr_cfg);
845
846 /* Notify HW */
847 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
848 qidx, qs->rbdr_len - 1);
849
850 /* Set threshold value for interrupt generation */
851 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
852 qidx, rbdr->thresh - 1);
853}
854
855/* Requests PF to assign and enable Qset */
856void nicvf_qset_config(struct nicvf *nic, bool enable)
857{
858 union nic_mbx mbx = {};
859 struct queue_set *qs = nic->qs;
860 struct qs_cfg *qs_cfg;
861
862 if (!qs) {
863 netdev_warn(nic->netdev,
864 "Qset is still not allocated, don't init queues\n");
865 return;
866 }
867
868 qs->enable = enable;
869 qs->vnic_id = nic->vf_id;
870
871 /* Send a mailbox msg to PF to config Qset */
872 mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
873 mbx.qs.num = qs->vnic_id;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300874 mbx.qs.sqs_count = nic->sqs_count;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700875
876 mbx.qs.cfg = 0;
877 qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
878 if (qs->enable) {
879 qs_cfg->ena = 1;
880#ifdef __BIG_ENDIAN
881 qs_cfg->be = 1;
882#endif
883 qs_cfg->vnic = qs->vnic_id;
884 }
885 nicvf_send_msg_to_pf(nic, &mbx);
886}
887
888static void nicvf_free_resources(struct nicvf *nic)
889{
890 int qidx;
891 struct queue_set *qs = nic->qs;
892
893 /* Free receive buffer descriptor ring */
894 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
895 nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
896
897 /* Free completion queue */
898 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
899 nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
900
901 /* Free send queue */
902 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
903 nicvf_free_snd_queue(nic, &qs->sq[qidx]);
904}
905
906static int nicvf_alloc_resources(struct nicvf *nic)
907{
908 int qidx;
909 struct queue_set *qs = nic->qs;
910
911 /* Alloc receive buffer descriptor ring */
912 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
913 if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
914 DMA_BUFFER_LEN))
915 goto alloc_fail;
916 }
917
918 /* Alloc send queue */
919 for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
920 if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
921 goto alloc_fail;
922 }
923
924 /* Alloc completion queue */
925 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
926 if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
927 goto alloc_fail;
928 }
929
930 return 0;
931alloc_fail:
932 nicvf_free_resources(nic);
933 return -ENOMEM;
934}
935
936int nicvf_set_qset_resources(struct nicvf *nic)
937{
938 struct queue_set *qs;
939
940 qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
941 if (!qs)
942 return -ENOMEM;
943 nic->qs = qs;
944
945 /* Set count of each queue */
Sunil Goutham3a397eb2016-08-12 16:51:27 +0530946 qs->rbdr_cnt = DEFAULT_RBDR_CNT;
947 qs->rq_cnt = min_t(u8, MAX_RCV_QUEUES_PER_QS, num_online_cpus());
948 qs->sq_cnt = min_t(u8, MAX_SND_QUEUES_PER_QS, num_online_cpus());
949 qs->cq_cnt = max_t(u8, qs->rq_cnt, qs->sq_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700950
951 /* Set queue lengths */
952 qs->rbdr_len = RCV_BUF_COUNT;
953 qs->sq_len = SND_QUEUE_LEN;
954 qs->cq_len = CMP_QUEUE_LEN;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300955
956 nic->rx_queues = qs->rq_cnt;
957 nic->tx_queues = qs->sq_cnt;
Sunil Goutham05c773f2017-05-02 18:36:54 +0530958 nic->xdp_tx_queues = 0;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300959
Sunil Goutham4863dea2015-05-26 19:20:15 -0700960 return 0;
961}
962
963int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
964{
965 bool disable = false;
966 struct queue_set *qs = nic->qs;
Sunil Gouthamfff4ffd2017-01-25 17:36:23 +0530967 struct queue_set *pqs = nic->pnicvf->qs;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700968 int qidx;
969
970 if (!qs)
971 return 0;
972
Sunil Gouthamfff4ffd2017-01-25 17:36:23 +0530973 /* Take primary VF's queue lengths.
974 * This is needed to take queue lengths set from ethtool
975 * into consideration.
976 */
977 if (nic->sqs_mode && pqs) {
978 qs->cq_len = pqs->cq_len;
979 qs->sq_len = pqs->sq_len;
980 }
981
Sunil Goutham4863dea2015-05-26 19:20:15 -0700982 if (enable) {
983 if (nicvf_alloc_resources(nic))
984 return -ENOMEM;
985
986 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
987 nicvf_snd_queue_config(nic, qs, qidx, enable);
988 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
989 nicvf_cmp_queue_config(nic, qs, qidx, enable);
990 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
991 nicvf_rbdr_config(nic, qs, qidx, enable);
992 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
993 nicvf_rcv_queue_config(nic, qs, qidx, enable);
994 } else {
995 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
996 nicvf_rcv_queue_config(nic, qs, qidx, disable);
997 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
998 nicvf_rbdr_config(nic, qs, qidx, disable);
999 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
1000 nicvf_snd_queue_config(nic, qs, qidx, disable);
1001 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
1002 nicvf_cmp_queue_config(nic, qs, qidx, disable);
1003
1004 nicvf_free_resources(nic);
1005 }
1006
Jerin Jacob3458c402016-08-12 16:51:39 +05301007 /* Reset RXQ's stats.
1008 * SQ's stats will get reset automatically once SQ is reset.
1009 */
1010 nicvf_reset_rcv_queue_stats(nic);
1011
Sunil Goutham4863dea2015-05-26 19:20:15 -07001012 return 0;
1013}
1014
1015/* Get a free desc from SQ
1016 * returns descriptor ponter & descriptor number
1017 */
1018static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
1019{
1020 int qentry;
1021
1022 qentry = sq->tail;
1023 atomic_sub(desc_cnt, &sq->free_cnt);
1024 sq->tail += desc_cnt;
1025 sq->tail &= (sq->dmem.q_len - 1);
1026
1027 return qentry;
1028}
1029
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301030/* Rollback to previous tail pointer when descriptors not used */
1031static inline void nicvf_rollback_sq_desc(struct snd_queue *sq,
1032 int qentry, int desc_cnt)
1033{
1034 sq->tail = qentry;
1035 atomic_add(desc_cnt, &sq->free_cnt);
1036}
1037
Sunil Goutham4863dea2015-05-26 19:20:15 -07001038/* Free descriptor back to SQ for future use */
1039void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
1040{
1041 atomic_add(desc_cnt, &sq->free_cnt);
1042 sq->head += desc_cnt;
1043 sq->head &= (sq->dmem.q_len - 1);
1044}
1045
1046static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
1047{
1048 qentry++;
1049 qentry &= (sq->dmem.q_len - 1);
1050 return qentry;
1051}
1052
1053void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
1054{
1055 u64 sq_cfg;
1056
1057 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
1058 sq_cfg |= NICVF_SQ_EN;
1059 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
1060 /* Ring doorbell so that H/W restarts processing SQEs */
1061 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
1062}
1063
1064void nicvf_sq_disable(struct nicvf *nic, int qidx)
1065{
1066 u64 sq_cfg;
1067
1068 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
1069 sq_cfg &= ~NICVF_SQ_EN;
1070 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
1071}
1072
1073void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
1074 int qidx)
1075{
1076 u64 head, tail;
1077 struct sk_buff *skb;
1078 struct nicvf *nic = netdev_priv(netdev);
1079 struct sq_hdr_subdesc *hdr;
1080
1081 head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
1082 tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
1083 while (sq->head != head) {
1084 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
1085 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
1086 nicvf_put_sq_desc(sq, 1);
1087 continue;
1088 }
1089 skb = (struct sk_buff *)sq->skbuff[sq->head];
Sunil Goutham143ceb02015-07-29 16:49:37 +03001090 if (skb)
1091 dev_kfree_skb_any(skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001092 atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
1093 atomic64_add(hdr->tot_len,
1094 (atomic64_t *)&netdev->stats.tx_bytes);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001095 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
1096 }
1097}
1098
1099/* Calculate no of SQ subdescriptors needed to transmit all
1100 * segments of this TSO packet.
1101 * Taken from 'Tilera network driver' with a minor modification.
1102 */
1103static int nicvf_tso_count_subdescs(struct sk_buff *skb)
1104{
1105 struct skb_shared_info *sh = skb_shinfo(skb);
1106 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1107 unsigned int data_len = skb->len - sh_len;
1108 unsigned int p_len = sh->gso_size;
1109 long f_id = -1; /* id of the current fragment */
1110 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
1111 long f_used = 0; /* bytes used from the current fragment */
1112 long n; /* size of the current piece of payload */
1113 int num_edescs = 0;
1114 int segment;
1115
1116 for (segment = 0; segment < sh->gso_segs; segment++) {
1117 unsigned int p_used = 0;
1118
1119 /* One edesc for header and for each piece of the payload. */
1120 for (num_edescs++; p_used < p_len; num_edescs++) {
1121 /* Advance as needed. */
1122 while (f_used >= f_size) {
1123 f_id++;
1124 f_size = skb_frag_size(&sh->frags[f_id]);
1125 f_used = 0;
1126 }
1127
1128 /* Use bytes from the current fragment. */
1129 n = p_len - p_used;
1130 if (n > f_size - f_used)
1131 n = f_size - f_used;
1132 f_used += n;
1133 p_used += n;
1134 }
1135
1136 /* The last segment may be less than gso_size. */
1137 data_len -= p_len;
1138 if (data_len < p_len)
1139 p_len = data_len;
1140 }
1141
1142 /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
1143 return num_edescs + sh->gso_segs;
1144}
1145
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301146#define POST_CQE_DESC_COUNT 2
1147
Sunil Goutham4863dea2015-05-26 19:20:15 -07001148/* Get the number of SQ descriptors needed to xmit this skb */
1149static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
1150{
1151 int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
1152
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301153 if (skb_shinfo(skb)->gso_size && !nic->hw_tso) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001154 subdesc_cnt = nicvf_tso_count_subdescs(skb);
1155 return subdesc_cnt;
1156 }
1157
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301158 /* Dummy descriptors to get TSO pkt completion notification */
1159 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size)
1160 subdesc_cnt += POST_CQE_DESC_COUNT;
1161
Sunil Goutham4863dea2015-05-26 19:20:15 -07001162 if (skb_shinfo(skb)->nr_frags)
1163 subdesc_cnt += skb_shinfo(skb)->nr_frags;
1164
1165 return subdesc_cnt;
1166}
1167
1168/* Add SQ HEADER subdescriptor.
1169 * First subdescriptor for every send descriptor.
1170 */
1171static inline void
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301172nicvf_sq_add_hdr_subdesc(struct nicvf *nic, struct snd_queue *sq, int qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001173 int subdesc_cnt, struct sk_buff *skb, int len)
1174{
1175 int proto;
1176 struct sq_hdr_subdesc *hdr;
Thanneeru Srinivasulu3a9024f2017-04-06 16:12:26 +05301177 union {
1178 struct iphdr *v4;
1179 struct ipv6hdr *v6;
1180 unsigned char *hdr;
1181 } ip;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001182
Thanneeru Srinivasulu3a9024f2017-04-06 16:12:26 +05301183 ip.hdr = skb_network_header(skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001184 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001185 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
1186 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301187
1188 if (nic->t88 && nic->hw_tso && skb_shinfo(skb)->gso_size) {
1189 /* post_cqe = 0, to avoid HW posting a CQE for every TSO
1190 * segment transmitted on 88xx.
1191 */
1192 hdr->subdesc_cnt = subdesc_cnt - POST_CQE_DESC_COUNT;
1193 } else {
1194 sq->skbuff[qentry] = (u64)skb;
1195 /* Enable notification via CQE after processing SQE */
1196 hdr->post_cqe = 1;
1197 /* No of subdescriptors following this */
1198 hdr->subdesc_cnt = subdesc_cnt;
1199 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001200 hdr->tot_len = len;
1201
1202 /* Offload checksum calculation to HW */
1203 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001204 hdr->csum_l3 = 1; /* Enable IP csum calculation */
1205 hdr->l3_offset = skb_network_offset(skb);
1206 hdr->l4_offset = skb_transport_offset(skb);
1207
Thanneeru Srinivasulu3a9024f2017-04-06 16:12:26 +05301208 proto = (ip.v4->version == 4) ? ip.v4->protocol :
1209 ip.v6->nexthdr;
1210
Sunil Goutham4863dea2015-05-26 19:20:15 -07001211 switch (proto) {
1212 case IPPROTO_TCP:
1213 hdr->csum_l4 = SEND_L4_CSUM_TCP;
1214 break;
1215 case IPPROTO_UDP:
1216 hdr->csum_l4 = SEND_L4_CSUM_UDP;
1217 break;
1218 case IPPROTO_SCTP:
1219 hdr->csum_l4 = SEND_L4_CSUM_SCTP;
1220 break;
1221 }
1222 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301223
1224 if (nic->hw_tso && skb_shinfo(skb)->gso_size) {
1225 hdr->tso = 1;
1226 hdr->tso_start = skb_transport_offset(skb) + tcp_hdrlen(skb);
1227 hdr->tso_max_paysize = skb_shinfo(skb)->gso_size;
1228 /* For non-tunneled pkts, point this to L2 ethertype */
1229 hdr->inner_l3_offset = skb_network_offset(skb) - 2;
Sunil Goutham964cb692016-11-15 17:38:16 +05301230 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301231 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001232}
1233
1234/* SQ GATHER subdescriptor
1235 * Must follow HDR descriptor
1236 */
1237static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
1238 int size, u64 data)
1239{
1240 struct sq_gather_subdesc *gather;
1241
1242 qentry &= (sq->dmem.q_len - 1);
1243 gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
1244
1245 memset(gather, 0, SND_QUEUE_DESC_SIZE);
1246 gather->subdesc_type = SQ_DESC_TYPE_GATHER;
Sunil Goutham4b561c12015-07-29 16:49:36 +03001247 gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001248 gather->size = size;
1249 gather->addr = data;
1250}
1251
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301252/* Add HDR + IMMEDIATE subdescriptors right after descriptors of a TSO
1253 * packet so that a CQE is posted as a notifation for transmission of
1254 * TSO packet.
1255 */
1256static inline void nicvf_sq_add_cqe_subdesc(struct snd_queue *sq, int qentry,
1257 int tso_sqe, struct sk_buff *skb)
1258{
1259 struct sq_imm_subdesc *imm;
1260 struct sq_hdr_subdesc *hdr;
1261
1262 sq->skbuff[qentry] = (u64)skb;
1263
1264 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
1265 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
1266 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
1267 /* Enable notification via CQE after processing SQE */
1268 hdr->post_cqe = 1;
1269 /* There is no packet to transmit here */
1270 hdr->dont_send = 1;
1271 hdr->subdesc_cnt = POST_CQE_DESC_COUNT - 1;
1272 hdr->tot_len = 1;
1273 /* Actual TSO header SQE index, needed for cleanup */
1274 hdr->rsvd2 = tso_sqe;
1275
1276 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1277 imm = (struct sq_imm_subdesc *)GET_SQ_DESC(sq, qentry);
1278 memset(imm, 0, SND_QUEUE_DESC_SIZE);
1279 imm->subdesc_type = SQ_DESC_TYPE_IMMEDIATE;
1280 imm->len = 1;
1281}
1282
Sunil Goutham2c204c22016-09-23 14:42:28 +05301283static inline void nicvf_sq_doorbell(struct nicvf *nic, struct sk_buff *skb,
1284 int sq_num, int desc_cnt)
1285{
1286 struct netdev_queue *txq;
1287
1288 txq = netdev_get_tx_queue(nic->pnicvf->netdev,
1289 skb_get_queue_mapping(skb));
1290
1291 netdev_tx_sent_queue(txq, skb->len);
1292
1293 /* make sure all memory stores are done before ringing doorbell */
1294 smp_wmb();
1295
1296 /* Inform HW to xmit all TSO segments */
1297 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
1298 sq_num, desc_cnt);
1299}
1300
Sunil Goutham4863dea2015-05-26 19:20:15 -07001301/* Segment a TSO packet into 'gso_size' segments and append
1302 * them to SQ for transfer
1303 */
1304static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
Sunil Goutham92dc8762015-08-30 12:29:15 +03001305 int sq_num, int qentry, struct sk_buff *skb)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001306{
1307 struct tso_t tso;
1308 int seg_subdescs = 0, desc_cnt = 0;
1309 int seg_len, total_len, data_left;
1310 int hdr_qentry = qentry;
1311 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1312
1313 tso_start(skb, &tso);
1314 total_len = skb->len - hdr_len;
1315 while (total_len > 0) {
1316 char *hdr;
1317
1318 /* Save Qentry for adding HDR_SUBDESC at the end */
1319 hdr_qentry = qentry;
1320
1321 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1322 total_len -= data_left;
1323
1324 /* Add segment's header */
1325 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1326 hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
1327 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1328 nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
1329 sq->tso_hdrs_phys +
1330 qentry * TSO_HEADER_SIZE);
1331 /* HDR_SUDESC + GATHER */
1332 seg_subdescs = 2;
1333 seg_len = hdr_len;
1334
1335 /* Add segment's payload fragments */
1336 while (data_left > 0) {
1337 int size;
1338
1339 size = min_t(int, tso.size, data_left);
1340
1341 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1342 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1343 virt_to_phys(tso.data));
1344 seg_subdescs++;
1345 seg_len += size;
1346
1347 data_left -= size;
1348 tso_build_data(skb, &tso, size);
1349 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301350 nicvf_sq_add_hdr_subdesc(nic, sq, hdr_qentry,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001351 seg_subdescs - 1, skb, seg_len);
Sunil Goutham143ceb02015-07-29 16:49:37 +03001352 sq->skbuff[hdr_qentry] = (u64)NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001353 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1354
1355 desc_cnt += seg_subdescs;
1356 }
1357 /* Save SKB in the last segment for freeing */
1358 sq->skbuff[hdr_qentry] = (u64)skb;
1359
Sunil Goutham2c204c22016-09-23 14:42:28 +05301360 nicvf_sq_doorbell(nic, skb, sq_num, desc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001361
Sunil Goutham964cb692016-11-15 17:38:16 +05301362 this_cpu_inc(nic->pnicvf->drv_stats->tx_tso);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001363 return 1;
1364}
1365
1366/* Append an skb to a SQ for packet transfer. */
Sunil Gouthambd3ad7d2016-12-01 18:24:28 +05301367int nicvf_sq_append_skb(struct nicvf *nic, struct snd_queue *sq,
1368 struct sk_buff *skb, u8 sq_num)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001369{
1370 int i, size;
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301371 int subdesc_cnt, hdr_sqe = 0;
Sunil Gouthambd3ad7d2016-12-01 18:24:28 +05301372 int qentry;
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301373 u64 dma_addr;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001374
1375 subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
1376 if (subdesc_cnt > atomic_read(&sq->free_cnt))
1377 goto append_fail;
1378
1379 qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
1380
1381 /* Check if its a TSO packet */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301382 if (skb_shinfo(skb)->gso_size && !nic->hw_tso)
Sunil Goutham92dc8762015-08-30 12:29:15 +03001383 return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001384
1385 /* Add SQ header subdesc */
Sunil Goutham40fb5f82015-12-10 13:25:19 +05301386 nicvf_sq_add_hdr_subdesc(nic, sq, qentry, subdesc_cnt - 1,
1387 skb, skb->len);
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301388 hdr_sqe = qentry;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001389
1390 /* Add SQ gather subdescs */
1391 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1392 size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301393 /* HW will ensure data coherency, CPU sync not required */
1394 dma_addr = dma_map_page_attrs(&nic->pdev->dev, virt_to_page(skb->data),
1395 offset_in_page(skb->data), size,
1396 DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
1397 if (dma_mapping_error(&nic->pdev->dev, dma_addr)) {
1398 nicvf_rollback_sq_desc(sq, qentry, subdesc_cnt);
1399 return 0;
1400 }
1401
1402 nicvf_sq_add_gather_subdesc(sq, qentry, size, dma_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001403
1404 /* Check for scattered buffer */
1405 if (!skb_is_nonlinear(skb))
1406 goto doorbell;
1407
1408 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1409 const struct skb_frag_struct *frag;
1410
1411 frag = &skb_shinfo(skb)->frags[i];
1412
1413 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1414 size = skb_frag_size(frag);
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301415 dma_addr = dma_map_page_attrs(&nic->pdev->dev,
1416 skb_frag_page(frag),
1417 frag->page_offset, size,
1418 DMA_TO_DEVICE,
1419 DMA_ATTR_SKIP_CPU_SYNC);
1420 if (dma_mapping_error(&nic->pdev->dev, dma_addr)) {
1421 /* Free entire chain of mapped buffers
1422 * here 'i' = frags mapped + above mapped skb->data
1423 */
1424 nicvf_unmap_sndq_buffers(nic, sq, hdr_sqe, i);
1425 nicvf_rollback_sq_desc(sq, qentry, subdesc_cnt);
1426 return 0;
1427 }
1428 nicvf_sq_add_gather_subdesc(sq, qentry, size, dma_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001429 }
1430
1431doorbell:
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301432 if (nic->t88 && skb_shinfo(skb)->gso_size) {
1433 qentry = nicvf_get_nxt_sqentry(sq, qentry);
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301434 nicvf_sq_add_cqe_subdesc(sq, qentry, hdr_sqe, skb);
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301435 }
1436
Sunil Goutham2c204c22016-09-23 14:42:28 +05301437 nicvf_sq_doorbell(nic, skb, sq_num, subdesc_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001438
Sunil Goutham4863dea2015-05-26 19:20:15 -07001439 return 1;
1440
1441append_fail:
Sunil Goutham92dc8762015-08-30 12:29:15 +03001442 /* Use original PCI dev for debug log */
1443 nic = nic->pnicvf;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001444 netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
1445 return 0;
1446}
1447
1448static inline unsigned frag_num(unsigned i)
1449{
1450#ifdef __BIG_ENDIAN
1451 return (i & ~3) + 3 - (i & 3);
1452#else
1453 return i;
1454#endif
1455}
1456
1457/* Returns SKB for a received packet */
1458struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
1459{
1460 int frag;
1461 int payload_len = 0;
1462 struct sk_buff *skb = NULL;
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301463 struct page *page;
1464 int offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001465 u16 *rb_lens = NULL;
1466 u64 *rb_ptrs = NULL;
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301467 u64 phys_addr;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001468
1469 rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
Sunil Goutham02a72bd2016-08-12 16:51:28 +05301470 /* Except 88xx pass1 on all other chips CQE_RX2_S is added to
1471 * CQE_RX at word6, hence buffer pointers move by word
1472 *
1473 * Use existing 'hw_tso' flag which will be set for all chips
1474 * except 88xx pass1 instead of a additional cache line
1475 * access (or miss) by using pci dev's revision.
1476 */
1477 if (!nic->hw_tso)
1478 rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
1479 else
1480 rb_ptrs = (void *)cqe_rx + (7 * sizeof(u64));
Sunil Goutham4863dea2015-05-26 19:20:15 -07001481
Sunil Goutham4863dea2015-05-26 19:20:15 -07001482 for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
1483 payload_len = rb_lens[frag_num(frag)];
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301484 phys_addr = nicvf_iova_to_phys(nic, *rb_ptrs);
1485 if (!phys_addr) {
1486 if (skb)
1487 dev_kfree_skb_any(skb);
1488 return NULL;
1489 }
1490
Sunil Goutham4863dea2015-05-26 19:20:15 -07001491 if (!frag) {
1492 /* First fragment */
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301493 dma_unmap_page_attrs(&nic->pdev->dev,
1494 *rb_ptrs - cqe_rx->align_pad,
1495 RCV_FRAG_LEN, DMA_FROM_DEVICE,
1496 DMA_ATTR_SKIP_CPU_SYNC);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001497 skb = nicvf_rb_ptr_to_skb(nic,
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301498 phys_addr - cqe_rx->align_pad,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001499 payload_len);
1500 if (!skb)
1501 return NULL;
1502 skb_reserve(skb, cqe_rx->align_pad);
1503 skb_put(skb, payload_len);
1504 } else {
1505 /* Add fragments */
Sunil Goutham83abb7d2017-03-07 18:09:08 +05301506 dma_unmap_page_attrs(&nic->pdev->dev, *rb_ptrs,
1507 RCV_FRAG_LEN, DMA_FROM_DEVICE,
1508 DMA_ATTR_SKIP_CPU_SYNC);
1509 page = virt_to_page(phys_to_virt(phys_addr));
1510 offset = phys_to_virt(phys_addr) - page_address(page);
Sunil Gouthama8671ac2016-08-12 16:51:37 +05301511 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
1512 offset, payload_len, RCV_FRAG_LEN);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001513 }
1514 /* Next buffer pointer */
1515 rb_ptrs++;
1516 }
1517 return skb;
1518}
1519
Yury Norovb45ceb42015-12-07 10:30:32 +05301520static u64 nicvf_int_type_to_mask(int int_type, int q_idx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001521{
1522 u64 reg_val;
1523
Sunil Goutham4863dea2015-05-26 19:20:15 -07001524 switch (int_type) {
1525 case NICVF_INTR_CQ:
1526 reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1527 break;
1528 case NICVF_INTR_SQ:
1529 reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1530 break;
1531 case NICVF_INTR_RBDR:
1532 reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1533 break;
1534 case NICVF_INTR_PKT_DROP:
1535 reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1536 break;
1537 case NICVF_INTR_TCP_TIMER:
1538 reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1539 break;
1540 case NICVF_INTR_MBOX:
1541 reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
1542 break;
1543 case NICVF_INTR_QS_ERR:
Yury Norovb45ceb42015-12-07 10:30:32 +05301544 reg_val = (1ULL << NICVF_INTR_QS_ERR_SHIFT);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001545 break;
1546 default:
Yury Norovb45ceb42015-12-07 10:30:32 +05301547 reg_val = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001548 }
1549
Yury Norovb45ceb42015-12-07 10:30:32 +05301550 return reg_val;
1551}
1552
1553/* Enable interrupt */
1554void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
1555{
1556 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1557
1558 if (!mask) {
1559 netdev_dbg(nic->netdev,
1560 "Failed to enable interrupt: unknown type\n");
1561 return;
1562 }
1563 nicvf_reg_write(nic, NIC_VF_ENA_W1S,
1564 nicvf_reg_read(nic, NIC_VF_ENA_W1S) | mask);
1565}
1566
1567/* Disable interrupt */
1568void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
1569{
1570 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1571
1572 if (!mask) {
1573 netdev_dbg(nic->netdev,
1574 "Failed to disable interrupt: unknown type\n");
1575 return;
1576 }
1577
1578 nicvf_reg_write(nic, NIC_VF_ENA_W1C, mask);
1579}
1580
1581/* Clear interrupt */
1582void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
1583{
1584 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1585
1586 if (!mask) {
1587 netdev_dbg(nic->netdev,
1588 "Failed to clear interrupt: unknown type\n");
1589 return;
1590 }
1591
1592 nicvf_reg_write(nic, NIC_VF_INT, mask);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001593}
1594
1595/* Check if interrupt is enabled */
1596int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
1597{
Yury Norovb45ceb42015-12-07 10:30:32 +05301598 u64 mask = nicvf_int_type_to_mask(int_type, q_idx);
1599 /* If interrupt type is unknown, we treat it disabled. */
1600 if (!mask) {
1601 netdev_dbg(nic->netdev,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001602 "Failed to check interrupt enable: unknown type\n");
Yury Norovb45ceb42015-12-07 10:30:32 +05301603 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001604 }
1605
Yury Norovb45ceb42015-12-07 10:30:32 +05301606 return mask & nicvf_reg_read(nic, NIC_VF_ENA_W1S);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001607}
1608
1609void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
1610{
1611 struct rcv_queue *rq;
1612
1613#define GET_RQ_STATS(reg) \
1614 nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
1615 (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1616
1617 rq = &nic->qs->rq[rq_idx];
1618 rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
1619 rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
1620}
1621
1622void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
1623{
1624 struct snd_queue *sq;
1625
1626#define GET_SQ_STATS(reg) \
1627 nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
1628 (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1629
1630 sq = &nic->qs->sq[sq_idx];
1631 sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
1632 sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
1633}
1634
1635/* Check for errors in the receive cmp.queue entry */
Sunil Gouthamad2eceb2016-02-16 16:29:51 +05301636int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001637{
Sunil Goutham4863dea2015-05-26 19:20:15 -07001638 if (netif_msg_rx_err(nic))
1639 netdev_err(nic->netdev,
1640 "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
1641 nic->netdev->name,
1642 cqe_rx->err_level, cqe_rx->err_opcode);
1643
Sunil Goutham4863dea2015-05-26 19:20:15 -07001644 switch (cqe_rx->err_opcode) {
1645 case CQ_RX_ERROP_RE_PARTIAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301646 this_cpu_inc(nic->drv_stats->rx_bgx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001647 break;
1648 case CQ_RX_ERROP_RE_JABBER:
Sunil Goutham964cb692016-11-15 17:38:16 +05301649 this_cpu_inc(nic->drv_stats->rx_jabber_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001650 break;
1651 case CQ_RX_ERROP_RE_FCS:
Sunil Goutham964cb692016-11-15 17:38:16 +05301652 this_cpu_inc(nic->drv_stats->rx_fcs_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001653 break;
1654 case CQ_RX_ERROP_RE_RX_CTL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301655 this_cpu_inc(nic->drv_stats->rx_bgx_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001656 break;
1657 case CQ_RX_ERROP_PREL2_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301658 this_cpu_inc(nic->drv_stats->rx_prel2_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001659 break;
1660 case CQ_RX_ERROP_L2_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301661 this_cpu_inc(nic->drv_stats->rx_l2_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001662 break;
1663 case CQ_RX_ERROP_L2_OVERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301664 this_cpu_inc(nic->drv_stats->rx_oversize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001665 break;
1666 case CQ_RX_ERROP_L2_UNDERSIZE:
Sunil Goutham964cb692016-11-15 17:38:16 +05301667 this_cpu_inc(nic->drv_stats->rx_undersize);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001668 break;
1669 case CQ_RX_ERROP_L2_LENMISM:
Sunil Goutham964cb692016-11-15 17:38:16 +05301670 this_cpu_inc(nic->drv_stats->rx_l2_len_mismatch);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001671 break;
1672 case CQ_RX_ERROP_L2_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301673 this_cpu_inc(nic->drv_stats->rx_l2_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001674 break;
1675 case CQ_RX_ERROP_IP_NOT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301676 this_cpu_inc(nic->drv_stats->rx_ip_ver_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001677 break;
1678 case CQ_RX_ERROP_IP_CSUM_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301679 this_cpu_inc(nic->drv_stats->rx_ip_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001680 break;
1681 case CQ_RX_ERROP_IP_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301682 this_cpu_inc(nic->drv_stats->rx_ip_hdr_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001683 break;
1684 case CQ_RX_ERROP_IP_MALD:
Sunil Goutham964cb692016-11-15 17:38:16 +05301685 this_cpu_inc(nic->drv_stats->rx_ip_payload_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001686 break;
1687 case CQ_RX_ERROP_IP_HOP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301688 this_cpu_inc(nic->drv_stats->rx_ip_ttl_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001689 break;
1690 case CQ_RX_ERROP_L3_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301691 this_cpu_inc(nic->drv_stats->rx_l3_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001692 break;
1693 case CQ_RX_ERROP_L4_MAL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301694 this_cpu_inc(nic->drv_stats->rx_l4_malformed);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001695 break;
1696 case CQ_RX_ERROP_L4_CHK:
Sunil Goutham964cb692016-11-15 17:38:16 +05301697 this_cpu_inc(nic->drv_stats->rx_l4_csum_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001698 break;
1699 case CQ_RX_ERROP_UDP_LEN:
Sunil Goutham964cb692016-11-15 17:38:16 +05301700 this_cpu_inc(nic->drv_stats->rx_udp_len_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001701 break;
1702 case CQ_RX_ERROP_L4_PORT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301703 this_cpu_inc(nic->drv_stats->rx_l4_port_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001704 break;
1705 case CQ_RX_ERROP_TCP_FLAG:
Sunil Goutham964cb692016-11-15 17:38:16 +05301706 this_cpu_inc(nic->drv_stats->rx_tcp_flag_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001707 break;
1708 case CQ_RX_ERROP_TCP_OFFSET:
Sunil Goutham964cb692016-11-15 17:38:16 +05301709 this_cpu_inc(nic->drv_stats->rx_tcp_offset_errs);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001710 break;
1711 case CQ_RX_ERROP_L4_PCLP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301712 this_cpu_inc(nic->drv_stats->rx_l4_pclp);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001713 break;
1714 case CQ_RX_ERROP_RBDR_TRUNC:
Sunil Goutham964cb692016-11-15 17:38:16 +05301715 this_cpu_inc(nic->drv_stats->rx_truncated_pkts);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001716 break;
1717 }
1718
1719 return 1;
1720}
1721
1722/* Check for errors in the send cmp.queue entry */
Sunil Goutham964cb692016-11-15 17:38:16 +05301723int nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cqe_send_t *cqe_tx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001724{
Sunil Goutham4863dea2015-05-26 19:20:15 -07001725 switch (cqe_tx->send_status) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001726 case CQ_TX_ERROP_DESC_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301727 this_cpu_inc(nic->drv_stats->tx_desc_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001728 break;
1729 case CQ_TX_ERROP_HDR_CONS_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301730 this_cpu_inc(nic->drv_stats->tx_hdr_cons_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001731 break;
1732 case CQ_TX_ERROP_SUBDC_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301733 this_cpu_inc(nic->drv_stats->tx_subdesc_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001734 break;
Sunil Goutham712c3182016-11-15 17:37:36 +05301735 case CQ_TX_ERROP_MAX_SIZE_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301736 this_cpu_inc(nic->drv_stats->tx_max_size_exceeded);
Sunil Goutham712c3182016-11-15 17:37:36 +05301737 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001738 case CQ_TX_ERROP_IMM_SIZE_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301739 this_cpu_inc(nic->drv_stats->tx_imm_size_oflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001740 break;
1741 case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301742 this_cpu_inc(nic->drv_stats->tx_data_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001743 break;
1744 case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
Sunil Goutham964cb692016-11-15 17:38:16 +05301745 this_cpu_inc(nic->drv_stats->tx_mem_seq_err);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001746 break;
1747 case CQ_TX_ERROP_LOCK_VIOL:
Sunil Goutham964cb692016-11-15 17:38:16 +05301748 this_cpu_inc(nic->drv_stats->tx_lock_viol);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001749 break;
1750 case CQ_TX_ERROP_DATA_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301751 this_cpu_inc(nic->drv_stats->tx_data_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001752 break;
1753 case CQ_TX_ERROP_TSTMP_CONFLICT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301754 this_cpu_inc(nic->drv_stats->tx_tstmp_conflict);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001755 break;
1756 case CQ_TX_ERROP_TSTMP_TIMEOUT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301757 this_cpu_inc(nic->drv_stats->tx_tstmp_timeout);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001758 break;
1759 case CQ_TX_ERROP_MEM_FAULT:
Sunil Goutham964cb692016-11-15 17:38:16 +05301760 this_cpu_inc(nic->drv_stats->tx_mem_fault);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001761 break;
1762 case CQ_TX_ERROP_CK_OVERLAP:
Sunil Goutham964cb692016-11-15 17:38:16 +05301763 this_cpu_inc(nic->drv_stats->tx_csum_overlap);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001764 break;
1765 case CQ_TX_ERROP_CK_OFLOW:
Sunil Goutham964cb692016-11-15 17:38:16 +05301766 this_cpu_inc(nic->drv_stats->tx_csum_overflow);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001767 break;
1768 }
1769
1770 return 1;
1771}