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