net/mlx5: support flag flow action
[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/mlx5_hw.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 /* Compute the number of DS. */
77 #define MLX5_WQE_DS(n) \
78         (((n) + MLX5_WQE_DWORD_SIZE - 1) / MLX5_WQE_DWORD_SIZE)
79
80 /* Room for inline data in multi-packet WQE. */
81 #define MLX5_MWQE64_INL_DATA 28
82
83 #ifndef HAVE_VERBS_MLX5_OPCODE_TSO
84 #define MLX5_OPCODE_TSO MLX5_OPCODE_LSO_MPW /* Compat with OFED 3.3. */
85 #endif
86
87 /* CQE value to inform that VLAN is stripped. */
88 #define MLX5_CQE_VLAN_STRIPPED (1u << 0)
89
90 /* IPv4 options. */
91 #define MLX5_CQE_RX_IP_EXT_OPTS_PACKET (1u << 1)
92
93 /* IPv6 packet. */
94 #define MLX5_CQE_RX_IPV6_PACKET (1u << 2)
95
96 /* IPv4 packet. */
97 #define MLX5_CQE_RX_IPV4_PACKET (1u << 3)
98
99 /* TCP packet. */
100 #define MLX5_CQE_RX_TCP_PACKET (1u << 4)
101
102 /* UDP packet. */
103 #define MLX5_CQE_RX_UDP_PACKET (1u << 5)
104
105 /* IP is fragmented. */
106 #define MLX5_CQE_RX_IP_FRAG_PACKET (1u << 7)
107
108 /* L2 header is valid. */
109 #define MLX5_CQE_RX_L2_HDR_VALID (1u << 8)
110
111 /* L3 header is valid. */
112 #define MLX5_CQE_RX_L3_HDR_VALID (1u << 9)
113
114 /* L4 header is valid. */
115 #define MLX5_CQE_RX_L4_HDR_VALID (1u << 10)
116
117 /* Outer packet, 0 IPv4, 1 IPv6. */
118 #define MLX5_CQE_RX_OUTER_PACKET (1u << 1)
119
120 /* Tunnel packet bit in the CQE. */
121 #define MLX5_CQE_RX_TUNNEL_PACKET (1u << 0)
122
123 /* INVALID is used by packets matching no flow rules. */
124 #define MLX5_FLOW_MARK_INVALID 0
125
126 /* Maximum allowed value to mark a packet. */
127 #define MLX5_FLOW_MARK_MAX 0xfffff0
128
129 /* Default mark value used when none is provided. */
130 #define MLX5_FLOW_MARK_DEFAULT 0xffffff
131
132 /* Subset of struct mlx5_wqe_eth_seg. */
133 struct mlx5_wqe_eth_seg_small {
134         uint32_t rsvd0;
135         uint8_t cs_flags;
136         uint8_t rsvd1;
137         uint16_t mss;
138         uint32_t rsvd2;
139         uint16_t inline_hdr_sz;
140         uint8_t inline_hdr[2];
141 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
142
143 struct mlx5_wqe_inl_small {
144         uint32_t byte_cnt;
145         uint8_t raw;
146 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
147
148 struct mlx5_wqe_ctrl {
149         uint32_t ctrl0;
150         uint32_t ctrl1;
151         uint32_t ctrl2;
152         uint32_t ctrl3;
153 } __rte_aligned(MLX5_WQE_DWORD_SIZE);
154
155 /* Small common part of the WQE. */
156 struct mlx5_wqe {
157         uint32_t ctrl[4];
158         struct mlx5_wqe_eth_seg_small eseg;
159 };
160
161 /* Vectorize WQE header. */
162 struct mlx5_wqe_v {
163         rte_v128u32_t ctrl;
164         rte_v128u32_t eseg;
165 };
166
167 /* WQE. */
168 struct mlx5_wqe64 {
169         struct mlx5_wqe hdr;
170         uint8_t raw[32];
171 } __rte_aligned(MLX5_WQE_SIZE);
172
173 /* MPW session status. */
174 enum mlx5_mpw_state {
175         MLX5_MPW_STATE_OPENED,
176         MLX5_MPW_INL_STATE_OPENED,
177         MLX5_MPW_STATE_CLOSED,
178 };
179
180 /* MPW session descriptor. */
181 struct mlx5_mpw {
182         enum mlx5_mpw_state state;
183         unsigned int pkts_n;
184         unsigned int len;
185         unsigned int total_len;
186         volatile struct mlx5_wqe *wqe;
187         union {
188                 volatile struct mlx5_wqe_data_seg *dseg[MLX5_MPW_DSEG_MAX];
189                 volatile uint8_t *raw;
190         } data;
191 };
192
193 /* CQ element structure - should be equal to the cache line size */
194 struct mlx5_cqe {
195 #if (RTE_CACHE_LINE_SIZE == 128)
196         uint8_t padding[64];
197 #endif
198         uint8_t pkt_info;
199         uint8_t rsvd0[11];
200         uint32_t rx_hash_res;
201         uint8_t rx_hash_type;
202         uint8_t rsvd1[11];
203         uint16_t hdr_type_etc;
204         uint16_t vlan_info;
205         uint8_t rsvd2[12];
206         uint32_t byte_cnt;
207         uint64_t timestamp;
208         uint32_t sop_drop_qpn;
209         uint16_t wqe_counter;
210         uint8_t rsvd4;
211         uint8_t op_own;
212 };
213
214 /**
215  * Convert a user mark to flow mark.
216  *
217  * @param val
218  *   Mark value to convert.
219  *
220  * @return
221  *   Converted mark value.
222  */
223 static inline uint32_t
224 mlx5_flow_mark_set(uint32_t val)
225 {
226         uint32_t ret;
227
228         /*
229          * Add one to the user value to differentiate un-marked flows from
230          * marked flows, if the ID is equal to MLX5_FLOW_MARK_DEFAULT it
231          * remains untouched.
232          */
233         if (val != MLX5_FLOW_MARK_DEFAULT)
234                 ++val;
235 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
236         /*
237          * Mark is 24 bits (minus reserved values) but is stored on a 32 bit
238          * word, byte-swapped by the kernel on little-endian systems. In this
239          * case, left-shifting the resulting big-endian value ensures the
240          * least significant 24 bits are retained when converting it back.
241          */
242         ret = rte_cpu_to_be_32(val) >> 8;
243 #else
244         ret = val;
245 #endif
246         return ret;
247 }
248
249 /**
250  * Convert a mark to user mark.
251  *
252  * @param val
253  *   Mark value to convert.
254  *
255  * @return
256  *   Converted mark value.
257  */
258 static inline uint32_t
259 mlx5_flow_mark_get(uint32_t val)
260 {
261         /*
262          * Subtract one from the retrieved value. It was added by
263          * mlx5_flow_mark_set() to distinguish unmarked flows.
264          */
265 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
266         return (val >> 8) - 1;
267 #else
268         return val - 1;
269 #endif
270 }
271
272 #endif /* RTE_PMD_MLX5_PRM_H_ */