1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #ifndef _ICE_PROTOCOL_TYPE_H_
5 #define _ICE_PROTOCOL_TYPE_H_
6 #define ICE_IPV6_ADDR_LENGTH 16
7 
8 /* Each recipe can match up to 5 different fields. Fields to match can be meta-
9  * data, values extracted from packet headers, or results from other recipes.
10  * One of the 5 fields is reserved for matching the switch ID. So, up to 4
11  * recipes can provide intermediate results to another one through chaining,
12  * e.g. recipes 0, 1, 2, and 3 can provide intermediate results to recipe 4.
13  */
14 #define ICE_NUM_WORDS_RECIPE 4
15 
16 /* Max recipes that can be chained */
17 #define ICE_MAX_CHAIN_RECIPE 5
18 
19 /* 1 word reserved for switch ID from allowed 5 words.
20  * So a recipe can have max 4 words. And you can chain 5 such recipes
21  * together. So maximum words that can be programmed for look up is 5 * 4.
22  */
23 #define ICE_MAX_CHAIN_WORDS (ICE_NUM_WORDS_RECIPE * ICE_MAX_CHAIN_RECIPE)
24 
25 /* Field vector index corresponding to chaining */
26 #define ICE_CHAIN_FV_INDEX_START 47
27 
28 enum ice_protocol_type {
29 	ICE_MAC_OFOS = 0,
30 	ICE_MAC_IL,
31 	ICE_ETYPE_OL,
32 	ICE_ETYPE_IL,
33 	ICE_VLAN_OFOS,
34 	ICE_IPV4_OFOS,
35 	ICE_IPV4_IL,
36 	ICE_IPV6_OFOS,
37 	ICE_IPV6_IL,
38 	ICE_TCP_IL,
39 	ICE_UDP_OF,
40 	ICE_UDP_ILOS,
41 	ICE_VXLAN,
42 	ICE_GENEVE,
43 	ICE_NVGRE,
44 	ICE_GTP,
45 	ICE_GTP_NO_PAY,
46 	ICE_PFCP,
47 	ICE_PPPOE,
48 	ICE_L2TPV3,
49 	ICE_VLAN_EX,
50 	ICE_VLAN_IN,
51 	ICE_HW_METADATA,
52 	ICE_VXLAN_GPE,
53 	ICE_SCTP_IL,
54 	ICE_PROTOCOL_LAST
55 };
56 
57 enum ice_sw_tunnel_type {
58 	ICE_NON_TUN = 0,
59 	ICE_SW_TUN_AND_NON_TUN,
60 	ICE_SW_TUN_VXLAN,
61 	ICE_SW_TUN_GENEVE,
62 	ICE_SW_TUN_NVGRE,
63 	ICE_SW_TUN_GTPU,
64 	ICE_SW_TUN_GTPC,
65 	ICE_SW_TUN_PFCP,
66 	ICE_ALL_TUNNELS /* All tunnel types including NVGRE */
67 };
68 
69 /* Decoders for ice_prot_id:
70  * - F: First
71  * - I: Inner
72  * - L: Last
73  * - O: Outer
74  * - S: Single
75  */
76 enum ice_prot_id {
77 	ICE_PROT_ID_INVAL	= 0,
78 	ICE_PROT_MAC_OF_OR_S	= 1,
79 	ICE_PROT_MAC_IL		= 4,
80 	ICE_PROT_ETYPE_OL	= 9,
81 	ICE_PROT_ETYPE_IL	= 10,
82 	ICE_PROT_IPV4_OF_OR_S	= 32,
83 	ICE_PROT_IPV4_IL	= 33,
84 	ICE_PROT_IPV6_OF_OR_S	= 40,
85 	ICE_PROT_IPV6_IL	= 41,
86 	ICE_PROT_TCP_IL		= 49,
87 	ICE_PROT_UDP_OF		= 52,
88 	ICE_PROT_UDP_IL_OR_S	= 53,
89 	ICE_PROT_GRE_OF		= 64,
90 	ICE_PROT_ESP_F		= 88,
91 	ICE_PROT_ESP_2		= 89,
92 	ICE_PROT_SCTP_IL	= 96,
93 	ICE_PROT_ICMP_IL	= 98,
94 	ICE_PROT_ICMPV6_IL	= 100,
95 	ICE_PROT_PPPOE		= 103,
96 	ICE_PROT_L2TPV3		= 104,
97 	ICE_PROT_ARP_OF		= 118,
98 	ICE_PROT_META_ID	= 255, /* when offset == metadata */
99 	ICE_PROT_INVALID	= 255  /* when offset == ICE_FV_OFFSET_INVAL */
100 };
101 
102 #define ICE_VNI_OFFSET		12 /* offset of VNI from ICE_PROT_UDP_OF */
103 
104 #define ICE_MAC_OFOS_HW		1
105 #define ICE_MAC_IL_HW		4
106 #define ICE_ETYPE_OL_HW		9
107 #define ICE_ETYPE_IL_HW		10
108 #define ICE_VLAN_OF_HW		16
109 #define ICE_VLAN_OL_HW		17
110 #define ICE_IPV4_OFOS_HW	32
111 #define ICE_IPV4_IL_HW		33
112 #define ICE_IPV6_OFOS_HW	40
113 #define ICE_IPV6_IL_HW		41
114 #define ICE_TCP_IL_HW		49
115 #define ICE_UDP_ILOS_HW		53
116 #define ICE_GRE_OF_HW		64
117 #define ICE_PPPOE_HW		103
118 #define ICE_L2TPV3_HW		104
119 
120 #define ICE_UDP_OF_HW	52 /* UDP Tunnels */
121 
122 
123 #define ICE_TUN_FLAG_FV_IND 2
124 
125 /* Mapping of software defined protocol ID to hardware defined protocol ID */
126 struct ice_protocol_entry {
127 	enum ice_protocol_type type;
128 	u8 protocol_id;
129 };
130 
131 struct ice_ether_hdr {
132 	u8 dst_addr[ETH_ALEN];
133 	u8 src_addr[ETH_ALEN];
134 };
135 
136 struct ice_ethtype_hdr {
137 	__be16 ethtype_id;
138 };
139 
140 struct ice_ether_vlan_hdr {
141 	u8 dst_addr[ETH_ALEN];
142 	u8 src_addr[ETH_ALEN];
143 	__be32 vlan_id;
144 };
145 
146 struct ice_vlan_hdr {
147 	__be16 type;
148 	__be16 vlan;
149 };
150 
151 struct ice_ipv4_hdr {
152 	u8 version;
153 	u8 tos;
154 	__be16 total_length;
155 	__be16 id;
156 	__be16 frag_off;
157 	u8 time_to_live;
158 	u8 protocol;
159 	__be16 check;
160 	__be32 src_addr;
161 	__be32 dst_addr;
162 };
163 
164 struct ice_ipv6_hdr {
165 	__be32 be_ver_tc_flow;
166 	__be16 payload_len;
167 	u8 next_hdr;
168 	u8 hop_limit;
169 	u8 src_addr[ICE_IPV6_ADDR_LENGTH];
170 	u8 dst_addr[ICE_IPV6_ADDR_LENGTH];
171 };
172 
173 struct ice_sctp_hdr {
174 	__be16 src_port;
175 	__be16 dst_port;
176 	__be32 verification_tag;
177 	__be32 check;
178 };
179 
180 struct ice_l4_hdr {
181 	__be16 src_port;
182 	__be16 dst_port;
183 	__be16 len;
184 	__be16 check;
185 };
186 
187 struct ice_udp_tnl_hdr {
188 	__be16 field;
189 	__be16 proto_type;
190 	__be32 vni;     /* only use lower 24-bits */
191 };
192 
193 struct ice_udp_gtp_hdr {
194 	u8 flags;
195 	u8 msg_type;
196 	__be16 rsrvd_len;
197 	__be32 teid;
198 	__be16 rsrvd_seq_nbr;
199 	u8 rsrvd_n_pdu_nbr;
200 	u8 rsrvd_next_ext;
201 	u8 rsvrd_ext_len;
202 	u8 pdu_type;
203 	u8 qfi;
204 	u8 rsvrd;
205 };
206 
207 struct ice_pfcp_hdr {
208 	u8 flags;
209 	u8 msg_type;
210 	__be16 length;
211 	__be64 seid;
212 	__be32 seq;
213 	u8 spare;
214 } __packed __aligned(__alignof__(u16));
215 
216 struct ice_pppoe_hdr {
217 	u8 rsrvd_ver_type;
218 	u8 rsrvd_code;
219 	__be16 session_id;
220 	__be16 length;
221 	__be16 ppp_prot_id; /* control and data only */
222 };
223 
224 struct ice_l2tpv3_sess_hdr {
225 	__be32 session_id;
226 	__be64 cookie;
227 };
228 
229 struct ice_nvgre_hdr {
230 	__be16 flags;
231 	__be16 protocol;
232 	__be32 tni_flow;
233 };
234 
235 /* Metadata information
236  *
237  * Not all MDIDs can be used by switch block. It depends on package version.
238  *
239  * MDID 16 (Rx offset)
240  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241  * |  A  |   B     |   Reserved    |
242  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
243  *
244  * A = Source port where the transaction came from (3b).
245  *
246  * B = Destination TC of the packet. The TC is relative to a port (5b).
247  *
248  * MDID 17
249  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250  * |      PTYPE        | Reserved  |
251  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252  *
253  * PTYPE = Encodes the packet type (10b).
254  *
255  * MDID 18
256  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
257  * | Packet length             | R |
258  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
259  *
260  * Packet length = Length of the packet in bytes
261  *		   (packet always carriers CRC) (14b).
262  * R = Reserved (2b).
263  *
264  * MDID 19
265  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266  * |   Source VSI      | Reserved  |
267  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268  *
269  * Source VSI = Source VSI of packet loopbacked in switch (for egress) (10b).
270  */
271 #define ICE_MDID_SOURCE_VSI_MASK GENMASK(9, 0)
272 
273 /*
274  * MDID 20
275  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
276  * |A|B|C|D|E|F|R|R|G|H|I|J|K|L|M|N|
277  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278  *
279  * A = DSI - set for DSI RX pkts.
280  * B = ipsec_decrypted - invalid on NIC.
281  * C = marker - this is a marker packet.
282  * D = from_network - for TX sets to 0
283  *		      for RX:
284  *		        * 1 - packet is from external link
285  *		        * 0 - packet source is from internal
286  * E = source_interface_is_rx - reflect the physical interface from where the
287  *				packet was received:
288  *				* 1 - Rx
289  *				* 0 - Tx
290  * F = from_mng - The bit signals that the packet's origin is the management.
291  * G = ucast - Outer L2 MAC address is unicast.
292  * H = mcast - Outer L2 MAC address is multicast.
293  * I = bcast - Outer L2 MAC address is broadcast.
294  * J = second_outer_mac_present - 2 outer MAC headers are present in the packet.
295  * K = STAG or BVLAN - Outer L2 header has STAG (ethernet type 0x88a8) or
296  *		       BVLAN (ethernet type 0x88a8).
297  * L = ITAG - Outer L2 header has ITAG *ethernet type 0x88e7)
298  * M = EVLAN (0x8100) - Outer L2 header has EVLAN (ethernet type 0x8100)
299  * N = EVLAN (0x9100) - Outer L2 header has EVLAN (ethernet type 0x9100)
300  */
301 #define ICE_PKT_FROM_NETWORK	BIT(3)
302 #define ICE_PKT_VLAN_STAG	BIT(12)
303 #define ICE_PKT_VLAN_ITAG	BIT(13)
304 #define ICE_PKT_VLAN_EVLAN	(BIT(14) | BIT(15))
305 #define ICE_PKT_VLAN_MASK	(ICE_PKT_VLAN_STAG | ICE_PKT_VLAN_ITAG | \
306 				ICE_PKT_VLAN_EVLAN)
307 /* MDID 21
308  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309  * |A|B|C|D|E|F|G|H|I|J|R|R|K|L|M|N|
310  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311  *
312  * A = VLAN (0x8100) - Outer L2 header has VLAN (ethernet type 0x8100)
313  * B = NSHoE - Outer L2 header has NSH (ethernet type 0x894f)
314  * C = MPLS (0x8847) - There is at least 1 MPLS tag in the outer header
315  *		       (ethernet type 0x8847)
316  * D = MPLS (0x8848) - There is at least 1 MPLS tag in the outer header
317  *		       (ethernet type 0x8848)
318  * E = multi MPLS - There is more than a single MPLS tag in the outer header
319  * F = inner MPLS - There is inner MPLS tag in the packet
320  * G = tunneled MAC - Set if the packet includes a tunneled MAC
321  * H = tunneled VLAN - Same as VLAN, but for a tunneled header
322  * I = pkt_is_frag - Packet is fragmented (ipv4 or ipv6)
323  * J = ipv6_ext - The packet has routing or destination ipv6 extension in inner
324  *		  or outer ipv6 headers
325  * K = RoCE - UDP packet detected as RoCEv2
326  * L = UDP_XSUM_0 - Set to 1 if L4 checksum is 0 in a UDP packet
327  * M = ESP - This is a ESP packet
328  * N = NAT_ESP - This is a ESP packet encapsulated in UDP NAT
329  */
330 #define ICE_PKT_TUNNEL_MAC	BIT(6)
331 #define ICE_PKT_TUNNEL_VLAN	BIT(7)
332 #define ICE_PKT_TUNNEL_MASK	(ICE_PKT_TUNNEL_MAC | ICE_PKT_TUNNEL_VLAN)
333 
334 /* MDID 22
335  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336  * |A|B|C|D|E|F|  G  |H|I|J| K |L|M|
337  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
338  *
339  * A = fin - fin flag in tcp header
340  * B = sync - sync flag in tcp header
341  * C = rst - rst flag in tcp header
342  * D = psh - psh flag in tcp header
343  * E = ack - ack flag in tcp header
344  * F = urg - urg flag in tcp header
345  * G = tunnel type (3b) - Flags used to decode tunnel type:
346  *			  * b000 - not a VXLAN/Geneve/GRE tunnel
347  *			  * b001 - VXLAN-GPE
348  *			  * b010 - VXLAN (non-GPE)
349  *			  * b011 - Geneve
350  *			  * b100 - GRE (no key, no xsum)
351  *			  * b101 - GREK (key, no xsum)
352  *			  * b110 - GREC (no key, xsum)
353  *			  * b111 - GREKC (key, xsum)
354  * H = UDP_GRE - Packet is UDP (VXLAN or VLAN_GPE or Geneve or MPLSoUDP or GRE)
355  *		 tunnel
356  * I = OAM - VXLAN/Geneve/tunneled NSH packet with the OAM bit set
357  * J = tunneled NSH - Packet has NSHoGRE or NSHoUDP
358  * K = switch (2b) - Direction on switch
359  *		     * b00 - normal
360  *		     * b01 - TX force only LAN
361  *		     * b10 - TX disable LAN
362  *		     * b11 - direct to VSI
363  * L = swpe - Represents SWPE bit in TX command
364  * M = sw_cmd - Switch command
365  *
366  * MDID 23
367  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368  * |A|B|C|D|        R        |E|F|R|
369  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370  *
371  * A = MAC error - Produced by MAC according to L2 error conditions
372  * B = PPRS no offload - FIFO overflow in PPRS or any problematic condition in
373  *			 PPRS ANA
374  * C = abort - Set when malicious packet is detected
375  * D = partial analysis - ANA's analysing got cut in the middle
376  *			 (header > 504B etc.)
377  * E = FLM - Flow director hit indication
378  * F = FDLONG - Flow direector long bucket indication
379  *
380  */
381 #define ICE_MDID_SIZE 2
382 #define ICE_META_DATA_ID_HW 255
383 
384 enum ice_hw_metadata_id {
385 	ICE_SOURCE_PORT_MDID = 16,
386 	ICE_PTYPE_MDID = 17,
387 	ICE_PACKET_LENGTH_MDID = 18,
388 	ICE_SOURCE_VSI_MDID = 19,
389 	ICE_PKT_VLAN_MDID = 20,
390 	ICE_PKT_TUNNEL_MDID = 21,
391 	ICE_PKT_TCP_MDID = 22,
392 	ICE_PKT_ERROR_MDID = 23,
393 };
394 
395 enum ice_hw_metadata_offset {
396 	ICE_SOURCE_PORT_MDID_OFFSET = ICE_MDID_SIZE * ICE_SOURCE_PORT_MDID,
397 	ICE_PTYPE_MDID_OFFSET = ICE_MDID_SIZE * ICE_PTYPE_MDID,
398 	ICE_PACKET_LENGTH_MDID_OFFSET = ICE_MDID_SIZE * ICE_PACKET_LENGTH_MDID,
399 	ICE_SOURCE_VSI_MDID_OFFSET = ICE_MDID_SIZE * ICE_SOURCE_VSI_MDID,
400 	ICE_PKT_VLAN_MDID_OFFSET = ICE_MDID_SIZE * ICE_PKT_VLAN_MDID,
401 	ICE_PKT_TUNNEL_MDID_OFFSET = ICE_MDID_SIZE * ICE_PKT_TUNNEL_MDID,
402 	ICE_PKT_TCP_MDID_OFFSET = ICE_MDID_SIZE * ICE_PKT_TCP_MDID,
403 	ICE_PKT_ERROR_MDID_OFFSET = ICE_MDID_SIZE * ICE_PKT_ERROR_MDID,
404 };
405 
406 enum ice_pkt_flags {
407 	ICE_PKT_FLAGS_MDID20 = 0,
408 	ICE_PKT_FLAGS_MDID21 = 1,
409 	ICE_PKT_FLAGS_MDID22 = 2,
410 	ICE_PKT_FLAGS_MDID23 = 3,
411 };
412 
413 struct ice_hw_metadata {
414 	__be16 source_port;
415 	__be16 ptype;
416 	__be16 packet_length;
417 	__be16 source_vsi;
418 	__be16 flags[4];
419 };
420 
421 union ice_prot_hdr {
422 	struct ice_ether_hdr eth_hdr;
423 	struct ice_ethtype_hdr ethertype;
424 	struct ice_vlan_hdr vlan_hdr;
425 	struct ice_ipv4_hdr ipv4_hdr;
426 	struct ice_ipv6_hdr ipv6_hdr;
427 	struct ice_l4_hdr l4_hdr;
428 	struct ice_sctp_hdr sctp_hdr;
429 	struct ice_udp_tnl_hdr tnl_hdr;
430 	struct ice_nvgre_hdr nvgre_hdr;
431 	struct ice_udp_gtp_hdr gtp_hdr;
432 	struct ice_pfcp_hdr pfcp_hdr;
433 	struct ice_pppoe_hdr pppoe_hdr;
434 	struct ice_l2tpv3_sess_hdr l2tpv3_sess_hdr;
435 	struct ice_hw_metadata metadata;
436 };
437 
438 /* This is mapping table entry that maps every word within a given protocol
439  * structure to the real byte offset as per the specification of that
440  * protocol header.
441  * for e.g. dst address is 3 words in ethertype header and corresponding bytes
442  * are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
443  */
444 struct ice_prot_ext_tbl_entry {
445 	enum ice_protocol_type prot_type;
446 	/* Byte offset into header of given protocol type */
447 	u8 offs[sizeof(union ice_prot_hdr)];
448 };
449 
450 /* Extractions to be looked up for a given recipe */
451 struct ice_prot_lkup_ext {
452 	u16 prot_type;
453 	u8 n_val_words;
454 	/* create a buffer to hold max words per recipe */
455 	u16 field_off[ICE_MAX_CHAIN_WORDS];
456 	u16 field_mask[ICE_MAX_CHAIN_WORDS];
457 
458 	struct ice_fv_word fv_words[ICE_MAX_CHAIN_WORDS];
459 
460 	/* Indicate field offsets that have field vector indices assigned */
461 	DECLARE_BITMAP(done, ICE_MAX_CHAIN_WORDS);
462 };
463 
464 struct ice_pref_recipe_group {
465 	u8 n_val_pairs;		/* Number of valid pairs */
466 	struct ice_fv_word pairs[ICE_NUM_WORDS_RECIPE];
467 	u16 mask[ICE_NUM_WORDS_RECIPE];
468 };
469 
470 struct ice_recp_grp_entry {
471 	struct list_head l_entry;
472 
473 #define ICE_INVAL_CHAIN_IND 0xFF
474 	u16 rid;
475 	u8 chain_idx;
476 	u16 fv_idx[ICE_NUM_WORDS_RECIPE];
477 	u16 fv_mask[ICE_NUM_WORDS_RECIPE];
478 	struct ice_pref_recipe_group r_group;
479 };
480 #endif /* _ICE_PROTOCOL_TYPE_H_ */
481