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