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