xref: /openbmc/linux/drivers/net/ethernet/broadcom/bnxt/bnxt_tc.c (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2017 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/netdevice.h>
11 #include <linux/inetdevice.h>
12 #include <linux/if_vlan.h>
13 #include <net/flow_dissector.h>
14 #include <net/pkt_cls.h>
15 #include <net/tc_act/tc_gact.h>
16 #include <net/tc_act/tc_skbedit.h>
17 #include <net/tc_act/tc_mirred.h>
18 #include <net/tc_act/tc_vlan.h>
19 #include <net/tc_act/tc_tunnel_key.h>
20 
21 #include "bnxt_hsi.h"
22 #include "bnxt.h"
23 #include "bnxt_sriov.h"
24 #include "bnxt_tc.h"
25 #include "bnxt_vfr.h"
26 
27 #define BNXT_FID_INVALID			0xffff
28 #define VLAN_TCI(vid, prio)	((vid) | ((prio) << VLAN_PRIO_SHIFT))
29 
30 /* Return the dst fid of the func for flow forwarding
31  * For PFs: src_fid is the fid of the PF
32  * For VF-reps: src_fid the fid of the VF
33  */
34 static u16 bnxt_flow_get_dst_fid(struct bnxt *pf_bp, struct net_device *dev)
35 {
36 	struct bnxt *bp;
37 
38 	/* check if dev belongs to the same switch */
39 	if (!switchdev_port_same_parent_id(pf_bp->dev, dev)) {
40 		netdev_info(pf_bp->dev, "dev(ifindex=%d) not on same switch",
41 			    dev->ifindex);
42 		return BNXT_FID_INVALID;
43 	}
44 
45 	/* Is dev a VF-rep? */
46 	if (bnxt_dev_is_vf_rep(dev))
47 		return bnxt_vf_rep_get_fid(dev);
48 
49 	bp = netdev_priv(dev);
50 	return bp->pf.fw_fid;
51 }
52 
53 static int bnxt_tc_parse_redir(struct bnxt *bp,
54 			       struct bnxt_tc_actions *actions,
55 			       const struct tc_action *tc_act)
56 {
57 	struct net_device *dev = tcf_mirred_dev(tc_act);
58 
59 	if (!dev) {
60 		netdev_info(bp->dev, "no dev in mirred action");
61 		return -EINVAL;
62 	}
63 
64 	actions->flags |= BNXT_TC_ACTION_FLAG_FWD;
65 	actions->dst_dev = dev;
66 	return 0;
67 }
68 
69 static void bnxt_tc_parse_vlan(struct bnxt *bp,
70 			       struct bnxt_tc_actions *actions,
71 			       const struct tc_action *tc_act)
72 {
73 	if (tcf_vlan_action(tc_act) == TCA_VLAN_ACT_POP) {
74 		actions->flags |= BNXT_TC_ACTION_FLAG_POP_VLAN;
75 	} else if (tcf_vlan_action(tc_act) == TCA_VLAN_ACT_PUSH) {
76 		actions->flags |= BNXT_TC_ACTION_FLAG_PUSH_VLAN;
77 		actions->push_vlan_tci = htons(tcf_vlan_push_vid(tc_act));
78 		actions->push_vlan_tpid = tcf_vlan_push_proto(tc_act);
79 	}
80 }
81 
82 static int bnxt_tc_parse_tunnel_set(struct bnxt *bp,
83 				    struct bnxt_tc_actions *actions,
84 				    const struct tc_action *tc_act)
85 {
86 	struct ip_tunnel_info *tun_info = tcf_tunnel_info(tc_act);
87 	struct ip_tunnel_key *tun_key = &tun_info->key;
88 
89 	if (ip_tunnel_info_af(tun_info) != AF_INET) {
90 		netdev_info(bp->dev, "only IPv4 tunnel-encap is supported");
91 		return -EOPNOTSUPP;
92 	}
93 
94 	actions->tun_encap_key = *tun_key;
95 	actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP;
96 	return 0;
97 }
98 
99 static int bnxt_tc_parse_actions(struct bnxt *bp,
100 				 struct bnxt_tc_actions *actions,
101 				 struct tcf_exts *tc_exts)
102 {
103 	const struct tc_action *tc_act;
104 	LIST_HEAD(tc_actions);
105 	int rc;
106 
107 	if (!tcf_exts_has_actions(tc_exts)) {
108 		netdev_info(bp->dev, "no actions");
109 		return -EINVAL;
110 	}
111 
112 	tcf_exts_to_list(tc_exts, &tc_actions);
113 	list_for_each_entry(tc_act, &tc_actions, list) {
114 		/* Drop action */
115 		if (is_tcf_gact_shot(tc_act)) {
116 			actions->flags |= BNXT_TC_ACTION_FLAG_DROP;
117 			return 0; /* don't bother with other actions */
118 		}
119 
120 		/* Redirect action */
121 		if (is_tcf_mirred_egress_redirect(tc_act)) {
122 			rc = bnxt_tc_parse_redir(bp, actions, tc_act);
123 			if (rc)
124 				return rc;
125 			continue;
126 		}
127 
128 		/* Push/pop VLAN */
129 		if (is_tcf_vlan(tc_act)) {
130 			bnxt_tc_parse_vlan(bp, actions, tc_act);
131 			continue;
132 		}
133 
134 		/* Tunnel encap */
135 		if (is_tcf_tunnel_set(tc_act)) {
136 			rc = bnxt_tc_parse_tunnel_set(bp, actions, tc_act);
137 			if (rc)
138 				return rc;
139 			continue;
140 		}
141 
142 		/* Tunnel decap */
143 		if (is_tcf_tunnel_release(tc_act)) {
144 			actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_DECAP;
145 			continue;
146 		}
147 	}
148 
149 	if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
150 		if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
151 			/* dst_fid is PF's fid */
152 			actions->dst_fid = bp->pf.fw_fid;
153 		} else {
154 			/* find the FID from dst_dev */
155 			actions->dst_fid =
156 				bnxt_flow_get_dst_fid(bp, actions->dst_dev);
157 			if (actions->dst_fid == BNXT_FID_INVALID)
158 				return -EINVAL;
159 		}
160 	}
161 
162 	return 0;
163 }
164 
165 #define GET_KEY(flow_cmd, key_type)					\
166 		skb_flow_dissector_target((flow_cmd)->dissector, key_type,\
167 					  (flow_cmd)->key)
168 #define GET_MASK(flow_cmd, key_type)					\
169 		skb_flow_dissector_target((flow_cmd)->dissector, key_type,\
170 					  (flow_cmd)->mask)
171 
172 static int bnxt_tc_parse_flow(struct bnxt *bp,
173 			      struct tc_cls_flower_offload *tc_flow_cmd,
174 			      struct bnxt_tc_flow *flow)
175 {
176 	struct flow_dissector *dissector = tc_flow_cmd->dissector;
177 	u16 addr_type = 0;
178 
179 	/* KEY_CONTROL and KEY_BASIC are needed for forming a meaningful key */
180 	if ((dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_CONTROL)) == 0 ||
181 	    (dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_BASIC)) == 0) {
182 		netdev_info(bp->dev, "cannot form TC key: used_keys = 0x%x",
183 			    dissector->used_keys);
184 		return -EOPNOTSUPP;
185 	}
186 
187 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
188 		struct flow_dissector_key_control *key =
189 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_CONTROL);
190 
191 		addr_type = key->addr_type;
192 	}
193 
194 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_BASIC)) {
195 		struct flow_dissector_key_basic *key =
196 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_BASIC);
197 		struct flow_dissector_key_basic *mask =
198 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_BASIC);
199 
200 		flow->l2_key.ether_type = key->n_proto;
201 		flow->l2_mask.ether_type = mask->n_proto;
202 
203 		if (key->n_proto == htons(ETH_P_IP) ||
204 		    key->n_proto == htons(ETH_P_IPV6)) {
205 			flow->l4_key.ip_proto = key->ip_proto;
206 			flow->l4_mask.ip_proto = mask->ip_proto;
207 		}
208 	}
209 
210 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
211 		struct flow_dissector_key_eth_addrs *key =
212 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ETH_ADDRS);
213 		struct flow_dissector_key_eth_addrs *mask =
214 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ETH_ADDRS);
215 
216 		flow->flags |= BNXT_TC_FLOW_FLAGS_ETH_ADDRS;
217 		ether_addr_copy(flow->l2_key.dmac, key->dst);
218 		ether_addr_copy(flow->l2_mask.dmac, mask->dst);
219 		ether_addr_copy(flow->l2_key.smac, key->src);
220 		ether_addr_copy(flow->l2_mask.smac, mask->src);
221 	}
222 
223 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_VLAN)) {
224 		struct flow_dissector_key_vlan *key =
225 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_VLAN);
226 		struct flow_dissector_key_vlan *mask =
227 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_VLAN);
228 
229 		flow->l2_key.inner_vlan_tci =
230 		   cpu_to_be16(VLAN_TCI(key->vlan_id, key->vlan_priority));
231 		flow->l2_mask.inner_vlan_tci =
232 		   cpu_to_be16((VLAN_TCI(mask->vlan_id, mask->vlan_priority)));
233 		flow->l2_key.inner_vlan_tpid = htons(ETH_P_8021Q);
234 		flow->l2_mask.inner_vlan_tpid = htons(0xffff);
235 		flow->l2_key.num_vlans = 1;
236 	}
237 
238 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
239 		struct flow_dissector_key_ipv4_addrs *key =
240 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
241 		struct flow_dissector_key_ipv4_addrs *mask =
242 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
243 
244 		flow->flags |= BNXT_TC_FLOW_FLAGS_IPV4_ADDRS;
245 		flow->l3_key.ipv4.daddr.s_addr = key->dst;
246 		flow->l3_mask.ipv4.daddr.s_addr = mask->dst;
247 		flow->l3_key.ipv4.saddr.s_addr = key->src;
248 		flow->l3_mask.ipv4.saddr.s_addr = mask->src;
249 	} else if (dissector_uses_key(dissector,
250 				      FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
251 		struct flow_dissector_key_ipv6_addrs *key =
252 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
253 		struct flow_dissector_key_ipv6_addrs *mask =
254 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
255 
256 		flow->flags |= BNXT_TC_FLOW_FLAGS_IPV6_ADDRS;
257 		flow->l3_key.ipv6.daddr = key->dst;
258 		flow->l3_mask.ipv6.daddr = mask->dst;
259 		flow->l3_key.ipv6.saddr = key->src;
260 		flow->l3_mask.ipv6.saddr = mask->src;
261 	}
262 
263 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_PORTS)) {
264 		struct flow_dissector_key_ports *key =
265 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_PORTS);
266 		struct flow_dissector_key_ports *mask =
267 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_PORTS);
268 
269 		flow->flags |= BNXT_TC_FLOW_FLAGS_PORTS;
270 		flow->l4_key.ports.dport = key->dst;
271 		flow->l4_mask.ports.dport = mask->dst;
272 		flow->l4_key.ports.sport = key->src;
273 		flow->l4_mask.ports.sport = mask->src;
274 	}
275 
276 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ICMP)) {
277 		struct flow_dissector_key_icmp *key =
278 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ICMP);
279 		struct flow_dissector_key_icmp *mask =
280 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ICMP);
281 
282 		flow->flags |= BNXT_TC_FLOW_FLAGS_ICMP;
283 		flow->l4_key.icmp.type = key->type;
284 		flow->l4_key.icmp.code = key->code;
285 		flow->l4_mask.icmp.type = mask->type;
286 		flow->l4_mask.icmp.code = mask->code;
287 	}
288 
289 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
290 		struct flow_dissector_key_control *key =
291 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_CONTROL);
292 
293 		addr_type = key->addr_type;
294 	}
295 
296 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
297 		struct flow_dissector_key_ipv4_addrs *key =
298 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS);
299 		struct flow_dissector_key_ipv4_addrs *mask =
300 				GET_MASK(tc_flow_cmd,
301 					 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS);
302 
303 		flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS;
304 		flow->tun_key.u.ipv4.dst = key->dst;
305 		flow->tun_mask.u.ipv4.dst = mask->dst;
306 		flow->tun_key.u.ipv4.src = key->src;
307 		flow->tun_mask.u.ipv4.src = mask->src;
308 	} else if (dissector_uses_key(dissector,
309 				      FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
310 		return -EOPNOTSUPP;
311 	}
312 
313 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
314 		struct flow_dissector_key_keyid *key =
315 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_KEYID);
316 		struct flow_dissector_key_keyid *mask =
317 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_KEYID);
318 
319 		flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ID;
320 		flow->tun_key.tun_id = key32_to_tunnel_id(key->keyid);
321 		flow->tun_mask.tun_id = key32_to_tunnel_id(mask->keyid);
322 	}
323 
324 	if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
325 		struct flow_dissector_key_ports *key =
326 			GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_PORTS);
327 		struct flow_dissector_key_ports *mask =
328 			GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_PORTS);
329 
330 		flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_PORTS;
331 		flow->tun_key.tp_dst = key->dst;
332 		flow->tun_mask.tp_dst = mask->dst;
333 		flow->tun_key.tp_src = key->src;
334 		flow->tun_mask.tp_src = mask->src;
335 	}
336 
337 	return bnxt_tc_parse_actions(bp, &flow->actions, tc_flow_cmd->exts);
338 }
339 
340 static int bnxt_hwrm_cfa_flow_free(struct bnxt *bp, __le16 flow_handle)
341 {
342 	struct hwrm_cfa_flow_free_input req = { 0 };
343 	int rc;
344 
345 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_FREE, -1, -1);
346 	req.flow_handle = flow_handle;
347 
348 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
349 	if (rc)
350 		netdev_info(bp->dev, "Error: %s: flow_handle=0x%x rc=%d",
351 			    __func__, flow_handle, rc);
352 
353 	if (rc)
354 		rc = -EIO;
355 	return rc;
356 }
357 
358 static int ipv6_mask_len(struct in6_addr *mask)
359 {
360 	int mask_len = 0, i;
361 
362 	for (i = 0; i < 4; i++)
363 		mask_len += inet_mask_len(mask->s6_addr32[i]);
364 
365 	return mask_len;
366 }
367 
368 static bool is_wildcard(void *mask, int len)
369 {
370 	const u8 *p = mask;
371 	int i;
372 
373 	for (i = 0; i < len; i++) {
374 		if (p[i] != 0)
375 			return false;
376 	}
377 	return true;
378 }
379 
380 static int bnxt_hwrm_cfa_flow_alloc(struct bnxt *bp, struct bnxt_tc_flow *flow,
381 				    __le16 ref_flow_handle,
382 				    __le32 tunnel_handle, __le16 *flow_handle)
383 {
384 	struct hwrm_cfa_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr;
385 	struct bnxt_tc_actions *actions = &flow->actions;
386 	struct bnxt_tc_l3_key *l3_mask = &flow->l3_mask;
387 	struct bnxt_tc_l3_key *l3_key = &flow->l3_key;
388 	struct hwrm_cfa_flow_alloc_input req = { 0 };
389 	u16 flow_flags = 0, action_flags = 0;
390 	int rc;
391 
392 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_ALLOC, -1, -1);
393 
394 	req.src_fid = cpu_to_le16(flow->src_fid);
395 	req.ref_flow_handle = ref_flow_handle;
396 
397 	if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP ||
398 	    actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
399 		req.tunnel_handle = tunnel_handle;
400 		flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_TUNNEL;
401 		action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_TUNNEL;
402 	}
403 
404 	req.ethertype = flow->l2_key.ether_type;
405 	req.ip_proto = flow->l4_key.ip_proto;
406 
407 	if (flow->flags & BNXT_TC_FLOW_FLAGS_ETH_ADDRS) {
408 		memcpy(req.dmac, flow->l2_key.dmac, ETH_ALEN);
409 		memcpy(req.smac, flow->l2_key.smac, ETH_ALEN);
410 	}
411 
412 	if (flow->l2_key.num_vlans > 0) {
413 		flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_NUM_VLAN_ONE;
414 		/* FW expects the inner_vlan_tci value to be set
415 		 * in outer_vlan_tci when num_vlans is 1 (which is
416 		 * always the case in TC.)
417 		 */
418 		req.outer_vlan_tci = flow->l2_key.inner_vlan_tci;
419 	}
420 
421 	/* If all IP and L4 fields are wildcarded then this is an L2 flow */
422 	if (is_wildcard(l3_mask, sizeof(*l3_mask)) &&
423 	    is_wildcard(&flow->l4_mask, sizeof(flow->l4_mask))) {
424 		flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_L2;
425 	} else {
426 		flow_flags |= flow->l2_key.ether_type == htons(ETH_P_IP) ?
427 				CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV4 :
428 				CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV6;
429 
430 		if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV4_ADDRS) {
431 			req.ip_dst[0] = l3_key->ipv4.daddr.s_addr;
432 			req.ip_dst_mask_len =
433 				inet_mask_len(l3_mask->ipv4.daddr.s_addr);
434 			req.ip_src[0] = l3_key->ipv4.saddr.s_addr;
435 			req.ip_src_mask_len =
436 				inet_mask_len(l3_mask->ipv4.saddr.s_addr);
437 		} else if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV6_ADDRS) {
438 			memcpy(req.ip_dst, l3_key->ipv6.daddr.s6_addr32,
439 			       sizeof(req.ip_dst));
440 			req.ip_dst_mask_len =
441 					ipv6_mask_len(&l3_mask->ipv6.daddr);
442 			memcpy(req.ip_src, l3_key->ipv6.saddr.s6_addr32,
443 			       sizeof(req.ip_src));
444 			req.ip_src_mask_len =
445 					ipv6_mask_len(&l3_mask->ipv6.saddr);
446 		}
447 	}
448 
449 	if (flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) {
450 		req.l4_src_port = flow->l4_key.ports.sport;
451 		req.l4_src_port_mask = flow->l4_mask.ports.sport;
452 		req.l4_dst_port = flow->l4_key.ports.dport;
453 		req.l4_dst_port_mask = flow->l4_mask.ports.dport;
454 	} else if (flow->flags & BNXT_TC_FLOW_FLAGS_ICMP) {
455 		/* l4 ports serve as type/code when ip_proto is ICMP */
456 		req.l4_src_port = htons(flow->l4_key.icmp.type);
457 		req.l4_src_port_mask = htons(flow->l4_mask.icmp.type);
458 		req.l4_dst_port = htons(flow->l4_key.icmp.code);
459 		req.l4_dst_port_mask = htons(flow->l4_mask.icmp.code);
460 	}
461 	req.flags = cpu_to_le16(flow_flags);
462 
463 	if (actions->flags & BNXT_TC_ACTION_FLAG_DROP) {
464 		action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_DROP;
465 	} else {
466 		if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
467 			action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_FWD;
468 			req.dst_fid = cpu_to_le16(actions->dst_fid);
469 		}
470 		if (actions->flags & BNXT_TC_ACTION_FLAG_PUSH_VLAN) {
471 			action_flags |=
472 			    CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
473 			req.l2_rewrite_vlan_tpid = actions->push_vlan_tpid;
474 			req.l2_rewrite_vlan_tci = actions->push_vlan_tci;
475 			memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN);
476 			memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN);
477 		}
478 		if (actions->flags & BNXT_TC_ACTION_FLAG_POP_VLAN) {
479 			action_flags |=
480 			    CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
481 			/* Rewrite config with tpid = 0 implies vlan pop */
482 			req.l2_rewrite_vlan_tpid = 0;
483 			memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN);
484 			memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN);
485 		}
486 	}
487 	req.action_flags = cpu_to_le16(action_flags);
488 
489 	mutex_lock(&bp->hwrm_cmd_lock);
490 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
491 	if (!rc)
492 		*flow_handle = resp->flow_handle;
493 	mutex_unlock(&bp->hwrm_cmd_lock);
494 
495 	if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR)
496 		rc = -ENOSPC;
497 	else if (rc)
498 		rc = -EIO;
499 	return rc;
500 }
501 
502 static int hwrm_cfa_decap_filter_alloc(struct bnxt *bp,
503 				       struct bnxt_tc_flow *flow,
504 				       struct bnxt_tc_l2_key *l2_info,
505 				       __le32 ref_decap_handle,
506 				       __le32 *decap_filter_handle)
507 {
508 	struct hwrm_cfa_decap_filter_alloc_output *resp =
509 						bp->hwrm_cmd_resp_addr;
510 	struct hwrm_cfa_decap_filter_alloc_input req = { 0 };
511 	struct ip_tunnel_key *tun_key = &flow->tun_key;
512 	u32 enables = 0;
513 	int rc;
514 
515 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_ALLOC, -1, -1);
516 
517 	req.flags = cpu_to_le32(CFA_DECAP_FILTER_ALLOC_REQ_FLAGS_OVS_TUNNEL);
518 	enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE |
519 		   CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL;
520 	req.tunnel_type = CFA_DECAP_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
521 	req.ip_protocol = CFA_DECAP_FILTER_ALLOC_REQ_IP_PROTOCOL_UDP;
522 
523 	if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ID) {
524 		enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_ID;
525 		/* tunnel_id is wrongly defined in hsi defn. as __le32 */
526 		req.tunnel_id = tunnel_id_to_key32(tun_key->tun_id);
527 	}
528 
529 	if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS) {
530 		enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_MACADDR;
531 		ether_addr_copy(req.dst_macaddr, l2_info->dmac);
532 	}
533 	if (l2_info->num_vlans) {
534 		enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_T_IVLAN_VID;
535 		req.t_ivlan_vid = l2_info->inner_vlan_tci;
536 	}
537 
538 	enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE;
539 	req.ethertype = htons(ETH_P_IP);
540 
541 	if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS) {
542 		enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |
543 			   CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |
544 			   CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE;
545 		req.ip_addr_type = CFA_DECAP_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
546 		req.dst_ipaddr[0] = tun_key->u.ipv4.dst;
547 		req.src_ipaddr[0] = tun_key->u.ipv4.src;
548 	}
549 
550 	if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_PORTS) {
551 		enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_PORT;
552 		req.dst_port = tun_key->tp_dst;
553 	}
554 
555 	/* Eventhough the decap_handle returned by hwrm_cfa_decap_filter_alloc
556 	 * is defined as __le32, l2_ctxt_ref_id is defined in HSI as __le16.
557 	 */
558 	req.l2_ctxt_ref_id = (__force __le16)ref_decap_handle;
559 	req.enables = cpu_to_le32(enables);
560 
561 	mutex_lock(&bp->hwrm_cmd_lock);
562 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
563 	if (!rc)
564 		*decap_filter_handle = resp->decap_filter_id;
565 	else
566 		netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
567 	mutex_unlock(&bp->hwrm_cmd_lock);
568 
569 	if (rc)
570 		rc = -EIO;
571 	return rc;
572 }
573 
574 static int hwrm_cfa_decap_filter_free(struct bnxt *bp,
575 				      __le32 decap_filter_handle)
576 {
577 	struct hwrm_cfa_decap_filter_free_input req = { 0 };
578 	int rc;
579 
580 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_FREE, -1, -1);
581 	req.decap_filter_id = decap_filter_handle;
582 
583 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
584 	if (rc)
585 		netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
586 
587 	if (rc)
588 		rc = -EIO;
589 	return rc;
590 }
591 
592 static int hwrm_cfa_encap_record_alloc(struct bnxt *bp,
593 				       struct ip_tunnel_key *encap_key,
594 				       struct bnxt_tc_l2_key *l2_info,
595 				       __le32 *encap_record_handle)
596 {
597 	struct hwrm_cfa_encap_record_alloc_output *resp =
598 						bp->hwrm_cmd_resp_addr;
599 	struct hwrm_cfa_encap_record_alloc_input req = { 0 };
600 	struct hwrm_cfa_encap_data_vxlan *encap =
601 			(struct hwrm_cfa_encap_data_vxlan *)&req.encap_data;
602 	struct hwrm_vxlan_ipv4_hdr *encap_ipv4 =
603 				(struct hwrm_vxlan_ipv4_hdr *)encap->l3;
604 	int rc;
605 
606 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_ALLOC, -1, -1);
607 
608 	req.encap_type = CFA_ENCAP_RECORD_ALLOC_REQ_ENCAP_TYPE_VXLAN;
609 
610 	ether_addr_copy(encap->dst_mac_addr, l2_info->dmac);
611 	ether_addr_copy(encap->src_mac_addr, l2_info->smac);
612 	if (l2_info->num_vlans) {
613 		encap->num_vlan_tags = l2_info->num_vlans;
614 		encap->ovlan_tci = l2_info->inner_vlan_tci;
615 		encap->ovlan_tpid = l2_info->inner_vlan_tpid;
616 	}
617 
618 	encap_ipv4->ver_hlen = 4 << VXLAN_IPV4_HDR_VER_HLEN_VERSION_SFT;
619 	encap_ipv4->ver_hlen |= 5 << VXLAN_IPV4_HDR_VER_HLEN_HEADER_LENGTH_SFT;
620 	encap_ipv4->ttl = encap_key->ttl;
621 
622 	encap_ipv4->dest_ip_addr = encap_key->u.ipv4.dst;
623 	encap_ipv4->src_ip_addr = encap_key->u.ipv4.src;
624 	encap_ipv4->protocol = IPPROTO_UDP;
625 
626 	encap->dst_port = encap_key->tp_dst;
627 	encap->vni = tunnel_id_to_key32(encap_key->tun_id);
628 
629 	mutex_lock(&bp->hwrm_cmd_lock);
630 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
631 	if (!rc)
632 		*encap_record_handle = resp->encap_record_id;
633 	else
634 		netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
635 	mutex_unlock(&bp->hwrm_cmd_lock);
636 
637 	if (rc)
638 		rc = -EIO;
639 	return rc;
640 }
641 
642 static int hwrm_cfa_encap_record_free(struct bnxt *bp,
643 				      __le32 encap_record_handle)
644 {
645 	struct hwrm_cfa_encap_record_free_input req = { 0 };
646 	int rc;
647 
648 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_FREE, -1, -1);
649 	req.encap_record_id = encap_record_handle;
650 
651 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
652 	if (rc)
653 		netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
654 
655 	if (rc)
656 		rc = -EIO;
657 	return rc;
658 }
659 
660 static int bnxt_tc_put_l2_node(struct bnxt *bp,
661 			       struct bnxt_tc_flow_node *flow_node)
662 {
663 	struct bnxt_tc_l2_node *l2_node = flow_node->l2_node;
664 	struct bnxt_tc_info *tc_info = bp->tc_info;
665 	int rc;
666 
667 	/* remove flow_node from the L2 shared flow list */
668 	list_del(&flow_node->l2_list_node);
669 	if (--l2_node->refcount == 0) {
670 		rc =  rhashtable_remove_fast(&tc_info->l2_table, &l2_node->node,
671 					     tc_info->l2_ht_params);
672 		if (rc)
673 			netdev_err(bp->dev,
674 				   "Error: %s: rhashtable_remove_fast: %d",
675 				   __func__, rc);
676 		kfree_rcu(l2_node, rcu);
677 	}
678 	return 0;
679 }
680 
681 static struct bnxt_tc_l2_node *
682 bnxt_tc_get_l2_node(struct bnxt *bp, struct rhashtable *l2_table,
683 		    struct rhashtable_params ht_params,
684 		    struct bnxt_tc_l2_key *l2_key)
685 {
686 	struct bnxt_tc_l2_node *l2_node;
687 	int rc;
688 
689 	l2_node = rhashtable_lookup_fast(l2_table, l2_key, ht_params);
690 	if (!l2_node) {
691 		l2_node = kzalloc(sizeof(*l2_node), GFP_KERNEL);
692 		if (!l2_node) {
693 			rc = -ENOMEM;
694 			return NULL;
695 		}
696 
697 		l2_node->key = *l2_key;
698 		rc = rhashtable_insert_fast(l2_table, &l2_node->node,
699 					    ht_params);
700 		if (rc) {
701 			kfree_rcu(l2_node, rcu);
702 			netdev_err(bp->dev,
703 				   "Error: %s: rhashtable_insert_fast: %d",
704 				   __func__, rc);
705 			return NULL;
706 		}
707 		INIT_LIST_HEAD(&l2_node->common_l2_flows);
708 	}
709 	return l2_node;
710 }
711 
712 /* Get the ref_flow_handle for a flow by checking if there are any other
713  * flows that share the same L2 key as this flow.
714  */
715 static int
716 bnxt_tc_get_ref_flow_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
717 			    struct bnxt_tc_flow_node *flow_node,
718 			    __le16 *ref_flow_handle)
719 {
720 	struct bnxt_tc_info *tc_info = bp->tc_info;
721 	struct bnxt_tc_flow_node *ref_flow_node;
722 	struct bnxt_tc_l2_node *l2_node;
723 
724 	l2_node = bnxt_tc_get_l2_node(bp, &tc_info->l2_table,
725 				      tc_info->l2_ht_params,
726 				      &flow->l2_key);
727 	if (!l2_node)
728 		return -1;
729 
730 	/* If any other flow is using this l2_node, use it's flow_handle
731 	 * as the ref_flow_handle
732 	 */
733 	if (l2_node->refcount > 0) {
734 		ref_flow_node = list_first_entry(&l2_node->common_l2_flows,
735 						 struct bnxt_tc_flow_node,
736 						 l2_list_node);
737 		*ref_flow_handle = ref_flow_node->flow_handle;
738 	} else {
739 		*ref_flow_handle = cpu_to_le16(0xffff);
740 	}
741 
742 	/* Insert the l2_node into the flow_node so that subsequent flows
743 	 * with a matching l2 key can use the flow_handle of this flow
744 	 * as their ref_flow_handle
745 	 */
746 	flow_node->l2_node = l2_node;
747 	list_add(&flow_node->l2_list_node, &l2_node->common_l2_flows);
748 	l2_node->refcount++;
749 	return 0;
750 }
751 
752 /* After the flow parsing is done, this routine is used for checking
753  * if there are any aspects of the flow that prevent it from being
754  * offloaded.
755  */
756 static bool bnxt_tc_can_offload(struct bnxt *bp, struct bnxt_tc_flow *flow)
757 {
758 	/* If L4 ports are specified then ip_proto must be TCP or UDP */
759 	if ((flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) &&
760 	    (flow->l4_key.ip_proto != IPPROTO_TCP &&
761 	     flow->l4_key.ip_proto != IPPROTO_UDP)) {
762 		netdev_info(bp->dev, "Cannot offload non-TCP/UDP (%d) ports",
763 			    flow->l4_key.ip_proto);
764 		return false;
765 	}
766 
767 	return true;
768 }
769 
770 /* Returns the final refcount of the node on success
771  * or a -ve error code on failure
772  */
773 static int bnxt_tc_put_tunnel_node(struct bnxt *bp,
774 				   struct rhashtable *tunnel_table,
775 				   struct rhashtable_params *ht_params,
776 				   struct bnxt_tc_tunnel_node *tunnel_node)
777 {
778 	int rc;
779 
780 	if (--tunnel_node->refcount == 0) {
781 		rc =  rhashtable_remove_fast(tunnel_table, &tunnel_node->node,
782 					     *ht_params);
783 		if (rc) {
784 			netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
785 			rc = -1;
786 		}
787 		kfree_rcu(tunnel_node, rcu);
788 		return rc;
789 	} else {
790 		return tunnel_node->refcount;
791 	}
792 }
793 
794 /* Get (or add) either encap or decap tunnel node from/to the supplied
795  * hash table.
796  */
797 static struct bnxt_tc_tunnel_node *
798 bnxt_tc_get_tunnel_node(struct bnxt *bp, struct rhashtable *tunnel_table,
799 			struct rhashtable_params *ht_params,
800 			struct ip_tunnel_key *tun_key)
801 {
802 	struct bnxt_tc_tunnel_node *tunnel_node;
803 	int rc;
804 
805 	tunnel_node = rhashtable_lookup_fast(tunnel_table, tun_key, *ht_params);
806 	if (!tunnel_node) {
807 		tunnel_node = kzalloc(sizeof(*tunnel_node), GFP_KERNEL);
808 		if (!tunnel_node) {
809 			rc = -ENOMEM;
810 			goto err;
811 		}
812 
813 		tunnel_node->key = *tun_key;
814 		tunnel_node->tunnel_handle = INVALID_TUNNEL_HANDLE;
815 		rc = rhashtable_insert_fast(tunnel_table, &tunnel_node->node,
816 					    *ht_params);
817 		if (rc) {
818 			kfree_rcu(tunnel_node, rcu);
819 			goto err;
820 		}
821 	}
822 	tunnel_node->refcount++;
823 	return tunnel_node;
824 err:
825 	netdev_info(bp->dev, "error rc=%d", rc);
826 	return NULL;
827 }
828 
829 static int bnxt_tc_get_ref_decap_handle(struct bnxt *bp,
830 					struct bnxt_tc_flow *flow,
831 					struct bnxt_tc_l2_key *l2_key,
832 					struct bnxt_tc_flow_node *flow_node,
833 					__le32 *ref_decap_handle)
834 {
835 	struct bnxt_tc_info *tc_info = bp->tc_info;
836 	struct bnxt_tc_flow_node *ref_flow_node;
837 	struct bnxt_tc_l2_node *decap_l2_node;
838 
839 	decap_l2_node = bnxt_tc_get_l2_node(bp, &tc_info->decap_l2_table,
840 					    tc_info->decap_l2_ht_params,
841 					    l2_key);
842 	if (!decap_l2_node)
843 		return -1;
844 
845 	/* If any other flow is using this decap_l2_node, use it's decap_handle
846 	 * as the ref_decap_handle
847 	 */
848 	if (decap_l2_node->refcount > 0) {
849 		ref_flow_node =
850 			list_first_entry(&decap_l2_node->common_l2_flows,
851 					 struct bnxt_tc_flow_node,
852 					 decap_l2_list_node);
853 		*ref_decap_handle = ref_flow_node->decap_node->tunnel_handle;
854 	} else {
855 		*ref_decap_handle = INVALID_TUNNEL_HANDLE;
856 	}
857 
858 	/* Insert the l2_node into the flow_node so that subsequent flows
859 	 * with a matching decap l2 key can use the decap_filter_handle of
860 	 * this flow as their ref_decap_handle
861 	 */
862 	flow_node->decap_l2_node = decap_l2_node;
863 	list_add(&flow_node->decap_l2_list_node,
864 		 &decap_l2_node->common_l2_flows);
865 	decap_l2_node->refcount++;
866 	return 0;
867 }
868 
869 static void bnxt_tc_put_decap_l2_node(struct bnxt *bp,
870 				      struct bnxt_tc_flow_node *flow_node)
871 {
872 	struct bnxt_tc_l2_node *decap_l2_node = flow_node->decap_l2_node;
873 	struct bnxt_tc_info *tc_info = bp->tc_info;
874 	int rc;
875 
876 	/* remove flow_node from the decap L2 sharing flow list */
877 	list_del(&flow_node->decap_l2_list_node);
878 	if (--decap_l2_node->refcount == 0) {
879 		rc =  rhashtable_remove_fast(&tc_info->decap_l2_table,
880 					     &decap_l2_node->node,
881 					     tc_info->decap_l2_ht_params);
882 		if (rc)
883 			netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
884 		kfree_rcu(decap_l2_node, rcu);
885 	}
886 }
887 
888 static void bnxt_tc_put_decap_handle(struct bnxt *bp,
889 				     struct bnxt_tc_flow_node *flow_node)
890 {
891 	__le32 decap_handle = flow_node->decap_node->tunnel_handle;
892 	struct bnxt_tc_info *tc_info = bp->tc_info;
893 	int rc;
894 
895 	if (flow_node->decap_l2_node)
896 		bnxt_tc_put_decap_l2_node(bp, flow_node);
897 
898 	rc = bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
899 				     &tc_info->decap_ht_params,
900 				     flow_node->decap_node);
901 	if (!rc && decap_handle != INVALID_TUNNEL_HANDLE)
902 		hwrm_cfa_decap_filter_free(bp, decap_handle);
903 }
904 
905 static int bnxt_tc_resolve_tunnel_hdrs(struct bnxt *bp,
906 				       struct ip_tunnel_key *tun_key,
907 				       struct bnxt_tc_l2_key *l2_info)
908 {
909 #ifdef CONFIG_INET
910 	struct net_device *real_dst_dev = bp->dev;
911 	struct flowi4 flow = { {0} };
912 	struct net_device *dst_dev;
913 	struct neighbour *nbr;
914 	struct rtable *rt;
915 	int rc;
916 
917 	flow.flowi4_proto = IPPROTO_UDP;
918 	flow.fl4_dport = tun_key->tp_dst;
919 	flow.daddr = tun_key->u.ipv4.dst;
920 
921 	rt = ip_route_output_key(dev_net(real_dst_dev), &flow);
922 	if (IS_ERR(rt)) {
923 		netdev_info(bp->dev, "no route to %pI4b", &flow.daddr);
924 		return -EOPNOTSUPP;
925 	}
926 
927 	/* The route must either point to the real_dst_dev or a dst_dev that
928 	 * uses the real_dst_dev.
929 	 */
930 	dst_dev = rt->dst.dev;
931 	if (is_vlan_dev(dst_dev)) {
932 #if IS_ENABLED(CONFIG_VLAN_8021Q)
933 		struct vlan_dev_priv *vlan = vlan_dev_priv(dst_dev);
934 
935 		if (vlan->real_dev != real_dst_dev) {
936 			netdev_info(bp->dev,
937 				    "dst_dev(%s) doesn't use PF-if(%s)",
938 				    netdev_name(dst_dev),
939 				    netdev_name(real_dst_dev));
940 			rc = -EOPNOTSUPP;
941 			goto put_rt;
942 		}
943 		l2_info->inner_vlan_tci = htons(vlan->vlan_id);
944 		l2_info->inner_vlan_tpid = vlan->vlan_proto;
945 		l2_info->num_vlans = 1;
946 #endif
947 	} else if (dst_dev != real_dst_dev) {
948 		netdev_info(bp->dev,
949 			    "dst_dev(%s) for %pI4b is not PF-if(%s)",
950 			    netdev_name(dst_dev), &flow.daddr,
951 			    netdev_name(real_dst_dev));
952 		rc = -EOPNOTSUPP;
953 		goto put_rt;
954 	}
955 
956 	nbr = dst_neigh_lookup(&rt->dst, &flow.daddr);
957 	if (!nbr) {
958 		netdev_info(bp->dev, "can't lookup neighbor for %pI4b",
959 			    &flow.daddr);
960 		rc = -EOPNOTSUPP;
961 		goto put_rt;
962 	}
963 
964 	tun_key->u.ipv4.src = flow.saddr;
965 	tun_key->ttl = ip4_dst_hoplimit(&rt->dst);
966 	neigh_ha_snapshot(l2_info->dmac, nbr, dst_dev);
967 	ether_addr_copy(l2_info->smac, dst_dev->dev_addr);
968 	neigh_release(nbr);
969 	ip_rt_put(rt);
970 
971 	return 0;
972 put_rt:
973 	ip_rt_put(rt);
974 	return rc;
975 #else
976 	return -EOPNOTSUPP;
977 #endif
978 }
979 
980 static int bnxt_tc_get_decap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
981 				    struct bnxt_tc_flow_node *flow_node,
982 				    __le32 *decap_filter_handle)
983 {
984 	struct ip_tunnel_key *decap_key = &flow->tun_key;
985 	struct bnxt_tc_info *tc_info = bp->tc_info;
986 	struct bnxt_tc_l2_key l2_info = { {0} };
987 	struct bnxt_tc_tunnel_node *decap_node;
988 	struct ip_tunnel_key tun_key = { 0 };
989 	struct bnxt_tc_l2_key *decap_l2_info;
990 	__le32 ref_decap_handle;
991 	int rc;
992 
993 	/* Check if there's another flow using the same tunnel decap.
994 	 * If not, add this tunnel to the table and resolve the other
995 	 * tunnel header fileds
996 	 */
997 	decap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->decap_table,
998 					     &tc_info->decap_ht_params,
999 					     decap_key);
1000 	if (!decap_node)
1001 		return -ENOMEM;
1002 
1003 	flow_node->decap_node = decap_node;
1004 
1005 	if (decap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1006 		goto done;
1007 
1008 	/* Resolve the L2 fields for tunnel decap
1009 	 * Resolve the route for remote vtep (saddr) of the decap key
1010 	 * Find it's next-hop mac addrs
1011 	 */
1012 	tun_key.u.ipv4.dst = flow->tun_key.u.ipv4.src;
1013 	tun_key.tp_dst = flow->tun_key.tp_dst;
1014 	rc = bnxt_tc_resolve_tunnel_hdrs(bp, &tun_key, &l2_info);
1015 	if (rc)
1016 		goto put_decap;
1017 
1018 	decap_l2_info = &decap_node->l2_info;
1019 	/* decap smac is wildcarded */
1020 	ether_addr_copy(decap_l2_info->dmac, l2_info.smac);
1021 	if (l2_info.num_vlans) {
1022 		decap_l2_info->num_vlans = l2_info.num_vlans;
1023 		decap_l2_info->inner_vlan_tpid = l2_info.inner_vlan_tpid;
1024 		decap_l2_info->inner_vlan_tci = l2_info.inner_vlan_tci;
1025 	}
1026 	flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS;
1027 
1028 	/* For getting a decap_filter_handle we first need to check if
1029 	 * there are any other decap flows that share the same tunnel L2
1030 	 * key and if so, pass that flow's decap_filter_handle as the
1031 	 * ref_decap_handle for this flow.
1032 	 */
1033 	rc = bnxt_tc_get_ref_decap_handle(bp, flow, decap_l2_info, flow_node,
1034 					  &ref_decap_handle);
1035 	if (rc)
1036 		goto put_decap;
1037 
1038 	/* Issue the hwrm cmd to allocate a decap filter handle */
1039 	rc = hwrm_cfa_decap_filter_alloc(bp, flow, decap_l2_info,
1040 					 ref_decap_handle,
1041 					 &decap_node->tunnel_handle);
1042 	if (rc)
1043 		goto put_decap_l2;
1044 
1045 done:
1046 	*decap_filter_handle = decap_node->tunnel_handle;
1047 	return 0;
1048 
1049 put_decap_l2:
1050 	bnxt_tc_put_decap_l2_node(bp, flow_node);
1051 put_decap:
1052 	bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
1053 				&tc_info->decap_ht_params,
1054 				flow_node->decap_node);
1055 	return rc;
1056 }
1057 
1058 static void bnxt_tc_put_encap_handle(struct bnxt *bp,
1059 				     struct bnxt_tc_tunnel_node *encap_node)
1060 {
1061 	__le32 encap_handle = encap_node->tunnel_handle;
1062 	struct bnxt_tc_info *tc_info = bp->tc_info;
1063 	int rc;
1064 
1065 	rc = bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1066 				     &tc_info->encap_ht_params, encap_node);
1067 	if (!rc && encap_handle != INVALID_TUNNEL_HANDLE)
1068 		hwrm_cfa_encap_record_free(bp, encap_handle);
1069 }
1070 
1071 /* Lookup the tunnel encap table and check if there's an encap_handle
1072  * alloc'd already.
1073  * If not, query L2 info via a route lookup and issue an encap_record_alloc
1074  * cmd to FW.
1075  */
1076 static int bnxt_tc_get_encap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1077 				    struct bnxt_tc_flow_node *flow_node,
1078 				    __le32 *encap_handle)
1079 {
1080 	struct ip_tunnel_key *encap_key = &flow->actions.tun_encap_key;
1081 	struct bnxt_tc_info *tc_info = bp->tc_info;
1082 	struct bnxt_tc_tunnel_node *encap_node;
1083 	int rc;
1084 
1085 	/* Check if there's another flow using the same tunnel encap.
1086 	 * If not, add this tunnel to the table and resolve the other
1087 	 * tunnel header fileds
1088 	 */
1089 	encap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->encap_table,
1090 					     &tc_info->encap_ht_params,
1091 					     encap_key);
1092 	if (!encap_node)
1093 		return -ENOMEM;
1094 
1095 	flow_node->encap_node = encap_node;
1096 
1097 	if (encap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1098 		goto done;
1099 
1100 	rc = bnxt_tc_resolve_tunnel_hdrs(bp, encap_key, &encap_node->l2_info);
1101 	if (rc)
1102 		goto put_encap;
1103 
1104 	/* Allocate a new tunnel encap record */
1105 	rc = hwrm_cfa_encap_record_alloc(bp, encap_key, &encap_node->l2_info,
1106 					 &encap_node->tunnel_handle);
1107 	if (rc)
1108 		goto put_encap;
1109 
1110 done:
1111 	*encap_handle = encap_node->tunnel_handle;
1112 	return 0;
1113 
1114 put_encap:
1115 	bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1116 				&tc_info->encap_ht_params, encap_node);
1117 	return rc;
1118 }
1119 
1120 static void bnxt_tc_put_tunnel_handle(struct bnxt *bp,
1121 				      struct bnxt_tc_flow *flow,
1122 				      struct bnxt_tc_flow_node *flow_node)
1123 {
1124 	if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1125 		bnxt_tc_put_decap_handle(bp, flow_node);
1126 	else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1127 		bnxt_tc_put_encap_handle(bp, flow_node->encap_node);
1128 }
1129 
1130 static int bnxt_tc_get_tunnel_handle(struct bnxt *bp,
1131 				     struct bnxt_tc_flow *flow,
1132 				     struct bnxt_tc_flow_node *flow_node,
1133 				     __le32 *tunnel_handle)
1134 {
1135 	if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1136 		return bnxt_tc_get_decap_handle(bp, flow, flow_node,
1137 						tunnel_handle);
1138 	else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1139 		return bnxt_tc_get_encap_handle(bp, flow, flow_node,
1140 						tunnel_handle);
1141 	else
1142 		return 0;
1143 }
1144 static int __bnxt_tc_del_flow(struct bnxt *bp,
1145 			      struct bnxt_tc_flow_node *flow_node)
1146 {
1147 	struct bnxt_tc_info *tc_info = bp->tc_info;
1148 	int rc;
1149 
1150 	/* send HWRM cmd to free the flow-id */
1151 	bnxt_hwrm_cfa_flow_free(bp, flow_node->flow_handle);
1152 
1153 	mutex_lock(&tc_info->lock);
1154 
1155 	/* release references to any tunnel encap/decap nodes */
1156 	bnxt_tc_put_tunnel_handle(bp, &flow_node->flow, flow_node);
1157 
1158 	/* release reference to l2 node */
1159 	bnxt_tc_put_l2_node(bp, flow_node);
1160 
1161 	mutex_unlock(&tc_info->lock);
1162 
1163 	rc = rhashtable_remove_fast(&tc_info->flow_table, &flow_node->node,
1164 				    tc_info->flow_ht_params);
1165 	if (rc)
1166 		netdev_err(bp->dev, "Error: %s: rhashtable_remove_fast rc=%d",
1167 			   __func__, rc);
1168 
1169 	kfree_rcu(flow_node, rcu);
1170 	return 0;
1171 }
1172 
1173 static void bnxt_tc_set_src_fid(struct bnxt *bp, struct bnxt_tc_flow *flow,
1174 				u16 src_fid)
1175 {
1176 	if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1177 		flow->src_fid = bp->pf.fw_fid;
1178 	else
1179 		flow->src_fid = src_fid;
1180 }
1181 
1182 /* Add a new flow or replace an existing flow.
1183  * Notes on locking:
1184  * There are essentially two critical sections here.
1185  * 1. while adding a new flow
1186  *    a) lookup l2-key
1187  *    b) issue HWRM cmd and get flow_handle
1188  *    c) link l2-key with flow
1189  * 2. while deleting a flow
1190  *    a) unlinking l2-key from flow
1191  * A lock is needed to protect these two critical sections.
1192  *
1193  * The hash-tables are already protected by the rhashtable API.
1194  */
1195 static int bnxt_tc_add_flow(struct bnxt *bp, u16 src_fid,
1196 			    struct tc_cls_flower_offload *tc_flow_cmd)
1197 {
1198 	struct bnxt_tc_flow_node *new_node, *old_node;
1199 	struct bnxt_tc_info *tc_info = bp->tc_info;
1200 	struct bnxt_tc_flow *flow;
1201 	__le32 tunnel_handle = 0;
1202 	__le16 ref_flow_handle;
1203 	int rc;
1204 
1205 	/* allocate memory for the new flow and it's node */
1206 	new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
1207 	if (!new_node) {
1208 		rc = -ENOMEM;
1209 		goto done;
1210 	}
1211 	new_node->cookie = tc_flow_cmd->cookie;
1212 	flow = &new_node->flow;
1213 
1214 	rc = bnxt_tc_parse_flow(bp, tc_flow_cmd, flow);
1215 	if (rc)
1216 		goto free_node;
1217 
1218 	bnxt_tc_set_src_fid(bp, flow, src_fid);
1219 
1220 	if (!bnxt_tc_can_offload(bp, flow)) {
1221 		rc = -ENOSPC;
1222 		goto free_node;
1223 	}
1224 
1225 	/* If a flow exists with the same cookie, delete it */
1226 	old_node = rhashtable_lookup_fast(&tc_info->flow_table,
1227 					  &tc_flow_cmd->cookie,
1228 					  tc_info->flow_ht_params);
1229 	if (old_node)
1230 		__bnxt_tc_del_flow(bp, old_node);
1231 
1232 	/* Check if the L2 part of the flow has been offloaded already.
1233 	 * If so, bump up it's refcnt and get it's reference handle.
1234 	 */
1235 	mutex_lock(&tc_info->lock);
1236 	rc = bnxt_tc_get_ref_flow_handle(bp, flow, new_node, &ref_flow_handle);
1237 	if (rc)
1238 		goto unlock;
1239 
1240 	/* If the flow involves tunnel encap/decap, get tunnel_handle */
1241 	rc = bnxt_tc_get_tunnel_handle(bp, flow, new_node, &tunnel_handle);
1242 	if (rc)
1243 		goto put_l2;
1244 
1245 	/* send HWRM cmd to alloc the flow */
1246 	rc = bnxt_hwrm_cfa_flow_alloc(bp, flow, ref_flow_handle,
1247 				      tunnel_handle, &new_node->flow_handle);
1248 	if (rc)
1249 		goto put_tunnel;
1250 
1251 	flow->lastused = jiffies;
1252 	spin_lock_init(&flow->stats_lock);
1253 	/* add new flow to flow-table */
1254 	rc = rhashtable_insert_fast(&tc_info->flow_table, &new_node->node,
1255 				    tc_info->flow_ht_params);
1256 	if (rc)
1257 		goto hwrm_flow_free;
1258 
1259 	mutex_unlock(&tc_info->lock);
1260 	return 0;
1261 
1262 hwrm_flow_free:
1263 	bnxt_hwrm_cfa_flow_free(bp, new_node->flow_handle);
1264 put_tunnel:
1265 	bnxt_tc_put_tunnel_handle(bp, flow, new_node);
1266 put_l2:
1267 	bnxt_tc_put_l2_node(bp, new_node);
1268 unlock:
1269 	mutex_unlock(&tc_info->lock);
1270 free_node:
1271 	kfree_rcu(new_node, rcu);
1272 done:
1273 	netdev_err(bp->dev, "Error: %s: cookie=0x%lx error=%d",
1274 		   __func__, tc_flow_cmd->cookie, rc);
1275 	return rc;
1276 }
1277 
1278 static int bnxt_tc_del_flow(struct bnxt *bp,
1279 			    struct tc_cls_flower_offload *tc_flow_cmd)
1280 {
1281 	struct bnxt_tc_info *tc_info = bp->tc_info;
1282 	struct bnxt_tc_flow_node *flow_node;
1283 
1284 	flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1285 					   &tc_flow_cmd->cookie,
1286 					   tc_info->flow_ht_params);
1287 	if (!flow_node)
1288 		return -EINVAL;
1289 
1290 	return __bnxt_tc_del_flow(bp, flow_node);
1291 }
1292 
1293 static int bnxt_tc_get_flow_stats(struct bnxt *bp,
1294 				  struct tc_cls_flower_offload *tc_flow_cmd)
1295 {
1296 	struct bnxt_tc_flow_stats stats, *curr_stats, *prev_stats;
1297 	struct bnxt_tc_info *tc_info = bp->tc_info;
1298 	struct bnxt_tc_flow_node *flow_node;
1299 	struct bnxt_tc_flow *flow;
1300 	unsigned long lastused;
1301 
1302 	flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1303 					   &tc_flow_cmd->cookie,
1304 					   tc_info->flow_ht_params);
1305 	if (!flow_node)
1306 		return -1;
1307 
1308 	flow = &flow_node->flow;
1309 	curr_stats = &flow->stats;
1310 	prev_stats = &flow->prev_stats;
1311 
1312 	spin_lock(&flow->stats_lock);
1313 	stats.packets = curr_stats->packets - prev_stats->packets;
1314 	stats.bytes = curr_stats->bytes - prev_stats->bytes;
1315 	*prev_stats = *curr_stats;
1316 	lastused = flow->lastused;
1317 	spin_unlock(&flow->stats_lock);
1318 
1319 	tcf_exts_stats_update(tc_flow_cmd->exts, stats.bytes, stats.packets,
1320 			      lastused);
1321 	return 0;
1322 }
1323 
1324 static int
1325 bnxt_hwrm_cfa_flow_stats_get(struct bnxt *bp, int num_flows,
1326 			     struct bnxt_tc_stats_batch stats_batch[])
1327 {
1328 	struct hwrm_cfa_flow_stats_output *resp = bp->hwrm_cmd_resp_addr;
1329 	struct hwrm_cfa_flow_stats_input req = { 0 };
1330 	__le16 *req_flow_handles = &req.flow_handle_0;
1331 	int rc, i;
1332 
1333 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_STATS, -1, -1);
1334 	req.num_flows = cpu_to_le16(num_flows);
1335 	for (i = 0; i < num_flows; i++) {
1336 		struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1337 
1338 		req_flow_handles[i] = flow_node->flow_handle;
1339 	}
1340 
1341 	mutex_lock(&bp->hwrm_cmd_lock);
1342 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1343 	if (!rc) {
1344 		__le64 *resp_packets = &resp->packet_0;
1345 		__le64 *resp_bytes = &resp->byte_0;
1346 
1347 		for (i = 0; i < num_flows; i++) {
1348 			stats_batch[i].hw_stats.packets =
1349 						le64_to_cpu(resp_packets[i]);
1350 			stats_batch[i].hw_stats.bytes =
1351 						le64_to_cpu(resp_bytes[i]);
1352 		}
1353 	} else {
1354 		netdev_info(bp->dev, "error rc=%d", rc);
1355 	}
1356 	mutex_unlock(&bp->hwrm_cmd_lock);
1357 
1358 	if (rc)
1359 		rc = -EIO;
1360 	return rc;
1361 }
1362 
1363 /* Add val to accum while handling a possible wraparound
1364  * of val. Eventhough val is of type u64, its actual width
1365  * is denoted by mask and will wrap-around beyond that width.
1366  */
1367 static void accumulate_val(u64 *accum, u64 val, u64 mask)
1368 {
1369 #define low_bits(x, mask)		((x) & (mask))
1370 #define high_bits(x, mask)		((x) & ~(mask))
1371 	bool wrapped = val < low_bits(*accum, mask);
1372 
1373 	*accum = high_bits(*accum, mask) + val;
1374 	if (wrapped)
1375 		*accum += (mask + 1);
1376 }
1377 
1378 /* The HW counters' width is much less than 64bits.
1379  * Handle possible wrap-around while updating the stat counters
1380  */
1381 static void bnxt_flow_stats_accum(struct bnxt_tc_info *tc_info,
1382 				  struct bnxt_tc_flow_stats *acc_stats,
1383 				  struct bnxt_tc_flow_stats *hw_stats)
1384 {
1385 	accumulate_val(&acc_stats->bytes, hw_stats->bytes, tc_info->bytes_mask);
1386 	accumulate_val(&acc_stats->packets, hw_stats->packets,
1387 		       tc_info->packets_mask);
1388 }
1389 
1390 static int
1391 bnxt_tc_flow_stats_batch_update(struct bnxt *bp, int num_flows,
1392 				struct bnxt_tc_stats_batch stats_batch[])
1393 {
1394 	struct bnxt_tc_info *tc_info = bp->tc_info;
1395 	int rc, i;
1396 
1397 	rc = bnxt_hwrm_cfa_flow_stats_get(bp, num_flows, stats_batch);
1398 	if (rc)
1399 		return rc;
1400 
1401 	for (i = 0; i < num_flows; i++) {
1402 		struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1403 		struct bnxt_tc_flow *flow = &flow_node->flow;
1404 
1405 		spin_lock(&flow->stats_lock);
1406 		bnxt_flow_stats_accum(tc_info, &flow->stats,
1407 				      &stats_batch[i].hw_stats);
1408 		if (flow->stats.packets != flow->prev_stats.packets)
1409 			flow->lastused = jiffies;
1410 		spin_unlock(&flow->stats_lock);
1411 	}
1412 
1413 	return 0;
1414 }
1415 
1416 static int
1417 bnxt_tc_flow_stats_batch_prep(struct bnxt *bp,
1418 			      struct bnxt_tc_stats_batch stats_batch[],
1419 			      int *num_flows)
1420 {
1421 	struct bnxt_tc_info *tc_info = bp->tc_info;
1422 	struct rhashtable_iter *iter = &tc_info->iter;
1423 	void *flow_node;
1424 	int rc, i;
1425 
1426 	rhashtable_walk_start(iter);
1427 
1428 	rc = 0;
1429 	for (i = 0; i < BNXT_FLOW_STATS_BATCH_MAX; i++) {
1430 		flow_node = rhashtable_walk_next(iter);
1431 		if (IS_ERR(flow_node)) {
1432 			i = 0;
1433 			if (PTR_ERR(flow_node) == -EAGAIN) {
1434 				continue;
1435 			} else {
1436 				rc = PTR_ERR(flow_node);
1437 				goto done;
1438 			}
1439 		}
1440 
1441 		/* No more flows */
1442 		if (!flow_node)
1443 			goto done;
1444 
1445 		stats_batch[i].flow_node = flow_node;
1446 	}
1447 done:
1448 	rhashtable_walk_stop(iter);
1449 	*num_flows = i;
1450 	return rc;
1451 }
1452 
1453 void bnxt_tc_flow_stats_work(struct bnxt *bp)
1454 {
1455 	struct bnxt_tc_info *tc_info = bp->tc_info;
1456 	int num_flows, rc;
1457 
1458 	num_flows = atomic_read(&tc_info->flow_table.nelems);
1459 	if (!num_flows)
1460 		return;
1461 
1462 	rhashtable_walk_enter(&tc_info->flow_table, &tc_info->iter);
1463 
1464 	for (;;) {
1465 		rc = bnxt_tc_flow_stats_batch_prep(bp, tc_info->stats_batch,
1466 						   &num_flows);
1467 		if (rc) {
1468 			if (rc == -EAGAIN)
1469 				continue;
1470 			break;
1471 		}
1472 
1473 		if (!num_flows)
1474 			break;
1475 
1476 		bnxt_tc_flow_stats_batch_update(bp, num_flows,
1477 						tc_info->stats_batch);
1478 	}
1479 
1480 	rhashtable_walk_exit(&tc_info->iter);
1481 }
1482 
1483 int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
1484 			 struct tc_cls_flower_offload *cls_flower)
1485 {
1486 	int rc = 0;
1487 
1488 	switch (cls_flower->command) {
1489 	case TC_CLSFLOWER_REPLACE:
1490 		rc = bnxt_tc_add_flow(bp, src_fid, cls_flower);
1491 		break;
1492 
1493 	case TC_CLSFLOWER_DESTROY:
1494 		rc = bnxt_tc_del_flow(bp, cls_flower);
1495 		break;
1496 
1497 	case TC_CLSFLOWER_STATS:
1498 		rc = bnxt_tc_get_flow_stats(bp, cls_flower);
1499 		break;
1500 	}
1501 	return rc;
1502 }
1503 
1504 static const struct rhashtable_params bnxt_tc_flow_ht_params = {
1505 	.head_offset = offsetof(struct bnxt_tc_flow_node, node),
1506 	.key_offset = offsetof(struct bnxt_tc_flow_node, cookie),
1507 	.key_len = sizeof(((struct bnxt_tc_flow_node *)0)->cookie),
1508 	.automatic_shrinking = true
1509 };
1510 
1511 static const struct rhashtable_params bnxt_tc_l2_ht_params = {
1512 	.head_offset = offsetof(struct bnxt_tc_l2_node, node),
1513 	.key_offset = offsetof(struct bnxt_tc_l2_node, key),
1514 	.key_len = BNXT_TC_L2_KEY_LEN,
1515 	.automatic_shrinking = true
1516 };
1517 
1518 static const struct rhashtable_params bnxt_tc_decap_l2_ht_params = {
1519 	.head_offset = offsetof(struct bnxt_tc_l2_node, node),
1520 	.key_offset = offsetof(struct bnxt_tc_l2_node, key),
1521 	.key_len = BNXT_TC_L2_KEY_LEN,
1522 	.automatic_shrinking = true
1523 };
1524 
1525 static const struct rhashtable_params bnxt_tc_tunnel_ht_params = {
1526 	.head_offset = offsetof(struct bnxt_tc_tunnel_node, node),
1527 	.key_offset = offsetof(struct bnxt_tc_tunnel_node, key),
1528 	.key_len = sizeof(struct ip_tunnel_key),
1529 	.automatic_shrinking = true
1530 };
1531 
1532 /* convert counter width in bits to a mask */
1533 #define mask(width)		((u64)~0 >> (64 - (width)))
1534 
1535 int bnxt_init_tc(struct bnxt *bp)
1536 {
1537 	struct bnxt_tc_info *tc_info;
1538 	int rc;
1539 
1540 	if (bp->hwrm_spec_code < 0x10803) {
1541 		netdev_warn(bp->dev,
1542 			    "Firmware does not support TC flower offload.\n");
1543 		return -ENOTSUPP;
1544 	}
1545 
1546 	tc_info = kzalloc(sizeof(*tc_info), GFP_KERNEL);
1547 	if (!tc_info)
1548 		return -ENOMEM;
1549 	mutex_init(&tc_info->lock);
1550 
1551 	/* Counter widths are programmed by FW */
1552 	tc_info->bytes_mask = mask(36);
1553 	tc_info->packets_mask = mask(28);
1554 
1555 	tc_info->flow_ht_params = bnxt_tc_flow_ht_params;
1556 	rc = rhashtable_init(&tc_info->flow_table, &tc_info->flow_ht_params);
1557 	if (rc)
1558 		goto free_tc_info;
1559 
1560 	tc_info->l2_ht_params = bnxt_tc_l2_ht_params;
1561 	rc = rhashtable_init(&tc_info->l2_table, &tc_info->l2_ht_params);
1562 	if (rc)
1563 		goto destroy_flow_table;
1564 
1565 	tc_info->decap_l2_ht_params = bnxt_tc_decap_l2_ht_params;
1566 	rc = rhashtable_init(&tc_info->decap_l2_table,
1567 			     &tc_info->decap_l2_ht_params);
1568 	if (rc)
1569 		goto destroy_l2_table;
1570 
1571 	tc_info->decap_ht_params = bnxt_tc_tunnel_ht_params;
1572 	rc = rhashtable_init(&tc_info->decap_table,
1573 			     &tc_info->decap_ht_params);
1574 	if (rc)
1575 		goto destroy_decap_l2_table;
1576 
1577 	tc_info->encap_ht_params = bnxt_tc_tunnel_ht_params;
1578 	rc = rhashtable_init(&tc_info->encap_table,
1579 			     &tc_info->encap_ht_params);
1580 	if (rc)
1581 		goto destroy_decap_table;
1582 
1583 	tc_info->enabled = true;
1584 	bp->dev->hw_features |= NETIF_F_HW_TC;
1585 	bp->dev->features |= NETIF_F_HW_TC;
1586 	bp->tc_info = tc_info;
1587 	return 0;
1588 
1589 destroy_decap_table:
1590 	rhashtable_destroy(&tc_info->decap_table);
1591 destroy_decap_l2_table:
1592 	rhashtable_destroy(&tc_info->decap_l2_table);
1593 destroy_l2_table:
1594 	rhashtable_destroy(&tc_info->l2_table);
1595 destroy_flow_table:
1596 	rhashtable_destroy(&tc_info->flow_table);
1597 free_tc_info:
1598 	kfree(tc_info);
1599 	return rc;
1600 }
1601 
1602 void bnxt_shutdown_tc(struct bnxt *bp)
1603 {
1604 	struct bnxt_tc_info *tc_info = bp->tc_info;
1605 
1606 	if (!bnxt_tc_flower_enabled(bp))
1607 		return;
1608 
1609 	rhashtable_destroy(&tc_info->flow_table);
1610 	rhashtable_destroy(&tc_info->l2_table);
1611 	rhashtable_destroy(&tc_info->decap_l2_table);
1612 	rhashtable_destroy(&tc_info->decap_table);
1613 	rhashtable_destroy(&tc_info->encap_table);
1614 	kfree(tc_info);
1615 	bp->tc_info = NULL;
1616 }
1617