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