xref: /openbmc/linux/drivers/net/ethernet/sfc/filter.h (revision c832da79)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2013 Solarflare Communications Inc.
5  */
6 
7 #ifndef EFX_FILTER_H
8 #define EFX_FILTER_H
9 
10 #include <linux/types.h>
11 #include <linux/if_ether.h>
12 #include <asm/byteorder.h>
13 
14 /**
15  * enum efx_filter_match_flags - Flags for hardware filter match type
16  * @EFX_FILTER_MATCH_REM_HOST: Match by remote IP host address
17  * @EFX_FILTER_MATCH_LOC_HOST: Match by local IP host address
18  * @EFX_FILTER_MATCH_REM_MAC: Match by remote MAC address
19  * @EFX_FILTER_MATCH_REM_PORT: Match by remote TCP/UDP port
20  * @EFX_FILTER_MATCH_LOC_MAC: Match by local MAC address
21  * @EFX_FILTER_MATCH_LOC_PORT: Match by local TCP/UDP port
22  * @EFX_FILTER_MATCH_ETHER_TYPE: Match by Ether-type
23  * @EFX_FILTER_MATCH_INNER_VID: Match by inner VLAN ID
24  * @EFX_FILTER_MATCH_OUTER_VID: Match by outer VLAN ID
25  * @EFX_FILTER_MATCH_IP_PROTO: Match by IP transport protocol
26  * @EFX_FILTER_MATCH_LOC_MAC_IG: Match by local MAC address I/G bit.
27  * @EFX_FILTER_MATCH_ENCAP_TYPE: Match by encapsulation type.
28  *	Used for RX default unicast and multicast/broadcast filters.
29  *
30  * Only some combinations are supported, depending on NIC type:
31  *
32  * - Falcon supports RX filters matching by {TCP,UDP}/IPv4 4-tuple or
33  *   local 2-tuple (only implemented for Falcon B0)
34  *
35  * - Siena supports RX and TX filters matching by {TCP,UDP}/IPv4 4-tuple
36  *   or local 2-tuple, or local MAC with or without outer VID, and RX
37  *   default filters
38  *
39  * - Huntington supports filter matching controlled by firmware, potentially
40  *   using {TCP,UDP}/IPv{4,6} 4-tuple or local 2-tuple, local MAC or I/G bit,
41  *   with or without outer and inner VID
42  */
43 enum efx_filter_match_flags {
44 	EFX_FILTER_MATCH_REM_HOST =	0x0001,
45 	EFX_FILTER_MATCH_LOC_HOST =	0x0002,
46 	EFX_FILTER_MATCH_REM_MAC =	0x0004,
47 	EFX_FILTER_MATCH_REM_PORT =	0x0008,
48 	EFX_FILTER_MATCH_LOC_MAC =	0x0010,
49 	EFX_FILTER_MATCH_LOC_PORT =	0x0020,
50 	EFX_FILTER_MATCH_ETHER_TYPE =	0x0040,
51 	EFX_FILTER_MATCH_INNER_VID =	0x0080,
52 	EFX_FILTER_MATCH_OUTER_VID =	0x0100,
53 	EFX_FILTER_MATCH_IP_PROTO =	0x0200,
54 	EFX_FILTER_MATCH_LOC_MAC_IG =	0x0400,
55 	EFX_FILTER_MATCH_ENCAP_TYPE =	0x0800,
56 };
57 
58 /**
59  * enum efx_filter_priority - priority of a hardware filter specification
60  * @EFX_FILTER_PRI_HINT: Performance hint
61  * @EFX_FILTER_PRI_AUTO: Automatic filter based on device address list
62  *	or hardware requirements.  This may only be used by the filter
63  *	implementation for each NIC type.
64  * @EFX_FILTER_PRI_MANUAL: Manually configured filter
65  * @EFX_FILTER_PRI_REQUIRED: Required for correct behaviour (user-level
66  *	networking and SR-IOV)
67  */
68 enum efx_filter_priority {
69 	EFX_FILTER_PRI_HINT = 0,
70 	EFX_FILTER_PRI_AUTO,
71 	EFX_FILTER_PRI_MANUAL,
72 	EFX_FILTER_PRI_REQUIRED,
73 };
74 
75 /**
76  * enum efx_filter_flags - flags for hardware filter specifications
77  * @EFX_FILTER_FLAG_RX_RSS: Use RSS to spread across multiple queues.
78  *	By default, matching packets will be delivered only to the
79  *	specified queue. If this flag is set, they will be delivered
80  *	to a range of queues offset from the specified queue number
81  *	according to the indirection table.
82  * @EFX_FILTER_FLAG_RX_SCATTER: Enable DMA scatter on the receiving
83  *	queue.
84  * @EFX_FILTER_FLAG_RX_OVER_AUTO: Indicates a filter that is
85  *	overriding an automatic filter (priority
86  *	%EFX_FILTER_PRI_AUTO).  This may only be set by the filter
87  *	implementation for each type.  A removal request will restore
88  *	the automatic filter in its place.
89  * @EFX_FILTER_FLAG_RX: Filter is for RX
90  * @EFX_FILTER_FLAG_TX: Filter is for TX
91  * @EFX_FILTER_FLAG_VPORT_ID: Virtual port ID for adapter switching.
92  */
93 enum efx_filter_flags {
94 	EFX_FILTER_FLAG_RX_RSS = 0x01,
95 	EFX_FILTER_FLAG_RX_SCATTER = 0x02,
96 	EFX_FILTER_FLAG_RX_OVER_AUTO = 0x04,
97 	EFX_FILTER_FLAG_RX = 0x08,
98 	EFX_FILTER_FLAG_TX = 0x10,
99 	EFX_FILTER_FLAG_VPORT_ID = 0x20,
100 };
101 
102 /** enum efx_encap_type - types of encapsulation
103  * @EFX_ENCAP_TYPE_NONE: no encapsulation
104  * @EFX_ENCAP_TYPE_VXLAN: VXLAN encapsulation
105  * @EFX_ENCAP_TYPE_NVGRE: NVGRE encapsulation
106  * @EFX_ENCAP_TYPE_GENEVE: GENEVE encapsulation
107  * @EFX_ENCAP_FLAG_IPV6: indicates IPv6 outer frame
108  *
109  * Contains both enumerated types and flags.
110  * To get just the type, OR with @EFX_ENCAP_TYPES_MASK.
111  */
112 enum efx_encap_type {
113 	EFX_ENCAP_TYPE_NONE = 0,
114 	EFX_ENCAP_TYPE_VXLAN = 1,
115 	EFX_ENCAP_TYPE_NVGRE = 2,
116 	EFX_ENCAP_TYPE_GENEVE = 3,
117 
118 	EFX_ENCAP_TYPES_MASK = 7,
119 	EFX_ENCAP_FLAG_IPV6 = 8,
120 };
121 
122 /**
123  * struct efx_filter_spec - specification for a hardware filter
124  * @match_flags: Match type flags, from &enum efx_filter_match_flags
125  * @priority: Priority of the filter, from &enum efx_filter_priority
126  * @flags: Miscellaneous flags, from &enum efx_filter_flags
127  * @rss_context: RSS context to use, if %EFX_FILTER_FLAG_RX_RSS is set.  This
128  *	is a user_id (with 0 meaning the driver/default RSS context), not an
129  *	MCFW context_id.
130  * @dmaq_id: Source/target queue index, or %EFX_FILTER_RX_DMAQ_ID_DROP for
131  *	an RX drop filter
132  * @vport_id: Virtual port ID associated with RX queue, for adapter switching,
133  *	if %EFX_FILTER_FLAG_VPORT_ID is set.  This is an MCFW vport_id, or on
134  *	EF100 an mport selector.
135  * @outer_vid: Outer VLAN ID to match, if %EFX_FILTER_MATCH_OUTER_VID is set
136  * @inner_vid: Inner VLAN ID to match, if %EFX_FILTER_MATCH_INNER_VID is set
137  * @loc_mac: Local MAC address to match, if %EFX_FILTER_MATCH_LOC_MAC or
138  *	%EFX_FILTER_MATCH_LOC_MAC_IG is set
139  * @rem_mac: Remote MAC address to match, if %EFX_FILTER_MATCH_REM_MAC is set
140  * @ether_type: Ether-type to match, if %EFX_FILTER_MATCH_ETHER_TYPE is set
141  * @ip_proto: IP transport protocol to match, if %EFX_FILTER_MATCH_IP_PROTO
142  *	is set
143  * @loc_host: Local IP host to match, if %EFX_FILTER_MATCH_LOC_HOST is set
144  * @rem_host: Remote IP host to match, if %EFX_FILTER_MATCH_REM_HOST is set
145  * @loc_port: Local TCP/UDP port to match, if %EFX_FILTER_MATCH_LOC_PORT is set
146  * @rem_port: Remote TCP/UDP port to match, if %EFX_FILTER_MATCH_REM_PORT is set
147  * @encap_type: Encapsulation type to match (from &enum efx_encap_type), if
148  *	%EFX_FILTER_MATCH_ENCAP_TYPE is set
149  *
150  * The efx_filter_init_rx() or efx_filter_init_tx() function *must* be
151  * used to initialise the structure.  The efx_filter_set_*() functions
152  * may then be used to set @rss_context, @match_flags and related
153  * fields.
154  *
155  * The @priority field is used by software to determine whether a new
156  * filter may replace an old one.  The hardware priority of a filter
157  * depends on which fields are matched.
158  */
159 struct efx_filter_spec {
160 	u32	match_flags:12;
161 	u32	priority:2;
162 	u32	flags:6;
163 	u32	dmaq_id:12;
164 	u32	vport_id;
165 	u32	rss_context;
166 	__be16	outer_vid __aligned(4); /* allow jhash2() of match values */
167 	__be16	inner_vid;
168 	u8	loc_mac[ETH_ALEN];
169 	u8	rem_mac[ETH_ALEN];
170 	__be16	ether_type;
171 	u8	ip_proto;
172 	__be32	loc_host[4];
173 	__be32	rem_host[4];
174 	__be16	loc_port;
175 	__be16	rem_port;
176 	u32     encap_type:4;
177 	/* total 65 bytes */
178 };
179 
180 enum {
181 	EFX_FILTER_RX_DMAQ_ID_DROP = 0xfff
182 };
183 
184 static inline void efx_filter_init_rx(struct efx_filter_spec *spec,
185 				      enum efx_filter_priority priority,
186 				      enum efx_filter_flags flags,
187 				      unsigned rxq_id)
188 {
189 	memset(spec, 0, sizeof(*spec));
190 	spec->priority = priority;
191 	spec->flags = EFX_FILTER_FLAG_RX | flags;
192 	spec->rss_context = 0;
193 	spec->dmaq_id = rxq_id;
194 }
195 
196 static inline void efx_filter_init_tx(struct efx_filter_spec *spec,
197 				      unsigned txq_id)
198 {
199 	memset(spec, 0, sizeof(*spec));
200 	spec->priority = EFX_FILTER_PRI_REQUIRED;
201 	spec->flags = EFX_FILTER_FLAG_TX;
202 	spec->dmaq_id = txq_id;
203 }
204 
205 /**
206  * efx_filter_set_ipv4_local - specify IPv4 host, transport protocol and port
207  * @spec: Specification to initialise
208  * @proto: Transport layer protocol number
209  * @host: Local host address (network byte order)
210  * @port: Local port (network byte order)
211  */
212 static inline int
213 efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
214 			  __be32 host, __be16 port)
215 {
216 	spec->match_flags |=
217 		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
218 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT;
219 	spec->ether_type = htons(ETH_P_IP);
220 	spec->ip_proto = proto;
221 	spec->loc_host[0] = host;
222 	spec->loc_port = port;
223 	return 0;
224 }
225 
226 /**
227  * efx_filter_set_ipv4_full - specify IPv4 hosts, transport protocol and ports
228  * @spec: Specification to initialise
229  * @proto: Transport layer protocol number
230  * @lhost: Local host address (network byte order)
231  * @lport: Local port (network byte order)
232  * @rhost: Remote host address (network byte order)
233  * @rport: Remote port (network byte order)
234  */
235 static inline int
236 efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
237 			 __be32 lhost, __be16 lport,
238 			 __be32 rhost, __be16 rport)
239 {
240 	spec->match_flags |=
241 		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
242 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
243 		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
244 	spec->ether_type = htons(ETH_P_IP);
245 	spec->ip_proto = proto;
246 	spec->loc_host[0] = lhost;
247 	spec->loc_port = lport;
248 	spec->rem_host[0] = rhost;
249 	spec->rem_port = rport;
250 	return 0;
251 }
252 
253 enum {
254 	EFX_FILTER_VID_UNSPEC = 0xffff,
255 };
256 
257 /**
258  * efx_filter_set_eth_local - specify local Ethernet address and/or VID
259  * @spec: Specification to initialise
260  * @vid: Outer VLAN ID to match, or %EFX_FILTER_VID_UNSPEC
261  * @addr: Local Ethernet MAC address, or %NULL
262  */
263 static inline int efx_filter_set_eth_local(struct efx_filter_spec *spec,
264 					   u16 vid, const u8 *addr)
265 {
266 	if (vid == EFX_FILTER_VID_UNSPEC && addr == NULL)
267 		return -EINVAL;
268 
269 	if (vid != EFX_FILTER_VID_UNSPEC) {
270 		spec->match_flags |= EFX_FILTER_MATCH_OUTER_VID;
271 		spec->outer_vid = htons(vid);
272 	}
273 	if (addr != NULL) {
274 		spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC;
275 		ether_addr_copy(spec->loc_mac, addr);
276 	}
277 	return 0;
278 }
279 
280 /**
281  * efx_filter_set_uc_def - specify matching otherwise-unmatched unicast
282  * @spec: Specification to initialise
283  */
284 static inline int efx_filter_set_uc_def(struct efx_filter_spec *spec)
285 {
286 	spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
287 	return 0;
288 }
289 
290 /**
291  * efx_filter_set_mc_def - specify matching otherwise-unmatched multicast
292  * @spec: Specification to initialise
293  */
294 static inline int efx_filter_set_mc_def(struct efx_filter_spec *spec)
295 {
296 	spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
297 	spec->loc_mac[0] = 1;
298 	return 0;
299 }
300 
301 /**
302  * efx_filter_set_vport_id - override virtual port id relating to filter
303  * @spec: Specification to initialise
304  * @vport_id: firmware ID of the virtual port
305  */
306 static inline void efx_filter_set_vport_id(struct efx_filter_spec *spec,
307 					   u32 vport_id)
308 {
309 	spec->flags |= EFX_FILTER_FLAG_VPORT_ID;
310 	spec->vport_id = vport_id;
311 }
312 
313 static inline void efx_filter_set_encap_type(struct efx_filter_spec *spec,
314 					     enum efx_encap_type encap_type)
315 {
316 	spec->match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
317 	spec->encap_type = encap_type;
318 }
319 
320 static inline enum efx_encap_type efx_filter_get_encap_type(
321 		const struct efx_filter_spec *spec)
322 {
323 	if (spec->match_flags & EFX_FILTER_MATCH_ENCAP_TYPE)
324 		return spec->encap_type;
325 	return EFX_ENCAP_TYPE_NONE;
326 }
327 #endif /* EFX_FILTER_H */
328