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