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