net/sfc/base: split local MAC I/G back into separate flags
[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_IN_MATCH_SRC_IP));
127         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_HOST ==
128             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_IP));
129         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_MAC ==
130             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_SRC_MAC));
131         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_PORT ==
132             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_SRC_PORT));
133         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_MAC ==
134             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_MAC));
135         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_PORT ==
136             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_PORT));
137         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_ETHER_TYPE ==
138             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_ETHER_TYPE));
139         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_INNER_VID ==
140             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_INNER_VLAN));
141         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_OUTER_VID ==
142             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_OUTER_VLAN));
143         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IP_PROTO ==
144             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_IP_PROTO));
145         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_UNKNOWN_MCAST_DST ==
146             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST));
147         EFX_STATIC_ASSERT((uint32_t)EFX_FILTER_MATCH_UNKNOWN_UCAST_DST ==
148             MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST));
149 #undef MATCH_MASK
150
151         EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (ef10_filter_table_t), eftp);
152
153         if (!eftp) {
154                 rc = ENOMEM;
155                 goto fail1;
156         }
157
158         enp->en_filter.ef_ef10_filter_table = eftp;
159
160         return (0);
161
162 fail1:
163         EFSYS_PROBE1(fail1, efx_rc_t, rc);
164
165         return (rc);
166 }
167
168                         void
169 ef10_filter_fini(
170         __in            efx_nic_t *enp)
171 {
172         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
173                     enp->en_family == EFX_FAMILY_MEDFORD);
174
175         if (enp->en_filter.ef_ef10_filter_table != NULL) {
176                 EFSYS_KMEM_FREE(enp->en_esip, sizeof (ef10_filter_table_t),
177                     enp->en_filter.ef_ef10_filter_table);
178         }
179 }
180
181 static  __checkReturn   efx_rc_t
182 efx_mcdi_filter_op_add(
183         __in            efx_nic_t *enp,
184         __in            efx_filter_spec_t *spec,
185         __in            unsigned int filter_op,
186         __inout         ef10_filter_handle_t *handle)
187 {
188         efx_mcdi_req_t req;
189         uint8_t payload[MAX(MC_CMD_FILTER_OP_IN_LEN,
190                             MC_CMD_FILTER_OP_OUT_LEN)];
191         efx_rc_t rc;
192
193         memset(payload, 0, sizeof (payload));
194         req.emr_cmd = MC_CMD_FILTER_OP;
195         req.emr_in_buf = payload;
196         req.emr_in_length = MC_CMD_FILTER_OP_IN_LEN;
197         req.emr_out_buf = payload;
198         req.emr_out_length = MC_CMD_FILTER_OP_OUT_LEN;
199
200         switch (filter_op) {
201         case MC_CMD_FILTER_OP_IN_OP_REPLACE:
202                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_LO,
203                     handle->efh_lo);
204                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_HI,
205                     handle->efh_hi);
206                 /* Fall through */
207         case MC_CMD_FILTER_OP_IN_OP_INSERT:
208         case MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE:
209                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP, filter_op);
210                 break;
211         default:
212                 EFSYS_ASSERT(0);
213                 rc = EINVAL;
214                 goto fail1;
215         }
216
217         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_PORT_ID,
218             EVB_PORT_ID_ASSIGNED);
219         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_MATCH_FIELDS,
220             spec->efs_match_flags);
221         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_DEST,
222             MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
223         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_QUEUE,
224             spec->efs_dmaq_id);
225         if (spec->efs_flags & EFX_FILTER_FLAG_RX_RSS) {
226                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_CONTEXT,
227                     spec->efs_rss_context);
228         }
229         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_MODE,
230             spec->efs_flags & EFX_FILTER_FLAG_RX_RSS ?
231             MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
232             MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
233         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_TX_DEST,
234             MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
235
236         if (filter_op != MC_CMD_FILTER_OP_IN_OP_REPLACE) {
237                 /*
238                  * NOTE: Unlike most MCDI requests, the filter fields
239                  * are presented in network (big endian) byte order.
240                  */
241                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_SRC_MAC),
242                     spec->efs_rem_mac, EFX_MAC_ADDR_LEN);
243                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_DST_MAC),
244                     spec->efs_loc_mac, EFX_MAC_ADDR_LEN);
245
246                 MCDI_IN_SET_WORD(req, FILTER_OP_IN_SRC_PORT,
247                     __CPU_TO_BE_16(spec->efs_rem_port));
248                 MCDI_IN_SET_WORD(req, FILTER_OP_IN_DST_PORT,
249                     __CPU_TO_BE_16(spec->efs_loc_port));
250
251                 MCDI_IN_SET_WORD(req, FILTER_OP_IN_ETHER_TYPE,
252                     __CPU_TO_BE_16(spec->efs_ether_type));
253
254                 MCDI_IN_SET_WORD(req, FILTER_OP_IN_INNER_VLAN,
255                     __CPU_TO_BE_16(spec->efs_inner_vid));
256                 MCDI_IN_SET_WORD(req, FILTER_OP_IN_OUTER_VLAN,
257                     __CPU_TO_BE_16(spec->efs_outer_vid));
258
259                 /* IP protocol (in low byte, high byte is zero) */
260                 MCDI_IN_SET_BYTE(req, FILTER_OP_IN_IP_PROTO,
261                     spec->efs_ip_proto);
262
263                 EFX_STATIC_ASSERT(sizeof (spec->efs_rem_host) ==
264                     MC_CMD_FILTER_OP_IN_SRC_IP_LEN);
265                 EFX_STATIC_ASSERT(sizeof (spec->efs_loc_host) ==
266                     MC_CMD_FILTER_OP_IN_DST_IP_LEN);
267
268                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_SRC_IP),
269                     &spec->efs_rem_host.eo_byte[0],
270                     MC_CMD_FILTER_OP_IN_SRC_IP_LEN);
271                 memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_DST_IP),
272                     &spec->efs_loc_host.eo_byte[0],
273                     MC_CMD_FILTER_OP_IN_DST_IP_LEN);
274         }
275
276         efx_mcdi_execute(enp, &req);
277
278         if (req.emr_rc != 0) {
279                 rc = req.emr_rc;
280                 goto fail2;
281         }
282
283         if (req.emr_out_length_used < MC_CMD_FILTER_OP_OUT_LEN) {
284                 rc = EMSGSIZE;
285                 goto fail3;
286         }
287
288         handle->efh_lo = MCDI_OUT_DWORD(req, FILTER_OP_OUT_HANDLE_LO);
289         handle->efh_hi = MCDI_OUT_DWORD(req, FILTER_OP_OUT_HANDLE_HI);
290
291         return (0);
292
293 fail3:
294         EFSYS_PROBE(fail3);
295 fail2:
296         EFSYS_PROBE(fail2);
297 fail1:
298         EFSYS_PROBE1(fail1, efx_rc_t, rc);
299
300         return (rc);
301
302 }
303
304 static  __checkReturn   efx_rc_t
305 efx_mcdi_filter_op_delete(
306         __in            efx_nic_t *enp,
307         __in            unsigned int filter_op,
308         __inout         ef10_filter_handle_t *handle)
309 {
310         efx_mcdi_req_t req;
311         uint8_t payload[MAX(MC_CMD_FILTER_OP_IN_LEN,
312                             MC_CMD_FILTER_OP_OUT_LEN)];
313         efx_rc_t rc;
314
315         memset(payload, 0, sizeof (payload));
316         req.emr_cmd = MC_CMD_FILTER_OP;
317         req.emr_in_buf = payload;
318         req.emr_in_length = MC_CMD_FILTER_OP_IN_LEN;
319         req.emr_out_buf = payload;
320         req.emr_out_length = MC_CMD_FILTER_OP_OUT_LEN;
321
322         switch (filter_op) {
323         case MC_CMD_FILTER_OP_IN_OP_REMOVE:
324                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP,
325                     MC_CMD_FILTER_OP_IN_OP_REMOVE);
326                 break;
327         case MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE:
328                 MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP,
329                     MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
330                 break;
331         default:
332                 EFSYS_ASSERT(0);
333                 rc = EINVAL;
334                 goto fail1;
335         }
336
337         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_LO, handle->efh_lo);
338         MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_HI, handle->efh_hi);
339
340         efx_mcdi_execute_quiet(enp, &req);
341
342         if (req.emr_rc != 0) {
343                 rc = req.emr_rc;
344                 goto fail2;
345         }
346
347         if (req.emr_out_length_used < MC_CMD_FILTER_OP_OUT_LEN) {
348                 rc = EMSGSIZE;
349                 goto fail3;
350         }
351
352         return (0);
353
354 fail3:
355         EFSYS_PROBE(fail3);
356
357 fail2:
358         EFSYS_PROBE(fail2);
359 fail1:
360         EFSYS_PROBE1(fail1, efx_rc_t, rc);
361
362         return (rc);
363 }
364
365 static  __checkReturn   boolean_t
366 ef10_filter_equal(
367         __in            const efx_filter_spec_t *left,
368         __in            const efx_filter_spec_t *right)
369 {
370         /* FIXME: Consider rx vs tx filters (look at efs_flags) */
371         if (left->efs_match_flags != right->efs_match_flags)
372                 return (B_FALSE);
373         if (!EFX_OWORD_IS_EQUAL(left->efs_rem_host, right->efs_rem_host))
374                 return (B_FALSE);
375         if (!EFX_OWORD_IS_EQUAL(left->efs_loc_host, right->efs_loc_host))
376                 return (B_FALSE);
377         if (memcmp(left->efs_rem_mac, right->efs_rem_mac, EFX_MAC_ADDR_LEN))
378                 return (B_FALSE);
379         if (memcmp(left->efs_loc_mac, right->efs_loc_mac, EFX_MAC_ADDR_LEN))
380                 return (B_FALSE);
381         if (left->efs_rem_port != right->efs_rem_port)
382                 return (B_FALSE);
383         if (left->efs_loc_port != right->efs_loc_port)
384                 return (B_FALSE);
385         if (left->efs_inner_vid != right->efs_inner_vid)
386                 return (B_FALSE);
387         if (left->efs_outer_vid != right->efs_outer_vid)
388                 return (B_FALSE);
389         if (left->efs_ether_type != right->efs_ether_type)
390                 return (B_FALSE);
391         if (left->efs_ip_proto != right->efs_ip_proto)
392                 return (B_FALSE);
393
394         return (B_TRUE);
395
396 }
397
398 static  __checkReturn   boolean_t
399 ef10_filter_same_dest(
400         __in            const efx_filter_spec_t *left,
401         __in            const efx_filter_spec_t *right)
402 {
403         if ((left->efs_flags & EFX_FILTER_FLAG_RX_RSS) &&
404             (right->efs_flags & EFX_FILTER_FLAG_RX_RSS)) {
405                 if (left->efs_rss_context == right->efs_rss_context)
406                         return (B_TRUE);
407         } else if ((~(left->efs_flags) & EFX_FILTER_FLAG_RX_RSS) &&
408             (~(right->efs_flags) & EFX_FILTER_FLAG_RX_RSS)) {
409                 if (left->efs_dmaq_id == right->efs_dmaq_id)
410                         return (B_TRUE);
411         }
412         return (B_FALSE);
413 }
414
415 static  __checkReturn   uint32_t
416 ef10_filter_hash(
417         __in            efx_filter_spec_t *spec)
418 {
419         EFX_STATIC_ASSERT((sizeof (efx_filter_spec_t) % sizeof (uint32_t))
420                             == 0);
421         EFX_STATIC_ASSERT((EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid) %
422                             sizeof (uint32_t)) == 0);
423
424         /*
425          * As the area of the efx_filter_spec_t we need to hash is DWORD
426          * aligned and an exact number of DWORDs in size we can use the
427          * optimised efx_hash_dwords() rather than efx_hash_bytes()
428          */
429         return (efx_hash_dwords((const uint32_t *)&spec->efs_outer_vid,
430                         (sizeof (efx_filter_spec_t) -
431                         EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid)) /
432                         sizeof (uint32_t), 0));
433 }
434
435 /*
436  * Decide whether a filter should be exclusive or else should allow
437  * delivery to additional recipients.  Currently we decide that
438  * filters for specific local unicast MAC and IP addresses are
439  * exclusive.
440  */
441 static  __checkReturn   boolean_t
442 ef10_filter_is_exclusive(
443         __in            efx_filter_spec_t *spec)
444 {
445         if ((spec->efs_match_flags & EFX_FILTER_MATCH_LOC_MAC) &&
446             !EFX_MAC_ADDR_IS_MULTICAST(spec->efs_loc_mac))
447                 return (B_TRUE);
448
449         if ((spec->efs_match_flags &
450                 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
451             (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
452                 if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV4) &&
453                     ((spec->efs_loc_host.eo_u8[0] & 0xf) != 0xe))
454                         return (B_TRUE);
455                 if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV6) &&
456                     (spec->efs_loc_host.eo_u8[0] != 0xff))
457                         return (B_TRUE);
458         }
459
460         return (B_FALSE);
461 }
462
463         __checkReturn   efx_rc_t
464 ef10_filter_restore(
465         __in            efx_nic_t *enp)
466 {
467         int tbl_id;
468         efx_filter_spec_t *spec;
469         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
470         boolean_t restoring;
471         efsys_lock_state_t state;
472         efx_rc_t rc;
473
474         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
475                     enp->en_family == EFX_FAMILY_MEDFORD);
476
477         for (tbl_id = 0; tbl_id < EFX_EF10_FILTER_TBL_ROWS; tbl_id++) {
478
479                 EFSYS_LOCK(enp->en_eslp, state);
480
481                 spec = ef10_filter_entry_spec(eftp, tbl_id);
482                 if (spec == NULL) {
483                         restoring = B_FALSE;
484                 } else if (ef10_filter_entry_is_busy(eftp, tbl_id)) {
485                         /* Ignore busy entries. */
486                         restoring = B_FALSE;
487                 } else {
488                         ef10_filter_set_entry_busy(eftp, tbl_id);
489                         restoring = B_TRUE;
490                 }
491
492                 EFSYS_UNLOCK(enp->en_eslp, state);
493
494                 if (restoring == B_FALSE)
495                         continue;
496
497                 if (ef10_filter_is_exclusive(spec)) {
498                         rc = efx_mcdi_filter_op_add(enp, spec,
499                             MC_CMD_FILTER_OP_IN_OP_INSERT,
500                             &eftp->eft_entry[tbl_id].efe_handle);
501                 } else {
502                         rc = efx_mcdi_filter_op_add(enp, spec,
503                             MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
504                             &eftp->eft_entry[tbl_id].efe_handle);
505                 }
506
507                 if (rc != 0)
508                         goto fail1;
509
510                 EFSYS_LOCK(enp->en_eslp, state);
511
512                 ef10_filter_set_entry_not_busy(eftp, tbl_id);
513
514                 EFSYS_UNLOCK(enp->en_eslp, state);
515         }
516
517         return (0);
518
519 fail1:
520         EFSYS_PROBE1(fail1, efx_rc_t, rc);
521
522         return (rc);
523 }
524
525 /*
526  * An arbitrary search limit for the software hash table. As per the linux net
527  * driver.
528  */
529 #define EF10_FILTER_SEARCH_LIMIT 200
530
531 static  __checkReturn   efx_rc_t
532 ef10_filter_add_internal(
533         __in            efx_nic_t *enp,
534         __inout         efx_filter_spec_t *spec,
535         __in            boolean_t may_replace,
536         __out_opt       uint32_t *filter_id)
537 {
538         efx_rc_t rc;
539         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
540         efx_filter_spec_t *saved_spec;
541         uint32_t hash;
542         unsigned int depth;
543         int ins_index;
544         boolean_t replacing = B_FALSE;
545         unsigned int i;
546         efsys_lock_state_t state;
547         boolean_t locked = B_FALSE;
548
549         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
550                     enp->en_family == EFX_FAMILY_MEDFORD);
551
552 #if EFSYS_OPT_RX_SCALE
553         spec->efs_rss_context = enp->en_rss_context;
554 #endif
555
556         hash = ef10_filter_hash(spec);
557
558         /*
559          * FIXME: Add support for inserting filters of different priorities
560          * and removing lower priority multicast filters (bug 42378)
561          */
562
563         /*
564          * Find any existing filters with the same match tuple or
565          * else a free slot to insert at.  If any of them are busy,
566          * we have to wait and retry.
567          */
568         for (;;) {
569                 ins_index = -1;
570                 depth = 1;
571                 EFSYS_LOCK(enp->en_eslp, state);
572                 locked = B_TRUE;
573
574                 for (;;) {
575                         i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
576                         saved_spec = ef10_filter_entry_spec(eftp, i);
577
578                         if (!saved_spec) {
579                                 if (ins_index < 0) {
580                                         ins_index = i;
581                                 }
582                         } else if (ef10_filter_equal(spec, saved_spec)) {
583                                 if (ef10_filter_entry_is_busy(eftp, i))
584                                         break;
585                                 if (saved_spec->efs_priority
586                                             == EFX_FILTER_PRI_AUTO) {
587                                         ins_index = i;
588                                         goto found;
589                                 } else if (ef10_filter_is_exclusive(spec)) {
590                                         if (may_replace) {
591                                                 ins_index = i;
592                                                 goto found;
593                                         } else {
594                                                 rc = EEXIST;
595                                                 goto fail1;
596                                         }
597                                 }
598
599                                 /* Leave existing */
600                         }
601
602                         /*
603                          * Once we reach the maximum search depth, use
604                          * the first suitable slot or return EBUSY if
605                          * there was none.
606                          */
607                         if (depth == EF10_FILTER_SEARCH_LIMIT) {
608                                 if (ins_index < 0) {
609                                         rc = EBUSY;
610                                         goto fail2;
611                                 }
612                                 goto found;
613                         }
614                         depth++;
615                 }
616                 EFSYS_UNLOCK(enp->en_eslp, state);
617                 locked = B_FALSE;
618         }
619
620 found:
621         /*
622          * Create a software table entry if necessary, and mark it
623          * busy.  We might yet fail to insert, but any attempt to
624          * insert a conflicting filter while we're waiting for the
625          * firmware must find the busy entry.
626          */
627         saved_spec = ef10_filter_entry_spec(eftp, ins_index);
628         if (saved_spec) {
629                 if (saved_spec->efs_priority == EFX_FILTER_PRI_AUTO) {
630                         /* This is a filter we are refreshing */
631                         ef10_filter_set_entry_not_auto_old(eftp, ins_index);
632                         goto out_unlock;
633
634                 }
635                 replacing = B_TRUE;
636         } else {
637                 EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (*spec), saved_spec);
638                 if (!saved_spec) {
639                         rc = ENOMEM;
640                         goto fail3;
641                 }
642                 *saved_spec = *spec;
643                 ef10_filter_set_entry(eftp, ins_index, saved_spec);
644         }
645         ef10_filter_set_entry_busy(eftp, ins_index);
646
647         EFSYS_UNLOCK(enp->en_eslp, state);
648         locked = B_FALSE;
649
650         /*
651          * On replacing the filter handle may change after after a successful
652          * replace operation.
653          */
654         if (replacing) {
655                 rc = efx_mcdi_filter_op_add(enp, spec,
656                     MC_CMD_FILTER_OP_IN_OP_REPLACE,
657                     &eftp->eft_entry[ins_index].efe_handle);
658         } else if (ef10_filter_is_exclusive(spec)) {
659                 rc = efx_mcdi_filter_op_add(enp, spec,
660                     MC_CMD_FILTER_OP_IN_OP_INSERT,
661                     &eftp->eft_entry[ins_index].efe_handle);
662         } else {
663                 rc = efx_mcdi_filter_op_add(enp, spec,
664                     MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
665                     &eftp->eft_entry[ins_index].efe_handle);
666         }
667
668         if (rc != 0)
669                 goto fail4;
670
671         EFSYS_LOCK(enp->en_eslp, state);
672         locked = B_TRUE;
673
674         if (replacing) {
675                 /* Update the fields that may differ */
676                 saved_spec->efs_priority = spec->efs_priority;
677                 saved_spec->efs_flags = spec->efs_flags;
678                 saved_spec->efs_rss_context = spec->efs_rss_context;
679                 saved_spec->efs_dmaq_id = spec->efs_dmaq_id;
680         }
681
682         ef10_filter_set_entry_not_busy(eftp, ins_index);
683
684 out_unlock:
685
686         EFSYS_UNLOCK(enp->en_eslp, state);
687         locked = B_FALSE;
688
689         if (filter_id)
690                 *filter_id = ins_index;
691
692         return (0);
693
694 fail4:
695         EFSYS_PROBE(fail4);
696
697         if (!replacing) {
698                 EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), saved_spec);
699                 saved_spec = NULL;
700         }
701         ef10_filter_set_entry_not_busy(eftp, ins_index);
702         ef10_filter_set_entry(eftp, ins_index, NULL);
703
704 fail3:
705         EFSYS_PROBE(fail3);
706
707 fail2:
708         EFSYS_PROBE(fail2);
709
710 fail1:
711         EFSYS_PROBE1(fail1, efx_rc_t, rc);
712
713         if (locked)
714                 EFSYS_UNLOCK(enp->en_eslp, state);
715
716         return (rc);
717 }
718
719         __checkReturn   efx_rc_t
720 ef10_filter_add(
721         __in            efx_nic_t *enp,
722         __inout         efx_filter_spec_t *spec,
723         __in            boolean_t may_replace)
724 {
725         efx_rc_t rc;
726
727         rc = ef10_filter_add_internal(enp, spec, may_replace, NULL);
728         if (rc != 0)
729                 goto fail1;
730
731         return (0);
732
733 fail1:
734         EFSYS_PROBE1(fail1, efx_rc_t, rc);
735
736         return (rc);
737 }
738
739
740 static  __checkReturn   efx_rc_t
741 ef10_filter_delete_internal(
742         __in            efx_nic_t *enp,
743         __in            uint32_t filter_id)
744 {
745         efx_rc_t rc;
746         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
747         efx_filter_spec_t *spec;
748         efsys_lock_state_t state;
749         uint32_t filter_idx = filter_id % EFX_EF10_FILTER_TBL_ROWS;
750
751         /*
752          * Find the software table entry and mark it busy.  Don't
753          * remove it yet; any attempt to update while we're waiting
754          * for the firmware must find the busy entry.
755          *
756          * FIXME: What if the busy flag is never cleared?
757          */
758         EFSYS_LOCK(enp->en_eslp, state);
759         while (ef10_filter_entry_is_busy(table, filter_idx)) {
760                 EFSYS_UNLOCK(enp->en_eslp, state);
761                 EFSYS_SPIN(1);
762                 EFSYS_LOCK(enp->en_eslp, state);
763         }
764         if ((spec = ef10_filter_entry_spec(table, filter_idx)) != NULL) {
765                 ef10_filter_set_entry_busy(table, filter_idx);
766         }
767         EFSYS_UNLOCK(enp->en_eslp, state);
768
769         if (spec == NULL) {
770                 rc = ENOENT;
771                 goto fail1;
772         }
773
774         /*
775          * Try to remove the hardware filter. This may fail if the MC has
776          * rebooted (which frees all hardware filter resources).
777          */
778         if (ef10_filter_is_exclusive(spec)) {
779                 rc = efx_mcdi_filter_op_delete(enp,
780                     MC_CMD_FILTER_OP_IN_OP_REMOVE,
781                     &table->eft_entry[filter_idx].efe_handle);
782         } else {
783                 rc = efx_mcdi_filter_op_delete(enp,
784                     MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE,
785                     &table->eft_entry[filter_idx].efe_handle);
786         }
787
788         /* Free the software table entry */
789         EFSYS_LOCK(enp->en_eslp, state);
790         ef10_filter_set_entry_not_busy(table, filter_idx);
791         ef10_filter_set_entry(table, filter_idx, NULL);
792         EFSYS_UNLOCK(enp->en_eslp, state);
793
794         EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), spec);
795
796         /* Check result of hardware filter removal */
797         if (rc != 0)
798                 goto fail2;
799
800         return (0);
801
802 fail2:
803         EFSYS_PROBE(fail2);
804
805 fail1:
806         EFSYS_PROBE1(fail1, efx_rc_t, rc);
807
808         return (rc);
809 }
810
811         __checkReturn   efx_rc_t
812 ef10_filter_delete(
813         __in            efx_nic_t *enp,
814         __inout         efx_filter_spec_t *spec)
815 {
816         efx_rc_t rc;
817         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
818         efx_filter_spec_t *saved_spec;
819         unsigned int hash;
820         unsigned int depth;
821         unsigned int i;
822         efsys_lock_state_t state;
823         boolean_t locked = B_FALSE;
824
825         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
826                     enp->en_family == EFX_FAMILY_MEDFORD);
827
828         hash = ef10_filter_hash(spec);
829
830         EFSYS_LOCK(enp->en_eslp, state);
831         locked = B_TRUE;
832
833         depth = 1;
834         for (;;) {
835                 i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
836                 saved_spec = ef10_filter_entry_spec(table, i);
837                 if (saved_spec && ef10_filter_equal(spec, saved_spec) &&
838                     ef10_filter_same_dest(spec, saved_spec)) {
839                         break;
840                 }
841                 if (depth == EF10_FILTER_SEARCH_LIMIT) {
842                         rc = ENOENT;
843                         goto fail1;
844                 }
845                 depth++;
846         }
847
848         EFSYS_UNLOCK(enp->en_eslp, state);
849         locked = B_FALSE;
850
851         rc = ef10_filter_delete_internal(enp, i);
852         if (rc != 0)
853                 goto fail2;
854
855         return (0);
856
857 fail2:
858         EFSYS_PROBE(fail2);
859
860 fail1:
861         EFSYS_PROBE1(fail1, efx_rc_t, rc);
862
863         if (locked)
864                 EFSYS_UNLOCK(enp->en_eslp, state);
865
866         return (rc);
867 }
868
869 static  __checkReturn   efx_rc_t
870 efx_mcdi_get_parser_disp_info(
871         __in            efx_nic_t *enp,
872         __out           uint32_t *list,
873         __out           size_t *length)
874 {
875         efx_mcdi_req_t req;
876         uint8_t payload[MAX(MC_CMD_GET_PARSER_DISP_INFO_IN_LEN,
877                             MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX)];
878         efx_rc_t rc;
879
880         (void) memset(payload, 0, sizeof (payload));
881         req.emr_cmd = MC_CMD_GET_PARSER_DISP_INFO;
882         req.emr_in_buf = payload;
883         req.emr_in_length = MC_CMD_GET_PARSER_DISP_INFO_IN_LEN;
884         req.emr_out_buf = payload;
885         req.emr_out_length = MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX;
886
887         MCDI_IN_SET_DWORD(req, GET_PARSER_DISP_INFO_OUT_OP,
888             MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
889
890         efx_mcdi_execute(enp, &req);
891
892         if (req.emr_rc != 0) {
893                 rc = req.emr_rc;
894                 goto fail1;
895         }
896
897         *length = MCDI_OUT_DWORD(req,
898             GET_PARSER_DISP_INFO_OUT_NUM_SUPPORTED_MATCHES);
899
900         if (req.emr_out_length_used <
901             MC_CMD_GET_PARSER_DISP_INFO_OUT_LEN(*length)) {
902                 rc = EMSGSIZE;
903                 goto fail2;
904         }
905
906         memcpy(list,
907             MCDI_OUT2(req,
908             uint32_t,
909             GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES),
910             (*length) * sizeof (uint32_t));
911         EFX_STATIC_ASSERT(sizeof (uint32_t) ==
912             MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN);
913
914         return (0);
915
916 fail2:
917         EFSYS_PROBE(fail2);
918 fail1:
919         EFSYS_PROBE1(fail1, efx_rc_t, rc);
920
921         return (rc);
922 }
923
924         __checkReturn   efx_rc_t
925 ef10_filter_supported_filters(
926         __in            efx_nic_t *enp,
927         __out           uint32_t *list,
928         __out           size_t *length)
929 {
930         efx_rc_t rc;
931
932         if ((rc = efx_mcdi_get_parser_disp_info(enp, list, length)) != 0)
933                 goto fail1;
934
935         return (0);
936
937 fail1:
938         EFSYS_PROBE1(fail1, efx_rc_t, rc);
939
940         return (rc);
941 }
942
943 static  __checkReturn   efx_rc_t
944 ef10_filter_insert_unicast(
945         __in                            efx_nic_t *enp,
946         __in_ecount(6)                  uint8_t const *addr,
947         __in                            efx_filter_flags_t filter_flags)
948 {
949         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
950         efx_filter_spec_t spec;
951         efx_rc_t rc;
952
953         /* Insert the filter for the local station address */
954         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
955             filter_flags,
956             eftp->eft_default_rxq);
957         efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC, addr);
958
959         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
960             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
961         if (rc != 0)
962                 goto fail1;
963
964         eftp->eft_unicst_filter_count++;
965         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
966                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
967
968         return (0);
969
970 fail1:
971         EFSYS_PROBE1(fail1, efx_rc_t, rc);
972         return (rc);
973 }
974
975 static  __checkReturn   efx_rc_t
976 ef10_filter_insert_all_unicast(
977         __in                            efx_nic_t *enp,
978         __in                            efx_filter_flags_t filter_flags)
979 {
980         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
981         efx_filter_spec_t spec;
982         efx_rc_t rc;
983
984         /* Insert the unknown unicast filter */
985         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
986             filter_flags,
987             eftp->eft_default_rxq);
988         efx_filter_spec_set_uc_def(&spec);
989         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
990             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
991         if (rc != 0)
992                 goto fail1;
993
994         eftp->eft_unicst_filter_count++;
995         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
996                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
997
998         return (0);
999
1000 fail1:
1001         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1002         return (rc);
1003 }
1004
1005 static  __checkReturn   efx_rc_t
1006 ef10_filter_insert_multicast_list(
1007         __in                            efx_nic_t *enp,
1008         __in                            boolean_t mulcst,
1009         __in                            boolean_t brdcst,
1010         __in_ecount(6*count)            uint8_t const *addrs,
1011         __in                            uint32_t count,
1012         __in                            efx_filter_flags_t filter_flags,
1013         __in                            boolean_t rollback)
1014 {
1015         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1016         efx_filter_spec_t spec;
1017         uint8_t addr[6];
1018         uint32_t i;
1019         uint32_t filter_index;
1020         uint32_t filter_count;
1021         efx_rc_t rc;
1022
1023         if (mulcst == B_FALSE)
1024                 count = 0;
1025
1026         if (count + (brdcst ? 1 : 0) >
1027             EFX_ARRAY_SIZE(eftp->eft_mulcst_filter_indexes)) {
1028                 /* Too many MAC addresses */
1029                 rc = EINVAL;
1030                 goto fail1;
1031         }
1032
1033         /* Insert/renew multicast address list filters */
1034         filter_count = 0;
1035         for (i = 0; i < count; i++) {
1036                 efx_filter_spec_init_rx(&spec,
1037                     EFX_FILTER_PRI_AUTO,
1038                     filter_flags,
1039                     eftp->eft_default_rxq);
1040
1041                 efx_filter_spec_set_eth_local(&spec,
1042                     EFX_FILTER_SPEC_VID_UNSPEC,
1043                     &addrs[i * EFX_MAC_ADDR_LEN]);
1044
1045                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1046                                             &filter_index);
1047
1048                 if (rc == 0) {
1049                         eftp->eft_mulcst_filter_indexes[filter_count] =
1050                                 filter_index;
1051                         filter_count++;
1052                 } else if (rollback == B_TRUE) {
1053                         /* Only stop upon failure if told to rollback */
1054                         goto rollback;
1055                 }
1056
1057         }
1058
1059         if (brdcst == B_TRUE) {
1060                 /* Insert/renew broadcast address filter */
1061                 efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1062                     filter_flags,
1063                     eftp->eft_default_rxq);
1064
1065                 EFX_MAC_BROADCAST_ADDR_SET(addr);
1066                 efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC,
1067                     addr);
1068
1069                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1070                                             &filter_index);
1071
1072                 if (rc == 0) {
1073                         eftp->eft_mulcst_filter_indexes[filter_count] =
1074                                 filter_index;
1075                         filter_count++;
1076                 } else if (rollback == B_TRUE) {
1077                         /* Only stop upon failure if told to rollback */
1078                         goto rollback;
1079                 }
1080         }
1081
1082         eftp->eft_mulcst_filter_count = filter_count;
1083         eftp->eft_using_all_mulcst = B_FALSE;
1084
1085         return (0);
1086
1087 rollback:
1088         /* Remove any filters we have inserted */
1089         i = filter_count;
1090         while (i--) {
1091                 (void) ef10_filter_delete_internal(enp,
1092                     eftp->eft_mulcst_filter_indexes[i]);
1093         }
1094         eftp->eft_mulcst_filter_count = 0;
1095
1096 fail1:
1097         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1098
1099         return (rc);
1100 }
1101
1102 static  __checkReturn   efx_rc_t
1103 ef10_filter_insert_all_multicast(
1104         __in                            efx_nic_t *enp,
1105         __in                            efx_filter_flags_t filter_flags)
1106 {
1107         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1108         efx_filter_spec_t spec;
1109         efx_rc_t rc;
1110
1111         /* Insert the unknown multicast filter */
1112         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1113             filter_flags,
1114             eftp->eft_default_rxq);
1115         efx_filter_spec_set_mc_def(&spec);
1116
1117         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1118             &eftp->eft_mulcst_filter_indexes[0]);
1119         if (rc != 0)
1120                 goto fail1;
1121
1122         eftp->eft_mulcst_filter_count = 1;
1123         eftp->eft_using_all_mulcst = B_TRUE;
1124
1125         /*
1126          * FIXME: If brdcst == B_FALSE, add a filter to drop broadcast traffic.
1127          */
1128
1129         return (0);
1130
1131 fail1:
1132         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1133
1134         return (rc);
1135 }
1136
1137 static                  void
1138 ef10_filter_remove_old(
1139         __in            efx_nic_t *enp)
1140 {
1141         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1142         uint32_t i;
1143
1144         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1145                 if (ef10_filter_entry_is_auto_old(table, i)) {
1146                         (void) ef10_filter_delete_internal(enp, i);
1147                 }
1148         }
1149 }
1150
1151
1152 static  __checkReturn   efx_rc_t
1153 ef10_filter_get_workarounds(
1154         __in                            efx_nic_t *enp)
1155 {
1156         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1157         uint32_t implemented = 0;
1158         uint32_t enabled = 0;
1159         efx_rc_t rc;
1160
1161         rc = efx_mcdi_get_workarounds(enp, &implemented, &enabled);
1162         if (rc == 0) {
1163                 /* Check if chained multicast filter support is enabled */
1164                 if (implemented & enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807)
1165                         encp->enc_bug26807_workaround = B_TRUE;
1166                 else
1167                         encp->enc_bug26807_workaround = B_FALSE;
1168         } else if (rc == ENOTSUP) {
1169                 /*
1170                  * Firmware is too old to support GET_WORKAROUNDS, and support
1171                  * for this workaround was implemented later.
1172                  */
1173                 encp->enc_bug26807_workaround = B_FALSE;
1174         } else {
1175                 goto fail1;
1176         }
1177
1178         return (0);
1179
1180 fail1:
1181         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1182
1183         return (rc);
1184
1185 }
1186
1187
1188 /*
1189  * Reconfigure all filters.
1190  * If all_unicst and/or all mulcst filters cannot be applied then
1191  * return ENOTSUP (Note the filters for the specified addresses are
1192  * still applied in this case).
1193  */
1194         __checkReturn   efx_rc_t
1195 ef10_filter_reconfigure(
1196         __in                            efx_nic_t *enp,
1197         __in_ecount(6)                  uint8_t const *mac_addr,
1198         __in                            boolean_t all_unicst,
1199         __in                            boolean_t mulcst,
1200         __in                            boolean_t all_mulcst,
1201         __in                            boolean_t brdcst,
1202         __in_ecount(6*count)            uint8_t const *addrs,
1203         __in                            uint32_t count)
1204 {
1205         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1206         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1207         efx_filter_flags_t filter_flags;
1208         unsigned int i;
1209         efx_rc_t all_unicst_rc = 0;
1210         efx_rc_t all_mulcst_rc = 0;
1211         efx_rc_t rc;
1212
1213         if (table->eft_default_rxq == NULL) {
1214                 /*
1215                  * Filters direct traffic to the default RXQ, and so cannot be
1216                  * inserted until it is available. Any currently configured
1217                  * filters must be removed (ignore errors in case the MC
1218                  * has rebooted, which removes hardware filters).
1219                  */
1220                 for (i = 0; i < table->eft_unicst_filter_count; i++) {
1221                         (void) ef10_filter_delete_internal(enp,
1222                                         table->eft_unicst_filter_indexes[i]);
1223                 }
1224                 table->eft_unicst_filter_count = 0;
1225
1226                 for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1227                         (void) ef10_filter_delete_internal(enp,
1228                                         table->eft_mulcst_filter_indexes[i]);
1229                 }
1230                 table->eft_mulcst_filter_count = 0;
1231
1232                 return (0);
1233         }
1234
1235         if (table->eft_using_rss)
1236                 filter_flags = EFX_FILTER_FLAG_RX_RSS;
1237         else
1238                 filter_flags = 0;
1239
1240         /* Mark old filters which may need to be removed */
1241         for (i = 0; i < table->eft_unicst_filter_count; i++) {
1242                 ef10_filter_set_entry_auto_old(table,
1243                                         table->eft_unicst_filter_indexes[i]);
1244         }
1245         for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1246                 ef10_filter_set_entry_auto_old(table,
1247                                         table->eft_mulcst_filter_indexes[i]);
1248         }
1249
1250         /*
1251          * Insert or renew unicast filters.
1252          *
1253          * Frimware does not perform chaining on unicast filters. As traffic is
1254          * therefore only delivered to the first matching filter, we should
1255          * always insert the specific filter for our MAC address, to try and
1256          * ensure we get that traffic.
1257          *
1258          * (If the filter for our MAC address has already been inserted by
1259          * another function, we won't receive traffic sent to us, even if we
1260          * insert a unicast mismatch filter. To prevent traffic stealing, this
1261          * therefore relies on the privilege model only allowing functions to
1262          * insert filters for their own MAC address unless explicitly given
1263          * additional privileges by the user. This also means that, even on a
1264          * priviliged function, inserting a unicast mismatch filter may not
1265          * catch all traffic in multi PCI function scenarios.)
1266          */
1267         table->eft_unicst_filter_count = 0;
1268         rc = ef10_filter_insert_unicast(enp, mac_addr, filter_flags);
1269         if (all_unicst || (rc != 0)) {
1270                 all_unicst_rc = ef10_filter_insert_all_unicast(enp,
1271                                                     filter_flags);
1272                 if ((rc != 0) && (all_unicst_rc != 0))
1273                         goto fail1;
1274         }
1275
1276         /*
1277          * WORKAROUND_BUG26807 controls firmware support for chained multicast
1278          * filters, and can only be enabled or disabled when the hardware filter
1279          * table is empty.
1280          *
1281          * Chained multicast filters require support from the datapath firmware,
1282          * and may not be available (e.g. low-latency variants or old Huntington
1283          * firmware).
1284          *
1285          * Firmware will reset (FLR) functions which have inserted filters in
1286          * the hardware filter table when the workaround is enabled/disabled.
1287          * Functions without any hardware filters are not reset.
1288          *
1289          * Re-check if the workaround is enabled after adding unicast hardware
1290          * filters. This ensures that encp->enc_bug26807_workaround matches the
1291          * firmware state, and that later changes to enable/disable the
1292          * workaround will result in this function seeing a reset (FLR).
1293          *
1294          * In common-code drivers, we only support multiple PCI function
1295          * scenarios with firmware that supports multicast chaining, so we can
1296          * assume it is enabled for such cases and hence simplify the filter
1297          * insertion logic. Firmware that does not support multicast chaining
1298          * does not support multiple PCI function configurations either, so
1299          * filter insertion is much simpler and the same strategies can still be
1300          * used.
1301          */
1302         if ((rc = ef10_filter_get_workarounds(enp)) != 0)
1303                 goto fail2;
1304
1305         if ((table->eft_using_all_mulcst != all_mulcst) &&
1306             (encp->enc_bug26807_workaround == B_TRUE)) {
1307                 /*
1308                  * Multicast filter chaining is enabled, so traffic that matches
1309                  * more than one multicast filter will be replicated and
1310                  * delivered to multiple recipients.  To avoid this duplicate
1311                  * delivery, remove old multicast filters before inserting new
1312                  * multicast filters.
1313                  */
1314                 ef10_filter_remove_old(enp);
1315         }
1316
1317         /* Insert or renew multicast filters */
1318         if (all_mulcst == B_TRUE) {
1319                 /*
1320                  * Insert the all multicast filter. If that fails, try to insert
1321                  * all of our multicast filters (but without rollback on
1322                  * failure).
1323                  */
1324                 all_mulcst_rc = ef10_filter_insert_all_multicast(enp,
1325                                                             filter_flags);
1326                 if (all_mulcst_rc != 0) {
1327                         rc = ef10_filter_insert_multicast_list(enp, B_TRUE,
1328                             brdcst, addrs, count, filter_flags, B_FALSE);
1329                         if (rc != 0)
1330                                 goto fail3;
1331                 }
1332         } else {
1333                 /*
1334                  * Insert filters for multicast addresses.
1335                  * If any insertion fails, then rollback and try to insert the
1336                  * all multicast filter instead.
1337                  * If that also fails, try to insert all of the multicast
1338                  * filters (but without rollback on failure).
1339                  */
1340                 rc = ef10_filter_insert_multicast_list(enp, mulcst, brdcst,
1341                             addrs, count, filter_flags, B_TRUE);
1342                 if (rc != 0) {
1343                         if ((table->eft_using_all_mulcst == B_FALSE) &&
1344                             (encp->enc_bug26807_workaround == B_TRUE)) {
1345                                 /*
1346                                  * Multicast filter chaining is on, so remove
1347                                  * old filters before inserting the multicast
1348                                  * all filter to avoid duplicate delivery caused
1349                                  * by packets matching multiple filters.
1350                                  */
1351                                 ef10_filter_remove_old(enp);
1352                         }
1353
1354                         rc = ef10_filter_insert_all_multicast(enp,
1355                                                             filter_flags);
1356                         if (rc != 0) {
1357                                 rc = ef10_filter_insert_multicast_list(enp,
1358                                     mulcst, brdcst,
1359                                     addrs, count, filter_flags, B_FALSE);
1360                                 if (rc != 0)
1361                                         goto fail4;
1362                         }
1363                 }
1364         }
1365
1366         /* Remove old filters which were not renewed */
1367         ef10_filter_remove_old(enp);
1368
1369         /* report if any optional flags were rejected */
1370         if (((all_unicst != B_FALSE) && (all_unicst_rc != 0)) ||
1371             ((all_mulcst != B_FALSE) && (all_mulcst_rc != 0))) {
1372                 rc = ENOTSUP;
1373         }
1374
1375         return (rc);
1376
1377 fail4:
1378         EFSYS_PROBE(fail4);
1379 fail3:
1380         EFSYS_PROBE(fail3);
1381 fail2:
1382         EFSYS_PROBE(fail2);
1383 fail1:
1384         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1385
1386         /* Clear auto old flags */
1387         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1388                 if (ef10_filter_entry_is_auto_old(table, i)) {
1389                         ef10_filter_set_entry_not_auto_old(table, i);
1390                 }
1391         }
1392
1393         return (rc);
1394 }
1395
1396                 void
1397 ef10_filter_get_default_rxq(
1398         __in            efx_nic_t *enp,
1399         __out           efx_rxq_t **erpp,
1400         __out           boolean_t *using_rss)
1401 {
1402         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1403
1404         *erpp = table->eft_default_rxq;
1405         *using_rss = table->eft_using_rss;
1406 }
1407
1408
1409                 void
1410 ef10_filter_default_rxq_set(
1411         __in            efx_nic_t *enp,
1412         __in            efx_rxq_t *erp,
1413         __in            boolean_t using_rss)
1414 {
1415         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1416
1417 #if EFSYS_OPT_RX_SCALE
1418         EFSYS_ASSERT((using_rss == B_FALSE) ||
1419             (enp->en_rss_context != EF10_RSS_CONTEXT_INVALID));
1420         table->eft_using_rss = using_rss;
1421 #else
1422         EFSYS_ASSERT(using_rss == B_FALSE);
1423         table->eft_using_rss = B_FALSE;
1424 #endif
1425         table->eft_default_rxq = erp;
1426 }
1427
1428                 void
1429 ef10_filter_default_rxq_clear(
1430         __in            efx_nic_t *enp)
1431 {
1432         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1433
1434         table->eft_default_rxq = NULL;
1435         table->eft_using_rss = B_FALSE;
1436 }
1437
1438
1439 #endif /* EFSYS_OPT_FILTER */
1440
1441 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */