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