ef10_filter.c revision 311484
1/*-
2 * Copyright (c) 2007-2016 Solarflare Communications Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * The views and conclusions contained in the software and documentation are
27 * those of the authors and should not be interpreted as representing official
28 * policies, either expressed or implied, of the FreeBSD Project.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/dev/sfxge/common/ef10_filter.c 311484 2017-01-06 07:12:27Z arybchik $");
33
34#include "efx.h"
35#include "efx_impl.h"
36
37#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
38
39#if EFSYS_OPT_FILTER
40
41#define	EFE_SPEC(eftp, index)	((eftp)->eft_entry[(index)].efe_spec)
42
43static			efx_filter_spec_t *
44ef10_filter_entry_spec(
45	__in		const ef10_filter_table_t *eftp,
46	__in		unsigned int index)
47{
48	return ((efx_filter_spec_t *)(EFE_SPEC(eftp, index) &
49		~(uintptr_t)EFX_EF10_FILTER_FLAGS));
50}
51
52static			boolean_t
53ef10_filter_entry_is_busy(
54	__in		const ef10_filter_table_t *eftp,
55	__in		unsigned int index)
56{
57	if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_BUSY)
58		return (B_TRUE);
59	else
60		return (B_FALSE);
61}
62
63static			boolean_t
64ef10_filter_entry_is_auto_old(
65	__in		const ef10_filter_table_t *eftp,
66	__in		unsigned int index)
67{
68	if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_AUTO_OLD)
69		return (B_TRUE);
70	else
71		return (B_FALSE);
72}
73
74static			void
75ef10_filter_set_entry(
76	__inout		ef10_filter_table_t *eftp,
77	__in		unsigned int index,
78	__in_opt	const efx_filter_spec_t *efsp)
79{
80	EFE_SPEC(eftp, index) = (uintptr_t)efsp;
81}
82
83static			void
84ef10_filter_set_entry_busy(
85	__inout		ef10_filter_table_t *eftp,
86	__in		unsigned int index)
87{
88	EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
89}
90
91static			void
92ef10_filter_set_entry_not_busy(
93	__inout		ef10_filter_table_t *eftp,
94	__in		unsigned int index)
95{
96	EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
97}
98
99static			void
100ef10_filter_set_entry_auto_old(
101	__inout		ef10_filter_table_t *eftp,
102	__in		unsigned int index)
103{
104	EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
105	EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
106}
107
108static			void
109ef10_filter_set_entry_not_auto_old(
110	__inout		ef10_filter_table_t *eftp,
111	__in		unsigned int index)
112{
113	EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
114	EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
115}
116
117	__checkReturn	efx_rc_t
118ef10_filter_init(
119	__in		efx_nic_t *enp)
120{
121	efx_rc_t rc;
122	ef10_filter_table_t *eftp;
123
124	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
125		    enp->en_family == EFX_FAMILY_MEDFORD);
126
127#define	MATCH_MASK(match) (EFX_MASK32(match) << EFX_LOW_BIT(match))
128	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_HOST ==
129	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_SRC_IP));
130	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_HOST ==
131	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_IP));
132	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_MAC ==
133	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_SRC_MAC));
134	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_PORT ==
135	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_SRC_PORT));
136	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_MAC ==
137	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_MAC));
138	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_PORT ==
139	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_DST_PORT));
140	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_ETHER_TYPE ==
141	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_ETHER_TYPE));
142	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_INNER_VID ==
143	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_INNER_VLAN));
144	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_OUTER_VID ==
145	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_OUTER_VLAN));
146	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IP_PROTO ==
147	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_IP_PROTO));
148	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_UNKNOWN_MCAST_DST ==
149	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST));
150	EFX_STATIC_ASSERT((uint32_t)EFX_FILTER_MATCH_UNKNOWN_UCAST_DST ==
151	    MATCH_MASK(MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST));
152#undef MATCH_MASK
153
154	EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (ef10_filter_table_t), eftp);
155
156	if (!eftp) {
157		rc = ENOMEM;
158		goto fail1;
159	}
160
161	enp->en_filter.ef_ef10_filter_table = eftp;
162
163	return (0);
164
165fail1:
166	EFSYS_PROBE1(fail1, efx_rc_t, rc);
167
168	return (rc);
169}
170
171			void
172ef10_filter_fini(
173	__in		efx_nic_t *enp)
174{
175	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
176		    enp->en_family == EFX_FAMILY_MEDFORD);
177
178	if (enp->en_filter.ef_ef10_filter_table != NULL) {
179		EFSYS_KMEM_FREE(enp->en_esip, sizeof (ef10_filter_table_t),
180		    enp->en_filter.ef_ef10_filter_table);
181	}
182}
183
184static	__checkReturn	efx_rc_t
185efx_mcdi_filter_op_add(
186	__in		efx_nic_t *enp,
187	__in		efx_filter_spec_t *spec,
188	__in		unsigned int filter_op,
189	__inout		ef10_filter_handle_t *handle)
190{
191	efx_mcdi_req_t req;
192	uint8_t payload[MAX(MC_CMD_FILTER_OP_IN_LEN,
193			    MC_CMD_FILTER_OP_OUT_LEN)];
194	efx_rc_t rc;
195
196	memset(payload, 0, sizeof (payload));
197	req.emr_cmd = MC_CMD_FILTER_OP;
198	req.emr_in_buf = payload;
199	req.emr_in_length = MC_CMD_FILTER_OP_IN_LEN;
200	req.emr_out_buf = payload;
201	req.emr_out_length = MC_CMD_FILTER_OP_OUT_LEN;
202
203	switch (filter_op) {
204	case MC_CMD_FILTER_OP_IN_OP_REPLACE:
205		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_LO,
206		    handle->efh_lo);
207		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_HI,
208		    handle->efh_hi);
209		/* Fall through */
210	case MC_CMD_FILTER_OP_IN_OP_INSERT:
211	case MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE:
212		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP, filter_op);
213		break;
214	default:
215		EFSYS_ASSERT(0);
216		rc = EINVAL;
217		goto fail1;
218	}
219
220	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_PORT_ID,
221	    EVB_PORT_ID_ASSIGNED);
222	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_MATCH_FIELDS,
223	    spec->efs_match_flags);
224	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_DEST,
225	    MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
226	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_QUEUE,
227	    spec->efs_dmaq_id);
228	if (spec->efs_flags & EFX_FILTER_FLAG_RX_RSS) {
229		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_CONTEXT,
230		    spec->efs_rss_context);
231	}
232	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_RX_MODE,
233	    spec->efs_flags & EFX_FILTER_FLAG_RX_RSS ?
234	    MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
235	    MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
236	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_TX_DEST,
237	    MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
238
239	if (filter_op != MC_CMD_FILTER_OP_IN_OP_REPLACE) {
240		/*
241		 * NOTE: Unlike most MCDI requests, the filter fields
242		 * are presented in network (big endian) byte order.
243		 */
244		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_SRC_MAC),
245		    spec->efs_rem_mac, EFX_MAC_ADDR_LEN);
246		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_DST_MAC),
247		    spec->efs_loc_mac, EFX_MAC_ADDR_LEN);
248
249		MCDI_IN_SET_WORD(req, FILTER_OP_IN_SRC_PORT,
250		    __CPU_TO_BE_16(spec->efs_rem_port));
251		MCDI_IN_SET_WORD(req, FILTER_OP_IN_DST_PORT,
252		    __CPU_TO_BE_16(spec->efs_loc_port));
253
254		MCDI_IN_SET_WORD(req, FILTER_OP_IN_ETHER_TYPE,
255		    __CPU_TO_BE_16(spec->efs_ether_type));
256
257		MCDI_IN_SET_WORD(req, FILTER_OP_IN_INNER_VLAN,
258		    __CPU_TO_BE_16(spec->efs_inner_vid));
259		MCDI_IN_SET_WORD(req, FILTER_OP_IN_OUTER_VLAN,
260		    __CPU_TO_BE_16(spec->efs_outer_vid));
261
262		/* IP protocol (in low byte, high byte is zero) */
263		MCDI_IN_SET_BYTE(req, FILTER_OP_IN_IP_PROTO,
264		    spec->efs_ip_proto);
265
266		EFX_STATIC_ASSERT(sizeof (spec->efs_rem_host) ==
267		    MC_CMD_FILTER_OP_IN_SRC_IP_LEN);
268		EFX_STATIC_ASSERT(sizeof (spec->efs_loc_host) ==
269		    MC_CMD_FILTER_OP_IN_DST_IP_LEN);
270
271		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_SRC_IP),
272		    &spec->efs_rem_host.eo_byte[0],
273		    MC_CMD_FILTER_OP_IN_SRC_IP_LEN);
274		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_IN_DST_IP),
275		    &spec->efs_loc_host.eo_byte[0],
276		    MC_CMD_FILTER_OP_IN_DST_IP_LEN);
277	}
278
279	efx_mcdi_execute(enp, &req);
280
281	if (req.emr_rc != 0) {
282		rc = req.emr_rc;
283		goto fail2;
284	}
285
286	if (req.emr_out_length_used < MC_CMD_FILTER_OP_OUT_LEN) {
287		rc = EMSGSIZE;
288		goto fail3;
289	}
290
291	handle->efh_lo = MCDI_OUT_DWORD(req, FILTER_OP_OUT_HANDLE_LO);
292	handle->efh_hi = MCDI_OUT_DWORD(req, FILTER_OP_OUT_HANDLE_HI);
293
294	return (0);
295
296fail3:
297	EFSYS_PROBE(fail3);
298fail2:
299	EFSYS_PROBE(fail2);
300fail1:
301	EFSYS_PROBE1(fail1, efx_rc_t, rc);
302
303	return (rc);
304
305}
306
307static	__checkReturn	efx_rc_t
308efx_mcdi_filter_op_delete(
309	__in		efx_nic_t *enp,
310	__in		unsigned int filter_op,
311	__inout		ef10_filter_handle_t *handle)
312{
313	efx_mcdi_req_t req;
314	uint8_t payload[MAX(MC_CMD_FILTER_OP_IN_LEN,
315			    MC_CMD_FILTER_OP_OUT_LEN)];
316	efx_rc_t rc;
317
318	memset(payload, 0, sizeof (payload));
319	req.emr_cmd = MC_CMD_FILTER_OP;
320	req.emr_in_buf = payload;
321	req.emr_in_length = MC_CMD_FILTER_OP_IN_LEN;
322	req.emr_out_buf = payload;
323	req.emr_out_length = MC_CMD_FILTER_OP_OUT_LEN;
324
325	switch (filter_op) {
326	case MC_CMD_FILTER_OP_IN_OP_REMOVE:
327		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP,
328		    MC_CMD_FILTER_OP_IN_OP_REMOVE);
329		break;
330	case MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE:
331		MCDI_IN_SET_DWORD(req, FILTER_OP_IN_OP,
332		    MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
333		break;
334	default:
335		EFSYS_ASSERT(0);
336		rc = EINVAL;
337		goto fail1;
338	}
339
340	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_LO, handle->efh_lo);
341	MCDI_IN_SET_DWORD(req, FILTER_OP_IN_HANDLE_HI, handle->efh_hi);
342
343	efx_mcdi_execute_quiet(enp, &req);
344
345	if (req.emr_rc != 0) {
346		rc = req.emr_rc;
347		goto fail2;
348	}
349
350	if (req.emr_out_length_used < MC_CMD_FILTER_OP_OUT_LEN) {
351		rc = EMSGSIZE;
352		goto fail3;
353	}
354
355	return (0);
356
357fail3:
358	EFSYS_PROBE(fail3);
359
360fail2:
361	EFSYS_PROBE(fail2);
362fail1:
363	EFSYS_PROBE1(fail1, efx_rc_t, rc);
364
365	return (rc);
366}
367
368static	__checkReturn	boolean_t
369ef10_filter_equal(
370	__in		const efx_filter_spec_t *left,
371	__in		const efx_filter_spec_t *right)
372{
373	/* FIXME: Consider rx vs tx filters (look at efs_flags) */
374	if (left->efs_match_flags != right->efs_match_flags)
375		return (B_FALSE);
376	if (!EFX_OWORD_IS_EQUAL(left->efs_rem_host, right->efs_rem_host))
377		return (B_FALSE);
378	if (!EFX_OWORD_IS_EQUAL(left->efs_loc_host, right->efs_loc_host))
379		return (B_FALSE);
380	if (memcmp(left->efs_rem_mac, right->efs_rem_mac, EFX_MAC_ADDR_LEN))
381		return (B_FALSE);
382	if (memcmp(left->efs_loc_mac, right->efs_loc_mac, EFX_MAC_ADDR_LEN))
383		return (B_FALSE);
384	if (left->efs_rem_port != right->efs_rem_port)
385		return (B_FALSE);
386	if (left->efs_loc_port != right->efs_loc_port)
387		return (B_FALSE);
388	if (left->efs_inner_vid != right->efs_inner_vid)
389		return (B_FALSE);
390	if (left->efs_outer_vid != right->efs_outer_vid)
391		return (B_FALSE);
392	if (left->efs_ether_type != right->efs_ether_type)
393		return (B_FALSE);
394	if (left->efs_ip_proto != right->efs_ip_proto)
395		return (B_FALSE);
396
397	return (B_TRUE);
398
399}
400
401static	__checkReturn	boolean_t
402ef10_filter_same_dest(
403	__in		const efx_filter_spec_t *left,
404	__in		const efx_filter_spec_t *right)
405{
406	if ((left->efs_flags & EFX_FILTER_FLAG_RX_RSS) &&
407	    (right->efs_flags & EFX_FILTER_FLAG_RX_RSS)) {
408		if (left->efs_rss_context == right->efs_rss_context)
409			return (B_TRUE);
410	} else if ((~(left->efs_flags) & EFX_FILTER_FLAG_RX_RSS) &&
411	    (~(right->efs_flags) & EFX_FILTER_FLAG_RX_RSS)) {
412		if (left->efs_dmaq_id == right->efs_dmaq_id)
413			return (B_TRUE);
414	}
415	return (B_FALSE);
416}
417
418static	__checkReturn	uint32_t
419ef10_filter_hash(
420	__in		efx_filter_spec_t *spec)
421{
422	EFX_STATIC_ASSERT((sizeof (efx_filter_spec_t) % sizeof (uint32_t))
423			    == 0);
424	EFX_STATIC_ASSERT((EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid) %
425			    sizeof (uint32_t)) == 0);
426
427	/*
428	 * As the area of the efx_filter_spec_t we need to hash is DWORD
429	 * aligned and an exact number of DWORDs in size we can use the
430	 * optimised efx_hash_dwords() rather than efx_hash_bytes()
431	 */
432	return (efx_hash_dwords((const uint32_t *)&spec->efs_outer_vid,
433			(sizeof (efx_filter_spec_t) -
434			EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid)) /
435			sizeof (uint32_t), 0));
436}
437
438/*
439 * Decide whether a filter should be exclusive or else should allow
440 * delivery to additional recipients.  Currently we decide that
441 * filters for specific local unicast MAC and IP addresses are
442 * exclusive.
443 */
444static	__checkReturn	boolean_t
445ef10_filter_is_exclusive(
446	__in		efx_filter_spec_t *spec)
447{
448	if ((spec->efs_match_flags & EFX_FILTER_MATCH_LOC_MAC) &&
449	    !EFX_MAC_ADDR_IS_MULTICAST(spec->efs_loc_mac))
450		return (B_TRUE);
451
452	if ((spec->efs_match_flags &
453		(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
454	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
455		if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV4) &&
456		    ((spec->efs_loc_host.eo_u8[0] & 0xf) != 0xe))
457			return (B_TRUE);
458		if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV6) &&
459		    (spec->efs_loc_host.eo_u8[0] != 0xff))
460			return (B_TRUE);
461	}
462
463	return (B_FALSE);
464}
465
466	__checkReturn	efx_rc_t
467ef10_filter_restore(
468	__in		efx_nic_t *enp)
469{
470	int tbl_id;
471	efx_filter_spec_t *spec;
472	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
473	boolean_t restoring;
474	efsys_lock_state_t state;
475	efx_rc_t rc;
476
477	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
478		    enp->en_family == EFX_FAMILY_MEDFORD);
479
480	for (tbl_id = 0; tbl_id < EFX_EF10_FILTER_TBL_ROWS; tbl_id++) {
481
482		EFSYS_LOCK(enp->en_eslp, state);
483
484		spec = ef10_filter_entry_spec(eftp, tbl_id);
485		if (spec == NULL) {
486			restoring = B_FALSE;
487		} else if (ef10_filter_entry_is_busy(eftp, tbl_id)) {
488			/* Ignore busy entries. */
489			restoring = B_FALSE;
490		} else {
491			ef10_filter_set_entry_busy(eftp, tbl_id);
492			restoring = B_TRUE;
493		}
494
495		EFSYS_UNLOCK(enp->en_eslp, state);
496
497		if (restoring == B_FALSE)
498			continue;
499
500		if (ef10_filter_is_exclusive(spec)) {
501			rc = efx_mcdi_filter_op_add(enp, spec,
502			    MC_CMD_FILTER_OP_IN_OP_INSERT,
503			    &eftp->eft_entry[tbl_id].efe_handle);
504		} else {
505			rc = efx_mcdi_filter_op_add(enp, spec,
506			    MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
507			    &eftp->eft_entry[tbl_id].efe_handle);
508		}
509
510		if (rc != 0)
511			goto fail1;
512
513		EFSYS_LOCK(enp->en_eslp, state);
514
515		ef10_filter_set_entry_not_busy(eftp, tbl_id);
516
517		EFSYS_UNLOCK(enp->en_eslp, state);
518	}
519
520	return (0);
521
522fail1:
523	EFSYS_PROBE1(fail1, efx_rc_t, rc);
524
525	return (rc);
526}
527
528/*
529 * An arbitrary search limit for the software hash table. As per the linux net
530 * driver.
531 */
532#define	EF10_FILTER_SEARCH_LIMIT 200
533
534static	__checkReturn	efx_rc_t
535ef10_filter_add_internal(
536	__in		efx_nic_t *enp,
537	__inout		efx_filter_spec_t *spec,
538	__in		boolean_t may_replace,
539	__out_opt	uint32_t *filter_id)
540{
541	efx_rc_t rc;
542	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
543	efx_filter_spec_t *saved_spec;
544	uint32_t hash;
545	unsigned int depth;
546	int ins_index;
547	boolean_t replacing = B_FALSE;
548	unsigned int i;
549	efsys_lock_state_t state;
550	boolean_t locked = B_FALSE;
551
552	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
553		    enp->en_family == EFX_FAMILY_MEDFORD);
554
555#if EFSYS_OPT_RX_SCALE
556	spec->efs_rss_context = enp->en_rss_context;
557#endif
558
559	hash = ef10_filter_hash(spec);
560
561	/*
562	 * FIXME: Add support for inserting filters of different priorities
563	 * and removing lower priority multicast filters (bug 42378)
564	 */
565
566	/*
567	 * Find any existing filters with the same match tuple or
568	 * else a free slot to insert at.  If any of them are busy,
569	 * we have to wait and retry.
570	 */
571	for (;;) {
572		ins_index = -1;
573		depth = 1;
574		EFSYS_LOCK(enp->en_eslp, state);
575		locked = B_TRUE;
576
577		for (;;) {
578			i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
579			saved_spec = ef10_filter_entry_spec(eftp, i);
580
581			if (!saved_spec) {
582				if (ins_index < 0) {
583					ins_index = i;
584				}
585			} else if (ef10_filter_equal(spec, saved_spec)) {
586				if (ef10_filter_entry_is_busy(eftp, i))
587					break;
588				if (saved_spec->efs_priority
589					    == EFX_FILTER_PRI_AUTO) {
590					ins_index = i;
591					goto found;
592				} else if (ef10_filter_is_exclusive(spec)) {
593					if (may_replace) {
594						ins_index = i;
595						goto found;
596					} else {
597						rc = EEXIST;
598						goto fail1;
599					}
600				}
601
602				/* Leave existing */
603			}
604
605			/*
606			 * Once we reach the maximum search depth, use
607			 * the first suitable slot or return EBUSY if
608			 * there was none.
609			 */
610			if (depth == EF10_FILTER_SEARCH_LIMIT) {
611				if (ins_index < 0) {
612					rc = EBUSY;
613					goto fail2;
614				}
615				goto found;
616			}
617			depth++;
618		}
619		EFSYS_UNLOCK(enp->en_eslp, state);
620		locked = B_FALSE;
621	}
622
623found:
624	/*
625	 * Create a software table entry if necessary, and mark it
626	 * busy.  We might yet fail to insert, but any attempt to
627	 * insert a conflicting filter while we're waiting for the
628	 * firmware must find the busy entry.
629	 */
630	saved_spec = ef10_filter_entry_spec(eftp, ins_index);
631	if (saved_spec) {
632		if (saved_spec->efs_priority == EFX_FILTER_PRI_AUTO) {
633			/* This is a filter we are refreshing */
634			ef10_filter_set_entry_not_auto_old(eftp, ins_index);
635			goto out_unlock;
636
637		}
638		replacing = B_TRUE;
639	} else {
640		EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (*spec), saved_spec);
641		if (!saved_spec) {
642			rc = ENOMEM;
643			goto fail3;
644		}
645		*saved_spec = *spec;
646		ef10_filter_set_entry(eftp, ins_index, saved_spec);
647	}
648	ef10_filter_set_entry_busy(eftp, ins_index);
649
650	EFSYS_UNLOCK(enp->en_eslp, state);
651	locked = B_FALSE;
652
653	/*
654	 * On replacing the filter handle may change after after a successful
655	 * replace operation.
656	 */
657	if (replacing) {
658		rc = efx_mcdi_filter_op_add(enp, spec,
659		    MC_CMD_FILTER_OP_IN_OP_REPLACE,
660		    &eftp->eft_entry[ins_index].efe_handle);
661	} else if (ef10_filter_is_exclusive(spec)) {
662		rc = efx_mcdi_filter_op_add(enp, spec,
663		    MC_CMD_FILTER_OP_IN_OP_INSERT,
664		    &eftp->eft_entry[ins_index].efe_handle);
665	} else {
666		rc = efx_mcdi_filter_op_add(enp, spec,
667		    MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
668		    &eftp->eft_entry[ins_index].efe_handle);
669	}
670
671	if (rc != 0)
672		goto fail4;
673
674	EFSYS_LOCK(enp->en_eslp, state);
675	locked = B_TRUE;
676
677	if (replacing) {
678		/* Update the fields that may differ */
679		saved_spec->efs_priority = spec->efs_priority;
680		saved_spec->efs_flags = spec->efs_flags;
681		saved_spec->efs_rss_context = spec->efs_rss_context;
682		saved_spec->efs_dmaq_id = spec->efs_dmaq_id;
683	}
684
685	ef10_filter_set_entry_not_busy(eftp, ins_index);
686
687out_unlock:
688
689	EFSYS_UNLOCK(enp->en_eslp, state);
690	locked = B_FALSE;
691
692	if (filter_id)
693		*filter_id = ins_index;
694
695	return (0);
696
697fail4:
698	EFSYS_PROBE(fail4);
699
700	if (!replacing) {
701		EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), saved_spec);
702		saved_spec = NULL;
703	}
704	ef10_filter_set_entry_not_busy(eftp, ins_index);
705	ef10_filter_set_entry(eftp, ins_index, NULL);
706
707fail3:
708	EFSYS_PROBE(fail3);
709
710fail2:
711	EFSYS_PROBE(fail2);
712
713fail1:
714	EFSYS_PROBE1(fail1, efx_rc_t, rc);
715
716	if (locked)
717		EFSYS_UNLOCK(enp->en_eslp, state);
718
719	return (rc);
720}
721
722	__checkReturn	efx_rc_t
723ef10_filter_add(
724	__in		efx_nic_t *enp,
725	__inout		efx_filter_spec_t *spec,
726	__in		boolean_t may_replace)
727{
728	efx_rc_t rc;
729
730	rc = ef10_filter_add_internal(enp, spec, may_replace, NULL);
731	if (rc != 0)
732		goto fail1;
733
734	return (0);
735
736fail1:
737	EFSYS_PROBE1(fail1, efx_rc_t, rc);
738
739	return (rc);
740}
741
742
743static	__checkReturn	efx_rc_t
744ef10_filter_delete_internal(
745	__in		efx_nic_t *enp,
746	__in		uint32_t filter_id)
747{
748	efx_rc_t rc;
749	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
750	efx_filter_spec_t *spec;
751	efsys_lock_state_t state;
752	uint32_t filter_idx = filter_id % EFX_EF10_FILTER_TBL_ROWS;
753
754	/*
755	 * Find the software table entry and mark it busy.  Don't
756	 * remove it yet; any attempt to update while we're waiting
757	 * for the firmware must find the busy entry.
758	 *
759	 * FIXME: What if the busy flag is never cleared?
760	 */
761	EFSYS_LOCK(enp->en_eslp, state);
762	while (ef10_filter_entry_is_busy(table, filter_idx)) {
763		EFSYS_UNLOCK(enp->en_eslp, state);
764		EFSYS_SPIN(1);
765		EFSYS_LOCK(enp->en_eslp, state);
766	}
767	if ((spec = ef10_filter_entry_spec(table, filter_idx)) != NULL) {
768		ef10_filter_set_entry_busy(table, filter_idx);
769	}
770	EFSYS_UNLOCK(enp->en_eslp, state);
771
772	if (spec == NULL) {
773		rc = ENOENT;
774		goto fail1;
775	}
776
777	/*
778	 * Try to remove the hardware filter. This may fail if the MC has
779	 * rebooted (which frees all hardware filter resources).
780	 */
781	if (ef10_filter_is_exclusive(spec)) {
782		rc = efx_mcdi_filter_op_delete(enp,
783		    MC_CMD_FILTER_OP_IN_OP_REMOVE,
784		    &table->eft_entry[filter_idx].efe_handle);
785	} else {
786		rc = efx_mcdi_filter_op_delete(enp,
787		    MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE,
788		    &table->eft_entry[filter_idx].efe_handle);
789	}
790
791	/* Free the software table entry */
792	EFSYS_LOCK(enp->en_eslp, state);
793	ef10_filter_set_entry_not_busy(table, filter_idx);
794	ef10_filter_set_entry(table, filter_idx, NULL);
795	EFSYS_UNLOCK(enp->en_eslp, state);
796
797	EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), spec);
798
799	/* Check result of hardware filter removal */
800	if (rc != 0)
801		goto fail2;
802
803	return (0);
804
805fail2:
806	EFSYS_PROBE(fail2);
807
808fail1:
809	EFSYS_PROBE1(fail1, efx_rc_t, rc);
810
811	return (rc);
812}
813
814	__checkReturn	efx_rc_t
815ef10_filter_delete(
816	__in		efx_nic_t *enp,
817	__inout		efx_filter_spec_t *spec)
818{
819	efx_rc_t rc;
820	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
821	efx_filter_spec_t *saved_spec;
822	unsigned int hash;
823	unsigned int depth;
824	unsigned int i;
825	efsys_lock_state_t state;
826	boolean_t locked = B_FALSE;
827
828	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
829		    enp->en_family == EFX_FAMILY_MEDFORD);
830
831	hash = ef10_filter_hash(spec);
832
833	EFSYS_LOCK(enp->en_eslp, state);
834	locked = B_TRUE;
835
836	depth = 1;
837	for (;;) {
838		i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
839		saved_spec = ef10_filter_entry_spec(table, i);
840		if (saved_spec && ef10_filter_equal(spec, saved_spec) &&
841		    ef10_filter_same_dest(spec, saved_spec)) {
842			break;
843		}
844		if (depth == EF10_FILTER_SEARCH_LIMIT) {
845			rc = ENOENT;
846			goto fail1;
847		}
848		depth++;
849	}
850
851	EFSYS_UNLOCK(enp->en_eslp, state);
852	locked = B_FALSE;
853
854	rc = ef10_filter_delete_internal(enp, i);
855	if (rc != 0)
856		goto fail2;
857
858	return (0);
859
860fail2:
861	EFSYS_PROBE(fail2);
862
863fail1:
864	EFSYS_PROBE1(fail1, efx_rc_t, rc);
865
866	if (locked)
867		EFSYS_UNLOCK(enp->en_eslp, state);
868
869	return (rc);
870}
871
872static	__checkReturn	efx_rc_t
873efx_mcdi_get_parser_disp_info(
874	__in		efx_nic_t *enp,
875	__out		uint32_t *list,
876	__out		size_t *length)
877{
878	efx_mcdi_req_t req;
879	uint8_t payload[MAX(MC_CMD_GET_PARSER_DISP_INFO_IN_LEN,
880			    MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX)];
881	efx_rc_t rc;
882
883	(void) memset(payload, 0, sizeof (payload));
884	req.emr_cmd = MC_CMD_GET_PARSER_DISP_INFO;
885	req.emr_in_buf = payload;
886	req.emr_in_length = MC_CMD_GET_PARSER_DISP_INFO_IN_LEN;
887	req.emr_out_buf = payload;
888	req.emr_out_length = MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX;
889
890	MCDI_IN_SET_DWORD(req, GET_PARSER_DISP_INFO_OUT_OP,
891	    MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
892
893	efx_mcdi_execute(enp, &req);
894
895	if (req.emr_rc != 0) {
896		rc = req.emr_rc;
897		goto fail1;
898	}
899
900	*length = MCDI_OUT_DWORD(req,
901	    GET_PARSER_DISP_INFO_OUT_NUM_SUPPORTED_MATCHES);
902
903	if (req.emr_out_length_used <
904	    MC_CMD_GET_PARSER_DISP_INFO_OUT_LEN(*length)) {
905		rc = EMSGSIZE;
906		goto fail2;
907	}
908
909	memcpy(list,
910	    MCDI_OUT2(req,
911	    uint32_t,
912	    GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES),
913	    (*length) * sizeof (uint32_t));
914	EFX_STATIC_ASSERT(sizeof (uint32_t) ==
915	    MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN);
916
917	return (0);
918
919fail2:
920	EFSYS_PROBE(fail2);
921fail1:
922	EFSYS_PROBE1(fail1, efx_rc_t, rc);
923
924	return (rc);
925}
926
927	__checkReturn	efx_rc_t
928ef10_filter_supported_filters(
929	__in		efx_nic_t *enp,
930	__out		uint32_t *list,
931	__out		size_t *length)
932{
933	efx_rc_t rc;
934
935	if ((rc = efx_mcdi_get_parser_disp_info(enp, list, length)) != 0)
936		goto fail1;
937
938	return (0);
939
940fail1:
941	EFSYS_PROBE1(fail1, efx_rc_t, rc);
942
943	return (rc);
944}
945
946static	__checkReturn	efx_rc_t
947ef10_filter_insert_unicast(
948	__in				efx_nic_t *enp,
949	__in_ecount(6)			uint8_t const *addr,
950	__in				efx_filter_flags_t filter_flags)
951{
952	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
953	efx_filter_spec_t spec;
954	efx_rc_t rc;
955
956	/* Insert the filter for the local station address */
957	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
958	    filter_flags,
959	    eftp->eft_default_rxq);
960	efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC, addr);
961
962	rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
963	    &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
964	if (rc != 0)
965		goto fail1;
966
967	eftp->eft_unicst_filter_count++;
968	EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
969		    EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
970
971	return (0);
972
973fail1:
974	EFSYS_PROBE1(fail1, efx_rc_t, rc);
975	return (rc);
976}
977
978static	__checkReturn	efx_rc_t
979ef10_filter_insert_all_unicast(
980	__in				efx_nic_t *enp,
981	__in				efx_filter_flags_t filter_flags)
982{
983	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
984	efx_filter_spec_t spec;
985	efx_rc_t rc;
986
987	/* Insert the unknown unicast filter */
988	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
989	    filter_flags,
990	    eftp->eft_default_rxq);
991	efx_filter_spec_set_uc_def(&spec);
992	rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
993	    &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
994	if (rc != 0)
995		goto fail1;
996
997	eftp->eft_unicst_filter_count++;
998	EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
999		    EFX_EF10_FILTER_UNICAST_FILTERS_MAX);
1000
1001	return (0);
1002
1003fail1:
1004	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1005	return (rc);
1006}
1007
1008static	__checkReturn	efx_rc_t
1009ef10_filter_insert_multicast_list(
1010	__in				efx_nic_t *enp,
1011	__in				boolean_t mulcst,
1012	__in				boolean_t brdcst,
1013	__in_ecount(6*count)		uint8_t const *addrs,
1014	__in				uint32_t count,
1015	__in				efx_filter_flags_t filter_flags,
1016	__in				boolean_t rollback)
1017{
1018	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1019	efx_filter_spec_t spec;
1020	uint8_t addr[6];
1021	uint32_t i;
1022	uint32_t filter_index;
1023	uint32_t filter_count;
1024	efx_rc_t rc;
1025
1026	if (mulcst == B_FALSE)
1027		count = 0;
1028
1029	if (count + (brdcst ? 1 : 0) >
1030	    EFX_ARRAY_SIZE(eftp->eft_mulcst_filter_indexes)) {
1031		/* Too many MAC addresses */
1032		rc = EINVAL;
1033		goto fail1;
1034	}
1035
1036	/* Insert/renew multicast address list filters */
1037	filter_count = 0;
1038	for (i = 0; i < count; i++) {
1039		efx_filter_spec_init_rx(&spec,
1040		    EFX_FILTER_PRI_AUTO,
1041		    filter_flags,
1042		    eftp->eft_default_rxq);
1043
1044		efx_filter_spec_set_eth_local(&spec,
1045		    EFX_FILTER_SPEC_VID_UNSPEC,
1046		    &addrs[i * EFX_MAC_ADDR_LEN]);
1047
1048		rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1049					    &filter_index);
1050
1051		if (rc == 0) {
1052			eftp->eft_mulcst_filter_indexes[filter_count] =
1053				filter_index;
1054			filter_count++;
1055		} else if (rollback == B_TRUE) {
1056			/* Only stop upon failure if told to rollback */
1057			goto rollback;
1058		}
1059
1060	}
1061
1062	if (brdcst == B_TRUE) {
1063		/* Insert/renew broadcast address filter */
1064		efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1065		    filter_flags,
1066		    eftp->eft_default_rxq);
1067
1068		EFX_MAC_BROADCAST_ADDR_SET(addr);
1069		efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC,
1070		    addr);
1071
1072		rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1073					    &filter_index);
1074
1075		if (rc == 0) {
1076			eftp->eft_mulcst_filter_indexes[filter_count] =
1077				filter_index;
1078			filter_count++;
1079		} else if (rollback == B_TRUE) {
1080			/* Only stop upon failure if told to rollback */
1081			goto rollback;
1082		}
1083	}
1084
1085	eftp->eft_mulcst_filter_count = filter_count;
1086	eftp->eft_using_all_mulcst = B_FALSE;
1087
1088	return (0);
1089
1090rollback:
1091	/* Remove any filters we have inserted */
1092	i = filter_count;
1093	while (i--) {
1094		(void) ef10_filter_delete_internal(enp,
1095		    eftp->eft_mulcst_filter_indexes[i]);
1096	}
1097	eftp->eft_mulcst_filter_count = 0;
1098
1099fail1:
1100	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1101
1102	return (rc);
1103}
1104
1105static	__checkReturn	efx_rc_t
1106ef10_filter_insert_all_multicast(
1107	__in				efx_nic_t *enp,
1108	__in				efx_filter_flags_t filter_flags)
1109{
1110	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
1111	efx_filter_spec_t spec;
1112	efx_rc_t rc;
1113
1114	/* Insert the unknown multicast filter */
1115	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
1116	    filter_flags,
1117	    eftp->eft_default_rxq);
1118	efx_filter_spec_set_mc_def(&spec);
1119
1120	rc = ef10_filter_add_internal(enp, &spec, B_TRUE,
1121	    &eftp->eft_mulcst_filter_indexes[0]);
1122	if (rc != 0)
1123		goto fail1;
1124
1125	eftp->eft_mulcst_filter_count = 1;
1126	eftp->eft_using_all_mulcst = B_TRUE;
1127
1128	/*
1129	 * FIXME: If brdcst == B_FALSE, add a filter to drop broadcast traffic.
1130	 */
1131
1132	return (0);
1133
1134fail1:
1135	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1136
1137	return (rc);
1138}
1139
1140static			void
1141ef10_filter_remove_old(
1142	__in		efx_nic_t *enp)
1143{
1144	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1145	uint32_t i;
1146
1147	for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1148		if (ef10_filter_entry_is_auto_old(table, i)) {
1149			(void) ef10_filter_delete_internal(enp, i);
1150		}
1151	}
1152}
1153
1154
1155static	__checkReturn	efx_rc_t
1156ef10_filter_get_workarounds(
1157	__in				efx_nic_t *enp)
1158{
1159	efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1160	uint32_t implemented = 0;
1161	uint32_t enabled = 0;
1162	efx_rc_t rc;
1163
1164	rc = efx_mcdi_get_workarounds(enp, &implemented, &enabled);
1165	if (rc == 0) {
1166		/* Check if chained multicast filter support is enabled */
1167		if (implemented & enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807)
1168			encp->enc_bug26807_workaround = B_TRUE;
1169		else
1170			encp->enc_bug26807_workaround = B_FALSE;
1171	} else if (rc == ENOTSUP) {
1172		/*
1173		 * Firmware is too old to support GET_WORKAROUNDS, and support
1174		 * for this workaround was implemented later.
1175		 */
1176		encp->enc_bug26807_workaround = B_FALSE;
1177	} else {
1178		goto fail1;
1179	}
1180
1181	return (0);
1182
1183fail1:
1184	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1185
1186	return (rc);
1187
1188}
1189
1190
1191/*
1192 * Reconfigure all filters.
1193 * If all_unicst and/or all mulcst filters cannot be applied then
1194 * return ENOTSUP (Note the filters for the specified addresses are
1195 * still applied in this case).
1196 */
1197	__checkReturn	efx_rc_t
1198ef10_filter_reconfigure(
1199	__in				efx_nic_t *enp,
1200	__in_ecount(6)			uint8_t const *mac_addr,
1201	__in				boolean_t all_unicst,
1202	__in				boolean_t mulcst,
1203	__in				boolean_t all_mulcst,
1204	__in				boolean_t brdcst,
1205	__in_ecount(6*count)		uint8_t const *addrs,
1206	__in				uint32_t count)
1207{
1208	efx_nic_cfg_t *encp = &enp->en_nic_cfg;
1209	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1210	efx_filter_flags_t filter_flags;
1211	unsigned int i;
1212	efx_rc_t all_unicst_rc = 0;
1213	efx_rc_t all_mulcst_rc = 0;
1214	efx_rc_t rc;
1215
1216	if (table->eft_default_rxq == NULL) {
1217		/*
1218		 * Filters direct traffic to the default RXQ, and so cannot be
1219		 * inserted until it is available. Any currently configured
1220		 * filters must be removed (ignore errors in case the MC
1221		 * has rebooted, which removes hardware filters).
1222		 */
1223		for (i = 0; i < table->eft_unicst_filter_count; i++) {
1224			(void) ef10_filter_delete_internal(enp,
1225					table->eft_unicst_filter_indexes[i]);
1226		}
1227		table->eft_unicst_filter_count = 0;
1228
1229		for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1230			(void) ef10_filter_delete_internal(enp,
1231					table->eft_mulcst_filter_indexes[i]);
1232		}
1233		table->eft_mulcst_filter_count = 0;
1234
1235		return (0);
1236	}
1237
1238	if (table->eft_using_rss)
1239		filter_flags = EFX_FILTER_FLAG_RX_RSS;
1240	else
1241		filter_flags = 0;
1242
1243	/* Mark old filters which may need to be removed */
1244	for (i = 0; i < table->eft_unicst_filter_count; i++) {
1245		ef10_filter_set_entry_auto_old(table,
1246					table->eft_unicst_filter_indexes[i]);
1247	}
1248	for (i = 0; i < table->eft_mulcst_filter_count; i++) {
1249		ef10_filter_set_entry_auto_old(table,
1250					table->eft_mulcst_filter_indexes[i]);
1251	}
1252
1253	/*
1254	 * Insert or renew unicast filters.
1255	 *
1256	 * Frimware does not perform chaining on unicast filters. As traffic is
1257	 * therefore only delivered to the first matching filter, we should
1258	 * always insert the specific filter for our MAC address, to try and
1259	 * ensure we get that traffic.
1260	 *
1261	 * (If the filter for our MAC address has already been inserted by
1262	 * another function, we won't receive traffic sent to us, even if we
1263	 * insert a unicast mismatch filter. To prevent traffic stealing, this
1264	 * therefore relies on the privilege model only allowing functions to
1265	 * insert filters for their own MAC address unless explicitly given
1266	 * additional privileges by the user. This also means that, even on a
1267	 * priviliged function, inserting a unicast mismatch filter may not
1268	 * catch all traffic in multi PCI function scenarios.)
1269	 */
1270	table->eft_unicst_filter_count = 0;
1271	rc = ef10_filter_insert_unicast(enp, mac_addr, filter_flags);
1272	if (all_unicst || (rc != 0)) {
1273		all_unicst_rc = ef10_filter_insert_all_unicast(enp,
1274						    filter_flags);
1275		if ((rc != 0) && (all_unicst_rc != 0))
1276			goto fail1;
1277	}
1278
1279	/*
1280	 * WORKAROUND_BUG26807 controls firmware support for chained multicast
1281	 * filters, and can only be enabled or disabled when the hardware filter
1282	 * table is empty.
1283	 *
1284	 * Chained multicast filters require support from the datapath firmware,
1285	 * and may not be available (e.g. low-latency variants or old Huntington
1286	 * firmware).
1287	 *
1288	 * Firmware will reset (FLR) functions which have inserted filters in
1289	 * the hardware filter table when the workaround is enabled/disabled.
1290	 * Functions without any hardware filters are not reset.
1291	 *
1292	 * Re-check if the workaround is enabled after adding unicast hardware
1293	 * filters. This ensures that encp->enc_bug26807_workaround matches the
1294	 * firmware state, and that later changes to enable/disable the
1295	 * workaround will result in this function seeing a reset (FLR).
1296	 *
1297	 * In common-code drivers, we only support multiple PCI function
1298	 * scenarios with firmware that supports multicast chaining, so we can
1299	 * assume it is enabled for such cases and hence simplify the filter
1300	 * insertion logic. Firmware that does not support multicast chaining
1301	 * does not support multiple PCI function configurations either, so
1302	 * filter insertion is much simpler and the same strategies can still be
1303	 * used.
1304	 */
1305	if ((rc = ef10_filter_get_workarounds(enp)) != 0)
1306		goto fail2;
1307
1308	if ((table->eft_using_all_mulcst != all_mulcst) &&
1309	    (encp->enc_bug26807_workaround == B_TRUE)) {
1310		/*
1311		 * Multicast filter chaining is enabled, so traffic that matches
1312		 * more than one multicast filter will be replicated and
1313		 * delivered to multiple recipients.  To avoid this duplicate
1314		 * delivery, remove old multicast filters before inserting new
1315		 * multicast filters.
1316		 */
1317		ef10_filter_remove_old(enp);
1318	}
1319
1320	/* Insert or renew multicast filters */
1321	if (all_mulcst == B_TRUE) {
1322		/*
1323		 * Insert the all multicast filter. If that fails, try to insert
1324		 * all of our multicast filters (but without rollback on
1325		 * failure).
1326		 */
1327		all_mulcst_rc = ef10_filter_insert_all_multicast(enp,
1328							    filter_flags);
1329		if (all_mulcst_rc != 0) {
1330			rc = ef10_filter_insert_multicast_list(enp, B_TRUE,
1331			    brdcst, addrs, count, filter_flags, B_FALSE);
1332			if (rc != 0)
1333				goto fail3;
1334		}
1335	} else {
1336		/*
1337		 * Insert filters for multicast addresses.
1338		 * If any insertion fails, then rollback and try to insert the
1339		 * all multicast filter instead.
1340		 * If that also fails, try to insert all of the multicast
1341		 * filters (but without rollback on failure).
1342		 */
1343		rc = ef10_filter_insert_multicast_list(enp, mulcst, brdcst,
1344			    addrs, count, filter_flags, B_TRUE);
1345		if (rc != 0) {
1346			if ((table->eft_using_all_mulcst == B_FALSE) &&
1347			    (encp->enc_bug26807_workaround == B_TRUE)) {
1348				/*
1349				 * Multicast filter chaining is on, so remove
1350				 * old filters before inserting the multicast
1351				 * all filter to avoid duplicate delivery caused
1352				 * by packets matching multiple filters.
1353				 */
1354				ef10_filter_remove_old(enp);
1355			}
1356
1357			rc = ef10_filter_insert_all_multicast(enp,
1358							    filter_flags);
1359			if (rc != 0) {
1360				rc = ef10_filter_insert_multicast_list(enp,
1361				    mulcst, brdcst,
1362				    addrs, count, filter_flags, B_FALSE);
1363				if (rc != 0)
1364					goto fail4;
1365			}
1366		}
1367	}
1368
1369	/* Remove old filters which were not renewed */
1370	ef10_filter_remove_old(enp);
1371
1372	/* report if any optional flags were rejected */
1373	if (((all_unicst != B_FALSE) && (all_unicst_rc != 0)) ||
1374	    ((all_mulcst != B_FALSE) && (all_mulcst_rc != 0))) {
1375		rc = ENOTSUP;
1376	}
1377
1378	return (rc);
1379
1380fail4:
1381	EFSYS_PROBE(fail4);
1382fail3:
1383	EFSYS_PROBE(fail3);
1384fail2:
1385	EFSYS_PROBE(fail2);
1386fail1:
1387	EFSYS_PROBE1(fail1, efx_rc_t, rc);
1388
1389	/* Clear auto old flags */
1390	for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
1391		if (ef10_filter_entry_is_auto_old(table, i)) {
1392			ef10_filter_set_entry_not_auto_old(table, i);
1393		}
1394	}
1395
1396	return (rc);
1397}
1398
1399		void
1400ef10_filter_get_default_rxq(
1401	__in		efx_nic_t *enp,
1402	__out		efx_rxq_t **erpp,
1403	__out		boolean_t *using_rss)
1404{
1405	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1406
1407	*erpp = table->eft_default_rxq;
1408	*using_rss = table->eft_using_rss;
1409}
1410
1411
1412		void
1413ef10_filter_default_rxq_set(
1414	__in		efx_nic_t *enp,
1415	__in		efx_rxq_t *erp,
1416	__in		boolean_t using_rss)
1417{
1418	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1419
1420#if EFSYS_OPT_RX_SCALE
1421	EFSYS_ASSERT((using_rss == B_FALSE) ||
1422	    (enp->en_rss_context != EF10_RSS_CONTEXT_INVALID));
1423	table->eft_using_rss = using_rss;
1424#else
1425	EFSYS_ASSERT(using_rss == B_FALSE);
1426	table->eft_using_rss = B_FALSE;
1427#endif
1428	table->eft_default_rxq = erp;
1429}
1430
1431		void
1432ef10_filter_default_rxq_clear(
1433	__in		efx_nic_t *enp)
1434{
1435	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
1436
1437	table->eft_default_rxq = NULL;
1438	table->eft_using_rss = B_FALSE;
1439}
1440
1441
1442#endif /* EFSYS_OPT_FILTER */
1443
1444#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
1445