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