sfc: Extend and abstract efx_filter_spec to cover Huntington/EF10
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / sfc / filter.h
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2005-2010 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  *      Used for RX default unicast and multicast/broadcast filters.
31  *
32  * Only some combinations are supported, depending on NIC type:
33  *
34  * - Falcon supports RX filters matching by {TCP,UDP}/IPv4 4-tuple or
35  *   local 2-tuple (only implemented for Falcon B0)
36  *
37  * - Siena supports RX and TX filters matching by {TCP,UDP}/IPv4 4-tuple
38  *   or local 2-tuple, or local MAC with or without outer VID, and RX
39  *   default filters
40  *
41  * - Huntington supports filter matching controlled by firmware, potentially
42  *   using {TCP,UDP}/IPv{4,6} 4-tuple or local 2-tuple, local MAC or I/G bit,
43  *   with or without outer and inner VID
44  */
45 enum efx_filter_match_flags {
46         EFX_FILTER_MATCH_REM_HOST =     0x0001,
47         EFX_FILTER_MATCH_LOC_HOST =     0x0002,
48         EFX_FILTER_MATCH_REM_MAC =      0x0004,
49         EFX_FILTER_MATCH_REM_PORT =     0x0008,
50         EFX_FILTER_MATCH_LOC_MAC =      0x0010,
51         EFX_FILTER_MATCH_LOC_PORT =     0x0020,
52         EFX_FILTER_MATCH_ETHER_TYPE =   0x0040,
53         EFX_FILTER_MATCH_INNER_VID =    0x0080,
54         EFX_FILTER_MATCH_OUTER_VID =    0x0100,
55         EFX_FILTER_MATCH_IP_PROTO =     0x0200,
56         EFX_FILTER_MATCH_LOC_MAC_IG =   0x0400,
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_MANUAL: Manually configured filter
63  * @EFX_FILTER_PRI_REQUIRED: Required for correct behaviour (user-level
64  *      networking and SR-IOV)
65  */
66 enum efx_filter_priority {
67         EFX_FILTER_PRI_HINT = 0,
68         EFX_FILTER_PRI_MANUAL,
69         EFX_FILTER_PRI_REQUIRED,
70 };
71
72 /**
73  * enum efx_filter_flags - flags for hardware filter specifications
74  * @EFX_FILTER_FLAG_RX_RSS: Use RSS to spread across multiple queues.
75  *      By default, matching packets will be delivered only to the
76  *      specified queue. If this flag is set, they will be delivered
77  *      to a range of queues offset from the specified queue number
78  *      according to the indirection table.
79  * @EFX_FILTER_FLAG_RX_SCATTER: Enable DMA scatter on the receiving
80  *      queue.
81  * @EFX_FILTER_FLAG_RX: Filter is for RX
82  * @EFX_FILTER_FLAG_TX: Filter is for TX
83  */
84 enum efx_filter_flags {
85         EFX_FILTER_FLAG_RX_RSS = 0x01,
86         EFX_FILTER_FLAG_RX_SCATTER = 0x02,
87         EFX_FILTER_FLAG_RX = 0x08,
88         EFX_FILTER_FLAG_TX = 0x10,
89 };
90
91 /**
92  * struct efx_filter_spec - specification for a hardware filter
93  * @match_flags: Match type flags, from &enum efx_filter_match_flags
94  * @priority: Priority of the filter, from &enum efx_filter_priority
95  * @flags: Miscellaneous flags, from &enum efx_filter_flags
96  * @rss_context: RSS context to use, if %EFX_FILTER_FLAG_RX_RSS is set
97  * @dmaq_id: Source/target queue index, or %EFX_FILTER_RX_DMAQ_ID_DROP for
98  *      an RX drop filter
99  * @outer_vid: Outer VLAN ID to match, if %EFX_FILTER_MATCH_OUTER_VID is set
100  * @inner_vid: Inner VLAN ID to match, if %EFX_FILTER_MATCH_INNER_VID is set
101  * @loc_mac: Local MAC address to match, if %EFX_FILTER_MATCH_LOC_MAC or
102  *      %EFX_FILTER_MATCH_LOC_MAC_IG is set
103  * @rem_mac: Remote MAC address to match, if %EFX_FILTER_MATCH_REM_MAC is set
104  * @ether_type: Ether-type to match, if %EFX_FILTER_MATCH_ETHER_TYPE is set
105  * @ip_proto: IP transport protocol to match, if %EFX_FILTER_MATCH_IP_PROTO
106  *      is set
107  * @loc_host: Local IP host to match, if %EFX_FILTER_MATCH_LOC_HOST is set
108  * @rem_host: Remote IP host to match, if %EFX_FILTER_MATCH_REM_HOST is set
109  * @loc_port: Local TCP/UDP port to match, if %EFX_FILTER_MATCH_LOC_PORT is set
110  * @rem_port: Remote TCP/UDP port to match, if %EFX_FILTER_MATCH_REM_PORT is set
111  *
112  * The efx_filter_init_rx() or efx_filter_init_tx() function *must* be
113  * used to initialise the structure.  The efx_filter_set_*() functions
114  * may then be used to set @rss_context, @match_flags and related
115  * fields.
116  *
117  * The @priority field is used by software to determine whether a new
118  * filter may replace an old one.  The hardware priority of a filter
119  * depends on which fields are matched.
120  */
121 struct efx_filter_spec {
122         u32     match_flags:12;
123         u32     priority:2;
124         u32     flags:6;
125         u32     dmaq_id:12;
126         u32     rss_context;
127         __be16  outer_vid __aligned(4); /* allow jhash2() of match values */
128         __be16  inner_vid;
129         u8      loc_mac[ETH_ALEN];
130         u8      rem_mac[ETH_ALEN];
131         __be16  ether_type;
132         u8      ip_proto;
133         __be32  loc_host[4];
134         __be32  rem_host[4];
135         __be16  loc_port;
136         __be16  rem_port;
137         /* total 64 bytes */
138 };
139
140 enum {
141         EFX_FILTER_RSS_CONTEXT_DEFAULT = 0xffffffff,
142         EFX_FILTER_RX_DMAQ_ID_DROP = 0xfff
143 };
144
145 static inline void efx_filter_init_rx(struct efx_filter_spec *spec,
146                                       enum efx_filter_priority priority,
147                                       enum efx_filter_flags flags,
148                                       unsigned rxq_id)
149 {
150         memset(spec, 0, sizeof(*spec));
151         spec->priority = priority;
152         spec->flags = EFX_FILTER_FLAG_RX | flags;
153         spec->rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
154         spec->dmaq_id = rxq_id;
155 }
156
157 static inline void efx_filter_init_tx(struct efx_filter_spec *spec,
158                                       unsigned txq_id)
159 {
160         memset(spec, 0, sizeof(*spec));
161         spec->priority = EFX_FILTER_PRI_REQUIRED;
162         spec->flags = EFX_FILTER_FLAG_TX;
163         spec->dmaq_id = txq_id;
164 }
165
166 /**
167  * efx_filter_set_ipv4_local - specify IPv4 host, transport protocol and port
168  * @spec: Specification to initialise
169  * @proto: Transport layer protocol number
170  * @host: Local host address (network byte order)
171  * @port: Local port (network byte order)
172  */
173 static inline int
174 efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
175                           __be32 host, __be16 port)
176 {
177         spec->match_flags |=
178                 EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
179                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT;
180         spec->ether_type = htons(ETH_P_IP);
181         spec->ip_proto = proto;
182         spec->loc_host[0] = host;
183         spec->loc_port = port;
184         return 0;
185 }
186
187 /**
188  * efx_filter_set_ipv4_full - specify IPv4 hosts, transport protocol and ports
189  * @spec: Specification to initialise
190  * @proto: Transport layer protocol number
191  * @lhost: Local host address (network byte order)
192  * @lport: Local port (network byte order)
193  * @rhost: Remote host address (network byte order)
194  * @rport: Remote port (network byte order)
195  */
196 static inline int
197 efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
198                          __be32 lhost, __be16 lport,
199                          __be32 rhost, __be16 rport)
200 {
201         spec->match_flags |=
202                 EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
203                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
204                 EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
205         spec->ether_type = htons(ETH_P_IP);
206         spec->ip_proto = proto;
207         spec->loc_host[0] = lhost;
208         spec->loc_port = lport;
209         spec->rem_host[0] = rhost;
210         spec->rem_port = rport;
211         return 0;
212 }
213
214 enum {
215         EFX_FILTER_VID_UNSPEC = 0xffff,
216 };
217
218 /**
219  * efx_filter_set_eth_local - specify local Ethernet address and/or VID
220  * @spec: Specification to initialise
221  * @vid: Outer VLAN ID to match, or %EFX_FILTER_VID_UNSPEC
222  * @addr: Local Ethernet MAC address, or %NULL
223  */
224 static inline int efx_filter_set_eth_local(struct efx_filter_spec *spec,
225                                            u16 vid, const u8 *addr)
226 {
227         if (vid == EFX_FILTER_VID_UNSPEC && addr == NULL)
228                 return -EINVAL;
229
230         if (vid != EFX_FILTER_VID_UNSPEC) {
231                 spec->match_flags |= EFX_FILTER_MATCH_OUTER_VID;
232                 spec->outer_vid = htons(vid);
233         }
234         if (addr != NULL) {
235                 spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC;
236                 memcpy(spec->loc_mac, addr, ETH_ALEN);
237         }
238         return 0;
239 }
240
241 /**
242  * efx_filter_set_uc_def - specify matching otherwise-unmatched unicast
243  * @spec: Specification to initialise
244  */
245 static inline int efx_filter_set_uc_def(struct efx_filter_spec *spec)
246 {
247         spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
248         return 0;
249 }
250
251 /**
252  * efx_filter_set_mc_def - specify matching otherwise-unmatched multicast
253  * @spec: Specification to initialise
254  */
255 static inline int efx_filter_set_mc_def(struct efx_filter_spec *spec)
256 {
257         spec->match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
258         spec->loc_mac[0] = 1;
259         return 0;
260 }
261
262 #endif /* EFX_FILTER_H */