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