xref: /openbmc/linux/drivers/net/ethernet/sfc/tc.c (revision febf2aaf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2019 Solarflare Communications Inc.
5  * Copyright 2020-2022 Xilinx Inc.
6  *
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.
10  */
11 
12 #include <net/pkt_cls.h>
13 #include <net/vxlan.h>
14 #include <net/geneve.h>
15 #include "tc.h"
16 #include "tc_bindings.h"
17 #include "tc_encap_actions.h"
18 #include "mae.h"
19 #include "ef100_rep.h"
20 #include "efx.h"
21 
22 enum efx_encap_type efx_tc_indr_netdev_type(struct net_device *net_dev)
23 {
24 	if (netif_is_vxlan(net_dev))
25 		return EFX_ENCAP_TYPE_VXLAN;
26 	if (netif_is_geneve(net_dev))
27 		return EFX_ENCAP_TYPE_GENEVE;
28 
29 	return EFX_ENCAP_TYPE_NONE;
30 }
31 
32 #define EFX_EFV_PF	NULL
33 /* Look up the representor information (efv) for a device.
34  * May return NULL for the PF (us), or an error pointer for a device that
35  * isn't supported as a TC offload endpoint
36  */
37 struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx,
38 					 struct net_device *dev)
39 {
40 	struct efx_rep *efv;
41 
42 	if (!dev)
43 		return ERR_PTR(-EOPNOTSUPP);
44 	/* Is it us (the PF)? */
45 	if (dev == efx->net_dev)
46 		return EFX_EFV_PF;
47 	/* Is it an efx vfrep at all? */
48 	if (dev->netdev_ops != &efx_ef100_rep_netdev_ops)
49 		return ERR_PTR(-EOPNOTSUPP);
50 	/* Is it ours?  We don't support TC rules that include another
51 	 * EF100's netdevices (not even on another port of the same NIC).
52 	 */
53 	efv = netdev_priv(dev);
54 	if (efv->parent != efx)
55 		return ERR_PTR(-EOPNOTSUPP);
56 	return efv;
57 }
58 
59 /* Convert a driver-internal vport ID into an internal device (PF or VF) */
60 static s64 efx_tc_flower_internal_mport(struct efx_nic *efx, struct efx_rep *efv)
61 {
62 	u32 mport;
63 
64 	if (IS_ERR(efv))
65 		return PTR_ERR(efv);
66 	if (!efv) /* device is PF (us) */
67 		efx_mae_mport_uplink(efx, &mport);
68 	else /* device is repr */
69 		efx_mae_mport_mport(efx, efv->mport, &mport);
70 	return mport;
71 }
72 
73 /* Convert a driver-internal vport ID into an external device (wire or VF) */
74 s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv)
75 {
76 	u32 mport;
77 
78 	if (IS_ERR(efv))
79 		return PTR_ERR(efv);
80 	if (!efv) /* device is PF (us) */
81 		efx_mae_mport_wire(efx, &mport);
82 	else /* device is repr */
83 		efx_mae_mport_mport(efx, efv->mport, &mport);
84 	return mport;
85 }
86 
87 static const struct rhashtable_params efx_tc_encap_match_ht_params = {
88 	.key_len	= offsetof(struct efx_tc_encap_match, linkage),
89 	.key_offset	= 0,
90 	.head_offset	= offsetof(struct efx_tc_encap_match, linkage),
91 };
92 
93 static const struct rhashtable_params efx_tc_match_action_ht_params = {
94 	.key_len	= sizeof(unsigned long),
95 	.key_offset	= offsetof(struct efx_tc_flow_rule, cookie),
96 	.head_offset	= offsetof(struct efx_tc_flow_rule, linkage),
97 };
98 
99 static void efx_tc_free_action_set(struct efx_nic *efx,
100 				   struct efx_tc_action_set *act, bool in_hw)
101 {
102 	/* Failure paths calling this on the 'cursor' action set in_hw=false,
103 	 * because if the alloc had succeeded we'd've put it in acts.list and
104 	 * not still have it in act.
105 	 */
106 	if (in_hw) {
107 		efx_mae_free_action_set(efx, act->fw_id);
108 		/* in_hw is true iff we are on an acts.list; make sure to
109 		 * remove ourselves from that list before we are freed.
110 		 */
111 		list_del(&act->list);
112 	}
113 	if (act->count)
114 		efx_tc_flower_put_counter_index(efx, act->count);
115 	if (act->encap_md) {
116 		list_del(&act->encap_user);
117 		efx_tc_flower_release_encap_md(efx, act->encap_md);
118 	}
119 	kfree(act);
120 }
121 
122 static void efx_tc_free_action_set_list(struct efx_nic *efx,
123 					struct efx_tc_action_set_list *acts,
124 					bool in_hw)
125 {
126 	struct efx_tc_action_set *act, *next;
127 
128 	/* Failure paths set in_hw=false, because usually the acts didn't get
129 	 * to efx_mae_alloc_action_set_list(); if they did, the failure tree
130 	 * has a separate efx_mae_free_action_set_list() before calling us.
131 	 */
132 	if (in_hw)
133 		efx_mae_free_action_set_list(efx, acts);
134 	/* Any act that's on the list will be in_hw even if the list isn't */
135 	list_for_each_entry_safe(act, next, &acts->list, list)
136 		efx_tc_free_action_set(efx, act, true);
137 	/* Don't kfree, as acts is embedded inside a struct efx_tc_flow_rule */
138 }
139 
140 /* Boilerplate for the simple 'copy a field' cases */
141 #define _MAP_KEY_AND_MASK(_name, _type, _tcget, _tcfield, _field)	\
142 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_##_name)) {		\
143 	struct flow_match_##_type fm;					\
144 									\
145 	flow_rule_match_##_tcget(rule, &fm);				\
146 	match->value._field = fm.key->_tcfield;				\
147 	match->mask._field = fm.mask->_tcfield;				\
148 }
149 #define MAP_KEY_AND_MASK(_name, _type, _tcfield, _field)	\
150 	_MAP_KEY_AND_MASK(_name, _type, _type, _tcfield, _field)
151 #define MAP_ENC_KEY_AND_MASK(_name, _type, _tcget, _tcfield, _field)	\
152 	_MAP_KEY_AND_MASK(ENC_##_name, _type, _tcget, _tcfield, _field)
153 
154 static int efx_tc_flower_parse_match(struct efx_nic *efx,
155 				     struct flow_rule *rule,
156 				     struct efx_tc_match *match,
157 				     struct netlink_ext_ack *extack)
158 {
159 	struct flow_dissector *dissector = rule->match.dissector;
160 	unsigned char ipv = 0;
161 
162 	/* Owing to internal TC infelicities, the IPV6_ADDRS key might be set
163 	 * even on IPv4 filters; so rather than relying on dissector->used_keys
164 	 * we check the addr_type in the CONTROL key.  If we don't find it (or
165 	 * it's masked, which should never happen), we treat both IPV4_ADDRS
166 	 * and IPV6_ADDRS as absent.
167 	 */
168 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
169 		struct flow_match_control fm;
170 
171 		flow_rule_match_control(rule, &fm);
172 		if (IS_ALL_ONES(fm.mask->addr_type))
173 			switch (fm.key->addr_type) {
174 			case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
175 				ipv = 4;
176 				break;
177 			case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
178 				ipv = 6;
179 				break;
180 			default:
181 				break;
182 			}
183 
184 		if (fm.mask->flags & FLOW_DIS_IS_FRAGMENT) {
185 			match->value.ip_frag = fm.key->flags & FLOW_DIS_IS_FRAGMENT;
186 			match->mask.ip_frag = true;
187 		}
188 		if (fm.mask->flags & FLOW_DIS_FIRST_FRAG) {
189 			match->value.ip_firstfrag = fm.key->flags & FLOW_DIS_FIRST_FRAG;
190 			match->mask.ip_firstfrag = true;
191 		}
192 		if (fm.mask->flags & ~(FLOW_DIS_IS_FRAGMENT | FLOW_DIS_FIRST_FRAG)) {
193 			NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported match on control.flags %#x",
194 					       fm.mask->flags);
195 			return -EOPNOTSUPP;
196 		}
197 	}
198 	if (dissector->used_keys &
199 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
200 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
201 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
202 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
203 	      BIT(FLOW_DISSECTOR_KEY_CVLAN) |
204 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
205 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
206 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
207 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
208 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
209 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
210 	      BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
211 	      BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
212 	      BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
213 	      BIT(FLOW_DISSECTOR_KEY_TCP) |
214 	      BIT(FLOW_DISSECTOR_KEY_IP))) {
215 		NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported flower keys %#x",
216 				       dissector->used_keys);
217 		return -EOPNOTSUPP;
218 	}
219 
220 	MAP_KEY_AND_MASK(BASIC, basic, n_proto, eth_proto);
221 	/* Make sure we're IP if any L3/L4 keys used. */
222 	if (!IS_ALL_ONES(match->mask.eth_proto) ||
223 	    !(match->value.eth_proto == htons(ETH_P_IP) ||
224 	      match->value.eth_proto == htons(ETH_P_IPV6)))
225 		if (dissector->used_keys &
226 		    (BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
227 		     BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
228 		     BIT(FLOW_DISSECTOR_KEY_PORTS) |
229 		     BIT(FLOW_DISSECTOR_KEY_IP) |
230 		     BIT(FLOW_DISSECTOR_KEY_TCP))) {
231 			NL_SET_ERR_MSG_FMT_MOD(extack, "L3/L4 flower keys %#x require protocol ipv[46]",
232 					       dissector->used_keys);
233 			return -EINVAL;
234 		}
235 
236 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
237 		struct flow_match_vlan fm;
238 
239 		flow_rule_match_vlan(rule, &fm);
240 		if (fm.mask->vlan_id || fm.mask->vlan_priority || fm.mask->vlan_tpid) {
241 			match->value.vlan_proto[0] = fm.key->vlan_tpid;
242 			match->mask.vlan_proto[0] = fm.mask->vlan_tpid;
243 			match->value.vlan_tci[0] = cpu_to_be16(fm.key->vlan_priority << 13 |
244 							       fm.key->vlan_id);
245 			match->mask.vlan_tci[0] = cpu_to_be16(fm.mask->vlan_priority << 13 |
246 							      fm.mask->vlan_id);
247 		}
248 	}
249 
250 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
251 		struct flow_match_vlan fm;
252 
253 		flow_rule_match_cvlan(rule, &fm);
254 		if (fm.mask->vlan_id || fm.mask->vlan_priority || fm.mask->vlan_tpid) {
255 			match->value.vlan_proto[1] = fm.key->vlan_tpid;
256 			match->mask.vlan_proto[1] = fm.mask->vlan_tpid;
257 			match->value.vlan_tci[1] = cpu_to_be16(fm.key->vlan_priority << 13 |
258 							       fm.key->vlan_id);
259 			match->mask.vlan_tci[1] = cpu_to_be16(fm.mask->vlan_priority << 13 |
260 							      fm.mask->vlan_id);
261 		}
262 	}
263 
264 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
265 		struct flow_match_eth_addrs fm;
266 
267 		flow_rule_match_eth_addrs(rule, &fm);
268 		ether_addr_copy(match->value.eth_saddr, fm.key->src);
269 		ether_addr_copy(match->value.eth_daddr, fm.key->dst);
270 		ether_addr_copy(match->mask.eth_saddr, fm.mask->src);
271 		ether_addr_copy(match->mask.eth_daddr, fm.mask->dst);
272 	}
273 
274 	MAP_KEY_AND_MASK(BASIC, basic, ip_proto, ip_proto);
275 	/* Make sure we're TCP/UDP if any L4 keys used. */
276 	if ((match->value.ip_proto != IPPROTO_UDP &&
277 	     match->value.ip_proto != IPPROTO_TCP) || !IS_ALL_ONES(match->mask.ip_proto))
278 		if (dissector->used_keys &
279 		    (BIT(FLOW_DISSECTOR_KEY_PORTS) |
280 		     BIT(FLOW_DISSECTOR_KEY_TCP))) {
281 			NL_SET_ERR_MSG_FMT_MOD(extack, "L4 flower keys %#x require ipproto udp or tcp",
282 					       dissector->used_keys);
283 			return -EINVAL;
284 		}
285 	MAP_KEY_AND_MASK(IP, ip, tos, ip_tos);
286 	MAP_KEY_AND_MASK(IP, ip, ttl, ip_ttl);
287 	if (ipv == 4) {
288 		MAP_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, src, src_ip);
289 		MAP_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, dst, dst_ip);
290 	}
291 #ifdef CONFIG_IPV6
292 	else if (ipv == 6) {
293 		MAP_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, src, src_ip6);
294 		MAP_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, dst, dst_ip6);
295 	}
296 #endif
297 	MAP_KEY_AND_MASK(PORTS, ports, src, l4_sport);
298 	MAP_KEY_AND_MASK(PORTS, ports, dst, l4_dport);
299 	MAP_KEY_AND_MASK(TCP, tcp, flags, tcp_flags);
300 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
301 		struct flow_match_control fm;
302 
303 		flow_rule_match_enc_control(rule, &fm);
304 		if (fm.mask->flags) {
305 			NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported match on enc_control.flags %#x",
306 					       fm.mask->flags);
307 			return -EOPNOTSUPP;
308 		}
309 		if (!IS_ALL_ONES(fm.mask->addr_type)) {
310 			NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported enc addr_type mask %u (key %u)",
311 					       fm.mask->addr_type,
312 					       fm.key->addr_type);
313 			return -EOPNOTSUPP;
314 		}
315 		switch (fm.key->addr_type) {
316 		case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
317 			MAP_ENC_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, enc_ipv4_addrs,
318 					     src, enc_src_ip);
319 			MAP_ENC_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, enc_ipv4_addrs,
320 					     dst, enc_dst_ip);
321 			break;
322 #ifdef CONFIG_IPV6
323 		case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
324 			MAP_ENC_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, enc_ipv6_addrs,
325 					     src, enc_src_ip6);
326 			MAP_ENC_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, enc_ipv6_addrs,
327 					     dst, enc_dst_ip6);
328 			break;
329 #endif
330 		default:
331 			NL_SET_ERR_MSG_FMT_MOD(extack,
332 					       "Unsupported enc addr_type %u (supported are IPv4, IPv6)",
333 					       fm.key->addr_type);
334 			return -EOPNOTSUPP;
335 		}
336 		MAP_ENC_KEY_AND_MASK(IP, ip, enc_ip, tos, enc_ip_tos);
337 		MAP_ENC_KEY_AND_MASK(IP, ip, enc_ip, ttl, enc_ip_ttl);
338 		MAP_ENC_KEY_AND_MASK(PORTS, ports, enc_ports, src, enc_sport);
339 		MAP_ENC_KEY_AND_MASK(PORTS, ports, enc_ports, dst, enc_dport);
340 		MAP_ENC_KEY_AND_MASK(KEYID, enc_keyid, enc_keyid, keyid, enc_keyid);
341 	} else if (dissector->used_keys &
342 		   (BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
343 		    BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
344 		    BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
345 		    BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
346 		    BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
347 		NL_SET_ERR_MSG_FMT_MOD(extack, "Flower enc keys require enc_control (keys: %#x)",
348 				       dissector->used_keys);
349 		return -EOPNOTSUPP;
350 	}
351 
352 	return 0;
353 }
354 
355 static void efx_tc_flower_release_encap_match(struct efx_nic *efx,
356 					      struct efx_tc_encap_match *encap)
357 {
358 	int rc;
359 
360 	if (!refcount_dec_and_test(&encap->ref))
361 		return; /* still in use */
362 
363 	if (encap->type == EFX_TC_EM_DIRECT) {
364 		rc = efx_mae_unregister_encap_match(efx, encap);
365 		if (rc)
366 			/* Display message but carry on and remove entry from our
367 			 * SW tables, because there's not much we can do about it.
368 			 */
369 			netif_err(efx, drv, efx->net_dev,
370 				  "Failed to release encap match %#x, rc %d\n",
371 				  encap->fw_id, rc);
372 	}
373 	rhashtable_remove_fast(&efx->tc->encap_match_ht, &encap->linkage,
374 			       efx_tc_encap_match_ht_params);
375 	if (encap->pseudo)
376 		efx_tc_flower_release_encap_match(efx, encap->pseudo);
377 	kfree(encap);
378 }
379 
380 static int efx_tc_flower_record_encap_match(struct efx_nic *efx,
381 					    struct efx_tc_match *match,
382 					    enum efx_encap_type type,
383 					    enum efx_tc_em_pseudo_type em_type,
384 					    u8 child_ip_tos_mask,
385 					    __be16 child_udp_sport_mask,
386 					    struct netlink_ext_ack *extack)
387 {
388 	struct efx_tc_encap_match *encap, *old, *pseudo = NULL;
389 	bool ipv6 = false;
390 	int rc;
391 
392 	/* We require that the socket-defining fields (IP addrs and UDP dest
393 	 * port) are present and exact-match.  Other fields may only be used
394 	 * if the field-set (and any masks) are the same for all encap
395 	 * matches on the same <sip,dip,dport> tuple; this is enforced by
396 	 * pseudo encap matches.
397 	 */
398 	if (match->mask.enc_dst_ip | match->mask.enc_src_ip) {
399 		if (!IS_ALL_ONES(match->mask.enc_dst_ip)) {
400 			NL_SET_ERR_MSG_MOD(extack,
401 					   "Egress encap match is not exact on dst IP address");
402 			return -EOPNOTSUPP;
403 		}
404 		if (!IS_ALL_ONES(match->mask.enc_src_ip)) {
405 			NL_SET_ERR_MSG_MOD(extack,
406 					   "Egress encap match is not exact on src IP address");
407 			return -EOPNOTSUPP;
408 		}
409 #ifdef CONFIG_IPV6
410 		if (!ipv6_addr_any(&match->mask.enc_dst_ip6) ||
411 		    !ipv6_addr_any(&match->mask.enc_src_ip6)) {
412 			NL_SET_ERR_MSG_MOD(extack,
413 					   "Egress encap match on both IPv4 and IPv6, don't understand");
414 			return -EOPNOTSUPP;
415 		}
416 	} else {
417 		ipv6 = true;
418 		if (!efx_ipv6_addr_all_ones(&match->mask.enc_dst_ip6)) {
419 			NL_SET_ERR_MSG_MOD(extack,
420 					   "Egress encap match is not exact on dst IP address");
421 			return -EOPNOTSUPP;
422 		}
423 		if (!efx_ipv6_addr_all_ones(&match->mask.enc_src_ip6)) {
424 			NL_SET_ERR_MSG_MOD(extack,
425 					   "Egress encap match is not exact on src IP address");
426 			return -EOPNOTSUPP;
427 		}
428 #endif
429 	}
430 	if (!IS_ALL_ONES(match->mask.enc_dport)) {
431 		NL_SET_ERR_MSG_MOD(extack, "Egress encap match is not exact on dst UDP port");
432 		return -EOPNOTSUPP;
433 	}
434 	if (match->mask.enc_sport || match->mask.enc_ip_tos) {
435 		struct efx_tc_match pmatch = *match;
436 
437 		if (em_type == EFX_TC_EM_PSEUDO_MASK) { /* can't happen */
438 			NL_SET_ERR_MSG_MOD(extack, "Bad recursion in egress encap match handler");
439 			return -EOPNOTSUPP;
440 		}
441 		pmatch.value.enc_ip_tos = 0;
442 		pmatch.mask.enc_ip_tos = 0;
443 		pmatch.value.enc_sport = 0;
444 		pmatch.mask.enc_sport = 0;
445 		rc = efx_tc_flower_record_encap_match(efx, &pmatch, type,
446 						      EFX_TC_EM_PSEUDO_MASK,
447 						      match->mask.enc_ip_tos,
448 						      match->mask.enc_sport,
449 						      extack);
450 		if (rc)
451 			return rc;
452 		pseudo = pmatch.encap;
453 	}
454 	if (match->mask.enc_ip_ttl) {
455 		NL_SET_ERR_MSG_MOD(extack, "Egress encap match on IP TTL not supported");
456 		rc = -EOPNOTSUPP;
457 		goto fail_pseudo;
458 	}
459 
460 	rc = efx_mae_check_encap_match_caps(efx, ipv6, match->mask.enc_ip_tos,
461 					    match->mask.enc_sport, extack);
462 	if (rc)
463 		goto fail_pseudo;
464 
465 	encap = kzalloc(sizeof(*encap), GFP_USER);
466 	if (!encap) {
467 		rc = -ENOMEM;
468 		goto fail_pseudo;
469 	}
470 	encap->src_ip = match->value.enc_src_ip;
471 	encap->dst_ip = match->value.enc_dst_ip;
472 #ifdef CONFIG_IPV6
473 	encap->src_ip6 = match->value.enc_src_ip6;
474 	encap->dst_ip6 = match->value.enc_dst_ip6;
475 #endif
476 	encap->udp_dport = match->value.enc_dport;
477 	encap->tun_type = type;
478 	encap->ip_tos = match->value.enc_ip_tos;
479 	encap->ip_tos_mask = match->mask.enc_ip_tos;
480 	encap->child_ip_tos_mask = child_ip_tos_mask;
481 	encap->udp_sport = match->value.enc_sport;
482 	encap->udp_sport_mask = match->mask.enc_sport;
483 	encap->child_udp_sport_mask = child_udp_sport_mask;
484 	encap->type = em_type;
485 	encap->pseudo = pseudo;
486 	old = rhashtable_lookup_get_insert_fast(&efx->tc->encap_match_ht,
487 						&encap->linkage,
488 						efx_tc_encap_match_ht_params);
489 	if (old) {
490 		/* don't need our new entry */
491 		kfree(encap);
492 		if (pseudo) /* don't need our new pseudo either */
493 			efx_tc_flower_release_encap_match(efx, pseudo);
494 		/* check old and new em_types are compatible */
495 		switch (old->type) {
496 		case EFX_TC_EM_DIRECT:
497 			/* old EM is in hardware, so mustn't overlap with a
498 			 * pseudo, but may be shared with another direct EM
499 			 */
500 			if (em_type == EFX_TC_EM_DIRECT)
501 				break;
502 			NL_SET_ERR_MSG_MOD(extack, "Pseudo encap match conflicts with existing direct entry");
503 			return -EEXIST;
504 		case EFX_TC_EM_PSEUDO_MASK:
505 			/* old EM is protecting a ToS- or src port-qualified
506 			 * filter, so may only be shared with another pseudo
507 			 * for the same ToS and src port masks.
508 			 */
509 			if (em_type != EFX_TC_EM_PSEUDO_MASK) {
510 				NL_SET_ERR_MSG_FMT_MOD(extack,
511 						       "%s encap match conflicts with existing pseudo(MASK) entry",
512 						       em_type ? "Pseudo" : "Direct");
513 				return -EEXIST;
514 			}
515 			if (child_ip_tos_mask != old->child_ip_tos_mask) {
516 				NL_SET_ERR_MSG_FMT_MOD(extack,
517 						       "Pseudo encap match for TOS mask %#04x conflicts with existing pseudo(MASK) entry for TOS mask %#04x",
518 						       child_ip_tos_mask,
519 						       old->child_ip_tos_mask);
520 				return -EEXIST;
521 			}
522 			if (child_udp_sport_mask != old->child_udp_sport_mask) {
523 				NL_SET_ERR_MSG_FMT_MOD(extack,
524 						       "Pseudo encap match for UDP src port mask %#x conflicts with existing pseudo(MASK) entry for mask %#x",
525 						       child_udp_sport_mask,
526 						       old->child_udp_sport_mask);
527 				return -EEXIST;
528 			}
529 			break;
530 		default: /* Unrecognised pseudo-type.  Just say no */
531 			NL_SET_ERR_MSG_FMT_MOD(extack,
532 					       "%s encap match conflicts with existing pseudo(%d) entry",
533 					       em_type ? "Pseudo" : "Direct",
534 					       old->type);
535 			return -EEXIST;
536 		}
537 		/* check old and new tun_types are compatible */
538 		if (old->tun_type != type) {
539 			NL_SET_ERR_MSG_FMT_MOD(extack,
540 					       "Egress encap match with conflicting tun_type %u != %u",
541 					       old->tun_type, type);
542 			return -EEXIST;
543 		}
544 		if (!refcount_inc_not_zero(&old->ref))
545 			return -EAGAIN;
546 		/* existing entry found */
547 		encap = old;
548 	} else {
549 		if (em_type == EFX_TC_EM_DIRECT) {
550 			rc = efx_mae_register_encap_match(efx, encap);
551 			if (rc) {
552 				NL_SET_ERR_MSG_MOD(extack, "Failed to record egress encap match in HW");
553 				goto fail;
554 			}
555 		}
556 		refcount_set(&encap->ref, 1);
557 	}
558 	match->encap = encap;
559 	return 0;
560 fail:
561 	rhashtable_remove_fast(&efx->tc->encap_match_ht, &encap->linkage,
562 			       efx_tc_encap_match_ht_params);
563 	kfree(encap);
564 fail_pseudo:
565 	if (pseudo)
566 		efx_tc_flower_release_encap_match(efx, pseudo);
567 	return rc;
568 }
569 
570 static void efx_tc_delete_rule(struct efx_nic *efx, struct efx_tc_flow_rule *rule)
571 {
572 	efx_mae_delete_rule(efx, rule->fw_id);
573 
574 	/* Release entries in subsidiary tables */
575 	efx_tc_free_action_set_list(efx, &rule->acts, true);
576 	if (rule->match.encap)
577 		efx_tc_flower_release_encap_match(efx, rule->match.encap);
578 	rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
579 }
580 
581 static const char *efx_tc_encap_type_name(enum efx_encap_type typ)
582 {
583 	switch (typ) {
584 	case EFX_ENCAP_TYPE_NONE:
585 		return "none";
586 	case EFX_ENCAP_TYPE_VXLAN:
587 		return "vxlan";
588 	case EFX_ENCAP_TYPE_GENEVE:
589 		return "geneve";
590 	default:
591 		pr_warn_once("Unknown efx_encap_type %d encountered\n", typ);
592 		return "unknown";
593 	}
594 }
595 
596 /* For details of action order constraints refer to SF-123102-TC-1§12.6.1 */
597 enum efx_tc_action_order {
598 	EFX_TC_AO_DECAP,
599 	EFX_TC_AO_VLAN_POP,
600 	EFX_TC_AO_VLAN_PUSH,
601 	EFX_TC_AO_COUNT,
602 	EFX_TC_AO_ENCAP,
603 	EFX_TC_AO_DELIVER
604 };
605 /* Determine whether we can add @new action without violating order */
606 static bool efx_tc_flower_action_order_ok(const struct efx_tc_action_set *act,
607 					  enum efx_tc_action_order new)
608 {
609 	switch (new) {
610 	case EFX_TC_AO_DECAP:
611 		if (act->decap)
612 			return false;
613 		fallthrough;
614 	case EFX_TC_AO_VLAN_POP:
615 		if (act->vlan_pop >= 2)
616 			return false;
617 		/* If we've already pushed a VLAN, we can't then pop it;
618 		 * the hardware would instead try to pop an existing VLAN
619 		 * before pushing the new one.
620 		 */
621 		if (act->vlan_push)
622 			return false;
623 		fallthrough;
624 	case EFX_TC_AO_VLAN_PUSH:
625 		if (act->vlan_push >= 2)
626 			return false;
627 		fallthrough;
628 	case EFX_TC_AO_COUNT:
629 		if (act->count)
630 			return false;
631 		fallthrough;
632 	case EFX_TC_AO_ENCAP:
633 		if (act->encap_md)
634 			return false;
635 		fallthrough;
636 	case EFX_TC_AO_DELIVER:
637 		return !act->deliver;
638 	default:
639 		/* Bad caller.  Whatever they wanted to do, say they can't. */
640 		WARN_ON_ONCE(1);
641 		return false;
642 	}
643 }
644 
645 static int efx_tc_flower_replace_foreign(struct efx_nic *efx,
646 					 struct net_device *net_dev,
647 					 struct flow_cls_offload *tc)
648 {
649 	struct flow_rule *fr = flow_cls_offload_flow_rule(tc);
650 	struct netlink_ext_ack *extack = tc->common.extack;
651 	struct efx_tc_flow_rule *rule = NULL, *old = NULL;
652 	struct efx_tc_action_set *act = NULL;
653 	bool found = false, uplinked = false;
654 	const struct flow_action_entry *fa;
655 	struct efx_tc_match match;
656 	struct efx_rep *to_efv;
657 	s64 rc;
658 	int i;
659 
660 	/* Parse match */
661 	memset(&match, 0, sizeof(match));
662 	rc = efx_tc_flower_parse_match(efx, fr, &match, NULL);
663 	if (rc)
664 		return rc;
665 	/* The rule as given to us doesn't specify a source netdevice.
666 	 * But, determining whether packets from a VF should match it is
667 	 * complicated, so leave those to the software slowpath: qualify
668 	 * the filter with source m-port == wire.
669 	 */
670 	rc = efx_tc_flower_external_mport(efx, EFX_EFV_PF);
671 	if (rc < 0) {
672 		NL_SET_ERR_MSG_MOD(extack, "Failed to identify ingress m-port for foreign filter");
673 		return rc;
674 	}
675 	match.value.ingress_port = rc;
676 	match.mask.ingress_port = ~0;
677 
678 	if (tc->common.chain_index) {
679 		NL_SET_ERR_MSG_MOD(extack, "No support for nonzero chain_index");
680 		return -EOPNOTSUPP;
681 	}
682 	match.mask.recirc_id = 0xff;
683 
684 	flow_action_for_each(i, fa, &fr->action) {
685 		switch (fa->id) {
686 		case FLOW_ACTION_REDIRECT:
687 		case FLOW_ACTION_MIRRED: /* mirred means mirror here */
688 			to_efv = efx_tc_flower_lookup_efv(efx, fa->dev);
689 			if (IS_ERR(to_efv))
690 				continue;
691 			found = true;
692 			break;
693 		default:
694 			break;
695 		}
696 	}
697 	if (!found) { /* We don't care. */
698 		netif_dbg(efx, drv, efx->net_dev,
699 			  "Ignoring foreign filter that doesn't egdev us\n");
700 		return -EOPNOTSUPP;
701 	}
702 
703 	rc = efx_mae_match_check_caps(efx, &match.mask, NULL);
704 	if (rc)
705 		return rc;
706 
707 	if (efx_tc_match_is_encap(&match.mask)) {
708 		enum efx_encap_type type;
709 
710 		type = efx_tc_indr_netdev_type(net_dev);
711 		if (type == EFX_ENCAP_TYPE_NONE) {
712 			NL_SET_ERR_MSG_MOD(extack,
713 					   "Egress encap match on unsupported tunnel device");
714 			return -EOPNOTSUPP;
715 		}
716 
717 		rc = efx_mae_check_encap_type_supported(efx, type);
718 		if (rc) {
719 			NL_SET_ERR_MSG_FMT_MOD(extack,
720 					       "Firmware reports no support for %s encap match",
721 					       efx_tc_encap_type_name(type));
722 			return rc;
723 		}
724 
725 		rc = efx_tc_flower_record_encap_match(efx, &match, type,
726 						      EFX_TC_EM_DIRECT, 0, 0,
727 						      extack);
728 		if (rc)
729 			return rc;
730 	} else {
731 		/* This is not a tunnel decap rule, ignore it */
732 		netif_dbg(efx, drv, efx->net_dev,
733 			  "Ignoring foreign filter without encap match\n");
734 		return -EOPNOTSUPP;
735 	}
736 
737 	rule = kzalloc(sizeof(*rule), GFP_USER);
738 	if (!rule) {
739 		rc = -ENOMEM;
740 		goto out_free;
741 	}
742 	INIT_LIST_HEAD(&rule->acts.list);
743 	rule->cookie = tc->cookie;
744 	old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht,
745 						&rule->linkage,
746 						efx_tc_match_action_ht_params);
747 	if (old) {
748 		netif_dbg(efx, drv, efx->net_dev,
749 			  "Ignoring already-offloaded rule (cookie %lx)\n",
750 			  tc->cookie);
751 		rc = -EEXIST;
752 		goto out_free;
753 	}
754 
755 	act = kzalloc(sizeof(*act), GFP_USER);
756 	if (!act) {
757 		rc = -ENOMEM;
758 		goto release;
759 	}
760 
761 	/* Parse actions.  For foreign rules we only support decap & redirect.
762 	 * See corresponding code in efx_tc_flower_replace() for theory of
763 	 * operation & how 'act' cursor is used.
764 	 */
765 	flow_action_for_each(i, fa, &fr->action) {
766 		struct efx_tc_action_set save;
767 
768 		switch (fa->id) {
769 		case FLOW_ACTION_REDIRECT:
770 		case FLOW_ACTION_MIRRED:
771 			/* See corresponding code in efx_tc_flower_replace() for
772 			 * long explanations of what's going on here.
773 			 */
774 			save = *act;
775 			if (fa->hw_stats) {
776 				struct efx_tc_counter_index *ctr;
777 
778 				if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) {
779 					NL_SET_ERR_MSG_FMT_MOD(extack,
780 							       "hw_stats_type %u not supported (only 'delayed')",
781 							       fa->hw_stats);
782 					rc = -EOPNOTSUPP;
783 					goto release;
784 				}
785 				if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) {
786 					rc = -EOPNOTSUPP;
787 					goto release;
788 				}
789 
790 				ctr = efx_tc_flower_get_counter_index(efx,
791 								      tc->cookie,
792 								      EFX_TC_COUNTER_TYPE_AR);
793 				if (IS_ERR(ctr)) {
794 					rc = PTR_ERR(ctr);
795 					NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter");
796 					goto release;
797 				}
798 				act->count = ctr;
799 			}
800 
801 			if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) {
802 				/* can't happen */
803 				rc = -EOPNOTSUPP;
804 				NL_SET_ERR_MSG_MOD(extack,
805 						   "Deliver action violates action order (can't happen)");
806 				goto release;
807 			}
808 			to_efv = efx_tc_flower_lookup_efv(efx, fa->dev);
809 			/* PF implies egdev is us, in which case we really
810 			 * want to deliver to the uplink (because this is an
811 			 * ingress filter).  If we don't recognise the egdev
812 			 * at all, then we'd better trap so SW can handle it.
813 			 */
814 			if (IS_ERR(to_efv))
815 				to_efv = EFX_EFV_PF;
816 			if (to_efv == EFX_EFV_PF) {
817 				if (uplinked)
818 					break;
819 				uplinked = true;
820 			}
821 			rc = efx_tc_flower_internal_mport(efx, to_efv);
822 			if (rc < 0) {
823 				NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port");
824 				goto release;
825 			}
826 			act->dest_mport = rc;
827 			act->deliver = 1;
828 			rc = efx_mae_alloc_action_set(efx, act);
829 			if (rc) {
830 				NL_SET_ERR_MSG_MOD(extack,
831 						   "Failed to write action set to hw (mirred)");
832 				goto release;
833 			}
834 			list_add_tail(&act->list, &rule->acts.list);
835 			act = NULL;
836 			if (fa->id == FLOW_ACTION_REDIRECT)
837 				break; /* end of the line */
838 			/* Mirror, so continue on with saved act */
839 			act = kzalloc(sizeof(*act), GFP_USER);
840 			if (!act) {
841 				rc = -ENOMEM;
842 				goto release;
843 			}
844 			*act = save;
845 			break;
846 		case FLOW_ACTION_TUNNEL_DECAP:
847 			if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DECAP)) {
848 				rc = -EINVAL;
849 				NL_SET_ERR_MSG_MOD(extack, "Decap action violates action order");
850 				goto release;
851 			}
852 			act->decap = 1;
853 			/* If we previously delivered/trapped to uplink, now
854 			 * that we've decapped we'll want another copy if we
855 			 * try to deliver/trap to uplink again.
856 			 */
857 			uplinked = false;
858 			break;
859 		default:
860 			NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u",
861 					       fa->id);
862 			rc = -EOPNOTSUPP;
863 			goto release;
864 		}
865 	}
866 
867 	if (act) {
868 		if (!uplinked) {
869 			/* Not shot/redirected, so deliver to default dest (which is
870 			 * the uplink, as this is an ingress filter)
871 			 */
872 			efx_mae_mport_uplink(efx, &act->dest_mport);
873 			act->deliver = 1;
874 		}
875 		rc = efx_mae_alloc_action_set(efx, act);
876 		if (rc) {
877 			NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)");
878 			goto release;
879 		}
880 		list_add_tail(&act->list, &rule->acts.list);
881 		act = NULL; /* Prevent double-free in error path */
882 	}
883 
884 	rule->match = match;
885 
886 	netif_dbg(efx, drv, efx->net_dev,
887 		  "Successfully parsed foreign filter (cookie %lx)\n",
888 		  tc->cookie);
889 
890 	rc = efx_mae_alloc_action_set_list(efx, &rule->acts);
891 	if (rc) {
892 		NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw");
893 		goto release;
894 	}
895 	rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC,
896 				 rule->acts.fw_id, &rule->fw_id);
897 	if (rc) {
898 		NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw");
899 		goto release_acts;
900 	}
901 	return 0;
902 
903 release_acts:
904 	efx_mae_free_action_set_list(efx, &rule->acts);
905 release:
906 	/* We failed to insert the rule, so free up any entries we created in
907 	 * subsidiary tables.
908 	 */
909 	if (act)
910 		efx_tc_free_action_set(efx, act, false);
911 	if (rule) {
912 		rhashtable_remove_fast(&efx->tc->match_action_ht,
913 				       &rule->linkage,
914 				       efx_tc_match_action_ht_params);
915 		efx_tc_free_action_set_list(efx, &rule->acts, false);
916 	}
917 out_free:
918 	kfree(rule);
919 	if (match.encap)
920 		efx_tc_flower_release_encap_match(efx, match.encap);
921 	return rc;
922 }
923 
924 static int efx_tc_flower_replace(struct efx_nic *efx,
925 				 struct net_device *net_dev,
926 				 struct flow_cls_offload *tc,
927 				 struct efx_rep *efv)
928 {
929 	struct flow_rule *fr = flow_cls_offload_flow_rule(tc);
930 	struct netlink_ext_ack *extack = tc->common.extack;
931 	const struct ip_tunnel_info *encap_info = NULL;
932 	struct efx_tc_flow_rule *rule = NULL, *old;
933 	struct efx_tc_action_set *act = NULL;
934 	const struct flow_action_entry *fa;
935 	struct efx_rep *from_efv, *to_efv;
936 	struct efx_tc_match match;
937 	u32 acts_id;
938 	s64 rc;
939 	int i;
940 
941 	if (!tc_can_offload_extack(efx->net_dev, extack))
942 		return -EOPNOTSUPP;
943 	if (WARN_ON(!efx->tc))
944 		return -ENETDOWN;
945 	if (WARN_ON(!efx->tc->up))
946 		return -ENETDOWN;
947 
948 	from_efv = efx_tc_flower_lookup_efv(efx, net_dev);
949 	if (IS_ERR(from_efv)) {
950 		/* Not from our PF or representors, so probably a tunnel dev */
951 		return efx_tc_flower_replace_foreign(efx, net_dev, tc);
952 	}
953 
954 	if (efv != from_efv) {
955 		/* can't happen */
956 		NL_SET_ERR_MSG_FMT_MOD(extack, "for %s efv is %snull but from_efv is %snull (can't happen)",
957 				       netdev_name(net_dev), efv ? "non-" : "",
958 				       from_efv ? "non-" : "");
959 		return -EINVAL;
960 	}
961 
962 	/* Parse match */
963 	memset(&match, 0, sizeof(match));
964 	rc = efx_tc_flower_external_mport(efx, from_efv);
965 	if (rc < 0) {
966 		NL_SET_ERR_MSG_MOD(extack, "Failed to identify ingress m-port");
967 		return rc;
968 	}
969 	match.value.ingress_port = rc;
970 	match.mask.ingress_port = ~0;
971 	rc = efx_tc_flower_parse_match(efx, fr, &match, extack);
972 	if (rc)
973 		return rc;
974 	if (efx_tc_match_is_encap(&match.mask)) {
975 		NL_SET_ERR_MSG_MOD(extack, "Ingress enc_key matches not supported");
976 		return -EOPNOTSUPP;
977 	}
978 
979 	if (tc->common.chain_index) {
980 		NL_SET_ERR_MSG_MOD(extack, "No support for nonzero chain_index");
981 		return -EOPNOTSUPP;
982 	}
983 	match.mask.recirc_id = 0xff;
984 
985 	rc = efx_mae_match_check_caps(efx, &match.mask, extack);
986 	if (rc)
987 		return rc;
988 
989 	rule = kzalloc(sizeof(*rule), GFP_USER);
990 	if (!rule)
991 		return -ENOMEM;
992 	INIT_LIST_HEAD(&rule->acts.list);
993 	rule->cookie = tc->cookie;
994 	old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht,
995 						&rule->linkage,
996 						efx_tc_match_action_ht_params);
997 	if (old) {
998 		netif_dbg(efx, drv, efx->net_dev,
999 			  "Already offloaded rule (cookie %lx)\n", tc->cookie);
1000 		NL_SET_ERR_MSG_MOD(extack, "Rule already offloaded");
1001 		kfree(rule);
1002 		return -EEXIST;
1003 	}
1004 
1005 	/* Parse actions */
1006 	act = kzalloc(sizeof(*act), GFP_USER);
1007 	if (!act) {
1008 		rc = -ENOMEM;
1009 		goto release;
1010 	}
1011 
1012 	/**
1013 	 * DOC: TC action translation
1014 	 *
1015 	 * Actions in TC are sequential and cumulative, with delivery actions
1016 	 * potentially anywhere in the order.  The EF100 MAE, however, takes
1017 	 * an 'action set list' consisting of 'action sets', each of which is
1018 	 * applied to the _original_ packet, and consists of a set of optional
1019 	 * actions in a fixed order with delivery at the end.
1020 	 * To translate between these two models, we maintain a 'cursor', @act,
1021 	 * which describes the cumulative effect of all the packet-mutating
1022 	 * actions encountered so far; on handling a delivery (mirred or drop)
1023 	 * action, once the action-set has been inserted into hardware, we
1024 	 * append @act to the action-set list (@rule->acts); if this is a pipe
1025 	 * action (mirred mirror) we then allocate a new @act with a copy of
1026 	 * the cursor state _before_ the delivery action, otherwise we set @act
1027 	 * to %NULL.
1028 	 * This ensures that every allocated action-set is either attached to
1029 	 * @rule->acts or pointed to by @act (and never both), and that only
1030 	 * those action-sets in @rule->acts exist in hardware.  Consequently,
1031 	 * in the failure path, @act only needs to be freed in memory, whereas
1032 	 * for @rule->acts we remove each action-set from hardware before
1033 	 * freeing it (efx_tc_free_action_set_list()), even if the action-set
1034 	 * list itself is not in hardware.
1035 	 */
1036 	flow_action_for_each(i, fa, &fr->action) {
1037 		struct efx_tc_action_set save;
1038 		u16 tci;
1039 
1040 		if (!act) {
1041 			/* more actions after a non-pipe action */
1042 			NL_SET_ERR_MSG_MOD(extack, "Action follows non-pipe action");
1043 			rc = -EINVAL;
1044 			goto release;
1045 		}
1046 
1047 		if ((fa->id == FLOW_ACTION_REDIRECT ||
1048 		     fa->id == FLOW_ACTION_MIRRED ||
1049 		     fa->id == FLOW_ACTION_DROP) && fa->hw_stats) {
1050 			struct efx_tc_counter_index *ctr;
1051 
1052 			/* Currently the only actions that want stats are
1053 			 * mirred and gact (ok, shot, trap, goto-chain), which
1054 			 * means we want stats just before delivery.  Also,
1055 			 * note that tunnel_key set shouldn't change the length
1056 			 * — it's only the subsequent mirred that does that,
1057 			 * and the stats are taken _before_ the mirred action
1058 			 * happens.
1059 			 */
1060 			if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) {
1061 				/* All supported actions that count either steal
1062 				 * (gact shot, mirred redirect) or clone act
1063 				 * (mirred mirror), so we should never get two
1064 				 * count actions on one action_set.
1065 				 */
1066 				NL_SET_ERR_MSG_MOD(extack, "Count-action conflict (can't happen)");
1067 				rc = -EOPNOTSUPP;
1068 				goto release;
1069 			}
1070 
1071 			if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) {
1072 				NL_SET_ERR_MSG_FMT_MOD(extack, "hw_stats_type %u not supported (only 'delayed')",
1073 						       fa->hw_stats);
1074 				rc = -EOPNOTSUPP;
1075 				goto release;
1076 			}
1077 
1078 			ctr = efx_tc_flower_get_counter_index(efx, tc->cookie,
1079 							      EFX_TC_COUNTER_TYPE_AR);
1080 			if (IS_ERR(ctr)) {
1081 				rc = PTR_ERR(ctr);
1082 				NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter");
1083 				goto release;
1084 			}
1085 			act->count = ctr;
1086 		}
1087 
1088 		switch (fa->id) {
1089 		case FLOW_ACTION_DROP:
1090 			rc = efx_mae_alloc_action_set(efx, act);
1091 			if (rc) {
1092 				NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (drop)");
1093 				goto release;
1094 			}
1095 			list_add_tail(&act->list, &rule->acts.list);
1096 			act = NULL; /* end of the line */
1097 			break;
1098 		case FLOW_ACTION_REDIRECT:
1099 		case FLOW_ACTION_MIRRED:
1100 			save = *act;
1101 
1102 			if (encap_info) {
1103 				struct efx_tc_encap_action *encap;
1104 
1105 				if (!efx_tc_flower_action_order_ok(act,
1106 								   EFX_TC_AO_ENCAP)) {
1107 					rc = -EOPNOTSUPP;
1108 					NL_SET_ERR_MSG_MOD(extack, "Encap action violates action order");
1109 					goto release;
1110 				}
1111 				encap = efx_tc_flower_create_encap_md(
1112 						efx, encap_info, fa->dev, extack);
1113 				if (IS_ERR_OR_NULL(encap)) {
1114 					rc = PTR_ERR(encap);
1115 					if (!rc)
1116 						rc = -EIO; /* arbitrary */
1117 					goto release;
1118 				}
1119 				act->encap_md = encap;
1120 				list_add_tail(&act->encap_user, &encap->users);
1121 				act->dest_mport = encap->dest_mport;
1122 				act->deliver = 1;
1123 				rc = efx_mae_alloc_action_set(efx, act);
1124 				if (rc) {
1125 					NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (encap)");
1126 					goto release;
1127 				}
1128 				list_add_tail(&act->list, &rule->acts.list);
1129 				act->user = &rule->acts;
1130 				act = NULL;
1131 				if (fa->id == FLOW_ACTION_REDIRECT)
1132 					break; /* end of the line */
1133 				/* Mirror, so continue on with saved act */
1134 				save.count = NULL;
1135 				act = kzalloc(sizeof(*act), GFP_USER);
1136 				if (!act) {
1137 					rc = -ENOMEM;
1138 					goto release;
1139 				}
1140 				*act = save;
1141 				break;
1142 			}
1143 
1144 			if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) {
1145 				/* can't happen */
1146 				rc = -EOPNOTSUPP;
1147 				NL_SET_ERR_MSG_MOD(extack, "Deliver action violates action order (can't happen)");
1148 				goto release;
1149 			}
1150 
1151 			to_efv = efx_tc_flower_lookup_efv(efx, fa->dev);
1152 			if (IS_ERR(to_efv)) {
1153 				NL_SET_ERR_MSG_MOD(extack, "Mirred egress device not on switch");
1154 				rc = PTR_ERR(to_efv);
1155 				goto release;
1156 			}
1157 			rc = efx_tc_flower_external_mport(efx, to_efv);
1158 			if (rc < 0) {
1159 				NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port");
1160 				goto release;
1161 			}
1162 			act->dest_mport = rc;
1163 			act->deliver = 1;
1164 			rc = efx_mae_alloc_action_set(efx, act);
1165 			if (rc) {
1166 				NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (mirred)");
1167 				goto release;
1168 			}
1169 			list_add_tail(&act->list, &rule->acts.list);
1170 			act = NULL;
1171 			if (fa->id == FLOW_ACTION_REDIRECT)
1172 				break; /* end of the line */
1173 			/* Mirror, so continue on with saved act */
1174 			save.count = NULL;
1175 			act = kzalloc(sizeof(*act), GFP_USER);
1176 			if (!act) {
1177 				rc = -ENOMEM;
1178 				goto release;
1179 			}
1180 			*act = save;
1181 			break;
1182 		case FLOW_ACTION_VLAN_POP:
1183 			if (act->vlan_push) {
1184 				act->vlan_push--;
1185 			} else if (efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_POP)) {
1186 				act->vlan_pop++;
1187 			} else {
1188 				NL_SET_ERR_MSG_MOD(extack,
1189 						   "More than two VLAN pops, or action order violated");
1190 				rc = -EINVAL;
1191 				goto release;
1192 			}
1193 			break;
1194 		case FLOW_ACTION_VLAN_PUSH:
1195 			if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_PUSH)) {
1196 				rc = -EINVAL;
1197 				NL_SET_ERR_MSG_MOD(extack,
1198 						   "More than two VLAN pushes, or action order violated");
1199 				goto release;
1200 			}
1201 			tci = fa->vlan.vid & VLAN_VID_MASK;
1202 			tci |= fa->vlan.prio << VLAN_PRIO_SHIFT;
1203 			act->vlan_tci[act->vlan_push] = cpu_to_be16(tci);
1204 			act->vlan_proto[act->vlan_push] = fa->vlan.proto;
1205 			act->vlan_push++;
1206 			break;
1207 		case FLOW_ACTION_TUNNEL_ENCAP:
1208 			if (encap_info) {
1209 				/* Can't specify encap multiple times.
1210 				 * If you want to overwrite an existing
1211 				 * encap_info, use an intervening
1212 				 * FLOW_ACTION_TUNNEL_DECAP to clear it.
1213 				 */
1214 				NL_SET_ERR_MSG_MOD(extack, "Tunnel key set when already set");
1215 				rc = -EINVAL;
1216 				goto release;
1217 			}
1218 			if (!fa->tunnel) {
1219 				NL_SET_ERR_MSG_MOD(extack, "Tunnel key set is missing key");
1220 				rc = -EOPNOTSUPP;
1221 				goto release;
1222 			}
1223 			encap_info = fa->tunnel;
1224 			break;
1225 		case FLOW_ACTION_TUNNEL_DECAP:
1226 			if (encap_info) {
1227 				encap_info = NULL;
1228 				break;
1229 			}
1230 			/* Since we don't support enc_key matches on ingress
1231 			 * (and if we did there'd be no tunnel-device to give
1232 			 * us a type), we can't offload a decap that's not
1233 			 * just undoing a previous encap action.
1234 			 */
1235 			NL_SET_ERR_MSG_MOD(extack, "Cannot offload tunnel decap action without tunnel device");
1236 			rc = -EOPNOTSUPP;
1237 			goto release;
1238 		default:
1239 			NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u",
1240 					       fa->id);
1241 			rc = -EOPNOTSUPP;
1242 			goto release;
1243 		}
1244 	}
1245 
1246 	if (act) {
1247 		/* Not shot/redirected, so deliver to default dest */
1248 		if (from_efv == EFX_EFV_PF)
1249 			/* Rule applies to traffic from the wire,
1250 			 * and default dest is thus the PF
1251 			 */
1252 			efx_mae_mport_uplink(efx, &act->dest_mport);
1253 		else
1254 			/* Representor, so rule applies to traffic from
1255 			 * representee, and default dest is thus the rep.
1256 			 * All reps use the same mport for delivery
1257 			 */
1258 			efx_mae_mport_mport(efx, efx->tc->reps_mport_id,
1259 					    &act->dest_mport);
1260 		act->deliver = 1;
1261 		rc = efx_mae_alloc_action_set(efx, act);
1262 		if (rc) {
1263 			NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)");
1264 			goto release;
1265 		}
1266 		list_add_tail(&act->list, &rule->acts.list);
1267 		act = NULL; /* Prevent double-free in error path */
1268 	}
1269 
1270 	netif_dbg(efx, drv, efx->net_dev,
1271 		  "Successfully parsed filter (cookie %lx)\n",
1272 		  tc->cookie);
1273 
1274 	rule->match = match;
1275 
1276 	rc = efx_mae_alloc_action_set_list(efx, &rule->acts);
1277 	if (rc) {
1278 		NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw");
1279 		goto release;
1280 	}
1281 	if (from_efv == EFX_EFV_PF)
1282 		/* PF netdev, so rule applies to traffic from wire */
1283 		rule->fallback = &efx->tc->facts.pf;
1284 	else
1285 		/* repdev, so rule applies to traffic from representee */
1286 		rule->fallback = &efx->tc->facts.reps;
1287 	if (!efx_tc_check_ready(efx, rule)) {
1288 		netif_dbg(efx, drv, efx->net_dev, "action not ready for hw\n");
1289 		acts_id = rule->fallback->fw_id;
1290 	} else {
1291 		netif_dbg(efx, drv, efx->net_dev, "ready for hw\n");
1292 		acts_id = rule->acts.fw_id;
1293 	}
1294 	rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC,
1295 				 acts_id, &rule->fw_id);
1296 	if (rc) {
1297 		NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw");
1298 		goto release_acts;
1299 	}
1300 	return 0;
1301 
1302 release_acts:
1303 	efx_mae_free_action_set_list(efx, &rule->acts);
1304 release:
1305 	/* We failed to insert the rule, so free up any entries we created in
1306 	 * subsidiary tables.
1307 	 */
1308 	if (act)
1309 		efx_tc_free_action_set(efx, act, false);
1310 	if (rule) {
1311 		rhashtable_remove_fast(&efx->tc->match_action_ht,
1312 				       &rule->linkage,
1313 				       efx_tc_match_action_ht_params);
1314 		efx_tc_free_action_set_list(efx, &rule->acts, false);
1315 	}
1316 	kfree(rule);
1317 	return rc;
1318 }
1319 
1320 static int efx_tc_flower_destroy(struct efx_nic *efx,
1321 				 struct net_device *net_dev,
1322 				 struct flow_cls_offload *tc)
1323 {
1324 	struct netlink_ext_ack *extack = tc->common.extack;
1325 	struct efx_tc_flow_rule *rule;
1326 
1327 	rule = rhashtable_lookup_fast(&efx->tc->match_action_ht, &tc->cookie,
1328 				      efx_tc_match_action_ht_params);
1329 	if (!rule) {
1330 		/* Only log a message if we're the ingress device.  Otherwise
1331 		 * it's a foreign filter and we might just not have been
1332 		 * interested (e.g. we might not have been the egress device
1333 		 * either).
1334 		 */
1335 		if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev)))
1336 			netif_warn(efx, drv, efx->net_dev,
1337 				   "Filter %lx not found to remove\n", tc->cookie);
1338 		NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules");
1339 		return -ENOENT;
1340 	}
1341 
1342 	/* Remove it from HW */
1343 	efx_tc_delete_rule(efx, rule);
1344 	/* Delete it from SW */
1345 	rhashtable_remove_fast(&efx->tc->match_action_ht, &rule->linkage,
1346 			       efx_tc_match_action_ht_params);
1347 	netif_dbg(efx, drv, efx->net_dev, "Removed filter %lx\n", rule->cookie);
1348 	kfree(rule);
1349 	return 0;
1350 }
1351 
1352 static int efx_tc_flower_stats(struct efx_nic *efx, struct net_device *net_dev,
1353 			       struct flow_cls_offload *tc)
1354 {
1355 	struct netlink_ext_ack *extack = tc->common.extack;
1356 	struct efx_tc_counter_index *ctr;
1357 	struct efx_tc_counter *cnt;
1358 	u64 packets, bytes;
1359 
1360 	ctr = efx_tc_flower_find_counter_index(efx, tc->cookie);
1361 	if (!ctr) {
1362 		/* See comment in efx_tc_flower_destroy() */
1363 		if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev)))
1364 			if (net_ratelimit())
1365 				netif_warn(efx, drv, efx->net_dev,
1366 					   "Filter %lx not found for stats\n",
1367 					   tc->cookie);
1368 		NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules");
1369 		return -ENOENT;
1370 	}
1371 	if (WARN_ON(!ctr->cnt)) /* can't happen */
1372 		return -EIO;
1373 	cnt = ctr->cnt;
1374 
1375 	spin_lock_bh(&cnt->lock);
1376 	/* Report only new pkts/bytes since last time TC asked */
1377 	packets = cnt->packets;
1378 	bytes = cnt->bytes;
1379 	flow_stats_update(&tc->stats, bytes - cnt->old_bytes,
1380 			  packets - cnt->old_packets, 0, cnt->touched,
1381 			  FLOW_ACTION_HW_STATS_DELAYED);
1382 	cnt->old_packets = packets;
1383 	cnt->old_bytes = bytes;
1384 	spin_unlock_bh(&cnt->lock);
1385 	return 0;
1386 }
1387 
1388 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev,
1389 		  struct flow_cls_offload *tc, struct efx_rep *efv)
1390 {
1391 	int rc;
1392 
1393 	if (!efx->tc)
1394 		return -EOPNOTSUPP;
1395 
1396 	mutex_lock(&efx->tc->mutex);
1397 	switch (tc->command) {
1398 	case FLOW_CLS_REPLACE:
1399 		rc = efx_tc_flower_replace(efx, net_dev, tc, efv);
1400 		break;
1401 	case FLOW_CLS_DESTROY:
1402 		rc = efx_tc_flower_destroy(efx, net_dev, tc);
1403 		break;
1404 	case FLOW_CLS_STATS:
1405 		rc = efx_tc_flower_stats(efx, net_dev, tc);
1406 		break;
1407 	default:
1408 		rc = -EOPNOTSUPP;
1409 		break;
1410 	}
1411 	mutex_unlock(&efx->tc->mutex);
1412 	return rc;
1413 }
1414 
1415 static int efx_tc_configure_default_rule(struct efx_nic *efx, u32 ing_port,
1416 					 u32 eg_port, struct efx_tc_flow_rule *rule)
1417 {
1418 	struct efx_tc_action_set_list *acts = &rule->acts;
1419 	struct efx_tc_match *match = &rule->match;
1420 	struct efx_tc_action_set *act;
1421 	int rc;
1422 
1423 	match->value.ingress_port = ing_port;
1424 	match->mask.ingress_port = ~0;
1425 	act = kzalloc(sizeof(*act), GFP_KERNEL);
1426 	if (!act)
1427 		return -ENOMEM;
1428 	act->deliver = 1;
1429 	act->dest_mport = eg_port;
1430 	rc = efx_mae_alloc_action_set(efx, act);
1431 	if (rc)
1432 		goto fail1;
1433 	EFX_WARN_ON_PARANOID(!list_empty(&acts->list));
1434 	list_add_tail(&act->list, &acts->list);
1435 	rc = efx_mae_alloc_action_set_list(efx, acts);
1436 	if (rc)
1437 		goto fail2;
1438 	rc = efx_mae_insert_rule(efx, match, EFX_TC_PRIO_DFLT,
1439 				 acts->fw_id, &rule->fw_id);
1440 	if (rc)
1441 		goto fail3;
1442 	return 0;
1443 fail3:
1444 	efx_mae_free_action_set_list(efx, acts);
1445 fail2:
1446 	list_del(&act->list);
1447 	efx_mae_free_action_set(efx, act->fw_id);
1448 fail1:
1449 	kfree(act);
1450 	return rc;
1451 }
1452 
1453 static int efx_tc_configure_default_rule_pf(struct efx_nic *efx)
1454 {
1455 	struct efx_tc_flow_rule *rule = &efx->tc->dflt.pf;
1456 	u32 ing_port, eg_port;
1457 
1458 	efx_mae_mport_uplink(efx, &ing_port);
1459 	efx_mae_mport_wire(efx, &eg_port);
1460 	return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
1461 }
1462 
1463 static int efx_tc_configure_default_rule_wire(struct efx_nic *efx)
1464 {
1465 	struct efx_tc_flow_rule *rule = &efx->tc->dflt.wire;
1466 	u32 ing_port, eg_port;
1467 
1468 	efx_mae_mport_wire(efx, &ing_port);
1469 	efx_mae_mport_uplink(efx, &eg_port);
1470 	return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
1471 }
1472 
1473 int efx_tc_configure_default_rule_rep(struct efx_rep *efv)
1474 {
1475 	struct efx_tc_flow_rule *rule = &efv->dflt;
1476 	struct efx_nic *efx = efv->parent;
1477 	u32 ing_port, eg_port;
1478 
1479 	efx_mae_mport_mport(efx, efv->mport, &ing_port);
1480 	efx_mae_mport_mport(efx, efx->tc->reps_mport_id, &eg_port);
1481 	return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
1482 }
1483 
1484 void efx_tc_deconfigure_default_rule(struct efx_nic *efx,
1485 				     struct efx_tc_flow_rule *rule)
1486 {
1487 	if (rule->fw_id != MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL)
1488 		efx_tc_delete_rule(efx, rule);
1489 	rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
1490 }
1491 
1492 static int efx_tc_configure_fallback_acts(struct efx_nic *efx, u32 eg_port,
1493 					  struct efx_tc_action_set_list *acts)
1494 {
1495 	struct efx_tc_action_set *act;
1496 	int rc;
1497 
1498 	act = kzalloc(sizeof(*act), GFP_KERNEL);
1499 	if (!act)
1500 		return -ENOMEM;
1501 	act->deliver = 1;
1502 	act->dest_mport = eg_port;
1503 	rc = efx_mae_alloc_action_set(efx, act);
1504 	if (rc)
1505 		goto fail1;
1506 	EFX_WARN_ON_PARANOID(!list_empty(&acts->list));
1507 	list_add_tail(&act->list, &acts->list);
1508 	rc = efx_mae_alloc_action_set_list(efx, acts);
1509 	if (rc)
1510 		goto fail2;
1511 	return 0;
1512 fail2:
1513 	list_del(&act->list);
1514 	efx_mae_free_action_set(efx, act->fw_id);
1515 fail1:
1516 	kfree(act);
1517 	return rc;
1518 }
1519 
1520 static int efx_tc_configure_fallback_acts_pf(struct efx_nic *efx)
1521 {
1522 	struct efx_tc_action_set_list *acts = &efx->tc->facts.pf;
1523 	u32 eg_port;
1524 
1525 	efx_mae_mport_uplink(efx, &eg_port);
1526 	return efx_tc_configure_fallback_acts(efx, eg_port, acts);
1527 }
1528 
1529 static int efx_tc_configure_fallback_acts_reps(struct efx_nic *efx)
1530 {
1531 	struct efx_tc_action_set_list *acts = &efx->tc->facts.reps;
1532 	u32 eg_port;
1533 
1534 	efx_mae_mport_mport(efx, efx->tc->reps_mport_id, &eg_port);
1535 	return efx_tc_configure_fallback_acts(efx, eg_port, acts);
1536 }
1537 
1538 static void efx_tc_deconfigure_fallback_acts(struct efx_nic *efx,
1539 					     struct efx_tc_action_set_list *acts)
1540 {
1541 	efx_tc_free_action_set_list(efx, acts, true);
1542 }
1543 
1544 static int efx_tc_configure_rep_mport(struct efx_nic *efx)
1545 {
1546 	u32 rep_mport_label;
1547 	int rc;
1548 
1549 	rc = efx_mae_allocate_mport(efx, &efx->tc->reps_mport_id, &rep_mport_label);
1550 	if (rc)
1551 		return rc;
1552 	pci_dbg(efx->pci_dev, "created rep mport 0x%08x (0x%04x)\n",
1553 		efx->tc->reps_mport_id, rep_mport_label);
1554 	/* Use mport *selector* as vport ID */
1555 	efx_mae_mport_mport(efx, efx->tc->reps_mport_id,
1556 			    &efx->tc->reps_mport_vport_id);
1557 	return 0;
1558 }
1559 
1560 static void efx_tc_deconfigure_rep_mport(struct efx_nic *efx)
1561 {
1562 	efx_mae_free_mport(efx, efx->tc->reps_mport_id);
1563 	efx->tc->reps_mport_id = MAE_MPORT_SELECTOR_NULL;
1564 }
1565 
1566 int efx_tc_insert_rep_filters(struct efx_nic *efx)
1567 {
1568 	struct efx_filter_spec promisc, allmulti;
1569 	int rc;
1570 
1571 	if (efx->type->is_vf)
1572 		return 0;
1573 	if (!efx->tc)
1574 		return 0;
1575 	efx_filter_init_rx(&promisc, EFX_FILTER_PRI_REQUIRED, 0, 0);
1576 	efx_filter_set_uc_def(&promisc);
1577 	efx_filter_set_vport_id(&promisc, efx->tc->reps_mport_vport_id);
1578 	rc = efx_filter_insert_filter(efx, &promisc, false);
1579 	if (rc < 0)
1580 		return rc;
1581 	efx->tc->reps_filter_uc = rc;
1582 	efx_filter_init_rx(&allmulti, EFX_FILTER_PRI_REQUIRED, 0, 0);
1583 	efx_filter_set_mc_def(&allmulti);
1584 	efx_filter_set_vport_id(&allmulti, efx->tc->reps_mport_vport_id);
1585 	rc = efx_filter_insert_filter(efx, &allmulti, false);
1586 	if (rc < 0)
1587 		return rc;
1588 	efx->tc->reps_filter_mc = rc;
1589 	return 0;
1590 }
1591 
1592 void efx_tc_remove_rep_filters(struct efx_nic *efx)
1593 {
1594 	if (efx->type->is_vf)
1595 		return;
1596 	if (!efx->tc)
1597 		return;
1598 	if (efx->tc->reps_filter_mc >= 0)
1599 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_mc);
1600 	efx->tc->reps_filter_mc = -1;
1601 	if (efx->tc->reps_filter_uc >= 0)
1602 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_uc);
1603 	efx->tc->reps_filter_uc = -1;
1604 }
1605 
1606 int efx_init_tc(struct efx_nic *efx)
1607 {
1608 	int rc;
1609 
1610 	rc = efx_mae_get_caps(efx, efx->tc->caps);
1611 	if (rc)
1612 		return rc;
1613 	if (efx->tc->caps->match_field_count > MAE_NUM_FIELDS)
1614 		/* Firmware supports some match fields the driver doesn't know
1615 		 * about.  Not fatal, unless any of those fields are required
1616 		 * (MAE_FIELD_SUPPORTED_MATCH_ALWAYS) but if so we don't know.
1617 		 */
1618 		netif_warn(efx, probe, efx->net_dev,
1619 			   "FW reports additional match fields %u\n",
1620 			   efx->tc->caps->match_field_count);
1621 	if (efx->tc->caps->action_prios < EFX_TC_PRIO__NUM) {
1622 		netif_err(efx, probe, efx->net_dev,
1623 			  "Too few action prios supported (have %u, need %u)\n",
1624 			  efx->tc->caps->action_prios, EFX_TC_PRIO__NUM);
1625 		return -EIO;
1626 	}
1627 	rc = efx_tc_configure_default_rule_pf(efx);
1628 	if (rc)
1629 		return rc;
1630 	rc = efx_tc_configure_default_rule_wire(efx);
1631 	if (rc)
1632 		return rc;
1633 	rc = efx_tc_configure_rep_mport(efx);
1634 	if (rc)
1635 		return rc;
1636 	rc = efx_tc_configure_fallback_acts_pf(efx);
1637 	if (rc)
1638 		return rc;
1639 	rc = efx_tc_configure_fallback_acts_reps(efx);
1640 	if (rc)
1641 		return rc;
1642 	efx->tc->up = true;
1643 	rc = flow_indr_dev_register(efx_tc_indr_setup_cb, efx);
1644 	if (rc)
1645 		return rc;
1646 	return 0;
1647 }
1648 
1649 void efx_fini_tc(struct efx_nic *efx)
1650 {
1651 	/* We can get called even if efx_init_struct_tc() failed */
1652 	if (!efx->tc)
1653 		return;
1654 	if (efx->tc->up)
1655 		flow_indr_dev_unregister(efx_tc_indr_setup_cb, efx, efx_tc_block_unbind);
1656 	efx_tc_deconfigure_rep_mport(efx);
1657 	efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.pf);
1658 	efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.wire);
1659 	efx_tc_deconfigure_fallback_acts(efx, &efx->tc->facts.pf);
1660 	efx_tc_deconfigure_fallback_acts(efx, &efx->tc->facts.reps);
1661 	efx->tc->up = false;
1662 }
1663 
1664 /* At teardown time, all TC filter rules (and thus all resources they created)
1665  * should already have been removed.  If we find any in our hashtables, make a
1666  * cursory attempt to clean up the software side.
1667  */
1668 static void efx_tc_encap_match_free(void *ptr, void *__unused)
1669 {
1670 	struct efx_tc_encap_match *encap = ptr;
1671 
1672 	WARN_ON(refcount_read(&encap->ref));
1673 	kfree(encap);
1674 }
1675 
1676 static void efx_tc_flow_free(void *ptr, void *arg)
1677 {
1678 	struct efx_tc_flow_rule *rule = ptr;
1679 	struct efx_nic *efx = arg;
1680 
1681 	netif_err(efx, drv, efx->net_dev,
1682 		  "tc rule %lx still present at teardown, removing\n",
1683 		  rule->cookie);
1684 
1685 	/* Also releases entries in subsidiary tables */
1686 	efx_tc_delete_rule(efx, rule);
1687 
1688 	kfree(rule);
1689 }
1690 
1691 int efx_init_struct_tc(struct efx_nic *efx)
1692 {
1693 	int rc;
1694 
1695 	if (efx->type->is_vf)
1696 		return 0;
1697 
1698 	efx->tc = kzalloc(sizeof(*efx->tc), GFP_KERNEL);
1699 	if (!efx->tc)
1700 		return -ENOMEM;
1701 	efx->tc->caps = kzalloc(sizeof(struct mae_caps), GFP_KERNEL);
1702 	if (!efx->tc->caps) {
1703 		rc = -ENOMEM;
1704 		goto fail_alloc_caps;
1705 	}
1706 	INIT_LIST_HEAD(&efx->tc->block_list);
1707 
1708 	mutex_init(&efx->tc->mutex);
1709 	init_waitqueue_head(&efx->tc->flush_wq);
1710 	rc = efx_tc_init_encap_actions(efx);
1711 	if (rc < 0)
1712 		goto fail_encap_actions;
1713 	rc = efx_tc_init_counters(efx);
1714 	if (rc < 0)
1715 		goto fail_counters;
1716 	rc = rhashtable_init(&efx->tc->encap_match_ht, &efx_tc_encap_match_ht_params);
1717 	if (rc < 0)
1718 		goto fail_encap_match_ht;
1719 	rc = rhashtable_init(&efx->tc->match_action_ht, &efx_tc_match_action_ht_params);
1720 	if (rc < 0)
1721 		goto fail_match_action_ht;
1722 	efx->tc->reps_filter_uc = -1;
1723 	efx->tc->reps_filter_mc = -1;
1724 	INIT_LIST_HEAD(&efx->tc->dflt.pf.acts.list);
1725 	efx->tc->dflt.pf.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
1726 	INIT_LIST_HEAD(&efx->tc->dflt.wire.acts.list);
1727 	efx->tc->dflt.wire.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
1728 	INIT_LIST_HEAD(&efx->tc->facts.pf.list);
1729 	efx->tc->facts.pf.fw_id = MC_CMD_MAE_ACTION_SET_ALLOC_OUT_ACTION_SET_ID_NULL;
1730 	INIT_LIST_HEAD(&efx->tc->facts.reps.list);
1731 	efx->tc->facts.reps.fw_id = MC_CMD_MAE_ACTION_SET_ALLOC_OUT_ACTION_SET_ID_NULL;
1732 	efx->extra_channel_type[EFX_EXTRA_CHANNEL_TC] = &efx_tc_channel_type;
1733 	return 0;
1734 fail_match_action_ht:
1735 	rhashtable_destroy(&efx->tc->encap_match_ht);
1736 fail_encap_match_ht:
1737 	efx_tc_destroy_counters(efx);
1738 fail_counters:
1739 	efx_tc_destroy_encap_actions(efx);
1740 fail_encap_actions:
1741 	mutex_destroy(&efx->tc->mutex);
1742 	kfree(efx->tc->caps);
1743 fail_alloc_caps:
1744 	kfree(efx->tc);
1745 	efx->tc = NULL;
1746 	return rc;
1747 }
1748 
1749 void efx_fini_struct_tc(struct efx_nic *efx)
1750 {
1751 	if (!efx->tc)
1752 		return;
1753 
1754 	mutex_lock(&efx->tc->mutex);
1755 	EFX_WARN_ON_PARANOID(efx->tc->dflt.pf.fw_id !=
1756 			     MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL);
1757 	EFX_WARN_ON_PARANOID(efx->tc->dflt.wire.fw_id !=
1758 			     MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL);
1759 	EFX_WARN_ON_PARANOID(efx->tc->facts.pf.fw_id !=
1760 			     MC_CMD_MAE_ACTION_SET_LIST_ALLOC_OUT_ACTION_SET_LIST_ID_NULL);
1761 	EFX_WARN_ON_PARANOID(efx->tc->facts.reps.fw_id !=
1762 			     MC_CMD_MAE_ACTION_SET_LIST_ALLOC_OUT_ACTION_SET_LIST_ID_NULL);
1763 	rhashtable_free_and_destroy(&efx->tc->match_action_ht, efx_tc_flow_free,
1764 				    efx);
1765 	rhashtable_free_and_destroy(&efx->tc->encap_match_ht,
1766 				    efx_tc_encap_match_free, NULL);
1767 	efx_tc_fini_counters(efx);
1768 	efx_tc_fini_encap_actions(efx);
1769 	mutex_unlock(&efx->tc->mutex);
1770 	mutex_destroy(&efx->tc->mutex);
1771 	kfree(efx->tc->caps);
1772 	kfree(efx->tc);
1773 	efx->tc = NULL;
1774 }
1775