net/mlx4: restore Rx offloads
[dpdk.git] / drivers / net / mlx4 / mlx4_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @file
36  * Data plane functions for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <inttypes.h>
41 #include <stdint.h>
42 #include <string.h>
43
44 /* Verbs headers do not support -pedantic. */
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic ignored "-Wpedantic"
47 #endif
48 #include <infiniband/verbs.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_branch_prediction.h>
54 #include <rte_common.h>
55 #include <rte_io.h>
56 #include <rte_mbuf.h>
57 #include <rte_mempool.h>
58 #include <rte_prefetch.h>
59
60 #include "mlx4.h"
61 #include "mlx4_prm.h"
62 #include "mlx4_rxtx.h"
63 #include "mlx4_utils.h"
64
65 /**
66  * Pointer-value pair structure used in tx_post_send for saving the first
67  * DWORD (32 byte) of a TXBB.
68  */
69 struct pv {
70         struct mlx4_wqe_data_seg *dseg;
71         uint32_t val;
72 };
73
74 /**
75  * Stamp a WQE so it won't be reused by the HW.
76  *
77  * Routine is used when freeing WQE used by the chip or when failing
78  * building an WQ entry has failed leaving partial information on the queue.
79  *
80  * @param sq
81  *   Pointer to the SQ structure.
82  * @param index
83  *   Index of the freed WQE.
84  * @param num_txbbs
85  *   Number of blocks to stamp.
86  *   If < 0 the routine will use the size written in the WQ entry.
87  * @param owner
88  *   The value of the WQE owner bit to use in the stamp.
89  *
90  * @return
91  *   The number of Tx basic blocs (TXBB) the WQE contained.
92  */
93 static int
94 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, uint16_t index, uint8_t owner)
95 {
96         uint32_t stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
97                                           (!!owner << MLX4_SQ_STAMP_SHIFT));
98         uint8_t *wqe = mlx4_get_send_wqe(sq, (index & sq->txbb_cnt_mask));
99         uint32_t *ptr = (uint32_t *)wqe;
100         int i;
101         int txbbs_size;
102         int num_txbbs;
103
104         /* Extract the size from the control segment of the WQE. */
105         num_txbbs = MLX4_SIZE_TO_TXBBS((((struct mlx4_wqe_ctrl_seg *)
106                                          wqe)->fence_size & 0x3f) << 4);
107         txbbs_size = num_txbbs * MLX4_TXBB_SIZE;
108         /* Optimize the common case when there is no wrap-around. */
109         if (wqe + txbbs_size <= sq->eob) {
110                 /* Stamp the freed descriptor. */
111                 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
112                         *ptr = stamp;
113                         ptr += MLX4_SQ_STAMP_DWORDS;
114                 }
115         } else {
116                 /* Stamp the freed descriptor. */
117                 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
118                         *ptr = stamp;
119                         ptr += MLX4_SQ_STAMP_DWORDS;
120                         if ((uint8_t *)ptr >= sq->eob) {
121                                 ptr = (uint32_t *)sq->buf;
122                                 stamp ^= RTE_BE32(0x80000000);
123                         }
124                 }
125         }
126         return num_txbbs;
127 }
128
129 /**
130  * Manage Tx completions.
131  *
132  * When sending a burst, mlx4_tx_burst() posts several WRs.
133  * To improve performance, a completion event is only required once every
134  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
135  * for other WRs, but this information would not be used anyway.
136  *
137  * @param txq
138  *   Pointer to Tx queue structure.
139  *
140  * @return
141  *   0 on success, -1 on failure.
142  */
143 static int
144 mlx4_txq_complete(struct txq *txq)
145 {
146         unsigned int elts_comp = txq->elts_comp;
147         unsigned int elts_tail = txq->elts_tail;
148         const unsigned int elts_n = txq->elts_n;
149         struct mlx4_cq *cq = &txq->mcq;
150         struct mlx4_sq *sq = &txq->msq;
151         struct mlx4_cqe *cqe;
152         uint32_t cons_index = cq->cons_index;
153         uint16_t new_index;
154         uint16_t nr_txbbs = 0;
155         int pkts = 0;
156
157         if (unlikely(elts_comp == 0))
158                 return 0;
159         /*
160          * Traverse over all CQ entries reported and handle each WQ entry
161          * reported by them.
162          */
163         do {
164                 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cons_index);
165                 if (unlikely(!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
166                     !!(cons_index & cq->cqe_cnt)))
167                         break;
168                 /*
169                  * Make sure we read the CQE after we read the ownership bit.
170                  */
171                 rte_rmb();
172                 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
173                              MLX4_CQE_OPCODE_ERROR)) {
174                         struct mlx4_err_cqe *cqe_err =
175                                 (struct mlx4_err_cqe *)cqe;
176                         ERROR("%p CQE error - vendor syndrome: 0x%x"
177                               " syndrome: 0x%x\n",
178                               (void *)txq, cqe_err->vendor_err,
179                               cqe_err->syndrome);
180                 }
181                 /* Get WQE index reported in the CQE. */
182                 new_index =
183                         rte_be_to_cpu_16(cqe->wqe_index) & sq->txbb_cnt_mask;
184                 do {
185                         /* Free next descriptor. */
186                         nr_txbbs +=
187                                 mlx4_txq_stamp_freed_wqe(sq,
188                                      (sq->tail + nr_txbbs) & sq->txbb_cnt_mask,
189                                      !!((sq->tail + nr_txbbs) & sq->txbb_cnt));
190                         pkts++;
191                 } while (((sq->tail + nr_txbbs) & sq->txbb_cnt_mask) !=
192                          new_index);
193                 cons_index++;
194         } while (1);
195         if (unlikely(pkts == 0))
196                 return 0;
197         /*
198          * Update CQ.
199          * To prevent CQ overflow we first update CQ consumer and only then
200          * the ring consumer.
201          */
202         cq->cons_index = cons_index;
203         *cq->set_ci_db = rte_cpu_to_be_32(cq->cons_index & 0xffffff);
204         rte_wmb();
205         sq->tail = sq->tail + nr_txbbs;
206         /* Update the list of packets posted for transmission. */
207         elts_comp -= pkts;
208         assert(elts_comp <= txq->elts_comp);
209         /*
210          * Assume completion status is successful as nothing can be done about
211          * it anyway.
212          */
213         elts_tail += pkts;
214         if (elts_tail >= elts_n)
215                 elts_tail -= elts_n;
216         txq->elts_tail = elts_tail;
217         txq->elts_comp = elts_comp;
218         return 0;
219 }
220
221 /**
222  * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
223  * the cloned mbuf is allocated is returned instead.
224  *
225  * @param buf
226  *   Pointer to mbuf.
227  *
228  * @return
229  *   Memory pool where data is located for given mbuf.
230  */
231 static struct rte_mempool *
232 mlx4_txq_mb2mp(struct rte_mbuf *buf)
233 {
234         if (unlikely(RTE_MBUF_INDIRECT(buf)))
235                 return rte_mbuf_from_indirect(buf)->pool;
236         return buf->pool;
237 }
238
239 /**
240  * Get memory region (MR) <-> memory pool (MP) association from txq->mp2mr[].
241  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
242  * remove an entry first.
243  *
244  * @param txq
245  *   Pointer to Tx queue structure.
246  * @param[in] mp
247  *   Memory pool for which a memory region lkey must be returned.
248  *
249  * @return
250  *   mr->lkey on success, (uint32_t)-1 on failure.
251  */
252 uint32_t
253 mlx4_txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
254 {
255         unsigned int i;
256         struct ibv_mr *mr;
257
258         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
259                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
260                         /* Unknown MP, add a new MR for it. */
261                         break;
262                 }
263                 if (txq->mp2mr[i].mp == mp) {
264                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
265                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
266                         return txq->mp2mr[i].lkey;
267                 }
268         }
269         /* Add a new entry, register MR first. */
270         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
271               (void *)txq, mp->name, (void *)mp);
272         mr = mlx4_mp2mr(txq->priv->pd, mp);
273         if (unlikely(mr == NULL)) {
274                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
275                       (void *)txq);
276                 return (uint32_t)-1;
277         }
278         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
279                 /* Table is full, remove oldest entry. */
280                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
281                       (void *)txq);
282                 --i;
283                 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
284                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
285                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
286         }
287         /* Store the new entry. */
288         txq->mp2mr[i].mp = mp;
289         txq->mp2mr[i].mr = mr;
290         txq->mp2mr[i].lkey = mr->lkey;
291         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
292               (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
293         return txq->mp2mr[i].lkey;
294 }
295
296 /**
297  * Posts a single work request to a send queue.
298  *
299  * @param txq
300  *   Target Tx queue.
301  * @param pkt
302  *   Packet to transmit.
303  *
304  * @return
305  *   0 on success, negative errno value otherwise and rte_errno is set.
306  */
307 static inline int
308 mlx4_post_send(struct txq *txq, struct rte_mbuf *pkt)
309 {
310         struct mlx4_wqe_ctrl_seg *ctrl;
311         struct mlx4_wqe_data_seg *dseg;
312         struct mlx4_sq *sq = &txq->msq;
313         struct rte_mbuf *buf;
314         uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
315         uint32_t lkey;
316         uintptr_t addr;
317         uint32_t srcrb_flags;
318         uint32_t owner_opcode = MLX4_OPCODE_SEND;
319         uint32_t byte_count;
320         int wqe_real_size;
321         int nr_txbbs;
322         int rc;
323         struct pv *pv = (struct pv *)txq->bounce_buf;
324         int pv_counter = 0;
325
326         /* Calculate the needed work queue entry size for this packet. */
327         wqe_real_size = sizeof(struct mlx4_wqe_ctrl_seg) +
328                         pkt->nb_segs * sizeof(struct mlx4_wqe_data_seg);
329         nr_txbbs = MLX4_SIZE_TO_TXBBS(wqe_real_size);
330         /*
331          * Check that there is room for this WQE in the send queue and that
332          * the WQE size is legal.
333          */
334         if (((sq->head - sq->tail) + nr_txbbs +
335              sq->headroom_txbbs) >= sq->txbb_cnt ||
336             nr_txbbs > MLX4_MAX_WQE_TXBBS) {
337                 rc = ENOSPC;
338                 goto err;
339         }
340         /* Get the control and data entries of the WQE. */
341         ctrl = (struct mlx4_wqe_ctrl_seg *)mlx4_get_send_wqe(sq, head_idx);
342         dseg = (struct mlx4_wqe_data_seg *)((uintptr_t)ctrl +
343                                             sizeof(struct mlx4_wqe_ctrl_seg));
344         /* Fill the data segments with buffer information. */
345         for (buf = pkt; buf != NULL; buf = buf->next, dseg++) {
346                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
347                 rte_prefetch0((volatile void *)addr);
348                 /* Handle WQE wraparound. */
349                 if (unlikely(dseg >= (struct mlx4_wqe_data_seg *)sq->eob))
350                         dseg = (struct mlx4_wqe_data_seg *)sq->buf;
351                 dseg->addr = rte_cpu_to_be_64(addr);
352                 /* Memory region key for this memory pool. */
353                 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(buf));
354                 if (unlikely(lkey == (uint32_t)-1)) {
355                         /* MR does not exist. */
356                         DEBUG("%p: unable to get MP <-> MR association",
357                               (void *)txq);
358                         /*
359                          * Restamp entry in case of failure.
360                          * Make sure that size is written correctly
361                          * Note that we give ownership to the SW, not the HW.
362                          */
363                         ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
364                         mlx4_txq_stamp_freed_wqe(sq, head_idx,
365                                      (sq->head & sq->txbb_cnt) ? 0 : 1);
366                         rc = EFAULT;
367                         goto err;
368                 }
369                 dseg->lkey = rte_cpu_to_be_32(lkey);
370                 if (likely(buf->data_len)) {
371                         byte_count = rte_cpu_to_be_32(buf->data_len);
372                 } else {
373                         /*
374                          * Zero length segment is treated as inline segment
375                          * with zero data.
376                          */
377                         byte_count = RTE_BE32(0x80000000);
378                 }
379                 /*
380                  * If the data segment is not at the beginning of a
381                  * Tx basic block (TXBB) then write the byte count,
382                  * else postpone the writing to just before updating the
383                  * control segment.
384                  */
385                 if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
386                         /*
387                          * Need a barrier here before writing the byte_count
388                          * fields to make sure that all the data is visible
389                          * before the byte_count field is set.
390                          * Otherwise, if the segment begins a new cacheline,
391                          * the HCA prefetcher could grab the 64-byte chunk and
392                          * get a valid (!= 0xffffffff) byte count but stale
393                          * data, and end up sending the wrong data.
394                          */
395                         rte_io_wmb();
396                         dseg->byte_count = byte_count;
397                 } else {
398                         /*
399                          * This data segment starts at the beginning of a new
400                          * TXBB, so we need to postpone its byte_count writing
401                          * for later.
402                          */
403                         pv[pv_counter].dseg = dseg;
404                         pv[pv_counter++].val = byte_count;
405                 }
406         }
407         /* Write the first DWORD of each TXBB save earlier. */
408         if (pv_counter) {
409                 /* Need a barrier here before writing the byte_count. */
410                 rte_io_wmb();
411                 for (--pv_counter; pv_counter  >= 0; pv_counter--)
412                         pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
413         }
414         /* Fill the control parameters for this packet. */
415         ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
416         /*
417          * The caller should prepare "imm" in advance in order to support
418          * VF to VF communication (when the device is a virtual-function
419          * device (VF)).
420          */
421         ctrl->imm = 0;
422         /*
423          * For raw Ethernet, the SOLICIT flag is used to indicate that no ICRC
424          * should be calculated.
425          */
426         txq->elts_comp_cd -= nr_txbbs;
427         if (unlikely(txq->elts_comp_cd <= 0)) {
428                 txq->elts_comp_cd = txq->elts_comp_cd_init;
429                 srcrb_flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
430                                        MLX4_WQE_CTRL_CQ_UPDATE);
431         } else {
432                 srcrb_flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
433         }
434         /* Enable HW checksum offload if requested */
435         if (txq->csum &&
436             (pkt->ol_flags &
437              (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
438                 const uint64_t is_tunneled = (pkt->ol_flags &
439                                               (PKT_TX_TUNNEL_GRE |
440                                                PKT_TX_TUNNEL_VXLAN));
441
442                 if (is_tunneled && txq->csum_l2tun) {
443                         owner_opcode |= MLX4_WQE_CTRL_IIP_HDR_CSUM |
444                                         MLX4_WQE_CTRL_IL4_HDR_CSUM;
445                         if (pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM)
446                                 srcrb_flags |=
447                                         RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
448                 } else {
449                         srcrb_flags |= RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
450                                                 MLX4_WQE_CTRL_TCP_UDP_CSUM);
451                 }
452         }
453         ctrl->srcrb_flags = srcrb_flags;
454         /*
455          * Make sure descriptor is fully written before
456          * setting ownership bit (because HW can start
457          * executing as soon as we do).
458          */
459         rte_wmb();
460         ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode |
461                                               ((sq->head & sq->txbb_cnt) ?
462                                                MLX4_BIT_WQE_OWN : 0));
463         sq->head += nr_txbbs;
464         return 0;
465 err:
466         rte_errno = rc;
467         return -rc;
468 }
469
470 /**
471  * DPDK callback for Tx.
472  *
473  * @param dpdk_txq
474  *   Generic pointer to Tx queue structure.
475  * @param[in] pkts
476  *   Packets to transmit.
477  * @param pkts_n
478  *   Number of packets in array.
479  *
480  * @return
481  *   Number of packets successfully transmitted (<= pkts_n).
482  */
483 uint16_t
484 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
485 {
486         struct txq *txq = (struct txq *)dpdk_txq;
487         unsigned int elts_head = txq->elts_head;
488         const unsigned int elts_n = txq->elts_n;
489         unsigned int elts_comp = 0;
490         unsigned int bytes_sent = 0;
491         unsigned int i;
492         unsigned int max;
493         int err;
494
495         assert(txq->elts_comp_cd != 0);
496         mlx4_txq_complete(txq);
497         max = (elts_n - (elts_head - txq->elts_tail));
498         if (max > elts_n)
499                 max -= elts_n;
500         assert(max >= 1);
501         assert(max <= elts_n);
502         /* Always leave one free entry in the ring. */
503         --max;
504         if (max == 0)
505                 return 0;
506         if (max > pkts_n)
507                 max = pkts_n;
508         for (i = 0; (i != max); ++i) {
509                 struct rte_mbuf *buf = pkts[i];
510                 unsigned int elts_head_next =
511                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
512                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
513                 struct txq_elt *elt = &(*txq->elts)[elts_head];
514
515                 /* Clean up old buffer. */
516                 if (likely(elt->buf != NULL)) {
517                         struct rte_mbuf *tmp = elt->buf;
518
519 #ifndef NDEBUG
520                         /* Poisoning. */
521                         memset(elt, 0x66, sizeof(*elt));
522 #endif
523                         /* Faster than rte_pktmbuf_free(). */
524                         do {
525                                 struct rte_mbuf *next = tmp->next;
526
527                                 rte_pktmbuf_free_seg(tmp);
528                                 tmp = next;
529                         } while (tmp != NULL);
530                 }
531                 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
532                 /* Post the packet for sending. */
533                 err = mlx4_post_send(txq, buf);
534                 if (unlikely(err)) {
535                         elt->buf = NULL;
536                         goto stop;
537                 }
538                 elt->buf = buf;
539                 bytes_sent += buf->pkt_len;
540                 ++elts_comp;
541                 elts_head = elts_head_next;
542         }
543 stop:
544         /* Take a shortcut if nothing must be sent. */
545         if (unlikely(i == 0))
546                 return 0;
547         /* Increment send statistics counters. */
548         txq->stats.opackets += i;
549         txq->stats.obytes += bytes_sent;
550         /* Make sure that descriptors are written before doorbell record. */
551         rte_wmb();
552         /* Ring QP doorbell. */
553         rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
554         txq->elts_head = elts_head;
555         txq->elts_comp += elts_comp;
556         return i;
557 }
558
559 /**
560  * Translate Rx completion flags to packet type.
561  *
562  * @param flags
563  *   Rx completion flags returned by mlx4_cqe_flags().
564  *
565  * @return
566  *   Packet type in mbuf format.
567  */
568 static inline uint32_t
569 rxq_cq_to_pkt_type(uint32_t flags)
570 {
571         uint32_t pkt_type;
572
573         if (flags & MLX4_CQE_L2_TUNNEL)
574                 pkt_type =
575                         mlx4_transpose(flags,
576                                        MLX4_CQE_L2_TUNNEL_IPV4,
577                                        RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
578                         mlx4_transpose(flags,
579                                        MLX4_CQE_STATUS_IPV4_PKT,
580                                        RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN);
581         else
582                 pkt_type = mlx4_transpose(flags,
583                                           MLX4_CQE_STATUS_IPV4_PKT,
584                                           RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
585         return pkt_type;
586 }
587
588 /**
589  * Translate Rx completion flags to offload flags.
590  *
591  * @param flags
592  *   Rx completion flags returned by mlx4_cqe_flags().
593  * @param csum
594  *   Whether Rx checksums are enabled.
595  * @param csum_l2tun
596  *   Whether Rx L2 tunnel checksums are enabled.
597  *
598  * @return
599  *   Offload flags (ol_flags) in mbuf format.
600  */
601 static inline uint32_t
602 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
603 {
604         uint32_t ol_flags = 0;
605
606         if (csum)
607                 ol_flags |=
608                         mlx4_transpose(flags,
609                                        MLX4_CQE_STATUS_IP_HDR_CSUM_OK,
610                                        PKT_RX_IP_CKSUM_GOOD) |
611                         mlx4_transpose(flags,
612                                        MLX4_CQE_STATUS_TCP_UDP_CSUM_OK,
613                                        PKT_RX_L4_CKSUM_GOOD);
614         if ((flags & MLX4_CQE_L2_TUNNEL) && csum_l2tun)
615                 ol_flags |=
616                         mlx4_transpose(flags,
617                                        MLX4_CQE_L2_TUNNEL_IPOK,
618                                        PKT_RX_IP_CKSUM_GOOD) |
619                         mlx4_transpose(flags,
620                                        MLX4_CQE_L2_TUNNEL_L4_CSUM,
621                                        PKT_RX_L4_CKSUM_GOOD);
622         return ol_flags;
623 }
624
625 /**
626  * Extract checksum information from CQE flags.
627  *
628  * @param cqe
629  *   Pointer to CQE structure.
630  * @param csum
631  *   Whether Rx checksums are enabled.
632  * @param csum_l2tun
633  *   Whether Rx L2 tunnel checksums are enabled.
634  *
635  * @return
636  *   CQE checksum information.
637  */
638 static inline uint32_t
639 mlx4_cqe_flags(struct mlx4_cqe *cqe, int csum, int csum_l2tun)
640 {
641         uint32_t flags = 0;
642
643         /*
644          * The relevant bits are in different locations on their
645          * CQE fields therefore we can join them in one 32bit
646          * variable.
647          */
648         if (csum)
649                 flags = (rte_be_to_cpu_32(cqe->status) &
650                          MLX4_CQE_STATUS_IPV4_CSUM_OK);
651         if (csum_l2tun)
652                 flags |= (rte_be_to_cpu_32(cqe->vlan_my_qpn) &
653                           (MLX4_CQE_L2_TUNNEL |
654                            MLX4_CQE_L2_TUNNEL_IPOK |
655                            MLX4_CQE_L2_TUNNEL_L4_CSUM |
656                            MLX4_CQE_L2_TUNNEL_IPV4));
657         return flags;
658 }
659
660 /**
661  * Poll one CQE from CQ.
662  *
663  * @param rxq
664  *   Pointer to the receive queue structure.
665  * @param[out] out
666  *   Just polled CQE.
667  *
668  * @return
669  *   Number of bytes of the CQE, 0 in case there is no completion.
670  */
671 static unsigned int
672 mlx4_cq_poll_one(struct rxq *rxq, struct mlx4_cqe **out)
673 {
674         int ret = 0;
675         struct mlx4_cqe *cqe = NULL;
676         struct mlx4_cq *cq = &rxq->mcq;
677
678         cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
679         if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
680             !!(cq->cons_index & cq->cqe_cnt))
681                 goto out;
682         /*
683          * Make sure we read CQ entry contents after we've checked the
684          * ownership bit.
685          */
686         rte_rmb();
687         assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
688         assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
689                MLX4_CQE_OPCODE_ERROR);
690         ret = rte_be_to_cpu_32(cqe->byte_cnt);
691         ++cq->cons_index;
692 out:
693         *out = cqe;
694         return ret;
695 }
696
697 /**
698  * DPDK callback for Rx with scattered packets support.
699  *
700  * @param dpdk_rxq
701  *   Generic pointer to Rx queue structure.
702  * @param[out] pkts
703  *   Array to store received packets.
704  * @param pkts_n
705  *   Maximum number of packets in array.
706  *
707  * @return
708  *   Number of packets successfully received (<= pkts_n).
709  */
710 uint16_t
711 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
712 {
713         struct rxq *rxq = dpdk_rxq;
714         const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
715         const uint16_t sges_n = rxq->sges_n;
716         struct rte_mbuf *pkt = NULL;
717         struct rte_mbuf *seg = NULL;
718         unsigned int i = 0;
719         uint32_t rq_ci = rxq->rq_ci << sges_n;
720         int len = 0;
721
722         while (pkts_n) {
723                 struct mlx4_cqe *cqe;
724                 uint32_t idx = rq_ci & wr_cnt;
725                 struct rte_mbuf *rep = (*rxq->elts)[idx];
726                 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
727
728                 /* Update the 'next' pointer of the previous segment. */
729                 if (pkt)
730                         seg->next = rep;
731                 seg = rep;
732                 rte_prefetch0(seg);
733                 rte_prefetch0(scat);
734                 rep = rte_mbuf_raw_alloc(rxq->mp);
735                 if (unlikely(rep == NULL)) {
736                         ++rxq->stats.rx_nombuf;
737                         if (!pkt) {
738                                 /*
739                                  * No buffers before we even started,
740                                  * bail out silently.
741                                  */
742                                 break;
743                         }
744                         while (pkt != seg) {
745                                 assert(pkt != (*rxq->elts)[idx]);
746                                 rep = pkt->next;
747                                 pkt->next = NULL;
748                                 pkt->nb_segs = 1;
749                                 rte_mbuf_raw_free(pkt);
750                                 pkt = rep;
751                         }
752                         break;
753                 }
754                 if (!pkt) {
755                         /* Looking for the new packet. */
756                         len = mlx4_cq_poll_one(rxq, &cqe);
757                         if (!len) {
758                                 rte_mbuf_raw_free(rep);
759                                 break;
760                         }
761                         if (unlikely(len < 0)) {
762                                 /* Rx error, packet is likely too large. */
763                                 rte_mbuf_raw_free(rep);
764                                 ++rxq->stats.idropped;
765                                 goto skip;
766                         }
767                         pkt = seg;
768                         if (rxq->csum | rxq->csum_l2tun) {
769                                 uint32_t flags =
770                                         mlx4_cqe_flags(cqe,
771                                                        rxq->csum,
772                                                        rxq->csum_l2tun);
773
774                                 pkt->ol_flags =
775                                         rxq_cq_to_ol_flags(flags,
776                                                            rxq->csum,
777                                                            rxq->csum_l2tun);
778                                 pkt->packet_type = rxq_cq_to_pkt_type(flags);
779                         } else {
780                                 pkt->packet_type = 0;
781                                 pkt->ol_flags = 0;
782                         }
783                         pkt->pkt_len = len;
784                 }
785                 rep->nb_segs = 1;
786                 rep->port = rxq->port_id;
787                 rep->data_len = seg->data_len;
788                 rep->data_off = seg->data_off;
789                 (*rxq->elts)[idx] = rep;
790                 /*
791                  * Fill NIC descriptor with the new buffer. The lkey and size
792                  * of the buffers are already known, only the buffer address
793                  * changes.
794                  */
795                 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
796                 if (len > seg->data_len) {
797                         len -= seg->data_len;
798                         ++pkt->nb_segs;
799                         ++rq_ci;
800                         continue;
801                 }
802                 /* The last segment. */
803                 seg->data_len = len;
804                 /* Increment bytes counter. */
805                 rxq->stats.ibytes += pkt->pkt_len;
806                 /* Return packet. */
807                 *(pkts++) = pkt;
808                 pkt = NULL;
809                 --pkts_n;
810                 ++i;
811 skip:
812                 /* Align consumer index to the next stride. */
813                 rq_ci >>= sges_n;
814                 ++rq_ci;
815                 rq_ci <<= sges_n;
816         }
817         if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
818                 return 0;
819         /* Update the consumer index. */
820         rxq->rq_ci = rq_ci >> sges_n;
821         rte_wmb();
822         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
823         *rxq->mcq.set_ci_db = rte_cpu_to_be_32(rxq->mcq.cons_index & 0xffffff);
824         /* Increment packets counter. */
825         rxq->stats.ipackets += i;
826         return i;
827 }
828
829 /**
830  * Dummy DPDK callback for Tx.
831  *
832  * This function is used to temporarily replace the real callback during
833  * unsafe control operations on the queue, or in case of error.
834  *
835  * @param dpdk_txq
836  *   Generic pointer to Tx queue structure.
837  * @param[in] pkts
838  *   Packets to transmit.
839  * @param pkts_n
840  *   Number of packets in array.
841  *
842  * @return
843  *   Number of packets successfully transmitted (<= pkts_n).
844  */
845 uint16_t
846 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
847 {
848         (void)dpdk_txq;
849         (void)pkts;
850         (void)pkts_n;
851         return 0;
852 }
853
854 /**
855  * Dummy DPDK callback for Rx.
856  *
857  * This function is used to temporarily replace the real callback during
858  * unsafe control operations on the queue, or in case of error.
859  *
860  * @param dpdk_rxq
861  *   Generic pointer to Rx queue structure.
862  * @param[out] pkts
863  *   Array to store received packets.
864  * @param pkts_n
865  *   Maximum number of packets in array.
866  *
867  * @return
868  *   Number of packets successfully received (<= pkts_n).
869  */
870 uint16_t
871 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
872 {
873         (void)dpdk_rxq;
874         (void)pkts;
875         (void)pkts_n;
876         return 0;
877 }