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