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