]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/common/ef10_filter.c
sfxge(4): add Medford2 support to FILTER module
[FreeBSD/FreeBSD.git] / sys / dev / sfxge / common / ef10_filter.c
1 /*-
2  * Copyright (c) 2007-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "efx.h"
35 #include "efx_impl.h"
36
37 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
38
39 #if EFSYS_OPT_FILTER
40
41 #define EFE_SPEC(eftp, index)   ((eftp)->eft_entry[(index)].efe_spec)
42
43 static                  efx_filter_spec_t *
44 ef10_filter_entry_spec(
45         __in            const ef10_filter_table_t *eftp,
46         __in            unsigned int index)
47 {
48         return ((efx_filter_spec_t *)(EFE_SPEC(eftp, index) &
49                 ~(uintptr_t)EFX_EF10_FILTER_FLAGS));
50 }
51
52 static                  boolean_t
53 ef10_filter_entry_is_busy(
54         __in            const ef10_filter_table_t *eftp,
55         __in            unsigned int index)
56 {
57         if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_BUSY)
58                 return (B_TRUE);
59         else
60                 return (B_FALSE);
61 }
62
63 static                  boolean_t
64 ef10_filter_entry_is_auto_old(
65         __in            const ef10_filter_table_t *eftp,
66         __in            unsigned int index)
67 {
68         if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_AUTO_OLD)
69                 return (B_TRUE);
70         else
71                 return (B_FALSE);
72 }
73
74 static                  void
75 ef10_filter_set_entry(
76         __inout         ef10_filter_table_t *eftp,
77         __in            unsigned int index,
78         __in_opt        const efx_filter_spec_t *efsp)
79 {
80         EFE_SPEC(eftp, index) = (uintptr_t)efsp;
81 }
82
83 static                  void
84 ef10_filter_set_entry_busy(
85         __inout         ef10_filter_table_t *eftp,
86         __in            unsigned int index)
87 {
88         EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
89 }
90
91 static                  void
92 ef10_filter_set_entry_not_busy(
93         __inout         ef10_filter_table_t *eftp,
94         __in            unsigned int index)
95 {
96         EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
97 }
98
99 static                  void
100 ef10_filter_set_entry_auto_old(
101         __inout         ef10_filter_table_t *eftp,
102         __in            unsigned int index)
103 {
104         EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
105         EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
106 }
107
108 static                  void
109 ef10_filter_set_entry_not_auto_old(
110         __inout         ef10_filter_table_t *eftp,
111         __in            unsigned int index)
112 {
113         EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
114         EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
115 }
116
117         __checkReturn   efx_rc_t
118 ef10_filter_init(
119         __in            efx_nic_t *enp)
120 {
121         efx_rc_t rc;
122         ef10_filter_table_t *eftp;
123
124         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
125             enp->en_family == EFX_FAMILY_MEDFORD ||
126             enp->en_family == EFX_FAMILY_MEDFORD2);
127
128 #define MATCH_MASK(match) (EFX_MASK32(match) << EFX_LOW_BIT(match))
129         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_HOST ==
130             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_IP));
131         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_HOST ==
132             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_IP));
133         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_MAC ==
134             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_MAC));
135         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_PORT ==
136             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_PORT));
137         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_MAC ==
138             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_MAC));
139         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_PORT ==
140             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_PORT));
141         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_ETHER_TYPE ==
142             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_ETHER_TYPE));
143         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_INNER_VID ==
144             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_INNER_VLAN));
145         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_OUTER_VID ==
146             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_OUTER_VLAN));
147         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IP_PROTO ==
148             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IP_PROTO));
149         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST ==
150             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_MCAST_DST));
151         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST ==
152             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_UCAST_DST));
153         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_UNKNOWN_MCAST_DST ==
154             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_MCAST_DST));
155         EFX_STATIC_ASSERT((uint32_t)EFX_FILTER_MATCH_UNKNOWN_UCAST_DST ==
156             MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_UCAST_DST));
157 #undef MATCH_MASK
158
159         EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (ef10_filter_table_t), eftp);
160
161         if (!eftp) {
162                 rc = ENOMEM;
163                 goto fail1;
164         }
165
166         enp->en_filter.ef_ef10_filter_table = eftp;
167
168         return (0);
169
170 fail1:
171         EFSYS_PROBE1(fail1, efx_rc_t, rc);
172
173         return (rc);
174 }
175
176                         void
177 ef10_filter_fini(
178         __in            efx_nic_t *enp)
179 {
180         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
181             enp->en_family == EFX_FAMILY_MEDFORD ||
182             enp->en_family == EFX_FAMILY_MEDFORD2);
183
184         if (enp->en_filter.ef_ef10_filter_table != NULL) {
185                 EFSYS_KMEM_FREE(enp->en_esip, sizeof (ef10_filter_table_t),
186                     enp->en_filter.ef_ef10_filter_table);
187         }
188 }
189
190 static  __checkReturn   efx_rc_t
191 efx_mcdi_filter_op_add(
192         __in            efx_nic_t *enp,
193         __in            efx_filter_spec_t *spec,
194         __in            unsigned int filter_op,
195         __inout         ef10_filter_handle_t *handle)
196 {
197         efx_mcdi_req_t req;
198         uint8_t payload[MAX(MC_CMD_FILTER_OP_EXT_IN_LEN,
199                             MC_CMD_FILTER_OP_EXT_OUT_LEN)];
200         efx_rc_t rc;
201
202         memset(payload, 0, sizeof (payload));
203         req.emr_cmd = MC_CMD_FILTER_OP;
204         req.emr_in_buf = payload;
205         req.emr_in_length = MC_CMD_FILTER_OP_EXT_IN_LEN;
206         req.emr_out_buf = payload;
207         req.emr_out_length = MC_CMD_FILTER_OP_EXT_OUT_LEN;
208
209         switch (filter_op) {
210         case MC_CMD_FILTER_OP_IN_OP_REPLACE:
211                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_LO,
212                     handle->efh_lo);
213                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_HI,
214                     handle->efh_hi);
215                 /* Fall through */
216         case MC_CMD_FILTER_OP_IN_OP_INSERT:
217         case MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE:
218                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP, filter_op);
219                 break;
220         default:
221                 EFSYS_ASSERT(0);
222                 rc = EINVAL;
223                 goto fail1;
224         }
225
226         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_PORT_ID,
227             EVB_PORT_ID_ASSIGNED);
228         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_MATCH_FIELDS,
229             spec->efs_match_flags);
230         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_DEST,
231             MC_CMD_FILTER_OP_EXT_IN_RX_DEST_HOST);
232         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_QUEUE,
233             spec->efs_dmaq_id);
234
235 #if EFSYS_OPT_RX_SCALE
236         if (spec->efs_flags & EFX_FILTER_FLAG_RX_RSS) {
237                 uint32_t rss_context;
238
239                 if (spec->efs_rss_context == EFX_RSS_CONTEXT_DEFAULT)
240                         rss_context = enp->en_rss_context;
241                 else
242                         rss_context = spec->efs_rss_context;
243                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_CONTEXT,
244                     rss_context);
245         }
246 #endif
247
248         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_MODE,
249             spec->efs_flags & EFX_FILTER_FLAG_RX_RSS ?
250             MC_CMD_FILTER_OP_EXT_IN_RX_MODE_RSS :
251             MC_CMD_FILTER_OP_EXT_IN_RX_MODE_SIMPLE);
252         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_TX_DEST,
253             MC_CMD_FILTER_OP_EXT_IN_TX_DEST_DEFAULT);
254
255         if (filter_op != MC_CMD_FILTER_OP_IN_OP_REPLACE) {
256                 /*
257                  * NOTE: Unlike most MCDI requests, the filter fields
258                  * are presented in network (big endian) byte order.
259                  */
260                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_SRC_MAC),
261                     spec->efs_rem_mac, EFX_MAC_ADDR_LEN);
262                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_DST_MAC),
263                     spec->efs_loc_mac, EFX_MAC_ADDR_LEN);
264
265                 MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_SRC_PORT,
266                     __CPU_TO_BE_16(spec->efs_rem_port));
267                 MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_DST_PORT,
268                     __CPU_TO_BE_16(spec->efs_loc_port));
269
270                 MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_ETHER_TYPE,
271                     __CPU_TO_BE_16(spec->efs_ether_type));
272
273                 MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_INNER_VLAN,
274                     __CPU_TO_BE_16(spec->efs_inner_vid));
275                 MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_OUTER_VLAN,
276                     __CPU_TO_BE_16(spec->efs_outer_vid));
277
278                 /* IP protocol (in low byte, high byte is zero) */
279                 MCDI_IN_SET_BYTE(req, FILTER_OP_EXT_IN_IP_PROTO,
280                     spec->efs_ip_proto);
281
282                 EFX_STATIC_ASSERT(sizeof (spec->efs_rem_host) ==
283                     MC_CMD_FILTER_OP_EXT_IN_SRC_IP_LEN);
284                 EFX_STATIC_ASSERT(sizeof (spec->efs_loc_host) ==
285                     MC_CMD_FILTER_OP_EXT_IN_DST_IP_LEN);
286
287                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_SRC_IP),
288                     &spec->efs_rem_host.eo_byte[0],
289                     MC_CMD_FILTER_OP_EXT_IN_SRC_IP_LEN);
290                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_DST_IP),
291                     &spec->efs_loc_host.eo_byte[0],
292                     MC_CMD_FILTER_OP_EXT_IN_DST_IP_LEN);
293
294                 /*
295                  * On Medford, filters for encapsulated packets match based on
296                  * the ether type and IP protocol in the outer frame.  In
297                  * addition we need to fill in the VNI or VSID type field.
298                  */
299                 switch (spec->efs_encap_type) {
300                 case EFX_TUNNEL_PROTOCOL_NONE:
301                         break;
302                 case EFX_TUNNEL_PROTOCOL_VXLAN:
303                 case EFX_TUNNEL_PROTOCOL_GENEVE:
304                         MCDI_IN_POPULATE_DWORD_1(req,
305                             FILTER_OP_EXT_IN_VNI_OR_VSID,
306                             FILTER_OP_EXT_IN_VNI_TYPE,
307                             spec->efs_encap_type == EFX_TUNNEL_PROTOCOL_VXLAN ?
308                                     MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_VXLAN :
309                                     MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_GENEVE);
310                         break;
311                 case EFX_TUNNEL_PROTOCOL_NVGRE:
312                         MCDI_IN_POPULATE_DWORD_1(req,
313                             FILTER_OP_EXT_IN_VNI_OR_VSID,
314                             FILTER_OP_EXT_IN_VSID_TYPE,
315                             MC_CMD_FILTER_OP_EXT_IN_VSID_TYPE_NVGRE);
316                         break;
317                 default:
318                         EFSYS_ASSERT(0);
319                         rc = EINVAL;
320                         goto fail2;
321                 }
322         }
323
324         efx_mcdi_execute(enp, &req);
325
326         if (req.emr_rc != 0) {
327                 rc = req.emr_rc;
328                 goto fail3;
329         }
330
331         if (req.emr_out_length_used < MC_CMD_FILTER_OP_EXT_OUT_LEN) {
332                 rc = EMSGSIZE;
333                 goto fail4;
334         }
335
336         handle->efh_lo = MCDI_OUT_DWORD(req, FILTER_OP_EXT_OUT_HANDLE_LO);
337         handle->efh_hi = MCDI_OUT_DWORD(req, FILTER_OP_EXT_OUT_HANDLE_HI);
338
339         return (0);
340
341 fail4:
342         EFSYS_PROBE(fail4);
343 fail3:
344         EFSYS_PROBE(fail3);
345 fail2:
346         EFSYS_PROBE(fail2);
347 fail1:
348         EFSYS_PROBE1(fail1, efx_rc_t, rc);
349
350         return (rc);
351
352 }
353
354 static  __checkReturn   efx_rc_t
355 efx_mcdi_filter_op_delete(
356         __in            efx_nic_t *enp,
357         __in            unsigned int filter_op,
358         __inout         ef10_filter_handle_t *handle)
359 {
360         efx_mcdi_req_t req;
361         uint8_t payload[MAX(MC_CMD_FILTER_OP_EXT_IN_LEN,
362                             MC_CMD_FILTER_OP_EXT_OUT_LEN)];
363         efx_rc_t rc;
364
365         memset(payload, 0, sizeof (payload));
366         req.emr_cmd = MC_CMD_FILTER_OP;
367         req.emr_in_buf = payload;
368         req.emr_in_length = MC_CMD_FILTER_OP_EXT_IN_LEN;
369         req.emr_out_buf = payload;
370         req.emr_out_length = MC_CMD_FILTER_OP_EXT_OUT_LEN;
371
372         switch (filter_op) {
373         case MC_CMD_FILTER_OP_IN_OP_REMOVE:
374                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP,
375                     MC_CMD_FILTER_OP_IN_OP_REMOVE);
376                 break;
377         case MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE:
378                 MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP,
379                     MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
380                 break;
381         default:
382                 EFSYS_ASSERT(0);
383                 rc = EINVAL;
384                 goto fail1;
385         }
386
387         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_LO, handle->efh_lo);
388         MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_HI, handle->efh_hi);
389
390         efx_mcdi_execute_quiet(enp, &req);
391
392         if (req.emr_rc != 0) {
393                 rc = req.emr_rc;
394                 goto fail2;
395         }
396
397         if (req.emr_out_length_used < MC_CMD_FILTER_OP_EXT_OUT_LEN) {
398                 rc = EMSGSIZE;
399                 goto fail3;
400         }
401
402         return (0);
403
404 fail3:
405         EFSYS_PROBE(fail3);
406
407 fail2:
408         EFSYS_PROBE(fail2);
409 fail1:
410         EFSYS_PROBE1(fail1, efx_rc_t, rc);
411
412         return (rc);
413 }
414
415 static  __checkReturn   boolean_t
416 ef10_filter_equal(
417         __in            const efx_filter_spec_t *left,
418         __in            const efx_filter_spec_t *right)
419 {
420         /* FIXME: Consider rx vs tx filters (look at efs_flags) */
421         if (left->efs_match_flags != right->efs_match_flags)
422                 return (B_FALSE);
423         if (!EFX_OWORD_IS_EQUAL(left->efs_rem_host, right->efs_rem_host))
424                 return (B_FALSE);
425         if (!EFX_OWORD_IS_EQUAL(left->efs_loc_host, right->efs_loc_host))
426                 return (B_FALSE);
427         if (memcmp(left->efs_rem_mac, right->efs_rem_mac, EFX_MAC_ADDR_LEN))
428                 return (B_FALSE);
429         if (memcmp(left->efs_loc_mac, right->efs_loc_mac, EFX_MAC_ADDR_LEN))
430                 return (B_FALSE);
431         if (left->efs_rem_port != right->efs_rem_port)
432                 return (B_FALSE);
433         if (left->efs_loc_port != right->efs_loc_port)
434                 return (B_FALSE);
435         if (left->efs_inner_vid != right->efs_inner_vid)
436                 return (B_FALSE);
437         if (left->efs_outer_vid != right->efs_outer_vid)
438                 return (B_FALSE);
439         if (left->efs_ether_type != right->efs_ether_type)
440                 return (B_FALSE);
441         if (left->efs_ip_proto != right->efs_ip_proto)
442                 return (B_FALSE);
443         if (left->efs_encap_type != right->efs_encap_type)
444                 return (B_FALSE);
445
446         return (B_TRUE);
447
448 }
449
450 static  __checkReturn   boolean_t
451 ef10_filter_same_dest(
452         __in            const efx_filter_spec_t *left,
453         __in            const efx_filter_spec_t *right)
454 {
455         if ((left->efs_flags & EFX_FILTER_FLAG_RX_RSS) &&
456             (right->efs_flags & EFX_FILTER_FLAG_RX_RSS)) {
457                 if (left->efs_rss_context == right->efs_rss_context)
458                         return (B_TRUE);
459         } else if ((~(left->efs_flags) & EFX_FILTER_FLAG_RX_RSS) &&
460             (~(right->efs_flags) & EFX_FILTER_FLAG_RX_RSS)) {
461                 if (left->efs_dmaq_id == right->efs_dmaq_id)
462                         return (B_TRUE);
463         }
464         return (B_FALSE);
465 }
466
467 static  __checkReturn   uint32_t
468 ef10_filter_hash(
469         __in            efx_filter_spec_t *spec)
470 {
471         EFX_STATIC_ASSERT((sizeof (efx_filter_spec_t) % sizeof (uint32_t))
472                             == 0);
473         EFX_STATIC_ASSERT((EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid) %
474                             sizeof (uint32_t)) == 0);
475
476         /*
477          * As the area of the efx_filter_spec_t we need to hash is DWORD
478          * aligned and an exact number of DWORDs in size we can use the
479          * optimised efx_hash_dwords() rather than efx_hash_bytes()
480          */
481         return (efx_hash_dwords((const uint32_t *)&spec->efs_outer_vid,
482                         (sizeof (efx_filter_spec_t) -
483                         EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid)) /
484                         sizeof (uint32_t), 0));
485 }
486
487 /*
488  * Decide whether a filter should be exclusive or else should allow
489  * delivery to additional recipients.  Currently we decide that
490  * filters for specific local unicast MAC and IP addresses are
491  * exclusive.
492  */
493 static  __checkReturn   boolean_t
494 ef10_filter_is_exclusive(
495         __in            efx_filter_spec_t *spec)
496 {
497         if ((spec->efs_match_flags & EFX_FILTER_MATCH_LOC_MAC) &&
498             !EFX_MAC_ADDR_IS_MULTICAST(spec->efs_loc_mac))
499                 return (B_TRUE);
500
501         if ((spec->efs_match_flags &
502                 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
503             (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
504                 if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV4) &&
505                     ((spec->efs_loc_host.eo_u8[0] & 0xf) != 0xe))
506                         return (B_TRUE);
507                 if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV6) &&
508                     (spec->efs_loc_host.eo_u8[0] != 0xff))
509                         return (B_TRUE);
510         }
511
512         return (B_FALSE);
513 }
514
515         __checkReturn   efx_rc_t
516 ef10_filter_restore(
517         __in            efx_nic_t *enp)
518 {
519         int tbl_id;
520         efx_filter_spec_t *spec;
521         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
522         boolean_t restoring;
523         efsys_lock_state_t state;
524         efx_rc_t rc;
525
526         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
527             enp->en_family == EFX_FAMILY_MEDFORD ||
528             enp->en_family == EFX_FAMILY_MEDFORD2);
529
530         for (tbl_id = 0; tbl_id < EFX_EF10_FILTER_TBL_ROWS; tbl_id++) {
531
532                 EFSYS_LOCK(enp->en_eslp, state);
533
534                 spec = ef10_filter_entry_spec(eftp, tbl_id);
535                 if (spec == NULL) {
536                         restoring = B_FALSE;
537                 } else if (ef10_filter_entry_is_busy(eftp, tbl_id)) {
538                         /* Ignore busy entries. */
539                         restoring = B_FALSE;
540                 } else {
541                         ef10_filter_set_entry_busy(eftp, tbl_id);
542                         restoring = B_TRUE;
543                 }
544
545                 EFSYS_UNLOCK(enp->en_eslp, state);
546
547                 if (restoring == B_FALSE)
548                         continue;
549
550                 if (ef10_filter_is_exclusive(spec)) {
551                         rc = efx_mcdi_filter_op_add(enp, spec,
552                             MC_CMD_FILTER_OP_IN_OP_INSERT,
553                             &eftp->eft_entry[tbl_id].efe_handle);
554                 } else {
555                         rc = efx_mcdi_filter_op_add(enp, spec,
556                             MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
557                             &eftp->eft_entry[tbl_id].efe_handle);
558                 }
559
560                 if (rc != 0)
561                         goto fail1;
562
563                 EFSYS_LOCK(enp->en_eslp, state);
564
565                 ef10_filter_set_entry_not_busy(eftp, tbl_id);
566
567                 EFSYS_UNLOCK(enp->en_eslp, state);
568         }
569
570         return (0);
571
572 fail1:
573         EFSYS_PROBE1(fail1, efx_rc_t, rc);
574
575         return (rc);
576 }
577
578 /*
579  * An arbitrary search limit for the software hash table. As per the linux net
580  * driver.
581  */
582 #define EF10_FILTER_SEARCH_LIMIT 200
583
584 static  __checkReturn   efx_rc_t
585 ef10_filter_add_internal(
586         __in            efx_nic_t *enp,
587         __inout         efx_filter_spec_t *spec,
588         __in            boolean_t may_replace,
589         __out_opt       uint32_t *filter_id)
590 {
591         efx_rc_t rc;
592         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
593         efx_filter_spec_t *saved_spec;
594         uint32_t hash;
595         unsigned int depth;
596         int ins_index;
597         boolean_t replacing = B_FALSE;
598         unsigned int i;
599         efsys_lock_state_t state;
600         boolean_t locked = B_FALSE;
601
602         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
603             enp->en_family == EFX_FAMILY_MEDFORD ||
604             enp->en_family == EFX_FAMILY_MEDFORD2);
605
606         hash = ef10_filter_hash(spec);
607
608         /*
609          * FIXME: Add support for inserting filters of different priorities
610          * and removing lower priority multicast filters (bug 42378)
611          */
612
613         /*
614          * Find any existing filters with the same match tuple or
615          * else a free slot to insert at.  If any of them are busy,
616          * we have to wait and retry.
617          */
618         for (;;) {
619                 ins_index = -1;
620                 depth = 1;
621                 EFSYS_LOCK(enp->en_eslp, state);
622                 locked = B_TRUE;
623
624                 for (;;) {
625                         i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
626                         saved_spec = ef10_filter_entry_spec(eftp, i);
627
628                         if (!saved_spec) {
629                                 if (ins_index < 0) {
630                                         ins_index = i;
631                                 }
632                         } else if (ef10_filter_equal(spec, saved_spec)) {
633                                 if (ef10_filter_entry_is_busy(eftp, i))
634                                         break;
635                                 if (saved_spec->efs_priority
636                                             == EFX_FILTER_PRI_AUTO) {
637                                         ins_index = i;
638                                         goto found;
639                                 } else if (ef10_filter_is_exclusive(spec)) {
640                                         if (may_replace) {
641                                                 ins_index = i;
642                                                 goto found;
643                                         } else {
644                                                 rc = EEXIST;
645                                                 goto fail1;
646                                         }
647                                 }
648
649                                 /* Leave existing */
650                         }
651
652                         /*
653                          * Once we reach the maximum search depth, use
654                          * the first suitable slot or return EBUSY if
655                          * there was none.
656                          */
657                         if (depth == EF10_FILTER_SEARCH_LIMIT) {
658                                 if (ins_index < 0) {
659                                         rc = EBUSY;
660                                         goto fail2;
661                                 }
662                                 goto found;
663                         }
664                         depth++;
665                 }
666                 EFSYS_UNLOCK(enp->en_eslp, state);
667                 locked = B_FALSE;
668         }
669
670 found:
671         /*
672          * Create a software table entry if necessary, and mark it
673          * busy.  We might yet fail to insert, but any attempt to
674          * insert a conflicting filter while we're waiting for the
675          * firmware must find the busy entry.
676          */
677         saved_spec = ef10_filter_entry_spec(eftp, ins_index);
678         if (saved_spec) {
679                 if (saved_spec->efs_priority == EFX_FILTER_PRI_AUTO) {
680                         /* This is a filter we are refreshing */
681                         ef10_filter_set_entry_not_auto_old(eftp, ins_index);
682                         goto out_unlock;
683
684                 }
685                 replacing = B_TRUE;
686         } else {
687                 EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (*spec), saved_spec);
688                 if (!saved_spec) {
689                         rc = ENOMEM;
690                         goto fail3;
691                 }
692                 *saved_spec = *spec;
693                 ef10_filter_set_entry(eftp, ins_index, saved_spec);
694         }
695         ef10_filter_set_entry_busy(eftp, ins_index);
696
697         EFSYS_UNLOCK(enp->en_eslp, state);
698         locked = B_FALSE;
699
700         /*
701          * On replacing the filter handle may change after after a successful
702          * replace operation.
703          */
704         if (replacing) {
705                 rc = efx_mcdi_filter_op_add(enp, spec,
706                     MC_CMD_FILTER_OP_IN_OP_REPLACE,
707                     &eftp->eft_entry[ins_index].efe_handle);
708         } else if (ef10_filter_is_exclusive(spec)) {
709                 rc = efx_mcdi_filter_op_add(enp, spec,
710                     MC_CMD_FILTER_OP_IN_OP_INSERT,
711                     &eftp->eft_entry[ins_index].efe_handle);
712         } else {
713                 rc = efx_mcdi_filter_op_add(enp, spec,
714                     MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
715                     &eftp->eft_entry[ins_index].efe_handle);
716         }
717
718         if (rc != 0)
719                 goto fail4;
720
721         EFSYS_LOCK(enp->en_eslp, state);
722         locked = B_TRUE;
723
724         if (replacing) {
725                 /* Update the fields that may differ */
726                 saved_spec->efs_priority = spec->efs_priority;
727                 saved_spec->efs_flags = spec->efs_flags;
728                 saved_spec->efs_rss_context = spec->efs_rss_context;
729                 saved_spec->efs_dmaq_id = spec->efs_dmaq_id;
730         }
731
732         ef10_filter_set_entry_not_busy(eftp, ins_index);
733
734 out_unlock:
735
736         EFSYS_UNLOCK(enp->en_eslp, state);
737         locked = B_FALSE;
738
739         if (filter_id)
740                 *filter_id = ins_index;
741
742         return (0);
743
744 fail4:
745         EFSYS_PROBE(fail4);
746
747         if (!replacing) {
748                 EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), saved_spec);
749                 saved_spec = NULL;
750         }
751         ef10_filter_set_entry_not_busy(eftp, ins_index);
752         ef10_filter_set_entry(eftp, ins_index, NULL);
753
754 fail3:
755         EFSYS_PROBE(fail3);
756
757 fail2:
758         EFSYS_PROBE(fail2);
759
760 fail1:
761         EFSYS_PROBE1(fail1, efx_rc_t, rc);
762
763         if (locked)
764                 EFSYS_UNLOCK(enp->en_eslp, state);
765
766         return (rc);
767 }
768
769         __checkReturn   efx_rc_t
770 ef10_filter_add(
771         __in            efx_nic_t *enp,
772         __inout         efx_filter_spec_t *spec,
773         __in            boolean_t may_replace)
774 {
775         efx_rc_t rc;
776
777         rc = ef10_filter_add_internal(enp, spec, may_replace, NULL);
778         if (rc != 0)
779                 goto fail1;
780
781         return (0);
782
783 fail1:
784         EFSYS_PROBE1(fail1, efx_rc_t, rc);
785
786         return (rc);
787 }
788
789
790 static  __checkReturn   efx_rc_t
791 ef10_filter_delete_internal(
792         __in            efx_nic_t *enp,
793         __in            uint32_t filter_id)
794 {
795         efx_rc_t rc;
796         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
797         efx_filter_spec_t *spec;
798         efsys_lock_state_t state;
799         uint32_t filter_idx = filter_id % EFX_EF10_FILTER_TBL_ROWS;
800
801         /*
802          * Find the software table entry and mark it busy.  Don't
803          * remove it yet; any attempt to update while we're waiting
804          * for the firmware must find the busy entry.
805          *
806          * FIXME: What if the busy flag is never cleared?
807          */
808         EFSYS_LOCK(enp->en_eslp, state);
809         while (ef10_filter_entry_is_busy(table, filter_idx)) {
810                 EFSYS_UNLOCK(enp->en_eslp, state);
811                 EFSYS_SPIN(1);
812                 EFSYS_LOCK(enp->en_eslp, state);
813         }
814         if ((spec = ef10_filter_entry_spec(table, filter_idx)) != NULL) {
815                 ef10_filter_set_entry_busy(table, filter_idx);
816         }
817         EFSYS_UNLOCK(enp->en_eslp, state);
818
819         if (spec == NULL) {
820                 rc = ENOENT;
821                 goto fail1;
822         }
823
824         /*
825          * Try to remove the hardware filter. This may fail if the MC has
826          * rebooted (which frees all hardware filter resources).
827          */
828         if (ef10_filter_is_exclusive(spec)) {
829                 rc = efx_mcdi_filter_op_delete(enp,
830                     MC_CMD_FILTER_OP_IN_OP_REMOVE,
831                     &table->eft_entry[filter_idx].efe_handle);
832         } else {
833                 rc = efx_mcdi_filter_op_delete(enp,
834                     MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE,
835                     &table->eft_entry[filter_idx].efe_handle);
836         }
837
838         /* Free the software table entry */
839         EFSYS_LOCK(enp->en_eslp, state);
840         ef10_filter_set_entry_not_busy(table, filter_idx);
841         ef10_filter_set_entry(table, filter_idx, NULL);
842         EFSYS_UNLOCK(enp->en_eslp, state);
843
844         EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), spec);
845
846         /* Check result of hardware filter removal */
847         if (rc != 0)
848                 goto fail2;
849
850         return (0);
851
852 fail2:
853         EFSYS_PROBE(fail2);
854
855 fail1:
856         EFSYS_PROBE1(fail1, efx_rc_t, rc);
857
858         return (rc);
859 }
860
861         __checkReturn   efx_rc_t
862 ef10_filter_delete(
863         __in            efx_nic_t *enp,
864         __inout         efx_filter_spec_t *spec)
865 {
866         efx_rc_t rc;
867         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
868         efx_filter_spec_t *saved_spec;
869         unsigned int hash;
870         unsigned int depth;
871         unsigned int i;
872         efsys_lock_state_t state;
873         boolean_t locked = B_FALSE;
874
875         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
876             enp->en_family == EFX_FAMILY_MEDFORD ||
877             enp->en_family == EFX_FAMILY_MEDFORD2);
878
879         hash = ef10_filter_hash(spec);
880
881         EFSYS_LOCK(enp->en_eslp, state);
882         locked = B_TRUE;
883
884         depth = 1;
885         for (;;) {
886                 i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
887                 saved_spec = ef10_filter_entry_spec(table, i);
888                 if (saved_spec && ef10_filter_equal(spec, saved_spec) &&
889                     ef10_filter_same_dest(spec, saved_spec)) {
890                         break;
891                 }
892                 if (depth == EF10_FILTER_SEARCH_LIMIT) {
893                         rc = ENOENT;
894                         goto fail1;
895                 }
896                 depth++;
897         }
898
899         EFSYS_UNLOCK(enp->en_eslp, state);
900         locked = B_FALSE;
901
902         rc = ef10_filter_delete_internal(enp, i);
903         if (rc != 0)
904                 goto fail2;
905
906         return (0);
907
908 fail2:
909         EFSYS_PROBE(fail2);
910
911 fail1:
912         EFSYS_PROBE1(fail1, efx_rc_t, rc);
913
914         if (locked)
915                 EFSYS_UNLOCK(enp->en_eslp, state);
916
917         return (rc);
918 }
919
920 static  __checkReturn   efx_rc_t
921 efx_mcdi_get_parser_disp_info(
922         __in                            efx_nic_t *enp,
923         __out_ecount(buffer_length)     uint32_t *buffer,
924         __in                            size_t buffer_length,
925         __out                           size_t *list_lengthp)
926 {
927         efx_mcdi_req_t req;
928         uint8_t payload[MAX(MC_CMD_GET_PARSER_DISP_INFO_IN_LEN,
929                             MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX)];
930         size_t matches_count;
931         size_t list_size;
932         efx_rc_t rc;
933
934         (void) memset(payload, 0, sizeof (payload));
935         req.emr_cmd = MC_CMD_GET_PARSER_DISP_INFO;
936         req.emr_in_buf = payload;
937         req.emr_in_length = MC_CMD_GET_PARSER_DISP_INFO_IN_LEN;
938         req.emr_out_buf = payload;
939         req.emr_out_length = MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX;
940
941         MCDI_IN_SET_DWORD(req, GET_PARSER_DISP_INFO_OUT_OP,
942             MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
943
944         efx_mcdi_execute(enp, &req);
945
946         if (req.emr_rc != 0) {
947                 rc = req.emr_rc;
948                 goto fail1;
949         }
950
951         matches_count = MCDI_OUT_DWORD(req,
952             GET_PARSER_DISP_INFO_OUT_NUM_SUPPORTED_MATCHES);
953
954         if (req.emr_out_length_used <
955             MC_CMD_GET_PARSER_DISP_INFO_OUT_LEN(matches_count)) {
956                 rc = EMSGSIZE;
957                 goto fail2;
958         }
959
960         *list_lengthp = matches_count;
961
962         if (buffer_length < matches_count) {
963                 rc = ENOSPC;
964                 goto fail3;
965         }
966
967         /*
968          * Check that the elements in the list in the MCDI response are the size
969          * we expect, so we can just copy them directly. Any conversion of the
970          * flags is handled by the caller.
971          */
972         EFX_STATIC_ASSERT(sizeof (uint32_t) ==
973             MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN);
974
975         list_size = matches_count *
976                 MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN;
977         memcpy(buffer,
978             MCDI_OUT2(req, uint32_t,
979                     GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES),
980             list_size);
981
982         return (0);
983
984 fail3:
985         EFSYS_PROBE(fail3);
986 fail2:
987         EFSYS_PROBE(fail2);
988 fail1:
989         EFSYS_PROBE1(fail1, efx_rc_t, rc);
990
991         return (rc);
992 }
993
994         __checkReturn   efx_rc_t
995 ef10_filter_supported_filters(
996         __in                            efx_nic_t *enp,
997         __out_ecount(buffer_length)     uint32_t *buffer,
998         __in                            size_t buffer_length,
999         __out                           size_t *list_lengthp)
1000 {
1001
1002         size_t mcdi_list_length;
1003         size_t list_length;
1004         uint32_t i;
1005         efx_rc_t rc;
1006         efx_filter_match_flags_t all_filter_flags =
1007             (EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_LOC_HOST |
1008             EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_REM_PORT |
1009             EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_PORT |
1010             EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_INNER_VID |
1011             EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_IP_PROTO |
1012             EFX_FILTER_MATCH_UNKNOWN_MCAST_DST |
1013             EFX_FILTER_MATCH_UNKNOWN_UCAST_DST);
1014
1015         rc = efx_mcdi_get_parser_disp_info(enp, buffer, buffer_length,
1016                                             &mcdi_list_length);
1017         if (rc != 0) {
1018                 if (rc == ENOSPC) {
1019                         /* Pass through mcdi_list_length for the list length */
1020                         *list_lengthp = mcdi_list_length;
1021                 }
1022                 goto fail1;
1023         }
1024
1025         /*
1026          * The static assertions in ef10_filter_init() ensure that the values of
1027          * the EFX_FILTER_MATCH flags match those used by MCDI, so they don't
1028          * need to be converted.
1029          *
1030          * In case support is added to MCDI for additional flags, remove any
1031          * matches from the list which include flags we don't support. The order
1032          * of the matches is preserved as they are ordered from highest to
1033          * lowest priority.
1034          */
1035         EFSYS_ASSERT(mcdi_list_length <= buffer_length);
1036         list_length = 0;
1037         for (i = 0; i < mcdi_list_length; i++) {
1038                 if ((buffer[i] & ~all_filter_flags) == 0) {
1039                         buffer[list_length] = buffer[i];
1040                         list_length++;
1041                 }
1042         }
1043
1044         *list_lengthp = list_length;
1045
1046         return (0);
1047
1048 fail1:
1049         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1050
1051         return (rc);
1052 }
1053
1054 static  __checkReturn   efx_rc_t
1055 ef10_filter_insert_unicast(
1056         __in                            efx_nic_t *enp,
1057         __in_ecount(6)                  uint8_t const *addr,
1058         __in                            efx_filter_flags_t filter_flags)
1059 {
1060         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1061         efx_filter_spec_t spec;
1062         efx_rc_t rc;
1063
1064         /* Insert the filter for the local station address */
1065         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1066             filter_flags,
1067             eftp->eft_default_rxq);
1068         efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC, addr);
1069
1070         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1071             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
1072         if (rc != 0)
1073                 goto fail1;
1074
1075         eftp->eft_unicst_filter_count++;
1076         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
1077                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
1078
1079         return (0);
1080
1081 fail1:
1082         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1083         return (rc);
1084 }
1085
1086 static  __checkReturn   efx_rc_t
1087 ef10_filter_insert_all_unicast(
1088         __in                            efx_nic_t *enp,
1089         __in                            efx_filter_flags_t filter_flags)
1090 {
1091         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1092         efx_filter_spec_t spec;
1093         efx_rc_t rc;
1094
1095         /* Insert the unknown unicast filter */
1096         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1097             filter_flags,
1098             eftp->eft_default_rxq);
1099         efx_filter_spec_set_uc_def(&spec);
1100         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1101             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
1102         if (rc != 0)
1103                 goto fail1;
1104
1105         eftp->eft_unicst_filter_count++;
1106         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
1107                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
1108
1109         return (0);
1110
1111 fail1:
1112         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1113         return (rc);
1114 }
1115
1116 static  __checkReturn   efx_rc_t
1117 ef10_filter_insert_multicast_list(
1118         __in                            efx_nic_t *enp,
1119         __in                            boolean_t mulcst,
1120         __in                            boolean_t brdcst,
1121         __in_ecount(6*count)            uint8_t const *addrs,
1122         __in                            uint32_t count,
1123         __in                            efx_filter_flags_t filter_flags,
1124         __in                            boolean_t rollback)
1125 {
1126         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1127         efx_filter_spec_t spec;
1128         uint8_t addr[6];
1129         uint32_t i;
1130         uint32_t filter_index;
1131         uint32_t filter_count;
1132         efx_rc_t rc;
1133
1134         if (mulcst == B_FALSE)
1135                 count = 0;
1136
1137         if (count + (brdcst ? 1 : 0) >
1138             EFX_ARRAY_SIZE(eftp->eft_mulcst_filter_indexes)) {
1139                 /* Too many MAC addresses */
1140                 rc = EINVAL;
1141                 goto fail1;
1142         }
1143
1144         /* Insert/renew multicast address list filters */
1145         filter_count = 0;
1146         for (i = 0; i < count; i++) {
1147                 efx_filter_spec_init_rx(&spec,
1148                     EFX_FILTER_PRI_AUTO,
1149                     filter_flags,
1150                     eftp->eft_default_rxq);
1151
1152                 efx_filter_spec_set_eth_local(&spec,
1153                     EFX_FILTER_SPEC_VID_UNSPEC,
1154                     &addrs[i * EFX_MAC_ADDR_LEN]);
1155
1156                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1157                                             &filter_index);
1158
1159                 if (rc == 0) {
1160                         eftp->eft_mulcst_filter_indexes[filter_count] =
1161                                 filter_index;
1162                         filter_count++;
1163                 } else if (rollback == B_TRUE) {
1164                         /* Only stop upon failure if told to rollback */
1165                         goto rollback;
1166                 }
1167
1168         }
1169
1170         if (brdcst == B_TRUE) {
1171                 /* Insert/renew broadcast address filter */
1172                 efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1173                     filter_flags,
1174                     eftp->eft_default_rxq);
1175
1176                 EFX_MAC_BROADCAST_ADDR_SET(addr);
1177                 efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC,
1178                     addr);
1179
1180                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1181                                             &filter_index);
1182
1183                 if (rc == 0) {
1184                         eftp->eft_mulcst_filter_indexes[filter_count] =
1185                                 filter_index;
1186                         filter_count++;
1187                 } else if (rollback == B_TRUE) {
1188                         /* Only stop upon failure if told to rollback */
1189                         goto rollback;
1190                 }
1191         }
1192
1193         eftp->eft_mulcst_filter_count = filter_count;
1194         eftp->eft_using_all_mulcst = B_FALSE;
1195
1196         return (0);
1197
1198 rollback:
1199         /* Remove any filters we have inserted */
1200         i = filter_count;
1201         while (i--) {
1202                 (void) ef10_filter_delete_internal(enp,
1203                     eftp->eft_mulcst_filter_indexes[i]);
1204         }
1205         eftp->eft_mulcst_filter_count = 0;
1206
1207 fail1:
1208         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1209
1210         return (rc);
1211 }
1212
1213 static  __checkReturn   efx_rc_t
1214 ef10_filter_insert_all_multicast(
1215         __in                            efx_nic_t *enp,
1216         __in                            efx_filter_flags_t filter_flags)
1217 {
1218         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1219         efx_filter_spec_t spec;
1220         efx_rc_t rc;
1221
1222         /* Insert the unknown multicast filter */
1223         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1224             filter_flags,
1225             eftp->eft_default_rxq);
1226         efx_filter_spec_set_mc_def(&spec);
1227
1228         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1229             &eftp->eft_mulcst_filter_indexes[0]);
1230         if (rc != 0)
1231                 goto fail1;
1232
1233         eftp->eft_mulcst_filter_count = 1;
1234         eftp->eft_using_all_mulcst = B_TRUE;
1235
1236         /*
1237          * FIXME: If brdcst == B_FALSE, add a filter to drop broadcast traffic.
1238          */
1239
1240         return (0);
1241
1242 fail1:
1243         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1244
1245         return (rc);
1246 }
1247
1248 typedef struct ef10_filter_encap_entry_s {
1249         uint16_t                ether_type;
1250         efx_tunnel_protocol_t   encap_type;
1251         uint32_t                inner_frame_match;
1252 } ef10_filter_encap_entry_t;
1253
1254 #define EF10_ENCAP_FILTER_ENTRY(ipv, encap_type, inner_frame_match)     \
1255         { EFX_ETHER_TYPE_##ipv, EFX_TUNNEL_PROTOCOL_##encap_type,       \
1256             EFX_FILTER_INNER_FRAME_MATCH_UNKNOWN_##inner_frame_match }
1257
1258 static ef10_filter_encap_entry_t ef10_filter_encap_list[] = {
1259         EF10_ENCAP_FILTER_ENTRY(IPV4, VXLAN, UCAST_DST),
1260         EF10_ENCAP_FILTER_ENTRY(IPV4, VXLAN, MCAST_DST),
1261         EF10_ENCAP_FILTER_ENTRY(IPV6, VXLAN, UCAST_DST),
1262         EF10_ENCAP_FILTER_ENTRY(IPV6, VXLAN, MCAST_DST),
1263
1264         EF10_ENCAP_FILTER_ENTRY(IPV4, GENEVE, UCAST_DST),
1265         EF10_ENCAP_FILTER_ENTRY(IPV4, GENEVE, MCAST_DST),
1266         EF10_ENCAP_FILTER_ENTRY(IPV6, GENEVE, UCAST_DST),
1267         EF10_ENCAP_FILTER_ENTRY(IPV6, GENEVE, MCAST_DST),
1268
1269         EF10_ENCAP_FILTER_ENTRY(IPV4, NVGRE, UCAST_DST),
1270         EF10_ENCAP_FILTER_ENTRY(IPV4, NVGRE, MCAST_DST),
1271         EF10_ENCAP_FILTER_ENTRY(IPV6, NVGRE, UCAST_DST),
1272         EF10_ENCAP_FILTER_ENTRY(IPV6, NVGRE, MCAST_DST),
1273 };
1274
1275 #undef EF10_ENCAP_FILTER_ENTRY
1276
1277 static  __checkReturn   efx_rc_t
1278 ef10_filter_insert_encap_filters(
1279         __in            efx_nic_t *enp,
1280         __in            boolean_t mulcst,
1281         __in            efx_filter_flags_t filter_flags)
1282 {
1283         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1284         uint32_t i;
1285         efx_rc_t rc;
1286
1287         EFX_STATIC_ASSERT(EFX_ARRAY_SIZE(ef10_filter_encap_list) <=
1288                             EFX_ARRAY_SIZE(table->eft_encap_filter_indexes));
1289
1290         /*
1291          * On Medford, full-featured firmware can identify packets as being
1292          * tunnel encapsulated, even if no encapsulated packet offloads are in
1293          * use. When packets are identified as such, ordinary filters are not
1294          * applied, only ones specific to encapsulated packets. Hence we need to
1295          * insert filters for encapsulated packets in order to receive them.
1296          *
1297          * Separate filters need to be inserted for each ether type,
1298          * encapsulation type, and inner frame type (unicast or multicast). To
1299          * keep things simple and reduce the number of filters needed, catch-all
1300          * filters for all combinations of types are inserted, even if
1301          * all_unicst or all_mulcst have not been set. (These catch-all filters
1302          * may well, however, fail to insert on unprivileged functions.)
1303          */
1304         table->eft_encap_filter_count = 0;
1305         for (i = 0; i < EFX_ARRAY_SIZE(ef10_filter_encap_list); i++) {
1306                 efx_filter_spec_t spec;
1307                 ef10_filter_encap_entry_t *encap_filter =
1308                         &ef10_filter_encap_list[i];
1309
1310                 /*
1311                  * Skip multicast filters if we've not been asked for
1312                  * any multicast traffic.
1313                  */
1314                 if ((mulcst == B_FALSE) &&
1315                     (encap_filter->inner_frame_match ==
1316                     EFX_FILTER_INNER_FRAME_MATCH_UNKNOWN_MCAST_DST))
1317                         continue;
1318
1319                 efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1320                                         filter_flags,
1321                                         table->eft_default_rxq);
1322                 efx_filter_spec_set_ether_type(&spec, encap_filter->ether_type);
1323                 rc = efx_filter_spec_set_encap_type(&spec,
1324                                             encap_filter->encap_type,
1325                                             encap_filter->inner_frame_match);
1326                 if (rc != 0)
1327                         goto fail1;
1328
1329                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1330                             &table->eft_encap_filter_indexes[
1331                                     table->eft_encap_filter_count]);
1332                 if (rc != 0) {
1333                         if (rc != EACCES)
1334                                 goto fail2;
1335                 } else {
1336                         table->eft_encap_filter_count++;
1337                 }
1338         }
1339
1340         return (0);
1341
1342 fail2:
1343         EFSYS_PROBE(fail2);
1344 fail1:
1345         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1346
1347         return (rc);
1348 }
1349
1350 static                  void
1351 ef10_filter_remove_old(
1352         __in            efx_nic_t *enp)
1353 {
1354         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1355         uint32_t i;
1356
1357         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1358                 if (ef10_filter_entry_is_auto_old(table, i)) {
1359                         (void) ef10_filter_delete_internal(enp, i);
1360                 }
1361         }
1362 }
1363
1364
1365 static  __checkReturn   efx_rc_t
1366 ef10_filter_get_workarounds(
1367         __in                            efx_nic_t *enp)
1368 {
1369         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1370         uint32_t implemented = 0;
1371         uint32_t enabled = 0;
1372         efx_rc_t rc;
1373
1374         rc = efx_mcdi_get_workarounds(enp, &implemented, &enabled);
1375         if (rc == 0) {
1376                 /* Check if chained multicast filter support is enabled */
1377                 if (implemented & enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807)
1378                         encp->enc_bug26807_workaround = B_TRUE;
1379                 else
1380                         encp->enc_bug26807_workaround = B_FALSE;
1381         } else if (rc == ENOTSUP) {
1382                 /*
1383                  * Firmware is too old to support GET_WORKAROUNDS, and support
1384                  * for this workaround was implemented later.
1385                  */
1386                 encp->enc_bug26807_workaround = B_FALSE;
1387         } else {
1388                 goto fail1;
1389         }
1390
1391         return (0);
1392
1393 fail1:
1394         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1395
1396         return (rc);
1397
1398 }
1399
1400
1401 /*
1402  * Reconfigure all filters.
1403  * If all_unicst and/or all mulcst filters cannot be applied then
1404  * return ENOTSUP (Note the filters for the specified addresses are
1405  * still applied in this case).
1406  */
1407         __checkReturn   efx_rc_t
1408 ef10_filter_reconfigure(
1409         __in                            efx_nic_t *enp,
1410         __in_ecount(6)                  uint8_t const *mac_addr,
1411         __in                            boolean_t all_unicst,
1412         __in                            boolean_t mulcst,
1413         __in                            boolean_t all_mulcst,
1414         __in                            boolean_t brdcst,
1415         __in_ecount(6*count)            uint8_t const *addrs,
1416         __in                            uint32_t count)
1417 {
1418         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1419         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1420         efx_filter_flags_t filter_flags;
1421         unsigned int i;
1422         efx_rc_t all_unicst_rc = 0;
1423         efx_rc_t all_mulcst_rc = 0;
1424         efx_rc_t rc;
1425
1426         if (table->eft_default_rxq == NULL) {
1427                 /*
1428                  * Filters direct traffic to the default RXQ, and so cannot be
1429                  * inserted until it is available. Any currently configured
1430                  * filters must be removed (ignore errors in case the MC
1431                  * has rebooted, which removes hardware filters).
1432                  */
1433                 for (i = 0; i < table->eft_unicst_filter_count; i++) {
1434                         (void) ef10_filter_delete_internal(enp,
1435                                         table->eft_unicst_filter_indexes[i]);
1436                 }
1437                 table->eft_unicst_filter_count = 0;
1438
1439                 for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1440                         (void) ef10_filter_delete_internal(enp,
1441                                         table->eft_mulcst_filter_indexes[i]);
1442                 }
1443                 table->eft_mulcst_filter_count = 0;
1444
1445                 for (i = 0; i < table->eft_encap_filter_count; i++) {
1446                         (void) ef10_filter_delete_internal(enp,
1447                                         table->eft_encap_filter_indexes[i]);
1448                 }
1449                 table->eft_encap_filter_count = 0;
1450
1451                 return (0);
1452         }
1453
1454         if (table->eft_using_rss)
1455                 filter_flags = EFX_FILTER_FLAG_RX_RSS;
1456         else
1457                 filter_flags = 0;
1458
1459         /* Mark old filters which may need to be removed */
1460         for (i = 0; i < table->eft_unicst_filter_count; i++) {
1461                 ef10_filter_set_entry_auto_old(table,
1462                                         table->eft_unicst_filter_indexes[i]);
1463         }
1464         for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1465                 ef10_filter_set_entry_auto_old(table,
1466                                         table->eft_mulcst_filter_indexes[i]);
1467         }
1468         for (i = 0; i < table->eft_encap_filter_count; i++) {
1469                 ef10_filter_set_entry_auto_old(table,
1470                                         table->eft_encap_filter_indexes[i]);
1471         }
1472
1473         /*
1474          * Insert or renew unicast filters.
1475          *
1476          * Frimware does not perform chaining on unicast filters. As traffic is
1477          * therefore only delivered to the first matching filter, we should
1478          * always insert the specific filter for our MAC address, to try and
1479          * ensure we get that traffic.
1480          *
1481          * (If the filter for our MAC address has already been inserted by
1482          * another function, we won't receive traffic sent to us, even if we
1483          * insert a unicast mismatch filter. To prevent traffic stealing, this
1484          * therefore relies on the privilege model only allowing functions to
1485          * insert filters for their own MAC address unless explicitly given
1486          * additional privileges by the user. This also means that, even on a
1487          * priviliged function, inserting a unicast mismatch filter may not
1488          * catch all traffic in multi PCI function scenarios.)
1489          */
1490         table->eft_unicst_filter_count = 0;
1491         rc = ef10_filter_insert_unicast(enp, mac_addr, filter_flags);
1492         if (all_unicst || (rc != 0)) {
1493                 all_unicst_rc = ef10_filter_insert_all_unicast(enp,
1494                                                     filter_flags);
1495                 if ((rc != 0) && (all_unicst_rc != 0))
1496                         goto fail1;
1497         }
1498
1499         /*
1500          * WORKAROUND_BUG26807 controls firmware support for chained multicast
1501          * filters, and can only be enabled or disabled when the hardware filter
1502          * table is empty.
1503          *
1504          * Chained multicast filters require support from the datapath firmware,
1505          * and may not be available (e.g. low-latency variants or old Huntington
1506          * firmware).
1507          *
1508          * Firmware will reset (FLR) functions which have inserted filters in
1509          * the hardware filter table when the workaround is enabled/disabled.
1510          * Functions without any hardware filters are not reset.
1511          *
1512          * Re-check if the workaround is enabled after adding unicast hardware
1513          * filters. This ensures that encp->enc_bug26807_workaround matches the
1514          * firmware state, and that later changes to enable/disable the
1515          * workaround will result in this function seeing a reset (FLR).
1516          *
1517          * In common-code drivers, we only support multiple PCI function
1518          * scenarios with firmware that supports multicast chaining, so we can
1519          * assume it is enabled for such cases and hence simplify the filter
1520          * insertion logic. Firmware that does not support multicast chaining
1521          * does not support multiple PCI function configurations either, so
1522          * filter insertion is much simpler and the same strategies can still be
1523          * used.
1524          */
1525         if ((rc = ef10_filter_get_workarounds(enp)) != 0)
1526                 goto fail2;
1527
1528         if ((table->eft_using_all_mulcst != all_mulcst) &&
1529             (encp->enc_bug26807_workaround == B_TRUE)) {
1530                 /*
1531                  * Multicast filter chaining is enabled, so traffic that matches
1532                  * more than one multicast filter will be replicated and
1533                  * delivered to multiple recipients.  To avoid this duplicate
1534                  * delivery, remove old multicast filters before inserting new
1535                  * multicast filters.
1536                  */
1537                 ef10_filter_remove_old(enp);
1538         }
1539
1540         /* Insert or renew multicast filters */
1541         if (all_mulcst == B_TRUE) {
1542                 /*
1543                  * Insert the all multicast filter. If that fails, try to insert
1544                  * all of our multicast filters (but without rollback on
1545                  * failure).
1546                  */
1547                 all_mulcst_rc = ef10_filter_insert_all_multicast(enp,
1548                                                             filter_flags);
1549                 if (all_mulcst_rc != 0) {
1550                         rc = ef10_filter_insert_multicast_list(enp, B_TRUE,
1551                             brdcst, addrs, count, filter_flags, B_FALSE);
1552                         if (rc != 0)
1553                                 goto fail3;
1554                 }
1555         } else {
1556                 /*
1557                  * Insert filters for multicast addresses.
1558                  * If any insertion fails, then rollback and try to insert the
1559                  * all multicast filter instead.
1560                  * If that also fails, try to insert all of the multicast
1561                  * filters (but without rollback on failure).
1562                  */
1563                 rc = ef10_filter_insert_multicast_list(enp, mulcst, brdcst,
1564                             addrs, count, filter_flags, B_TRUE);
1565                 if (rc != 0) {
1566                         if ((table->eft_using_all_mulcst == B_FALSE) &&
1567                             (encp->enc_bug26807_workaround == B_TRUE)) {
1568                                 /*
1569                                  * Multicast filter chaining is on, so remove
1570                                  * old filters before inserting the multicast
1571                                  * all filter to avoid duplicate delivery caused
1572                                  * by packets matching multiple filters.
1573                                  */
1574                                 ef10_filter_remove_old(enp);
1575                         }
1576
1577                         rc = ef10_filter_insert_all_multicast(enp,
1578                                                             filter_flags);
1579                         if (rc != 0) {
1580                                 rc = ef10_filter_insert_multicast_list(enp,
1581                                     mulcst, brdcst,
1582                                     addrs, count, filter_flags, B_FALSE);
1583                                 if (rc != 0)
1584                                         goto fail4;
1585                         }
1586                 }
1587         }
1588
1589         if (encp->enc_tunnel_encapsulations_supported != 0) {
1590                 /* Try to insert filters for encapsulated packets. */
1591                 (void) ef10_filter_insert_encap_filters(enp,
1592                                             mulcst || all_mulcst || brdcst,
1593                                             filter_flags);
1594         }
1595
1596         /* Remove old filters which were not renewed */
1597         ef10_filter_remove_old(enp);
1598
1599         /* report if any optional flags were rejected */
1600         if (((all_unicst != B_FALSE) && (all_unicst_rc != 0)) ||
1601             ((all_mulcst != B_FALSE) && (all_mulcst_rc != 0))) {
1602                 rc = ENOTSUP;
1603         }
1604
1605         return (rc);
1606
1607 fail4:
1608         EFSYS_PROBE(fail4);
1609 fail3:
1610         EFSYS_PROBE(fail3);
1611 fail2:
1612         EFSYS_PROBE(fail2);
1613 fail1:
1614         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1615
1616         /* Clear auto old flags */
1617         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1618                 if (ef10_filter_entry_is_auto_old(table, i)) {
1619                         ef10_filter_set_entry_not_auto_old(table, i);
1620                 }
1621         }
1622
1623         return (rc);
1624 }
1625
1626                 void
1627 ef10_filter_get_default_rxq(
1628         __in            efx_nic_t *enp,
1629         __out           efx_rxq_t **erpp,
1630         __out           boolean_t *using_rss)
1631 {
1632         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1633
1634         *erpp = table->eft_default_rxq;
1635         *using_rss = table->eft_using_rss;
1636 }
1637
1638
1639                 void
1640 ef10_filter_default_rxq_set(
1641         __in            efx_nic_t *enp,
1642         __in            efx_rxq_t *erp,
1643         __in            boolean_t using_rss)
1644 {
1645         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1646
1647 #if EFSYS_OPT_RX_SCALE
1648         EFSYS_ASSERT((using_rss == B_FALSE) ||
1649             (enp->en_rss_context != EF10_RSS_CONTEXT_INVALID));
1650         table->eft_using_rss = using_rss;
1651 #else
1652         EFSYS_ASSERT(using_rss == B_FALSE);
1653         table->eft_using_rss = B_FALSE;
1654 #endif
1655         table->eft_default_rxq = erp;
1656 }
1657
1658                 void
1659 ef10_filter_default_rxq_clear(
1660         __in            efx_nic_t *enp)
1661 {
1662         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1663
1664         table->eft_default_rxq = NULL;
1665         table->eft_using_rss = B_FALSE;
1666 }
1667
1668
1669 #endif /* EFSYS_OPT_FILTER */
1670
1671 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */