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