aa487757386f90d915a165c82d7357ae4d8bf740
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx_vec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include <rte_mbuf.h>
11 #include <rte_mempool.h>
12 #include <rte_prefetch.h>
13 #include <rte_vect.h>
14
15 #include <mlx5_glue.h>
16 #include <mlx5_prm.h>
17
18 #include "mlx5_defs.h"
19 #include "mlx5.h"
20 #include "mlx5_utils.h"
21 #include "mlx5_rxtx.h"
22 #include "mlx5_rxtx_vec.h"
23 #include "mlx5_autoconf.h"
24
25 #if defined RTE_ARCH_X86_64
26 #include "mlx5_rxtx_vec_sse.h"
27 #elif defined RTE_ARCH_ARM64
28 #include "mlx5_rxtx_vec_neon.h"
29 #elif defined RTE_ARCH_PPC_64
30 #include "mlx5_rxtx_vec_altivec.h"
31 #else
32 #error "This should not be compiled if SIMD instructions are not supported."
33 #endif
34
35 /**
36  * Skip error packets.
37  *
38  * @param rxq
39  *   Pointer to RX queue structure.
40  * @param[out] pkts
41  *   Array to store received packets.
42  * @param pkts_n
43  *   Maximum number of packets in array.
44  *
45  * @return
46  *   Number of packets successfully received (<= pkts_n).
47  */
48 static uint16_t
49 rxq_handle_pending_error(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
50                          uint16_t pkts_n)
51 {
52         uint16_t n = 0;
53         unsigned int i;
54 #ifdef MLX5_PMD_SOFT_COUNTERS
55         uint32_t err_bytes = 0;
56 #endif
57
58         for (i = 0; i < pkts_n; ++i) {
59                 struct rte_mbuf *pkt = pkts[i];
60
61                 if (pkt->packet_type == RTE_PTYPE_ALL_MASK || rxq->err_state) {
62 #ifdef MLX5_PMD_SOFT_COUNTERS
63                         err_bytes += PKT_LEN(pkt);
64 #endif
65                         rte_pktmbuf_free_seg(pkt);
66                 } else {
67                         pkts[n++] = pkt;
68                 }
69         }
70         rxq->stats.idropped += (pkts_n - n);
71 #ifdef MLX5_PMD_SOFT_COUNTERS
72         /* Correct counters of errored completions. */
73         rxq->stats.ipackets -= (pkts_n - n);
74         rxq->stats.ibytes -= err_bytes;
75 #endif
76         mlx5_rx_err_handle(rxq, 1);
77         return n;
78 }
79
80 /**
81  * Receive burst of packets. An errored completion also consumes a mbuf, but the
82  * packet_type is set to be RTE_PTYPE_ALL_MASK. Marked mbufs should be freed
83  * before returning to application.
84  *
85  * @param rxq
86  *   Pointer to RX queue structure.
87  * @param[out] pkts
88  *   Array to store received packets.
89  * @param pkts_n
90  *   Maximum number of packets in array.
91  * @param[out] err
92  *   Pointer to a flag. Set non-zero value if pkts array has at least one error
93  *   packet to handle.
94  * @param[out] no_cq
95  *   Pointer to a boolean. Set true if no new CQE seen.
96  *
97  * @return
98  *   Number of packets received including errors (<= pkts_n).
99  */
100 static inline uint16_t
101 rxq_burst_v(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
102             uint16_t pkts_n, uint64_t *err, bool *no_cq)
103 {
104         const uint16_t q_n = 1 << rxq->cqe_n;
105         const uint16_t q_mask = q_n - 1;
106         const uint16_t e_n = 1 << rxq->elts_n;
107         const uint16_t e_mask = e_n - 1;
108         volatile struct mlx5_cqe *cq;
109         struct rte_mbuf **elts;
110         uint64_t comp_idx = MLX5_VPMD_DESCS_PER_LOOP;
111         uint16_t nocmp_n = 0;
112         uint16_t rcvd_pkt = 0;
113         unsigned int cq_idx = rxq->cq_ci & q_mask;
114         unsigned int elts_idx;
115
116         MLX5_ASSERT(rxq->sges_n == 0);
117         MLX5_ASSERT(rxq->cqe_n == rxq->elts_n);
118         cq = &(*rxq->cqes)[cq_idx];
119         rte_prefetch0(cq);
120         rte_prefetch0(cq + 1);
121         rte_prefetch0(cq + 2);
122         rte_prefetch0(cq + 3);
123         pkts_n = RTE_MIN(pkts_n, MLX5_VPMD_RX_MAX_BURST);
124         mlx5_rx_replenish_bulk_mbuf(rxq);
125         /* See if there're unreturned mbufs from compressed CQE. */
126         rcvd_pkt = rxq->decompressed;
127         if (rcvd_pkt > 0) {
128                 rcvd_pkt = RTE_MIN(rcvd_pkt, pkts_n);
129                 rxq_copy_mbuf_v(&(*rxq->elts)[rxq->rq_pi & e_mask],
130                                 pkts, rcvd_pkt);
131                 rxq->rq_pi += rcvd_pkt;
132                 rxq->decompressed -= rcvd_pkt;
133                 pkts += rcvd_pkt;
134         }
135         elts_idx = rxq->rq_pi & e_mask;
136         elts = &(*rxq->elts)[elts_idx];
137         /* Not to overflow pkts array. */
138         pkts_n = RTE_ALIGN_FLOOR(pkts_n - rcvd_pkt, MLX5_VPMD_DESCS_PER_LOOP);
139         /* Not to cross queue end. */
140         pkts_n = RTE_MIN(pkts_n, q_n - elts_idx);
141         pkts_n = RTE_MIN(pkts_n, q_n - cq_idx);
142         if (!pkts_n) {
143                 *no_cq = !rcvd_pkt;
144                 return rcvd_pkt;
145         }
146         /* At this point, there shouldn't be any remaining packets. */
147         MLX5_ASSERT(rxq->decompressed == 0);
148         /* Process all the CQEs */
149         nocmp_n = rxq_cq_process_v(rxq, cq, elts, pkts, pkts_n, err, &comp_idx);
150         /* If no new CQE seen, return without updating cq_db. */
151         if (unlikely(!nocmp_n && comp_idx == MLX5_VPMD_DESCS_PER_LOOP)) {
152                 *no_cq = true;
153                 return rcvd_pkt;
154         }
155         /* Update the consumer indexes for non-compressed CQEs. */
156         MLX5_ASSERT(nocmp_n <= pkts_n);
157         rxq->cq_ci += nocmp_n;
158         rxq->rq_pi += nocmp_n;
159         rcvd_pkt += nocmp_n;
160         /* Decompress the last CQE if compressed. */
161         if (comp_idx < MLX5_VPMD_DESCS_PER_LOOP) {
162                 MLX5_ASSERT(comp_idx == (nocmp_n % MLX5_VPMD_DESCS_PER_LOOP));
163                 rxq->decompressed = rxq_cq_decompress_v(rxq, &cq[nocmp_n],
164                                                         &elts[nocmp_n]);
165                 rxq->cq_ci += rxq->decompressed;
166                 /* Return more packets if needed. */
167                 if (nocmp_n < pkts_n) {
168                         uint16_t n = rxq->decompressed;
169
170                         n = RTE_MIN(n, pkts_n - nocmp_n);
171                         rxq_copy_mbuf_v(&(*rxq->elts)[rxq->rq_pi & e_mask],
172                                         &pkts[nocmp_n], n);
173                         rxq->rq_pi += n;
174                         rcvd_pkt += n;
175                         rxq->decompressed -= n;
176                 }
177         }
178         rte_io_wmb();
179         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
180         *no_cq = !rcvd_pkt;
181         return rcvd_pkt;
182 }
183
184 /**
185  * DPDK callback for vectorized RX.
186  *
187  * @param dpdk_rxq
188  *   Generic pointer to RX queue structure.
189  * @param[out] pkts
190  *   Array to store received packets.
191  * @param pkts_n
192  *   Maximum number of packets in array.
193  *
194  * @return
195  *   Number of packets successfully received (<= pkts_n).
196  */
197 uint16_t
198 mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
199 {
200         struct mlx5_rxq_data *rxq = dpdk_rxq;
201         uint16_t nb_rx = 0;
202         uint16_t tn = 0;
203         uint64_t err = 0;
204         bool no_cq = false;
205
206         do {
207                 nb_rx = rxq_burst_v(rxq, pkts + tn, pkts_n - tn, &err, &no_cq);
208                 if (unlikely(err | rxq->err_state))
209                         nb_rx = rxq_handle_pending_error(rxq, pkts + tn, nb_rx);
210                 tn += nb_rx;
211                 if (unlikely(no_cq))
212                         break;
213         } while (tn != pkts_n);
214         return tn;
215 }
216
217 /**
218  * Check a RX queue can support vectorized RX.
219  *
220  * @param rxq
221  *   Pointer to RX queue.
222  *
223  * @return
224  *   1 if supported, negative errno value if not.
225  */
226 int __rte_cold
227 mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq)
228 {
229         struct mlx5_rxq_ctrl *ctrl =
230                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
231
232         if (mlx5_mprq_enabled(ETH_DEV(ctrl->priv)))
233                 return -ENOTSUP;
234         if (!ctrl->priv->config.rx_vec_en || rxq->sges_n != 0)
235                 return -ENOTSUP;
236         if (rxq->lro)
237                 return -ENOTSUP;
238         return 1;
239 }
240
241 /**
242  * Check a device can support vectorized RX.
243  *
244  * @param dev
245  *   Pointer to Ethernet device.
246  *
247  * @return
248  *   1 if supported, negative errno value if not.
249  */
250 int __rte_cold
251 mlx5_check_vec_rx_support(struct rte_eth_dev *dev)
252 {
253         struct mlx5_priv *priv = dev->data->dev_private;
254         uint32_t i;
255
256         if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)
257                 return -ENOTSUP;
258         if (!priv->config.rx_vec_en)
259                 return -ENOTSUP;
260         if (mlx5_mprq_enabled(dev))
261                 return -ENOTSUP;
262         /* All the configured queues should support. */
263         for (i = 0; i < priv->rxqs_n; ++i) {
264                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
265
266                 if (!rxq)
267                         continue;
268                 if (mlx5_rxq_check_vec_support(rxq) < 0)
269                         break;
270         }
271         if (i != priv->rxqs_n)
272                 return -ENOTSUP;
273         return 1;
274 }