2de310bcb99e419dfd8ea26e30f043406230b3c3
[dpdk.git] / drivers / net / mlx5 / mlx5_prm.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 6WIND S.A.
5  *   Copyright 2016 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 #ifndef RTE_PMD_MLX5_PRM_H_
35 #define RTE_PMD_MLX5_PRM_H_
36
37 #include <assert.h>
38
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-Wpedantic"
43 #endif
44 #include <infiniband/mlx5dv.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-Wpedantic"
47 #endif
48
49 #include <rte_vect.h>
50 #include "mlx5_autoconf.h"
51
52 /* Get CQE owner bit. */
53 #define MLX5_CQE_OWNER(op_own) ((op_own) & MLX5_CQE_OWNER_MASK)
54
55 /* Get CQE format. */
56 #define MLX5_CQE_FORMAT(op_own) (((op_own) & MLX5E_CQE_FORMAT_MASK) >> 2)
57
58 /* Get CQE opcode. */
59 #define MLX5_CQE_OPCODE(op_own) (((op_own) & 0xf0) >> 4)
60
61 /* Get CQE solicited event. */
62 #define MLX5_CQE_SE(op_own) (((op_own) >> 1) & 1)
63
64 /* Invalidate a CQE. */
65 #define MLX5_CQE_INVALIDATE (MLX5_CQE_INVALID << 4)
66
67 /* Maximum number of packets a multi-packet WQE can handle. */
68 #define MLX5_MPW_DSEG_MAX 5
69
70 /* WQE DWORD size */
71 #define MLX5_WQE_DWORD_SIZE 16
72
73 /* WQE size */
74 #define MLX5_WQE_SIZE (4 * MLX5_WQE_DWORD_SIZE)
75
76 /* Max size of a WQE session. */
77 #define MLX5_WQE_SIZE_MAX 960U
78
79 /* Compute the number of DS. */
80 #define MLX5_WQE_DS(n) \
81         (((n) + MLX5_WQE_DWORD_SIZE - 1) / MLX5_WQE_DWORD_SIZE)
82
83 /* Room for inline data in multi-packet WQE. */
84 #define MLX5_MWQE64_INL_DATA 28
85
86 /* Default minimum number of Tx queues for inlining packets. */
87 #define MLX5_EMPW_MIN_TXQS 8
88
89 /* Default max packet length to be inlined. */
90 #define MLX5_EMPW_MAX_INLINE_LEN (4U * MLX5_WQE_SIZE)
91
92
93 #define MLX5_OPC_MOD_ENHANCED_MPSW 0
94 #define MLX5_OPCODE_ENHANCED_MPSW 0x29
95
96 /* CQE value to inform that VLAN is stripped. */
97 #define MLX5_CQE_VLAN_STRIPPED (1u << 0)
98
99 /* IPv4 options. */
100 #define MLX5_CQE_RX_IP_EXT_OPTS_PACKET (1u << 1)
101
102 /* IPv6 packet. */
103 #define MLX5_CQE_RX_IPV6_PACKET (1u << 2)
104
105 /* IPv4 packet. */
106 #define MLX5_CQE_RX_IPV4_PACKET (1u << 3)
107
108 /* TCP packet. */
109 #define MLX5_CQE_RX_TCP_PACKET (1u << 4)
110
111 /* UDP packet. */
112 #define MLX5_CQE_RX_UDP_PACKET (1u << 5)
113
114 /* IP is fragmented. */
115 #define MLX5_CQE_RX_IP_FRAG_PACKET (1u << 7)
116
117 /* L2 header is valid. */
118 #define MLX5_CQE_RX_L2_HDR_VALID (1u << 8)
119
120 /* L3 header is valid. */
121 #define MLX5_CQE_RX_L3_HDR_VALID (1u << 9)
122
123 /* L4 header is valid. */
124 #define MLX5_CQE_RX_L4_HDR_VALID (1u << 10)
125
126 /* Outer packet, 0 IPv4, 1 IPv6. */
127 #define MLX5_CQE_RX_OUTER_PACKET (1u << 1)
128
129 /* Tunnel packet bit in the CQE. */
130 #define MLX5_CQE_RX_TUNNEL_PACKET (1u << 0)
131
132 /* Inner L3 checksum offload (Tunneled packets only). */
133 #define MLX5_ETH_WQE_L3_INNER_CSUM (1u << 4)
134
135 /* Inner L4 checksum offload (Tunneled packets only). */
136 #define MLX5_ETH_WQE_L4_INNER_CSUM (1u << 5)
137
138 /* Is flow mark valid. */
139 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
140 #define MLX5_FLOW_MARK_IS_VALID(val) ((val) & 0xffffff00)
141 #else
142 #define MLX5_FLOW_MARK_IS_VALID(val) ((val) & 0xffffff)
143 #endif
144
145 /* INVALID is used by packets matching no flow rules. */
146 #define MLX5_FLOW_MARK_INVALID 0
147
148 /* Maximum allowed value to mark a packet. */
149 #define MLX5_FLOW_MARK_MAX 0xfffff0
150
151 /* Default mark value used when none is provided. */
152 #define MLX5_FLOW_MARK_DEFAULT 0xffffff
153
154 /* Maximum number of DS in WQE. */
155 #define MLX5_DSEG_MAX 63
156
157 /* Subset of struct mlx5_wqe_eth_seg. */
158 struct mlx5_wqe_eth_seg_small {
159         uint32_t rsvd0;
160         uint8_t cs_flags;
161         uint8_t rsvd1;
162         uint16_t mss;
163         uint32_t rsvd2;
164         uint16_t inline_hdr_sz;
165         uint8_t inline_hdr[2];
166 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
167
168 struct mlx5_wqe_inl_small {
169         uint32_t byte_cnt;
170         uint8_t raw;
171 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
172
173 struct mlx5_wqe_ctrl {
174         uint32_t ctrl0;
175         uint32_t ctrl1;
176         uint32_t ctrl2;
177         uint32_t ctrl3;
178 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
179
180 /* Small common part of the WQE. */
181 struct mlx5_wqe {
182         uint32_t ctrl[4];
183         struct mlx5_wqe_eth_seg_small eseg;
184 };
185
186 /* Vectorize WQE header. */
187 struct mlx5_wqe_v {
188         rte_v128u32_t ctrl;
189         rte_v128u32_t eseg;
190 };
191
192 /* WQE. */
193 struct mlx5_wqe64 {
194         struct mlx5_wqe hdr;
195         uint8_t raw[32];
196 } __rte_aligned(MLX5_WQE_SIZE);
197
198 /* MPW mode. */
199 enum mlx5_mpw_mode {
200         MLX5_MPW_DISABLED,
201         MLX5_MPW,
202         MLX5_MPW_ENHANCED, /* Enhanced Multi-Packet Send WQE, a.k.a MPWv2. */
203 };
204
205 /* MPW session status. */
206 enum mlx5_mpw_state {
207         MLX5_MPW_STATE_OPENED,
208         MLX5_MPW_INL_STATE_OPENED,
209         MLX5_MPW_ENHANCED_STATE_OPENED,
210         MLX5_MPW_STATE_CLOSED,
211 };
212
213 /* MPW session descriptor. */
214 struct mlx5_mpw {
215         enum mlx5_mpw_state state;
216         unsigned int pkts_n;
217         unsigned int len;
218         unsigned int total_len;
219         volatile struct mlx5_wqe *wqe;
220         union {
221                 volatile struct mlx5_wqe_data_seg *dseg[MLX5_MPW_DSEG_MAX];
222                 volatile uint8_t *raw;
223         } data;
224 };
225
226 /* CQ element structure - should be equal to the cache line size */
227 struct mlx5_cqe {
228 #if (RTE_CACHE_LINE_SIZE == 128)
229         uint8_t padding[64];
230 #endif
231         uint8_t pkt_info;
232         uint8_t rsvd0[11];
233         uint32_t rx_hash_res;
234         uint8_t rx_hash_type;
235         uint8_t rsvd1[11];
236         uint16_t hdr_type_etc;
237         uint16_t vlan_info;
238         uint8_t rsvd2[12];
239         uint32_t byte_cnt;
240         uint64_t timestamp;
241         uint32_t sop_drop_qpn;
242         uint16_t wqe_counter;
243         uint8_t rsvd4;
244         uint8_t op_own;
245 };
246
247 /* Adding direct verbs to data-path. */
248
249 /* CQ sequence number mask. */
250 #define MLX5_CQ_SQN_MASK 0x3
251
252 /* CQ sequence number index. */
253 #define MLX5_CQ_SQN_OFFSET 28
254
255 /* CQ doorbell index mask. */
256 #define MLX5_CI_MASK 0xffffff
257
258 /* CQ doorbell offset. */
259 #define MLX5_CQ_ARM_DB 1
260
261 /* CQ doorbell offset*/
262 #define MLX5_CQ_DOORBELL 0x20
263
264 /* CQE format value. */
265 #define MLX5_COMPRESSED 0x3
266
267 /* CQE format mask. */
268 #define MLX5E_CQE_FORMAT_MASK 0xc
269
270 /* MPW opcode. */
271 #define MLX5_OPC_MOD_MPW 0x01
272
273 /* Compressed Rx CQE structure. */
274 struct mlx5_mini_cqe8 {
275         union {
276                 uint32_t rx_hash_result;
277                 uint32_t checksum;
278                 struct {
279                         uint16_t wqe_counter;
280                         uint8_t  s_wqe_opcode;
281                         uint8_t  reserved;
282                 } s_wqe_info;
283         };
284         uint32_t byte_cnt;
285 };
286
287 /**
288  * Convert a user mark to flow mark.
289  *
290  * @param val
291  *   Mark value to convert.
292  *
293  * @return
294  *   Converted mark value.
295  */
296 static inline uint32_t
297 mlx5_flow_mark_set(uint32_t val)
298 {
299         uint32_t ret;
300
301         /*
302          * Add one to the user value to differentiate un-marked flows from
303          * marked flows, if the ID is equal to MLX5_FLOW_MARK_DEFAULT it
304          * remains untouched.
305          */
306         if (val != MLX5_FLOW_MARK_DEFAULT)
307                 ++val;
308 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
309         /*
310          * Mark is 24 bits (minus reserved values) but is stored on a 32 bit
311          * word, byte-swapped by the kernel on little-endian systems. In this
312          * case, left-shifting the resulting big-endian value ensures the
313          * least significant 24 bits are retained when converting it back.
314          */
315         ret = rte_cpu_to_be_32(val) >> 8;
316 #else
317         ret = val;
318 #endif
319         return ret;
320 }
321
322 /**
323  * Convert a mark to user mark.
324  *
325  * @param val
326  *   Mark value to convert.
327  *
328  * @return
329  *   Converted mark value.
330  */
331 static inline uint32_t
332 mlx5_flow_mark_get(uint32_t val)
333 {
334         /*
335          * Subtract one from the retrieved value. It was added by
336          * mlx5_flow_mark_set() to distinguish unmarked flows.
337          */
338 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
339         return (val >> 8) - 1;
340 #else
341         return val - 1;
342 #endif
343 }
344
345 #endif /* RTE_PMD_MLX5_PRM_H_ */