common/mlx5: share MR management
[dpdk.git] / drivers / net / mlx5 / mlx5_rx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2021 6WIND S.A.
3  * Copyright 2021 Mellanox Technologies, Ltd
4  */
5
6 #ifndef RTE_PMD_MLX5_RX_H_
7 #define RTE_PMD_MLX5_RX_H_
8
9 #include <stdint.h>
10 #include <sys/queue.h>
11
12 #include <rte_mbuf.h>
13 #include <rte_mempool.h>
14 #include <rte_common.h>
15 #include <rte_spinlock.h>
16
17 #include <mlx5_common_mr.h>
18
19 #include "mlx5.h"
20 #include "mlx5_autoconf.h"
21
22 /* Support tunnel matching. */
23 #define MLX5_FLOW_TUNNEL 10
24
25 /* First entry must be NULL for comparison. */
26 #define mlx5_mr_btree_len(bt) ((bt)->len - 1)
27
28 struct mlx5_rxq_stats {
29 #ifdef MLX5_PMD_SOFT_COUNTERS
30         uint64_t ipackets; /**< Total of successfully received packets. */
31         uint64_t ibytes; /**< Total of successfully received bytes. */
32 #endif
33         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
34         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
35 };
36
37 /* Compressed CQE context. */
38 struct rxq_zip {
39         uint16_t ai; /* Array index. */
40         uint16_t ca; /* Current array index. */
41         uint16_t na; /* Next array index. */
42         uint16_t cq_ci; /* The next CQE. */
43         uint32_t cqe_cnt; /* Number of CQEs. */
44 };
45
46 /* Multi-Packet RQ buffer header. */
47 struct mlx5_mprq_buf {
48         struct rte_mempool *mp;
49         uint16_t refcnt; /* Atomically accessed refcnt. */
50         uint8_t pad[RTE_PKTMBUF_HEADROOM]; /* Headroom for the first packet. */
51         struct rte_mbuf_ext_shared_info shinfos[];
52         /*
53          * Shared information per stride.
54          * More memory will be allocated for the first stride head-room and for
55          * the strides data.
56          */
57 } __rte_cache_aligned;
58
59 /* Get pointer to the first stride. */
60 #define mlx5_mprq_buf_addr(ptr, strd_n) (RTE_PTR_ADD((ptr), \
61                                 sizeof(struct mlx5_mprq_buf) + \
62                                 (strd_n) * \
63                                 sizeof(struct rte_mbuf_ext_shared_info) + \
64                                 RTE_PKTMBUF_HEADROOM))
65
66 #define MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES 6
67 #define MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES 9
68
69 enum mlx5_rxq_err_state {
70         MLX5_RXQ_ERR_STATE_NO_ERROR = 0,
71         MLX5_RXQ_ERR_STATE_NEED_RESET,
72         MLX5_RXQ_ERR_STATE_NEED_READY,
73 };
74
75 enum mlx5_rqx_code {
76         MLX5_RXQ_CODE_EXIT = 0,
77         MLX5_RXQ_CODE_NOMBUF,
78         MLX5_RXQ_CODE_DROPPED,
79 };
80
81 struct mlx5_eth_rxseg {
82         struct rte_mempool *mp; /**< Memory pool to allocate segment from. */
83         uint16_t length; /**< Segment data length, configures split point. */
84         uint16_t offset; /**< Data offset from beginning of mbuf data buffer. */
85         uint32_t reserved; /**< Reserved field. */
86 };
87
88 /* RX queue descriptor. */
89 struct mlx5_rxq_data {
90         unsigned int csum:1; /* Enable checksum offloading. */
91         unsigned int hw_timestamp:1; /* Enable HW timestamp. */
92         unsigned int rt_timestamp:1; /* Realtime timestamp format. */
93         unsigned int vlan_strip:1; /* Enable VLAN stripping. */
94         unsigned int crc_present:1; /* CRC must be subtracted. */
95         unsigned int sges_n:3; /* Log 2 of SGEs (max buffers per packet). */
96         unsigned int cqe_n:4; /* Log 2 of CQ elements. */
97         unsigned int elts_n:4; /* Log 2 of Mbufs. */
98         unsigned int rss_hash:1; /* RSS hash result is enabled. */
99         unsigned int mark:1; /* Marked flow available on the queue. */
100         unsigned int strd_num_n:5; /* Log 2 of the number of stride. */
101         unsigned int strd_sz_n:4; /* Log 2 of stride size. */
102         unsigned int strd_shift_en:1; /* Enable 2bytes shift on a stride. */
103         unsigned int err_state:2; /* enum mlx5_rxq_err_state. */
104         unsigned int strd_scatter_en:1; /* Scattered packets from a stride. */
105         unsigned int lro:1; /* Enable LRO. */
106         unsigned int dynf_meta:1; /* Dynamic metadata is configured. */
107         unsigned int mcqe_format:3; /* CQE compression format. */
108         volatile uint32_t *rq_db;
109         volatile uint32_t *cq_db;
110         uint16_t port_id;
111         uint32_t elts_ci;
112         uint32_t rq_ci;
113         uint16_t consumed_strd; /* Number of consumed strides in WQE. */
114         uint32_t rq_pi;
115         uint32_t cq_ci;
116         uint16_t rq_repl_thresh; /* Threshold for buffer replenishment. */
117         uint32_t byte_mask;
118         union {
119                 struct rxq_zip zip; /* Compressed context. */
120                 uint16_t decompressed;
121                 /* Number of ready mbufs decompressed from the CQ. */
122         };
123         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
124         uint16_t mprq_max_memcpy_len; /* Maximum size of packet to memcpy. */
125         volatile void *wqes;
126         volatile struct mlx5_cqe(*cqes)[];
127         struct rte_mbuf *(*elts)[];
128         struct mlx5_mprq_buf *(*mprq_bufs)[];
129         struct rte_mempool *mp;
130         struct rte_mempool *mprq_mp; /* Mempool for Multi-Packet RQ. */
131         struct mlx5_mprq_buf *mprq_repl; /* Stashed mbuf for replenish. */
132         struct mlx5_dev_ctx_shared *sh; /* Shared context. */
133         uint16_t idx; /* Queue index. */
134         struct mlx5_rxq_stats stats;
135         rte_xmm_t mbuf_initializer; /* Default rearm/flags for vectorized Rx. */
136         struct rte_mbuf fake_mbuf; /* elts padding for vectorized Rx. */
137         void *cq_uar; /* Verbs CQ user access region. */
138         uint32_t cqn; /* CQ number. */
139         uint8_t cq_arm_sn; /* CQ arm seq number. */
140 #ifndef RTE_ARCH_64
141         rte_spinlock_t *uar_lock_cq;
142         /* CQ (UAR) access lock required for 32bit implementations */
143 #endif
144         uint32_t tunnel; /* Tunnel information. */
145         int timestamp_offset; /* Dynamic mbuf field for timestamp. */
146         uint64_t timestamp_rx_flag; /* Dynamic mbuf flag for timestamp. */
147         uint64_t flow_meta_mask;
148         int32_t flow_meta_offset;
149         uint32_t flow_meta_port_mask;
150         uint32_t rxseg_n; /* Number of split segment descriptions. */
151         struct mlx5_eth_rxseg rxseg[MLX5_MAX_RXQ_NSEG];
152         /* Buffer split segment descriptions - sizes, offsets, pools. */
153 } __rte_cache_aligned;
154
155 enum mlx5_rxq_type {
156         MLX5_RXQ_TYPE_STANDARD, /* Standard Rx queue. */
157         MLX5_RXQ_TYPE_HAIRPIN, /* Hairpin Rx queue. */
158         MLX5_RXQ_TYPE_UNDEFINED,
159 };
160
161 /* RX queue control descriptor. */
162 struct mlx5_rxq_ctrl {
163         struct mlx5_rxq_data rxq; /* Data path structure. */
164         LIST_ENTRY(mlx5_rxq_ctrl) next; /* Pointer to the next element. */
165         uint32_t refcnt; /* Reference counter. */
166         struct mlx5_rxq_obj *obj; /* Verbs/DevX elements. */
167         struct mlx5_priv *priv; /* Back pointer to private data. */
168         enum mlx5_rxq_type type; /* Rxq type. */
169         unsigned int socket; /* CPU socket ID for allocations. */
170         unsigned int irq:1; /* Whether IRQ is enabled. */
171         uint32_t flow_mark_n; /* Number of Mark/Flag flows using this Queue. */
172         uint32_t flow_tunnels_n[MLX5_FLOW_TUNNEL]; /* Tunnels counters. */
173         uint32_t wqn; /* WQ number. */
174         uint16_t dump_file_n; /* Number of dump files. */
175         struct rte_eth_hairpin_conf hairpin_conf; /* Hairpin configuration. */
176         uint32_t hairpin_status; /* Hairpin binding status. */
177 };
178
179 /* mlx5_rxq.c */
180
181 extern uint8_t rss_hash_default_key[];
182
183 unsigned int mlx5_rxq_cqe_num(struct mlx5_rxq_data *rxq_data);
184 int mlx5_mprq_free_mp(struct rte_eth_dev *dev);
185 int mlx5_mprq_alloc_mp(struct rte_eth_dev *dev);
186 int mlx5_rx_queue_start(struct rte_eth_dev *dev, uint16_t queue_id);
187 int mlx5_rx_queue_stop(struct rte_eth_dev *dev, uint16_t queue_id);
188 int mlx5_rx_queue_start_primary(struct rte_eth_dev *dev, uint16_t queue_id);
189 int mlx5_rx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t queue_id);
190 int mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
191                         unsigned int socket, const struct rte_eth_rxconf *conf,
192                         struct rte_mempool *mp);
193 int mlx5_rx_hairpin_queue_setup
194         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
195          const struct rte_eth_hairpin_conf *hairpin_conf);
196 void mlx5_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid);
197 int mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev);
198 void mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev);
199 int mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
200 int mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
201 int mlx5_rxq_obj_verify(struct rte_eth_dev *dev);
202 struct mlx5_rxq_ctrl *mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx,
203                                    uint16_t desc, unsigned int socket,
204                                    const struct rte_eth_rxconf *conf,
205                                    const struct rte_eth_rxseg_split *rx_seg,
206                                    uint16_t n_seg);
207 struct mlx5_rxq_ctrl *mlx5_rxq_hairpin_new
208         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
209          const struct rte_eth_hairpin_conf *hairpin_conf);
210 struct mlx5_rxq_ctrl *mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx);
211 int mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx);
212 int mlx5_rxq_verify(struct rte_eth_dev *dev);
213 int rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl);
214 int mlx5_ind_table_obj_verify(struct rte_eth_dev *dev);
215 struct mlx5_ind_table_obj *mlx5_ind_table_obj_get(struct rte_eth_dev *dev,
216                                                   const uint16_t *queues,
217                                                   uint32_t queues_n);
218 int mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
219                                struct mlx5_ind_table_obj *ind_tbl,
220                                bool standalone);
221 int mlx5_ind_table_obj_setup(struct rte_eth_dev *dev,
222                              struct mlx5_ind_table_obj *ind_tbl);
223 int mlx5_ind_table_obj_modify(struct rte_eth_dev *dev,
224                               struct mlx5_ind_table_obj *ind_tbl,
225                               uint16_t *queues, const uint32_t queues_n,
226                               bool standalone);
227 struct mlx5_list_entry *mlx5_hrxq_create_cb(void *tool_ctx, void *cb_ctx);
228 int mlx5_hrxq_match_cb(void *tool_ctx, struct mlx5_list_entry *entry,
229                        void *cb_ctx);
230 void mlx5_hrxq_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry);
231 struct mlx5_list_entry *mlx5_hrxq_clone_cb(void *tool_ctx,
232                                            struct mlx5_list_entry *entry,
233                                            void *cb_ctx __rte_unused);
234 void mlx5_hrxq_clone_free_cb(void *tool_ctx __rte_unused,
235                              struct mlx5_list_entry *entry);
236 uint32_t mlx5_hrxq_get(struct rte_eth_dev *dev,
237                        struct mlx5_flow_rss_desc *rss_desc);
238 int mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hxrq_idx);
239 uint32_t mlx5_hrxq_verify(struct rte_eth_dev *dev);
240 enum mlx5_rxq_type mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx);
241 const struct rte_eth_hairpin_conf *mlx5_rxq_get_hairpin_conf
242         (struct rte_eth_dev *dev, uint16_t idx);
243 struct mlx5_hrxq *mlx5_drop_action_create(struct rte_eth_dev *dev);
244 void mlx5_drop_action_destroy(struct rte_eth_dev *dev);
245 uint64_t mlx5_get_rx_port_offloads(void);
246 uint64_t mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev);
247 void mlx5_rxq_timestamp_set(struct rte_eth_dev *dev);
248 int mlx5_hrxq_modify(struct rte_eth_dev *dev, uint32_t hxrq_idx,
249                      const uint8_t *rss_key, uint32_t rss_key_len,
250                      uint64_t hash_fields,
251                      const uint16_t *queues, uint32_t queues_n);
252
253 /* mlx5_rx.c */
254
255 uint16_t mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
256 void mlx5_rxq_initialize(struct mlx5_rxq_data *rxq);
257 __rte_noinline int mlx5_rx_err_handle(struct mlx5_rxq_data *rxq, uint8_t vec);
258 void mlx5_mprq_buf_free_cb(void *addr, void *opaque);
259 void mlx5_mprq_buf_free(struct mlx5_mprq_buf *buf);
260 uint16_t mlx5_rx_burst_mprq(void *dpdk_rxq, struct rte_mbuf **pkts,
261                             uint16_t pkts_n);
262 uint16_t removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts,
263                           uint16_t pkts_n);
264 int mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset);
265 uint32_t mlx5_rx_queue_count(void *rx_queue);
266 void mlx5_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
267                        struct rte_eth_rxq_info *qinfo);
268 int mlx5_rx_burst_mode_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
269                            struct rte_eth_burst_mode *mode);
270 int mlx5_get_monitor_addr(void *rx_queue, struct rte_power_monitor_cond *pmc);
271
272 /* Vectorized version of mlx5_rx.c */
273 int mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq_data);
274 int mlx5_check_vec_rx_support(struct rte_eth_dev *dev);
275 uint16_t mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts,
276                            uint16_t pkts_n);
277 uint16_t mlx5_rx_burst_mprq_vec(void *dpdk_rxq, struct rte_mbuf **pkts,
278                                 uint16_t pkts_n);
279
280 static int mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq);
281
282 /**
283  * Query LKey from a packet buffer for Rx. No need to flush local caches
284  * as the Rx mempool database entries are valid for the lifetime of the queue.
285  *
286  * @param rxq
287  *   Pointer to Rx queue structure.
288  * @param addr
289  *   Address to search.
290  *
291  * @return
292  *   Searched LKey on success, UINT32_MAX on no match.
293  *   This function always succeeds on valid input.
294  */
295 static __rte_always_inline uint32_t
296 mlx5_rx_addr2mr(struct mlx5_rxq_data *rxq, uintptr_t addr)
297 {
298         struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
299         struct mlx5_rxq_ctrl *rxq_ctrl;
300         struct rte_mempool *mp;
301         uint32_t lkey;
302
303         /* Linear search on MR cache array. */
304         lkey = mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru,
305                                    MLX5_MR_CACHE_N, addr);
306         if (likely(lkey != UINT32_MAX))
307                 return lkey;
308         /*
309          * Slower search in the mempool database on miss.
310          * During queue creation rxq->sh is not yet set, so we use rxq_ctrl.
311          */
312         rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
313         mp = mlx5_rxq_mprq_enabled(rxq) ? rxq->mprq_mp : rxq->mp;
314         return mlx5_mr_mempool2mr_bh(&rxq_ctrl->priv->sh->cdev->mr_scache,
315                                      mr_ctrl, mp, addr);
316 }
317
318 #define mlx5_rx_mb2mr(rxq, mb) mlx5_rx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
319
320 /**
321  * Convert timestamp from HW format to linear counter
322  * from Packet Pacing Clock Queue CQE timestamp format.
323  *
324  * @param sh
325  *   Pointer to the device shared context. Might be needed
326  *   to convert according current device configuration.
327  * @param ts
328  *   Timestamp from CQE to convert.
329  * @return
330  *   UTC in nanoseconds
331  */
332 static __rte_always_inline uint64_t
333 mlx5_txpp_convert_rx_ts(struct mlx5_dev_ctx_shared *sh, uint64_t ts)
334 {
335         RTE_SET_USED(sh);
336         return (ts & UINT32_MAX) + (ts >> 32) * NS_PER_S;
337 }
338
339 /**
340  * Set timestamp in mbuf dynamic field.
341  *
342  * @param mbuf
343  *   Structure to write into.
344  * @param offset
345  *   Dynamic field offset in mbuf structure.
346  * @param timestamp
347  *   Value to write.
348  */
349 static __rte_always_inline void
350 mlx5_timestamp_set(struct rte_mbuf *mbuf, int offset,
351                 rte_mbuf_timestamp_t timestamp)
352 {
353         *RTE_MBUF_DYNFIELD(mbuf, offset, rte_mbuf_timestamp_t *) = timestamp;
354 }
355
356 /**
357  * Replace MPRQ buffer.
358  *
359  * @param rxq
360  *   Pointer to Rx queue structure.
361  * @param rq_idx
362  *   RQ index to replace.
363  */
364 static __rte_always_inline void
365 mprq_buf_replace(struct mlx5_rxq_data *rxq, uint16_t rq_idx)
366 {
367         const uint32_t strd_n = 1 << rxq->strd_num_n;
368         struct mlx5_mprq_buf *rep = rxq->mprq_repl;
369         volatile struct mlx5_wqe_data_seg *wqe =
370                 &((volatile struct mlx5_wqe_mprq *)rxq->wqes)[rq_idx].dseg;
371         struct mlx5_mprq_buf *buf = (*rxq->mprq_bufs)[rq_idx];
372         void *addr;
373
374         if (__atomic_load_n(&buf->refcnt, __ATOMIC_RELAXED) > 1) {
375                 MLX5_ASSERT(rep != NULL);
376                 /* Replace MPRQ buf. */
377                 (*rxq->mprq_bufs)[rq_idx] = rep;
378                 /* Replace WQE. */
379                 addr = mlx5_mprq_buf_addr(rep, strd_n);
380                 wqe->addr = rte_cpu_to_be_64((uintptr_t)addr);
381                 /* If there's only one MR, no need to replace LKey in WQE. */
382                 if (unlikely(mlx5_mr_btree_len(&rxq->mr_ctrl.cache_bh) > 1))
383                         wqe->lkey = mlx5_rx_addr2mr(rxq, (uintptr_t)addr);
384                 /* Stash a mbuf for next replacement. */
385                 if (likely(!rte_mempool_get(rxq->mprq_mp, (void **)&rep)))
386                         rxq->mprq_repl = rep;
387                 else
388                         rxq->mprq_repl = NULL;
389                 /* Release the old buffer. */
390                 mlx5_mprq_buf_free(buf);
391         } else if (unlikely(rxq->mprq_repl == NULL)) {
392                 struct mlx5_mprq_buf *rep;
393
394                 /*
395                  * Currently, the MPRQ mempool is out of buffer
396                  * and doing memcpy regardless of the size of Rx
397                  * packet. Retry allocation to get back to
398                  * normal.
399                  */
400                 if (!rte_mempool_get(rxq->mprq_mp, (void **)&rep))
401                         rxq->mprq_repl = rep;
402         }
403 }
404
405 /**
406  * Attach or copy MPRQ buffer content to a packet.
407  *
408  * @param rxq
409  *   Pointer to Rx queue structure.
410  * @param pkt
411  *   Pointer to a packet to fill.
412  * @param len
413  *   Packet length.
414  * @param buf
415  *   Pointer to a MPRQ buffer to take the data from.
416  * @param strd_idx
417  *   Stride index to start from.
418  * @param strd_cnt
419  *   Number of strides to consume.
420  */
421 static __rte_always_inline enum mlx5_rqx_code
422 mprq_buf_to_pkt(struct mlx5_rxq_data *rxq, struct rte_mbuf *pkt, uint32_t len,
423                 struct mlx5_mprq_buf *buf, uint16_t strd_idx, uint16_t strd_cnt)
424 {
425         const uint32_t strd_n = 1 << rxq->strd_num_n;
426         const uint16_t strd_sz = 1 << rxq->strd_sz_n;
427         const uint16_t strd_shift =
428                 MLX5_MPRQ_STRIDE_SHIFT_BYTE * rxq->strd_shift_en;
429         const int32_t hdrm_overlap =
430                 len + RTE_PKTMBUF_HEADROOM - strd_cnt * strd_sz;
431         const uint32_t offset = strd_idx * strd_sz + strd_shift;
432         void *addr = RTE_PTR_ADD(mlx5_mprq_buf_addr(buf, strd_n), offset);
433
434         /*
435          * Memcpy packets to the target mbuf if:
436          * - The size of packet is smaller than mprq_max_memcpy_len.
437          * - Out of buffer in the Mempool for Multi-Packet RQ.
438          * - The packet's stride overlaps a headroom and scatter is off.
439          */
440         if (len <= rxq->mprq_max_memcpy_len ||
441             rxq->mprq_repl == NULL ||
442             (hdrm_overlap > 0 && !rxq->strd_scatter_en)) {
443                 if (likely(len <=
444                            (uint32_t)(pkt->buf_len - RTE_PKTMBUF_HEADROOM))) {
445                         rte_memcpy(rte_pktmbuf_mtod(pkt, void *),
446                                    addr, len);
447                         DATA_LEN(pkt) = len;
448                 } else if (rxq->strd_scatter_en) {
449                         struct rte_mbuf *prev = pkt;
450                         uint32_t seg_len = RTE_MIN(len, (uint32_t)
451                                 (pkt->buf_len - RTE_PKTMBUF_HEADROOM));
452                         uint32_t rem_len = len - seg_len;
453
454                         rte_memcpy(rte_pktmbuf_mtod(pkt, void *),
455                                    addr, seg_len);
456                         DATA_LEN(pkt) = seg_len;
457                         while (rem_len) {
458                                 struct rte_mbuf *next =
459                                         rte_pktmbuf_alloc(rxq->mp);
460
461                                 if (unlikely(next == NULL))
462                                         return MLX5_RXQ_CODE_NOMBUF;
463                                 NEXT(prev) = next;
464                                 SET_DATA_OFF(next, 0);
465                                 addr = RTE_PTR_ADD(addr, seg_len);
466                                 seg_len = RTE_MIN(rem_len, (uint32_t)
467                                         (next->buf_len - RTE_PKTMBUF_HEADROOM));
468                                 rte_memcpy
469                                         (rte_pktmbuf_mtod(next, void *),
470                                          addr, seg_len);
471                                 DATA_LEN(next) = seg_len;
472                                 rem_len -= seg_len;
473                                 prev = next;
474                                 ++NB_SEGS(pkt);
475                         }
476                 } else {
477                         return MLX5_RXQ_CODE_DROPPED;
478                 }
479         } else {
480                 rte_iova_t buf_iova;
481                 struct rte_mbuf_ext_shared_info *shinfo;
482                 uint16_t buf_len = strd_cnt * strd_sz;
483                 void *buf_addr;
484
485                 /* Increment the refcnt of the whole chunk. */
486                 __atomic_add_fetch(&buf->refcnt, 1, __ATOMIC_RELAXED);
487                 MLX5_ASSERT(__atomic_load_n(&buf->refcnt,
488                             __ATOMIC_RELAXED) <= strd_n + 1);
489                 buf_addr = RTE_PTR_SUB(addr, RTE_PKTMBUF_HEADROOM);
490                 /*
491                  * MLX5 device doesn't use iova but it is necessary in a
492                  * case where the Rx packet is transmitted via a
493                  * different PMD.
494                  */
495                 buf_iova = rte_mempool_virt2iova(buf) +
496                            RTE_PTR_DIFF(buf_addr, buf);
497                 shinfo = &buf->shinfos[strd_idx];
498                 rte_mbuf_ext_refcnt_set(shinfo, 1);
499                 /*
500                  * EXT_ATTACHED_MBUF will be set to pkt->ol_flags when
501                  * attaching the stride to mbuf and more offload flags
502                  * will be added below by calling rxq_cq_to_mbuf().
503                  * Other fields will be overwritten.
504                  */
505                 rte_pktmbuf_attach_extbuf(pkt, buf_addr, buf_iova,
506                                           buf_len, shinfo);
507                 /* Set mbuf head-room. */
508                 SET_DATA_OFF(pkt, RTE_PKTMBUF_HEADROOM);
509                 MLX5_ASSERT(pkt->ol_flags == EXT_ATTACHED_MBUF);
510                 MLX5_ASSERT(rte_pktmbuf_tailroom(pkt) >=
511                         len - (hdrm_overlap > 0 ? hdrm_overlap : 0));
512                 DATA_LEN(pkt) = len;
513                 /*
514                  * Copy the last fragment of a packet (up to headroom
515                  * size bytes) in case there is a stride overlap with
516                  * a next packet's headroom. Allocate a separate mbuf
517                  * to store this fragment and link it. Scatter is on.
518                  */
519                 if (hdrm_overlap > 0) {
520                         MLX5_ASSERT(rxq->strd_scatter_en);
521                         struct rte_mbuf *seg =
522                                 rte_pktmbuf_alloc(rxq->mp);
523
524                         if (unlikely(seg == NULL))
525                                 return MLX5_RXQ_CODE_NOMBUF;
526                         SET_DATA_OFF(seg, 0);
527                         rte_memcpy(rte_pktmbuf_mtod(seg, void *),
528                                 RTE_PTR_ADD(addr, len - hdrm_overlap),
529                                 hdrm_overlap);
530                         DATA_LEN(seg) = hdrm_overlap;
531                         DATA_LEN(pkt) = len - hdrm_overlap;
532                         NEXT(pkt) = seg;
533                         NB_SEGS(pkt) = 2;
534                 }
535         }
536         return MLX5_RXQ_CODE_EXIT;
537 }
538
539 /**
540  * Check whether Multi-Packet RQ can be enabled for the device.
541  *
542  * @param dev
543  *   Pointer to Ethernet device.
544  *
545  * @return
546  *   1 if supported, negative errno value if not.
547  */
548 static __rte_always_inline int
549 mlx5_check_mprq_support(struct rte_eth_dev *dev)
550 {
551         struct mlx5_priv *priv = dev->data->dev_private;
552
553         if (priv->config.mprq.enabled &&
554             priv->rxqs_n >= priv->config.mprq.min_rxqs_num)
555                 return 1;
556         return -ENOTSUP;
557 }
558
559 /**
560  * Check whether Multi-Packet RQ is enabled for the Rx queue.
561  *
562  *  @param rxq
563  *     Pointer to receive queue structure.
564  *
565  * @return
566  *   0 if disabled, otherwise enabled.
567  */
568 static __rte_always_inline int
569 mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq)
570 {
571         return rxq->strd_num_n > 0;
572 }
573
574 /**
575  * Check whether Multi-Packet RQ is enabled for the device.
576  *
577  * @param dev
578  *   Pointer to Ethernet device.
579  *
580  * @return
581  *   0 if disabled, otherwise enabled.
582  */
583 static __rte_always_inline int
584 mlx5_mprq_enabled(struct rte_eth_dev *dev)
585 {
586         struct mlx5_priv *priv = dev->data->dev_private;
587         uint32_t i;
588         uint16_t n = 0;
589         uint16_t n_ibv = 0;
590
591         if (mlx5_check_mprq_support(dev) < 0)
592                 return 0;
593         /* All the configured queues should be enabled. */
594         for (i = 0; i < priv->rxqs_n; ++i) {
595                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
596                 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
597                         (rxq, struct mlx5_rxq_ctrl, rxq);
598
599                 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
600                         continue;
601                 n_ibv++;
602                 if (mlx5_rxq_mprq_enabled(rxq))
603                         ++n;
604         }
605         /* Multi-Packet RQ can't be partially configured. */
606         MLX5_ASSERT(n == 0 || n == n_ibv);
607         return n == n_ibv;
608 }
609
610 #endif /* RTE_PMD_MLX5_RX_H_ */