mlx5: support non-scattered Tx and Rx
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-pedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-pedantic"
47 #endif
48
49 /* DPDK headers don't like -pedantic. */
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic ignored "-pedantic"
52 #endif
53 #include <rte_mbuf.h>
54 #include <rte_mempool.h>
55 #include <rte_prefetch.h>
56 #include <rte_common.h>
57 #include <rte_branch_prediction.h>
58 #ifdef PEDANTIC
59 #pragma GCC diagnostic error "-pedantic"
60 #endif
61
62 #include "mlx5.h"
63 #include "mlx5_utils.h"
64 #include "mlx5_rxtx.h"
65 #include "mlx5_defs.h"
66
67 /**
68  * Manage TX completions.
69  *
70  * When sending a burst, mlx5_tx_burst() posts several WRs.
71  * To improve performance, a completion event is only required once every
72  * MLX5_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
73  * for other WRs, but this information would not be used anyway.
74  *
75  * @param txq
76  *   Pointer to TX queue structure.
77  *
78  * @return
79  *   0 on success, -1 on failure.
80  */
81 static int
82 txq_complete(struct txq *txq)
83 {
84         unsigned int elts_comp = txq->elts_comp;
85         unsigned int elts_tail = txq->elts_tail;
86         const unsigned int elts_n = txq->elts_n;
87         int wcs_n;
88
89         if (unlikely(elts_comp == 0))
90                 return 0;
91 #ifdef DEBUG_SEND
92         DEBUG("%p: processing %u work requests completions",
93               (void *)txq, elts_comp);
94 #endif
95         wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
96         if (unlikely(wcs_n == 0))
97                 return 0;
98         if (unlikely(wcs_n < 0)) {
99                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
100                       (void *)txq, wcs_n);
101                 return -1;
102         }
103         elts_comp -= wcs_n;
104         assert(elts_comp <= txq->elts_comp);
105         /*
106          * Assume WC status is successful as nothing can be done about it
107          * anyway.
108          */
109         elts_tail += wcs_n * txq->elts_comp_cd_init;
110         if (elts_tail >= elts_n)
111                 elts_tail -= elts_n;
112         txq->elts_tail = elts_tail;
113         txq->elts_comp = elts_comp;
114         return 0;
115 }
116
117 /**
118  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
119  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
120  * remove an entry first.
121  *
122  * @param txq
123  *   Pointer to TX queue structure.
124  * @param[in] mp
125  *   Memory Pool for which a Memory Region lkey must be returned.
126  *
127  * @return
128  *   mr->lkey on success, (uint32_t)-1 on failure.
129  */
130 static uint32_t
131 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
132 {
133         unsigned int i;
134         struct ibv_mr *mr;
135
136         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
137                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
138                         /* Unknown MP, add a new MR for it. */
139                         break;
140                 }
141                 if (txq->mp2mr[i].mp == mp) {
142                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
143                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
144                         return txq->mp2mr[i].lkey;
145                 }
146         }
147         /* Add a new entry, register MR first. */
148         DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
149         mr = ibv_reg_mr(txq->priv->pd,
150                         (void *)mp->elt_va_start,
151                         (mp->elt_va_end - mp->elt_va_start),
152                         (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
153         if (unlikely(mr == NULL)) {
154                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
155                       (void *)txq);
156                 return (uint32_t)-1;
157         }
158         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
159                 /* Table is full, remove oldest entry. */
160                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
161                       (void *)txq);
162                 --i;
163                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
164                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
165                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
166         }
167         /* Store the new entry. */
168         txq->mp2mr[i].mp = mp;
169         txq->mp2mr[i].mr = mr;
170         txq->mp2mr[i].lkey = mr->lkey;
171         DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
172               (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
173         return txq->mp2mr[i].lkey;
174 }
175
176 /**
177  * DPDK callback for TX.
178  *
179  * @param dpdk_txq
180  *   Generic pointer to TX queue structure.
181  * @param[in] pkts
182  *   Packets to transmit.
183  * @param pkts_n
184  *   Number of packets in array.
185  *
186  * @return
187  *   Number of packets successfully transmitted (<= pkts_n).
188  */
189 uint16_t
190 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
191 {
192         struct txq *txq = (struct txq *)dpdk_txq;
193         unsigned int elts_head = txq->elts_head;
194         const unsigned int elts_tail = txq->elts_tail;
195         const unsigned int elts_n = txq->elts_n;
196         unsigned int elts_comp_cd = txq->elts_comp_cd;
197         unsigned int elts_comp = 0;
198         unsigned int i;
199         unsigned int max;
200         int err;
201
202         assert(elts_comp_cd != 0);
203         txq_complete(txq);
204         max = (elts_n - (elts_head - elts_tail));
205         if (max > elts_n)
206                 max -= elts_n;
207         assert(max >= 1);
208         assert(max <= elts_n);
209         /* Always leave one free entry in the ring. */
210         --max;
211         if (max == 0)
212                 return 0;
213         if (max > pkts_n)
214                 max = pkts_n;
215         for (i = 0; (i != max); ++i) {
216                 struct rte_mbuf *buf = pkts[i];
217                 unsigned int elts_head_next =
218                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
219                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
220                 struct txq_elt *elt = &(*txq->elts)[elts_head];
221                 unsigned int segs = NB_SEGS(buf);
222                 uint32_t send_flags = 0;
223
224                 /* Clean up old buffer. */
225                 if (likely(elt->buf != NULL)) {
226                         struct rte_mbuf *tmp = elt->buf;
227
228                         /* Faster than rte_pktmbuf_free(). */
229                         do {
230                                 struct rte_mbuf *next = NEXT(tmp);
231
232                                 rte_pktmbuf_free_seg(tmp);
233                                 tmp = next;
234                         } while (tmp != NULL);
235                 }
236                 /* Request TX completion. */
237                 if (unlikely(--elts_comp_cd == 0)) {
238                         elts_comp_cd = txq->elts_comp_cd_init;
239                         ++elts_comp;
240                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
241                 }
242                 if (likely(segs == 1)) {
243                         uintptr_t addr;
244                         uint32_t length;
245                         uint32_t lkey;
246
247                         /* Retrieve buffer information. */
248                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
249                         length = DATA_LEN(buf);
250                         /* Retrieve Memory Region key for this memory pool. */
251                         lkey = txq_mp2mr(txq, buf->pool);
252                         if (unlikely(lkey == (uint32_t)-1)) {
253                                 /* MR does not exist. */
254                                 DEBUG("%p: unable to get MP <-> MR"
255                                       " association", (void *)txq);
256                                 /* Clean up TX element. */
257                                 elt->buf = NULL;
258                                 goto stop;
259                         }
260                         /* Update element. */
261                         elt->buf = buf;
262                         if (txq->priv->vf)
263                                 rte_prefetch0((volatile void *)
264                                               (uintptr_t)addr);
265                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
266                         /* Put packet into send queue. */
267 #if MLX5_PMD_MAX_INLINE > 0
268                         if (length <= txq->max_inline)
269                                 err = txq->if_qp->send_pending_inline
270                                         (txq->qp,
271                                          (void *)addr,
272                                          length,
273                                          send_flags);
274                         else
275 #endif
276                                 err = txq->if_qp->send_pending
277                                         (txq->qp,
278                                          addr,
279                                          length,
280                                          lkey,
281                                          send_flags);
282                         if (unlikely(err))
283                                 goto stop;
284                 } else {
285                         DEBUG("%p: TX scattered buffers support not"
286                               " compiled in", (void *)txq);
287                         goto stop;
288                 }
289                 elts_head = elts_head_next;
290         }
291 stop:
292         /* Take a shortcut if nothing must be sent. */
293         if (unlikely(i == 0))
294                 return 0;
295         /* Ring QP doorbell. */
296         err = txq->if_qp->send_flush(txq->qp);
297         if (unlikely(err)) {
298                 /* A nonzero value is not supposed to be returned.
299                  * Nothing can be done about it. */
300                 DEBUG("%p: send_flush() failed with error %d",
301                       (void *)txq, err);
302         }
303         txq->elts_head = elts_head;
304         txq->elts_comp += elts_comp;
305         txq->elts_comp_cd = elts_comp_cd;
306         return i;
307 }
308
309 /**
310  * DPDK callback for RX.
311  *
312  * @param dpdk_rxq
313  *   Generic pointer to RX queue structure.
314  * @param[out] pkts
315  *   Array to store received packets.
316  * @param pkts_n
317  *   Maximum number of packets in array.
318  *
319  * @return
320  *   Number of packets successfully received (<= pkts_n).
321  */
322 uint16_t
323 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
324 {
325         struct rxq *rxq = (struct rxq *)dpdk_rxq;
326         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
327         const unsigned int elts_n = rxq->elts_n;
328         unsigned int elts_head = rxq->elts_head;
329         struct ibv_sge sges[pkts_n];
330         unsigned int i;
331         unsigned int pkts_ret = 0;
332         int ret;
333
334         for (i = 0; (i != pkts_n); ++i) {
335                 struct rxq_elt *elt = &(*elts)[elts_head];
336                 struct ibv_recv_wr *wr = &elt->wr;
337                 uint64_t wr_id = wr->wr_id;
338                 unsigned int len;
339                 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
340                         WR_ID(wr_id).offset);
341                 struct rte_mbuf *rep;
342                 uint32_t flags;
343
344                 /* Sanity checks. */
345                 assert(WR_ID(wr_id).id < rxq->elts_n);
346                 assert(wr->sg_list == &elt->sge);
347                 assert(wr->num_sge == 1);
348                 assert(elts_head < rxq->elts_n);
349                 assert(rxq->elts_head < rxq->elts_n);
350                 /*
351                  * Fetch initial bytes of packet descriptor into a
352                  * cacheline while allocating rep.
353                  */
354                 rte_prefetch0(seg);
355                 rte_prefetch0(&seg->cacheline1);
356                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
357                                                     &flags);
358                 if (unlikely(ret < 0)) {
359                         struct ibv_wc wc;
360                         int wcs_n;
361
362                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
363                               (void *)rxq, ret);
364                         /* ibv_poll_cq() must be used in case of failure. */
365                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
366                         if (unlikely(wcs_n == 0))
367                                 break;
368                         if (unlikely(wcs_n < 0)) {
369                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
370                                       (void *)rxq, wcs_n);
371                                 break;
372                         }
373                         assert(wcs_n == 1);
374                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
375                                 /* Whatever, just repost the offending WR. */
376                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
377                                       " completion status (%d): %s",
378                                       (void *)rxq, wc.wr_id, wc.status,
379                                       ibv_wc_status_str(wc.status));
380                                 /* Add SGE to array for repost. */
381                                 sges[i] = elt->sge;
382                                 goto repost;
383                         }
384                         ret = wc.byte_len;
385                 }
386                 if (ret == 0)
387                         break;
388                 len = ret;
389                 rep = __rte_mbuf_raw_alloc(rxq->mp);
390                 if (unlikely(rep == NULL)) {
391                         /*
392                          * Unable to allocate a replacement mbuf,
393                          * repost WR.
394                          */
395                         DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
396                               " can't allocate a new mbuf",
397                               (void *)rxq, WR_ID(wr_id).id);
398                         /* Increment out of memory counters. */
399                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
400                         goto repost;
401                 }
402
403                 /* Reconfigure sge to use rep instead of seg. */
404                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
405                 assert(elt->sge.lkey == rxq->mr->lkey);
406                 WR_ID(wr->wr_id).offset =
407                         (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
408                          (uintptr_t)rep);
409                 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
410
411                 /* Add SGE to array for repost. */
412                 sges[i] = elt->sge;
413
414                 /* Update seg information. */
415                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
416                 NB_SEGS(seg) = 1;
417                 PORT(seg) = rxq->port_id;
418                 NEXT(seg) = NULL;
419                 PKT_LEN(seg) = len;
420                 DATA_LEN(seg) = len;
421
422                 /* Return packet. */
423                 *(pkts++) = seg;
424                 ++pkts_ret;
425 repost:
426                 if (++elts_head >= elts_n)
427                         elts_head = 0;
428                 continue;
429         }
430         if (unlikely(i == 0))
431                 return 0;
432         /* Repost WRs. */
433 #ifdef DEBUG_RECV
434         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
435 #endif
436         ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
437         if (unlikely(ret)) {
438                 /* Inability to repost WRs is fatal. */
439                 DEBUG("%p: recv_burst(): failed (ret=%d)",
440                       (void *)rxq->priv,
441                       ret);
442                 abort();
443         }
444         rxq->elts_head = elts_head;
445         return pkts_ret;
446 }
447
448 /**
449  * Dummy DPDK callback for TX.
450  *
451  * This function is used to temporarily replace the real callback during
452  * unsafe control operations on the queue, or in case of error.
453  *
454  * @param dpdk_txq
455  *   Generic pointer to TX queue structure.
456  * @param[in] pkts
457  *   Packets to transmit.
458  * @param pkts_n
459  *   Number of packets in array.
460  *
461  * @return
462  *   Number of packets successfully transmitted (<= pkts_n).
463  */
464 uint16_t
465 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
466 {
467         (void)dpdk_txq;
468         (void)pkts;
469         (void)pkts_n;
470         return 0;
471 }
472
473 /**
474  * Dummy DPDK callback for RX.
475  *
476  * This function is used to temporarily replace the real callback during
477  * unsafe control operations on the queue, or in case of error.
478  *
479  * @param dpdk_rxq
480  *   Generic pointer to RX queue structure.
481  * @param[out] pkts
482  *   Array to store received packets.
483  * @param pkts_n
484  *   Maximum number of packets in array.
485  *
486  * @return
487  *   Number of packets successfully received (<= pkts_n).
488  */
489 uint16_t
490 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
491 {
492         (void)dpdk_rxq;
493         (void)pkts;
494         (void)pkts_n;
495         return 0;
496 }