xref: /linux/drivers/net/ethernet/intel/ice/ice_flow.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include "ice_common.h"
5 #include "ice_flow.h"
6 #include <net/gre.h>
7 
8 /* Size of known protocol header fields */
9 #define ICE_FLOW_FLD_SZ_ETH_TYPE	2
10 #define ICE_FLOW_FLD_SZ_VLAN		2
11 #define ICE_FLOW_FLD_SZ_IPV4_ADDR	4
12 #define ICE_FLOW_FLD_SZ_IPV6_ADDR	16
13 #define ICE_FLOW_FLD_SZ_IPV6_PRE32_ADDR	4
14 #define ICE_FLOW_FLD_SZ_IPV6_PRE48_ADDR	6
15 #define ICE_FLOW_FLD_SZ_IPV6_PRE64_ADDR	8
16 #define ICE_FLOW_FLD_SZ_IPV4_ID		2
17 #define ICE_FLOW_FLD_SZ_IPV6_ID		4
18 #define ICE_FLOW_FLD_SZ_IP_CHKSUM	2
19 #define ICE_FLOW_FLD_SZ_TCP_CHKSUM	2
20 #define ICE_FLOW_FLD_SZ_UDP_CHKSUM	2
21 #define ICE_FLOW_FLD_SZ_SCTP_CHKSUM	4
22 #define ICE_FLOW_FLD_SZ_IP_DSCP		1
23 #define ICE_FLOW_FLD_SZ_IP_TTL		1
24 #define ICE_FLOW_FLD_SZ_IP_PROT		1
25 #define ICE_FLOW_FLD_SZ_PORT		2
26 #define ICE_FLOW_FLD_SZ_TCP_FLAGS	1
27 #define ICE_FLOW_FLD_SZ_ICMP_TYPE	1
28 #define ICE_FLOW_FLD_SZ_ICMP_CODE	1
29 #define ICE_FLOW_FLD_SZ_ARP_OPER	2
30 #define ICE_FLOW_FLD_SZ_GRE_KEYID	4
31 #define ICE_FLOW_FLD_SZ_GTP_TEID	4
32 #define ICE_FLOW_FLD_SZ_GTP_QFI		2
33 #define ICE_FLOW_FLD_SZ_PFCP_SEID 8
34 #define ICE_FLOW_FLD_SZ_ESP_SPI	4
35 #define ICE_FLOW_FLD_SZ_AH_SPI	4
36 #define ICE_FLOW_FLD_SZ_NAT_T_ESP_SPI	4
37 #define ICE_FLOW_FLD_SZ_L2TPV2_SESS_ID	2
38 #define ICE_FLOW_FLD_SZ_L2TPV2_LEN_SESS_ID	2
39 
40 /* Describe properties of a protocol header field */
41 struct ice_flow_field_info {
42 	enum ice_flow_seg_hdr hdr;
43 	s16 off;	/* Offset from start of a protocol header, in bits */
44 	u16 size;	/* Size of fields in bits */
45 	u16 mask;	/* 16-bit mask for field */
46 };
47 
48 #define ICE_FLOW_FLD_INFO(_hdr, _offset_bytes, _size_bytes) { \
49 	.hdr = _hdr, \
50 	.off = (_offset_bytes) * BITS_PER_BYTE, \
51 	.size = (_size_bytes) * BITS_PER_BYTE, \
52 	.mask = 0, \
53 }
54 
55 /* QFI: 6-bit field in GTP-U PDU Session Container (3GPP TS 38.415) */
56 #define ICE_FLOW_FLD_INFO_MSK(_hdr, _offset_bytes, _size_bytes, _mask) { \
57 	.hdr = _hdr, \
58 	.off = (_offset_bytes) * BITS_PER_BYTE, \
59 	.size = (_size_bytes) * BITS_PER_BYTE, \
60 	.mask = _mask, \
61 }
62 
63 /* Table containing properties of supported protocol header fields */
64 static const
65 struct ice_flow_field_info ice_flds_info[ICE_FLOW_FIELD_IDX_MAX] = {
66 	/* Ether */
67 	/* ICE_FLOW_FIELD_IDX_ETH_DA */
68 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ETH, 0, ETH_ALEN),
69 	/* ICE_FLOW_FIELD_IDX_ETH_SA */
70 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ETH, ETH_ALEN, ETH_ALEN),
71 	/* ICE_FLOW_FIELD_IDX_S_VLAN */
72 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_VLAN, 12, sizeof(__be16)),
73 	/* ICE_FLOW_FIELD_IDX_C_VLAN */
74 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_VLAN, 14, sizeof(__be16)),
75 	/* ICE_FLOW_FIELD_IDX_ETH_TYPE */
76 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ETH, 0, sizeof(__be16)),
77 	/* IPv4 / IPv6 */
78 	/* ICE_FLOW_FIELD_IDX_IPV4_DSCP */
79 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_IPV4, 0, 1, 0x00fc),
80 	/* ICE_FLOW_FIELD_IDX_IPV6_DSCP */
81 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_IPV6, 0, 1, 0x0ff0),
82 	/* ICE_FLOW_FIELD_IDX_IPV4_TTL */
83 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_NONE, 8, 1, 0xff00),
84 	/* ICE_FLOW_FIELD_IDX_IPV4_PROT */
85 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_NONE, 8, 1, 0x00ff),
86 	/* ICE_FLOW_FIELD_IDX_IPV6_TTL */
87 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_NONE, 6, 1, 0x00ff),
88 	/* ICE_FLOW_FIELD_IDX_IPV6_PROT */
89 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_NONE, 6, 1, 0xff00),
90 	/* ICE_FLOW_FIELD_IDX_IPV4_SA */
91 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV4, 12, sizeof(struct in_addr)),
92 	/* ICE_FLOW_FIELD_IDX_IPV4_DA */
93 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV4, 16, sizeof(struct in_addr)),
94 	/* ICE_FLOW_FIELD_IDX_IPV6_SA */
95 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 8, sizeof(struct in6_addr)),
96 	/* ICE_FLOW_FIELD_IDX_IPV6_DA */
97 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 24, ICE_FLOW_FLD_SZ_IPV6_ADDR),
98 	/* ICE_FLOW_FIELD_IDX_IPV4_CHKSUM */
99 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV4, 10, ICE_FLOW_FLD_SZ_IP_CHKSUM),
100 	/* ICE_FLOW_FIELD_IDX_IPV4_FRAG */
101 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV_FRAG, 4,
102 			  ICE_FLOW_FLD_SZ_IPV4_ID),
103 	/* ICE_FLOW_FIELD_IDX_IPV6_FRAG */
104 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV_FRAG, 4,
105 			  ICE_FLOW_FLD_SZ_IPV6_ID),
106 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE32_SA */
107 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 8,
108 			  ICE_FLOW_FLD_SZ_IPV6_PRE32_ADDR),
109 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE32_DA */
110 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 24,
111 			  ICE_FLOW_FLD_SZ_IPV6_PRE32_ADDR),
112 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE48_SA */
113 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 8,
114 			  ICE_FLOW_FLD_SZ_IPV6_PRE48_ADDR),
115 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE48_DA */
116 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 24,
117 			  ICE_FLOW_FLD_SZ_IPV6_PRE48_ADDR),
118 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE64_SA */
119 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 8,
120 			  ICE_FLOW_FLD_SZ_IPV6_PRE64_ADDR),
121 	/* ICE_FLOW_FIELD_IDX_IPV6_PRE64_DA */
122 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_IPV6, 24,
123 			  ICE_FLOW_FLD_SZ_IPV6_PRE64_ADDR),
124 	/* Transport */
125 	/* ICE_FLOW_FIELD_IDX_TCP_SRC_PORT */
126 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_TCP, 0, sizeof(__be16)),
127 	/* ICE_FLOW_FIELD_IDX_TCP_DST_PORT */
128 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_TCP, 2, sizeof(__be16)),
129 	/* ICE_FLOW_FIELD_IDX_UDP_SRC_PORT */
130 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_UDP, 0, sizeof(__be16)),
131 	/* ICE_FLOW_FIELD_IDX_UDP_DST_PORT */
132 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_UDP, 2, sizeof(__be16)),
133 	/* ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT */
134 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_SCTP, 0, sizeof(__be16)),
135 	/* ICE_FLOW_FIELD_IDX_SCTP_DST_PORT */
136 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_SCTP, 2, sizeof(__be16)),
137 	/* ICE_FLOW_FIELD_IDX_TCP_FLAGS */
138 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_TCP, 13, ICE_FLOW_FLD_SZ_TCP_FLAGS),
139 	/* ICE_FLOW_FIELD_IDX_TCP_CHKSUM */
140 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_TCP, 16, ICE_FLOW_FLD_SZ_TCP_CHKSUM),
141 	/* ICE_FLOW_FIELD_IDX_UDP_CHKSUM */
142 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_UDP, 6, ICE_FLOW_FLD_SZ_UDP_CHKSUM),
143 	/* ICE_FLOW_FIELD_IDX_SCTP_CHKSUM */
144 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_SCTP, 8,
145 			  ICE_FLOW_FLD_SZ_SCTP_CHKSUM),
146 	/* ARP */
147 	/* ICE_FLOW_FIELD_IDX_ARP_SIP */
148 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ARP, 14, sizeof(struct in_addr)),
149 	/* ICE_FLOW_FIELD_IDX_ARP_DIP */
150 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ARP, 24, sizeof(struct in_addr)),
151 	/* ICE_FLOW_FIELD_IDX_ARP_SHA */
152 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ARP, 8, ETH_ALEN),
153 	/* ICE_FLOW_FIELD_IDX_ARP_DHA */
154 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ARP, 18, ETH_ALEN),
155 	/* ICE_FLOW_FIELD_IDX_ARP_OP */
156 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ARP, 6, sizeof(__be16)),
157 	/* ICMP */
158 	/* ICE_FLOW_FIELD_IDX_ICMP_TYPE */
159 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ICMP, 0, 1),
160 	/* ICE_FLOW_FIELD_IDX_ICMP_CODE */
161 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ICMP, 1, 1),
162 	/* GRE */
163 	/* ICE_FLOW_FIELD_IDX_GRE_KEYID */
164 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GRE, 12,
165 			  sizeof_field(struct gre_full_hdr, key)),
166 	/* GTP */
167 	/* ICE_FLOW_FIELD_IDX_GTPC_TEID */
168 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GTPC_TEID, 12, sizeof(__be32)),
169 	/* ICE_FLOW_FIELD_IDX_GTPU_IP_TEID */
170 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GTPU_IP, 12, sizeof(__be32)),
171 	/* ICE_FLOW_FIELD_IDX_GTPU_EH_TEID */
172 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GTPU_EH, 12, sizeof(__be32)),
173 	/* ICE_FLOW_FIELD_IDX_GTPU_EH_QFI */
174 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_GTPU_EH, 22, sizeof(__be16),
175 			      0x3f00),
176 	/* ICE_FLOW_FIELD_IDX_GTPU_UP_TEID */
177 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GTPU_UP, 12,
178 			  ICE_FLOW_FLD_SZ_GTP_TEID),
179 	/* ICE_FLOW_FIELD_IDX_GTPU_UP_QFI */
180 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_GTPU_UP, 22,
181 			      ICE_FLOW_FLD_SZ_GTP_QFI, 0x3f00),
182 	/* ICE_FLOW_FIELD_IDX_GTPU_DWN_TEID */
183 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_GTPU_DWN, 12,
184 			  ICE_FLOW_FLD_SZ_GTP_TEID),
185 	/* ICE_FLOW_FIELD_IDX_GTPU_DWN_QFI */
186 	ICE_FLOW_FLD_INFO_MSK(ICE_FLOW_SEG_HDR_GTPU_DWN, 22,
187 			      ICE_FLOW_FLD_SZ_GTP_QFI, 0x3f00),
188 	/* PPPoE */
189 	/* ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID */
190 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_PPPOE, 2, sizeof(__be16)),
191 	/* PFCP */
192 	/* ICE_FLOW_FIELD_IDX_PFCP_SEID */
193 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_PFCP_SESSION, 12, sizeof(__be64)),
194 	/* L2TPv3 */
195 	/* ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID */
196 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_L2TPV3, 0, sizeof(__be32)),
197 	/* ESP */
198 	/* ICE_FLOW_FIELD_IDX_ESP_SPI */
199 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_ESP, 0, sizeof(__be32)),
200 	/* AH */
201 	/* ICE_FLOW_FIELD_IDX_AH_SPI */
202 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_AH, 4, sizeof(__be32)),
203 	/* NAT_T_ESP */
204 	/* ICE_FLOW_FIELD_IDX_NAT_T_ESP_SPI */
205 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_NAT_T_ESP, 8,
206 			  ICE_FLOW_FLD_SZ_NAT_T_ESP_SPI),
207 	/* L2TPV2 */
208 	/* ICE_FLOW_FIELD_IDX_L2TPV2_SESS_ID */
209 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_L2TPV2, 12,
210 			  ICE_FLOW_FLD_SZ_L2TPV2_SESS_ID),
211 	/* L2TPV2_LEN */
212 	/* ICE_FLOW_FIELD_IDX_L2TPV2_LEN_SESS_ID */
213 	ICE_FLOW_FLD_INFO(ICE_FLOW_SEG_HDR_L2TPV2, 14,
214 			  ICE_FLOW_FLD_SZ_L2TPV2_LEN_SESS_ID),
215 };
216 
217 /* Bitmaps indicating relevant packet types for a particular protocol header
218  *
219  * Packet types for packets with an Outer/First/Single MAC header
220  */
221 static const u32 ice_ptypes_mac_ofos[] = {
222 	0xFDC00846, 0xBFBF7F7E, 0xF70001DF, 0xFEFDFDFB,
223 	0x0000077E, 0x000003FF, 0x00000000, 0x00000000,
224 	0x00400000, 0x03FFF000, 0xFFFFFFE0, 0x00000707,
225 	0xFFFFF000, 0x000003FF, 0x00000000, 0x00000000,
226 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
227 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
228 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
229 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
230 };
231 
232 /* Packet types for packets with an Innermost/Last MAC VLAN header */
233 static const u32 ice_ptypes_macvlan_il[] = {
234 	0x00000000, 0xBC000000, 0x000001DF, 0xF0000000,
235 	0x0000077E, 0x00000000, 0x00000000, 0x00000000,
236 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
237 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
238 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
239 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
240 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
241 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
242 };
243 
244 /* Packet types for packets with an Outer/First/Single IPv4 header, does NOT
245  * include IPv4 other PTYPEs
246  */
247 static const u32 ice_ptypes_ipv4_ofos[] = {
248 	0x1D800000, 0xBFBF7800, 0x000001DF, 0x00000000,
249 	0x00000000, 0x00000155, 0x00000000, 0x00000000,
250 	0x00000000, 0x000FC000, 0x000002A0, 0x00000000,
251 	0x00015000, 0x00000000, 0x00000000, 0x00000000,
252 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
253 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
254 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
255 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
256 };
257 
258 /* Packet types for packets with an Outer/First/Single IPv4 header, includes
259  * IPv4 other PTYPEs
260  */
261 static const u32 ice_ptypes_ipv4_ofos_all[] = {
262 	0x1D800000, 0x27BF7800, 0x00000000, 0x00000000,
263 	0x00000000, 0x00000155, 0x00000000, 0x00000000,
264 	0x00000000, 0x000FC000, 0x83E0FAA0, 0x00000101,
265 	0x3FFD5000, 0x00000000, 0x02FBEFBC, 0x00000000,
266 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
267 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
268 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
269 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
270 };
271 
272 /* Packet types for packets with an Innermost/Last IPv4 header */
273 static const u32 ice_ptypes_ipv4_il[] = {
274 	0xE0000000, 0xB807700E, 0x80000003, 0xE01DC03B,
275 	0x0000000E, 0x00000000, 0x00000000, 0x00000000,
276 	0x00000000, 0x00000000, 0x001FF800, 0x00000000,
277 	0xC0FC0000, 0x0000000F, 0xBC0BC0BC, 0x00000BC0,
278 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
279 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
280 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
281 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
282 };
283 
284 /* Packet types for packets with an Outer/First/Single IPv6 header, does NOT
285  * include IPv6 other PTYPEs
286  */
287 static const u32 ice_ptypes_ipv6_ofos[] = {
288 	0x00000000, 0x00000000, 0x76000000, 0x10002000,
289 	0x00000000, 0x000002AA, 0x00000000, 0x00000000,
290 	0x00000000, 0x03F00000, 0x00000540, 0x00000000,
291 	0x0002A000, 0x00000000, 0x00000000, 0x00000000,
292 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
293 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
294 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
295 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
296 };
297 
298 /* Packet types for packets with an Outer/First/Single IPv6 header, includes
299  * IPv6 other PTYPEs
300  */
301 static const u32 ice_ptypes_ipv6_ofos_all[] = {
302 	0x00000000, 0x00000000, 0x76000000, 0xFEFDE000,
303 	0x0000077E, 0x000002AA, 0x00000000, 0x00000000,
304 	0x00000000, 0x03F00000, 0x7C1F0540, 0x00000206,
305 	0xC002A000, 0x000003FF, 0xBC000000, 0x0002FBEF,
306 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
307 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
308 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
309 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
310 };
311 
312 /* Packet types for packets with an Innermost/Last IPv6 header */
313 static const u32 ice_ptypes_ipv6_il[] = {
314 	0x00000000, 0x03B80770, 0x000001DC, 0x0EE00000,
315 	0x00000770, 0x00000000, 0x00000000, 0x00000000,
316 	0x00000000, 0x00000000, 0x7FE00000, 0x00000000,
317 	0x3F000000, 0x000003F0, 0x02F02F00, 0x0002F02F,
318 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
319 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
320 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
321 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
322 };
323 
324 /* Packet types for packets with an Outer/First/Single IPv4 header - no L4 */
325 static const u32 ice_ptypes_ipv4_ofos_no_l4[] = {
326 	0x10C00000, 0x04000800, 0x00000000, 0x00000000,
327 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
328 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
329 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
330 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
331 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
332 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
333 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
334 };
335 
336 /* Packet types for packets with an Outermost/First ARP header */
337 static const u32 ice_ptypes_arp_of[] = {
338 	0x00000800, 0x00000000, 0x00000000, 0x00000000,
339 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
340 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
341 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
342 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
343 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
344 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
345 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
346 };
347 
348 /* Packet types for packets with an Innermost/Last IPv4 header - no L4 */
349 static const u32 ice_ptypes_ipv4_il_no_l4[] = {
350 	0x60000000, 0x18043008, 0x80000002, 0x6010c021,
351 	0x00000008, 0x00000000, 0x00000000, 0x00000000,
352 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
353 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
354 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
355 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
356 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
357 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
358 };
359 
360 /* Packet types for packets with an Outer/First/Single IPv6 header - no L4 */
361 static const u32 ice_ptypes_ipv6_ofos_no_l4[] = {
362 	0x00000000, 0x00000000, 0x43000000, 0x10002000,
363 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
364 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
365 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
366 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
367 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
368 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
369 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
370 };
371 
372 /* Packet types for packets with an Innermost/Last IPv6 header - no L4 */
373 static const u32 ice_ptypes_ipv6_il_no_l4[] = {
374 	0x00000000, 0x02180430, 0x0000010c, 0x086010c0,
375 	0x00000430, 0x00000000, 0x00000000, 0x00000000,
376 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
377 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
378 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
379 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
380 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
381 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
382 };
383 
384 /* UDP Packet types for non-tunneled packets or tunneled
385  * packets with inner UDP.
386  */
387 static const u32 ice_ptypes_udp_il[] = {
388 	0x81000000, 0x20204040, 0x04000010, 0x80810102,
389 	0x00000040, 0x00000000, 0x00000000, 0x00000000,
390 	0x00000000, 0x00410000, 0x908427E0, 0x00000007,
391 	0x0413F000, 0x00000041, 0x10410410, 0x00004104,
392 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
393 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
394 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
395 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
396 };
397 
398 /* Packet types for packets with an Innermost/Last TCP header */
399 static const u32 ice_ptypes_tcp_il[] = {
400 	0x04000000, 0x80810102, 0x10000040, 0x02040408,
401 	0x00000102, 0x00000000, 0x00000000, 0x00000000,
402 	0x00000000, 0x00820000, 0x21084000, 0x00000000,
403 	0x08200000, 0x00000082, 0x20820820, 0x00008208,
404 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
405 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
406 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
407 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
408 };
409 
410 /* Packet types for packets with an Innermost/Last SCTP header */
411 static const u32 ice_ptypes_sctp_il[] = {
412 	0x08000000, 0x01020204, 0x20000081, 0x04080810,
413 	0x00000204, 0x00000000, 0x00000000, 0x00000000,
414 	0x00000000, 0x01040000, 0x00000000, 0x00000000,
415 	0x10400000, 0x00000104, 0x00000000, 0x00000000,
416 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
417 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
418 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
419 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
420 };
421 
422 /* Packet types for packets with an Outermost/First ICMP header */
423 static const u32 ice_ptypes_icmp_of[] = {
424 	0x10000000, 0x00000000, 0x00000000, 0x00000000,
425 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
426 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
427 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
428 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
429 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
430 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
431 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
432 };
433 
434 /* Packet types for packets with an Innermost/Last ICMP header */
435 static const u32 ice_ptypes_icmp_il[] = {
436 	0x00000000, 0x02040408, 0x40000102, 0x08101020,
437 	0x00000408, 0x00000000, 0x00000000, 0x00000000,
438 	0x00000000, 0x00000000, 0x42108000, 0x00000000,
439 	0x20800000, 0x00000208, 0x00000000, 0x00000000,
440 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
441 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
442 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
443 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
444 };
445 
446 /* Packet types for packets with an Outermost/First GRE header */
447 static const u32 ice_ptypes_gre_of[] = {
448 	0x00000000, 0xBFBF7800, 0x000001DF, 0xFEFDE000,
449 	0x0000017E, 0x00000000, 0x00000000, 0x00000000,
450 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
451 	0x00000000, 0x00000000, 0xBEFBEFBC, 0x0002FBEF,
452 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
453 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
454 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
455 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
456 };
457 
458 /* Packet types for packets with an Innermost/Last MAC header */
459 static const u32 ice_ptypes_mac_il[] = {
460 	0x00000000, 0x20000000, 0x00000000, 0x00000000,
461 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
462 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
463 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
464 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
465 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
466 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
467 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
468 };
469 
470 /* Packet types for GTPC */
471 static const u32 ice_ptypes_gtpc[] = {
472 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
473 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
474 	0x00000000, 0x00000000, 0x000001E0, 0x00000000,
475 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
476 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
477 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
478 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
479 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
480 };
481 
482 /* Packet types for GTPC with TEID */
483 static const u32 ice_ptypes_gtpc_tid[] = {
484 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
485 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
486 	0x00000000, 0x00000000, 0x00000060, 0x00000000,
487 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
488 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
489 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
490 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
491 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
492 };
493 
494 /* Packet types for GTPU */
495 static const struct ice_ptype_attributes ice_attr_gtpu_session[] = {
496 	{ ICE_MAC_IPV4_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_SESSION },
497 	{ ICE_MAC_IPV4_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_SESSION },
498 	{ ICE_MAC_IPV4_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_SESSION },
499 	{ ICE_MAC_IPV4_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_SESSION },
500 	{ ICE_MAC_IPV4_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_SESSION },
501 	{ ICE_MAC_IPV6_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_SESSION },
502 	{ ICE_MAC_IPV6_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_SESSION },
503 	{ ICE_MAC_IPV6_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_SESSION },
504 	{ ICE_MAC_IPV6_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_SESSION },
505 	{ ICE_MAC_IPV6_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_SESSION },
506 	{ ICE_MAC_IPV4_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_SESSION },
507 	{ ICE_MAC_IPV4_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_SESSION },
508 	{ ICE_MAC_IPV4_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_SESSION },
509 	{ ICE_MAC_IPV4_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_SESSION },
510 	{ ICE_MAC_IPV4_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_SESSION },
511 	{ ICE_MAC_IPV6_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_SESSION },
512 	{ ICE_MAC_IPV6_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_SESSION },
513 	{ ICE_MAC_IPV6_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_SESSION },
514 	{ ICE_MAC_IPV6_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_SESSION },
515 	{ ICE_MAC_IPV6_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_SESSION },
516 };
517 
518 static const struct ice_ptype_attributes ice_attr_gtpu_eh[] = {
519 	{ ICE_MAC_IPV4_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
520 	{ ICE_MAC_IPV4_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
521 	{ ICE_MAC_IPV4_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_PDU_EH },
522 	{ ICE_MAC_IPV4_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
523 	{ ICE_MAC_IPV4_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
524 	{ ICE_MAC_IPV6_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
525 	{ ICE_MAC_IPV6_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
526 	{ ICE_MAC_IPV6_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_PDU_EH },
527 	{ ICE_MAC_IPV6_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
528 	{ ICE_MAC_IPV6_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
529 	{ ICE_MAC_IPV4_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
530 	{ ICE_MAC_IPV4_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
531 	{ ICE_MAC_IPV4_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_PDU_EH },
532 	{ ICE_MAC_IPV4_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
533 	{ ICE_MAC_IPV4_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_PDU_EH },
534 	{ ICE_MAC_IPV6_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
535 	{ ICE_MAC_IPV6_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
536 	{ ICE_MAC_IPV6_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_PDU_EH },
537 	{ ICE_MAC_IPV6_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_PDU_EH },
538 	{ ICE_MAC_IPV6_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_PDU_EH },
539 };
540 
541 static const struct ice_ptype_attributes ice_attr_gtpu_down[] = {
542 	{ ICE_MAC_IPV4_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
543 	{ ICE_MAC_IPV4_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
544 	{ ICE_MAC_IPV4_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_DOWNLINK },
545 	{ ICE_MAC_IPV4_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
546 	{ ICE_MAC_IPV4_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
547 	{ ICE_MAC_IPV6_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
548 	{ ICE_MAC_IPV6_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
549 	{ ICE_MAC_IPV6_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_DOWNLINK },
550 	{ ICE_MAC_IPV6_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
551 	{ ICE_MAC_IPV6_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
552 	{ ICE_MAC_IPV4_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
553 	{ ICE_MAC_IPV4_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
554 	{ ICE_MAC_IPV4_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_DOWNLINK },
555 	{ ICE_MAC_IPV4_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
556 	{ ICE_MAC_IPV4_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_DOWNLINK },
557 	{ ICE_MAC_IPV6_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
558 	{ ICE_MAC_IPV6_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
559 	{ ICE_MAC_IPV6_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_DOWNLINK },
560 	{ ICE_MAC_IPV6_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_DOWNLINK },
561 	{ ICE_MAC_IPV6_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_DOWNLINK },
562 };
563 
564 static const struct ice_ptype_attributes ice_attr_gtpu_up[] = {
565 	{ ICE_MAC_IPV4_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_UPLINK },
566 	{ ICE_MAC_IPV4_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_UPLINK },
567 	{ ICE_MAC_IPV4_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_UPLINK },
568 	{ ICE_MAC_IPV4_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
569 	{ ICE_MAC_IPV4_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
570 	{ ICE_MAC_IPV6_GTPU_IPV4_FRAG,	  ICE_PTYPE_ATTR_GTP_UPLINK },
571 	{ ICE_MAC_IPV6_GTPU_IPV4_PAY,	  ICE_PTYPE_ATTR_GTP_UPLINK },
572 	{ ICE_MAC_IPV6_GTPU_IPV4_UDP_PAY, ICE_PTYPE_ATTR_GTP_UPLINK },
573 	{ ICE_MAC_IPV6_GTPU_IPV4_TCP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
574 	{ ICE_MAC_IPV6_GTPU_IPV4_ICMP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
575 	{ ICE_MAC_IPV4_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_UPLINK },
576 	{ ICE_MAC_IPV4_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_UPLINK },
577 	{ ICE_MAC_IPV4_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_UPLINK },
578 	{ ICE_MAC_IPV4_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
579 	{ ICE_MAC_IPV4_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_UPLINK },
580 	{ ICE_MAC_IPV6_GTPU_IPV6_FRAG,	  ICE_PTYPE_ATTR_GTP_UPLINK },
581 	{ ICE_MAC_IPV6_GTPU_IPV6_PAY,	  ICE_PTYPE_ATTR_GTP_UPLINK },
582 	{ ICE_MAC_IPV6_GTPU_IPV6_UDP_PAY, ICE_PTYPE_ATTR_GTP_UPLINK },
583 	{ ICE_MAC_IPV6_GTPU_IPV6_TCP,	  ICE_PTYPE_ATTR_GTP_UPLINK },
584 	{ ICE_MAC_IPV6_GTPU_IPV6_ICMPV6,  ICE_PTYPE_ATTR_GTP_UPLINK },
585 };
586 
587 static const u32 ice_ptypes_gtpu[] = {
588 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
589 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
590 	0x00000000, 0x00000000, 0x7FFFFE00, 0x00000000,
591 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
592 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
593 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
594 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
595 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
596 };
597 
598 /* Packet types for PPPoE */
599 static const u32 ice_ptypes_pppoe[] = {
600 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
601 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
602 	0x00000000, 0x03ffe000, 0x00000000, 0x00000000,
603 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
604 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
605 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
606 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
607 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
608 };
609 
610 /* Packet types for packets with PFCP NODE header */
611 static const u32 ice_ptypes_pfcp_node[] = {
612 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
613 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
614 	0x00000000, 0x00000000, 0x80000000, 0x00000002,
615 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
616 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
617 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
618 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
619 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
620 };
621 
622 /* Packet types for packets with PFCP SESSION header */
623 static const u32 ice_ptypes_pfcp_session[] = {
624 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
625 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
626 	0x00000000, 0x00000000, 0x00000000, 0x00000005,
627 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
628 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
629 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
630 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
631 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
632 };
633 
634 /* Packet types for L2TPv3 */
635 static const u32 ice_ptypes_l2tpv3[] = {
636 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
637 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
638 	0x00000000, 0x00000000, 0x00000000, 0x00000300,
639 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
640 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
641 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
642 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
643 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
644 };
645 
646 /* Packet types for ESP */
647 static const u32 ice_ptypes_esp[] = {
648 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
649 	0x00000000, 0x00000003, 0x00000000, 0x00000000,
650 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
651 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
652 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
653 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
654 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
655 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
656 };
657 
658 /* Packet types for AH */
659 static const u32 ice_ptypes_ah[] = {
660 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
661 	0x00000000, 0x0000000C, 0x00000000, 0x00000000,
662 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
663 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
664 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
665 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
666 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
667 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
668 };
669 
670 /* Packet types for packets with NAT_T ESP header */
671 static const u32 ice_ptypes_nat_t_esp[] = {
672 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
673 	0x00000000, 0x00000030, 0x00000000, 0x00000000,
674 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
675 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
676 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
677 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
678 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
679 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
680 };
681 
682 static const u32 ice_ptypes_mac_non_ip_ofos[] = {
683 	0x00000846, 0x00000000, 0x00000000, 0x00000000,
684 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
685 	0x00400000, 0x03FFF000, 0x00000000, 0x00000000,
686 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
687 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
688 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
689 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
690 	0x00000000, 0x00000000, 0x00000000, 0x00000000,
691 };
692 
693 /* Manage parameters and info. used during the creation of a flow profile */
694 struct ice_flow_prof_params {
695 	enum ice_block blk;
696 	u16 entry_length; /* # of bytes formatted entry will require */
697 	u8 es_cnt;
698 	struct ice_flow_prof *prof;
699 
700 	/* For ACL, the es[0] will have the data of ICE_RX_MDID_PKT_FLAGS_15_0
701 	 * This will give us the direction flags.
702 	 */
703 	struct ice_fv_word es[ICE_MAX_FV_WORDS];
704 	/* attributes can be used to add attributes to a particular PTYPE */
705 	const struct ice_ptype_attributes *attr;
706 	u16 attr_cnt;
707 
708 	u16 mask[ICE_MAX_FV_WORDS];
709 	DECLARE_BITMAP(ptypes, ICE_FLOW_PTYPE_MAX);
710 };
711 
712 #define ICE_FLOW_RSS_HDRS_INNER_MASK \
713 	(ICE_FLOW_SEG_HDR_PPPOE | ICE_FLOW_SEG_HDR_GTPC | \
714 	ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_GTPU | \
715 	ICE_FLOW_SEG_HDR_PFCP_SESSION | ICE_FLOW_SEG_HDR_L2TPV3 | \
716 	ICE_FLOW_SEG_HDR_ESP | ICE_FLOW_SEG_HDR_AH | \
717 	ICE_FLOW_SEG_HDR_NAT_T_ESP)
718 
719 #define ICE_FLOW_SEG_HDRS_L3_MASK	\
720 	(ICE_FLOW_SEG_HDR_IPV4 | ICE_FLOW_SEG_HDR_IPV6 | ICE_FLOW_SEG_HDR_ARP)
721 #define ICE_FLOW_SEG_HDRS_L4_MASK	\
722 	(ICE_FLOW_SEG_HDR_ICMP | ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_UDP | \
723 	 ICE_FLOW_SEG_HDR_SCTP)
724 /* mask for L4 protocols that are NOT part of IPv4/6 OTHER PTYPE groups */
725 #define ICE_FLOW_SEG_HDRS_L4_MASK_NO_OTHER	\
726 	(ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_SCTP)
727 
728 /**
729  * ice_flow_val_hdrs - validates packet segments for valid protocol headers
730  * @segs: array of one or more packet segments that describe the flow
731  * @segs_cnt: number of packet segments provided
732  */
733 static int ice_flow_val_hdrs(struct ice_flow_seg_info *segs, u8 segs_cnt)
734 {
735 	u8 i;
736 
737 	for (i = 0; i < segs_cnt; i++) {
738 		/* Multiple L3 headers */
739 		if (segs[i].hdrs & ICE_FLOW_SEG_HDRS_L3_MASK &&
740 		    !is_power_of_2(segs[i].hdrs & ICE_FLOW_SEG_HDRS_L3_MASK))
741 			return -EINVAL;
742 
743 		/* Multiple L4 headers */
744 		if (segs[i].hdrs & ICE_FLOW_SEG_HDRS_L4_MASK &&
745 		    !is_power_of_2(segs[i].hdrs & ICE_FLOW_SEG_HDRS_L4_MASK))
746 			return -EINVAL;
747 	}
748 
749 	return 0;
750 }
751 
752 /* Sizes of fixed known protocol headers without header options */
753 #define ICE_FLOW_PROT_HDR_SZ_MAC	14
754 #define ICE_FLOW_PROT_HDR_SZ_MAC_VLAN	(ICE_FLOW_PROT_HDR_SZ_MAC + 2)
755 #define ICE_FLOW_PROT_HDR_SZ_IPV4	20
756 #define ICE_FLOW_PROT_HDR_SZ_IPV6	40
757 #define ICE_FLOW_PROT_HDR_SZ_ARP	28
758 #define ICE_FLOW_PROT_HDR_SZ_ICMP	8
759 #define ICE_FLOW_PROT_HDR_SZ_TCP	20
760 #define ICE_FLOW_PROT_HDR_SZ_UDP	8
761 #define ICE_FLOW_PROT_HDR_SZ_SCTP	12
762 
763 /**
764  * ice_flow_calc_seg_sz - calculates size of a packet segment based on headers
765  * @params: information about the flow to be processed
766  * @seg: index of packet segment whose header size is to be determined
767  */
768 static u16 ice_flow_calc_seg_sz(struct ice_flow_prof_params *params, u8 seg)
769 {
770 	u16 sz;
771 
772 	/* L2 headers */
773 	sz = (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_VLAN) ?
774 		ICE_FLOW_PROT_HDR_SZ_MAC_VLAN : ICE_FLOW_PROT_HDR_SZ_MAC;
775 
776 	/* L3 headers */
777 	if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_IPV4)
778 		sz += ICE_FLOW_PROT_HDR_SZ_IPV4;
779 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_IPV6)
780 		sz += ICE_FLOW_PROT_HDR_SZ_IPV6;
781 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_ARP)
782 		sz += ICE_FLOW_PROT_HDR_SZ_ARP;
783 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDRS_L4_MASK)
784 		/* An L3 header is required if L4 is specified */
785 		return 0;
786 
787 	/* L4 headers */
788 	if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_ICMP)
789 		sz += ICE_FLOW_PROT_HDR_SZ_ICMP;
790 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_TCP)
791 		sz += ICE_FLOW_PROT_HDR_SZ_TCP;
792 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_UDP)
793 		sz += ICE_FLOW_PROT_HDR_SZ_UDP;
794 	else if (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_SCTP)
795 		sz += ICE_FLOW_PROT_HDR_SZ_SCTP;
796 
797 	return sz;
798 }
799 
800 /**
801  * ice_flow_proc_seg_hdrs - process protocol headers present in pkt segments
802  * @params: information about the flow to be processed
803  *
804  * This function identifies the packet types associated with the protocol
805  * headers being present in packet segments of the specified flow profile.
806  */
807 static int ice_flow_proc_seg_hdrs(struct ice_flow_prof_params *params)
808 {
809 	struct ice_flow_prof *prof;
810 	u8 i;
811 
812 	memset(params->ptypes, 0xff, sizeof(params->ptypes));
813 
814 	prof = params->prof;
815 
816 	for (i = 0; i < params->prof->segs_cnt; i++) {
817 		const unsigned long *src;
818 		u32 hdrs;
819 
820 		hdrs = prof->segs[i].hdrs;
821 
822 		if (hdrs & ICE_FLOW_SEG_HDR_ETH) {
823 			src = !i ? (const unsigned long *)ice_ptypes_mac_ofos :
824 				(const unsigned long *)ice_ptypes_mac_il;
825 			bitmap_and(params->ptypes, params->ptypes, src,
826 				   ICE_FLOW_PTYPE_MAX);
827 		}
828 
829 		if (i && hdrs & ICE_FLOW_SEG_HDR_VLAN) {
830 			src = (const unsigned long *)ice_ptypes_macvlan_il;
831 			bitmap_and(params->ptypes, params->ptypes, src,
832 				   ICE_FLOW_PTYPE_MAX);
833 		}
834 
835 		if (!i && hdrs & ICE_FLOW_SEG_HDR_ARP) {
836 			bitmap_and(params->ptypes, params->ptypes,
837 				   (const unsigned long *)ice_ptypes_arp_of,
838 				   ICE_FLOW_PTYPE_MAX);
839 		}
840 
841 		if ((hdrs & ICE_FLOW_SEG_HDR_IPV4) &&
842 		    (hdrs & ICE_FLOW_SEG_HDR_IPV_OTHER)) {
843 			src = i ? (const unsigned long *)ice_ptypes_ipv4_il :
844 				(const unsigned long *)ice_ptypes_ipv4_ofos_all;
845 			bitmap_and(params->ptypes, params->ptypes, src,
846 				   ICE_FLOW_PTYPE_MAX);
847 		} else if ((hdrs & ICE_FLOW_SEG_HDR_IPV6) &&
848 			   (hdrs & ICE_FLOW_SEG_HDR_IPV_OTHER)) {
849 			src = i ? (const unsigned long *)ice_ptypes_ipv6_il :
850 				(const unsigned long *)ice_ptypes_ipv6_ofos_all;
851 			bitmap_and(params->ptypes, params->ptypes, src,
852 				   ICE_FLOW_PTYPE_MAX);
853 		} else if ((hdrs & ICE_FLOW_SEG_HDR_IPV4) &&
854 			   !(hdrs & ICE_FLOW_SEG_HDRS_L4_MASK_NO_OTHER)) {
855 			src = !i ? (const unsigned long *)ice_ptypes_ipv4_ofos_no_l4 :
856 				(const unsigned long *)ice_ptypes_ipv4_il_no_l4;
857 			bitmap_and(params->ptypes, params->ptypes, src,
858 				   ICE_FLOW_PTYPE_MAX);
859 		} else if (hdrs & ICE_FLOW_SEG_HDR_IPV4) {
860 			src = !i ? (const unsigned long *)ice_ptypes_ipv4_ofos :
861 				(const unsigned long *)ice_ptypes_ipv4_il;
862 			bitmap_and(params->ptypes, params->ptypes, src,
863 				   ICE_FLOW_PTYPE_MAX);
864 		} else if ((hdrs & ICE_FLOW_SEG_HDR_IPV6) &&
865 			   !(hdrs & ICE_FLOW_SEG_HDRS_L4_MASK_NO_OTHER)) {
866 			src = !i ? (const unsigned long *)ice_ptypes_ipv6_ofos_no_l4 :
867 				(const unsigned long *)ice_ptypes_ipv6_il_no_l4;
868 			bitmap_and(params->ptypes, params->ptypes, src,
869 				   ICE_FLOW_PTYPE_MAX);
870 		} else if (hdrs & ICE_FLOW_SEG_HDR_IPV6) {
871 			src = !i ? (const unsigned long *)ice_ptypes_ipv6_ofos :
872 				(const unsigned long *)ice_ptypes_ipv6_il;
873 			bitmap_and(params->ptypes, params->ptypes, src,
874 				   ICE_FLOW_PTYPE_MAX);
875 		}
876 
877 		if (hdrs & ICE_FLOW_SEG_HDR_ETH_NON_IP) {
878 			src = (const unsigned long *)ice_ptypes_mac_non_ip_ofos;
879 			bitmap_and(params->ptypes, params->ptypes, src,
880 				   ICE_FLOW_PTYPE_MAX);
881 		} else if (hdrs & ICE_FLOW_SEG_HDR_PPPOE) {
882 			src = (const unsigned long *)ice_ptypes_pppoe;
883 			bitmap_and(params->ptypes, params->ptypes, src,
884 				   ICE_FLOW_PTYPE_MAX);
885 		} else {
886 			src = (const unsigned long *)ice_ptypes_pppoe;
887 			bitmap_andnot(params->ptypes, params->ptypes, src,
888 				      ICE_FLOW_PTYPE_MAX);
889 		}
890 
891 		if (hdrs & ICE_FLOW_SEG_HDR_UDP) {
892 			src = (const unsigned long *)ice_ptypes_udp_il;
893 			bitmap_and(params->ptypes, params->ptypes, src,
894 				   ICE_FLOW_PTYPE_MAX);
895 		} else if (hdrs & ICE_FLOW_SEG_HDR_TCP) {
896 			bitmap_and(params->ptypes, params->ptypes,
897 				   (const unsigned long *)ice_ptypes_tcp_il,
898 				   ICE_FLOW_PTYPE_MAX);
899 		} else if (hdrs & ICE_FLOW_SEG_HDR_SCTP) {
900 			src = (const unsigned long *)ice_ptypes_sctp_il;
901 			bitmap_and(params->ptypes, params->ptypes, src,
902 				   ICE_FLOW_PTYPE_MAX);
903 		}
904 
905 		if (hdrs & ICE_FLOW_SEG_HDR_ICMP) {
906 			src = !i ? (const unsigned long *)ice_ptypes_icmp_of :
907 				(const unsigned long *)ice_ptypes_icmp_il;
908 			bitmap_and(params->ptypes, params->ptypes, src,
909 				   ICE_FLOW_PTYPE_MAX);
910 		} else if (hdrs & ICE_FLOW_SEG_HDR_GRE) {
911 			if (!i) {
912 				src = (const unsigned long *)ice_ptypes_gre_of;
913 				bitmap_and(params->ptypes, params->ptypes,
914 					   src, ICE_FLOW_PTYPE_MAX);
915 			}
916 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPC) {
917 			src = (const unsigned long *)ice_ptypes_gtpc;
918 			bitmap_and(params->ptypes, params->ptypes, src,
919 				   ICE_FLOW_PTYPE_MAX);
920 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPC_TEID) {
921 			src = (const unsigned long *)ice_ptypes_gtpc_tid;
922 			bitmap_and(params->ptypes, params->ptypes, src,
923 				   ICE_FLOW_PTYPE_MAX);
924 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPU_DWN) {
925 			src = (const unsigned long *)ice_ptypes_gtpu;
926 			bitmap_and(params->ptypes, params->ptypes, src,
927 				   ICE_FLOW_PTYPE_MAX);
928 
929 			/* Attributes for GTP packet with downlink */
930 			params->attr = ice_attr_gtpu_down;
931 			params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_down);
932 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPU_UP) {
933 			src = (const unsigned long *)ice_ptypes_gtpu;
934 			bitmap_and(params->ptypes, params->ptypes, src,
935 				   ICE_FLOW_PTYPE_MAX);
936 
937 			/* Attributes for GTP packet with uplink */
938 			params->attr = ice_attr_gtpu_up;
939 			params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_up);
940 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPU_EH) {
941 			src = (const unsigned long *)ice_ptypes_gtpu;
942 			bitmap_and(params->ptypes, params->ptypes, src,
943 				   ICE_FLOW_PTYPE_MAX);
944 
945 			/* Attributes for GTP packet with Extension Header */
946 			params->attr = ice_attr_gtpu_eh;
947 			params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_eh);
948 		} else if (hdrs & ICE_FLOW_SEG_HDR_GTPU_IP) {
949 			src = (const unsigned long *)ice_ptypes_gtpu;
950 			bitmap_and(params->ptypes, params->ptypes, src,
951 				   ICE_FLOW_PTYPE_MAX);
952 		} else if (hdrs & ICE_FLOW_SEG_HDR_L2TPV3) {
953 			src = (const unsigned long *)ice_ptypes_l2tpv3;
954 			bitmap_and(params->ptypes, params->ptypes, src,
955 				   ICE_FLOW_PTYPE_MAX);
956 		} else if (hdrs & ICE_FLOW_SEG_HDR_ESP) {
957 			src = (const unsigned long *)ice_ptypes_esp;
958 			bitmap_and(params->ptypes, params->ptypes, src,
959 				   ICE_FLOW_PTYPE_MAX);
960 		} else if (hdrs & ICE_FLOW_SEG_HDR_AH) {
961 			src = (const unsigned long *)ice_ptypes_ah;
962 			bitmap_and(params->ptypes, params->ptypes, src,
963 				   ICE_FLOW_PTYPE_MAX);
964 		} else if (hdrs & ICE_FLOW_SEG_HDR_NAT_T_ESP) {
965 			src = (const unsigned long *)ice_ptypes_nat_t_esp;
966 			bitmap_and(params->ptypes, params->ptypes, src,
967 				   ICE_FLOW_PTYPE_MAX);
968 		}
969 
970 		if (hdrs & ICE_FLOW_SEG_HDR_PFCP) {
971 			if (hdrs & ICE_FLOW_SEG_HDR_PFCP_NODE)
972 				src = (const unsigned long *)ice_ptypes_pfcp_node;
973 			else
974 				src = (const unsigned long *)ice_ptypes_pfcp_session;
975 
976 			bitmap_and(params->ptypes, params->ptypes, src,
977 				   ICE_FLOW_PTYPE_MAX);
978 		} else {
979 			src = (const unsigned long *)ice_ptypes_pfcp_node;
980 			bitmap_andnot(params->ptypes, params->ptypes, src,
981 				      ICE_FLOW_PTYPE_MAX);
982 
983 			src = (const unsigned long *)ice_ptypes_pfcp_session;
984 			bitmap_andnot(params->ptypes, params->ptypes, src,
985 				      ICE_FLOW_PTYPE_MAX);
986 		}
987 	}
988 
989 	return 0;
990 }
991 
992 /**
993  * ice_flow_xtract_fld - Create an extraction sequence entry for the given field
994  * @hw: pointer to the HW struct
995  * @params: information about the flow to be processed
996  * @seg: packet segment index of the field to be extracted
997  * @fld: ID of field to be extracted
998  * @match: bit field of all fields
999  *
1000  * This function determines the protocol ID, offset, and size of the given
1001  * field. It then allocates one or more extraction sequence entries for the
1002  * given field, and fill the entries with protocol ID and offset information.
1003  */
1004 static int
1005 ice_flow_xtract_fld(struct ice_hw *hw, struct ice_flow_prof_params *params,
1006 		    u8 seg, enum ice_flow_field fld, u64 match)
1007 {
1008 	enum ice_flow_field sib = ICE_FLOW_FIELD_IDX_MAX;
1009 	enum ice_prot_id prot_id = ICE_PROT_ID_INVAL;
1010 	u8 fv_words = hw->blk[params->blk].es.fvw;
1011 	struct ice_flow_fld_info *flds;
1012 	u16 cnt, ese_bits, i;
1013 	u16 sib_mask = 0;
1014 	u16 mask;
1015 	u16 off;
1016 
1017 	flds = params->prof->segs[seg].fields;
1018 
1019 	switch (fld) {
1020 	case ICE_FLOW_FIELD_IDX_ETH_DA:
1021 	case ICE_FLOW_FIELD_IDX_ETH_SA:
1022 	case ICE_FLOW_FIELD_IDX_S_VLAN:
1023 	case ICE_FLOW_FIELD_IDX_C_VLAN:
1024 		prot_id = seg == 0 ? ICE_PROT_MAC_OF_OR_S : ICE_PROT_MAC_IL;
1025 		break;
1026 	case ICE_FLOW_FIELD_IDX_ETH_TYPE:
1027 		prot_id = seg == 0 ? ICE_PROT_ETYPE_OL : ICE_PROT_ETYPE_IL;
1028 		break;
1029 	case ICE_FLOW_FIELD_IDX_IPV4_DSCP:
1030 		prot_id = seg == 0 ? ICE_PROT_IPV4_OF_OR_S : ICE_PROT_IPV4_IL;
1031 		break;
1032 	case ICE_FLOW_FIELD_IDX_IPV6_DSCP:
1033 		prot_id = seg == 0 ? ICE_PROT_IPV6_OF_OR_S : ICE_PROT_IPV6_IL;
1034 		break;
1035 	case ICE_FLOW_FIELD_IDX_IPV4_TTL:
1036 	case ICE_FLOW_FIELD_IDX_IPV4_PROT:
1037 		prot_id = seg == 0 ? ICE_PROT_IPV4_OF_OR_S : ICE_PROT_IPV4_IL;
1038 
1039 		/* TTL and PROT share the same extraction seq. entry.
1040 		 * Each is considered a sibling to the other in terms of sharing
1041 		 * the same extraction sequence entry.
1042 		 */
1043 		if (fld == ICE_FLOW_FIELD_IDX_IPV4_TTL)
1044 			sib = ICE_FLOW_FIELD_IDX_IPV4_PROT;
1045 		else if (fld == ICE_FLOW_FIELD_IDX_IPV4_PROT)
1046 			sib = ICE_FLOW_FIELD_IDX_IPV4_TTL;
1047 
1048 		/* If the sibling field is also included, that field's
1049 		 * mask needs to be included.
1050 		 */
1051 		if (match & BIT(sib))
1052 			sib_mask = ice_flds_info[sib].mask;
1053 		break;
1054 	case ICE_FLOW_FIELD_IDX_IPV6_TTL:
1055 	case ICE_FLOW_FIELD_IDX_IPV6_PROT:
1056 		prot_id = seg == 0 ? ICE_PROT_IPV6_OF_OR_S : ICE_PROT_IPV6_IL;
1057 
1058 		/* TTL and PROT share the same extraction seq. entry.
1059 		 * Each is considered a sibling to the other in terms of sharing
1060 		 * the same extraction sequence entry.
1061 		 */
1062 		if (fld == ICE_FLOW_FIELD_IDX_IPV6_TTL)
1063 			sib = ICE_FLOW_FIELD_IDX_IPV6_PROT;
1064 		else if (fld == ICE_FLOW_FIELD_IDX_IPV6_PROT)
1065 			sib = ICE_FLOW_FIELD_IDX_IPV6_TTL;
1066 
1067 		/* If the sibling field is also included, that field's
1068 		 * mask needs to be included.
1069 		 */
1070 		if (match & BIT(sib))
1071 			sib_mask = ice_flds_info[sib].mask;
1072 		break;
1073 	case ICE_FLOW_FIELD_IDX_IPV4_SA:
1074 	case ICE_FLOW_FIELD_IDX_IPV4_DA:
1075 		prot_id = seg == 0 ? ICE_PROT_IPV4_OF_OR_S : ICE_PROT_IPV4_IL;
1076 		break;
1077 	case ICE_FLOW_FIELD_IDX_IPV6_SA:
1078 	case ICE_FLOW_FIELD_IDX_IPV6_DA:
1079 		prot_id = seg == 0 ? ICE_PROT_IPV6_OF_OR_S : ICE_PROT_IPV6_IL;
1080 		break;
1081 	case ICE_FLOW_FIELD_IDX_TCP_SRC_PORT:
1082 	case ICE_FLOW_FIELD_IDX_TCP_DST_PORT:
1083 	case ICE_FLOW_FIELD_IDX_TCP_FLAGS:
1084 		prot_id = ICE_PROT_TCP_IL;
1085 		break;
1086 	case ICE_FLOW_FIELD_IDX_UDP_SRC_PORT:
1087 	case ICE_FLOW_FIELD_IDX_UDP_DST_PORT:
1088 		prot_id = ICE_PROT_UDP_IL_OR_S;
1089 		break;
1090 	case ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT:
1091 	case ICE_FLOW_FIELD_IDX_SCTP_DST_PORT:
1092 		prot_id = ICE_PROT_SCTP_IL;
1093 		break;
1094 	case ICE_FLOW_FIELD_IDX_GTPC_TEID:
1095 	case ICE_FLOW_FIELD_IDX_GTPU_IP_TEID:
1096 	case ICE_FLOW_FIELD_IDX_GTPU_UP_TEID:
1097 	case ICE_FLOW_FIELD_IDX_GTPU_DWN_TEID:
1098 	case ICE_FLOW_FIELD_IDX_GTPU_EH_TEID:
1099 	case ICE_FLOW_FIELD_IDX_GTPU_EH_QFI:
1100 		/* GTP is accessed through UDP OF protocol */
1101 		prot_id = ICE_PROT_UDP_OF;
1102 		break;
1103 	case ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID:
1104 		prot_id = ICE_PROT_PPPOE;
1105 		break;
1106 	case ICE_FLOW_FIELD_IDX_PFCP_SEID:
1107 		prot_id = ICE_PROT_UDP_IL_OR_S;
1108 		break;
1109 	case ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID:
1110 		prot_id = ICE_PROT_L2TPV3;
1111 		break;
1112 	case ICE_FLOW_FIELD_IDX_ESP_SPI:
1113 		prot_id = ICE_PROT_ESP_F;
1114 		break;
1115 	case ICE_FLOW_FIELD_IDX_AH_SPI:
1116 		prot_id = ICE_PROT_ESP_2;
1117 		break;
1118 	case ICE_FLOW_FIELD_IDX_NAT_T_ESP_SPI:
1119 		prot_id = ICE_PROT_UDP_IL_OR_S;
1120 		break;
1121 	case ICE_FLOW_FIELD_IDX_ARP_SIP:
1122 	case ICE_FLOW_FIELD_IDX_ARP_DIP:
1123 	case ICE_FLOW_FIELD_IDX_ARP_SHA:
1124 	case ICE_FLOW_FIELD_IDX_ARP_DHA:
1125 	case ICE_FLOW_FIELD_IDX_ARP_OP:
1126 		prot_id = ICE_PROT_ARP_OF;
1127 		break;
1128 	case ICE_FLOW_FIELD_IDX_ICMP_TYPE:
1129 	case ICE_FLOW_FIELD_IDX_ICMP_CODE:
1130 		/* ICMP type and code share the same extraction seq. entry */
1131 		prot_id = (params->prof->segs[seg].hdrs & ICE_FLOW_SEG_HDR_IPV4) ?
1132 				ICE_PROT_ICMP_IL : ICE_PROT_ICMPV6_IL;
1133 		sib = fld == ICE_FLOW_FIELD_IDX_ICMP_TYPE ?
1134 			ICE_FLOW_FIELD_IDX_ICMP_CODE :
1135 			ICE_FLOW_FIELD_IDX_ICMP_TYPE;
1136 		break;
1137 	case ICE_FLOW_FIELD_IDX_GRE_KEYID:
1138 		prot_id = ICE_PROT_GRE_OF;
1139 		break;
1140 	default:
1141 		return -EOPNOTSUPP;
1142 	}
1143 
1144 	/* Each extraction sequence entry is a word in size, and extracts a
1145 	 * word-aligned offset from a protocol header.
1146 	 */
1147 	ese_bits = ICE_FLOW_FV_EXTRACT_SZ * BITS_PER_BYTE;
1148 
1149 	flds[fld].xtrct.prot_id = prot_id;
1150 	flds[fld].xtrct.off = (ice_flds_info[fld].off / ese_bits) *
1151 		ICE_FLOW_FV_EXTRACT_SZ;
1152 	flds[fld].xtrct.disp = (u8)(ice_flds_info[fld].off % ese_bits);
1153 	flds[fld].xtrct.idx = params->es_cnt;
1154 	flds[fld].xtrct.mask = ice_flds_info[fld].mask;
1155 
1156 	/* Adjust the next field-entry index after accommodating the number of
1157 	 * entries this field consumes
1158 	 */
1159 	cnt = DIV_ROUND_UP(flds[fld].xtrct.disp + ice_flds_info[fld].size,
1160 			   ese_bits);
1161 
1162 	/* Fill in the extraction sequence entries needed for this field */
1163 	off = flds[fld].xtrct.off;
1164 	mask = flds[fld].xtrct.mask;
1165 	for (i = 0; i < cnt; i++) {
1166 		/* Only consume an extraction sequence entry if there is no
1167 		 * sibling field associated with this field or the sibling entry
1168 		 * already extracts the word shared with this field.
1169 		 */
1170 		if (sib == ICE_FLOW_FIELD_IDX_MAX ||
1171 		    flds[sib].xtrct.prot_id == ICE_PROT_ID_INVAL ||
1172 		    flds[sib].xtrct.off != off) {
1173 			u8 idx;
1174 
1175 			/* Make sure the number of extraction sequence required
1176 			 * does not exceed the block's capability
1177 			 */
1178 			if (params->es_cnt >= fv_words)
1179 				return -ENOSPC;
1180 
1181 			/* some blocks require a reversed field vector layout */
1182 			if (hw->blk[params->blk].es.reverse)
1183 				idx = fv_words - params->es_cnt - 1;
1184 			else
1185 				idx = params->es_cnt;
1186 
1187 			params->es[idx].prot_id = prot_id;
1188 			params->es[idx].off = off;
1189 			params->mask[idx] = mask | sib_mask;
1190 			params->es_cnt++;
1191 		}
1192 
1193 		off += ICE_FLOW_FV_EXTRACT_SZ;
1194 	}
1195 
1196 	return 0;
1197 }
1198 
1199 /**
1200  * ice_flow_xtract_raws - Create extract sequence entries for raw bytes
1201  * @hw: pointer to the HW struct
1202  * @params: information about the flow to be processed
1203  * @seg: index of packet segment whose raw fields are to be extracted
1204  */
1205 static int
1206 ice_flow_xtract_raws(struct ice_hw *hw, struct ice_flow_prof_params *params,
1207 		     u8 seg)
1208 {
1209 	u16 fv_words;
1210 	u16 hdrs_sz;
1211 	u8 i;
1212 
1213 	if (!params->prof->segs[seg].raws_cnt)
1214 		return 0;
1215 
1216 	if (params->prof->segs[seg].raws_cnt >
1217 	    ARRAY_SIZE(params->prof->segs[seg].raws))
1218 		return -ENOSPC;
1219 
1220 	/* Offsets within the segment headers are not supported */
1221 	hdrs_sz = ice_flow_calc_seg_sz(params, seg);
1222 	if (!hdrs_sz)
1223 		return -EINVAL;
1224 
1225 	fv_words = hw->blk[params->blk].es.fvw;
1226 
1227 	for (i = 0; i < params->prof->segs[seg].raws_cnt; i++) {
1228 		struct ice_flow_seg_fld_raw *raw;
1229 		u16 off, cnt, j;
1230 
1231 		raw = &params->prof->segs[seg].raws[i];
1232 
1233 		/* Storing extraction information */
1234 		raw->info.xtrct.prot_id = ICE_PROT_MAC_OF_OR_S;
1235 		raw->info.xtrct.off = (raw->off / ICE_FLOW_FV_EXTRACT_SZ) *
1236 			ICE_FLOW_FV_EXTRACT_SZ;
1237 		raw->info.xtrct.disp = (raw->off % ICE_FLOW_FV_EXTRACT_SZ) *
1238 			BITS_PER_BYTE;
1239 		raw->info.xtrct.idx = params->es_cnt;
1240 
1241 		/* Determine the number of field vector entries this raw field
1242 		 * consumes.
1243 		 */
1244 		cnt = DIV_ROUND_UP(raw->info.xtrct.disp +
1245 				   (raw->info.src.last * BITS_PER_BYTE),
1246 				   (ICE_FLOW_FV_EXTRACT_SZ * BITS_PER_BYTE));
1247 		off = raw->info.xtrct.off;
1248 		for (j = 0; j < cnt; j++) {
1249 			u16 idx;
1250 
1251 			/* Make sure the number of extraction sequence required
1252 			 * does not exceed the block's capability
1253 			 */
1254 			if (params->es_cnt >= hw->blk[params->blk].es.count ||
1255 			    params->es_cnt >= ICE_MAX_FV_WORDS)
1256 				return -ENOSPC;
1257 
1258 			/* some blocks require a reversed field vector layout */
1259 			if (hw->blk[params->blk].es.reverse)
1260 				idx = fv_words - params->es_cnt - 1;
1261 			else
1262 				idx = params->es_cnt;
1263 
1264 			params->es[idx].prot_id = raw->info.xtrct.prot_id;
1265 			params->es[idx].off = off;
1266 			params->es_cnt++;
1267 			off += ICE_FLOW_FV_EXTRACT_SZ;
1268 		}
1269 	}
1270 
1271 	return 0;
1272 }
1273 
1274 /**
1275  * ice_flow_create_xtrct_seq - Create an extraction sequence for given segments
1276  * @hw: pointer to the HW struct
1277  * @params: information about the flow to be processed
1278  *
1279  * This function iterates through all matched fields in the given segments, and
1280  * creates an extraction sequence for the fields.
1281  */
1282 static int
1283 ice_flow_create_xtrct_seq(struct ice_hw *hw,
1284 			  struct ice_flow_prof_params *params)
1285 {
1286 	struct ice_flow_prof *prof = params->prof;
1287 	int status = 0;
1288 	u8 i;
1289 
1290 	for (i = 0; i < prof->segs_cnt; i++) {
1291 		u64 match = params->prof->segs[i].match;
1292 		enum ice_flow_field j;
1293 
1294 		for_each_set_bit(j, (unsigned long *)&match,
1295 				 ICE_FLOW_FIELD_IDX_MAX) {
1296 			status = ice_flow_xtract_fld(hw, params, i, j, match);
1297 			if (status)
1298 				return status;
1299 			clear_bit(j, (unsigned long *)&match);
1300 		}
1301 
1302 		/* Process raw matching bytes */
1303 		status = ice_flow_xtract_raws(hw, params, i);
1304 		if (status)
1305 			return status;
1306 	}
1307 
1308 	return status;
1309 }
1310 
1311 /**
1312  * ice_flow_proc_segs - process all packet segments associated with a profile
1313  * @hw: pointer to the HW struct
1314  * @params: information about the flow to be processed
1315  */
1316 static int
1317 ice_flow_proc_segs(struct ice_hw *hw, struct ice_flow_prof_params *params)
1318 {
1319 	int status;
1320 
1321 	status = ice_flow_proc_seg_hdrs(params);
1322 	if (status)
1323 		return status;
1324 
1325 	status = ice_flow_create_xtrct_seq(hw, params);
1326 	if (status)
1327 		return status;
1328 
1329 	switch (params->blk) {
1330 	case ICE_BLK_FD:
1331 	case ICE_BLK_RSS:
1332 		status = 0;
1333 		break;
1334 	default:
1335 		return -EOPNOTSUPP;
1336 	}
1337 
1338 	return status;
1339 }
1340 
1341 #define ICE_FLOW_FIND_PROF_CHK_FLDS	0x00000001
1342 #define ICE_FLOW_FIND_PROF_CHK_VSI	0x00000002
1343 #define ICE_FLOW_FIND_PROF_NOT_CHK_DIR	0x00000004
1344 #define ICE_FLOW_FIND_PROF_CHK_SYMM	0x00000008
1345 
1346 /**
1347  * ice_flow_find_prof_conds - Find a profile matching headers and conditions
1348  * @hw: pointer to the HW struct
1349  * @blk: classification stage
1350  * @dir: flow direction
1351  * @segs: array of one or more packet segments that describe the flow
1352  * @segs_cnt: number of packet segments provided
1353  * @symm: symmetric setting for RSS profiles
1354  * @vsi_handle: software VSI handle to check VSI (ICE_FLOW_FIND_PROF_CHK_VSI)
1355  * @conds: additional conditions to be checked (ICE_FLOW_FIND_PROF_CHK_*)
1356  */
1357 static struct ice_flow_prof *
1358 ice_flow_find_prof_conds(struct ice_hw *hw, enum ice_block blk,
1359 			 enum ice_flow_dir dir, struct ice_flow_seg_info *segs,
1360 			 u8 segs_cnt, bool symm, u16 vsi_handle, u32 conds)
1361 {
1362 	struct ice_flow_prof *p, *prof = NULL;
1363 
1364 	mutex_lock(&hw->fl_profs_locks[blk]);
1365 	list_for_each_entry(p, &hw->fl_profs[blk], l_entry)
1366 		if ((p->dir == dir || conds & ICE_FLOW_FIND_PROF_NOT_CHK_DIR) &&
1367 		    segs_cnt && segs_cnt == p->segs_cnt) {
1368 			u8 i;
1369 
1370 			/* Check for profile-VSI association if specified */
1371 			if ((conds & ICE_FLOW_FIND_PROF_CHK_VSI) &&
1372 			    ice_is_vsi_valid(hw, vsi_handle) &&
1373 			    !test_bit(vsi_handle, p->vsis))
1374 				continue;
1375 
1376 			/* Check for symmetric settings */
1377 			if ((conds & ICE_FLOW_FIND_PROF_CHK_SYMM) &&
1378 			    p->symm != symm)
1379 				continue;
1380 
1381 			/* Protocol headers must be checked. Matched fields are
1382 			 * checked if specified.
1383 			 */
1384 			for (i = 0; i < segs_cnt; i++)
1385 				if (segs[i].hdrs != p->segs[i].hdrs ||
1386 				    ((conds & ICE_FLOW_FIND_PROF_CHK_FLDS) &&
1387 				     segs[i].match != p->segs[i].match))
1388 					break;
1389 
1390 			/* A match is found if all segments are matched */
1391 			if (i == segs_cnt) {
1392 				prof = p;
1393 				break;
1394 			}
1395 		}
1396 	mutex_unlock(&hw->fl_profs_locks[blk]);
1397 
1398 	return prof;
1399 }
1400 
1401 /**
1402  * ice_flow_find_prof_id - Look up a profile with given profile ID
1403  * @hw: pointer to the HW struct
1404  * @blk: classification stage
1405  * @prof_id: unique ID to identify this flow profile
1406  */
1407 static struct ice_flow_prof *
1408 ice_flow_find_prof_id(struct ice_hw *hw, enum ice_block blk, u64 prof_id)
1409 {
1410 	struct ice_flow_prof *p;
1411 
1412 	list_for_each_entry(p, &hw->fl_profs[blk], l_entry)
1413 		if (p->id == prof_id)
1414 			return p;
1415 
1416 	return NULL;
1417 }
1418 
1419 /**
1420  * ice_flow_rem_entry_sync - Remove a flow entry
1421  * @hw: pointer to the HW struct
1422  * @blk: classification stage
1423  * @entry: flow entry to be removed
1424  */
1425 static int
1426 ice_flow_rem_entry_sync(struct ice_hw *hw, enum ice_block __always_unused blk,
1427 			struct ice_flow_entry *entry)
1428 {
1429 	if (!entry)
1430 		return -EINVAL;
1431 
1432 	list_del(&entry->l_entry);
1433 
1434 	devm_kfree(ice_hw_to_dev(hw), entry);
1435 
1436 	return 0;
1437 }
1438 
1439 /**
1440  * ice_flow_add_prof_sync - Add a flow profile for packet segments and fields
1441  * @hw: pointer to the HW struct
1442  * @blk: classification stage
1443  * @dir: flow direction
1444  * @segs: array of one or more packet segments that describe the flow
1445  * @segs_cnt: number of packet segments provided
1446  * @symm: symmetric setting for RSS profiles
1447  * @prof: stores the returned flow profile added
1448  *
1449  * Assumption: the caller has acquired the lock to the profile list
1450  */
1451 static int
1452 ice_flow_add_prof_sync(struct ice_hw *hw, enum ice_block blk,
1453 		       enum ice_flow_dir dir,
1454 		       struct ice_flow_seg_info *segs, u8 segs_cnt,
1455 		       bool symm, struct ice_flow_prof **prof)
1456 {
1457 	struct ice_flow_prof_params *params;
1458 	struct ice_prof_id *ids;
1459 	int status;
1460 	u64 prof_id;
1461 	u8 i;
1462 
1463 	if (!prof)
1464 		return -EINVAL;
1465 
1466 	ids = &hw->blk[blk].prof_id;
1467 	prof_id = find_first_zero_bit(ids->id, ids->count);
1468 	if (prof_id >= ids->count)
1469 		return -ENOSPC;
1470 
1471 	params = kzalloc_obj(*params);
1472 	if (!params)
1473 		return -ENOMEM;
1474 
1475 	params->prof = devm_kzalloc_obj(ice_hw_to_dev(hw), *params->prof);
1476 	if (!params->prof) {
1477 		status = -ENOMEM;
1478 		goto free_params;
1479 	}
1480 
1481 	/* initialize extraction sequence to all invalid (0xff) */
1482 	for (i = 0; i < ICE_MAX_FV_WORDS; i++) {
1483 		params->es[i].prot_id = ICE_PROT_INVALID;
1484 		params->es[i].off = ICE_FV_OFFSET_INVAL;
1485 	}
1486 
1487 	params->blk = blk;
1488 	params->prof->id = prof_id;
1489 	params->prof->dir = dir;
1490 	params->prof->segs_cnt = segs_cnt;
1491 	params->prof->symm = symm;
1492 
1493 	/* Make a copy of the segments that need to be persistent in the flow
1494 	 * profile instance
1495 	 */
1496 	for (i = 0; i < segs_cnt; i++)
1497 		memcpy(&params->prof->segs[i], &segs[i], sizeof(*segs));
1498 
1499 	status = ice_flow_proc_segs(hw, params);
1500 	if (status) {
1501 		ice_debug(hw, ICE_DBG_FLOW, "Error processing a flow's packet segments\n");
1502 		goto out;
1503 	}
1504 
1505 	/* Add a HW profile for this flow profile */
1506 	status = ice_add_prof(hw, blk, prof_id, params->ptypes,
1507 			      params->attr, params->attr_cnt, params->es,
1508 			      params->mask, symm, true);
1509 	if (status) {
1510 		ice_debug(hw, ICE_DBG_FLOW, "Error adding a HW flow profile\n");
1511 		goto out;
1512 	}
1513 
1514 	INIT_LIST_HEAD(&params->prof->entries);
1515 	mutex_init(&params->prof->entries_lock);
1516 	set_bit(prof_id, ids->id);
1517 	*prof = params->prof;
1518 
1519 out:
1520 	if (status)
1521 		devm_kfree(ice_hw_to_dev(hw), params->prof);
1522 free_params:
1523 	kfree(params);
1524 
1525 	return status;
1526 }
1527 
1528 /**
1529  * ice_flow_rem_prof_sync - remove a flow profile
1530  * @hw: pointer to the hardware structure
1531  * @blk: classification stage
1532  * @prof: pointer to flow profile to remove
1533  *
1534  * Assumption: the caller has acquired the lock to the profile list
1535  */
1536 static int
1537 ice_flow_rem_prof_sync(struct ice_hw *hw, enum ice_block blk,
1538 		       struct ice_flow_prof *prof)
1539 {
1540 	int status;
1541 
1542 	/* Remove all remaining flow entries before removing the flow profile */
1543 	if (!list_empty(&prof->entries)) {
1544 		struct ice_flow_entry *e, *t;
1545 
1546 		mutex_lock(&prof->entries_lock);
1547 
1548 		list_for_each_entry_safe(e, t, &prof->entries, l_entry) {
1549 			status = ice_flow_rem_entry_sync(hw, blk, e);
1550 			if (status)
1551 				break;
1552 		}
1553 
1554 		mutex_unlock(&prof->entries_lock);
1555 	}
1556 
1557 	/* Remove all hardware profiles associated with this flow profile */
1558 	status = ice_rem_prof(hw, blk, prof->id);
1559 	if (!status) {
1560 		clear_bit(prof->id, hw->blk[blk].prof_id.id);
1561 		list_del(&prof->l_entry);
1562 		mutex_destroy(&prof->entries_lock);
1563 		devm_kfree(ice_hw_to_dev(hw), prof);
1564 	}
1565 
1566 	return status;
1567 }
1568 
1569 /**
1570  * ice_flow_assoc_prof - associate a VSI with a flow profile
1571  * @hw: pointer to the hardware structure
1572  * @blk: classification stage
1573  * @prof: pointer to flow profile
1574  * @vsi_handle: software VSI handle
1575  *
1576  * Assumption: the caller has acquired the lock to the profile list
1577  * and the software VSI handle has been validated
1578  */
1579 static int
1580 ice_flow_assoc_prof(struct ice_hw *hw, enum ice_block blk,
1581 		    struct ice_flow_prof *prof, u16 vsi_handle)
1582 {
1583 	int status = 0;
1584 
1585 	if (!test_bit(vsi_handle, prof->vsis)) {
1586 		status = ice_add_prof_id_flow(hw, blk,
1587 					      ice_get_hw_vsi_num(hw,
1588 								 vsi_handle),
1589 					      prof->id);
1590 		if (!status)
1591 			set_bit(vsi_handle, prof->vsis);
1592 		else
1593 			ice_debug(hw, ICE_DBG_FLOW, "HW profile add failed, %d\n",
1594 				  status);
1595 	}
1596 
1597 	return status;
1598 }
1599 
1600 /**
1601  * ice_flow_disassoc_prof - disassociate a VSI from a flow profile
1602  * @hw: pointer to the hardware structure
1603  * @blk: classification stage
1604  * @prof: pointer to flow profile
1605  * @vsi_handle: software VSI handle
1606  *
1607  * Assumption: the caller has acquired the lock to the profile list
1608  * and the software VSI handle has been validated
1609  */
1610 static int
1611 ice_flow_disassoc_prof(struct ice_hw *hw, enum ice_block blk,
1612 		       struct ice_flow_prof *prof, u16 vsi_handle)
1613 {
1614 	int status = 0;
1615 
1616 	if (test_bit(vsi_handle, prof->vsis)) {
1617 		status = ice_rem_prof_id_flow(hw, blk,
1618 					      ice_get_hw_vsi_num(hw,
1619 								 vsi_handle),
1620 					      prof->id);
1621 		if (!status)
1622 			clear_bit(vsi_handle, prof->vsis);
1623 		else
1624 			ice_debug(hw, ICE_DBG_FLOW, "HW profile remove failed, %d\n",
1625 				  status);
1626 	}
1627 
1628 	return status;
1629 }
1630 
1631 #define FLAG_GTP_EH_PDU_LINK	BIT_ULL(13)
1632 #define FLAG_GTP_EH_PDU		BIT_ULL(14)
1633 
1634 #define HI_BYTE_IN_WORD		GENMASK(15, 8)
1635 #define LO_BYTE_IN_WORD		GENMASK(7, 0)
1636 
1637 #define FLAG_GTPU_MSK	\
1638 	(FLAG_GTP_EH_PDU | FLAG_GTP_EH_PDU_LINK)
1639 #define FLAG_GTPU_UP	\
1640 	(FLAG_GTP_EH_PDU | FLAG_GTP_EH_PDU_LINK)
1641 #define FLAG_GTPU_DW	FLAG_GTP_EH_PDU
1642 
1643 /**
1644  * ice_flow_set_parser_prof - Set flow profile based on the parsed profile info
1645  * @hw: pointer to the HW struct
1646  * @dest_vsi: dest VSI
1647  * @fdir_vsi: fdir programming VSI
1648  * @prof: stores parsed profile info from raw flow
1649  * @blk: classification blk
1650  *
1651  * Return: 0 on success or negative errno on failure.
1652  */
1653 int
1654 ice_flow_set_parser_prof(struct ice_hw *hw, u16 dest_vsi, u16 fdir_vsi,
1655 			 struct ice_parser_profile *prof, enum ice_block blk)
1656 {
1657 	u64 id = find_first_bit(prof->ptypes, ICE_FLOW_PTYPE_MAX);
1658 	struct ice_flow_prof_params *params __free(kfree);
1659 	u8 fv_words = hw->blk[blk].es.fvw;
1660 	int status;
1661 	int i, idx;
1662 
1663 	params = kzalloc_obj(*params);
1664 	if (!params)
1665 		return -ENOMEM;
1666 
1667 	for (i = 0; i < ICE_MAX_FV_WORDS; i++) {
1668 		params->es[i].prot_id = ICE_PROT_INVALID;
1669 		params->es[i].off = ICE_FV_OFFSET_INVAL;
1670 	}
1671 
1672 	for (i = 0; i < prof->fv_num; i++) {
1673 		if (hw->blk[blk].es.reverse)
1674 			idx = fv_words - i - 1;
1675 		else
1676 			idx = i;
1677 		params->es[idx].prot_id = prof->fv[i].proto_id;
1678 		params->es[idx].off = prof->fv[i].offset;
1679 		params->mask[idx] = (((prof->fv[i].msk) << BITS_PER_BYTE) &
1680 				      HI_BYTE_IN_WORD) |
1681 				    (((prof->fv[i].msk) >> BITS_PER_BYTE) &
1682 				      LO_BYTE_IN_WORD);
1683 	}
1684 
1685 	switch (prof->flags) {
1686 	case FLAG_GTPU_DW:
1687 		params->attr = ice_attr_gtpu_down;
1688 		params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_down);
1689 		break;
1690 	case FLAG_GTPU_UP:
1691 		params->attr = ice_attr_gtpu_up;
1692 		params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_up);
1693 		break;
1694 	default:
1695 		if (prof->flags_msk & FLAG_GTPU_MSK) {
1696 			params->attr = ice_attr_gtpu_session;
1697 			params->attr_cnt = ARRAY_SIZE(ice_attr_gtpu_session);
1698 		}
1699 		break;
1700 	}
1701 
1702 	status = ice_add_prof(hw, blk, id, prof->ptypes,
1703 			      params->attr, params->attr_cnt,
1704 			      params->es, params->mask, false, false);
1705 	if (status)
1706 		return status;
1707 
1708 	status = ice_flow_assoc_fdir_prof(hw, blk, dest_vsi, fdir_vsi, id);
1709 	if (status)
1710 		ice_rem_prof(hw, blk, id);
1711 
1712 	return status;
1713 }
1714 
1715 /**
1716  * ice_flow_add_prof - Add a flow profile for packet segments and matched fields
1717  * @hw: pointer to the HW struct
1718  * @blk: classification stage
1719  * @dir: flow direction
1720  * @segs: array of one or more packet segments that describe the flow
1721  * @segs_cnt: number of packet segments provided
1722  * @symm: symmetric setting for RSS profiles
1723  * @prof: stores the returned flow profile added
1724  */
1725 int
1726 ice_flow_add_prof(struct ice_hw *hw, enum ice_block blk, enum ice_flow_dir dir,
1727 		  struct ice_flow_seg_info *segs, u8 segs_cnt,
1728 		  bool symm, struct ice_flow_prof **prof)
1729 {
1730 	int status;
1731 
1732 	if (segs_cnt > ICE_FLOW_SEG_MAX)
1733 		return -ENOSPC;
1734 
1735 	if (!segs_cnt)
1736 		return -EINVAL;
1737 
1738 	if (!segs)
1739 		return -EINVAL;
1740 
1741 	status = ice_flow_val_hdrs(segs, segs_cnt);
1742 	if (status)
1743 		return status;
1744 
1745 	mutex_lock(&hw->fl_profs_locks[blk]);
1746 
1747 	status = ice_flow_add_prof_sync(hw, blk, dir, segs, segs_cnt,
1748 					symm, prof);
1749 	if (!status)
1750 		list_add(&(*prof)->l_entry, &hw->fl_profs[blk]);
1751 
1752 	mutex_unlock(&hw->fl_profs_locks[blk]);
1753 
1754 	return status;
1755 }
1756 
1757 /**
1758  * ice_flow_rem_prof - Remove a flow profile and all entries associated with it
1759  * @hw: pointer to the HW struct
1760  * @blk: the block for which the flow profile is to be removed
1761  * @prof_id: unique ID of the flow profile to be removed
1762  */
1763 int ice_flow_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 prof_id)
1764 {
1765 	struct ice_flow_prof *prof;
1766 	int status;
1767 
1768 	mutex_lock(&hw->fl_profs_locks[blk]);
1769 
1770 	prof = ice_flow_find_prof_id(hw, blk, prof_id);
1771 	if (!prof) {
1772 		status = -ENOENT;
1773 		goto out;
1774 	}
1775 
1776 	/* prof becomes invalid after the call */
1777 	status = ice_flow_rem_prof_sync(hw, blk, prof);
1778 
1779 out:
1780 	mutex_unlock(&hw->fl_profs_locks[blk]);
1781 
1782 	return status;
1783 }
1784 
1785 /**
1786  * ice_flow_add_entry - Add a flow entry
1787  * @hw: pointer to the HW struct
1788  * @blk: classification stage
1789  * @prof_id: ID of the profile to add a new flow entry to
1790  * @entry_id: unique ID to identify this flow entry
1791  * @vsi_handle: software VSI handle for the flow entry
1792  * @prio: priority of the flow entry
1793  * @data: pointer to a data buffer containing flow entry's match values/masks
1794  * @entry_h: pointer to buffer that receives the new flow entry's handle
1795  */
1796 int
1797 ice_flow_add_entry(struct ice_hw *hw, enum ice_block blk, u64 prof_id,
1798 		   u64 entry_id, u16 vsi_handle, enum ice_flow_priority prio,
1799 		   void *data, u64 *entry_h)
1800 {
1801 	struct ice_flow_entry *e = NULL;
1802 	struct ice_flow_prof *prof;
1803 	int status;
1804 
1805 	/* No flow entry data is expected for RSS */
1806 	if (!entry_h || (!data && blk != ICE_BLK_RSS))
1807 		return -EINVAL;
1808 
1809 	if (!ice_is_vsi_valid(hw, vsi_handle))
1810 		return -EINVAL;
1811 
1812 	mutex_lock(&hw->fl_profs_locks[blk]);
1813 
1814 	prof = ice_flow_find_prof_id(hw, blk, prof_id);
1815 	if (!prof) {
1816 		status = -ENOENT;
1817 	} else {
1818 		/* Allocate memory for the entry being added and associate
1819 		 * the VSI to the found flow profile
1820 		 */
1821 		e = devm_kzalloc_obj(ice_hw_to_dev(hw), *e);
1822 		if (!e)
1823 			status = -ENOMEM;
1824 		else
1825 			status = ice_flow_assoc_prof(hw, blk, prof, vsi_handle);
1826 	}
1827 
1828 	mutex_unlock(&hw->fl_profs_locks[blk]);
1829 	if (status)
1830 		goto out;
1831 
1832 	e->id = entry_id;
1833 	e->vsi_handle = vsi_handle;
1834 	e->prof = prof;
1835 	e->priority = prio;
1836 
1837 	switch (blk) {
1838 	case ICE_BLK_FD:
1839 	case ICE_BLK_RSS:
1840 		break;
1841 	default:
1842 		status = -EOPNOTSUPP;
1843 		goto out;
1844 	}
1845 
1846 	mutex_lock(&prof->entries_lock);
1847 	list_add(&e->l_entry, &prof->entries);
1848 	mutex_unlock(&prof->entries_lock);
1849 
1850 	*entry_h = ICE_FLOW_ENTRY_HNDL(e);
1851 
1852 out:
1853 	if (status)
1854 		devm_kfree(ice_hw_to_dev(hw), e);
1855 
1856 	return status;
1857 }
1858 
1859 /**
1860  * ice_flow_rem_entry - Remove a flow entry
1861  * @hw: pointer to the HW struct
1862  * @blk: classification stage
1863  * @entry_h: handle to the flow entry to be removed
1864  */
1865 int ice_flow_rem_entry(struct ice_hw *hw, enum ice_block blk, u64 entry_h)
1866 {
1867 	struct ice_flow_entry *entry;
1868 	struct ice_flow_prof *prof;
1869 	int status = 0;
1870 
1871 	if (entry_h == ICE_FLOW_ENTRY_HANDLE_INVAL)
1872 		return -EINVAL;
1873 
1874 	entry = ICE_FLOW_ENTRY_PTR(entry_h);
1875 
1876 	/* Retain the pointer to the flow profile as the entry will be freed */
1877 	prof = entry->prof;
1878 
1879 	if (prof) {
1880 		mutex_lock(&prof->entries_lock);
1881 		status = ice_flow_rem_entry_sync(hw, blk, entry);
1882 		mutex_unlock(&prof->entries_lock);
1883 	}
1884 
1885 	return status;
1886 }
1887 
1888 /**
1889  * ice_flow_set_fld_ext - specifies locations of field from entry's input buffer
1890  * @seg: packet segment the field being set belongs to
1891  * @fld: field to be set
1892  * @field_type: type of the field
1893  * @val_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of the value to match from
1894  *           entry's input buffer
1895  * @mask_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of mask value from entry's
1896  *            input buffer
1897  * @last_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of last/upper value from
1898  *            entry's input buffer
1899  *
1900  * This helper function stores information of a field being matched, including
1901  * the type of the field and the locations of the value to match, the mask, and
1902  * the upper-bound value in the start of the input buffer for a flow entry.
1903  * This function should only be used for fixed-size data structures.
1904  *
1905  * This function also opportunistically determines the protocol headers to be
1906  * present based on the fields being set. Some fields cannot be used alone to
1907  * determine the protocol headers present. Sometimes, fields for particular
1908  * protocol headers are not matched. In those cases, the protocol headers
1909  * must be explicitly set.
1910  */
1911 static void
1912 ice_flow_set_fld_ext(struct ice_flow_seg_info *seg, enum ice_flow_field fld,
1913 		     enum ice_flow_fld_match_type field_type, u16 val_loc,
1914 		     u16 mask_loc, u16 last_loc)
1915 {
1916 	u64 bit = BIT_ULL(fld);
1917 
1918 	seg->match |= bit;
1919 	if (field_type == ICE_FLOW_FLD_TYPE_RANGE)
1920 		seg->range |= bit;
1921 
1922 	seg->fields[fld].type = field_type;
1923 	seg->fields[fld].src.val = val_loc;
1924 	seg->fields[fld].src.mask = mask_loc;
1925 	seg->fields[fld].src.last = last_loc;
1926 
1927 	ICE_FLOW_SET_HDRS(seg, ice_flds_info[fld].hdr);
1928 }
1929 
1930 /**
1931  * ice_flow_set_fld - specifies locations of field from entry's input buffer
1932  * @seg: packet segment the field being set belongs to
1933  * @fld: field to be set
1934  * @val_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of the value to match from
1935  *           entry's input buffer
1936  * @mask_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of mask value from entry's
1937  *            input buffer
1938  * @last_loc: if not ICE_FLOW_FLD_OFF_INVAL, location of last/upper value from
1939  *            entry's input buffer
1940  * @range: indicate if field being matched is to be in a range
1941  *
1942  * This function specifies the locations, in the form of byte offsets from the
1943  * start of the input buffer for a flow entry, from where the value to match,
1944  * the mask value, and upper value can be extracted. These locations are then
1945  * stored in the flow profile. When adding a flow entry associated with the
1946  * flow profile, these locations will be used to quickly extract the values and
1947  * create the content of a match entry. This function should only be used for
1948  * fixed-size data structures.
1949  */
1950 void
1951 ice_flow_set_fld(struct ice_flow_seg_info *seg, enum ice_flow_field fld,
1952 		 u16 val_loc, u16 mask_loc, u16 last_loc, bool range)
1953 {
1954 	enum ice_flow_fld_match_type t = range ?
1955 		ICE_FLOW_FLD_TYPE_RANGE : ICE_FLOW_FLD_TYPE_REG;
1956 
1957 	ice_flow_set_fld_ext(seg, fld, t, val_loc, mask_loc, last_loc);
1958 }
1959 
1960 /**
1961  * ice_flow_add_fld_raw - sets locations of a raw field from entry's input buf
1962  * @seg: packet segment the field being set belongs to
1963  * @off: offset of the raw field from the beginning of the segment in bytes
1964  * @len: length of the raw pattern to be matched
1965  * @val_loc: location of the value to match from entry's input buffer
1966  * @mask_loc: location of mask value from entry's input buffer
1967  *
1968  * This function specifies the offset of the raw field to be match from the
1969  * beginning of the specified packet segment, and the locations, in the form of
1970  * byte offsets from the start of the input buffer for a flow entry, from where
1971  * the value to match and the mask value to be extracted. These locations are
1972  * then stored in the flow profile. When adding flow entries to the associated
1973  * flow profile, these locations can be used to quickly extract the values to
1974  * create the content of a match entry. This function should only be used for
1975  * fixed-size data structures.
1976  */
1977 void
1978 ice_flow_add_fld_raw(struct ice_flow_seg_info *seg, u16 off, u8 len,
1979 		     u16 val_loc, u16 mask_loc)
1980 {
1981 	if (seg->raws_cnt < ICE_FLOW_SEG_RAW_FLD_MAX) {
1982 		seg->raws[seg->raws_cnt].off = off;
1983 		seg->raws[seg->raws_cnt].info.type = ICE_FLOW_FLD_TYPE_SIZE;
1984 		seg->raws[seg->raws_cnt].info.src.val = val_loc;
1985 		seg->raws[seg->raws_cnt].info.src.mask = mask_loc;
1986 		/* The "last" field is used to store the length of the field */
1987 		seg->raws[seg->raws_cnt].info.src.last = len;
1988 	}
1989 
1990 	/* Overflows of "raws" will be handled as an error condition later in
1991 	 * the flow when this information is processed.
1992 	 */
1993 	seg->raws_cnt++;
1994 }
1995 
1996 /**
1997  * ice_flow_rem_vsi_prof - remove VSI from flow profile
1998  * @hw: pointer to the hardware structure
1999  * @vsi_handle: software VSI handle
2000  * @prof_id: unique ID to identify this flow profile
2001  *
2002  * This function removes the flow entries associated to the input
2003  * VSI handle and disassociate the VSI from the flow profile.
2004  */
2005 int ice_flow_rem_vsi_prof(struct ice_hw *hw, u16 vsi_handle, u64 prof_id)
2006 {
2007 	struct ice_flow_prof *prof;
2008 	int status = 0;
2009 
2010 	if (!ice_is_vsi_valid(hw, vsi_handle))
2011 		return -EINVAL;
2012 
2013 	/* find flow profile pointer with input package block and profile ID */
2014 	prof = ice_flow_find_prof_id(hw, ICE_BLK_FD, prof_id);
2015 	if (!prof) {
2016 		ice_debug(hw, ICE_DBG_PKG, "Cannot find flow profile id=%llu\n",
2017 			  prof_id);
2018 		return -ENOENT;
2019 	}
2020 
2021 	/* Remove all remaining flow entries before removing the flow profile */
2022 	if (!list_empty(&prof->entries)) {
2023 		struct ice_flow_entry *e, *t;
2024 
2025 		mutex_lock(&prof->entries_lock);
2026 		list_for_each_entry_safe(e, t, &prof->entries, l_entry) {
2027 			if (e->vsi_handle != vsi_handle)
2028 				continue;
2029 
2030 			status = ice_flow_rem_entry_sync(hw, ICE_BLK_FD, e);
2031 			if (status)
2032 				break;
2033 		}
2034 		mutex_unlock(&prof->entries_lock);
2035 	}
2036 	if (status)
2037 		return status;
2038 
2039 	/* disassociate the flow profile from sw VSI handle */
2040 	status = ice_flow_disassoc_prof(hw, ICE_BLK_FD, prof, vsi_handle);
2041 	if (status)
2042 		ice_debug(hw, ICE_DBG_PKG, "ice_flow_disassoc_prof() failed with status=%d\n",
2043 			  status);
2044 	return status;
2045 }
2046 
2047 #define ICE_FLOW_RSS_SEG_HDR_L2_MASKS \
2048 	(ICE_FLOW_SEG_HDR_ETH | ICE_FLOW_SEG_HDR_VLAN)
2049 
2050 #define ICE_FLOW_RSS_SEG_HDR_L3_MASKS \
2051 	(ICE_FLOW_SEG_HDR_IPV4 | ICE_FLOW_SEG_HDR_IPV6)
2052 
2053 #define ICE_FLOW_RSS_SEG_HDR_L4_MASKS \
2054 	(ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_SCTP)
2055 
2056 #define ICE_FLOW_RSS_SEG_HDR_VAL_MASKS \
2057 	(ICE_FLOW_RSS_SEG_HDR_L2_MASKS | \
2058 	 ICE_FLOW_RSS_SEG_HDR_L3_MASKS | \
2059 	 ICE_FLOW_RSS_SEG_HDR_L4_MASKS)
2060 
2061 /**
2062  * ice_flow_set_rss_seg_info - setup packet segments for RSS
2063  * @segs: pointer to the flow field segment(s)
2064  * @seg_cnt: segment count
2065  * @cfg: configure parameters
2066  *
2067  * Helper function to extract fields from hash bitmap and use flow
2068  * header value to set flow field segment for further use in flow
2069  * profile entry or removal.
2070  */
2071 static int
2072 ice_flow_set_rss_seg_info(struct ice_flow_seg_info *segs, u8 seg_cnt,
2073 			  const struct ice_rss_hash_cfg *cfg)
2074 {
2075 	struct ice_flow_seg_info *seg;
2076 	u64 val;
2077 	u16 i;
2078 
2079 	/* set inner most segment */
2080 	seg = &segs[seg_cnt - 1];
2081 
2082 	for_each_set_bit(i, (const unsigned long *)&cfg->hash_flds,
2083 			 (u16)ICE_FLOW_FIELD_IDX_MAX)
2084 		ice_flow_set_fld(seg, (enum ice_flow_field)i,
2085 				 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
2086 				 ICE_FLOW_FLD_OFF_INVAL, false);
2087 
2088 	ICE_FLOW_SET_HDRS(seg, cfg->addl_hdrs);
2089 
2090 	/* set outer most header */
2091 	if (cfg->hdr_type == ICE_RSS_INNER_HEADERS_W_OUTER_IPV4)
2092 		segs[ICE_RSS_OUTER_HEADERS].hdrs |= ICE_FLOW_SEG_HDR_IPV4 |
2093 						    ICE_FLOW_SEG_HDR_IPV_OTHER;
2094 	else if (cfg->hdr_type == ICE_RSS_INNER_HEADERS_W_OUTER_IPV6)
2095 		segs[ICE_RSS_OUTER_HEADERS].hdrs |= ICE_FLOW_SEG_HDR_IPV6 |
2096 						    ICE_FLOW_SEG_HDR_IPV_OTHER;
2097 
2098 	if (seg->hdrs & ~ICE_FLOW_RSS_SEG_HDR_VAL_MASKS &
2099 	    ~ICE_FLOW_RSS_HDRS_INNER_MASK & ~ICE_FLOW_SEG_HDR_IPV_OTHER)
2100 		return -EINVAL;
2101 
2102 	val = (u64)(seg->hdrs & ICE_FLOW_RSS_SEG_HDR_L3_MASKS);
2103 	if (val && !is_power_of_2(val))
2104 		return -EIO;
2105 
2106 	val = (u64)(seg->hdrs & ICE_FLOW_RSS_SEG_HDR_L4_MASKS);
2107 	if (val && !is_power_of_2(val))
2108 		return -EIO;
2109 
2110 	return 0;
2111 }
2112 
2113 /**
2114  * ice_rem_vsi_rss_list - remove VSI from RSS list
2115  * @hw: pointer to the hardware structure
2116  * @vsi_handle: software VSI handle
2117  *
2118  * Remove the VSI from all RSS configurations in the list.
2119  */
2120 void ice_rem_vsi_rss_list(struct ice_hw *hw, u16 vsi_handle)
2121 {
2122 	struct ice_rss_cfg *r, *tmp;
2123 
2124 	if (list_empty(&hw->rss_list_head))
2125 		return;
2126 
2127 	mutex_lock(&hw->rss_locks);
2128 	list_for_each_entry_safe(r, tmp, &hw->rss_list_head, l_entry)
2129 		if (test_and_clear_bit(vsi_handle, r->vsis))
2130 			if (bitmap_empty(r->vsis, ICE_MAX_VSI)) {
2131 				list_del(&r->l_entry);
2132 				devm_kfree(ice_hw_to_dev(hw), r);
2133 			}
2134 	mutex_unlock(&hw->rss_locks);
2135 }
2136 
2137 /**
2138  * ice_rem_vsi_rss_cfg - remove RSS configurations associated with VSI
2139  * @hw: pointer to the hardware structure
2140  * @vsi_handle: software VSI handle
2141  *
2142  * This function will iterate through all flow profiles and disassociate
2143  * the VSI from that profile. If the flow profile has no VSIs it will
2144  * be removed.
2145  */
2146 int ice_rem_vsi_rss_cfg(struct ice_hw *hw, u16 vsi_handle)
2147 {
2148 	const enum ice_block blk = ICE_BLK_RSS;
2149 	struct ice_flow_prof *p, *t;
2150 	int status = 0;
2151 
2152 	if (!ice_is_vsi_valid(hw, vsi_handle))
2153 		return -EINVAL;
2154 
2155 	if (list_empty(&hw->fl_profs[blk]))
2156 		return 0;
2157 
2158 	mutex_lock(&hw->rss_locks);
2159 	list_for_each_entry_safe(p, t, &hw->fl_profs[blk], l_entry)
2160 		if (test_bit(vsi_handle, p->vsis)) {
2161 			status = ice_flow_disassoc_prof(hw, blk, p, vsi_handle);
2162 			if (status)
2163 				break;
2164 
2165 			if (bitmap_empty(p->vsis, ICE_MAX_VSI)) {
2166 				status = ice_flow_rem_prof(hw, blk, p->id);
2167 				if (status)
2168 					break;
2169 			}
2170 		}
2171 	mutex_unlock(&hw->rss_locks);
2172 
2173 	return status;
2174 }
2175 
2176 /**
2177  * ice_get_rss_hdr_type - get a RSS profile's header type
2178  * @prof: RSS flow profile
2179  */
2180 static enum ice_rss_cfg_hdr_type
2181 ice_get_rss_hdr_type(struct ice_flow_prof *prof)
2182 {
2183 	if (prof->segs_cnt == ICE_FLOW_SEG_SINGLE) {
2184 		return ICE_RSS_OUTER_HEADERS;
2185 	} else if (prof->segs_cnt == ICE_FLOW_SEG_MAX) {
2186 		const struct ice_flow_seg_info *s;
2187 
2188 		s = &prof->segs[ICE_RSS_OUTER_HEADERS];
2189 		if (s->hdrs == ICE_FLOW_SEG_HDR_NONE)
2190 			return ICE_RSS_INNER_HEADERS;
2191 		if (s->hdrs & ICE_FLOW_SEG_HDR_IPV4)
2192 			return ICE_RSS_INNER_HEADERS_W_OUTER_IPV4;
2193 		if (s->hdrs & ICE_FLOW_SEG_HDR_IPV6)
2194 			return ICE_RSS_INNER_HEADERS_W_OUTER_IPV6;
2195 	}
2196 
2197 	return ICE_RSS_ANY_HEADERS;
2198 }
2199 
2200 static bool
2201 ice_rss_match_prof(struct ice_rss_cfg *r, struct ice_flow_prof *prof,
2202 		   enum ice_rss_cfg_hdr_type hdr_type)
2203 {
2204 	return (r->hash.hdr_type == hdr_type &&
2205 		r->hash.hash_flds == prof->segs[prof->segs_cnt - 1].match &&
2206 		r->hash.addl_hdrs == prof->segs[prof->segs_cnt - 1].hdrs);
2207 }
2208 
2209 /**
2210  * ice_rem_rss_list - remove RSS configuration from list
2211  * @hw: pointer to the hardware structure
2212  * @vsi_handle: software VSI handle
2213  * @prof: pointer to flow profile
2214  *
2215  * Assumption: lock has already been acquired for RSS list
2216  */
2217 static void
2218 ice_rem_rss_list(struct ice_hw *hw, u16 vsi_handle, struct ice_flow_prof *prof)
2219 {
2220 	enum ice_rss_cfg_hdr_type hdr_type;
2221 	struct ice_rss_cfg *r, *tmp;
2222 
2223 	/* Search for RSS hash fields associated to the VSI that match the
2224 	 * hash configurations associated to the flow profile. If found
2225 	 * remove from the RSS entry list of the VSI context and delete entry.
2226 	 */
2227 	hdr_type = ice_get_rss_hdr_type(prof);
2228 	list_for_each_entry_safe(r, tmp, &hw->rss_list_head, l_entry)
2229 		if (ice_rss_match_prof(r, prof, hdr_type)) {
2230 			clear_bit(vsi_handle, r->vsis);
2231 			if (bitmap_empty(r->vsis, ICE_MAX_VSI)) {
2232 				list_del(&r->l_entry);
2233 				devm_kfree(ice_hw_to_dev(hw), r);
2234 			}
2235 			return;
2236 		}
2237 }
2238 
2239 /**
2240  * ice_add_rss_list - add RSS configuration to list
2241  * @hw: pointer to the hardware structure
2242  * @vsi_handle: software VSI handle
2243  * @prof: pointer to flow profile
2244  *
2245  * Assumption: lock has already been acquired for RSS list
2246  */
2247 static int
2248 ice_add_rss_list(struct ice_hw *hw, u16 vsi_handle, struct ice_flow_prof *prof)
2249 {
2250 	enum ice_rss_cfg_hdr_type hdr_type;
2251 	struct ice_rss_cfg *r, *rss_cfg;
2252 
2253 	hdr_type = ice_get_rss_hdr_type(prof);
2254 	list_for_each_entry(r, &hw->rss_list_head, l_entry)
2255 		if (ice_rss_match_prof(r, prof, hdr_type)) {
2256 			set_bit(vsi_handle, r->vsis);
2257 			return 0;
2258 		}
2259 
2260 	rss_cfg = devm_kzalloc_obj(ice_hw_to_dev(hw), *rss_cfg);
2261 	if (!rss_cfg)
2262 		return -ENOMEM;
2263 
2264 	rss_cfg->hash.hash_flds = prof->segs[prof->segs_cnt - 1].match;
2265 	rss_cfg->hash.addl_hdrs = prof->segs[prof->segs_cnt - 1].hdrs;
2266 	rss_cfg->hash.hdr_type = hdr_type;
2267 	rss_cfg->hash.symm = prof->symm;
2268 	set_bit(vsi_handle, rss_cfg->vsis);
2269 
2270 	list_add_tail(&rss_cfg->l_entry, &hw->rss_list_head);
2271 
2272 	return 0;
2273 }
2274 
2275 /**
2276  * ice_rss_config_xor_word - set the HSYMM registers for one input set word
2277  * @hw: pointer to the hardware structure
2278  * @prof_id: RSS hardware profile id
2279  * @src: the FV index used by the protocol's source field
2280  * @dst: the FV index used by the protocol's destination field
2281  *
2282  * Write to the HSYMM register with the index of @src FV the value of the @dst
2283  * FV index. This will tell the hardware to XOR HSYMM[src] with INSET[dst]
2284  * while calculating the RSS input set.
2285  */
2286 static void
2287 ice_rss_config_xor_word(struct ice_hw *hw, u8 prof_id, u8 src, u8 dst)
2288 {
2289 	u32 val, reg, bits_shift;
2290 	u8 reg_idx;
2291 
2292 	reg_idx = src / GLQF_HSYMM_REG_SIZE;
2293 	bits_shift = ((src % GLQF_HSYMM_REG_SIZE) << 3);
2294 	val = dst | GLQF_HSYMM_ENABLE_BIT;
2295 
2296 	reg = rd32(hw, GLQF_HSYMM(prof_id, reg_idx));
2297 	reg = (reg & ~(0xff << bits_shift)) | (val << bits_shift);
2298 	wr32(hw, GLQF_HSYMM(prof_id, reg_idx), reg);
2299 }
2300 
2301 /**
2302  * ice_rss_config_xor - set the symmetric registers for a profile's protocol
2303  * @hw: pointer to the hardware structure
2304  * @prof_id: RSS hardware profile id
2305  * @src: the FV index used by the protocol's source field
2306  * @dst: the FV index used by the protocol's destination field
2307  * @len: length of the source/destination fields in words
2308  */
2309 static void
2310 ice_rss_config_xor(struct ice_hw *hw, u8 prof_id, u8 src, u8 dst, u8 len)
2311 {
2312 	int fv_last_word =
2313 		ICE_FLOW_SW_FIELD_VECTOR_MAX / ICE_FLOW_FV_EXTRACT_SZ - 1;
2314 	int i;
2315 
2316 	for (i = 0; i < len; i++) {
2317 		ice_rss_config_xor_word(hw, prof_id,
2318 					/* Yes, field vector in GLQF_HSYMM and
2319 					 * GLQF_HINSET is inversed!
2320 					 */
2321 					fv_last_word - (src + i),
2322 					fv_last_word - (dst + i));
2323 		ice_rss_config_xor_word(hw, prof_id,
2324 					fv_last_word - (dst + i),
2325 					fv_last_word - (src + i));
2326 	}
2327 }
2328 
2329 /**
2330  * ice_rss_set_symm - set the symmetric settings for an RSS profile
2331  * @hw: pointer to the hardware structure
2332  * @prof: pointer to flow profile
2333  *
2334  * The symmetric hash will result from XORing the protocol's fields with
2335  * indexes in GLQF_HSYMM and GLQF_HINSET. This function configures the profile's
2336  * GLQF_HSYMM registers.
2337  */
2338 static void ice_rss_set_symm(struct ice_hw *hw, struct ice_flow_prof *prof)
2339 {
2340 	struct ice_prof_map *map;
2341 	u8 prof_id, m;
2342 
2343 	mutex_lock(&hw->blk[ICE_BLK_RSS].es.prof_map_lock);
2344 	map = ice_search_prof_id(hw, ICE_BLK_RSS, prof->id);
2345 	if (map)
2346 		prof_id = map->prof_id;
2347 	mutex_unlock(&hw->blk[ICE_BLK_RSS].es.prof_map_lock);
2348 
2349 	if (!map)
2350 		return;
2351 
2352 	/* clear to default */
2353 	for (m = 0; m < GLQF_HSYMM_REG_PER_PROF; m++)
2354 		wr32(hw, GLQF_HSYMM(prof_id, m), 0);
2355 
2356 	if (prof->symm) {
2357 		struct ice_flow_seg_xtrct *ipv4_src, *ipv4_dst;
2358 		struct ice_flow_seg_xtrct *ipv6_src, *ipv6_dst;
2359 		struct ice_flow_seg_xtrct *sctp_src, *sctp_dst;
2360 		struct ice_flow_seg_xtrct *tcp_src, *tcp_dst;
2361 		struct ice_flow_seg_xtrct *udp_src, *udp_dst;
2362 		struct ice_flow_seg_info *seg;
2363 
2364 		seg = &prof->segs[prof->segs_cnt - 1];
2365 
2366 		ipv4_src = &seg->fields[ICE_FLOW_FIELD_IDX_IPV4_SA].xtrct;
2367 		ipv4_dst = &seg->fields[ICE_FLOW_FIELD_IDX_IPV4_DA].xtrct;
2368 
2369 		ipv6_src = &seg->fields[ICE_FLOW_FIELD_IDX_IPV6_SA].xtrct;
2370 		ipv6_dst = &seg->fields[ICE_FLOW_FIELD_IDX_IPV6_DA].xtrct;
2371 
2372 		tcp_src = &seg->fields[ICE_FLOW_FIELD_IDX_TCP_SRC_PORT].xtrct;
2373 		tcp_dst = &seg->fields[ICE_FLOW_FIELD_IDX_TCP_DST_PORT].xtrct;
2374 
2375 		udp_src = &seg->fields[ICE_FLOW_FIELD_IDX_UDP_SRC_PORT].xtrct;
2376 		udp_dst = &seg->fields[ICE_FLOW_FIELD_IDX_UDP_DST_PORT].xtrct;
2377 
2378 		sctp_src = &seg->fields[ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT].xtrct;
2379 		sctp_dst = &seg->fields[ICE_FLOW_FIELD_IDX_SCTP_DST_PORT].xtrct;
2380 
2381 		/* xor IPv4 */
2382 		if (ipv4_src->prot_id != 0 && ipv4_dst->prot_id != 0)
2383 			ice_rss_config_xor(hw, prof_id,
2384 					   ipv4_src->idx, ipv4_dst->idx, 2);
2385 
2386 		/* xor IPv6 */
2387 		if (ipv6_src->prot_id != 0 && ipv6_dst->prot_id != 0)
2388 			ice_rss_config_xor(hw, prof_id,
2389 					   ipv6_src->idx, ipv6_dst->idx, 8);
2390 
2391 		/* xor TCP */
2392 		if (tcp_src->prot_id != 0 && tcp_dst->prot_id != 0)
2393 			ice_rss_config_xor(hw, prof_id,
2394 					   tcp_src->idx, tcp_dst->idx, 1);
2395 
2396 		/* xor UDP */
2397 		if (udp_src->prot_id != 0 && udp_dst->prot_id != 0)
2398 			ice_rss_config_xor(hw, prof_id,
2399 					   udp_src->idx, udp_dst->idx, 1);
2400 
2401 		/* xor SCTP */
2402 		if (sctp_src->prot_id != 0 && sctp_dst->prot_id != 0)
2403 			ice_rss_config_xor(hw, prof_id,
2404 					   sctp_src->idx, sctp_dst->idx, 1);
2405 	}
2406 }
2407 
2408 /**
2409  * ice_rss_cfg_raw_symm - Configure symmetric RSS for a raw parser profile
2410  * @hw:      device HW
2411  * @prof:    parser profile describing extracted FV (field vector) entries
2412  * @prof_id: RSS profile identifier used to program symmetry registers
2413  *
2414  * The routine scans the parser profile's FV entries and looks for
2415  * direction-sensitive pairs (L3 src/dst, L4 src/dst). When a pair is found,
2416  * it programs XOR-based symmetry so that flows hash identically regardless
2417  * of packet direction. This preserves CPU affinity for the same 5-tuple.
2418  *
2419  * Notes:
2420  * - The size of each logical field (IPv4/IPv6 address, L4 port) is expressed
2421  *   in units of ICE_FLOW_FV_EXTRACT_SZ so we can step across fv[] correctly.
2422  * - We guard against out-of-bounds access before looking at fv[i + len].
2423  */
2424 static void ice_rss_cfg_raw_symm(struct ice_hw *hw,
2425 				 const struct ice_parser_profile *prof,
2426 				 u64 prof_id)
2427 {
2428 	for (size_t i = 0; i < prof->fv_num; i++) {
2429 		u8 proto_id = prof->fv[i].proto_id;
2430 		u16 src_off = 0, dst_off = 0;
2431 		size_t src_idx, dst_idx;
2432 		bool is_matched = false;
2433 		unsigned int len = 0;
2434 
2435 		switch (proto_id) {
2436 		/* IPv4 address pairs (outer/inner variants) */
2437 		case ICE_PROT_IPV4_OF_OR_S:
2438 		case ICE_PROT_IPV4_IL:
2439 		case ICE_PROT_IPV4_IL_IL:
2440 			len = ICE_FLOW_FLD_SZ_IPV4_ADDR /
2441 			      ICE_FLOW_FV_EXTRACT_SZ;
2442 			src_off = ICE_FLOW_FIELD_IPV4_SRC_OFFSET;
2443 			dst_off = ICE_FLOW_FIELD_IPV4_DST_OFFSET;
2444 			break;
2445 
2446 		/* IPv6 address pairs (outer/inner variants) */
2447 		case ICE_PROT_IPV6_OF_OR_S:
2448 		case ICE_PROT_IPV6_IL:
2449 		case ICE_PROT_IPV6_IL_IL:
2450 			len = ICE_FLOW_FLD_SZ_IPV6_ADDR /
2451 			      ICE_FLOW_FV_EXTRACT_SZ;
2452 			src_off = ICE_FLOW_FIELD_IPV6_SRC_OFFSET;
2453 			dst_off = ICE_FLOW_FIELD_IPV6_DST_OFFSET;
2454 			break;
2455 
2456 		/* L4 port pairs (TCP/UDP/SCTP) */
2457 		case ICE_PROT_TCP_IL:
2458 		case ICE_PROT_UDP_IL_OR_S:
2459 		case ICE_PROT_SCTP_IL:
2460 			len = ICE_FLOW_FLD_SZ_PORT / ICE_FLOW_FV_EXTRACT_SZ;
2461 			src_off = ICE_FLOW_FIELD_SRC_PORT_OFFSET;
2462 			dst_off = ICE_FLOW_FIELD_DST_PORT_OFFSET;
2463 			break;
2464 
2465 		default:
2466 			continue;
2467 		}
2468 
2469 		/* Bounds check before accessing fv[i + len]. */
2470 		if (i + len >= prof->fv_num)
2471 			continue;
2472 
2473 		/* Verify src/dst pairing for this protocol id. */
2474 		is_matched = prof->fv[i].offset == src_off &&
2475 			     prof->fv[i + len].proto_id == proto_id &&
2476 			     prof->fv[i + len].offset == dst_off;
2477 		if (!is_matched)
2478 			continue;
2479 
2480 		/* Program XOR symmetry for this field pair. */
2481 		src_idx = i;
2482 		dst_idx = i + len;
2483 
2484 		ice_rss_config_xor(hw, prof_id, src_idx, dst_idx, len);
2485 
2486 		/* Skip over the pair we just handled; the loop's ++i advances
2487 		 * one more element, hence the --i after the jump.
2488 		 */
2489 		i += (2 * len);
2490 		/* not strictly needed; keeps static analyzers happy */
2491 		if (i == 0)
2492 			break;
2493 		--i;
2494 	}
2495 }
2496 
2497 /* Max registers index per packet profile */
2498 #define ICE_SYMM_REG_INDEX_MAX 6
2499 
2500 /**
2501  * ice_rss_update_raw_symm - update symmetric hash configuration
2502  * for raw pattern
2503  * @hw: pointer to the hardware structure
2504  * @cfg: configure parameters for raw pattern
2505  * @id: profile tracking ID
2506  *
2507  * Update symmetric hash configuration for raw pattern if required.
2508  * Otherwise only clear to default.
2509  */
2510 void
2511 ice_rss_update_raw_symm(struct ice_hw *hw,
2512 			struct ice_rss_raw_cfg *cfg, u64 id)
2513 {
2514 	struct ice_prof_map *map;
2515 	u8 prof_id, m;
2516 
2517 	mutex_lock(&hw->blk[ICE_BLK_RSS].es.prof_map_lock);
2518 	map = ice_search_prof_id(hw, ICE_BLK_RSS, id);
2519 	if (map)
2520 		prof_id = map->prof_id;
2521 	mutex_unlock(&hw->blk[ICE_BLK_RSS].es.prof_map_lock);
2522 	if (!map)
2523 		return;
2524 	/* clear to default */
2525 	for (m = 0; m < ICE_SYMM_REG_INDEX_MAX; m++)
2526 		wr32(hw, GLQF_HSYMM(prof_id, m), 0);
2527 
2528 	if (cfg->symm)
2529 		ice_rss_cfg_raw_symm(hw, &cfg->prof, prof_id);
2530 }
2531 
2532 /**
2533  * ice_add_rss_cfg_sync - add an RSS configuration
2534  * @hw: pointer to the hardware structure
2535  * @vsi_handle: software VSI handle
2536  * @cfg: configure parameters
2537  *
2538  * Assumption: lock has already been acquired for RSS list
2539  */
2540 static int
2541 ice_add_rss_cfg_sync(struct ice_hw *hw, u16 vsi_handle,
2542 		     const struct ice_rss_hash_cfg *cfg)
2543 {
2544 	const enum ice_block blk = ICE_BLK_RSS;
2545 	struct ice_flow_prof *prof = NULL;
2546 	struct ice_flow_seg_info *segs;
2547 	u8 segs_cnt;
2548 	int status;
2549 
2550 	segs_cnt = (cfg->hdr_type == ICE_RSS_OUTER_HEADERS) ?
2551 			ICE_FLOW_SEG_SINGLE : ICE_FLOW_SEG_MAX;
2552 
2553 	segs = kzalloc_objs(*segs, segs_cnt);
2554 	if (!segs)
2555 		return -ENOMEM;
2556 
2557 	/* Construct the packet segment info from the hashed fields */
2558 	status = ice_flow_set_rss_seg_info(segs, segs_cnt, cfg);
2559 	if (status)
2560 		goto exit;
2561 
2562 	/* Search for a flow profile that has matching headers, hash fields,
2563 	 * symm and has the input VSI associated to it. If found, no further
2564 	 * operations required and exit.
2565 	 */
2566 	prof = ice_flow_find_prof_conds(hw, blk, ICE_FLOW_RX, segs, segs_cnt,
2567 					cfg->symm, vsi_handle,
2568 					ICE_FLOW_FIND_PROF_CHK_FLDS |
2569 					ICE_FLOW_FIND_PROF_CHK_SYMM |
2570 					ICE_FLOW_FIND_PROF_CHK_VSI);
2571 	if (prof)
2572 		goto exit;
2573 
2574 	/* Check if a flow profile exists with the same protocol headers and
2575 	 * associated with the input VSI. If so disassociate the VSI from
2576 	 * this profile. The VSI will be added to a new profile created with
2577 	 * the protocol header and new hash field configuration.
2578 	 */
2579 	prof = ice_flow_find_prof_conds(hw, blk, ICE_FLOW_RX, segs, segs_cnt,
2580 					cfg->symm, vsi_handle,
2581 					ICE_FLOW_FIND_PROF_CHK_VSI);
2582 	if (prof) {
2583 		status = ice_flow_disassoc_prof(hw, blk, prof, vsi_handle);
2584 		if (!status)
2585 			ice_rem_rss_list(hw, vsi_handle, prof);
2586 		else
2587 			goto exit;
2588 
2589 		/* Remove profile if it has no VSIs associated */
2590 		if (bitmap_empty(prof->vsis, ICE_MAX_VSI)) {
2591 			status = ice_flow_rem_prof(hw, blk, prof->id);
2592 			if (status)
2593 				goto exit;
2594 		}
2595 	}
2596 
2597 	/* Search for a profile that has the same match fields and symmetric
2598 	 * setting. If this exists then associate the VSI to this profile.
2599 	 */
2600 	prof = ice_flow_find_prof_conds(hw, blk, ICE_FLOW_RX, segs, segs_cnt,
2601 					cfg->symm, vsi_handle,
2602 					ICE_FLOW_FIND_PROF_CHK_SYMM |
2603 					ICE_FLOW_FIND_PROF_CHK_FLDS);
2604 	if (prof) {
2605 		status = ice_flow_assoc_prof(hw, blk, prof, vsi_handle);
2606 		if (!status)
2607 			status = ice_add_rss_list(hw, vsi_handle, prof);
2608 		goto exit;
2609 	}
2610 
2611 	/* Create a new flow profile with packet segment information. */
2612 	status = ice_flow_add_prof(hw, blk, ICE_FLOW_RX,
2613 				   segs, segs_cnt, cfg->symm, &prof);
2614 	if (status)
2615 		goto exit;
2616 
2617 	prof->symm = cfg->symm;
2618 	ice_rss_set_symm(hw, prof);
2619 	status = ice_flow_assoc_prof(hw, blk, prof, vsi_handle);
2620 	/* If association to a new flow profile failed then this profile can
2621 	 * be removed.
2622 	 */
2623 	if (status) {
2624 		ice_flow_rem_prof(hw, blk, prof->id);
2625 		goto exit;
2626 	}
2627 
2628 	status = ice_add_rss_list(hw, vsi_handle, prof);
2629 
2630 exit:
2631 	kfree(segs);
2632 	return status;
2633 }
2634 
2635 /**
2636  * ice_add_rss_cfg - add an RSS configuration with specified hashed fields
2637  * @hw: pointer to the hardware structure
2638  * @vsi: VSI to add the RSS configuration to
2639  * @cfg: configure parameters
2640  *
2641  * This function will generate a flow profile based on fields associated with
2642  * the input fields to hash on, the flow type and use the VSI number to add
2643  * a flow entry to the profile.
2644  */
2645 int
2646 ice_add_rss_cfg(struct ice_hw *hw, struct ice_vsi *vsi,
2647 		const struct ice_rss_hash_cfg *cfg)
2648 {
2649 	struct ice_rss_hash_cfg local_cfg;
2650 	u16 vsi_handle;
2651 	int status;
2652 
2653 	if (!vsi)
2654 		return -EINVAL;
2655 
2656 	vsi_handle = vsi->idx;
2657 	if (!ice_is_vsi_valid(hw, vsi_handle) ||
2658 	    !cfg || cfg->hdr_type > ICE_RSS_ANY_HEADERS ||
2659 	    cfg->hash_flds == ICE_HASH_INVALID)
2660 		return -EINVAL;
2661 
2662 	mutex_lock(&hw->rss_locks);
2663 	local_cfg = *cfg;
2664 	if (cfg->hdr_type < ICE_RSS_ANY_HEADERS) {
2665 		status = ice_add_rss_cfg_sync(hw, vsi_handle, &local_cfg);
2666 	} else {
2667 		local_cfg.hdr_type = ICE_RSS_OUTER_HEADERS;
2668 		status = ice_add_rss_cfg_sync(hw, vsi_handle, &local_cfg);
2669 		if (!status) {
2670 			local_cfg.hdr_type = ICE_RSS_INNER_HEADERS;
2671 			status = ice_add_rss_cfg_sync(hw, vsi_handle,
2672 						      &local_cfg);
2673 		}
2674 	}
2675 	mutex_unlock(&hw->rss_locks);
2676 
2677 	return status;
2678 }
2679 
2680 /**
2681  * ice_rem_rss_cfg_sync - remove an existing RSS configuration
2682  * @hw: pointer to the hardware structure
2683  * @vsi_handle: software VSI handle
2684  * @cfg: configure parameters
2685  *
2686  * Assumption: lock has already been acquired for RSS list
2687  */
2688 static int
2689 ice_rem_rss_cfg_sync(struct ice_hw *hw, u16 vsi_handle,
2690 		     const struct ice_rss_hash_cfg *cfg)
2691 {
2692 	const enum ice_block blk = ICE_BLK_RSS;
2693 	struct ice_flow_seg_info *segs;
2694 	struct ice_flow_prof *prof;
2695 	u8 segs_cnt;
2696 	int status;
2697 
2698 	segs_cnt = (cfg->hdr_type == ICE_RSS_OUTER_HEADERS) ?
2699 			ICE_FLOW_SEG_SINGLE : ICE_FLOW_SEG_MAX;
2700 	segs = kzalloc_objs(*segs, segs_cnt);
2701 	if (!segs)
2702 		return -ENOMEM;
2703 
2704 	/* Construct the packet segment info from the hashed fields */
2705 	status = ice_flow_set_rss_seg_info(segs, segs_cnt, cfg);
2706 	if (status)
2707 		goto out;
2708 
2709 	prof = ice_flow_find_prof_conds(hw, blk, ICE_FLOW_RX, segs, segs_cnt,
2710 					cfg->symm, vsi_handle,
2711 					ICE_FLOW_FIND_PROF_CHK_FLDS);
2712 	if (!prof) {
2713 		status = -ENOENT;
2714 		goto out;
2715 	}
2716 
2717 	status = ice_flow_disassoc_prof(hw, blk, prof, vsi_handle);
2718 	if (status)
2719 		goto out;
2720 
2721 	/* Remove RSS configuration from VSI context before deleting
2722 	 * the flow profile.
2723 	 */
2724 	ice_rem_rss_list(hw, vsi_handle, prof);
2725 
2726 	if (bitmap_empty(prof->vsis, ICE_MAX_VSI))
2727 		status = ice_flow_rem_prof(hw, blk, prof->id);
2728 
2729 out:
2730 	kfree(segs);
2731 	return status;
2732 }
2733 
2734 /**
2735  * ice_rem_rss_cfg - remove an existing RSS config with matching hashed fields
2736  * @hw: pointer to the hardware structure
2737  * @vsi_handle: software VSI handle
2738  * @cfg: configure parameters
2739  *
2740  * This function will lookup the flow profile based on the input
2741  * hash field bitmap, iterate through the profile entry list of
2742  * that profile and find entry associated with input VSI to be
2743  * removed. Calls are made to underlying flow apis which will in
2744  * turn build or update buffers for RSS XLT1 section.
2745  */
2746 int
2747 ice_rem_rss_cfg(struct ice_hw *hw, u16 vsi_handle,
2748 		const struct ice_rss_hash_cfg *cfg)
2749 {
2750 	struct ice_rss_hash_cfg local_cfg;
2751 	int status;
2752 
2753 	if (!ice_is_vsi_valid(hw, vsi_handle) ||
2754 	    !cfg || cfg->hdr_type > ICE_RSS_ANY_HEADERS ||
2755 	    cfg->hash_flds == ICE_HASH_INVALID)
2756 		return -EINVAL;
2757 
2758 	mutex_lock(&hw->rss_locks);
2759 	local_cfg = *cfg;
2760 	if (cfg->hdr_type < ICE_RSS_ANY_HEADERS) {
2761 		status = ice_rem_rss_cfg_sync(hw, vsi_handle, &local_cfg);
2762 	} else {
2763 		local_cfg.hdr_type = ICE_RSS_OUTER_HEADERS;
2764 		status = ice_rem_rss_cfg_sync(hw, vsi_handle, &local_cfg);
2765 		if (!status) {
2766 			local_cfg.hdr_type = ICE_RSS_INNER_HEADERS;
2767 			status = ice_rem_rss_cfg_sync(hw, vsi_handle,
2768 						      &local_cfg);
2769 		}
2770 	}
2771 	mutex_unlock(&hw->rss_locks);
2772 
2773 	return status;
2774 }
2775 
2776 /* Mapping of AVF hash bit fields to an L3-L4 hash combination.
2777  * As the ice_flow_avf_hdr_field represent individual bit shifts in a hash,
2778  * convert its values to their appropriate flow L3, L4 values.
2779  */
2780 #define ICE_FLOW_AVF_RSS_IPV4_MASKS \
2781 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER) | \
2782 	 BIT_ULL(LIBIE_FILTER_PCTYPE_FRAG_IPV4))
2783 #define ICE_FLOW_AVF_RSS_TCP_IPV4_MASKS \
2784 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) | \
2785 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP))
2786 #define ICE_FLOW_AVF_RSS_UDP_IPV4_MASKS \
2787 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) | \
2788 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) | \
2789 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP))
2790 #define ICE_FLOW_AVF_RSS_ALL_IPV4_MASKS \
2791 	(ICE_FLOW_AVF_RSS_TCP_IPV4_MASKS | ICE_FLOW_AVF_RSS_UDP_IPV4_MASKS | \
2792 	 ICE_FLOW_AVF_RSS_IPV4_MASKS | BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP))
2793 
2794 #define ICE_FLOW_AVF_RSS_IPV6_MASKS \
2795 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER) | \
2796 	 BIT_ULL(LIBIE_FILTER_PCTYPE_FRAG_IPV6))
2797 #define ICE_FLOW_AVF_RSS_UDP_IPV6_MASKS \
2798 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) | \
2799 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) | \
2800 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP))
2801 #define ICE_FLOW_AVF_RSS_TCP_IPV6_MASKS \
2802 	(BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK) | \
2803 	 BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP))
2804 #define ICE_FLOW_AVF_RSS_ALL_IPV6_MASKS \
2805 	(ICE_FLOW_AVF_RSS_TCP_IPV6_MASKS | ICE_FLOW_AVF_RSS_UDP_IPV6_MASKS | \
2806 	 ICE_FLOW_AVF_RSS_IPV6_MASKS | BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP))
2807 
2808 /**
2809  * ice_add_avf_rss_cfg - add an RSS configuration for AVF driver
2810  * @hw: pointer to the hardware structure
2811  * @vsi: VF's VSI
2812  * @avf_hash: hash bit fields (LIBIE_FILTER_PCTYPE_*) to configure
2813  *
2814  * This function will take the hash bitmap provided by the AVF driver via a
2815  * message, convert it to ICE-compatible values, and configure RSS flow
2816  * profiles.
2817  */
2818 int ice_add_avf_rss_cfg(struct ice_hw *hw, struct ice_vsi *vsi, u64 avf_hash)
2819 {
2820 	struct ice_rss_hash_cfg hcfg;
2821 	u16 vsi_handle;
2822 	int status = 0;
2823 	u64 hash_flds;
2824 
2825 	if (!vsi)
2826 		return -EINVAL;
2827 
2828 	vsi_handle = vsi->idx;
2829 	if (!avf_hash || !ice_is_vsi_valid(hw, vsi_handle))
2830 		return -EINVAL;
2831 
2832 	/* Make sure no unsupported bits are specified */
2833 	if (avf_hash & ~(ICE_FLOW_AVF_RSS_ALL_IPV4_MASKS |
2834 			 ICE_FLOW_AVF_RSS_ALL_IPV6_MASKS))
2835 		return -EIO;
2836 
2837 	hash_flds = avf_hash;
2838 
2839 	/* Always create an L3 RSS configuration for any L4 RSS configuration */
2840 	if (hash_flds & ICE_FLOW_AVF_RSS_ALL_IPV4_MASKS)
2841 		hash_flds |= ICE_FLOW_AVF_RSS_IPV4_MASKS;
2842 
2843 	if (hash_flds & ICE_FLOW_AVF_RSS_ALL_IPV6_MASKS)
2844 		hash_flds |= ICE_FLOW_AVF_RSS_IPV6_MASKS;
2845 
2846 	/* Create the corresponding RSS configuration for each valid hash bit */
2847 	while (hash_flds) {
2848 		u64 rss_hash = ICE_HASH_INVALID;
2849 
2850 		if (hash_flds & ICE_FLOW_AVF_RSS_ALL_IPV4_MASKS) {
2851 			if (hash_flds & ICE_FLOW_AVF_RSS_IPV4_MASKS) {
2852 				rss_hash = ICE_FLOW_HASH_IPV4;
2853 				hash_flds &= ~ICE_FLOW_AVF_RSS_IPV4_MASKS;
2854 			} else if (hash_flds &
2855 				   ICE_FLOW_AVF_RSS_TCP_IPV4_MASKS) {
2856 				rss_hash = ICE_FLOW_HASH_IPV4 |
2857 					ICE_FLOW_HASH_TCP_PORT;
2858 				hash_flds &= ~ICE_FLOW_AVF_RSS_TCP_IPV4_MASKS;
2859 			} else if (hash_flds &
2860 				   ICE_FLOW_AVF_RSS_UDP_IPV4_MASKS) {
2861 				rss_hash = ICE_FLOW_HASH_IPV4 |
2862 					ICE_FLOW_HASH_UDP_PORT;
2863 				hash_flds &= ~ICE_FLOW_AVF_RSS_UDP_IPV4_MASKS;
2864 			} else if (hash_flds &
2865 				   BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP)) {
2866 				rss_hash = ICE_FLOW_HASH_IPV4 |
2867 					ICE_FLOW_HASH_SCTP_PORT;
2868 				hash_flds &=
2869 					~BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP);
2870 			}
2871 		} else if (hash_flds & ICE_FLOW_AVF_RSS_ALL_IPV6_MASKS) {
2872 			if (hash_flds & ICE_FLOW_AVF_RSS_IPV6_MASKS) {
2873 				rss_hash = ICE_FLOW_HASH_IPV6;
2874 				hash_flds &= ~ICE_FLOW_AVF_RSS_IPV6_MASKS;
2875 			} else if (hash_flds &
2876 				   ICE_FLOW_AVF_RSS_TCP_IPV6_MASKS) {
2877 				rss_hash = ICE_FLOW_HASH_IPV6 |
2878 					ICE_FLOW_HASH_TCP_PORT;
2879 				hash_flds &= ~ICE_FLOW_AVF_RSS_TCP_IPV6_MASKS;
2880 			} else if (hash_flds &
2881 				   ICE_FLOW_AVF_RSS_UDP_IPV6_MASKS) {
2882 				rss_hash = ICE_FLOW_HASH_IPV6 |
2883 					ICE_FLOW_HASH_UDP_PORT;
2884 				hash_flds &= ~ICE_FLOW_AVF_RSS_UDP_IPV6_MASKS;
2885 			} else if (hash_flds &
2886 				   BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP)) {
2887 				rss_hash = ICE_FLOW_HASH_IPV6 |
2888 					ICE_FLOW_HASH_SCTP_PORT;
2889 				hash_flds &=
2890 					~BIT_ULL(LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP);
2891 			}
2892 		}
2893 
2894 		if (rss_hash == ICE_HASH_INVALID)
2895 			return -EIO;
2896 
2897 		hcfg.addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
2898 		hcfg.hash_flds = rss_hash;
2899 		hcfg.hdr_type = ICE_RSS_ANY_HEADERS;
2900 		hcfg.symm = false;
2901 		status = ice_add_rss_cfg(hw, vsi, &hcfg);
2902 		if (status)
2903 			break;
2904 	}
2905 
2906 	return status;
2907 }
2908 
2909 static bool rss_cfg_symm_valid(u64 hfld)
2910 {
2911 	return !((!!(hfld & ICE_FLOW_HASH_FLD_IPV4_SA) ^
2912 		  !!(hfld & ICE_FLOW_HASH_FLD_IPV4_DA)) ||
2913 		 (!!(hfld & ICE_FLOW_HASH_FLD_IPV6_SA) ^
2914 		  !!(hfld & ICE_FLOW_HASH_FLD_IPV6_DA)) ||
2915 		 (!!(hfld & ICE_FLOW_HASH_FLD_TCP_SRC_PORT) ^
2916 		  !!(hfld & ICE_FLOW_HASH_FLD_TCP_DST_PORT)) ||
2917 		 (!!(hfld & ICE_FLOW_HASH_FLD_UDP_SRC_PORT) ^
2918 		  !!(hfld & ICE_FLOW_HASH_FLD_UDP_DST_PORT)) ||
2919 		 (!!(hfld & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT) ^
2920 		  !!(hfld & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)));
2921 }
2922 
2923 /**
2924  * ice_set_rss_cfg_symm - set symmtery for all VSI's RSS configurations
2925  * @hw: pointer to the hardware structure
2926  * @vsi: VSI to set/unset Symmetric RSS
2927  * @symm: TRUE to set Symmetric RSS hashing
2928  */
2929 int ice_set_rss_cfg_symm(struct ice_hw *hw, struct ice_vsi *vsi, bool symm)
2930 {
2931 	struct ice_rss_hash_cfg	local;
2932 	struct ice_rss_cfg *r, *tmp;
2933 	u16 vsi_handle = vsi->idx;
2934 	int status = 0;
2935 
2936 	if (!ice_is_vsi_valid(hw, vsi_handle))
2937 		return -EINVAL;
2938 
2939 	mutex_lock(&hw->rss_locks);
2940 	list_for_each_entry_safe(r, tmp, &hw->rss_list_head, l_entry) {
2941 		if (test_bit(vsi_handle, r->vsis) && r->hash.symm != symm) {
2942 			local = r->hash;
2943 			local.symm = symm;
2944 			if (symm && !rss_cfg_symm_valid(r->hash.hash_flds))
2945 				continue;
2946 
2947 			status = ice_add_rss_cfg_sync(hw, vsi_handle, &local);
2948 			if (status)
2949 				break;
2950 		}
2951 	}
2952 	mutex_unlock(&hw->rss_locks);
2953 
2954 	return status;
2955 }
2956 
2957 /**
2958  * ice_replay_rss_cfg - replay RSS configurations associated with VSI
2959  * @hw: pointer to the hardware structure
2960  * @vsi_handle: software VSI handle
2961  */
2962 int ice_replay_rss_cfg(struct ice_hw *hw, u16 vsi_handle)
2963 {
2964 	struct ice_rss_cfg *r;
2965 	int status = 0;
2966 
2967 	if (!ice_is_vsi_valid(hw, vsi_handle))
2968 		return -EINVAL;
2969 
2970 	mutex_lock(&hw->rss_locks);
2971 	list_for_each_entry(r, &hw->rss_list_head, l_entry) {
2972 		if (test_bit(vsi_handle, r->vsis)) {
2973 			status = ice_add_rss_cfg_sync(hw, vsi_handle, &r->hash);
2974 			if (status)
2975 				break;
2976 		}
2977 	}
2978 	mutex_unlock(&hw->rss_locks);
2979 
2980 	return status;
2981 }
2982 
2983 /**
2984  * ice_get_rss_cfg - returns hashed fields for the given header types
2985  * @hw: pointer to the hardware structure
2986  * @vsi_handle: software VSI handle
2987  * @hdrs: protocol header type
2988  * @symm: whether the RSS is symmetric (bool, output)
2989  *
2990  * This function will return the match fields of the first instance of flow
2991  * profile having the given header types and containing input VSI
2992  */
2993 u64 ice_get_rss_cfg(struct ice_hw *hw, u16 vsi_handle, u32 hdrs, bool *symm)
2994 {
2995 	u64 rss_hash = ICE_HASH_INVALID;
2996 	struct ice_rss_cfg *r;
2997 
2998 	/* verify if the protocol header is non zero and VSI is valid */
2999 	if (hdrs == ICE_FLOW_SEG_HDR_NONE || !ice_is_vsi_valid(hw, vsi_handle))
3000 		return ICE_HASH_INVALID;
3001 
3002 	mutex_lock(&hw->rss_locks);
3003 	list_for_each_entry(r, &hw->rss_list_head, l_entry)
3004 		if (test_bit(vsi_handle, r->vsis) &&
3005 		    r->hash.addl_hdrs == hdrs) {
3006 			rss_hash = r->hash.hash_flds;
3007 			*symm = r->hash.symm;
3008 			break;
3009 		}
3010 	mutex_unlock(&hw->rss_locks);
3011 
3012 	return rss_hash;
3013 }
3014