695bb8478523df7f738999d155b16c97f8ea46ef
[deb_dpdk.git] / drivers / net / sfc / base / ef10_filter.c
1 /*
2  * Copyright (c) 2007-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
35
36 #if EFSYS_OPT_FILTER
37
38 #define EFE_SPEC(eftp, index)   ((eftp)->eft_entry[(index)].efe_spec)
39
40 static                  efx_filter_spec_t *
41 ef10_filter_entry_spec(
42         __in            const ef10_filter_table_t *eftp,
43         __in            unsigned int index)
44 {
45         return ((efx_filter_spec_t *)(EFE_SPEC(eftp, index) &
46                 ~(uintptr_t)EFX_EF10_FILTER_FLAGS));
47 }
48
49 static                  boolean_t
50 ef10_filter_entry_is_busy(
51         __in            const ef10_filter_table_t *eftp,
52         __in            unsigned int index)
53 {
54         if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_BUSY)
55                 return (B_TRUE);
56         else
57                 return (B_FALSE);
58 }
59
60 static                  boolean_t
61 ef10_filter_entry_is_auto_old(
62         __in            const ef10_filter_table_t *eftp,
63         __in            unsigned int index)
64 {
65         if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_AUTO_OLD)
66                 return (B_TRUE);
67         else
68                 return (B_FALSE);
69 }
70
71 static                  void
72 ef10_filter_set_entry(
73         __inout         ef10_filter_table_t *eftp,
74         __in            unsigned int index,
75         __in_opt        const efx_filter_spec_t *efsp)
76 {
77         EFE_SPEC(eftp, index) = (uintptr_t)efsp;
78 }
79
80 static                  void
81 ef10_filter_set_entry_busy(
82         __inout         ef10_filter_table_t *eftp,
83         __in            unsigned int index)
84 {
85         EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
86 }
87
88 static                  void
89 ef10_filter_set_entry_not_busy(
90         __inout         ef10_filter_table_t *eftp,
91         __in            unsigned int index)
92 {
93         EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
94 }
95
96 static                  void
97 ef10_filter_set_entry_auto_old(
98         __inout         ef10_filter_table_t *eftp,
99         __in            unsigned int index)
100 {
101         EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
102         EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
103 }
104
105 static                  void
106 ef10_filter_set_entry_not_auto_old(
107         __inout         ef10_filter_table_t *eftp,
108         __in            unsigned int index)
109 {
110         EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
111         EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
112 }
113
114         __checkReturn   efx_rc_t
115 ef10_filter_init(
116         __in            efx_nic_t *enp)
117 {
118         efx_rc_t rc;
119         ef10_filter_table_t *eftp;
120
121         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
122                     enp->en_family == EFX_FAMILY_MEDFORD);
123
124 #define MATCH_MASK(match) (EFX_MASK32(match) << EFX_LOW_BIT(match))
125         EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_HOST ==
126             MATCH_MASK(MC_CMD_FILTER_OP_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_ecount(buffer_length)     uint32_t *buffer,
873         __in                            size_t buffer_length,
874         __out                           size_t *list_lengthp)
875 {
876         efx_mcdi_req_t req;
877         uint8_t payload[MAX(MC_CMD_GET_PARSER_DISP_INFO_IN_LEN,
878                             MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX)];
879         size_t matches_count;
880         size_t list_size;
881         efx_rc_t rc;
882
883         (void) memset(payload, 0, sizeof (payload));
884         req.emr_cmd = MC_CMD_GET_PARSER_DISP_INFO;
885         req.emr_in_buf = payload;
886         req.emr_in_length = MC_CMD_GET_PARSER_DISP_INFO_IN_LEN;
887         req.emr_out_buf = payload;
888         req.emr_out_length = MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX;
889
890         MCDI_IN_SET_DWORD(req, GET_PARSER_DISP_INFO_OUT_OP,
891             MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
892
893         efx_mcdi_execute(enp, &req);
894
895         if (req.emr_rc != 0) {
896                 rc = req.emr_rc;
897                 goto fail1;
898         }
899
900         matches_count = MCDI_OUT_DWORD(req,
901             GET_PARSER_DISP_INFO_OUT_NUM_SUPPORTED_MATCHES);
902
903         if (req.emr_out_length_used <
904             MC_CMD_GET_PARSER_DISP_INFO_OUT_LEN(matches_count)) {
905                 rc = EMSGSIZE;
906                 goto fail2;
907         }
908
909         *list_lengthp = matches_count;
910
911         if (buffer_length < matches_count) {
912                 rc = ENOSPC;
913                 goto fail3;
914         }
915
916         /*
917          * Check that the elements in the list in the MCDI response are the size
918          * we expect, so we can just copy them directly. Any conversion of the
919          * flags is handled by the caller.
920          */
921         EFX_STATIC_ASSERT(sizeof (uint32_t) ==
922             MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN);
923
924         list_size = matches_count *
925                 MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN;
926         memcpy(buffer,
927             MCDI_OUT2(req, uint32_t,
928                     GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES),
929             list_size);
930
931         return (0);
932
933 fail3:
934         EFSYS_PROBE(fail3);
935 fail2:
936         EFSYS_PROBE(fail2);
937 fail1:
938         EFSYS_PROBE1(fail1, efx_rc_t, rc);
939
940         return (rc);
941 }
942
943         __checkReturn   efx_rc_t
944 ef10_filter_supported_filters(
945         __in                            efx_nic_t *enp,
946         __out_ecount(buffer_length)     uint32_t *buffer,
947         __in                            size_t buffer_length,
948         __out                           size_t *list_lengthp)
949 {
950
951         size_t mcdi_list_length;
952         size_t list_length;
953         uint32_t i;
954         efx_rc_t rc;
955         uint32_t all_filter_flags =
956             (EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_LOC_HOST |
957             EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_REM_PORT |
958             EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_PORT |
959             EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_INNER_VID |
960             EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_IP_PROTO |
961             EFX_FILTER_MATCH_UNKNOWN_MCAST_DST |
962             EFX_FILTER_MATCH_UNKNOWN_UCAST_DST);
963
964         rc = efx_mcdi_get_parser_disp_info(enp, buffer, buffer_length,
965                                             &mcdi_list_length);
966         if (rc != 0) {
967                 if (rc == ENOSPC) {
968                         /* Pass through mcdi_list_length for the list length */
969                         *list_lengthp = mcdi_list_length;
970                 }
971                 goto fail1;
972         }
973
974         /*
975          * The static assertions in ef10_filter_init() ensure that the values of
976          * the EFX_FILTER_MATCH flags match those used by MCDI, so they don't
977          * need to be converted.
978          *
979          * In case support is added to MCDI for additional flags, remove any
980          * matches from the list which include flags we don't support. The order
981          * of the matches is preserved as they are ordered from highest to
982          * lowest priority.
983          */
984         EFSYS_ASSERT(mcdi_list_length <= buffer_length);
985         list_length = 0;
986         for (i = 0; i < mcdi_list_length; i++) {
987                 if ((buffer[i] & ~all_filter_flags) == 0) {
988                         buffer[list_length] = buffer[i];
989                         list_length++;
990                 }
991         }
992
993         *list_lengthp = list_length;
994
995         return (0);
996
997 fail1:
998         EFSYS_PROBE1(fail1, efx_rc_t, rc);
999
1000         return (rc);
1001 }
1002
1003 static  __checkReturn   efx_rc_t
1004 ef10_filter_insert_unicast(
1005         __in                            efx_nic_t *enp,
1006         __in_ecount(6)                  uint8_t const *addr,
1007         __in                            efx_filter_flags_t filter_flags)
1008 {
1009         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1010         efx_filter_spec_t spec;
1011         efx_rc_t rc;
1012
1013         /* Insert the filter for the local station address */
1014         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1015             filter_flags,
1016             eftp->eft_default_rxq);
1017         efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC, addr);
1018
1019         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1020             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
1021         if (rc != 0)
1022                 goto fail1;
1023
1024         eftp->eft_unicst_filter_count++;
1025         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
1026                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
1027
1028         return (0);
1029
1030 fail1:
1031         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1032         return (rc);
1033 }
1034
1035 static  __checkReturn   efx_rc_t
1036 ef10_filter_insert_all_unicast(
1037         __in                            efx_nic_t *enp,
1038         __in                            efx_filter_flags_t filter_flags)
1039 {
1040         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1041         efx_filter_spec_t spec;
1042         efx_rc_t rc;
1043
1044         /* Insert the unknown unicast filter */
1045         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1046             filter_flags,
1047             eftp->eft_default_rxq);
1048         efx_filter_spec_set_uc_def(&spec);
1049         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1050             &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
1051         if (rc != 0)
1052                 goto fail1;
1053
1054         eftp->eft_unicst_filter_count++;
1055         EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
1056                     EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
1057
1058         return (0);
1059
1060 fail1:
1061         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1062         return (rc);
1063 }
1064
1065 static  __checkReturn   efx_rc_t
1066 ef10_filter_insert_multicast_list(
1067         __in                            efx_nic_t *enp,
1068         __in                            boolean_t mulcst,
1069         __in                            boolean_t brdcst,
1070         __in_ecount(6*count)            uint8_t const *addrs,
1071         __in                            uint32_t count,
1072         __in                            efx_filter_flags_t filter_flags,
1073         __in                            boolean_t rollback)
1074 {
1075         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1076         efx_filter_spec_t spec;
1077         uint8_t addr[6];
1078         uint32_t i;
1079         uint32_t filter_index;
1080         uint32_t filter_count;
1081         efx_rc_t rc;
1082
1083         if (mulcst == B_FALSE)
1084                 count = 0;
1085
1086         if (count + (brdcst ? 1 : 0) >
1087             EFX_ARRAY_SIZE(eftp->eft_mulcst_filter_indexes)) {
1088                 /* Too many MAC addresses */
1089                 rc = EINVAL;
1090                 goto fail1;
1091         }
1092
1093         /* Insert/renew multicast address list filters */
1094         filter_count = 0;
1095         for (i = 0; i < count; i++) {
1096                 efx_filter_spec_init_rx(&spec,
1097                     EFX_FILTER_PRI_AUTO,
1098                     filter_flags,
1099                     eftp->eft_default_rxq);
1100
1101                 efx_filter_spec_set_eth_local(&spec,
1102                     EFX_FILTER_SPEC_VID_UNSPEC,
1103                     &addrs[i * EFX_MAC_ADDR_LEN]);
1104
1105                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1106                                             &filter_index);
1107
1108                 if (rc == 0) {
1109                         eftp->eft_mulcst_filter_indexes[filter_count] =
1110                                 filter_index;
1111                         filter_count++;
1112                 } else if (rollback == B_TRUE) {
1113                         /* Only stop upon failure if told to rollback */
1114                         goto rollback;
1115                 }
1116
1117         }
1118
1119         if (brdcst == B_TRUE) {
1120                 /* Insert/renew broadcast address filter */
1121                 efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1122                     filter_flags,
1123                     eftp->eft_default_rxq);
1124
1125                 EFX_MAC_BROADCAST_ADDR_SET(addr);
1126                 efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC,
1127                     addr);
1128
1129                 rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1130                                             &filter_index);
1131
1132                 if (rc == 0) {
1133                         eftp->eft_mulcst_filter_indexes[filter_count] =
1134                                 filter_index;
1135                         filter_count++;
1136                 } else if (rollback == B_TRUE) {
1137                         /* Only stop upon failure if told to rollback */
1138                         goto rollback;
1139                 }
1140         }
1141
1142         eftp->eft_mulcst_filter_count = filter_count;
1143         eftp->eft_using_all_mulcst = B_FALSE;
1144
1145         return (0);
1146
1147 rollback:
1148         /* Remove any filters we have inserted */
1149         i = filter_count;
1150         while (i--) {
1151                 (void) ef10_filter_delete_internal(enp,
1152                     eftp->eft_mulcst_filter_indexes[i]);
1153         }
1154         eftp->eft_mulcst_filter_count = 0;
1155
1156 fail1:
1157         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1158
1159         return (rc);
1160 }
1161
1162 static  __checkReturn   efx_rc_t
1163 ef10_filter_insert_all_multicast(
1164         __in                            efx_nic_t *enp,
1165         __in                            efx_filter_flags_t filter_flags)
1166 {
1167         ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1168         efx_filter_spec_t spec;
1169         efx_rc_t rc;
1170
1171         /* Insert the unknown multicast filter */
1172         efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1173             filter_flags,
1174             eftp->eft_default_rxq);
1175         efx_filter_spec_set_mc_def(&spec);
1176
1177         rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1178             &eftp->eft_mulcst_filter_indexes[0]);
1179         if (rc != 0)
1180                 goto fail1;
1181
1182         eftp->eft_mulcst_filter_count = 1;
1183         eftp->eft_using_all_mulcst = B_TRUE;
1184
1185         /*
1186          * FIXME: If brdcst == B_FALSE, add a filter to drop broadcast traffic.
1187          */
1188
1189         return (0);
1190
1191 fail1:
1192         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1193
1194         return (rc);
1195 }
1196
1197 static                  void
1198 ef10_filter_remove_old(
1199         __in            efx_nic_t *enp)
1200 {
1201         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1202         uint32_t i;
1203
1204         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1205                 if (ef10_filter_entry_is_auto_old(table, i)) {
1206                         (void) ef10_filter_delete_internal(enp, i);
1207                 }
1208         }
1209 }
1210
1211
1212 static  __checkReturn   efx_rc_t
1213 ef10_filter_get_workarounds(
1214         __in                            efx_nic_t *enp)
1215 {
1216         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1217         uint32_t implemented = 0;
1218         uint32_t enabled = 0;
1219         efx_rc_t rc;
1220
1221         rc = efx_mcdi_get_workarounds(enp, &implemented, &enabled);
1222         if (rc == 0) {
1223                 /* Check if chained multicast filter support is enabled */
1224                 if (implemented & enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807)
1225                         encp->enc_bug26807_workaround = B_TRUE;
1226                 else
1227                         encp->enc_bug26807_workaround = B_FALSE;
1228         } else if (rc == ENOTSUP) {
1229                 /*
1230                  * Firmware is too old to support GET_WORKAROUNDS, and support
1231                  * for this workaround was implemented later.
1232                  */
1233                 encp->enc_bug26807_workaround = B_FALSE;
1234         } else {
1235                 goto fail1;
1236         }
1237
1238         return (0);
1239
1240 fail1:
1241         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1242
1243         return (rc);
1244
1245 }
1246
1247
1248 /*
1249  * Reconfigure all filters.
1250  * If all_unicst and/or all mulcst filters cannot be applied then
1251  * return ENOTSUP (Note the filters for the specified addresses are
1252  * still applied in this case).
1253  */
1254         __checkReturn   efx_rc_t
1255 ef10_filter_reconfigure(
1256         __in                            efx_nic_t *enp,
1257         __in_ecount(6)                  uint8_t const *mac_addr,
1258         __in                            boolean_t all_unicst,
1259         __in                            boolean_t mulcst,
1260         __in                            boolean_t all_mulcst,
1261         __in                            boolean_t brdcst,
1262         __in_ecount(6*count)            uint8_t const *addrs,
1263         __in                            uint32_t count)
1264 {
1265         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1266         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1267         efx_filter_flags_t filter_flags;
1268         unsigned int i;
1269         efx_rc_t all_unicst_rc = 0;
1270         efx_rc_t all_mulcst_rc = 0;
1271         efx_rc_t rc;
1272
1273         if (table->eft_default_rxq == NULL) {
1274                 /*
1275                  * Filters direct traffic to the default RXQ, and so cannot be
1276                  * inserted until it is available. Any currently configured
1277                  * filters must be removed (ignore errors in case the MC
1278                  * has rebooted, which removes hardware filters).
1279                  */
1280                 for (i = 0; i < table->eft_unicst_filter_count; i++) {
1281                         (void) ef10_filter_delete_internal(enp,
1282                                         table->eft_unicst_filter_indexes[i]);
1283                 }
1284                 table->eft_unicst_filter_count = 0;
1285
1286                 for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1287                         (void) ef10_filter_delete_internal(enp,
1288                                         table->eft_mulcst_filter_indexes[i]);
1289                 }
1290                 table->eft_mulcst_filter_count = 0;
1291
1292                 return (0);
1293         }
1294
1295         if (table->eft_using_rss)
1296                 filter_flags = EFX_FILTER_FLAG_RX_RSS;
1297         else
1298                 filter_flags = 0;
1299
1300         /* Mark old filters which may need to be removed */
1301         for (i = 0; i < table->eft_unicst_filter_count; i++) {
1302                 ef10_filter_set_entry_auto_old(table,
1303                                         table->eft_unicst_filter_indexes[i]);
1304         }
1305         for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1306                 ef10_filter_set_entry_auto_old(table,
1307                                         table->eft_mulcst_filter_indexes[i]);
1308         }
1309
1310         /*
1311          * Insert or renew unicast filters.
1312          *
1313          * Frimware does not perform chaining on unicast filters. As traffic is
1314          * therefore only delivered to the first matching filter, we should
1315          * always insert the specific filter for our MAC address, to try and
1316          * ensure we get that traffic.
1317          *
1318          * (If the filter for our MAC address has already been inserted by
1319          * another function, we won't receive traffic sent to us, even if we
1320          * insert a unicast mismatch filter. To prevent traffic stealing, this
1321          * therefore relies on the privilege model only allowing functions to
1322          * insert filters for their own MAC address unless explicitly given
1323          * additional privileges by the user. This also means that, even on a
1324          * priviliged function, inserting a unicast mismatch filter may not
1325          * catch all traffic in multi PCI function scenarios.)
1326          */
1327         table->eft_unicst_filter_count = 0;
1328         rc = ef10_filter_insert_unicast(enp, mac_addr, filter_flags);
1329         if (all_unicst || (rc != 0)) {
1330                 all_unicst_rc = ef10_filter_insert_all_unicast(enp,
1331                                                     filter_flags);
1332                 if ((rc != 0) && (all_unicst_rc != 0))
1333                         goto fail1;
1334         }
1335
1336         /*
1337          * WORKAROUND_BUG26807 controls firmware support for chained multicast
1338          * filters, and can only be enabled or disabled when the hardware filter
1339          * table is empty.
1340          *
1341          * Chained multicast filters require support from the datapath firmware,
1342          * and may not be available (e.g. low-latency variants or old Huntington
1343          * firmware).
1344          *
1345          * Firmware will reset (FLR) functions which have inserted filters in
1346          * the hardware filter table when the workaround is enabled/disabled.
1347          * Functions without any hardware filters are not reset.
1348          *
1349          * Re-check if the workaround is enabled after adding unicast hardware
1350          * filters. This ensures that encp->enc_bug26807_workaround matches the
1351          * firmware state, and that later changes to enable/disable the
1352          * workaround will result in this function seeing a reset (FLR).
1353          *
1354          * In common-code drivers, we only support multiple PCI function
1355          * scenarios with firmware that supports multicast chaining, so we can
1356          * assume it is enabled for such cases and hence simplify the filter
1357          * insertion logic. Firmware that does not support multicast chaining
1358          * does not support multiple PCI function configurations either, so
1359          * filter insertion is much simpler and the same strategies can still be
1360          * used.
1361          */
1362         if ((rc = ef10_filter_get_workarounds(enp)) != 0)
1363                 goto fail2;
1364
1365         if ((table->eft_using_all_mulcst != all_mulcst) &&
1366             (encp->enc_bug26807_workaround == B_TRUE)) {
1367                 /*
1368                  * Multicast filter chaining is enabled, so traffic that matches
1369                  * more than one multicast filter will be replicated and
1370                  * delivered to multiple recipients.  To avoid this duplicate
1371                  * delivery, remove old multicast filters before inserting new
1372                  * multicast filters.
1373                  */
1374                 ef10_filter_remove_old(enp);
1375         }
1376
1377         /* Insert or renew multicast filters */
1378         if (all_mulcst == B_TRUE) {
1379                 /*
1380                  * Insert the all multicast filter. If that fails, try to insert
1381                  * all of our multicast filters (but without rollback on
1382                  * failure).
1383                  */
1384                 all_mulcst_rc = ef10_filter_insert_all_multicast(enp,
1385                                                             filter_flags);
1386                 if (all_mulcst_rc != 0) {
1387                         rc = ef10_filter_insert_multicast_list(enp, B_TRUE,
1388                             brdcst, addrs, count, filter_flags, B_FALSE);
1389                         if (rc != 0)
1390                                 goto fail3;
1391                 }
1392         } else {
1393                 /*
1394                  * Insert filters for multicast addresses.
1395                  * If any insertion fails, then rollback and try to insert the
1396                  * all multicast filter instead.
1397                  * If that also fails, try to insert all of the multicast
1398                  * filters (but without rollback on failure).
1399                  */
1400                 rc = ef10_filter_insert_multicast_list(enp, mulcst, brdcst,
1401                             addrs, count, filter_flags, B_TRUE);
1402                 if (rc != 0) {
1403                         if ((table->eft_using_all_mulcst == B_FALSE) &&
1404                             (encp->enc_bug26807_workaround == B_TRUE)) {
1405                                 /*
1406                                  * Multicast filter chaining is on, so remove
1407                                  * old filters before inserting the multicast
1408                                  * all filter to avoid duplicate delivery caused
1409                                  * by packets matching multiple filters.
1410                                  */
1411                                 ef10_filter_remove_old(enp);
1412                         }
1413
1414                         rc = ef10_filter_insert_all_multicast(enp,
1415                                                             filter_flags);
1416                         if (rc != 0) {
1417                                 rc = ef10_filter_insert_multicast_list(enp,
1418                                     mulcst, brdcst,
1419                                     addrs, count, filter_flags, B_FALSE);
1420                                 if (rc != 0)
1421                                         goto fail4;
1422                         }
1423                 }
1424         }
1425
1426         /* Remove old filters which were not renewed */
1427         ef10_filter_remove_old(enp);
1428
1429         /* report if any optional flags were rejected */
1430         if (((all_unicst != B_FALSE) && (all_unicst_rc != 0)) ||
1431             ((all_mulcst != B_FALSE) && (all_mulcst_rc != 0))) {
1432                 rc = ENOTSUP;
1433         }
1434
1435         return (rc);
1436
1437 fail4:
1438         EFSYS_PROBE(fail4);
1439 fail3:
1440         EFSYS_PROBE(fail3);
1441 fail2:
1442         EFSYS_PROBE(fail2);
1443 fail1:
1444         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1445
1446         /* Clear auto old flags */
1447         for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1448                 if (ef10_filter_entry_is_auto_old(table, i)) {
1449                         ef10_filter_set_entry_not_auto_old(table, i);
1450                 }
1451         }
1452
1453         return (rc);
1454 }
1455
1456                 void
1457 ef10_filter_get_default_rxq(
1458         __in            efx_nic_t *enp,
1459         __out           efx_rxq_t **erpp,
1460         __out           boolean_t *using_rss)
1461 {
1462         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1463
1464         *erpp = table->eft_default_rxq;
1465         *using_rss = table->eft_using_rss;
1466 }
1467
1468
1469                 void
1470 ef10_filter_default_rxq_set(
1471         __in            efx_nic_t *enp,
1472         __in            efx_rxq_t *erp,
1473         __in            boolean_t using_rss)
1474 {
1475         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1476
1477 #if EFSYS_OPT_RX_SCALE
1478         EFSYS_ASSERT((using_rss == B_FALSE) ||
1479             (enp->en_rss_context != EF10_RSS_CONTEXT_INVALID));
1480         table->eft_using_rss = using_rss;
1481 #else
1482         EFSYS_ASSERT(using_rss == B_FALSE);
1483         table->eft_using_rss = B_FALSE;
1484 #endif
1485         table->eft_default_rxq = erp;
1486 }
1487
1488                 void
1489 ef10_filter_default_rxq_clear(
1490         __in            efx_nic_t *enp)
1491 {
1492         ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1493
1494         table->eft_default_rxq = NULL;
1495         table->eft_using_rss = B_FALSE;
1496 }
1497
1498
1499 #endif /* EFSYS_OPT_FILTER */
1500
1501 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */