1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2017 Broadcom Limited
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.
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>
23 #include "bnxt_sriov.h"
27 #define BNXT_FID_INVALID 0xffff
28 #define VLAN_TCI(vid, prio) ((vid) | ((prio) << VLAN_PRIO_SHIFT))
30 #define is_vlan_pcp_wildcarded(vlan_tci_mask) \
31 ((ntohs(vlan_tci_mask) & VLAN_PRIO_MASK) == 0x0000)
32 #define is_vlan_pcp_exactmatch(vlan_tci_mask) \
33 ((ntohs(vlan_tci_mask) & VLAN_PRIO_MASK) == VLAN_PRIO_MASK)
34 #define is_vlan_pcp_zero(vlan_tci) \
35 ((ntohs(vlan_tci) & VLAN_PRIO_MASK) == 0x0000)
36 #define is_vid_exactmatch(vlan_tci_mask) \
37 ((ntohs(vlan_tci_mask) & VLAN_VID_MASK) == VLAN_VID_MASK)
39 /* Return the dst fid of the func for flow forwarding
40 * For PFs: src_fid is the fid of the PF
41 * For VF-reps: src_fid the fid of the VF
43 static u16 bnxt_flow_get_dst_fid(struct bnxt *pf_bp, struct net_device *dev)
47 /* check if dev belongs to the same switch */
48 if (!switchdev_port_same_parent_id(pf_bp->dev, dev)) {
49 netdev_info(pf_bp->dev, "dev(ifindex=%d) not on same switch",
51 return BNXT_FID_INVALID;
54 /* Is dev a VF-rep? */
55 if (bnxt_dev_is_vf_rep(dev))
56 return bnxt_vf_rep_get_fid(dev);
58 bp = netdev_priv(dev);
62 static int bnxt_tc_parse_redir(struct bnxt *bp,
63 struct bnxt_tc_actions *actions,
64 const struct tc_action *tc_act)
66 struct net_device *dev = tcf_mirred_dev(tc_act);
69 netdev_info(bp->dev, "no dev in mirred action");
73 actions->flags |= BNXT_TC_ACTION_FLAG_FWD;
74 actions->dst_dev = dev;
78 static void bnxt_tc_parse_vlan(struct bnxt *bp,
79 struct bnxt_tc_actions *actions,
80 const struct tc_action *tc_act)
82 if (tcf_vlan_action(tc_act) == TCA_VLAN_ACT_POP) {
83 actions->flags |= BNXT_TC_ACTION_FLAG_POP_VLAN;
84 } else if (tcf_vlan_action(tc_act) == TCA_VLAN_ACT_PUSH) {
85 actions->flags |= BNXT_TC_ACTION_FLAG_PUSH_VLAN;
86 actions->push_vlan_tci = htons(tcf_vlan_push_vid(tc_act));
87 actions->push_vlan_tpid = tcf_vlan_push_proto(tc_act);
91 static int bnxt_tc_parse_tunnel_set(struct bnxt *bp,
92 struct bnxt_tc_actions *actions,
93 const struct tc_action *tc_act)
95 struct ip_tunnel_info *tun_info = tcf_tunnel_info(tc_act);
96 struct ip_tunnel_key *tun_key = &tun_info->key;
98 if (ip_tunnel_info_af(tun_info) != AF_INET) {
99 netdev_info(bp->dev, "only IPv4 tunnel-encap is supported");
103 actions->tun_encap_key = *tun_key;
104 actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP;
108 static int bnxt_tc_parse_actions(struct bnxt *bp,
109 struct bnxt_tc_actions *actions,
110 struct tcf_exts *tc_exts)
112 const struct tc_action *tc_act;
113 LIST_HEAD(tc_actions);
116 if (!tcf_exts_has_actions(tc_exts)) {
117 netdev_info(bp->dev, "no actions");
121 tcf_exts_to_list(tc_exts, &tc_actions);
122 list_for_each_entry(tc_act, &tc_actions, list) {
124 if (is_tcf_gact_shot(tc_act)) {
125 actions->flags |= BNXT_TC_ACTION_FLAG_DROP;
126 return 0; /* don't bother with other actions */
129 /* Redirect action */
130 if (is_tcf_mirred_egress_redirect(tc_act)) {
131 rc = bnxt_tc_parse_redir(bp, actions, tc_act);
138 if (is_tcf_vlan(tc_act)) {
139 bnxt_tc_parse_vlan(bp, actions, tc_act);
144 if (is_tcf_tunnel_set(tc_act)) {
145 rc = bnxt_tc_parse_tunnel_set(bp, actions, tc_act);
152 if (is_tcf_tunnel_release(tc_act)) {
153 actions->flags |= BNXT_TC_ACTION_FLAG_TUNNEL_DECAP;
158 if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
159 if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
160 /* dst_fid is PF's fid */
161 actions->dst_fid = bp->pf.fw_fid;
163 /* find the FID from dst_dev */
165 bnxt_flow_get_dst_fid(bp, actions->dst_dev);
166 if (actions->dst_fid == BNXT_FID_INVALID)
174 #define GET_KEY(flow_cmd, key_type) \
175 skb_flow_dissector_target((flow_cmd)->dissector, key_type,\
177 #define GET_MASK(flow_cmd, key_type) \
178 skb_flow_dissector_target((flow_cmd)->dissector, key_type,\
181 static int bnxt_tc_parse_flow(struct bnxt *bp,
182 struct tc_cls_flower_offload *tc_flow_cmd,
183 struct bnxt_tc_flow *flow)
185 struct flow_dissector *dissector = tc_flow_cmd->dissector;
188 /* KEY_CONTROL and KEY_BASIC are needed for forming a meaningful key */
189 if ((dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_CONTROL)) == 0 ||
190 (dissector->used_keys & BIT(FLOW_DISSECTOR_KEY_BASIC)) == 0) {
191 netdev_info(bp->dev, "cannot form TC key: used_keys = 0x%x",
192 dissector->used_keys);
196 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
197 struct flow_dissector_key_control *key =
198 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_CONTROL);
200 addr_type = key->addr_type;
203 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_BASIC)) {
204 struct flow_dissector_key_basic *key =
205 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_BASIC);
206 struct flow_dissector_key_basic *mask =
207 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_BASIC);
209 flow->l2_key.ether_type = key->n_proto;
210 flow->l2_mask.ether_type = mask->n_proto;
212 if (key->n_proto == htons(ETH_P_IP) ||
213 key->n_proto == htons(ETH_P_IPV6)) {
214 flow->l4_key.ip_proto = key->ip_proto;
215 flow->l4_mask.ip_proto = mask->ip_proto;
219 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
220 struct flow_dissector_key_eth_addrs *key =
221 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ETH_ADDRS);
222 struct flow_dissector_key_eth_addrs *mask =
223 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ETH_ADDRS);
225 flow->flags |= BNXT_TC_FLOW_FLAGS_ETH_ADDRS;
226 ether_addr_copy(flow->l2_key.dmac, key->dst);
227 ether_addr_copy(flow->l2_mask.dmac, mask->dst);
228 ether_addr_copy(flow->l2_key.smac, key->src);
229 ether_addr_copy(flow->l2_mask.smac, mask->src);
232 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_VLAN)) {
233 struct flow_dissector_key_vlan *key =
234 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_VLAN);
235 struct flow_dissector_key_vlan *mask =
236 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_VLAN);
238 flow->l2_key.inner_vlan_tci =
239 cpu_to_be16(VLAN_TCI(key->vlan_id, key->vlan_priority));
240 flow->l2_mask.inner_vlan_tci =
241 cpu_to_be16((VLAN_TCI(mask->vlan_id, mask->vlan_priority)));
242 flow->l2_key.inner_vlan_tpid = htons(ETH_P_8021Q);
243 flow->l2_mask.inner_vlan_tpid = htons(0xffff);
244 flow->l2_key.num_vlans = 1;
247 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
248 struct flow_dissector_key_ipv4_addrs *key =
249 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
250 struct flow_dissector_key_ipv4_addrs *mask =
251 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV4_ADDRS);
253 flow->flags |= BNXT_TC_FLOW_FLAGS_IPV4_ADDRS;
254 flow->l3_key.ipv4.daddr.s_addr = key->dst;
255 flow->l3_mask.ipv4.daddr.s_addr = mask->dst;
256 flow->l3_key.ipv4.saddr.s_addr = key->src;
257 flow->l3_mask.ipv4.saddr.s_addr = mask->src;
258 } else if (dissector_uses_key(dissector,
259 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
260 struct flow_dissector_key_ipv6_addrs *key =
261 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
262 struct flow_dissector_key_ipv6_addrs *mask =
263 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_IPV6_ADDRS);
265 flow->flags |= BNXT_TC_FLOW_FLAGS_IPV6_ADDRS;
266 flow->l3_key.ipv6.daddr = key->dst;
267 flow->l3_mask.ipv6.daddr = mask->dst;
268 flow->l3_key.ipv6.saddr = key->src;
269 flow->l3_mask.ipv6.saddr = mask->src;
272 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_PORTS)) {
273 struct flow_dissector_key_ports *key =
274 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_PORTS);
275 struct flow_dissector_key_ports *mask =
276 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_PORTS);
278 flow->flags |= BNXT_TC_FLOW_FLAGS_PORTS;
279 flow->l4_key.ports.dport = key->dst;
280 flow->l4_mask.ports.dport = mask->dst;
281 flow->l4_key.ports.sport = key->src;
282 flow->l4_mask.ports.sport = mask->src;
285 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ICMP)) {
286 struct flow_dissector_key_icmp *key =
287 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ICMP);
288 struct flow_dissector_key_icmp *mask =
289 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ICMP);
291 flow->flags |= BNXT_TC_FLOW_FLAGS_ICMP;
292 flow->l4_key.icmp.type = key->type;
293 flow->l4_key.icmp.code = key->code;
294 flow->l4_mask.icmp.type = mask->type;
295 flow->l4_mask.icmp.code = mask->code;
298 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
299 struct flow_dissector_key_control *key =
300 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_CONTROL);
302 addr_type = key->addr_type;
305 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
306 struct flow_dissector_key_ipv4_addrs *key =
307 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS);
308 struct flow_dissector_key_ipv4_addrs *mask =
309 GET_MASK(tc_flow_cmd,
310 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS);
312 flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS;
313 flow->tun_key.u.ipv4.dst = key->dst;
314 flow->tun_mask.u.ipv4.dst = mask->dst;
315 flow->tun_key.u.ipv4.src = key->src;
316 flow->tun_mask.u.ipv4.src = mask->src;
317 } else if (dissector_uses_key(dissector,
318 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
322 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
323 struct flow_dissector_key_keyid *key =
324 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_KEYID);
325 struct flow_dissector_key_keyid *mask =
326 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_KEYID);
328 flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ID;
329 flow->tun_key.tun_id = key32_to_tunnel_id(key->keyid);
330 flow->tun_mask.tun_id = key32_to_tunnel_id(mask->keyid);
333 if (dissector_uses_key(dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
334 struct flow_dissector_key_ports *key =
335 GET_KEY(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_PORTS);
336 struct flow_dissector_key_ports *mask =
337 GET_MASK(tc_flow_cmd, FLOW_DISSECTOR_KEY_ENC_PORTS);
339 flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_PORTS;
340 flow->tun_key.tp_dst = key->dst;
341 flow->tun_mask.tp_dst = mask->dst;
342 flow->tun_key.tp_src = key->src;
343 flow->tun_mask.tp_src = mask->src;
346 return bnxt_tc_parse_actions(bp, &flow->actions, tc_flow_cmd->exts);
349 static int bnxt_hwrm_cfa_flow_free(struct bnxt *bp, __le16 flow_handle)
351 struct hwrm_cfa_flow_free_input req = { 0 };
354 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_FREE, -1, -1);
355 req.flow_handle = flow_handle;
357 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
359 netdev_info(bp->dev, "Error: %s: flow_handle=0x%x rc=%d",
360 __func__, flow_handle, rc);
367 static int ipv6_mask_len(struct in6_addr *mask)
371 for (i = 0; i < 4; i++)
372 mask_len += inet_mask_len(mask->s6_addr32[i]);
377 static bool is_wildcard(void *mask, int len)
382 for (i = 0; i < len; i++) {
389 static bool is_exactmatch(void *mask, int len)
394 for (i = 0; i < len; i++)
401 static bool is_vlan_tci_allowed(__be16 vlan_tci_mask,
404 /* VLAN priority must be either exactly zero or fully wildcarded and
405 * VLAN id must be exact match.
407 if (is_vid_exactmatch(vlan_tci_mask) &&
408 ((is_vlan_pcp_exactmatch(vlan_tci_mask) &&
409 is_vlan_pcp_zero(vlan_tci)) ||
410 is_vlan_pcp_wildcarded(vlan_tci_mask)))
416 static bool bits_set(void *key, int len)
421 for (i = 0; i < len; i++)
428 static int bnxt_hwrm_cfa_flow_alloc(struct bnxt *bp, struct bnxt_tc_flow *flow,
429 __le16 ref_flow_handle,
430 __le32 tunnel_handle, __le16 *flow_handle)
432 struct hwrm_cfa_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr;
433 struct bnxt_tc_actions *actions = &flow->actions;
434 struct bnxt_tc_l3_key *l3_mask = &flow->l3_mask;
435 struct bnxt_tc_l3_key *l3_key = &flow->l3_key;
436 struct hwrm_cfa_flow_alloc_input req = { 0 };
437 u16 flow_flags = 0, action_flags = 0;
440 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_ALLOC, -1, -1);
442 req.src_fid = cpu_to_le16(flow->src_fid);
443 req.ref_flow_handle = ref_flow_handle;
445 if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP ||
446 actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) {
447 req.tunnel_handle = tunnel_handle;
448 flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_TUNNEL;
449 action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_TUNNEL;
452 req.ethertype = flow->l2_key.ether_type;
453 req.ip_proto = flow->l4_key.ip_proto;
455 if (flow->flags & BNXT_TC_FLOW_FLAGS_ETH_ADDRS) {
456 memcpy(req.dmac, flow->l2_key.dmac, ETH_ALEN);
457 memcpy(req.smac, flow->l2_key.smac, ETH_ALEN);
460 if (flow->l2_key.num_vlans > 0) {
461 flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_NUM_VLAN_ONE;
462 /* FW expects the inner_vlan_tci value to be set
463 * in outer_vlan_tci when num_vlans is 1 (which is
464 * always the case in TC.)
466 req.outer_vlan_tci = flow->l2_key.inner_vlan_tci;
469 /* If all IP and L4 fields are wildcarded then this is an L2 flow */
470 if (is_wildcard(l3_mask, sizeof(*l3_mask)) &&
471 is_wildcard(&flow->l4_mask, sizeof(flow->l4_mask))) {
472 flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_L2;
474 flow_flags |= flow->l2_key.ether_type == htons(ETH_P_IP) ?
475 CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV4 :
476 CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV6;
478 if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV4_ADDRS) {
479 req.ip_dst[0] = l3_key->ipv4.daddr.s_addr;
480 req.ip_dst_mask_len =
481 inet_mask_len(l3_mask->ipv4.daddr.s_addr);
482 req.ip_src[0] = l3_key->ipv4.saddr.s_addr;
483 req.ip_src_mask_len =
484 inet_mask_len(l3_mask->ipv4.saddr.s_addr);
485 } else if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV6_ADDRS) {
486 memcpy(req.ip_dst, l3_key->ipv6.daddr.s6_addr32,
488 req.ip_dst_mask_len =
489 ipv6_mask_len(&l3_mask->ipv6.daddr);
490 memcpy(req.ip_src, l3_key->ipv6.saddr.s6_addr32,
492 req.ip_src_mask_len =
493 ipv6_mask_len(&l3_mask->ipv6.saddr);
497 if (flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) {
498 req.l4_src_port = flow->l4_key.ports.sport;
499 req.l4_src_port_mask = flow->l4_mask.ports.sport;
500 req.l4_dst_port = flow->l4_key.ports.dport;
501 req.l4_dst_port_mask = flow->l4_mask.ports.dport;
502 } else if (flow->flags & BNXT_TC_FLOW_FLAGS_ICMP) {
503 /* l4 ports serve as type/code when ip_proto is ICMP */
504 req.l4_src_port = htons(flow->l4_key.icmp.type);
505 req.l4_src_port_mask = htons(flow->l4_mask.icmp.type);
506 req.l4_dst_port = htons(flow->l4_key.icmp.code);
507 req.l4_dst_port_mask = htons(flow->l4_mask.icmp.code);
509 req.flags = cpu_to_le16(flow_flags);
511 if (actions->flags & BNXT_TC_ACTION_FLAG_DROP) {
512 action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_DROP;
514 if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) {
515 action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_FWD;
516 req.dst_fid = cpu_to_le16(actions->dst_fid);
518 if (actions->flags & BNXT_TC_ACTION_FLAG_PUSH_VLAN) {
520 CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
521 req.l2_rewrite_vlan_tpid = actions->push_vlan_tpid;
522 req.l2_rewrite_vlan_tci = actions->push_vlan_tci;
523 memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN);
524 memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN);
526 if (actions->flags & BNXT_TC_ACTION_FLAG_POP_VLAN) {
528 CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE;
529 /* Rewrite config with tpid = 0 implies vlan pop */
530 req.l2_rewrite_vlan_tpid = 0;
531 memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN);
532 memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN);
535 req.action_flags = cpu_to_le16(action_flags);
537 mutex_lock(&bp->hwrm_cmd_lock);
538 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
540 *flow_handle = resp->flow_handle;
541 mutex_unlock(&bp->hwrm_cmd_lock);
543 if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR)
550 static int hwrm_cfa_decap_filter_alloc(struct bnxt *bp,
551 struct bnxt_tc_flow *flow,
552 struct bnxt_tc_l2_key *l2_info,
553 __le32 ref_decap_handle,
554 __le32 *decap_filter_handle)
556 struct hwrm_cfa_decap_filter_alloc_output *resp =
557 bp->hwrm_cmd_resp_addr;
558 struct hwrm_cfa_decap_filter_alloc_input req = { 0 };
559 struct ip_tunnel_key *tun_key = &flow->tun_key;
563 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_ALLOC, -1, -1);
565 req.flags = cpu_to_le32(CFA_DECAP_FILTER_ALLOC_REQ_FLAGS_OVS_TUNNEL);
566 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE |
567 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL;
568 req.tunnel_type = CFA_DECAP_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
569 req.ip_protocol = CFA_DECAP_FILTER_ALLOC_REQ_IP_PROTOCOL_UDP;
571 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ID) {
572 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_ID;
573 /* tunnel_id is wrongly defined in hsi defn. as __le32 */
574 req.tunnel_id = tunnel_id_to_key32(tun_key->tun_id);
577 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS) {
578 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_MACADDR;
579 ether_addr_copy(req.dst_macaddr, l2_info->dmac);
581 if (l2_info->num_vlans) {
582 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_T_IVLAN_VID;
583 req.t_ivlan_vid = l2_info->inner_vlan_tci;
586 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE;
587 req.ethertype = htons(ETH_P_IP);
589 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS) {
590 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |
591 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |
592 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE;
593 req.ip_addr_type = CFA_DECAP_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
594 req.dst_ipaddr[0] = tun_key->u.ipv4.dst;
595 req.src_ipaddr[0] = tun_key->u.ipv4.src;
598 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_PORTS) {
599 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_PORT;
600 req.dst_port = tun_key->tp_dst;
603 /* Eventhough the decap_handle returned by hwrm_cfa_decap_filter_alloc
604 * is defined as __le32, l2_ctxt_ref_id is defined in HSI as __le16.
606 req.l2_ctxt_ref_id = (__force __le16)ref_decap_handle;
607 req.enables = cpu_to_le32(enables);
609 mutex_lock(&bp->hwrm_cmd_lock);
610 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
612 *decap_filter_handle = resp->decap_filter_id;
614 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
615 mutex_unlock(&bp->hwrm_cmd_lock);
622 static int hwrm_cfa_decap_filter_free(struct bnxt *bp,
623 __le32 decap_filter_handle)
625 struct hwrm_cfa_decap_filter_free_input req = { 0 };
628 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_FREE, -1, -1);
629 req.decap_filter_id = decap_filter_handle;
631 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
633 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
640 static int hwrm_cfa_encap_record_alloc(struct bnxt *bp,
641 struct ip_tunnel_key *encap_key,
642 struct bnxt_tc_l2_key *l2_info,
643 __le32 *encap_record_handle)
645 struct hwrm_cfa_encap_record_alloc_output *resp =
646 bp->hwrm_cmd_resp_addr;
647 struct hwrm_cfa_encap_record_alloc_input req = { 0 };
648 struct hwrm_cfa_encap_data_vxlan *encap =
649 (struct hwrm_cfa_encap_data_vxlan *)&req.encap_data;
650 struct hwrm_vxlan_ipv4_hdr *encap_ipv4 =
651 (struct hwrm_vxlan_ipv4_hdr *)encap->l3;
654 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_ALLOC, -1, -1);
656 req.encap_type = CFA_ENCAP_RECORD_ALLOC_REQ_ENCAP_TYPE_VXLAN;
658 ether_addr_copy(encap->dst_mac_addr, l2_info->dmac);
659 ether_addr_copy(encap->src_mac_addr, l2_info->smac);
660 if (l2_info->num_vlans) {
661 encap->num_vlan_tags = l2_info->num_vlans;
662 encap->ovlan_tci = l2_info->inner_vlan_tci;
663 encap->ovlan_tpid = l2_info->inner_vlan_tpid;
666 encap_ipv4->ver_hlen = 4 << VXLAN_IPV4_HDR_VER_HLEN_VERSION_SFT;
667 encap_ipv4->ver_hlen |= 5 << VXLAN_IPV4_HDR_VER_HLEN_HEADER_LENGTH_SFT;
668 encap_ipv4->ttl = encap_key->ttl;
670 encap_ipv4->dest_ip_addr = encap_key->u.ipv4.dst;
671 encap_ipv4->src_ip_addr = encap_key->u.ipv4.src;
672 encap_ipv4->protocol = IPPROTO_UDP;
674 encap->dst_port = encap_key->tp_dst;
675 encap->vni = tunnel_id_to_key32(encap_key->tun_id);
677 mutex_lock(&bp->hwrm_cmd_lock);
678 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
680 *encap_record_handle = resp->encap_record_id;
682 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
683 mutex_unlock(&bp->hwrm_cmd_lock);
690 static int hwrm_cfa_encap_record_free(struct bnxt *bp,
691 __le32 encap_record_handle)
693 struct hwrm_cfa_encap_record_free_input req = { 0 };
696 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_FREE, -1, -1);
697 req.encap_record_id = encap_record_handle;
699 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
701 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
708 static int bnxt_tc_put_l2_node(struct bnxt *bp,
709 struct bnxt_tc_flow_node *flow_node)
711 struct bnxt_tc_l2_node *l2_node = flow_node->l2_node;
712 struct bnxt_tc_info *tc_info = bp->tc_info;
715 /* remove flow_node from the L2 shared flow list */
716 list_del(&flow_node->l2_list_node);
717 if (--l2_node->refcount == 0) {
718 rc = rhashtable_remove_fast(&tc_info->l2_table, &l2_node->node,
719 tc_info->l2_ht_params);
722 "Error: %s: rhashtable_remove_fast: %d",
724 kfree_rcu(l2_node, rcu);
729 static struct bnxt_tc_l2_node *
730 bnxt_tc_get_l2_node(struct bnxt *bp, struct rhashtable *l2_table,
731 struct rhashtable_params ht_params,
732 struct bnxt_tc_l2_key *l2_key)
734 struct bnxt_tc_l2_node *l2_node;
737 l2_node = rhashtable_lookup_fast(l2_table, l2_key, ht_params);
739 l2_node = kzalloc(sizeof(*l2_node), GFP_KERNEL);
745 l2_node->key = *l2_key;
746 rc = rhashtable_insert_fast(l2_table, &l2_node->node,
749 kfree_rcu(l2_node, rcu);
751 "Error: %s: rhashtable_insert_fast: %d",
755 INIT_LIST_HEAD(&l2_node->common_l2_flows);
760 /* Get the ref_flow_handle for a flow by checking if there are any other
761 * flows that share the same L2 key as this flow.
764 bnxt_tc_get_ref_flow_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
765 struct bnxt_tc_flow_node *flow_node,
766 __le16 *ref_flow_handle)
768 struct bnxt_tc_info *tc_info = bp->tc_info;
769 struct bnxt_tc_flow_node *ref_flow_node;
770 struct bnxt_tc_l2_node *l2_node;
772 l2_node = bnxt_tc_get_l2_node(bp, &tc_info->l2_table,
773 tc_info->l2_ht_params,
778 /* If any other flow is using this l2_node, use it's flow_handle
779 * as the ref_flow_handle
781 if (l2_node->refcount > 0) {
782 ref_flow_node = list_first_entry(&l2_node->common_l2_flows,
783 struct bnxt_tc_flow_node,
785 *ref_flow_handle = ref_flow_node->flow_handle;
787 *ref_flow_handle = cpu_to_le16(0xffff);
790 /* Insert the l2_node into the flow_node so that subsequent flows
791 * with a matching l2 key can use the flow_handle of this flow
792 * as their ref_flow_handle
794 flow_node->l2_node = l2_node;
795 list_add(&flow_node->l2_list_node, &l2_node->common_l2_flows);
800 /* After the flow parsing is done, this routine is used for checking
801 * if there are any aspects of the flow that prevent it from being
804 static bool bnxt_tc_can_offload(struct bnxt *bp, struct bnxt_tc_flow *flow)
806 /* If L4 ports are specified then ip_proto must be TCP or UDP */
807 if ((flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) &&
808 (flow->l4_key.ip_proto != IPPROTO_TCP &&
809 flow->l4_key.ip_proto != IPPROTO_UDP)) {
810 netdev_info(bp->dev, "Cannot offload non-TCP/UDP (%d) ports",
811 flow->l4_key.ip_proto);
815 /* Currently source/dest MAC cannot be partial wildcard */
816 if (bits_set(&flow->l2_key.smac, sizeof(flow->l2_key.smac)) &&
817 !is_exactmatch(flow->l2_mask.smac, sizeof(flow->l2_mask.smac))) {
818 netdev_info(bp->dev, "Wildcard match unsupported for Source MAC\n");
821 if (bits_set(&flow->l2_key.dmac, sizeof(flow->l2_key.dmac)) &&
822 !is_exactmatch(&flow->l2_mask.dmac, sizeof(flow->l2_mask.dmac))) {
823 netdev_info(bp->dev, "Wildcard match unsupported for Dest MAC\n");
827 /* Currently VLAN fields cannot be partial wildcard */
828 if (bits_set(&flow->l2_key.inner_vlan_tci,
829 sizeof(flow->l2_key.inner_vlan_tci)) &&
830 !is_vlan_tci_allowed(flow->l2_mask.inner_vlan_tci,
831 flow->l2_key.inner_vlan_tci)) {
832 netdev_info(bp->dev, "Unsupported VLAN TCI\n");
835 if (bits_set(&flow->l2_key.inner_vlan_tpid,
836 sizeof(flow->l2_key.inner_vlan_tpid)) &&
837 !is_exactmatch(&flow->l2_mask.inner_vlan_tpid,
838 sizeof(flow->l2_mask.inner_vlan_tpid))) {
839 netdev_info(bp->dev, "Wildcard match unsupported for VLAN TPID\n");
843 /* Currently Ethertype must be set */
844 if (!is_exactmatch(&flow->l2_mask.ether_type,
845 sizeof(flow->l2_mask.ether_type))) {
846 netdev_info(bp->dev, "Wildcard match unsupported for Ethertype\n");
853 /* Returns the final refcount of the node on success
854 * or a -ve error code on failure
856 static int bnxt_tc_put_tunnel_node(struct bnxt *bp,
857 struct rhashtable *tunnel_table,
858 struct rhashtable_params *ht_params,
859 struct bnxt_tc_tunnel_node *tunnel_node)
863 if (--tunnel_node->refcount == 0) {
864 rc = rhashtable_remove_fast(tunnel_table, &tunnel_node->node,
867 netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
870 kfree_rcu(tunnel_node, rcu);
873 return tunnel_node->refcount;
877 /* Get (or add) either encap or decap tunnel node from/to the supplied
880 static struct bnxt_tc_tunnel_node *
881 bnxt_tc_get_tunnel_node(struct bnxt *bp, struct rhashtable *tunnel_table,
882 struct rhashtable_params *ht_params,
883 struct ip_tunnel_key *tun_key)
885 struct bnxt_tc_tunnel_node *tunnel_node;
888 tunnel_node = rhashtable_lookup_fast(tunnel_table, tun_key, *ht_params);
890 tunnel_node = kzalloc(sizeof(*tunnel_node), GFP_KERNEL);
896 tunnel_node->key = *tun_key;
897 tunnel_node->tunnel_handle = INVALID_TUNNEL_HANDLE;
898 rc = rhashtable_insert_fast(tunnel_table, &tunnel_node->node,
901 kfree_rcu(tunnel_node, rcu);
905 tunnel_node->refcount++;
908 netdev_info(bp->dev, "error rc=%d", rc);
912 static int bnxt_tc_get_ref_decap_handle(struct bnxt *bp,
913 struct bnxt_tc_flow *flow,
914 struct bnxt_tc_l2_key *l2_key,
915 struct bnxt_tc_flow_node *flow_node,
916 __le32 *ref_decap_handle)
918 struct bnxt_tc_info *tc_info = bp->tc_info;
919 struct bnxt_tc_flow_node *ref_flow_node;
920 struct bnxt_tc_l2_node *decap_l2_node;
922 decap_l2_node = bnxt_tc_get_l2_node(bp, &tc_info->decap_l2_table,
923 tc_info->decap_l2_ht_params,
928 /* If any other flow is using this decap_l2_node, use it's decap_handle
929 * as the ref_decap_handle
931 if (decap_l2_node->refcount > 0) {
933 list_first_entry(&decap_l2_node->common_l2_flows,
934 struct bnxt_tc_flow_node,
936 *ref_decap_handle = ref_flow_node->decap_node->tunnel_handle;
938 *ref_decap_handle = INVALID_TUNNEL_HANDLE;
941 /* Insert the l2_node into the flow_node so that subsequent flows
942 * with a matching decap l2 key can use the decap_filter_handle of
943 * this flow as their ref_decap_handle
945 flow_node->decap_l2_node = decap_l2_node;
946 list_add(&flow_node->decap_l2_list_node,
947 &decap_l2_node->common_l2_flows);
948 decap_l2_node->refcount++;
952 static void bnxt_tc_put_decap_l2_node(struct bnxt *bp,
953 struct bnxt_tc_flow_node *flow_node)
955 struct bnxt_tc_l2_node *decap_l2_node = flow_node->decap_l2_node;
956 struct bnxt_tc_info *tc_info = bp->tc_info;
959 /* remove flow_node from the decap L2 sharing flow list */
960 list_del(&flow_node->decap_l2_list_node);
961 if (--decap_l2_node->refcount == 0) {
962 rc = rhashtable_remove_fast(&tc_info->decap_l2_table,
963 &decap_l2_node->node,
964 tc_info->decap_l2_ht_params);
966 netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
967 kfree_rcu(decap_l2_node, rcu);
971 static void bnxt_tc_put_decap_handle(struct bnxt *bp,
972 struct bnxt_tc_flow_node *flow_node)
974 __le32 decap_handle = flow_node->decap_node->tunnel_handle;
975 struct bnxt_tc_info *tc_info = bp->tc_info;
978 if (flow_node->decap_l2_node)
979 bnxt_tc_put_decap_l2_node(bp, flow_node);
981 rc = bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
982 &tc_info->decap_ht_params,
983 flow_node->decap_node);
984 if (!rc && decap_handle != INVALID_TUNNEL_HANDLE)
985 hwrm_cfa_decap_filter_free(bp, decap_handle);
988 static int bnxt_tc_resolve_tunnel_hdrs(struct bnxt *bp,
989 struct ip_tunnel_key *tun_key,
990 struct bnxt_tc_l2_key *l2_info)
993 struct net_device *real_dst_dev = bp->dev;
994 struct flowi4 flow = { {0} };
995 struct net_device *dst_dev;
996 struct neighbour *nbr;
1000 flow.flowi4_proto = IPPROTO_UDP;
1001 flow.fl4_dport = tun_key->tp_dst;
1002 flow.daddr = tun_key->u.ipv4.dst;
1004 rt = ip_route_output_key(dev_net(real_dst_dev), &flow);
1006 netdev_info(bp->dev, "no route to %pI4b", &flow.daddr);
1010 /* The route must either point to the real_dst_dev or a dst_dev that
1011 * uses the real_dst_dev.
1013 dst_dev = rt->dst.dev;
1014 if (is_vlan_dev(dst_dev)) {
1015 #if IS_ENABLED(CONFIG_VLAN_8021Q)
1016 struct vlan_dev_priv *vlan = vlan_dev_priv(dst_dev);
1018 if (vlan->real_dev != real_dst_dev) {
1019 netdev_info(bp->dev,
1020 "dst_dev(%s) doesn't use PF-if(%s)",
1021 netdev_name(dst_dev),
1022 netdev_name(real_dst_dev));
1026 l2_info->inner_vlan_tci = htons(vlan->vlan_id);
1027 l2_info->inner_vlan_tpid = vlan->vlan_proto;
1028 l2_info->num_vlans = 1;
1030 } else if (dst_dev != real_dst_dev) {
1031 netdev_info(bp->dev,
1032 "dst_dev(%s) for %pI4b is not PF-if(%s)",
1033 netdev_name(dst_dev), &flow.daddr,
1034 netdev_name(real_dst_dev));
1039 nbr = dst_neigh_lookup(&rt->dst, &flow.daddr);
1041 netdev_info(bp->dev, "can't lookup neighbor for %pI4b",
1047 tun_key->u.ipv4.src = flow.saddr;
1048 tun_key->ttl = ip4_dst_hoplimit(&rt->dst);
1049 neigh_ha_snapshot(l2_info->dmac, nbr, dst_dev);
1050 ether_addr_copy(l2_info->smac, dst_dev->dev_addr);
1063 static int bnxt_tc_get_decap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1064 struct bnxt_tc_flow_node *flow_node,
1065 __le32 *decap_filter_handle)
1067 struct ip_tunnel_key *decap_key = &flow->tun_key;
1068 struct bnxt_tc_info *tc_info = bp->tc_info;
1069 struct bnxt_tc_l2_key l2_info = { {0} };
1070 struct bnxt_tc_tunnel_node *decap_node;
1071 struct ip_tunnel_key tun_key = { 0 };
1072 struct bnxt_tc_l2_key *decap_l2_info;
1073 __le32 ref_decap_handle;
1076 /* Check if there's another flow using the same tunnel decap.
1077 * If not, add this tunnel to the table and resolve the other
1078 * tunnel header fileds. Ignore src_port in the tunnel_key,
1079 * since it is not required for decap filters.
1081 decap_key->tp_src = 0;
1082 decap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->decap_table,
1083 &tc_info->decap_ht_params,
1088 flow_node->decap_node = decap_node;
1090 if (decap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1093 /* Resolve the L2 fields for tunnel decap
1094 * Resolve the route for remote vtep (saddr) of the decap key
1095 * Find it's next-hop mac addrs
1097 tun_key.u.ipv4.dst = flow->tun_key.u.ipv4.src;
1098 tun_key.tp_dst = flow->tun_key.tp_dst;
1099 rc = bnxt_tc_resolve_tunnel_hdrs(bp, &tun_key, &l2_info);
1103 decap_l2_info = &decap_node->l2_info;
1104 /* decap smac is wildcarded */
1105 ether_addr_copy(decap_l2_info->dmac, l2_info.smac);
1106 if (l2_info.num_vlans) {
1107 decap_l2_info->num_vlans = l2_info.num_vlans;
1108 decap_l2_info->inner_vlan_tpid = l2_info.inner_vlan_tpid;
1109 decap_l2_info->inner_vlan_tci = l2_info.inner_vlan_tci;
1111 flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS;
1113 /* For getting a decap_filter_handle we first need to check if
1114 * there are any other decap flows that share the same tunnel L2
1115 * key and if so, pass that flow's decap_filter_handle as the
1116 * ref_decap_handle for this flow.
1118 rc = bnxt_tc_get_ref_decap_handle(bp, flow, decap_l2_info, flow_node,
1123 /* Issue the hwrm cmd to allocate a decap filter handle */
1124 rc = hwrm_cfa_decap_filter_alloc(bp, flow, decap_l2_info,
1126 &decap_node->tunnel_handle);
1131 *decap_filter_handle = decap_node->tunnel_handle;
1135 bnxt_tc_put_decap_l2_node(bp, flow_node);
1137 bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
1138 &tc_info->decap_ht_params,
1139 flow_node->decap_node);
1143 static void bnxt_tc_put_encap_handle(struct bnxt *bp,
1144 struct bnxt_tc_tunnel_node *encap_node)
1146 __le32 encap_handle = encap_node->tunnel_handle;
1147 struct bnxt_tc_info *tc_info = bp->tc_info;
1150 rc = bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1151 &tc_info->encap_ht_params, encap_node);
1152 if (!rc && encap_handle != INVALID_TUNNEL_HANDLE)
1153 hwrm_cfa_encap_record_free(bp, encap_handle);
1156 /* Lookup the tunnel encap table and check if there's an encap_handle
1158 * If not, query L2 info via a route lookup and issue an encap_record_alloc
1161 static int bnxt_tc_get_encap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1162 struct bnxt_tc_flow_node *flow_node,
1163 __le32 *encap_handle)
1165 struct ip_tunnel_key *encap_key = &flow->actions.tun_encap_key;
1166 struct bnxt_tc_info *tc_info = bp->tc_info;
1167 struct bnxt_tc_tunnel_node *encap_node;
1170 /* Check if there's another flow using the same tunnel encap.
1171 * If not, add this tunnel to the table and resolve the other
1172 * tunnel header fileds
1174 encap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->encap_table,
1175 &tc_info->encap_ht_params,
1180 flow_node->encap_node = encap_node;
1182 if (encap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1185 rc = bnxt_tc_resolve_tunnel_hdrs(bp, encap_key, &encap_node->l2_info);
1189 /* Allocate a new tunnel encap record */
1190 rc = hwrm_cfa_encap_record_alloc(bp, encap_key, &encap_node->l2_info,
1191 &encap_node->tunnel_handle);
1196 *encap_handle = encap_node->tunnel_handle;
1200 bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1201 &tc_info->encap_ht_params, encap_node);
1205 static void bnxt_tc_put_tunnel_handle(struct bnxt *bp,
1206 struct bnxt_tc_flow *flow,
1207 struct bnxt_tc_flow_node *flow_node)
1209 if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1210 bnxt_tc_put_decap_handle(bp, flow_node);
1211 else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1212 bnxt_tc_put_encap_handle(bp, flow_node->encap_node);
1215 static int bnxt_tc_get_tunnel_handle(struct bnxt *bp,
1216 struct bnxt_tc_flow *flow,
1217 struct bnxt_tc_flow_node *flow_node,
1218 __le32 *tunnel_handle)
1220 if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1221 return bnxt_tc_get_decap_handle(bp, flow, flow_node,
1223 else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1224 return bnxt_tc_get_encap_handle(bp, flow, flow_node,
1229 static int __bnxt_tc_del_flow(struct bnxt *bp,
1230 struct bnxt_tc_flow_node *flow_node)
1232 struct bnxt_tc_info *tc_info = bp->tc_info;
1235 /* send HWRM cmd to free the flow-id */
1236 bnxt_hwrm_cfa_flow_free(bp, flow_node->flow_handle);
1238 mutex_lock(&tc_info->lock);
1240 /* release references to any tunnel encap/decap nodes */
1241 bnxt_tc_put_tunnel_handle(bp, &flow_node->flow, flow_node);
1243 /* release reference to l2 node */
1244 bnxt_tc_put_l2_node(bp, flow_node);
1246 mutex_unlock(&tc_info->lock);
1248 rc = rhashtable_remove_fast(&tc_info->flow_table, &flow_node->node,
1249 tc_info->flow_ht_params);
1251 netdev_err(bp->dev, "Error: %s: rhashtable_remove_fast rc=%d",
1254 kfree_rcu(flow_node, rcu);
1258 static void bnxt_tc_set_src_fid(struct bnxt *bp, struct bnxt_tc_flow *flow,
1261 if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1262 flow->src_fid = bp->pf.fw_fid;
1264 flow->src_fid = src_fid;
1267 /* Add a new flow or replace an existing flow.
1269 * There are essentially two critical sections here.
1270 * 1. while adding a new flow
1272 * b) issue HWRM cmd and get flow_handle
1273 * c) link l2-key with flow
1274 * 2. while deleting a flow
1275 * a) unlinking l2-key from flow
1276 * A lock is needed to protect these two critical sections.
1278 * The hash-tables are already protected by the rhashtable API.
1280 static int bnxt_tc_add_flow(struct bnxt *bp, u16 src_fid,
1281 struct tc_cls_flower_offload *tc_flow_cmd)
1283 struct bnxt_tc_flow_node *new_node, *old_node;
1284 struct bnxt_tc_info *tc_info = bp->tc_info;
1285 struct bnxt_tc_flow *flow;
1286 __le32 tunnel_handle = 0;
1287 __le16 ref_flow_handle;
1290 /* allocate memory for the new flow and it's node */
1291 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
1296 new_node->cookie = tc_flow_cmd->cookie;
1297 flow = &new_node->flow;
1299 rc = bnxt_tc_parse_flow(bp, tc_flow_cmd, flow);
1303 bnxt_tc_set_src_fid(bp, flow, src_fid);
1305 if (!bnxt_tc_can_offload(bp, flow)) {
1310 /* If a flow exists with the same cookie, delete it */
1311 old_node = rhashtable_lookup_fast(&tc_info->flow_table,
1312 &tc_flow_cmd->cookie,
1313 tc_info->flow_ht_params);
1315 __bnxt_tc_del_flow(bp, old_node);
1317 /* Check if the L2 part of the flow has been offloaded already.
1318 * If so, bump up it's refcnt and get it's reference handle.
1320 mutex_lock(&tc_info->lock);
1321 rc = bnxt_tc_get_ref_flow_handle(bp, flow, new_node, &ref_flow_handle);
1325 /* If the flow involves tunnel encap/decap, get tunnel_handle */
1326 rc = bnxt_tc_get_tunnel_handle(bp, flow, new_node, &tunnel_handle);
1330 /* send HWRM cmd to alloc the flow */
1331 rc = bnxt_hwrm_cfa_flow_alloc(bp, flow, ref_flow_handle,
1332 tunnel_handle, &new_node->flow_handle);
1336 flow->lastused = jiffies;
1337 spin_lock_init(&flow->stats_lock);
1338 /* add new flow to flow-table */
1339 rc = rhashtable_insert_fast(&tc_info->flow_table, &new_node->node,
1340 tc_info->flow_ht_params);
1342 goto hwrm_flow_free;
1344 mutex_unlock(&tc_info->lock);
1348 bnxt_hwrm_cfa_flow_free(bp, new_node->flow_handle);
1350 bnxt_tc_put_tunnel_handle(bp, flow, new_node);
1352 bnxt_tc_put_l2_node(bp, new_node);
1354 mutex_unlock(&tc_info->lock);
1356 kfree_rcu(new_node, rcu);
1358 netdev_err(bp->dev, "Error: %s: cookie=0x%lx error=%d",
1359 __func__, tc_flow_cmd->cookie, rc);
1363 static int bnxt_tc_del_flow(struct bnxt *bp,
1364 struct tc_cls_flower_offload *tc_flow_cmd)
1366 struct bnxt_tc_info *tc_info = bp->tc_info;
1367 struct bnxt_tc_flow_node *flow_node;
1369 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1370 &tc_flow_cmd->cookie,
1371 tc_info->flow_ht_params);
1375 return __bnxt_tc_del_flow(bp, flow_node);
1378 static int bnxt_tc_get_flow_stats(struct bnxt *bp,
1379 struct tc_cls_flower_offload *tc_flow_cmd)
1381 struct bnxt_tc_flow_stats stats, *curr_stats, *prev_stats;
1382 struct bnxt_tc_info *tc_info = bp->tc_info;
1383 struct bnxt_tc_flow_node *flow_node;
1384 struct bnxt_tc_flow *flow;
1385 unsigned long lastused;
1387 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1388 &tc_flow_cmd->cookie,
1389 tc_info->flow_ht_params);
1393 flow = &flow_node->flow;
1394 curr_stats = &flow->stats;
1395 prev_stats = &flow->prev_stats;
1397 spin_lock(&flow->stats_lock);
1398 stats.packets = curr_stats->packets - prev_stats->packets;
1399 stats.bytes = curr_stats->bytes - prev_stats->bytes;
1400 *prev_stats = *curr_stats;
1401 lastused = flow->lastused;
1402 spin_unlock(&flow->stats_lock);
1404 tcf_exts_stats_update(tc_flow_cmd->exts, stats.bytes, stats.packets,
1410 bnxt_hwrm_cfa_flow_stats_get(struct bnxt *bp, int num_flows,
1411 struct bnxt_tc_stats_batch stats_batch[])
1413 struct hwrm_cfa_flow_stats_output *resp = bp->hwrm_cmd_resp_addr;
1414 struct hwrm_cfa_flow_stats_input req = { 0 };
1415 __le16 *req_flow_handles = &req.flow_handle_0;
1418 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_STATS, -1, -1);
1419 req.num_flows = cpu_to_le16(num_flows);
1420 for (i = 0; i < num_flows; i++) {
1421 struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1423 req_flow_handles[i] = flow_node->flow_handle;
1426 mutex_lock(&bp->hwrm_cmd_lock);
1427 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1429 __le64 *resp_packets = &resp->packet_0;
1430 __le64 *resp_bytes = &resp->byte_0;
1432 for (i = 0; i < num_flows; i++) {
1433 stats_batch[i].hw_stats.packets =
1434 le64_to_cpu(resp_packets[i]);
1435 stats_batch[i].hw_stats.bytes =
1436 le64_to_cpu(resp_bytes[i]);
1439 netdev_info(bp->dev, "error rc=%d", rc);
1441 mutex_unlock(&bp->hwrm_cmd_lock);
1448 /* Add val to accum while handling a possible wraparound
1449 * of val. Eventhough val is of type u64, its actual width
1450 * is denoted by mask and will wrap-around beyond that width.
1452 static void accumulate_val(u64 *accum, u64 val, u64 mask)
1454 #define low_bits(x, mask) ((x) & (mask))
1455 #define high_bits(x, mask) ((x) & ~(mask))
1456 bool wrapped = val < low_bits(*accum, mask);
1458 *accum = high_bits(*accum, mask) + val;
1460 *accum += (mask + 1);
1463 /* The HW counters' width is much less than 64bits.
1464 * Handle possible wrap-around while updating the stat counters
1466 static void bnxt_flow_stats_accum(struct bnxt_tc_info *tc_info,
1467 struct bnxt_tc_flow_stats *acc_stats,
1468 struct bnxt_tc_flow_stats *hw_stats)
1470 accumulate_val(&acc_stats->bytes, hw_stats->bytes, tc_info->bytes_mask);
1471 accumulate_val(&acc_stats->packets, hw_stats->packets,
1472 tc_info->packets_mask);
1476 bnxt_tc_flow_stats_batch_update(struct bnxt *bp, int num_flows,
1477 struct bnxt_tc_stats_batch stats_batch[])
1479 struct bnxt_tc_info *tc_info = bp->tc_info;
1482 rc = bnxt_hwrm_cfa_flow_stats_get(bp, num_flows, stats_batch);
1486 for (i = 0; i < num_flows; i++) {
1487 struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1488 struct bnxt_tc_flow *flow = &flow_node->flow;
1490 spin_lock(&flow->stats_lock);
1491 bnxt_flow_stats_accum(tc_info, &flow->stats,
1492 &stats_batch[i].hw_stats);
1493 if (flow->stats.packets != flow->prev_stats.packets)
1494 flow->lastused = jiffies;
1495 spin_unlock(&flow->stats_lock);
1502 bnxt_tc_flow_stats_batch_prep(struct bnxt *bp,
1503 struct bnxt_tc_stats_batch stats_batch[],
1506 struct bnxt_tc_info *tc_info = bp->tc_info;
1507 struct rhashtable_iter *iter = &tc_info->iter;
1511 rhashtable_walk_start(iter);
1514 for (i = 0; i < BNXT_FLOW_STATS_BATCH_MAX; i++) {
1515 flow_node = rhashtable_walk_next(iter);
1516 if (IS_ERR(flow_node)) {
1518 if (PTR_ERR(flow_node) == -EAGAIN) {
1521 rc = PTR_ERR(flow_node);
1530 stats_batch[i].flow_node = flow_node;
1533 rhashtable_walk_stop(iter);
1538 void bnxt_tc_flow_stats_work(struct bnxt *bp)
1540 struct bnxt_tc_info *tc_info = bp->tc_info;
1543 num_flows = atomic_read(&tc_info->flow_table.nelems);
1547 rhashtable_walk_enter(&tc_info->flow_table, &tc_info->iter);
1550 rc = bnxt_tc_flow_stats_batch_prep(bp, tc_info->stats_batch,
1561 bnxt_tc_flow_stats_batch_update(bp, num_flows,
1562 tc_info->stats_batch);
1565 rhashtable_walk_exit(&tc_info->iter);
1568 int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
1569 struct tc_cls_flower_offload *cls_flower)
1573 switch (cls_flower->command) {
1574 case TC_CLSFLOWER_REPLACE:
1575 rc = bnxt_tc_add_flow(bp, src_fid, cls_flower);
1578 case TC_CLSFLOWER_DESTROY:
1579 rc = bnxt_tc_del_flow(bp, cls_flower);
1582 case TC_CLSFLOWER_STATS:
1583 rc = bnxt_tc_get_flow_stats(bp, cls_flower);
1589 static const struct rhashtable_params bnxt_tc_flow_ht_params = {
1590 .head_offset = offsetof(struct bnxt_tc_flow_node, node),
1591 .key_offset = offsetof(struct bnxt_tc_flow_node, cookie),
1592 .key_len = sizeof(((struct bnxt_tc_flow_node *)0)->cookie),
1593 .automatic_shrinking = true
1596 static const struct rhashtable_params bnxt_tc_l2_ht_params = {
1597 .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1598 .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1599 .key_len = BNXT_TC_L2_KEY_LEN,
1600 .automatic_shrinking = true
1603 static const struct rhashtable_params bnxt_tc_decap_l2_ht_params = {
1604 .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1605 .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1606 .key_len = BNXT_TC_L2_KEY_LEN,
1607 .automatic_shrinking = true
1610 static const struct rhashtable_params bnxt_tc_tunnel_ht_params = {
1611 .head_offset = offsetof(struct bnxt_tc_tunnel_node, node),
1612 .key_offset = offsetof(struct bnxt_tc_tunnel_node, key),
1613 .key_len = sizeof(struct ip_tunnel_key),
1614 .automatic_shrinking = true
1617 /* convert counter width in bits to a mask */
1618 #define mask(width) ((u64)~0 >> (64 - (width)))
1620 int bnxt_init_tc(struct bnxt *bp)
1622 struct bnxt_tc_info *tc_info;
1625 if (bp->hwrm_spec_code < 0x10803) {
1626 netdev_warn(bp->dev,
1627 "Firmware does not support TC flower offload.\n");
1631 tc_info = kzalloc(sizeof(*tc_info), GFP_KERNEL);
1634 mutex_init(&tc_info->lock);
1636 /* Counter widths are programmed by FW */
1637 tc_info->bytes_mask = mask(36);
1638 tc_info->packets_mask = mask(28);
1640 tc_info->flow_ht_params = bnxt_tc_flow_ht_params;
1641 rc = rhashtable_init(&tc_info->flow_table, &tc_info->flow_ht_params);
1645 tc_info->l2_ht_params = bnxt_tc_l2_ht_params;
1646 rc = rhashtable_init(&tc_info->l2_table, &tc_info->l2_ht_params);
1648 goto destroy_flow_table;
1650 tc_info->decap_l2_ht_params = bnxt_tc_decap_l2_ht_params;
1651 rc = rhashtable_init(&tc_info->decap_l2_table,
1652 &tc_info->decap_l2_ht_params);
1654 goto destroy_l2_table;
1656 tc_info->decap_ht_params = bnxt_tc_tunnel_ht_params;
1657 rc = rhashtable_init(&tc_info->decap_table,
1658 &tc_info->decap_ht_params);
1660 goto destroy_decap_l2_table;
1662 tc_info->encap_ht_params = bnxt_tc_tunnel_ht_params;
1663 rc = rhashtable_init(&tc_info->encap_table,
1664 &tc_info->encap_ht_params);
1666 goto destroy_decap_table;
1668 tc_info->enabled = true;
1669 bp->dev->hw_features |= NETIF_F_HW_TC;
1670 bp->dev->features |= NETIF_F_HW_TC;
1671 bp->tc_info = tc_info;
1674 destroy_decap_table:
1675 rhashtable_destroy(&tc_info->decap_table);
1676 destroy_decap_l2_table:
1677 rhashtable_destroy(&tc_info->decap_l2_table);
1679 rhashtable_destroy(&tc_info->l2_table);
1681 rhashtable_destroy(&tc_info->flow_table);
1687 void bnxt_shutdown_tc(struct bnxt *bp)
1689 struct bnxt_tc_info *tc_info = bp->tc_info;
1691 if (!bnxt_tc_flower_enabled(bp))
1694 rhashtable_destroy(&tc_info->flow_table);
1695 rhashtable_destroy(&tc_info->l2_table);
1696 rhashtable_destroy(&tc_info->decap_l2_table);
1697 rhashtable_destroy(&tc_info->decap_table);
1698 rhashtable_destroy(&tc_info->encap_table);