1 /*
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
21 struct rbuf_info {
22 struct page *page;
23 void *data;
24 u64 offset;
25 };
26
27 #define GET_RBUF_INFO(x) ((struct rbuf_info *)(x - NICVF_RCV_BUF_ALIGN_BYTES))
28
29 /* Poll a register for a specific value */
nicvf_poll_reg(struct nicvf * nic,int qidx,u64 reg,int bit_pos,int bits,int val)30 static int nicvf_poll_reg(struct nicvf *nic, int qidx,
31 u64 reg, int bit_pos, int bits, int val)
32 {
33 u64 bit_mask;
34 u64 reg_val;
35 int timeout = 10;
36
37 bit_mask = (1ULL << bits) - 1;
38 bit_mask = (bit_mask << bit_pos);
39
40 while (timeout) {
41 reg_val = nicvf_queue_reg_read(nic, reg, qidx);
42 if (((reg_val & bit_mask) >> bit_pos) == val)
43 return 0;
44 usleep_range(1000, 2000);
45 timeout--;
46 }
47 netdev_err(nic->netdev, "Poll on reg 0x%llx failed\n", reg);
48 return 1;
49 }
50
51 /* Allocate memory for a queue's descriptors */
nicvf_alloc_q_desc_mem(struct nicvf * nic,struct q_desc_mem * dmem,int q_len,int desc_size,int align_bytes)52 static int nicvf_alloc_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem,
53 int q_len, int desc_size, int align_bytes)
54 {
55 dmem->q_len = q_len;
56 dmem->size = (desc_size * q_len) + align_bytes;
57 /* Save address, need it while freeing */
58 dmem->unalign_base = dma_zalloc_coherent(&nic->pdev->dev, dmem->size,
59 &dmem->dma, GFP_KERNEL);
60 if (!dmem->unalign_base)
61 return -ENOMEM;
62
63 /* Align memory address for 'align_bytes' */
64 dmem->phys_base = NICVF_ALIGNED_ADDR((u64)dmem->dma, align_bytes);
65 dmem->base = dmem->unalign_base + (dmem->phys_base - dmem->dma);
66 return 0;
67 }
68
69 /* Free queue's descriptor memory */
nicvf_free_q_desc_mem(struct nicvf * nic,struct q_desc_mem * dmem)70 static void nicvf_free_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem)
71 {
72 if (!dmem)
73 return;
74
75 dma_free_coherent(&nic->pdev->dev, dmem->size,
76 dmem->unalign_base, dmem->dma);
77 dmem->unalign_base = NULL;
78 dmem->base = NULL;
79 }
80
81 /* Allocate buffer for packet reception
82 * HW returns memory address where packet is DMA'ed but not a pointer
83 * into RBDR ring, so save buffer address at the start of fragment and
84 * align the start address to a cache aligned address
85 */
nicvf_alloc_rcv_buffer(struct nicvf * nic,gfp_t gfp,u32 buf_len,u64 ** rbuf)86 static inline int nicvf_alloc_rcv_buffer(struct nicvf *nic, gfp_t gfp,
87 u32 buf_len, u64 **rbuf)
88 {
89 u64 data;
90 struct rbuf_info *rinfo;
91 int order = get_order(buf_len);
92
93 /* Check if request can be accomodated in previous allocated page */
94 if (nic->rb_page) {
95 if ((nic->rb_page_offset + buf_len + buf_len) >
96 (PAGE_SIZE << order)) {
97 nic->rb_page = NULL;
98 } else {
99 nic->rb_page_offset += buf_len;
100 get_page(nic->rb_page);
101 }
102 }
103
104 /* Allocate a new page */
105 if (!nic->rb_page) {
106 nic->rb_page = alloc_pages(gfp | __GFP_COMP | __GFP_NOWARN,
107 order);
108 if (!nic->rb_page) {
109 netdev_err(nic->netdev,
110 "Failed to allocate new rcv buffer\n");
111 return -ENOMEM;
112 }
113 nic->rb_page_offset = 0;
114 }
115
116 data = (u64)page_address(nic->rb_page) + nic->rb_page_offset;
117
118 /* Align buffer addr to cache line i.e 128 bytes */
119 rinfo = (struct rbuf_info *)(data + NICVF_RCV_BUF_ALIGN_LEN(data));
120 /* Save page address for reference updation */
121 rinfo->page = nic->rb_page;
122 /* Store start address for later retrieval */
123 rinfo->data = (void *)data;
124 /* Store alignment offset */
125 rinfo->offset = NICVF_RCV_BUF_ALIGN_LEN(data);
126
127 data += rinfo->offset;
128
129 /* Give next aligned address to hw for DMA */
130 *rbuf = (u64 *)(data + NICVF_RCV_BUF_ALIGN_BYTES);
131 return 0;
132 }
133
134 /* Retrieve actual buffer start address and build skb for received packet */
nicvf_rb_ptr_to_skb(struct nicvf * nic,u64 rb_ptr,int len)135 static struct sk_buff *nicvf_rb_ptr_to_skb(struct nicvf *nic,
136 u64 rb_ptr, int len)
137 {
138 struct sk_buff *skb;
139 struct rbuf_info *rinfo;
140
141 rb_ptr = (u64)phys_to_virt(rb_ptr);
142 /* Get buffer start address and alignment offset */
143 rinfo = GET_RBUF_INFO(rb_ptr);
144
145 /* Now build an skb to give to stack */
146 skb = build_skb(rinfo->data, RCV_FRAG_LEN);
147 if (!skb) {
148 put_page(rinfo->page);
149 return NULL;
150 }
151
152 /* Set correct skb->data */
153 skb_reserve(skb, rinfo->offset + NICVF_RCV_BUF_ALIGN_BYTES);
154
155 prefetch((void *)rb_ptr);
156 return skb;
157 }
158
159 /* Allocate RBDR ring and populate receive buffers */
nicvf_init_rbdr(struct nicvf * nic,struct rbdr * rbdr,int ring_len,int buf_size)160 static int nicvf_init_rbdr(struct nicvf *nic, struct rbdr *rbdr,
161 int ring_len, int buf_size)
162 {
163 int idx;
164 u64 *rbuf;
165 struct rbdr_entry_t *desc;
166 int err;
167
168 err = nicvf_alloc_q_desc_mem(nic, &rbdr->dmem, ring_len,
169 sizeof(struct rbdr_entry_t),
170 NICVF_RCV_BUF_ALIGN_BYTES);
171 if (err)
172 return err;
173
174 rbdr->desc = rbdr->dmem.base;
175 /* Buffer size has to be in multiples of 128 bytes */
176 rbdr->dma_size = buf_size;
177 rbdr->enable = true;
178 rbdr->thresh = RBDR_THRESH;
179
180 nic->rb_page = NULL;
181 for (idx = 0; idx < ring_len; idx++) {
182 err = nicvf_alloc_rcv_buffer(nic, GFP_KERNEL, RCV_FRAG_LEN,
183 &rbuf);
184 if (err)
185 return err;
186
187 desc = GET_RBDR_DESC(rbdr, idx);
188 desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
189 }
190 return 0;
191 }
192
193 /* Free RBDR ring and its receive buffers */
nicvf_free_rbdr(struct nicvf * nic,struct rbdr * rbdr)194 static void nicvf_free_rbdr(struct nicvf *nic, struct rbdr *rbdr)
195 {
196 int head, tail;
197 u64 buf_addr;
198 struct rbdr_entry_t *desc;
199 struct rbuf_info *rinfo;
200
201 if (!rbdr)
202 return;
203
204 rbdr->enable = false;
205 if (!rbdr->dmem.base)
206 return;
207
208 head = rbdr->head;
209 tail = rbdr->tail;
210
211 /* Free SKBs */
212 while (head != tail) {
213 desc = GET_RBDR_DESC(rbdr, head);
214 buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
215 rinfo = GET_RBUF_INFO((u64)phys_to_virt(buf_addr));
216 put_page(rinfo->page);
217 head++;
218 head &= (rbdr->dmem.q_len - 1);
219 }
220 /* Free SKB of tail desc */
221 desc = GET_RBDR_DESC(rbdr, tail);
222 buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
223 rinfo = GET_RBUF_INFO((u64)phys_to_virt(buf_addr));
224 put_page(rinfo->page);
225
226 /* Free RBDR ring */
227 nicvf_free_q_desc_mem(nic, &rbdr->dmem);
228 }
229
230 /* Refill receive buffer descriptors with new buffers.
231 */
nicvf_refill_rbdr(struct nicvf * nic,gfp_t gfp)232 static void nicvf_refill_rbdr(struct nicvf *nic, gfp_t gfp)
233 {
234 struct queue_set *qs = nic->qs;
235 int rbdr_idx = qs->rbdr_cnt;
236 int tail, qcount;
237 int refill_rb_cnt;
238 struct rbdr *rbdr;
239 struct rbdr_entry_t *desc;
240 u64 *rbuf;
241 int new_rb = 0;
242
243 refill:
244 if (!rbdr_idx)
245 return;
246 rbdr_idx--;
247 rbdr = &qs->rbdr[rbdr_idx];
248 /* Check if it's enabled */
249 if (!rbdr->enable)
250 goto next_rbdr;
251
252 /* Get no of desc's to be refilled */
253 qcount = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, rbdr_idx);
254 qcount &= 0x7FFFF;
255 /* Doorbell can be ringed with a max of ring size minus 1 */
256 if (qcount >= (qs->rbdr_len - 1))
257 goto next_rbdr;
258 else
259 refill_rb_cnt = qs->rbdr_len - qcount - 1;
260
261 /* Start filling descs from tail */
262 tail = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, rbdr_idx) >> 3;
263 while (refill_rb_cnt) {
264 tail++;
265 tail &= (rbdr->dmem.q_len - 1);
266
267 if (nicvf_alloc_rcv_buffer(nic, gfp, RCV_FRAG_LEN, &rbuf))
268 break;
269
270 desc = GET_RBDR_DESC(rbdr, tail);
271 desc->buf_addr = virt_to_phys(rbuf) >> NICVF_RCV_BUF_ALIGN;
272 refill_rb_cnt--;
273 new_rb++;
274 }
275
276 /* make sure all memory stores are done before ringing doorbell */
277 smp_wmb();
278
279 /* Check if buffer allocation failed */
280 if (refill_rb_cnt)
281 nic->rb_alloc_fail = true;
282 else
283 nic->rb_alloc_fail = false;
284
285 /* Notify HW */
286 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
287 rbdr_idx, new_rb);
288 next_rbdr:
289 /* Re-enable RBDR interrupts only if buffer allocation is success */
290 if (!nic->rb_alloc_fail && rbdr->enable)
291 nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
292
293 if (rbdr_idx)
294 goto refill;
295 }
296
297 /* Alloc rcv buffers in non-atomic mode for better success */
nicvf_rbdr_work(struct work_struct * work)298 void nicvf_rbdr_work(struct work_struct *work)
299 {
300 struct nicvf *nic = container_of(work, struct nicvf, rbdr_work.work);
301
302 nicvf_refill_rbdr(nic, GFP_KERNEL);
303 if (nic->rb_alloc_fail)
304 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
305 else
306 nic->rb_work_scheduled = false;
307 }
308
309 /* In Softirq context, alloc rcv buffers in atomic mode */
nicvf_rbdr_task(unsigned long data)310 void nicvf_rbdr_task(unsigned long data)
311 {
312 struct nicvf *nic = (struct nicvf *)data;
313
314 nicvf_refill_rbdr(nic, GFP_ATOMIC);
315 if (nic->rb_alloc_fail) {
316 nic->rb_work_scheduled = true;
317 schedule_delayed_work(&nic->rbdr_work, msecs_to_jiffies(10));
318 }
319 }
320
321 /* Initialize completion queue */
nicvf_init_cmp_queue(struct nicvf * nic,struct cmp_queue * cq,int q_len)322 static int nicvf_init_cmp_queue(struct nicvf *nic,
323 struct cmp_queue *cq, int q_len)
324 {
325 int err;
326
327 err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
328 NICVF_CQ_BASE_ALIGN_BYTES);
329 if (err)
330 return err;
331
332 cq->desc = cq->dmem.base;
333 cq->thresh = CMP_QUEUE_CQE_THRESH;
334 nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
335
336 return 0;
337 }
338
nicvf_free_cmp_queue(struct nicvf * nic,struct cmp_queue * cq)339 static void nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
340 {
341 if (!cq)
342 return;
343 if (!cq->dmem.base)
344 return;
345
346 nicvf_free_q_desc_mem(nic, &cq->dmem);
347 }
348
349 /* Initialize transmit queue */
nicvf_init_snd_queue(struct nicvf * nic,struct snd_queue * sq,int q_len)350 static int nicvf_init_snd_queue(struct nicvf *nic,
351 struct snd_queue *sq, int q_len)
352 {
353 int err;
354
355 err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
356 NICVF_SQ_BASE_ALIGN_BYTES);
357 if (err)
358 return err;
359
360 sq->desc = sq->dmem.base;
361 sq->skbuff = kcalloc(q_len, sizeof(u64), GFP_KERNEL);
362 if (!sq->skbuff)
363 return -ENOMEM;
364 sq->head = 0;
365 sq->tail = 0;
366 atomic_set(&sq->free_cnt, q_len - 1);
367 sq->thresh = SND_QUEUE_THRESH;
368
369 /* Preallocate memory for TSO segment's header */
370 sq->tso_hdrs = dma_alloc_coherent(&nic->pdev->dev,
371 q_len * TSO_HEADER_SIZE,
372 &sq->tso_hdrs_phys, GFP_KERNEL);
373 if (!sq->tso_hdrs)
374 return -ENOMEM;
375
376 return 0;
377 }
378
nicvf_free_snd_queue(struct nicvf * nic,struct snd_queue * sq)379 static void nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
380 {
381 if (!sq)
382 return;
383 if (!sq->dmem.base)
384 return;
385
386 if (sq->tso_hdrs)
387 dma_free_coherent(&nic->pdev->dev,
388 sq->dmem.q_len * TSO_HEADER_SIZE,
389 sq->tso_hdrs, sq->tso_hdrs_phys);
390
391 kfree(sq->skbuff);
392 nicvf_free_q_desc_mem(nic, &sq->dmem);
393 }
394
nicvf_reclaim_snd_queue(struct nicvf * nic,struct queue_set * qs,int qidx)395 static void nicvf_reclaim_snd_queue(struct nicvf *nic,
396 struct queue_set *qs, int qidx)
397 {
398 /* Disable send queue */
399 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
400 /* Check if SQ is stopped */
401 if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
402 return;
403 /* Reset send queue */
404 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
405 }
406
nicvf_reclaim_rcv_queue(struct nicvf * nic,struct queue_set * qs,int qidx)407 static void nicvf_reclaim_rcv_queue(struct nicvf *nic,
408 struct queue_set *qs, int qidx)
409 {
410 union nic_mbx mbx = {};
411
412 /* Make sure all packets in the pipeline are written back into mem */
413 mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
414 nicvf_send_msg_to_pf(nic, &mbx);
415 }
416
nicvf_reclaim_cmp_queue(struct nicvf * nic,struct queue_set * qs,int qidx)417 static void nicvf_reclaim_cmp_queue(struct nicvf *nic,
418 struct queue_set *qs, int qidx)
419 {
420 /* Disable timer threshold (doesn't get reset upon CQ reset */
421 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
422 /* Disable completion queue */
423 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
424 /* Reset completion queue */
425 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
426 }
427
nicvf_reclaim_rbdr(struct nicvf * nic,struct rbdr * rbdr,int qidx)428 static void nicvf_reclaim_rbdr(struct nicvf *nic,
429 struct rbdr *rbdr, int qidx)
430 {
431 u64 tmp, fifo_state;
432 int timeout = 10;
433
434 /* Save head and tail pointers for feeing up buffers */
435 rbdr->head = nicvf_queue_reg_read(nic,
436 NIC_QSET_RBDR_0_1_HEAD,
437 qidx) >> 3;
438 rbdr->tail = nicvf_queue_reg_read(nic,
439 NIC_QSET_RBDR_0_1_TAIL,
440 qidx) >> 3;
441
442 /* If RBDR FIFO is in 'FAIL' state then do a reset first
443 * before relaiming.
444 */
445 fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
446 if (((fifo_state >> 62) & 0x03) == 0x3)
447 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
448 qidx, NICVF_RBDR_RESET);
449
450 /* Disable RBDR */
451 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
452 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
453 return;
454 while (1) {
455 tmp = nicvf_queue_reg_read(nic,
456 NIC_QSET_RBDR_0_1_PREFETCH_STATUS,
457 qidx);
458 if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
459 break;
460 usleep_range(1000, 2000);
461 timeout--;
462 if (!timeout) {
463 netdev_err(nic->netdev,
464 "Failed polling on prefetch status\n");
465 return;
466 }
467 }
468 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
469 qidx, NICVF_RBDR_RESET);
470
471 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
472 return;
473 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
474 if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
475 return;
476 }
477
nicvf_config_vlan_stripping(struct nicvf * nic,netdev_features_t features)478 void nicvf_config_vlan_stripping(struct nicvf *nic, netdev_features_t features)
479 {
480 u64 rq_cfg;
481 int sqs;
482
483 rq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_RQ_GEN_CFG, 0);
484
485 /* Enable first VLAN stripping */
486 if (features & NETIF_F_HW_VLAN_CTAG_RX)
487 rq_cfg |= (1ULL << 25);
488 else
489 rq_cfg &= ~(1ULL << 25);
490 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
491
492 /* Configure Secondary Qsets, if any */
493 for (sqs = 0; sqs < nic->sqs_count; sqs++)
494 if (nic->snicvf[sqs])
495 nicvf_queue_reg_write(nic->snicvf[sqs],
496 NIC_QSET_RQ_GEN_CFG, 0, rq_cfg);
497 }
498
499 /* Configures receive queue */
nicvf_rcv_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,bool enable)500 static void nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
501 int qidx, bool enable)
502 {
503 union nic_mbx mbx = {};
504 struct rcv_queue *rq;
505 struct rq_cfg rq_cfg;
506
507 rq = &qs->rq[qidx];
508 rq->enable = enable;
509
510 /* Disable receive queue */
511 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
512
513 if (!rq->enable) {
514 nicvf_reclaim_rcv_queue(nic, qs, qidx);
515 return;
516 }
517
518 rq->cq_qs = qs->vnic_id;
519 rq->cq_idx = qidx;
520 rq->start_rbdr_qs = qs->vnic_id;
521 rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
522 rq->cont_rbdr_qs = qs->vnic_id;
523 rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
524 /* all writes of RBDR data to be loaded into L2 Cache as well*/
525 rq->caching = 1;
526
527 /* Send a mailbox msg to PF to config RQ */
528 mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
529 mbx.rq.qs_num = qs->vnic_id;
530 mbx.rq.rq_num = qidx;
531 mbx.rq.cfg = ((u64)rq->caching << 26) | (rq->cq_qs << 19) |
532 (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
533 (rq->cont_qs_rbdr_idx << 8) |
534 (rq->start_rbdr_qs << 1) | (rq->start_qs_rbdr_idx);
535 nicvf_send_msg_to_pf(nic, &mbx);
536
537 mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
538 mbx.rq.cfg = (1ULL << 63) | (1ULL << 62) | (qs->vnic_id << 0);
539 nicvf_send_msg_to_pf(nic, &mbx);
540
541 /* RQ drop config
542 * Enable CQ drop to reserve sufficient CQEs for all tx packets
543 */
544 mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
545 mbx.rq.cfg = (1ULL << 62) | (RQ_CQ_DROP << 8);
546 nicvf_send_msg_to_pf(nic, &mbx);
547
548 nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, 0x00);
549 if (!nic->sqs_mode)
550 nicvf_config_vlan_stripping(nic, nic->netdev->features);
551
552 /* Enable Receive queue */
553 memset(&rq_cfg, 0, sizeof(struct rq_cfg));
554 rq_cfg.ena = 1;
555 rq_cfg.tcp_ena = 0;
556 nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, *(u64 *)&rq_cfg);
557 }
558
559 /* Configures completion queue */
nicvf_cmp_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,bool enable)560 void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
561 int qidx, bool enable)
562 {
563 struct cmp_queue *cq;
564 struct cq_cfg cq_cfg;
565
566 cq = &qs->cq[qidx];
567 cq->enable = enable;
568
569 if (!cq->enable) {
570 nicvf_reclaim_cmp_queue(nic, qs, qidx);
571 return;
572 }
573
574 /* Reset completion queue */
575 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
576
577 if (!cq->enable)
578 return;
579
580 spin_lock_init(&cq->lock);
581 /* Set completion queue base address */
582 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE,
583 qidx, (u64)(cq->dmem.phys_base));
584
585 /* Enable Completion queue */
586 memset(&cq_cfg, 0, sizeof(struct cq_cfg));
587 cq_cfg.ena = 1;
588 cq_cfg.reset = 0;
589 cq_cfg.caching = 0;
590 cq_cfg.qsize = CMP_QSIZE;
591 cq_cfg.avg_con = 0;
592 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(u64 *)&cq_cfg);
593
594 /* Set threshold value for interrupt generation */
595 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
596 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2,
597 qidx, CMP_QUEUE_TIMER_THRESH);
598 }
599
600 /* Configures transmit queue */
nicvf_snd_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,bool enable)601 static void nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs,
602 int qidx, bool enable)
603 {
604 union nic_mbx mbx = {};
605 struct snd_queue *sq;
606 struct sq_cfg sq_cfg;
607
608 sq = &qs->sq[qidx];
609 sq->enable = enable;
610
611 if (!sq->enable) {
612 nicvf_reclaim_snd_queue(nic, qs, qidx);
613 return;
614 }
615
616 /* Reset send queue */
617 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
618
619 sq->cq_qs = qs->vnic_id;
620 sq->cq_idx = qidx;
621
622 /* Send a mailbox msg to PF to config SQ */
623 mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
624 mbx.sq.qs_num = qs->vnic_id;
625 mbx.sq.sq_num = qidx;
626 mbx.sq.sqs_mode = nic->sqs_mode;
627 mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
628 nicvf_send_msg_to_pf(nic, &mbx);
629
630 /* Set queue base address */
631 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE,
632 qidx, (u64)(sq->dmem.phys_base));
633
634 /* Enable send queue & set queue size */
635 memset(&sq_cfg, 0, sizeof(struct sq_cfg));
636 sq_cfg.ena = 1;
637 sq_cfg.reset = 0;
638 sq_cfg.ldwb = 0;
639 sq_cfg.qsize = SND_QSIZE;
640 sq_cfg.tstmp_bgx_intf = 0;
641 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(u64 *)&sq_cfg);
642
643 /* Set threshold value for interrupt generation */
644 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
645
646 /* Set queue:cpu affinity for better load distribution */
647 if (cpu_online(qidx)) {
648 cpumask_set_cpu(qidx, &sq->affinity_mask);
649 netif_set_xps_queue(nic->netdev,
650 &sq->affinity_mask, qidx);
651 }
652 }
653
654 /* Configures receive buffer descriptor ring */
nicvf_rbdr_config(struct nicvf * nic,struct queue_set * qs,int qidx,bool enable)655 static void nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs,
656 int qidx, bool enable)
657 {
658 struct rbdr *rbdr;
659 struct rbdr_cfg rbdr_cfg;
660
661 rbdr = &qs->rbdr[qidx];
662 nicvf_reclaim_rbdr(nic, rbdr, qidx);
663 if (!enable)
664 return;
665
666 /* Set descriptor base address */
667 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE,
668 qidx, (u64)(rbdr->dmem.phys_base));
669
670 /* Enable RBDR & set queue size */
671 /* Buffer size should be in multiples of 128 bytes */
672 memset(&rbdr_cfg, 0, sizeof(struct rbdr_cfg));
673 rbdr_cfg.ena = 1;
674 rbdr_cfg.reset = 0;
675 rbdr_cfg.ldwb = 0;
676 rbdr_cfg.qsize = RBDR_SIZE;
677 rbdr_cfg.avg_con = 0;
678 rbdr_cfg.lines = rbdr->dma_size / 128;
679 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
680 qidx, *(u64 *)&rbdr_cfg);
681
682 /* Notify HW */
683 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
684 qidx, qs->rbdr_len - 1);
685
686 /* Set threshold value for interrupt generation */
687 nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH,
688 qidx, rbdr->thresh - 1);
689 }
690
691 /* Requests PF to assign and enable Qset */
nicvf_qset_config(struct nicvf * nic,bool enable)692 void nicvf_qset_config(struct nicvf *nic, bool enable)
693 {
694 union nic_mbx mbx = {};
695 struct queue_set *qs = nic->qs;
696 struct qs_cfg *qs_cfg;
697
698 if (!qs) {
699 netdev_warn(nic->netdev,
700 "Qset is still not allocated, don't init queues\n");
701 return;
702 }
703
704 qs->enable = enable;
705 qs->vnic_id = nic->vf_id;
706
707 /* Send a mailbox msg to PF to config Qset */
708 mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
709 mbx.qs.num = qs->vnic_id;
710 mbx.qs.sqs_count = nic->sqs_count;
711
712 mbx.qs.cfg = 0;
713 qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
714 if (qs->enable) {
715 qs_cfg->ena = 1;
716 #ifdef __BIG_ENDIAN
717 qs_cfg->be = 1;
718 #endif
719 qs_cfg->vnic = qs->vnic_id;
720 }
721 nicvf_send_msg_to_pf(nic, &mbx);
722 }
723
nicvf_free_resources(struct nicvf * nic)724 static void nicvf_free_resources(struct nicvf *nic)
725 {
726 int qidx;
727 struct queue_set *qs = nic->qs;
728
729 /* Free receive buffer descriptor ring */
730 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
731 nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
732
733 /* Free completion queue */
734 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
735 nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
736
737 /* Free send queue */
738 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
739 nicvf_free_snd_queue(nic, &qs->sq[qidx]);
740 }
741
nicvf_alloc_resources(struct nicvf * nic)742 static int nicvf_alloc_resources(struct nicvf *nic)
743 {
744 int qidx;
745 struct queue_set *qs = nic->qs;
746
747 /* Alloc receive buffer descriptor ring */
748 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
749 if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
750 DMA_BUFFER_LEN))
751 goto alloc_fail;
752 }
753
754 /* Alloc send queue */
755 for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
756 if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len))
757 goto alloc_fail;
758 }
759
760 /* Alloc completion queue */
761 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
762 if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len))
763 goto alloc_fail;
764 }
765
766 return 0;
767 alloc_fail:
768 nicvf_free_resources(nic);
769 return -ENOMEM;
770 }
771
nicvf_set_qset_resources(struct nicvf * nic)772 int nicvf_set_qset_resources(struct nicvf *nic)
773 {
774 struct queue_set *qs;
775
776 qs = devm_kzalloc(&nic->pdev->dev, sizeof(*qs), GFP_KERNEL);
777 if (!qs)
778 return -ENOMEM;
779 nic->qs = qs;
780
781 /* Set count of each queue */
782 qs->rbdr_cnt = RBDR_CNT;
783 qs->rq_cnt = RCV_QUEUE_CNT;
784 qs->sq_cnt = SND_QUEUE_CNT;
785 qs->cq_cnt = CMP_QUEUE_CNT;
786
787 /* Set queue lengths */
788 qs->rbdr_len = RCV_BUF_COUNT;
789 qs->sq_len = SND_QUEUE_LEN;
790 qs->cq_len = CMP_QUEUE_LEN;
791
792 nic->rx_queues = qs->rq_cnt;
793 nic->tx_queues = qs->sq_cnt;
794
795 return 0;
796 }
797
nicvf_config_data_transfer(struct nicvf * nic,bool enable)798 int nicvf_config_data_transfer(struct nicvf *nic, bool enable)
799 {
800 bool disable = false;
801 struct queue_set *qs = nic->qs;
802 int qidx;
803
804 if (!qs)
805 return 0;
806
807 if (enable) {
808 if (nicvf_alloc_resources(nic))
809 return -ENOMEM;
810
811 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
812 nicvf_snd_queue_config(nic, qs, qidx, enable);
813 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
814 nicvf_cmp_queue_config(nic, qs, qidx, enable);
815 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
816 nicvf_rbdr_config(nic, qs, qidx, enable);
817 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
818 nicvf_rcv_queue_config(nic, qs, qidx, enable);
819 } else {
820 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
821 nicvf_rcv_queue_config(nic, qs, qidx, disable);
822 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
823 nicvf_rbdr_config(nic, qs, qidx, disable);
824 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
825 nicvf_snd_queue_config(nic, qs, qidx, disable);
826 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
827 nicvf_cmp_queue_config(nic, qs, qidx, disable);
828
829 nicvf_free_resources(nic);
830 }
831
832 return 0;
833 }
834
835 /* Get a free desc from SQ
836 * returns descriptor ponter & descriptor number
837 */
nicvf_get_sq_desc(struct snd_queue * sq,int desc_cnt)838 static inline int nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
839 {
840 int qentry;
841
842 qentry = sq->tail;
843 atomic_sub(desc_cnt, &sq->free_cnt);
844 sq->tail += desc_cnt;
845 sq->tail &= (sq->dmem.q_len - 1);
846
847 return qentry;
848 }
849
850 /* Free descriptor back to SQ for future use */
nicvf_put_sq_desc(struct snd_queue * sq,int desc_cnt)851 void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
852 {
853 atomic_add(desc_cnt, &sq->free_cnt);
854 sq->head += desc_cnt;
855 sq->head &= (sq->dmem.q_len - 1);
856 }
857
nicvf_get_nxt_sqentry(struct snd_queue * sq,int qentry)858 static inline int nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
859 {
860 qentry++;
861 qentry &= (sq->dmem.q_len - 1);
862 return qentry;
863 }
864
nicvf_sq_enable(struct nicvf * nic,struct snd_queue * sq,int qidx)865 void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
866 {
867 u64 sq_cfg;
868
869 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
870 sq_cfg |= NICVF_SQ_EN;
871 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
872 /* Ring doorbell so that H/W restarts processing SQEs */
873 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
874 }
875
nicvf_sq_disable(struct nicvf * nic,int qidx)876 void nicvf_sq_disable(struct nicvf *nic, int qidx)
877 {
878 u64 sq_cfg;
879
880 sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
881 sq_cfg &= ~NICVF_SQ_EN;
882 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
883 }
884
nicvf_sq_free_used_descs(struct net_device * netdev,struct snd_queue * sq,int qidx)885 void nicvf_sq_free_used_descs(struct net_device *netdev, struct snd_queue *sq,
886 int qidx)
887 {
888 u64 head, tail;
889 struct sk_buff *skb;
890 struct nicvf *nic = netdev_priv(netdev);
891 struct sq_hdr_subdesc *hdr;
892
893 head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
894 tail = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, qidx) >> 4;
895 while (sq->head != head) {
896 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
897 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
898 nicvf_put_sq_desc(sq, 1);
899 continue;
900 }
901 skb = (struct sk_buff *)sq->skbuff[sq->head];
902 if (skb)
903 dev_kfree_skb_any(skb);
904 atomic64_add(1, (atomic64_t *)&netdev->stats.tx_packets);
905 atomic64_add(hdr->tot_len,
906 (atomic64_t *)&netdev->stats.tx_bytes);
907 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
908 }
909 }
910
911 /* Calculate no of SQ subdescriptors needed to transmit all
912 * segments of this TSO packet.
913 * Taken from 'Tilera network driver' with a minor modification.
914 */
nicvf_tso_count_subdescs(struct sk_buff * skb)915 static int nicvf_tso_count_subdescs(struct sk_buff *skb)
916 {
917 struct skb_shared_info *sh = skb_shinfo(skb);
918 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
919 unsigned int data_len = skb->len - sh_len;
920 unsigned int p_len = sh->gso_size;
921 long f_id = -1; /* id of the current fragment */
922 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
923 long f_used = 0; /* bytes used from the current fragment */
924 long n; /* size of the current piece of payload */
925 int num_edescs = 0;
926 int segment;
927
928 for (segment = 0; segment < sh->gso_segs; segment++) {
929 unsigned int p_used = 0;
930
931 /* One edesc for header and for each piece of the payload. */
932 for (num_edescs++; p_used < p_len; num_edescs++) {
933 /* Advance as needed. */
934 while (f_used >= f_size) {
935 f_id++;
936 f_size = skb_frag_size(&sh->frags[f_id]);
937 f_used = 0;
938 }
939
940 /* Use bytes from the current fragment. */
941 n = p_len - p_used;
942 if (n > f_size - f_used)
943 n = f_size - f_used;
944 f_used += n;
945 p_used += n;
946 }
947
948 /* The last segment may be less than gso_size. */
949 data_len -= p_len;
950 if (data_len < p_len)
951 p_len = data_len;
952 }
953
954 /* '+ gso_segs' for SQ_HDR_SUDESCs for each segment */
955 return num_edescs + sh->gso_segs;
956 }
957
958 /* Get the number of SQ descriptors needed to xmit this skb */
nicvf_sq_subdesc_required(struct nicvf * nic,struct sk_buff * skb)959 static int nicvf_sq_subdesc_required(struct nicvf *nic, struct sk_buff *skb)
960 {
961 int subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT;
962
963 if (skb_shinfo(skb)->gso_size) {
964 subdesc_cnt = nicvf_tso_count_subdescs(skb);
965 return subdesc_cnt;
966 }
967
968 if (skb_shinfo(skb)->nr_frags)
969 subdesc_cnt += skb_shinfo(skb)->nr_frags;
970
971 return subdesc_cnt;
972 }
973
974 /* Add SQ HEADER subdescriptor.
975 * First subdescriptor for every send descriptor.
976 */
977 static inline void
nicvf_sq_add_hdr_subdesc(struct snd_queue * sq,int qentry,int subdesc_cnt,struct sk_buff * skb,int len)978 nicvf_sq_add_hdr_subdesc(struct snd_queue *sq, int qentry,
979 int subdesc_cnt, struct sk_buff *skb, int len)
980 {
981 int proto;
982 struct sq_hdr_subdesc *hdr;
983
984 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
985 sq->skbuff[qentry] = (u64)skb;
986
987 memset(hdr, 0, SND_QUEUE_DESC_SIZE);
988 hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
989 /* Enable notification via CQE after processing SQE */
990 hdr->post_cqe = 1;
991 /* No of subdescriptors following this */
992 hdr->subdesc_cnt = subdesc_cnt;
993 hdr->tot_len = len;
994
995 /* Offload checksum calculation to HW */
996 if (skb->ip_summed == CHECKSUM_PARTIAL) {
997 hdr->csum_l3 = 1; /* Enable IP csum calculation */
998 hdr->l3_offset = skb_network_offset(skb);
999 hdr->l4_offset = skb_transport_offset(skb);
1000
1001 proto = ip_hdr(skb)->protocol;
1002 switch (proto) {
1003 case IPPROTO_TCP:
1004 hdr->csum_l4 = SEND_L4_CSUM_TCP;
1005 break;
1006 case IPPROTO_UDP:
1007 hdr->csum_l4 = SEND_L4_CSUM_UDP;
1008 break;
1009 case IPPROTO_SCTP:
1010 hdr->csum_l4 = SEND_L4_CSUM_SCTP;
1011 break;
1012 }
1013 }
1014 }
1015
1016 /* SQ GATHER subdescriptor
1017 * Must follow HDR descriptor
1018 */
nicvf_sq_add_gather_subdesc(struct snd_queue * sq,int qentry,int size,u64 data)1019 static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
1020 int size, u64 data)
1021 {
1022 struct sq_gather_subdesc *gather;
1023
1024 qentry &= (sq->dmem.q_len - 1);
1025 gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
1026
1027 memset(gather, 0, SND_QUEUE_DESC_SIZE);
1028 gather->subdesc_type = SQ_DESC_TYPE_GATHER;
1029 gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
1030 gather->size = size;
1031 gather->addr = data;
1032 }
1033
1034 /* Segment a TSO packet into 'gso_size' segments and append
1035 * them to SQ for transfer
1036 */
nicvf_sq_append_tso(struct nicvf * nic,struct snd_queue * sq,int sq_num,int qentry,struct sk_buff * skb)1037 static int nicvf_sq_append_tso(struct nicvf *nic, struct snd_queue *sq,
1038 int sq_num, int qentry, struct sk_buff *skb)
1039 {
1040 struct tso_t tso;
1041 int seg_subdescs = 0, desc_cnt = 0;
1042 int seg_len, total_len, data_left;
1043 int hdr_qentry = qentry;
1044 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1045
1046 tso_start(skb, &tso);
1047 total_len = skb->len - hdr_len;
1048 while (total_len > 0) {
1049 char *hdr;
1050
1051 /* Save Qentry for adding HDR_SUBDESC at the end */
1052 hdr_qentry = qentry;
1053
1054 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1055 total_len -= data_left;
1056
1057 /* Add segment's header */
1058 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1059 hdr = sq->tso_hdrs + qentry * TSO_HEADER_SIZE;
1060 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
1061 nicvf_sq_add_gather_subdesc(sq, qentry, hdr_len,
1062 sq->tso_hdrs_phys +
1063 qentry * TSO_HEADER_SIZE);
1064 /* HDR_SUDESC + GATHER */
1065 seg_subdescs = 2;
1066 seg_len = hdr_len;
1067
1068 /* Add segment's payload fragments */
1069 while (data_left > 0) {
1070 int size;
1071
1072 size = min_t(int, tso.size, data_left);
1073
1074 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1075 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1076 virt_to_phys(tso.data));
1077 seg_subdescs++;
1078 seg_len += size;
1079
1080 data_left -= size;
1081 tso_build_data(skb, &tso, size);
1082 }
1083 nicvf_sq_add_hdr_subdesc(sq, hdr_qentry,
1084 seg_subdescs - 1, skb, seg_len);
1085 sq->skbuff[hdr_qentry] = (u64)NULL;
1086 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1087
1088 desc_cnt += seg_subdescs;
1089 }
1090 /* Save SKB in the last segment for freeing */
1091 sq->skbuff[hdr_qentry] = (u64)skb;
1092
1093 /* make sure all memory stores are done before ringing doorbell */
1094 smp_wmb();
1095
1096 /* Inform HW to xmit all TSO segments */
1097 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
1098 sq_num, desc_cnt);
1099 nic->drv_stats.tx_tso++;
1100 return 1;
1101 }
1102
1103 /* Append an skb to a SQ for packet transfer. */
nicvf_sq_append_skb(struct nicvf * nic,struct sk_buff * skb)1104 int nicvf_sq_append_skb(struct nicvf *nic, struct sk_buff *skb)
1105 {
1106 int i, size;
1107 int subdesc_cnt;
1108 int sq_num, qentry;
1109 struct queue_set *qs;
1110 struct snd_queue *sq;
1111
1112 sq_num = skb_get_queue_mapping(skb);
1113 if (sq_num >= MAX_SND_QUEUES_PER_QS) {
1114 /* Get secondary Qset's SQ structure */
1115 i = sq_num / MAX_SND_QUEUES_PER_QS;
1116 if (!nic->snicvf[i - 1]) {
1117 netdev_warn(nic->netdev,
1118 "Secondary Qset#%d's ptr not initialized\n",
1119 i - 1);
1120 return 1;
1121 }
1122 nic = (struct nicvf *)nic->snicvf[i - 1];
1123 sq_num = sq_num % MAX_SND_QUEUES_PER_QS;
1124 }
1125
1126 qs = nic->qs;
1127 sq = &qs->sq[sq_num];
1128
1129 subdesc_cnt = nicvf_sq_subdesc_required(nic, skb);
1130 if (subdesc_cnt > atomic_read(&sq->free_cnt))
1131 goto append_fail;
1132
1133 qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
1134
1135 /* Check if its a TSO packet */
1136 if (skb_shinfo(skb)->gso_size)
1137 return nicvf_sq_append_tso(nic, sq, sq_num, qentry, skb);
1138
1139 /* Add SQ header subdesc */
1140 nicvf_sq_add_hdr_subdesc(sq, qentry, subdesc_cnt - 1, skb, skb->len);
1141
1142 /* Add SQ gather subdescs */
1143 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1144 size = skb_is_nonlinear(skb) ? skb_headlen(skb) : skb->len;
1145 nicvf_sq_add_gather_subdesc(sq, qentry, size, virt_to_phys(skb->data));
1146
1147 /* Check for scattered buffer */
1148 if (!skb_is_nonlinear(skb))
1149 goto doorbell;
1150
1151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1152 const struct skb_frag_struct *frag;
1153
1154 frag = &skb_shinfo(skb)->frags[i];
1155
1156 qentry = nicvf_get_nxt_sqentry(sq, qentry);
1157 size = skb_frag_size(frag);
1158 nicvf_sq_add_gather_subdesc(sq, qentry, size,
1159 virt_to_phys(
1160 skb_frag_address(frag)));
1161 }
1162
1163 doorbell:
1164 /* make sure all memory stores are done before ringing doorbell */
1165 smp_wmb();
1166
1167 /* Inform HW to xmit new packet */
1168 nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR,
1169 sq_num, subdesc_cnt);
1170 return 1;
1171
1172 append_fail:
1173 /* Use original PCI dev for debug log */
1174 nic = nic->pnicvf;
1175 netdev_dbg(nic->netdev, "Not enough SQ descriptors to xmit pkt\n");
1176 return 0;
1177 }
1178
frag_num(unsigned i)1179 static inline unsigned frag_num(unsigned i)
1180 {
1181 #ifdef __BIG_ENDIAN
1182 return (i & ~3) + 3 - (i & 3);
1183 #else
1184 return i;
1185 #endif
1186 }
1187
1188 /* Returns SKB for a received packet */
nicvf_get_rcv_skb(struct nicvf * nic,struct cqe_rx_t * cqe_rx)1189 struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
1190 {
1191 int frag;
1192 int payload_len = 0;
1193 struct sk_buff *skb = NULL;
1194 struct sk_buff *skb_frag = NULL;
1195 struct sk_buff *prev_frag = NULL;
1196 u16 *rb_lens = NULL;
1197 u64 *rb_ptrs = NULL;
1198
1199 rb_lens = (void *)cqe_rx + (3 * sizeof(u64));
1200 rb_ptrs = (void *)cqe_rx + (6 * sizeof(u64));
1201
1202 netdev_dbg(nic->netdev, "%s rb_cnt %d rb0_ptr %llx rb0_sz %d\n",
1203 __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
1204
1205 for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
1206 payload_len = rb_lens[frag_num(frag)];
1207 if (!frag) {
1208 /* First fragment */
1209 skb = nicvf_rb_ptr_to_skb(nic,
1210 *rb_ptrs - cqe_rx->align_pad,
1211 payload_len);
1212 if (!skb)
1213 return NULL;
1214 skb_reserve(skb, cqe_rx->align_pad);
1215 skb_put(skb, payload_len);
1216 } else {
1217 /* Add fragments */
1218 skb_frag = nicvf_rb_ptr_to_skb(nic, *rb_ptrs,
1219 payload_len);
1220 if (!skb_frag) {
1221 dev_kfree_skb(skb);
1222 return NULL;
1223 }
1224
1225 if (!skb_shinfo(skb)->frag_list)
1226 skb_shinfo(skb)->frag_list = skb_frag;
1227 else
1228 prev_frag->next = skb_frag;
1229
1230 prev_frag = skb_frag;
1231 skb->len += payload_len;
1232 skb->data_len += payload_len;
1233 skb_frag->len = payload_len;
1234 }
1235 /* Next buffer pointer */
1236 rb_ptrs++;
1237 }
1238 return skb;
1239 }
1240
1241 /* Enable interrupt */
nicvf_enable_intr(struct nicvf * nic,int int_type,int q_idx)1242 void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
1243 {
1244 u64 reg_val;
1245
1246 reg_val = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
1247
1248 switch (int_type) {
1249 case NICVF_INTR_CQ:
1250 reg_val |= ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1251 break;
1252 case NICVF_INTR_SQ:
1253 reg_val |= ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1254 break;
1255 case NICVF_INTR_RBDR:
1256 reg_val |= ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1257 break;
1258 case NICVF_INTR_PKT_DROP:
1259 reg_val |= (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1260 break;
1261 case NICVF_INTR_TCP_TIMER:
1262 reg_val |= (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1263 break;
1264 case NICVF_INTR_MBOX:
1265 reg_val |= (1ULL << NICVF_INTR_MBOX_SHIFT);
1266 break;
1267 case NICVF_INTR_QS_ERR:
1268 reg_val |= (1ULL << NICVF_INTR_QS_ERR_SHIFT);
1269 break;
1270 default:
1271 netdev_err(nic->netdev,
1272 "Failed to enable interrupt: unknown type\n");
1273 break;
1274 }
1275
1276 nicvf_reg_write(nic, NIC_VF_ENA_W1S, reg_val);
1277 }
1278
1279 /* Disable interrupt */
nicvf_disable_intr(struct nicvf * nic,int int_type,int q_idx)1280 void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
1281 {
1282 u64 reg_val = 0;
1283
1284 switch (int_type) {
1285 case NICVF_INTR_CQ:
1286 reg_val |= ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1287 break;
1288 case NICVF_INTR_SQ:
1289 reg_val |= ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1290 break;
1291 case NICVF_INTR_RBDR:
1292 reg_val |= ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1293 break;
1294 case NICVF_INTR_PKT_DROP:
1295 reg_val |= (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1296 break;
1297 case NICVF_INTR_TCP_TIMER:
1298 reg_val |= (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1299 break;
1300 case NICVF_INTR_MBOX:
1301 reg_val |= (1ULL << NICVF_INTR_MBOX_SHIFT);
1302 break;
1303 case NICVF_INTR_QS_ERR:
1304 reg_val |= (1ULL << NICVF_INTR_QS_ERR_SHIFT);
1305 break;
1306 default:
1307 netdev_err(nic->netdev,
1308 "Failed to disable interrupt: unknown type\n");
1309 break;
1310 }
1311
1312 nicvf_reg_write(nic, NIC_VF_ENA_W1C, reg_val);
1313 }
1314
1315 /* Clear interrupt */
nicvf_clear_intr(struct nicvf * nic,int int_type,int q_idx)1316 void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
1317 {
1318 u64 reg_val = 0;
1319
1320 switch (int_type) {
1321 case NICVF_INTR_CQ:
1322 reg_val = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1323 break;
1324 case NICVF_INTR_SQ:
1325 reg_val = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1326 break;
1327 case NICVF_INTR_RBDR:
1328 reg_val = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1329 break;
1330 case NICVF_INTR_PKT_DROP:
1331 reg_val = (1ULL << NICVF_INTR_PKT_DROP_SHIFT);
1332 break;
1333 case NICVF_INTR_TCP_TIMER:
1334 reg_val = (1ULL << NICVF_INTR_TCP_TIMER_SHIFT);
1335 break;
1336 case NICVF_INTR_MBOX:
1337 reg_val = (1ULL << NICVF_INTR_MBOX_SHIFT);
1338 break;
1339 case NICVF_INTR_QS_ERR:
1340 reg_val |= (1ULL << NICVF_INTR_QS_ERR_SHIFT);
1341 break;
1342 default:
1343 netdev_err(nic->netdev,
1344 "Failed to clear interrupt: unknown type\n");
1345 break;
1346 }
1347
1348 nicvf_reg_write(nic, NIC_VF_INT, reg_val);
1349 }
1350
1351 /* Check if interrupt is enabled */
nicvf_is_intr_enabled(struct nicvf * nic,int int_type,int q_idx)1352 int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
1353 {
1354 u64 reg_val;
1355 u64 mask = 0xff;
1356
1357 reg_val = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
1358
1359 switch (int_type) {
1360 case NICVF_INTR_CQ:
1361 mask = ((1ULL << q_idx) << NICVF_INTR_CQ_SHIFT);
1362 break;
1363 case NICVF_INTR_SQ:
1364 mask = ((1ULL << q_idx) << NICVF_INTR_SQ_SHIFT);
1365 break;
1366 case NICVF_INTR_RBDR:
1367 mask = ((1ULL << q_idx) << NICVF_INTR_RBDR_SHIFT);
1368 break;
1369 case NICVF_INTR_PKT_DROP:
1370 mask = NICVF_INTR_PKT_DROP_MASK;
1371 break;
1372 case NICVF_INTR_TCP_TIMER:
1373 mask = NICVF_INTR_TCP_TIMER_MASK;
1374 break;
1375 case NICVF_INTR_MBOX:
1376 mask = NICVF_INTR_MBOX_MASK;
1377 break;
1378 case NICVF_INTR_QS_ERR:
1379 mask = NICVF_INTR_QS_ERR_MASK;
1380 break;
1381 default:
1382 netdev_err(nic->netdev,
1383 "Failed to check interrupt enable: unknown type\n");
1384 break;
1385 }
1386
1387 return (reg_val & mask);
1388 }
1389
nicvf_update_rq_stats(struct nicvf * nic,int rq_idx)1390 void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
1391 {
1392 struct rcv_queue *rq;
1393
1394 #define GET_RQ_STATS(reg) \
1395 nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
1396 (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1397
1398 rq = &nic->qs->rq[rq_idx];
1399 rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
1400 rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
1401 }
1402
nicvf_update_sq_stats(struct nicvf * nic,int sq_idx)1403 void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
1404 {
1405 struct snd_queue *sq;
1406
1407 #define GET_SQ_STATS(reg) \
1408 nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
1409 (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
1410
1411 sq = &nic->qs->sq[sq_idx];
1412 sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
1413 sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
1414 }
1415
1416 /* Check for errors in the receive cmp.queue entry */
nicvf_check_cqe_rx_errs(struct nicvf * nic,struct cqe_rx_t * cqe_rx)1417 int nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
1418 {
1419 struct nicvf_hw_stats *stats = &nic->hw_stats;
1420
1421 if (!cqe_rx->err_level && !cqe_rx->err_opcode)
1422 return 0;
1423
1424 if (netif_msg_rx_err(nic))
1425 netdev_err(nic->netdev,
1426 "%s: RX error CQE err_level 0x%x err_opcode 0x%x\n",
1427 nic->netdev->name,
1428 cqe_rx->err_level, cqe_rx->err_opcode);
1429
1430 switch (cqe_rx->err_opcode) {
1431 case CQ_RX_ERROP_RE_PARTIAL:
1432 stats->rx_bgx_truncated_pkts++;
1433 break;
1434 case CQ_RX_ERROP_RE_JABBER:
1435 stats->rx_jabber_errs++;
1436 break;
1437 case CQ_RX_ERROP_RE_FCS:
1438 stats->rx_fcs_errs++;
1439 break;
1440 case CQ_RX_ERROP_RE_RX_CTL:
1441 stats->rx_bgx_errs++;
1442 break;
1443 case CQ_RX_ERROP_PREL2_ERR:
1444 stats->rx_prel2_errs++;
1445 break;
1446 case CQ_RX_ERROP_L2_MAL:
1447 stats->rx_l2_hdr_malformed++;
1448 break;
1449 case CQ_RX_ERROP_L2_OVERSIZE:
1450 stats->rx_oversize++;
1451 break;
1452 case CQ_RX_ERROP_L2_UNDERSIZE:
1453 stats->rx_undersize++;
1454 break;
1455 case CQ_RX_ERROP_L2_LENMISM:
1456 stats->rx_l2_len_mismatch++;
1457 break;
1458 case CQ_RX_ERROP_L2_PCLP:
1459 stats->rx_l2_pclp++;
1460 break;
1461 case CQ_RX_ERROP_IP_NOT:
1462 stats->rx_ip_ver_errs++;
1463 break;
1464 case CQ_RX_ERROP_IP_CSUM_ERR:
1465 stats->rx_ip_csum_errs++;
1466 break;
1467 case CQ_RX_ERROP_IP_MAL:
1468 stats->rx_ip_hdr_malformed++;
1469 break;
1470 case CQ_RX_ERROP_IP_MALD:
1471 stats->rx_ip_payload_malformed++;
1472 break;
1473 case CQ_RX_ERROP_IP_HOP:
1474 stats->rx_ip_ttl_errs++;
1475 break;
1476 case CQ_RX_ERROP_L3_PCLP:
1477 stats->rx_l3_pclp++;
1478 break;
1479 case CQ_RX_ERROP_L4_MAL:
1480 stats->rx_l4_malformed++;
1481 break;
1482 case CQ_RX_ERROP_L4_CHK:
1483 stats->rx_l4_csum_errs++;
1484 break;
1485 case CQ_RX_ERROP_UDP_LEN:
1486 stats->rx_udp_len_errs++;
1487 break;
1488 case CQ_RX_ERROP_L4_PORT:
1489 stats->rx_l4_port_errs++;
1490 break;
1491 case CQ_RX_ERROP_TCP_FLAG:
1492 stats->rx_tcp_flag_errs++;
1493 break;
1494 case CQ_RX_ERROP_TCP_OFFSET:
1495 stats->rx_tcp_offset_errs++;
1496 break;
1497 case CQ_RX_ERROP_L4_PCLP:
1498 stats->rx_l4_pclp++;
1499 break;
1500 case CQ_RX_ERROP_RBDR_TRUNC:
1501 stats->rx_truncated_pkts++;
1502 break;
1503 }
1504
1505 return 1;
1506 }
1507
1508 /* Check for errors in the send cmp.queue entry */
nicvf_check_cqe_tx_errs(struct nicvf * nic,struct cmp_queue * cq,struct cqe_send_t * cqe_tx)1509 int nicvf_check_cqe_tx_errs(struct nicvf *nic,
1510 struct cmp_queue *cq, struct cqe_send_t *cqe_tx)
1511 {
1512 struct cmp_queue_stats *stats = &cq->stats;
1513
1514 switch (cqe_tx->send_status) {
1515 case CQ_TX_ERROP_GOOD:
1516 stats->tx.good++;
1517 return 0;
1518 case CQ_TX_ERROP_DESC_FAULT:
1519 stats->tx.desc_fault++;
1520 break;
1521 case CQ_TX_ERROP_HDR_CONS_ERR:
1522 stats->tx.hdr_cons_err++;
1523 break;
1524 case CQ_TX_ERROP_SUBDC_ERR:
1525 stats->tx.subdesc_err++;
1526 break;
1527 case CQ_TX_ERROP_IMM_SIZE_OFLOW:
1528 stats->tx.imm_size_oflow++;
1529 break;
1530 case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
1531 stats->tx.data_seq_err++;
1532 break;
1533 case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
1534 stats->tx.mem_seq_err++;
1535 break;
1536 case CQ_TX_ERROP_LOCK_VIOL:
1537 stats->tx.lock_viol++;
1538 break;
1539 case CQ_TX_ERROP_DATA_FAULT:
1540 stats->tx.data_fault++;
1541 break;
1542 case CQ_TX_ERROP_TSTMP_CONFLICT:
1543 stats->tx.tstmp_conflict++;
1544 break;
1545 case CQ_TX_ERROP_TSTMP_TIMEOUT:
1546 stats->tx.tstmp_timeout++;
1547 break;
1548 case CQ_TX_ERROP_MEM_FAULT:
1549 stats->tx.mem_fault++;
1550 break;
1551 case CQ_TX_ERROP_CK_OVERLAP:
1552 stats->tx.csum_overlap++;
1553 break;
1554 case CQ_TX_ERROP_CK_OFLOW:
1555 stats->tx.csum_overflow++;
1556 break;
1557 }
1558
1559 return 1;
1560 }
1561