New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / mlx4 / mlx4_rxtx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #ifndef MLX4_RXTX_H_
7 #define MLX4_RXTX_H_
8
9 #include <stdint.h>
10 #include <sys/queue.h>
11
12 /* Verbs headers do not support -pedantic. */
13 #ifdef PEDANTIC
14 #pragma GCC diagnostic ignored "-Wpedantic"
15 #endif
16 #include <infiniband/mlx4dv.h>
17 #include <infiniband/verbs.h>
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic error "-Wpedantic"
20 #endif
21
22 #include <rte_ethdev_driver.h>
23 #include <rte_mbuf.h>
24 #include <rte_mempool.h>
25
26 #include "mlx4.h"
27 #include "mlx4_prm.h"
28 #include "mlx4_mr.h"
29
30 /** Rx queue counters. */
31 struct mlx4_rxq_stats {
32         unsigned int idx; /**< Mapping index. */
33         uint64_t ipackets; /**< Total of successfully received packets. */
34         uint64_t ibytes; /**< Total of successfully received bytes. */
35         uint64_t idropped; /**< Total of packets dropped when Rx ring full. */
36         uint64_t rx_nombuf; /**< Total of Rx mbuf allocation failures. */
37 };
38
39 /** Rx queue descriptor. */
40 struct rxq {
41         struct priv *priv; /**< Back pointer to private data. */
42         struct rte_mempool *mp; /**< Memory pool for allocations. */
43         struct ibv_cq *cq; /**< Completion queue. */
44         struct ibv_wq *wq; /**< Work queue. */
45         struct ibv_comp_channel *channel; /**< Rx completion channel. */
46         uint16_t rq_ci; /**< Saved RQ consumer index. */
47         uint16_t port_id; /**< Port ID for incoming packets. */
48         uint16_t sges_n; /**< Number of segments per packet (log2 value). */
49         uint16_t elts_n; /**< Mbuf queue size (log2 value). */
50         struct mlx4_mr_ctrl mr_ctrl; /* MR control descriptor. */
51         struct rte_mbuf *(*elts)[]; /**< Rx elements. */
52         volatile struct mlx4_wqe_data_seg (*wqes)[]; /**< HW queue entries. */
53         volatile uint32_t *rq_db; /**< RQ doorbell record. */
54         uint32_t csum:1; /**< Enable checksum offloading. */
55         uint32_t csum_l2tun:1; /**< Same for L2 tunnels. */
56         uint32_t crc_present:1; /**< CRC must be subtracted. */
57         uint32_t l2tun_offload:1; /**< L2 tunnel offload is enabled. */
58         struct mlx4_cq mcq;  /**< Info for directly manipulating the CQ. */
59         struct mlx4_rxq_stats stats; /**< Rx queue counters. */
60         unsigned int socket; /**< CPU socket ID for allocations. */
61         uint32_t usecnt; /**< Number of users relying on queue resources. */
62         uint8_t data[]; /**< Remaining queue resources. */
63 };
64
65 /** Shared flow target for Rx queues. */
66 struct mlx4_rss {
67         LIST_ENTRY(mlx4_rss) next; /**< Next entry in list. */
68         struct priv *priv; /**< Back pointer to private data. */
69         uint32_t refcnt; /**< Reference count for this object. */
70         uint32_t usecnt; /**< Number of users relying on @p qp and @p ind. */
71         struct ibv_qp *qp; /**< Queue pair. */
72         struct ibv_rwq_ind_table *ind; /**< Indirection table. */
73         uint64_t fields; /**< Fields for RSS processing (Verbs format). */
74         uint8_t key[MLX4_RSS_HASH_KEY_SIZE]; /**< Hash key to use. */
75         uint16_t queues; /**< Number of target queues. */
76         uint16_t queue_id[]; /**< Target queues. */
77 };
78
79 /** Tx element. */
80 struct txq_elt {
81         struct rte_mbuf *buf; /**< Buffer. */
82         union {
83                 volatile struct mlx4_wqe_ctrl_seg *wqe; /**< SQ WQE. */
84                 volatile uint32_t *eocb; /**< End of completion burst. */
85         };
86 };
87
88 /** Tx queue counters. */
89 struct mlx4_txq_stats {
90         unsigned int idx; /**< Mapping index. */
91         uint64_t opackets; /**< Total of successfully sent packets. */
92         uint64_t obytes; /**< Total of successfully sent bytes. */
93         uint64_t odropped; /**< Total number of packets failed to transmit. */
94 };
95
96 /** Tx queue descriptor. */
97 struct txq {
98         struct mlx4_sq msq; /**< Info for directly manipulating the SQ. */
99         struct mlx4_cq mcq; /**< Info for directly manipulating the CQ. */
100         unsigned int elts_head; /**< Current index in (*elts)[]. */
101         unsigned int elts_tail; /**< First element awaiting completion. */
102         int elts_comp_cd; /**< Countdown for next completion. */
103         unsigned int elts_comp_cd_init; /**< Initial value for countdown. */
104         unsigned int elts_n; /**< (*elts)[] length. */
105         struct mlx4_mr_ctrl mr_ctrl; /* MR control descriptor. */
106         struct txq_elt (*elts)[]; /**< Tx elements. */
107         struct mlx4_txq_stats stats; /**< Tx queue counters. */
108         uint32_t max_inline; /**< Max inline send size. */
109         uint32_t csum:1; /**< Enable checksum offloading. */
110         uint32_t csum_l2tun:1; /**< Same for L2 tunnels. */
111         uint32_t lb:1; /**< Whether packets should be looped back by eSwitch. */
112         uint8_t *bounce_buf;
113         /**< Memory used for storing the first DWORD of data TXBBs. */
114         struct priv *priv; /**< Back pointer to private data. */
115         unsigned int socket; /**< CPU socket ID for allocations. */
116         struct ibv_cq *cq; /**< Completion queue. */
117         struct ibv_qp *qp; /**< Queue pair. */
118         uint8_t data[]; /**< Remaining queue resources. */
119 };
120
121 /* mlx4_rxq.c */
122
123 uint8_t mlx4_rss_hash_key_default[MLX4_RSS_HASH_KEY_SIZE];
124 int mlx4_rss_init(struct priv *priv);
125 void mlx4_rss_deinit(struct priv *priv);
126 struct mlx4_rss *mlx4_rss_get(struct priv *priv, uint64_t fields,
127                               const uint8_t key[MLX4_RSS_HASH_KEY_SIZE],
128                               uint16_t queues, const uint16_t queue_id[]);
129 void mlx4_rss_put(struct mlx4_rss *rss);
130 int mlx4_rss_attach(struct mlx4_rss *rss);
131 void mlx4_rss_detach(struct mlx4_rss *rss);
132 int mlx4_rxq_attach(struct rxq *rxq);
133 void mlx4_rxq_detach(struct rxq *rxq);
134 uint64_t mlx4_get_rx_port_offloads(struct priv *priv);
135 uint64_t mlx4_get_rx_queue_offloads(struct priv *priv);
136 int mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
137                         uint16_t desc, unsigned int socket,
138                         const struct rte_eth_rxconf *conf,
139                         struct rte_mempool *mp);
140 void mlx4_rx_queue_release(void *dpdk_rxq);
141
142 /* mlx4_rxtx.c */
143
144 uint16_t mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
145                        uint16_t pkts_n);
146 uint16_t mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts,
147                        uint16_t pkts_n);
148 uint16_t mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts,
149                                uint16_t pkts_n);
150 uint16_t mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts,
151                                uint16_t pkts_n);
152
153 /* mlx4_txq.c */
154
155 uint64_t mlx4_get_tx_port_offloads(struct priv *priv);
156 int mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
157                         uint16_t desc, unsigned int socket,
158                         const struct rte_eth_txconf *conf);
159 void mlx4_tx_queue_release(void *dpdk_txq);
160
161 /* mlx4_mr.c */
162
163 void mlx4_mr_flush_local_cache(struct mlx4_mr_ctrl *mr_ctrl);
164 uint32_t mlx4_rx_addr2mr_bh(struct rxq *rxq, uintptr_t addr);
165 uint32_t mlx4_tx_addr2mr_bh(struct txq *txq, uintptr_t addr);
166 uint32_t mlx4_tx_update_ext_mp(struct txq *txq, uintptr_t addr,
167                                struct rte_mempool *mp);
168
169 /**
170  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which the
171  * cloned mbuf is allocated is returned instead.
172  *
173  * @param buf
174  *   Pointer to mbuf.
175  *
176  * @return
177  *   Memory pool where data is located for given mbuf.
178  */
179 static struct rte_mempool *
180 mlx4_mb2mp(struct rte_mbuf *buf)
181 {
182         if (unlikely(RTE_MBUF_INDIRECT(buf)))
183                 return rte_mbuf_from_indirect(buf)->pool;
184         return buf->pool;
185 }
186
187 /**
188  * Query LKey from a packet buffer for Rx. No need to flush local caches for Rx
189  * as mempool is pre-configured and static.
190  *
191  * @param rxq
192  *   Pointer to Rx queue structure.
193  * @param addr
194  *   Address to search.
195  *
196  * @return
197  *   Searched LKey on success, UINT32_MAX on no match.
198  */
199 static __rte_always_inline uint32_t
200 mlx4_rx_addr2mr(struct rxq *rxq, uintptr_t addr)
201 {
202         struct mlx4_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
203         uint32_t lkey;
204
205         /* Linear search on MR cache array. */
206         lkey = mlx4_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
207                                     MLX4_MR_CACHE_N, addr);
208         if (likely(lkey != UINT32_MAX))
209                 return lkey;
210         /* Take slower bottom-half (Binary Search) on miss. */
211         return mlx4_rx_addr2mr_bh(rxq, addr);
212 }
213
214 #define mlx4_rx_mb2mr(rxq, mb) mlx4_rx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
215
216 /**
217  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
218  *
219  * @param txq
220  *   Pointer to Tx queue structure.
221  * @param addr
222  *   Address to search.
223  *
224  * @return
225  *   Searched LKey on success, UINT32_MAX on no match.
226  */
227 static __rte_always_inline uint32_t
228 mlx4_tx_addr2mr(struct txq *txq, uintptr_t addr)
229 {
230         struct mlx4_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
231         uint32_t lkey;
232
233         /* Check generation bit to see if there's any change on existing MRs. */
234         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
235                 mlx4_mr_flush_local_cache(mr_ctrl);
236         /* Linear search on MR cache array. */
237         lkey = mlx4_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
238                                     MLX4_MR_CACHE_N, addr);
239         if (likely(lkey != UINT32_MAX))
240                 return lkey;
241         /* Take slower bottom-half (binary search) on miss. */
242         return mlx4_tx_addr2mr_bh(txq, addr);
243 }
244
245 static __rte_always_inline uint32_t
246 mlx4_tx_mb2mr(struct txq *txq, struct rte_mbuf *mb)
247 {
248         uintptr_t addr = (uintptr_t)mb->buf_addr;
249         uint32_t lkey = mlx4_tx_addr2mr(txq, addr);
250
251         if (likely(lkey != UINT32_MAX))
252                 return lkey;
253         if (rte_errno == ENXIO) {
254                 /* Mempool may have externally allocated memory. */
255                 lkey = mlx4_tx_update_ext_mp(txq, addr, mlx4_mb2mp(mb));
256         }
257         return lkey;
258 }
259
260 #endif /* MLX4_RXTX_H_ */