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