1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2018 Solarflare Communications Inc.
5 * Copyright 2019-2020 Xilinx Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation, incorporated herein by reference.
12 #include "mcdi_filters.h"
15 #include "rx_common.h"
17 /* The maximum size of a shared RSS context */
18 /* TODO: this should really be from the mcdi protocol export */
19 #define EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE 64UL
21 #define EFX_EF10_FILTER_ID_INVALID 0xffff
23 /* An arbitrary search limit for the software hash table */
24 #define EFX_EF10_FILTER_SEARCH_LIMIT 200
26 static struct efx_filter_spec *
27 efx_mcdi_filter_entry_spec(const struct efx_mcdi_filter_table *table,
28 unsigned int filter_idx)
30 return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
31 ~EFX_EF10_FILTER_FLAGS);
35 efx_mcdi_filter_entry_flags(const struct efx_mcdi_filter_table *table,
36 unsigned int filter_idx)
38 return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
41 static u32 efx_mcdi_filter_get_unsafe_id(u32 filter_id)
43 WARN_ON_ONCE(filter_id == EFX_EF10_FILTER_ID_INVALID);
44 return filter_id & (EFX_MCDI_FILTER_TBL_ROWS - 1);
47 static unsigned int efx_mcdi_filter_get_unsafe_pri(u32 filter_id)
49 return filter_id / (EFX_MCDI_FILTER_TBL_ROWS * 2);
52 static u32 efx_mcdi_filter_make_filter_id(unsigned int pri, u16 idx)
54 return pri * EFX_MCDI_FILTER_TBL_ROWS * 2 + idx;
58 * Decide whether a filter should be exclusive or else should allow
59 * delivery to additional recipients. Currently we decide that
60 * filters for specific local unicast MAC and IP addresses are
63 static bool efx_mcdi_filter_is_exclusive(const struct efx_filter_spec *spec)
65 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
66 !is_multicast_ether_addr(spec->loc_mac))
69 if ((spec->match_flags &
70 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
71 (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
72 if (spec->ether_type == htons(ETH_P_IP) &&
73 !ipv4_is_multicast(spec->loc_host[0]))
75 if (spec->ether_type == htons(ETH_P_IPV6) &&
76 ((const u8 *)spec->loc_host)[0] != 0xff)
84 efx_mcdi_filter_set_entry(struct efx_mcdi_filter_table *table,
85 unsigned int filter_idx,
86 const struct efx_filter_spec *spec,
89 table->entry[filter_idx].spec = (unsigned long)spec | flags;
93 efx_mcdi_filter_push_prep_set_match_fields(struct efx_nic *efx,
94 const struct efx_filter_spec *spec,
97 enum efx_encap_type encap_type = efx_filter_get_encap_type(spec);
98 u32 match_fields = 0, uc_match, mc_match;
100 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
101 efx_mcdi_filter_is_exclusive(spec) ?
102 MC_CMD_FILTER_OP_IN_OP_INSERT :
103 MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
106 * Convert match flags and values. Unlike almost
107 * everything else in MCDI, these fields are in
108 * network byte order.
110 #define COPY_VALUE(value, mcdi_field) \
113 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
114 mcdi_field ## _LBN; \
116 MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
118 memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
119 &value, sizeof(value)); \
121 #define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
122 if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
123 COPY_VALUE(spec->gen_field, mcdi_field); \
126 * Handle encap filters first. They will always be mismatch
127 * (unknown UC or MC) filters
131 * ether_type and outer_ip_proto need to be variables
132 * because COPY_VALUE wants to memcpy them
135 htons(encap_type & EFX_ENCAP_FLAG_IPV6 ?
136 ETH_P_IPV6 : ETH_P_IP);
137 u8 vni_type = MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_GENEVE;
140 switch (encap_type & EFX_ENCAP_TYPES_MASK) {
141 case EFX_ENCAP_TYPE_VXLAN:
142 vni_type = MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_VXLAN;
144 case EFX_ENCAP_TYPE_GENEVE:
145 COPY_VALUE(ether_type, ETHER_TYPE);
146 outer_ip_proto = IPPROTO_UDP;
147 COPY_VALUE(outer_ip_proto, IP_PROTO);
149 * We always need to set the type field, even
150 * though we're not matching on the TNI.
152 MCDI_POPULATE_DWORD_1(inbuf,
153 FILTER_OP_EXT_IN_VNI_OR_VSID,
154 FILTER_OP_EXT_IN_VNI_TYPE,
157 case EFX_ENCAP_TYPE_NVGRE:
158 COPY_VALUE(ether_type, ETHER_TYPE);
159 outer_ip_proto = IPPROTO_GRE;
160 COPY_VALUE(outer_ip_proto, IP_PROTO);
166 uc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_UCAST_DST_LBN;
167 mc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_MCAST_DST_LBN;
169 uc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
170 mc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_MCAST_DST_LBN;
173 if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
175 is_multicast_ether_addr(spec->loc_mac) ?
178 COPY_FIELD(REM_HOST, rem_host, SRC_IP);
179 COPY_FIELD(LOC_HOST, loc_host, DST_IP);
180 COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
181 COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
182 COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
183 COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
184 COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
185 COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
186 COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
187 COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
190 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
194 static void efx_mcdi_filter_push_prep(struct efx_nic *efx,
195 const struct efx_filter_spec *spec,
196 efx_dword_t *inbuf, u64 handle,
197 struct efx_rss_context *ctx,
200 u32 flags = spec->flags;
202 memset(inbuf, 0, MC_CMD_FILTER_OP_EXT_IN_LEN);
204 /* If RSS filter, caller better have given us an RSS context */
205 if (flags & EFX_FILTER_FLAG_RX_RSS) {
207 * We don't have the ability to return an error, so we'll just
208 * log a warning and disable RSS for the filter.
210 if (WARN_ON_ONCE(!ctx))
211 flags &= ~EFX_FILTER_FLAG_RX_RSS;
212 else if (WARN_ON_ONCE(ctx->context_id == EFX_MCDI_RSS_CONTEXT_INVALID))
213 flags &= ~EFX_FILTER_FLAG_RX_RSS;
217 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
218 MC_CMD_FILTER_OP_IN_OP_REPLACE);
219 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
221 efx_mcdi_filter_push_prep_set_match_fields(efx, spec, inbuf);
224 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, efx->vport_id);
225 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
226 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
227 MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
228 MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
229 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DOMAIN, 0);
230 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
231 MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
232 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE,
233 spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
235 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
236 (flags & EFX_FILTER_FLAG_RX_RSS) ?
237 MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
238 MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
239 if (flags & EFX_FILTER_FLAG_RX_RSS)
240 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT, ctx->context_id);
243 static int efx_mcdi_filter_push(struct efx_nic *efx,
244 const struct efx_filter_spec *spec, u64 *handle,
245 struct efx_rss_context *ctx, bool replacing)
247 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_EXT_IN_LEN);
248 MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_EXT_OUT_LEN);
252 efx_mcdi_filter_push_prep(efx, spec, inbuf, *handle, ctx, replacing);
253 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
254 outbuf, sizeof(outbuf), &outlen);
255 if (rc && spec->priority != EFX_FILTER_PRI_HINT)
256 efx_mcdi_display_error(efx, MC_CMD_FILTER_OP, sizeof(inbuf),
259 *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
261 rc = -EBUSY; /* to match efx_farch_filter_insert() */
265 static u32 efx_mcdi_filter_mcdi_flags_from_spec(const struct efx_filter_spec *spec)
267 enum efx_encap_type encap_type = efx_filter_get_encap_type(spec);
268 unsigned int match_flags = spec->match_flags;
269 unsigned int uc_match, mc_match;
272 #define MAP_FILTER_TO_MCDI_FLAG(gen_flag, mcdi_field, encap) { \
273 unsigned int old_match_flags = match_flags; \
274 match_flags &= ~EFX_FILTER_MATCH_ ## gen_flag; \
275 if (match_flags != old_match_flags) \
278 MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_ ## \
279 mcdi_field ## _LBN : \
280 MC_CMD_FILTER_OP_EXT_IN_MATCH_ ##\
281 mcdi_field ## _LBN)); \
283 /* inner or outer based on encap type */
284 MAP_FILTER_TO_MCDI_FLAG(REM_HOST, SRC_IP, encap_type);
285 MAP_FILTER_TO_MCDI_FLAG(LOC_HOST, DST_IP, encap_type);
286 MAP_FILTER_TO_MCDI_FLAG(REM_MAC, SRC_MAC, encap_type);
287 MAP_FILTER_TO_MCDI_FLAG(REM_PORT, SRC_PORT, encap_type);
288 MAP_FILTER_TO_MCDI_FLAG(LOC_MAC, DST_MAC, encap_type);
289 MAP_FILTER_TO_MCDI_FLAG(LOC_PORT, DST_PORT, encap_type);
290 MAP_FILTER_TO_MCDI_FLAG(ETHER_TYPE, ETHER_TYPE, encap_type);
291 MAP_FILTER_TO_MCDI_FLAG(IP_PROTO, IP_PROTO, encap_type);
293 MAP_FILTER_TO_MCDI_FLAG(INNER_VID, INNER_VLAN, false);
294 MAP_FILTER_TO_MCDI_FLAG(OUTER_VID, OUTER_VLAN, false);
295 #undef MAP_FILTER_TO_MCDI_FLAG
297 /* special handling for encap type, and mismatch */
299 match_flags &= ~EFX_FILTER_MATCH_ENCAP_TYPE;
301 (1 << MC_CMD_FILTER_OP_EXT_IN_MATCH_ETHER_TYPE_LBN);
302 mcdi_flags |= (1 << MC_CMD_FILTER_OP_EXT_IN_MATCH_IP_PROTO_LBN);
304 uc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_UCAST_DST_LBN;
305 mc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_MCAST_DST_LBN;
307 uc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
308 mc_match = MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_MCAST_DST_LBN;
311 if (match_flags & EFX_FILTER_MATCH_LOC_MAC_IG) {
312 match_flags &= ~EFX_FILTER_MATCH_LOC_MAC_IG;
314 is_multicast_ether_addr(spec->loc_mac) ?
319 /* Did we map them all? */
320 WARN_ON_ONCE(match_flags);
325 static int efx_mcdi_filter_pri(struct efx_mcdi_filter_table *table,
326 const struct efx_filter_spec *spec)
328 u32 mcdi_flags = efx_mcdi_filter_mcdi_flags_from_spec(spec);
329 unsigned int match_pri;
332 match_pri < table->rx_match_count;
334 if (table->rx_match_mcdi_flags[match_pri] == mcdi_flags)
337 return -EPROTONOSUPPORT;
340 static s32 efx_mcdi_filter_insert_locked(struct efx_nic *efx,
341 struct efx_filter_spec *spec,
344 DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
345 struct efx_mcdi_filter_table *table;
346 struct efx_filter_spec *saved_spec;
347 struct efx_rss_context *ctx = NULL;
348 unsigned int match_pri, hash;
349 unsigned int priv_flags;
350 bool rss_locked = false;
351 bool replacing = false;
352 unsigned int depth, i;
358 WARN_ON(!rwsem_is_locked(&efx->filter_sem));
359 table = efx->filter_state;
360 down_write(&table->lock);
362 /* For now, only support RX filters */
363 if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
364 EFX_FILTER_FLAG_RX) {
369 rc = efx_mcdi_filter_pri(table, spec);
374 hash = efx_filter_spec_hash(spec);
375 is_mc_recip = efx_filter_is_mc_recipient(spec);
377 bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
379 if (spec->flags & EFX_FILTER_FLAG_RX_RSS) {
380 mutex_lock(&efx->rss_lock);
382 if (spec->rss_context)
383 ctx = efx_find_rss_context_entry(efx, spec->rss_context);
385 ctx = &efx->rss_context;
390 if (ctx->context_id == EFX_MCDI_RSS_CONTEXT_INVALID) {
396 /* Find any existing filters with the same match tuple or
397 * else a free slot to insert at.
399 for (depth = 1; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
400 i = (hash + depth) & (EFX_MCDI_FILTER_TBL_ROWS - 1);
401 saved_spec = efx_mcdi_filter_entry_spec(table, i);
406 } else if (efx_filter_spec_equal(spec, saved_spec)) {
407 if (spec->priority < saved_spec->priority &&
408 spec->priority != EFX_FILTER_PRI_AUTO) {
413 /* This is the only one */
414 if (spec->priority ==
415 saved_spec->priority &&
422 } else if (spec->priority >
423 saved_spec->priority ||
425 saved_spec->priority &&
430 __set_bit(depth, mc_rem_map);
435 /* Once we reach the maximum search depth, use the first suitable
436 * slot, or return -EBUSY if there was none
443 /* Create a software table entry if necessary. */
444 saved_spec = efx_mcdi_filter_entry_spec(table, ins_index);
446 if (spec->priority == EFX_FILTER_PRI_AUTO &&
447 saved_spec->priority >= EFX_FILTER_PRI_AUTO) {
448 /* Just make sure it won't be removed */
449 if (saved_spec->priority > EFX_FILTER_PRI_AUTO)
450 saved_spec->flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
451 table->entry[ins_index].spec &=
452 ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
457 priv_flags = efx_mcdi_filter_entry_flags(table, ins_index);
459 saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
467 efx_mcdi_filter_set_entry(table, ins_index, saved_spec, priv_flags);
469 /* Actually insert the filter on the HW */
470 rc = efx_mcdi_filter_push(efx, spec, &table->entry[ins_index].handle,
473 if (rc == -EINVAL && efx->must_realloc_vis)
474 /* The MC rebooted under us, causing it to reject our filter
475 * insertion as pointing to an invalid VI (spec->dmaq_id).
479 /* Finalise the software table entry */
482 /* Update the fields that may differ */
483 if (saved_spec->priority == EFX_FILTER_PRI_AUTO)
485 EFX_FILTER_FLAG_RX_OVER_AUTO;
486 saved_spec->priority = spec->priority;
487 saved_spec->flags &= EFX_FILTER_FLAG_RX_OVER_AUTO;
488 saved_spec->flags |= spec->flags;
489 saved_spec->rss_context = spec->rss_context;
490 saved_spec->dmaq_id = spec->dmaq_id;
492 } else if (!replacing) {
496 /* We failed to replace, so the old filter is still present.
497 * Roll back the software table to reflect this. In fact the
498 * efx_mcdi_filter_set_entry() call below will do the right
499 * thing, so nothing extra is needed here.
502 efx_mcdi_filter_set_entry(table, ins_index, saved_spec, priv_flags);
504 /* Remove and finalise entries for lower-priority multicast
508 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_EXT_IN_LEN);
509 unsigned int depth, i;
511 memset(inbuf, 0, sizeof(inbuf));
513 for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
514 if (!test_bit(depth, mc_rem_map))
517 i = (hash + depth) & (EFX_MCDI_FILTER_TBL_ROWS - 1);
518 saved_spec = efx_mcdi_filter_entry_spec(table, i);
519 priv_flags = efx_mcdi_filter_entry_flags(table, i);
522 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
523 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
524 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
525 table->entry[i].handle);
526 rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
527 inbuf, sizeof(inbuf),
536 efx_mcdi_filter_set_entry(table, i, saved_spec,
541 /* If successful, return the inserted filter ID */
543 rc = efx_mcdi_filter_make_filter_id(match_pri, ins_index);
547 mutex_unlock(&efx->rss_lock);
548 up_write(&table->lock);
552 s32 efx_mcdi_filter_insert(struct efx_nic *efx, struct efx_filter_spec *spec,
557 down_read(&efx->filter_sem);
558 ret = efx_mcdi_filter_insert_locked(efx, spec, replace_equal);
559 up_read(&efx->filter_sem);
566 * If !by_index, remove by ID
567 * If by_index, remove by index
568 * Filter ID may come from userland and must be range-checked.
569 * Caller must hold efx->filter_sem for read, and efx->filter_state->lock
572 static int efx_mcdi_filter_remove_internal(struct efx_nic *efx,
573 unsigned int priority_mask,
574 u32 filter_id, bool by_index)
576 unsigned int filter_idx = efx_mcdi_filter_get_unsafe_id(filter_id);
577 struct efx_mcdi_filter_table *table = efx->filter_state;
578 MCDI_DECLARE_BUF(inbuf,
579 MC_CMD_FILTER_OP_IN_HANDLE_OFST +
580 MC_CMD_FILTER_OP_IN_HANDLE_LEN);
581 struct efx_filter_spec *spec;
585 spec = efx_mcdi_filter_entry_spec(table, filter_idx);
588 efx_mcdi_filter_pri(table, spec) !=
589 efx_mcdi_filter_get_unsafe_pri(filter_id)))
592 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO &&
593 priority_mask == (1U << EFX_FILTER_PRI_AUTO)) {
594 /* Just remove flags */
595 spec->flags &= ~EFX_FILTER_FLAG_RX_OVER_AUTO;
596 table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
600 if (!(priority_mask & (1U << spec->priority)))
603 if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
604 /* Reset to an automatic filter */
606 struct efx_filter_spec new_spec = *spec;
608 new_spec.priority = EFX_FILTER_PRI_AUTO;
609 new_spec.flags = (EFX_FILTER_FLAG_RX |
610 (efx_rss_active(&efx->rss_context) ?
611 EFX_FILTER_FLAG_RX_RSS : 0));
612 new_spec.dmaq_id = 0;
613 new_spec.rss_context = 0;
614 rc = efx_mcdi_filter_push(efx, &new_spec,
615 &table->entry[filter_idx].handle,
622 /* Really remove the filter */
624 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
625 efx_mcdi_filter_is_exclusive(spec) ?
626 MC_CMD_FILTER_OP_IN_OP_REMOVE :
627 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
628 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
629 table->entry[filter_idx].handle);
630 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FILTER_OP,
631 inbuf, sizeof(inbuf), NULL, 0, NULL);
633 if ((rc == 0) || (rc == -ENOENT)) {
634 /* Filter removed OK or didn't actually exist */
636 efx_mcdi_filter_set_entry(table, filter_idx, NULL, 0);
638 efx_mcdi_display_error(efx, MC_CMD_FILTER_OP,
639 MC_CMD_FILTER_OP_EXT_IN_LEN,
647 /* Remove filters that weren't renewed. */
648 static void efx_mcdi_filter_remove_old(struct efx_nic *efx)
650 struct efx_mcdi_filter_table *table = efx->filter_state;
651 int remove_failed = 0;
652 int remove_noent = 0;
656 down_write(&table->lock);
657 for (i = 0; i < EFX_MCDI_FILTER_TBL_ROWS; i++) {
658 if (READ_ONCE(table->entry[i].spec) &
659 EFX_EF10_FILTER_FLAG_AUTO_OLD) {
660 rc = efx_mcdi_filter_remove_internal(efx,
661 1U << EFX_FILTER_PRI_AUTO, i, true);
668 up_write(&table->lock);
671 netif_info(efx, drv, efx->net_dev,
672 "%s: failed to remove %d filters\n",
673 __func__, remove_failed);
675 netif_info(efx, drv, efx->net_dev,
676 "%s: failed to remove %d non-existent filters\n",
677 __func__, remove_noent);
680 int efx_mcdi_filter_remove_safe(struct efx_nic *efx,
681 enum efx_filter_priority priority,
684 struct efx_mcdi_filter_table *table;
687 down_read(&efx->filter_sem);
688 table = efx->filter_state;
689 down_write(&table->lock);
690 rc = efx_mcdi_filter_remove_internal(efx, 1U << priority, filter_id,
692 up_write(&table->lock);
693 up_read(&efx->filter_sem);
697 /* Caller must hold efx->filter_sem for read */
698 static void efx_mcdi_filter_remove_unsafe(struct efx_nic *efx,
699 enum efx_filter_priority priority,
702 struct efx_mcdi_filter_table *table = efx->filter_state;
704 if (filter_id == EFX_EF10_FILTER_ID_INVALID)
707 down_write(&table->lock);
708 efx_mcdi_filter_remove_internal(efx, 1U << priority, filter_id,
710 up_write(&table->lock);
713 int efx_mcdi_filter_get_safe(struct efx_nic *efx,
714 enum efx_filter_priority priority,
715 u32 filter_id, struct efx_filter_spec *spec)
717 unsigned int filter_idx = efx_mcdi_filter_get_unsafe_id(filter_id);
718 const struct efx_filter_spec *saved_spec;
719 struct efx_mcdi_filter_table *table;
722 down_read(&efx->filter_sem);
723 table = efx->filter_state;
724 down_read(&table->lock);
725 saved_spec = efx_mcdi_filter_entry_spec(table, filter_idx);
726 if (saved_spec && saved_spec->priority == priority &&
727 efx_mcdi_filter_pri(table, saved_spec) ==
728 efx_mcdi_filter_get_unsafe_pri(filter_id)) {
734 up_read(&table->lock);
735 up_read(&efx->filter_sem);
739 static int efx_mcdi_filter_insert_addr_list(struct efx_nic *efx,
740 struct efx_mcdi_filter_vlan *vlan,
741 bool multicast, bool rollback)
743 struct efx_mcdi_filter_table *table = efx->filter_state;
744 struct efx_mcdi_dev_addr *addr_list;
745 enum efx_filter_flags filter_flags;
746 struct efx_filter_spec spec;
754 addr_list = table->dev_mc_list;
755 addr_count = table->dev_mc_count;
758 addr_list = table->dev_uc_list;
759 addr_count = table->dev_uc_count;
763 filter_flags = efx_rss_active(&efx->rss_context) ? EFX_FILTER_FLAG_RX_RSS : 0;
765 /* Insert/renew filters */
766 for (i = 0; i < addr_count; i++) {
767 EFX_WARN_ON_PARANOID(ids[i] != EFX_EF10_FILTER_ID_INVALID);
768 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
769 efx_filter_set_eth_local(&spec, vlan->vid, addr_list[i].addr);
770 rc = efx_mcdi_filter_insert_locked(efx, &spec, true);
773 netif_info(efx, drv, efx->net_dev,
774 "efx_mcdi_filter_insert failed rc=%d\n",
776 /* Fall back to promiscuous */
777 for (j = 0; j < i; j++) {
778 efx_mcdi_filter_remove_unsafe(
779 efx, EFX_FILTER_PRI_AUTO,
781 ids[j] = EFX_EF10_FILTER_ID_INVALID;
785 /* keep invalid ID, and carry on */
788 ids[i] = efx_mcdi_filter_get_unsafe_id(rc);
792 if (multicast && rollback) {
793 /* Also need an Ethernet broadcast filter */
794 EFX_WARN_ON_PARANOID(vlan->default_filters[EFX_EF10_BCAST] !=
795 EFX_EF10_FILTER_ID_INVALID);
796 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
797 eth_broadcast_addr(baddr);
798 efx_filter_set_eth_local(&spec, vlan->vid, baddr);
799 rc = efx_mcdi_filter_insert_locked(efx, &spec, true);
801 netif_warn(efx, drv, efx->net_dev,
802 "Broadcast filter insert failed rc=%d\n", rc);
803 /* Fall back to promiscuous */
804 for (j = 0; j < i; j++) {
805 efx_mcdi_filter_remove_unsafe(
806 efx, EFX_FILTER_PRI_AUTO,
808 ids[j] = EFX_EF10_FILTER_ID_INVALID;
812 vlan->default_filters[EFX_EF10_BCAST] =
813 efx_mcdi_filter_get_unsafe_id(rc);
820 static int efx_mcdi_filter_insert_def(struct efx_nic *efx,
821 struct efx_mcdi_filter_vlan *vlan,
822 enum efx_encap_type encap_type,
823 bool multicast, bool rollback)
825 struct efx_mcdi_filter_table *table = efx->filter_state;
826 enum efx_filter_flags filter_flags;
827 struct efx_filter_spec spec;
832 filter_flags = efx_rss_active(&efx->rss_context) ? EFX_FILTER_FLAG_RX_RSS : 0;
834 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO, filter_flags, 0);
837 efx_filter_set_mc_def(&spec);
839 efx_filter_set_uc_def(&spec);
842 if (efx_has_cap(efx, VXLAN_NVGRE))
843 efx_filter_set_encap_type(&spec, encap_type);
846 * don't insert encap filters on non-supporting
847 * platforms. ID will be left as INVALID.
852 if (vlan->vid != EFX_FILTER_VID_UNSPEC)
853 efx_filter_set_eth_local(&spec, vlan->vid, NULL);
855 rc = efx_mcdi_filter_insert_locked(efx, &spec, true);
857 const char *um = multicast ? "Multicast" : "Unicast";
858 const char *encap_name = "";
859 const char *encap_ipv = "";
861 if ((encap_type & EFX_ENCAP_TYPES_MASK) ==
862 EFX_ENCAP_TYPE_VXLAN)
863 encap_name = "VXLAN ";
864 else if ((encap_type & EFX_ENCAP_TYPES_MASK) ==
865 EFX_ENCAP_TYPE_NVGRE)
866 encap_name = "NVGRE ";
867 else if ((encap_type & EFX_ENCAP_TYPES_MASK) ==
868 EFX_ENCAP_TYPE_GENEVE)
869 encap_name = "GENEVE ";
870 if (encap_type & EFX_ENCAP_FLAG_IPV6)
876 * unprivileged functions can't insert mismatch filters
877 * for encapsulated or unicast traffic, so downgrade
878 * those warnings to debug.
880 netif_cond_dbg(efx, drv, efx->net_dev,
881 rc == -EPERM && (encap_type || !multicast), warn,
882 "%s%s%s mismatch filter insert failed rc=%d\n",
883 encap_name, encap_ipv, um, rc);
884 } else if (multicast) {
885 /* mapping from encap types to default filter IDs (multicast) */
886 static enum efx_mcdi_filter_default_filters map[] = {
887 [EFX_ENCAP_TYPE_NONE] = EFX_EF10_MCDEF,
888 [EFX_ENCAP_TYPE_VXLAN] = EFX_EF10_VXLAN4_MCDEF,
889 [EFX_ENCAP_TYPE_NVGRE] = EFX_EF10_NVGRE4_MCDEF,
890 [EFX_ENCAP_TYPE_GENEVE] = EFX_EF10_GENEVE4_MCDEF,
891 [EFX_ENCAP_TYPE_VXLAN | EFX_ENCAP_FLAG_IPV6] =
892 EFX_EF10_VXLAN6_MCDEF,
893 [EFX_ENCAP_TYPE_NVGRE | EFX_ENCAP_FLAG_IPV6] =
894 EFX_EF10_NVGRE6_MCDEF,
895 [EFX_ENCAP_TYPE_GENEVE | EFX_ENCAP_FLAG_IPV6] =
896 EFX_EF10_GENEVE6_MCDEF,
899 /* quick bounds check (BCAST result impossible) */
900 BUILD_BUG_ON(EFX_EF10_BCAST != 0);
901 if (encap_type >= ARRAY_SIZE(map) || map[encap_type] == 0) {
905 /* then follow map */
906 id = &vlan->default_filters[map[encap_type]];
908 EFX_WARN_ON_PARANOID(*id != EFX_EF10_FILTER_ID_INVALID);
909 *id = efx_mcdi_filter_get_unsafe_id(rc);
910 if (!table->mc_chaining && !encap_type) {
911 /* Also need an Ethernet broadcast filter */
912 efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
914 eth_broadcast_addr(baddr);
915 efx_filter_set_eth_local(&spec, vlan->vid, baddr);
916 rc = efx_mcdi_filter_insert_locked(efx, &spec, true);
918 netif_warn(efx, drv, efx->net_dev,
919 "Broadcast filter insert failed rc=%d\n",
922 /* Roll back the mc_def filter */
923 efx_mcdi_filter_remove_unsafe(
924 efx, EFX_FILTER_PRI_AUTO,
926 *id = EFX_EF10_FILTER_ID_INVALID;
930 EFX_WARN_ON_PARANOID(
931 vlan->default_filters[EFX_EF10_BCAST] !=
932 EFX_EF10_FILTER_ID_INVALID);
933 vlan->default_filters[EFX_EF10_BCAST] =
934 efx_mcdi_filter_get_unsafe_id(rc);
939 /* mapping from encap types to default filter IDs (unicast) */
940 static enum efx_mcdi_filter_default_filters map[] = {
941 [EFX_ENCAP_TYPE_NONE] = EFX_EF10_UCDEF,
942 [EFX_ENCAP_TYPE_VXLAN] = EFX_EF10_VXLAN4_UCDEF,
943 [EFX_ENCAP_TYPE_NVGRE] = EFX_EF10_NVGRE4_UCDEF,
944 [EFX_ENCAP_TYPE_GENEVE] = EFX_EF10_GENEVE4_UCDEF,
945 [EFX_ENCAP_TYPE_VXLAN | EFX_ENCAP_FLAG_IPV6] =
946 EFX_EF10_VXLAN6_UCDEF,
947 [EFX_ENCAP_TYPE_NVGRE | EFX_ENCAP_FLAG_IPV6] =
948 EFX_EF10_NVGRE6_UCDEF,
949 [EFX_ENCAP_TYPE_GENEVE | EFX_ENCAP_FLAG_IPV6] =
950 EFX_EF10_GENEVE6_UCDEF,
953 /* quick bounds check (BCAST result impossible) */
954 BUILD_BUG_ON(EFX_EF10_BCAST != 0);
955 if (encap_type >= ARRAY_SIZE(map) || map[encap_type] == 0) {
959 /* then follow map */
960 id = &vlan->default_filters[map[encap_type]];
961 EFX_WARN_ON_PARANOID(*id != EFX_EF10_FILTER_ID_INVALID);
969 * Caller must hold efx->filter_sem for read if race against
970 * efx_mcdi_filter_table_remove() is possible
972 static void efx_mcdi_filter_vlan_sync_rx_mode(struct efx_nic *efx,
973 struct efx_mcdi_filter_vlan *vlan)
975 struct efx_mcdi_filter_table *table = efx->filter_state;
978 * Do not install unspecified VID if VLAN filtering is enabled.
979 * Do not install all specified VIDs if VLAN filtering is disabled.
981 if ((vlan->vid == EFX_FILTER_VID_UNSPEC) == table->vlan_filter)
984 /* Insert/renew unicast filters */
985 if (table->uc_promisc) {
986 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_NONE,
988 efx_mcdi_filter_insert_addr_list(efx, vlan, false, false);
991 * If any of the filters failed to insert, fall back to
992 * promiscuous mode - add in the uc_def filter. But keep
993 * our individual unicast filters.
995 if (efx_mcdi_filter_insert_addr_list(efx, vlan, false, false))
996 efx_mcdi_filter_insert_def(efx, vlan,
1000 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_VXLAN,
1002 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_VXLAN |
1003 EFX_ENCAP_FLAG_IPV6,
1005 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_NVGRE,
1007 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_NVGRE |
1008 EFX_ENCAP_FLAG_IPV6,
1010 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_GENEVE,
1012 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_GENEVE |
1013 EFX_ENCAP_FLAG_IPV6,
1017 * Insert/renew multicast filters
1019 * If changing promiscuous state with cascaded multicast filters, remove
1020 * old filters first, so that packets are dropped rather than duplicated
1022 if (table->mc_chaining && table->mc_promisc_last != table->mc_promisc)
1023 efx_mcdi_filter_remove_old(efx);
1024 if (table->mc_promisc) {
1025 if (table->mc_chaining) {
1027 * If we failed to insert promiscuous filters, rollback
1028 * and fall back to individual multicast filters
1030 if (efx_mcdi_filter_insert_def(efx, vlan,
1031 EFX_ENCAP_TYPE_NONE,
1033 /* Changing promisc state, so remove old filters */
1034 efx_mcdi_filter_remove_old(efx);
1035 efx_mcdi_filter_insert_addr_list(efx, vlan,
1040 * If we failed to insert promiscuous filters, don't
1041 * rollback. Regardless, also insert the mc_list,
1042 * unless it's incomplete due to overflow
1044 efx_mcdi_filter_insert_def(efx, vlan,
1045 EFX_ENCAP_TYPE_NONE,
1047 if (!table->mc_overflow)
1048 efx_mcdi_filter_insert_addr_list(efx, vlan,
1053 * If any filters failed to insert, rollback and fall back to
1054 * promiscuous mode - mc_def filter and maybe broadcast. If
1055 * that fails, roll back again and insert as many of our
1056 * individual multicast filters as we can.
1058 if (efx_mcdi_filter_insert_addr_list(efx, vlan, true, true)) {
1059 /* Changing promisc state, so remove old filters */
1060 if (table->mc_chaining)
1061 efx_mcdi_filter_remove_old(efx);
1062 if (efx_mcdi_filter_insert_def(efx, vlan,
1063 EFX_ENCAP_TYPE_NONE,
1065 efx_mcdi_filter_insert_addr_list(efx, vlan,
1069 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_VXLAN,
1071 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_VXLAN |
1072 EFX_ENCAP_FLAG_IPV6,
1074 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_NVGRE,
1076 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_NVGRE |
1077 EFX_ENCAP_FLAG_IPV6,
1079 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_GENEVE,
1081 efx_mcdi_filter_insert_def(efx, vlan, EFX_ENCAP_TYPE_GENEVE |
1082 EFX_ENCAP_FLAG_IPV6,
1086 int efx_mcdi_filter_clear_rx(struct efx_nic *efx,
1087 enum efx_filter_priority priority)
1089 struct efx_mcdi_filter_table *table;
1090 unsigned int priority_mask;
1094 priority_mask = (((1U << (priority + 1)) - 1) &
1095 ~(1U << EFX_FILTER_PRI_AUTO));
1097 down_read(&efx->filter_sem);
1098 table = efx->filter_state;
1099 down_write(&table->lock);
1100 for (i = 0; i < EFX_MCDI_FILTER_TBL_ROWS; i++) {
1101 rc = efx_mcdi_filter_remove_internal(efx, priority_mask,
1103 if (rc && rc != -ENOENT)
1108 up_write(&table->lock);
1109 up_read(&efx->filter_sem);
1113 u32 efx_mcdi_filter_count_rx_used(struct efx_nic *efx,
1114 enum efx_filter_priority priority)
1116 struct efx_mcdi_filter_table *table;
1117 unsigned int filter_idx;
1120 down_read(&efx->filter_sem);
1121 table = efx->filter_state;
1122 down_read(&table->lock);
1123 for (filter_idx = 0; filter_idx < EFX_MCDI_FILTER_TBL_ROWS; filter_idx++) {
1124 if (table->entry[filter_idx].spec &&
1125 efx_mcdi_filter_entry_spec(table, filter_idx)->priority ==
1129 up_read(&table->lock);
1130 up_read(&efx->filter_sem);
1134 u32 efx_mcdi_filter_get_rx_id_limit(struct efx_nic *efx)
1136 struct efx_mcdi_filter_table *table = efx->filter_state;
1138 return table->rx_match_count * EFX_MCDI_FILTER_TBL_ROWS * 2;
1141 s32 efx_mcdi_filter_get_rx_ids(struct efx_nic *efx,
1142 enum efx_filter_priority priority,
1145 struct efx_mcdi_filter_table *table;
1146 struct efx_filter_spec *spec;
1147 unsigned int filter_idx;
1150 down_read(&efx->filter_sem);
1151 table = efx->filter_state;
1152 down_read(&table->lock);
1154 for (filter_idx = 0; filter_idx < EFX_MCDI_FILTER_TBL_ROWS; filter_idx++) {
1155 spec = efx_mcdi_filter_entry_spec(table, filter_idx);
1156 if (spec && spec->priority == priority) {
1157 if (count == size) {
1162 efx_mcdi_filter_make_filter_id(
1163 efx_mcdi_filter_pri(table, spec),
1167 up_read(&table->lock);
1168 up_read(&efx->filter_sem);
1172 static int efx_mcdi_filter_match_flags_from_mcdi(bool encap, u32 mcdi_flags)
1174 int match_flags = 0;
1176 #define MAP_FLAG(gen_flag, mcdi_field) do { \
1177 u32 old_mcdi_flags = mcdi_flags; \
1178 mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_EXT_IN_MATCH_ ## \
1179 mcdi_field ## _LBN); \
1180 if (mcdi_flags != old_mcdi_flags) \
1181 match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
1185 /* encap filters must specify encap type */
1186 match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
1187 /* and imply ethertype and ip proto */
1189 ~(1 << MC_CMD_FILTER_OP_EXT_IN_MATCH_IP_PROTO_LBN);
1191 ~(1 << MC_CMD_FILTER_OP_EXT_IN_MATCH_ETHER_TYPE_LBN);
1192 /* VLAN tags refer to the outer packet */
1193 MAP_FLAG(INNER_VID, INNER_VLAN);
1194 MAP_FLAG(OUTER_VID, OUTER_VLAN);
1195 /* everything else refers to the inner packet */
1196 MAP_FLAG(LOC_MAC_IG, IFRM_UNKNOWN_UCAST_DST);
1197 MAP_FLAG(LOC_MAC_IG, IFRM_UNKNOWN_MCAST_DST);
1198 MAP_FLAG(REM_HOST, IFRM_SRC_IP);
1199 MAP_FLAG(LOC_HOST, IFRM_DST_IP);
1200 MAP_FLAG(REM_MAC, IFRM_SRC_MAC);
1201 MAP_FLAG(REM_PORT, IFRM_SRC_PORT);
1202 MAP_FLAG(LOC_MAC, IFRM_DST_MAC);
1203 MAP_FLAG(LOC_PORT, IFRM_DST_PORT);
1204 MAP_FLAG(ETHER_TYPE, IFRM_ETHER_TYPE);
1205 MAP_FLAG(IP_PROTO, IFRM_IP_PROTO);
1207 MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
1208 MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
1209 MAP_FLAG(REM_HOST, SRC_IP);
1210 MAP_FLAG(LOC_HOST, DST_IP);
1211 MAP_FLAG(REM_MAC, SRC_MAC);
1212 MAP_FLAG(REM_PORT, SRC_PORT);
1213 MAP_FLAG(LOC_MAC, DST_MAC);
1214 MAP_FLAG(LOC_PORT, DST_PORT);
1215 MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
1216 MAP_FLAG(INNER_VID, INNER_VLAN);
1217 MAP_FLAG(OUTER_VID, OUTER_VLAN);
1218 MAP_FLAG(IP_PROTO, IP_PROTO);
1222 /* Did we map them all? */
1229 bool efx_mcdi_filter_match_supported(struct efx_mcdi_filter_table *table,
1231 enum efx_filter_match_flags match_flags)
1233 unsigned int match_pri;
1237 match_pri < table->rx_match_count;
1239 mf = efx_mcdi_filter_match_flags_from_mcdi(encap,
1240 table->rx_match_mcdi_flags[match_pri]);
1241 if (mf == match_flags)
1249 efx_mcdi_filter_table_probe_matches(struct efx_nic *efx,
1250 struct efx_mcdi_filter_table *table,
1253 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
1254 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
1255 unsigned int pd_match_pri, pd_match_count;
1259 /* Find out which RX filter types are supported, and their priorities */
1260 MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
1262 MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_ENCAP_RX_MATCHES :
1263 MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
1264 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
1265 inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
1270 pd_match_count = MCDI_VAR_ARRAY_LEN(
1271 outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
1273 for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
1277 GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
1279 rc = efx_mcdi_filter_match_flags_from_mcdi(encap, mcdi_flags);
1281 netif_dbg(efx, probe, efx->net_dev,
1282 "%s: fw flags %#x pri %u not supported in driver\n",
1283 __func__, mcdi_flags, pd_match_pri);
1285 netif_dbg(efx, probe, efx->net_dev,
1286 "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
1287 __func__, mcdi_flags, pd_match_pri,
1288 rc, table->rx_match_count);
1289 table->rx_match_mcdi_flags[table->rx_match_count] = mcdi_flags;
1290 table->rx_match_count++;
1297 int efx_mcdi_filter_table_probe(struct efx_nic *efx, bool multicast_chaining)
1299 struct net_device *net_dev = efx->net_dev;
1300 struct efx_mcdi_filter_table *table;
1303 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1306 if (efx->filter_state) /* already probed */
1309 table = kzalloc(sizeof(*table), GFP_KERNEL);
1313 table->mc_chaining = multicast_chaining;
1314 table->rx_match_count = 0;
1315 rc = efx_mcdi_filter_table_probe_matches(efx, table, false);
1318 if (efx_has_cap(efx, VXLAN_NVGRE))
1319 rc = efx_mcdi_filter_table_probe_matches(efx, table, true);
1322 if ((efx_supported_features(efx) & NETIF_F_HW_VLAN_CTAG_FILTER) &&
1323 !(efx_mcdi_filter_match_supported(table, false,
1324 (EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_LOC_MAC)) &&
1325 efx_mcdi_filter_match_supported(table, false,
1326 (EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_LOC_MAC_IG)))) {
1327 netif_info(efx, probe, net_dev,
1328 "VLAN filters are not supported in this firmware variant\n");
1329 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1330 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1331 net_dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1334 table->entry = vzalloc(array_size(EFX_MCDI_FILTER_TBL_ROWS,
1335 sizeof(*table->entry)));
1336 if (!table->entry) {
1341 table->mc_promisc_last = false;
1342 table->vlan_filter =
1343 !!(efx->net_dev->features & NETIF_F_HW_VLAN_CTAG_FILTER);
1344 INIT_LIST_HEAD(&table->vlan_list);
1345 init_rwsem(&table->lock);
1347 efx->filter_state = table;
1355 void efx_mcdi_filter_table_reset_mc_allocations(struct efx_nic *efx)
1357 struct efx_mcdi_filter_table *table = efx->filter_state;
1360 table->must_restore_filters = true;
1361 table->must_restore_rss_contexts = true;
1366 * Caller must hold efx->filter_sem for read if race against
1367 * efx_mcdi_filter_table_remove() is possible
1369 void efx_mcdi_filter_table_restore(struct efx_nic *efx)
1371 struct efx_mcdi_filter_table *table = efx->filter_state;
1372 unsigned int invalid_filters = 0, failed = 0;
1373 struct efx_mcdi_filter_vlan *vlan;
1374 struct efx_filter_spec *spec;
1375 struct efx_rss_context *ctx;
1376 unsigned int filter_idx;
1381 WARN_ON(!rwsem_is_locked(&efx->filter_sem));
1383 if (!table || !table->must_restore_filters)
1386 down_write(&table->lock);
1387 mutex_lock(&efx->rss_lock);
1389 for (filter_idx = 0; filter_idx < EFX_MCDI_FILTER_TBL_ROWS; filter_idx++) {
1390 spec = efx_mcdi_filter_entry_spec(table, filter_idx);
1394 mcdi_flags = efx_mcdi_filter_mcdi_flags_from_spec(spec);
1396 while (match_pri < table->rx_match_count &&
1397 table->rx_match_mcdi_flags[match_pri] != mcdi_flags)
1399 if (match_pri >= table->rx_match_count) {
1403 if (spec->rss_context)
1404 ctx = efx_find_rss_context_entry(efx, spec->rss_context);
1406 ctx = &efx->rss_context;
1407 if (spec->flags & EFX_FILTER_FLAG_RX_RSS) {
1409 netif_warn(efx, drv, efx->net_dev,
1410 "Warning: unable to restore a filter with nonexistent RSS context %u.\n",
1415 if (ctx->context_id == EFX_MCDI_RSS_CONTEXT_INVALID) {
1416 netif_warn(efx, drv, efx->net_dev,
1417 "Warning: unable to restore a filter with RSS context %u as it was not created.\n",
1424 rc = efx_mcdi_filter_push(efx, spec,
1425 &table->entry[filter_idx].handle,
1432 list_for_each_entry(vlan, &table->vlan_list, list)
1433 for (i = 0; i < EFX_EF10_NUM_DEFAULT_FILTERS; ++i)
1434 if (vlan->default_filters[i] == filter_idx)
1435 vlan->default_filters[i] =
1436 EFX_EF10_FILTER_ID_INVALID;
1439 efx_mcdi_filter_set_entry(table, filter_idx, NULL, 0);
1443 mutex_unlock(&efx->rss_lock);
1444 up_write(&table->lock);
1447 * This can happen validly if the MC's capabilities have changed, so
1450 if (invalid_filters)
1451 netif_dbg(efx, drv, efx->net_dev,
1452 "Did not restore %u filters that are now unsupported.\n",
1456 netif_err(efx, hw, efx->net_dev,
1457 "unable to restore %u filters\n", failed);
1459 table->must_restore_filters = false;
1462 void efx_mcdi_filter_table_down(struct efx_nic *efx)
1464 struct efx_mcdi_filter_table *table = efx->filter_state;
1465 MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_EXT_IN_LEN);
1466 struct efx_filter_spec *spec;
1467 unsigned int filter_idx;
1473 efx_mcdi_filter_cleanup_vlans(efx);
1475 for (filter_idx = 0; filter_idx < EFX_MCDI_FILTER_TBL_ROWS; filter_idx++) {
1476 spec = efx_mcdi_filter_entry_spec(table, filter_idx);
1480 MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
1481 efx_mcdi_filter_is_exclusive(spec) ?
1482 MC_CMD_FILTER_OP_IN_OP_REMOVE :
1483 MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
1484 MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
1485 table->entry[filter_idx].handle);
1486 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FILTER_OP, inbuf,
1487 sizeof(inbuf), NULL, 0, NULL);
1489 netif_info(efx, drv, efx->net_dev,
1490 "%s: filter %04x remove failed\n",
1491 __func__, filter_idx);
1496 void efx_mcdi_filter_table_remove(struct efx_nic *efx)
1498 struct efx_mcdi_filter_table *table = efx->filter_state;
1500 efx_mcdi_filter_table_down(efx);
1502 efx->filter_state = NULL;
1504 * If we were called without locking, then it's not safe to free
1505 * the table as others might be using it. So we just WARN, leak
1506 * the memory, and potentially get an inconsistent filter table
1508 * This should never actually happen.
1510 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1516 vfree(table->entry);
1520 static void efx_mcdi_filter_mark_one_old(struct efx_nic *efx, uint16_t *id)
1522 struct efx_mcdi_filter_table *table = efx->filter_state;
1523 unsigned int filter_idx;
1525 efx_rwsem_assert_write_locked(&table->lock);
1527 if (*id != EFX_EF10_FILTER_ID_INVALID) {
1528 filter_idx = efx_mcdi_filter_get_unsafe_id(*id);
1529 if (!table->entry[filter_idx].spec)
1530 netif_dbg(efx, drv, efx->net_dev,
1531 "marked null spec old %04x:%04x\n", *id,
1533 table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_AUTO_OLD;
1534 *id = EFX_EF10_FILTER_ID_INVALID;
1538 /* Mark old per-VLAN filters that may need to be removed */
1539 static void _efx_mcdi_filter_vlan_mark_old(struct efx_nic *efx,
1540 struct efx_mcdi_filter_vlan *vlan)
1542 struct efx_mcdi_filter_table *table = efx->filter_state;
1545 for (i = 0; i < table->dev_uc_count; i++)
1546 efx_mcdi_filter_mark_one_old(efx, &vlan->uc[i]);
1547 for (i = 0; i < table->dev_mc_count; i++)
1548 efx_mcdi_filter_mark_one_old(efx, &vlan->mc[i]);
1549 for (i = 0; i < EFX_EF10_NUM_DEFAULT_FILTERS; i++)
1550 efx_mcdi_filter_mark_one_old(efx, &vlan->default_filters[i]);
1554 * Mark old filters that may need to be removed.
1555 * Caller must hold efx->filter_sem for read if race against
1556 * efx_mcdi_filter_table_remove() is possible
1558 static void efx_mcdi_filter_mark_old(struct efx_nic *efx)
1560 struct efx_mcdi_filter_table *table = efx->filter_state;
1561 struct efx_mcdi_filter_vlan *vlan;
1563 down_write(&table->lock);
1564 list_for_each_entry(vlan, &table->vlan_list, list)
1565 _efx_mcdi_filter_vlan_mark_old(efx, vlan);
1566 up_write(&table->lock);
1569 int efx_mcdi_filter_add_vlan(struct efx_nic *efx, u16 vid)
1571 struct efx_mcdi_filter_table *table = efx->filter_state;
1572 struct efx_mcdi_filter_vlan *vlan;
1575 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1578 vlan = efx_mcdi_filter_find_vlan(efx, vid);
1579 if (WARN_ON(vlan)) {
1580 netif_err(efx, drv, efx->net_dev,
1581 "VLAN %u already added\n", vid);
1585 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1591 for (i = 0; i < ARRAY_SIZE(vlan->uc); i++)
1592 vlan->uc[i] = EFX_EF10_FILTER_ID_INVALID;
1593 for (i = 0; i < ARRAY_SIZE(vlan->mc); i++)
1594 vlan->mc[i] = EFX_EF10_FILTER_ID_INVALID;
1595 for (i = 0; i < EFX_EF10_NUM_DEFAULT_FILTERS; i++)
1596 vlan->default_filters[i] = EFX_EF10_FILTER_ID_INVALID;
1598 list_add_tail(&vlan->list, &table->vlan_list);
1600 if (efx_dev_registered(efx))
1601 efx_mcdi_filter_vlan_sync_rx_mode(efx, vlan);
1606 static void efx_mcdi_filter_del_vlan_internal(struct efx_nic *efx,
1607 struct efx_mcdi_filter_vlan *vlan)
1611 /* See comment in efx_mcdi_filter_table_remove() */
1612 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1615 list_del(&vlan->list);
1617 for (i = 0; i < ARRAY_SIZE(vlan->uc); i++)
1618 efx_mcdi_filter_remove_unsafe(efx, EFX_FILTER_PRI_AUTO,
1620 for (i = 0; i < ARRAY_SIZE(vlan->mc); i++)
1621 efx_mcdi_filter_remove_unsafe(efx, EFX_FILTER_PRI_AUTO,
1623 for (i = 0; i < EFX_EF10_NUM_DEFAULT_FILTERS; i++)
1624 if (vlan->default_filters[i] != EFX_EF10_FILTER_ID_INVALID)
1625 efx_mcdi_filter_remove_unsafe(efx, EFX_FILTER_PRI_AUTO,
1626 vlan->default_filters[i]);
1631 void efx_mcdi_filter_del_vlan(struct efx_nic *efx, u16 vid)
1633 struct efx_mcdi_filter_vlan *vlan;
1635 /* See comment in efx_mcdi_filter_table_remove() */
1636 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1639 vlan = efx_mcdi_filter_find_vlan(efx, vid);
1641 netif_err(efx, drv, efx->net_dev,
1642 "VLAN %u not found in filter state\n", vid);
1646 efx_mcdi_filter_del_vlan_internal(efx, vlan);
1649 struct efx_mcdi_filter_vlan *efx_mcdi_filter_find_vlan(struct efx_nic *efx,
1652 struct efx_mcdi_filter_table *table = efx->filter_state;
1653 struct efx_mcdi_filter_vlan *vlan;
1655 WARN_ON(!rwsem_is_locked(&efx->filter_sem));
1657 list_for_each_entry(vlan, &table->vlan_list, list) {
1658 if (vlan->vid == vid)
1665 void efx_mcdi_filter_cleanup_vlans(struct efx_nic *efx)
1667 struct efx_mcdi_filter_table *table = efx->filter_state;
1668 struct efx_mcdi_filter_vlan *vlan, *next_vlan;
1670 /* See comment in efx_mcdi_filter_table_remove() */
1671 if (!efx_rwsem_assert_write_locked(&efx->filter_sem))
1677 list_for_each_entry_safe(vlan, next_vlan, &table->vlan_list, list)
1678 efx_mcdi_filter_del_vlan_internal(efx, vlan);
1681 static void efx_mcdi_filter_uc_addr_list(struct efx_nic *efx)
1683 struct efx_mcdi_filter_table *table = efx->filter_state;
1684 struct net_device *net_dev = efx->net_dev;
1685 struct netdev_hw_addr *uc;
1688 table->uc_promisc = !!(net_dev->flags & IFF_PROMISC);
1689 ether_addr_copy(table->dev_uc_list[0].addr, net_dev->dev_addr);
1691 netdev_for_each_uc_addr(uc, net_dev) {
1692 if (i >= EFX_EF10_FILTER_DEV_UC_MAX) {
1693 table->uc_promisc = true;
1696 ether_addr_copy(table->dev_uc_list[i].addr, uc->addr);
1700 table->dev_uc_count = i;
1703 static void efx_mcdi_filter_mc_addr_list(struct efx_nic *efx)
1705 struct efx_mcdi_filter_table *table = efx->filter_state;
1706 struct net_device *net_dev = efx->net_dev;
1707 struct netdev_hw_addr *mc;
1710 table->mc_overflow = false;
1711 table->mc_promisc = !!(net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI));
1714 netdev_for_each_mc_addr(mc, net_dev) {
1715 if (i >= EFX_EF10_FILTER_DEV_MC_MAX) {
1716 table->mc_promisc = true;
1717 table->mc_overflow = true;
1720 ether_addr_copy(table->dev_mc_list[i].addr, mc->addr);
1724 table->dev_mc_count = i;
1728 * Caller must hold efx->filter_sem for read if race against
1729 * efx_mcdi_filter_table_remove() is possible
1731 void efx_mcdi_filter_sync_rx_mode(struct efx_nic *efx)
1733 struct efx_mcdi_filter_table *table = efx->filter_state;
1734 struct net_device *net_dev = efx->net_dev;
1735 struct efx_mcdi_filter_vlan *vlan;
1738 if (!efx_dev_registered(efx))
1744 efx_mcdi_filter_mark_old(efx);
1747 * Copy/convert the address lists; add the primary station
1748 * address and broadcast address
1750 netif_addr_lock_bh(net_dev);
1751 efx_mcdi_filter_uc_addr_list(efx);
1752 efx_mcdi_filter_mc_addr_list(efx);
1753 netif_addr_unlock_bh(net_dev);
1756 * If VLAN filtering changes, all old filters are finally removed.
1757 * Do it in advance to avoid conflicts for unicast untagged and
1758 * VLAN 0 tagged filters.
1760 vlan_filter = !!(net_dev->features & NETIF_F_HW_VLAN_CTAG_FILTER);
1761 if (table->vlan_filter != vlan_filter) {
1762 table->vlan_filter = vlan_filter;
1763 efx_mcdi_filter_remove_old(efx);
1766 list_for_each_entry(vlan, &table->vlan_list, list)
1767 efx_mcdi_filter_vlan_sync_rx_mode(efx, vlan);
1769 efx_mcdi_filter_remove_old(efx);
1770 table->mc_promisc_last = table->mc_promisc;
1773 #ifdef CONFIG_RFS_ACCEL
1775 bool efx_mcdi_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
1776 unsigned int filter_idx)
1778 struct efx_filter_spec *spec, saved_spec;
1779 struct efx_mcdi_filter_table *table;
1780 struct efx_arfs_rule *rule = NULL;
1781 bool ret = true, force = false;
1784 down_read(&efx->filter_sem);
1785 table = efx->filter_state;
1786 down_write(&table->lock);
1787 spec = efx_mcdi_filter_entry_spec(table, filter_idx);
1789 if (!spec || spec->priority != EFX_FILTER_PRI_HINT)
1792 spin_lock_bh(&efx->rps_hash_lock);
1793 if (!efx->rps_hash_table) {
1794 /* In the absence of the table, we always return 0 to ARFS. */
1797 rule = efx_rps_hash_find(efx, spec);
1799 /* ARFS table doesn't know of this filter, so remove it */
1801 arfs_id = rule->arfs_id;
1802 ret = efx_rps_check_rule(rule, filter_idx, &force);
1806 spin_unlock_bh(&efx->rps_hash_lock);
1810 if (!rps_may_expire_flow(efx->net_dev, spec->dmaq_id, flow_id, arfs_id))
1813 rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
1815 saved_spec = *spec; /* remove operation will kfree spec */
1816 spin_unlock_bh(&efx->rps_hash_lock);
1818 * At this point (since we dropped the lock), another thread might queue
1819 * up a fresh insertion request (but the actual insertion will be held
1820 * up by our possession of the filter table lock). In that case, it
1821 * will set rule->filter_id to EFX_ARFS_FILTER_ID_PENDING, meaning that
1822 * the rule is not removed by efx_rps_hash_del() below.
1825 ret = efx_mcdi_filter_remove_internal(efx, 1U << spec->priority,
1826 filter_idx, true) == 0;
1828 * While we can't safely dereference rule (we dropped the lock), we can
1829 * still test it for NULL.
1832 /* Expiring, so remove entry from ARFS table */
1833 spin_lock_bh(&efx->rps_hash_lock);
1834 efx_rps_hash_del(efx, &saved_spec);
1835 spin_unlock_bh(&efx->rps_hash_lock);
1838 up_write(&table->lock);
1839 up_read(&efx->filter_sem);
1843 #endif /* CONFIG_RFS_ACCEL */
1845 #define RSS_MODE_HASH_ADDRS (1 << RSS_MODE_HASH_SRC_ADDR_LBN |\
1846 1 << RSS_MODE_HASH_DST_ADDR_LBN)
1847 #define RSS_MODE_HASH_PORTS (1 << RSS_MODE_HASH_SRC_PORT_LBN |\
1848 1 << RSS_MODE_HASH_DST_PORT_LBN)
1849 #define RSS_CONTEXT_FLAGS_DEFAULT (1 << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TOEPLITZ_IPV4_EN_LBN |\
1850 1 << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TOEPLITZ_TCPV4_EN_LBN |\
1851 1 << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TOEPLITZ_IPV6_EN_LBN |\
1852 1 << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TOEPLITZ_TCPV6_EN_LBN |\
1853 (RSS_MODE_HASH_ADDRS | RSS_MODE_HASH_PORTS) << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TCP_IPV4_RSS_MODE_LBN |\
1854 RSS_MODE_HASH_ADDRS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_UDP_IPV4_RSS_MODE_LBN |\
1855 RSS_MODE_HASH_ADDRS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_OTHER_IPV4_RSS_MODE_LBN |\
1856 (RSS_MODE_HASH_ADDRS | RSS_MODE_HASH_PORTS) << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_TCP_IPV6_RSS_MODE_LBN |\
1857 RSS_MODE_HASH_ADDRS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_UDP_IPV6_RSS_MODE_LBN |\
1858 RSS_MODE_HASH_ADDRS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_OTHER_IPV6_RSS_MODE_LBN)
1860 int efx_mcdi_get_rss_context_flags(struct efx_nic *efx, u32 context, u32 *flags)
1863 * Firmware had a bug (sfc bug 61952) where it would not actually
1864 * fill in the flags field in the response to MC_CMD_RSS_CONTEXT_GET_FLAGS.
1865 * This meant that it would always contain whatever was previously
1866 * in the MCDI buffer. Fortunately, all firmware versions with
1867 * this bug have the same default flags value for a newly-allocated
1868 * RSS context, and the only time we want to get the flags is just
1869 * after allocating. Moreover, the response has a 32-bit hole
1870 * where the context ID would be in the request, so we can use an
1871 * overlength buffer in the request and pre-fill the flags field
1872 * with what we believe the default to be. Thus if the firmware
1873 * has the bug, it will leave our pre-filled value in the flags
1874 * field of the response, and we will get the right answer.
1876 * However, this does mean that this function should NOT be used if
1877 * the RSS context flags might not be their defaults - it is ONLY
1878 * reliably correct for a newly-allocated RSS context.
1880 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_LEN);
1881 MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_LEN);
1885 /* Check we have a hole for the context ID */
1886 BUILD_BUG_ON(MC_CMD_RSS_CONTEXT_GET_FLAGS_IN_LEN != MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_FLAGS_OFST);
1887 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_GET_FLAGS_IN_RSS_CONTEXT_ID, context);
1888 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_GET_FLAGS_OUT_FLAGS,
1889 RSS_CONTEXT_FLAGS_DEFAULT);
1890 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_GET_FLAGS, inbuf,
1891 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
1893 if (outlen < MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_LEN)
1896 *flags = MCDI_DWORD(outbuf, RSS_CONTEXT_GET_FLAGS_OUT_FLAGS);
1902 * Attempt to enable 4-tuple UDP hashing on the specified RSS context.
1903 * If we fail, we just leave the RSS context at its default hash settings,
1904 * which is safe but may slightly reduce performance.
1905 * Defaults are 4-tuple for TCP and 2-tuple for UDP and other-IP, so we
1906 * just need to set the UDP ports flags (for both IP versions).
1908 void efx_mcdi_set_rss_context_flags(struct efx_nic *efx,
1909 struct efx_rss_context *ctx)
1911 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_SET_FLAGS_IN_LEN);
1914 BUILD_BUG_ON(MC_CMD_RSS_CONTEXT_SET_FLAGS_OUT_LEN != 0);
1916 if (efx_mcdi_get_rss_context_flags(efx, ctx->context_id, &flags) != 0)
1918 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_SET_FLAGS_IN_RSS_CONTEXT_ID,
1920 flags |= RSS_MODE_HASH_PORTS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_UDP_IPV4_RSS_MODE_LBN;
1921 flags |= RSS_MODE_HASH_PORTS << MC_CMD_RSS_CONTEXT_GET_FLAGS_OUT_UDP_IPV6_RSS_MODE_LBN;
1922 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_SET_FLAGS_IN_FLAGS, flags);
1923 if (!efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_FLAGS, inbuf, sizeof(inbuf),
1925 /* Succeeded, so UDP 4-tuple is now enabled */
1926 ctx->rx_hash_udp_4tuple = true;
1929 static int efx_mcdi_filter_alloc_rss_context(struct efx_nic *efx, bool exclusive,
1930 struct efx_rss_context *ctx,
1931 unsigned *context_size)
1933 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
1934 MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
1937 u32 alloc_type = exclusive ?
1938 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE :
1939 MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_SHARED;
1940 unsigned rss_spread = exclusive ?
1942 min(rounddown_pow_of_two(efx->rss_spread),
1943 EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE);
1945 if (!exclusive && rss_spread == 1) {
1946 ctx->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
1952 if (efx_has_cap(efx, RX_RSS_LIMITED))
1955 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
1957 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE, alloc_type);
1958 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES, rss_spread);
1960 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
1961 outbuf, sizeof(outbuf), &outlen);
1965 if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
1968 ctx->context_id = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
1971 *context_size = rss_spread;
1973 if (efx_has_cap(efx, ADDITIONAL_RSS_MODES))
1974 efx_mcdi_set_rss_context_flags(efx, ctx);
1979 static int efx_mcdi_filter_free_rss_context(struct efx_nic *efx, u32 context)
1981 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
1983 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
1985 return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
1989 static int efx_mcdi_filter_populate_rss_table(struct efx_nic *efx, u32 context,
1990 const u32 *rx_indir_table, const u8 *key)
1992 MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
1993 MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
1996 MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
1998 BUILD_BUG_ON(ARRAY_SIZE(efx->rss_context.rx_indir_table) !=
1999 MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
2001 /* This iterates over the length of efx->rss_context.rx_indir_table, but
2002 * copies bytes from rx_indir_table. That's because the latter is a
2003 * pointer rather than an array, but should have the same length.
2004 * The efx->rss_context.rx_hash_key loop below is similar.
2006 for (i = 0; i < ARRAY_SIZE(efx->rss_context.rx_indir_table); ++i)
2008 RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
2009 (u8) rx_indir_table[i];
2011 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
2012 sizeof(tablebuf), NULL, 0, NULL);
2016 MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
2018 BUILD_BUG_ON(ARRAY_SIZE(efx->rss_context.rx_hash_key) !=
2019 MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
2020 for (i = 0; i < ARRAY_SIZE(efx->rss_context.rx_hash_key); ++i)
2021 MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] = key[i];
2023 return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
2024 sizeof(keybuf), NULL, 0, NULL);
2027 void efx_mcdi_rx_free_indir_table(struct efx_nic *efx)
2031 if (efx->rss_context.context_id != EFX_MCDI_RSS_CONTEXT_INVALID) {
2032 rc = efx_mcdi_filter_free_rss_context(efx, efx->rss_context.context_id);
2035 efx->rss_context.context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
2038 static int efx_mcdi_filter_rx_push_shared_rss_config(struct efx_nic *efx,
2039 unsigned *context_size)
2041 struct efx_mcdi_filter_table *table = efx->filter_state;
2042 int rc = efx_mcdi_filter_alloc_rss_context(efx, false, &efx->rss_context,
2048 table->rx_rss_context_exclusive = false;
2049 efx_set_default_rx_indir_table(efx, &efx->rss_context);
2053 static int efx_mcdi_filter_rx_push_exclusive_rss_config(struct efx_nic *efx,
2054 const u32 *rx_indir_table,
2057 struct efx_mcdi_filter_table *table = efx->filter_state;
2058 u32 old_rx_rss_context = efx->rss_context.context_id;
2061 if (efx->rss_context.context_id == EFX_MCDI_RSS_CONTEXT_INVALID ||
2062 !table->rx_rss_context_exclusive) {
2063 rc = efx_mcdi_filter_alloc_rss_context(efx, true, &efx->rss_context,
2065 if (rc == -EOPNOTSUPP)
2071 rc = efx_mcdi_filter_populate_rss_table(efx, efx->rss_context.context_id,
2072 rx_indir_table, key);
2076 if (efx->rss_context.context_id != old_rx_rss_context &&
2077 old_rx_rss_context != EFX_MCDI_RSS_CONTEXT_INVALID)
2078 WARN_ON(efx_mcdi_filter_free_rss_context(efx, old_rx_rss_context) != 0);
2079 table->rx_rss_context_exclusive = true;
2080 if (rx_indir_table != efx->rss_context.rx_indir_table)
2081 memcpy(efx->rss_context.rx_indir_table, rx_indir_table,
2082 sizeof(efx->rss_context.rx_indir_table));
2083 if (key != efx->rss_context.rx_hash_key)
2084 memcpy(efx->rss_context.rx_hash_key, key,
2085 efx->type->rx_hash_key_size);
2090 if (old_rx_rss_context != efx->rss_context.context_id) {
2091 WARN_ON(efx_mcdi_filter_free_rss_context(efx, efx->rss_context.context_id) != 0);
2092 efx->rss_context.context_id = old_rx_rss_context;
2095 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
2099 int efx_mcdi_rx_push_rss_context_config(struct efx_nic *efx,
2100 struct efx_rss_context *ctx,
2101 const u32 *rx_indir_table,
2106 WARN_ON(!mutex_is_locked(&efx->rss_lock));
2108 if (ctx->context_id == EFX_MCDI_RSS_CONTEXT_INVALID) {
2109 rc = efx_mcdi_filter_alloc_rss_context(efx, true, ctx, NULL);
2114 if (!rx_indir_table) /* Delete this context */
2115 return efx_mcdi_filter_free_rss_context(efx, ctx->context_id);
2117 rc = efx_mcdi_filter_populate_rss_table(efx, ctx->context_id,
2118 rx_indir_table, key);
2122 memcpy(ctx->rx_indir_table, rx_indir_table,
2123 sizeof(efx->rss_context.rx_indir_table));
2124 memcpy(ctx->rx_hash_key, key, efx->type->rx_hash_key_size);
2129 int efx_mcdi_rx_pull_rss_context_config(struct efx_nic *efx,
2130 struct efx_rss_context *ctx)
2132 MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_GET_TABLE_IN_LEN);
2133 MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_GET_TABLE_OUT_LEN);
2134 MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_GET_KEY_OUT_LEN);
2138 WARN_ON(!mutex_is_locked(&efx->rss_lock));
2140 BUILD_BUG_ON(MC_CMD_RSS_CONTEXT_GET_TABLE_IN_LEN !=
2141 MC_CMD_RSS_CONTEXT_GET_KEY_IN_LEN);
2143 if (ctx->context_id == EFX_MCDI_RSS_CONTEXT_INVALID)
2146 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_GET_TABLE_IN_RSS_CONTEXT_ID,
2148 BUILD_BUG_ON(ARRAY_SIZE(ctx->rx_indir_table) !=
2149 MC_CMD_RSS_CONTEXT_GET_TABLE_OUT_INDIRECTION_TABLE_LEN);
2150 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_GET_TABLE, inbuf, sizeof(inbuf),
2151 tablebuf, sizeof(tablebuf), &outlen);
2155 if (WARN_ON(outlen != MC_CMD_RSS_CONTEXT_GET_TABLE_OUT_LEN))
2158 for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
2159 ctx->rx_indir_table[i] = MCDI_PTR(tablebuf,
2160 RSS_CONTEXT_GET_TABLE_OUT_INDIRECTION_TABLE)[i];
2162 MCDI_SET_DWORD(inbuf, RSS_CONTEXT_GET_KEY_IN_RSS_CONTEXT_ID,
2164 BUILD_BUG_ON(ARRAY_SIZE(ctx->rx_hash_key) !=
2165 MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
2166 rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_GET_KEY, inbuf, sizeof(inbuf),
2167 keybuf, sizeof(keybuf), &outlen);
2171 if (WARN_ON(outlen != MC_CMD_RSS_CONTEXT_GET_KEY_OUT_LEN))
2174 for (i = 0; i < ARRAY_SIZE(ctx->rx_hash_key); ++i)
2175 ctx->rx_hash_key[i] = MCDI_PTR(
2176 keybuf, RSS_CONTEXT_GET_KEY_OUT_TOEPLITZ_KEY)[i];
2181 int efx_mcdi_rx_pull_rss_config(struct efx_nic *efx)
2185 mutex_lock(&efx->rss_lock);
2186 rc = efx_mcdi_rx_pull_rss_context_config(efx, &efx->rss_context);
2187 mutex_unlock(&efx->rss_lock);
2191 void efx_mcdi_rx_restore_rss_contexts(struct efx_nic *efx)
2193 struct efx_mcdi_filter_table *table = efx->filter_state;
2194 struct efx_rss_context *ctx;
2197 WARN_ON(!mutex_is_locked(&efx->rss_lock));
2199 if (!table->must_restore_rss_contexts)
2202 list_for_each_entry(ctx, &efx->rss_context.list, list) {
2203 /* previous NIC RSS context is gone */
2204 ctx->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
2205 /* so try to allocate a new one */
2206 rc = efx_mcdi_rx_push_rss_context_config(efx, ctx,
2207 ctx->rx_indir_table,
2210 netif_warn(efx, probe, efx->net_dev,
2211 "failed to restore RSS context %u, rc=%d"
2212 "; RSS filters may fail to be applied\n",
2215 table->must_restore_rss_contexts = false;
2218 int efx_mcdi_pf_rx_push_rss_config(struct efx_nic *efx, bool user,
2219 const u32 *rx_indir_table,
2224 if (efx->rss_spread == 1)
2228 key = efx->rss_context.rx_hash_key;
2230 rc = efx_mcdi_filter_rx_push_exclusive_rss_config(efx, rx_indir_table, key);
2232 if (rc == -ENOBUFS && !user) {
2233 unsigned context_size;
2234 bool mismatch = false;
2238 i < ARRAY_SIZE(efx->rss_context.rx_indir_table) && !mismatch;
2240 mismatch = rx_indir_table[i] !=
2241 ethtool_rxfh_indir_default(i, efx->rss_spread);
2243 rc = efx_mcdi_filter_rx_push_shared_rss_config(efx, &context_size);
2245 if (context_size != efx->rss_spread)
2246 netif_warn(efx, probe, efx->net_dev,
2247 "Could not allocate an exclusive RSS"
2248 " context; allocated a shared one of"
2250 " Wanted %u, got %u.\n",
2251 efx->rss_spread, context_size);
2253 netif_warn(efx, probe, efx->net_dev,
2254 "Could not allocate an exclusive RSS"
2255 " context; allocated a shared one but"
2256 " could not apply custom"
2259 netif_info(efx, probe, efx->net_dev,
2260 "Could not allocate an exclusive RSS"
2261 " context; allocated a shared one.\n");
2267 int efx_mcdi_vf_rx_push_rss_config(struct efx_nic *efx, bool user,
2268 const u32 *rx_indir_table
2269 __attribute__ ((unused)),
2271 __attribute__ ((unused)))
2275 if (efx->rss_context.context_id != EFX_MCDI_RSS_CONTEXT_INVALID)
2277 return efx_mcdi_filter_rx_push_shared_rss_config(efx, NULL);
2280 int efx_mcdi_push_default_indir_table(struct efx_nic *efx,
2281 unsigned int rss_spread)
2285 if (efx->rss_spread == rss_spread)
2288 efx->rss_spread = rss_spread;
2289 if (!efx->filter_state)
2292 efx_mcdi_rx_free_indir_table(efx);
2293 if (rss_spread > 1) {
2294 efx_set_default_rx_indir_table(efx, &efx->rss_context);
2295 rc = efx->type->rx_push_rss_config(efx, false,
2296 efx->rss_context.rx_indir_table, NULL);