blob: 96bff48af971ef072f99932417ad1c218df23b34 [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 Perla8c95f772017-10-26 11:51:29 -0400507static int hwrm_cfa_decap_filter_alloc(struct bnxt *bp,
508 struct bnxt_tc_flow *flow,
509 struct bnxt_tc_l2_key *l2_info,
510 __le32 ref_decap_handle,
511 __le32 *decap_filter_handle)
512{
Sathya Perlaf484f672017-10-26 11:51:30 -0400513 struct hwrm_cfa_decap_filter_alloc_output *resp =
514 bp->hwrm_cmd_resp_addr;
515 struct hwrm_cfa_decap_filter_alloc_input req = { 0 };
516 struct ip_tunnel_key *tun_key = &flow->tun_key;
517 u32 enables = 0;
518 int rc;
519
520 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_ALLOC, -1, -1);
521
522 req.flags = cpu_to_le32(CFA_DECAP_FILTER_ALLOC_REQ_FLAGS_OVS_TUNNEL);
523 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE |
524 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL;
525 req.tunnel_type = CFA_DECAP_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
526 req.ip_protocol = CFA_DECAP_FILTER_ALLOC_REQ_IP_PROTOCOL_UDP;
527
528 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ID) {
529 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_TUNNEL_ID;
530 /* tunnel_id is wrongly defined in hsi defn. as __le32 */
531 req.tunnel_id = tunnel_id_to_key32(tun_key->tun_id);
532 }
533
534 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS) {
Sunil Challac8fb7b82017-12-01 03:13:03 -0500535 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_MACADDR;
Sathya Perlaf484f672017-10-26 11:51:30 -0400536 ether_addr_copy(req.dst_macaddr, l2_info->dmac);
Sathya Perlaf484f672017-10-26 11:51:30 -0400537 }
538 if (l2_info->num_vlans) {
539 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_T_IVLAN_VID;
540 req.t_ivlan_vid = l2_info->inner_vlan_tci;
541 }
542
543 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE;
544 req.ethertype = htons(ETH_P_IP);
545
546 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS) {
547 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |
548 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |
549 CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE;
550 req.ip_addr_type = CFA_DECAP_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
551 req.dst_ipaddr[0] = tun_key->u.ipv4.dst;
552 req.src_ipaddr[0] = tun_key->u.ipv4.src;
553 }
554
555 if (flow->flags & BNXT_TC_FLOW_FLAGS_TUNL_PORTS) {
556 enables |= CFA_DECAP_FILTER_ALLOC_REQ_ENABLES_DST_PORT;
557 req.dst_port = tun_key->tp_dst;
558 }
559
560 /* Eventhough the decap_handle returned by hwrm_cfa_decap_filter_alloc
561 * is defined as __le32, l2_ctxt_ref_id is defined in HSI as __le16.
562 */
563 req.l2_ctxt_ref_id = (__force __le16)ref_decap_handle;
564 req.enables = cpu_to_le32(enables);
565
566 mutex_lock(&bp->hwrm_cmd_lock);
567 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
568 if (!rc)
569 *decap_filter_handle = resp->decap_filter_id;
570 else
571 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
572 mutex_unlock(&bp->hwrm_cmd_lock);
573
574 return rc;
Sathya Perla8c95f772017-10-26 11:51:29 -0400575}
576
577static int hwrm_cfa_decap_filter_free(struct bnxt *bp,
578 __le32 decap_filter_handle)
579{
Sathya Perlaf484f672017-10-26 11:51:30 -0400580 struct hwrm_cfa_decap_filter_free_input req = { 0 };
581 int rc;
582
583 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_DECAP_FILTER_FREE, -1, -1);
584 req.decap_filter_id = decap_filter_handle;
585
586 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
587 if (rc)
588 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
589 return rc;
Sathya Perla8c95f772017-10-26 11:51:29 -0400590}
591
592static int hwrm_cfa_encap_record_alloc(struct bnxt *bp,
593 struct ip_tunnel_key *encap_key,
594 struct bnxt_tc_l2_key *l2_info,
595 __le32 *encap_record_handle)
596{
Sathya Perlaf484f672017-10-26 11:51:30 -0400597 struct hwrm_cfa_encap_record_alloc_output *resp =
598 bp->hwrm_cmd_resp_addr;
599 struct hwrm_cfa_encap_record_alloc_input req = { 0 };
600 struct hwrm_cfa_encap_data_vxlan *encap =
601 (struct hwrm_cfa_encap_data_vxlan *)&req.encap_data;
602 struct hwrm_vxlan_ipv4_hdr *encap_ipv4 =
603 (struct hwrm_vxlan_ipv4_hdr *)encap->l3;
604 int rc;
605
606 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_ALLOC, -1, -1);
607
608 req.encap_type = CFA_ENCAP_RECORD_ALLOC_REQ_ENCAP_TYPE_VXLAN;
609
610 ether_addr_copy(encap->dst_mac_addr, l2_info->dmac);
611 ether_addr_copy(encap->src_mac_addr, l2_info->smac);
612 if (l2_info->num_vlans) {
613 encap->num_vlan_tags = l2_info->num_vlans;
614 encap->ovlan_tci = l2_info->inner_vlan_tci;
615 encap->ovlan_tpid = l2_info->inner_vlan_tpid;
616 }
617
618 encap_ipv4->ver_hlen = 4 << VXLAN_IPV4_HDR_VER_HLEN_VERSION_SFT;
619 encap_ipv4->ver_hlen |= 5 << VXLAN_IPV4_HDR_VER_HLEN_HEADER_LENGTH_SFT;
620 encap_ipv4->ttl = encap_key->ttl;
621
622 encap_ipv4->dest_ip_addr = encap_key->u.ipv4.dst;
623 encap_ipv4->src_ip_addr = encap_key->u.ipv4.src;
624 encap_ipv4->protocol = IPPROTO_UDP;
625
626 encap->dst_port = encap_key->tp_dst;
627 encap->vni = tunnel_id_to_key32(encap_key->tun_id);
628
629 mutex_lock(&bp->hwrm_cmd_lock);
630 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
631 if (!rc)
632 *encap_record_handle = resp->encap_record_id;
633 else
634 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
635 mutex_unlock(&bp->hwrm_cmd_lock);
636
637 return rc;
Sathya Perla8c95f772017-10-26 11:51:29 -0400638}
639
640static int hwrm_cfa_encap_record_free(struct bnxt *bp,
641 __le32 encap_record_handle)
642{
Sathya Perlaf484f672017-10-26 11:51:30 -0400643 struct hwrm_cfa_encap_record_free_input req = { 0 };
644 int rc;
645
646 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ENCAP_RECORD_FREE, -1, -1);
647 req.encap_record_id = encap_record_handle;
648
649 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
650 if (rc)
651 netdev_info(bp->dev, "%s: Error rc=%d", __func__, rc);
652 return rc;
Sathya Perla8c95f772017-10-26 11:51:29 -0400653}
654
Sathya Perla2ae74082017-08-28 13:40:33 -0400655static int bnxt_tc_put_l2_node(struct bnxt *bp,
656 struct bnxt_tc_flow_node *flow_node)
657{
658 struct bnxt_tc_l2_node *l2_node = flow_node->l2_node;
Sathya Perlacd663582017-10-26 11:51:32 -0400659 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -0400660 int rc;
661
662 /* remove flow_node from the L2 shared flow list */
663 list_del(&flow_node->l2_list_node);
664 if (--l2_node->refcount == 0) {
665 rc = rhashtable_remove_fast(&tc_info->l2_table, &l2_node->node,
666 tc_info->l2_ht_params);
667 if (rc)
668 netdev_err(bp->dev,
669 "Error: %s: rhashtable_remove_fast: %d",
670 __func__, rc);
671 kfree_rcu(l2_node, rcu);
672 }
673 return 0;
674}
675
676static struct bnxt_tc_l2_node *
677bnxt_tc_get_l2_node(struct bnxt *bp, struct rhashtable *l2_table,
678 struct rhashtable_params ht_params,
679 struct bnxt_tc_l2_key *l2_key)
680{
681 struct bnxt_tc_l2_node *l2_node;
682 int rc;
683
684 l2_node = rhashtable_lookup_fast(l2_table, l2_key, ht_params);
685 if (!l2_node) {
686 l2_node = kzalloc(sizeof(*l2_node), GFP_KERNEL);
687 if (!l2_node) {
688 rc = -ENOMEM;
689 return NULL;
690 }
691
692 l2_node->key = *l2_key;
693 rc = rhashtable_insert_fast(l2_table, &l2_node->node,
694 ht_params);
695 if (rc) {
Sathya Perla8c95f772017-10-26 11:51:29 -0400696 kfree_rcu(l2_node, rcu);
Sathya Perla2ae74082017-08-28 13:40:33 -0400697 netdev_err(bp->dev,
698 "Error: %s: rhashtable_insert_fast: %d",
699 __func__, rc);
700 return NULL;
701 }
702 INIT_LIST_HEAD(&l2_node->common_l2_flows);
703 }
704 return l2_node;
705}
706
707/* Get the ref_flow_handle for a flow by checking if there are any other
708 * flows that share the same L2 key as this flow.
709 */
710static int
711bnxt_tc_get_ref_flow_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
712 struct bnxt_tc_flow_node *flow_node,
713 __le16 *ref_flow_handle)
714{
Sathya Perlacd663582017-10-26 11:51:32 -0400715 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -0400716 struct bnxt_tc_flow_node *ref_flow_node;
717 struct bnxt_tc_l2_node *l2_node;
718
719 l2_node = bnxt_tc_get_l2_node(bp, &tc_info->l2_table,
720 tc_info->l2_ht_params,
721 &flow->l2_key);
722 if (!l2_node)
723 return -1;
724
725 /* If any other flow is using this l2_node, use it's flow_handle
726 * as the ref_flow_handle
727 */
728 if (l2_node->refcount > 0) {
729 ref_flow_node = list_first_entry(&l2_node->common_l2_flows,
730 struct bnxt_tc_flow_node,
731 l2_list_node);
732 *ref_flow_handle = ref_flow_node->flow_handle;
733 } else {
734 *ref_flow_handle = cpu_to_le16(0xffff);
735 }
736
737 /* Insert the l2_node into the flow_node so that subsequent flows
738 * with a matching l2 key can use the flow_handle of this flow
739 * as their ref_flow_handle
740 */
741 flow_node->l2_node = l2_node;
742 list_add(&flow_node->l2_list_node, &l2_node->common_l2_flows);
743 l2_node->refcount++;
744 return 0;
745}
746
747/* After the flow parsing is done, this routine is used for checking
748 * if there are any aspects of the flow that prevent it from being
749 * offloaded.
750 */
751static bool bnxt_tc_can_offload(struct bnxt *bp, struct bnxt_tc_flow *flow)
752{
753 /* If L4 ports are specified then ip_proto must be TCP or UDP */
754 if ((flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) &&
755 (flow->l4_key.ip_proto != IPPROTO_TCP &&
756 flow->l4_key.ip_proto != IPPROTO_UDP)) {
757 netdev_info(bp->dev, "Cannot offload non-TCP/UDP (%d) ports",
758 flow->l4_key.ip_proto);
759 return false;
760 }
761
762 return true;
763}
764
Sathya Perla8c95f772017-10-26 11:51:29 -0400765/* Returns the final refcount of the node on success
766 * or a -ve error code on failure
767 */
768static int bnxt_tc_put_tunnel_node(struct bnxt *bp,
769 struct rhashtable *tunnel_table,
770 struct rhashtable_params *ht_params,
771 struct bnxt_tc_tunnel_node *tunnel_node)
772{
773 int rc;
774
775 if (--tunnel_node->refcount == 0) {
776 rc = rhashtable_remove_fast(tunnel_table, &tunnel_node->node,
777 *ht_params);
778 if (rc) {
779 netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
780 rc = -1;
781 }
782 kfree_rcu(tunnel_node, rcu);
783 return rc;
784 } else {
785 return tunnel_node->refcount;
786 }
787}
788
789/* Get (or add) either encap or decap tunnel node from/to the supplied
790 * hash table.
791 */
792static struct bnxt_tc_tunnel_node *
793bnxt_tc_get_tunnel_node(struct bnxt *bp, struct rhashtable *tunnel_table,
794 struct rhashtable_params *ht_params,
795 struct ip_tunnel_key *tun_key)
796{
797 struct bnxt_tc_tunnel_node *tunnel_node;
798 int rc;
799
800 tunnel_node = rhashtable_lookup_fast(tunnel_table, tun_key, *ht_params);
801 if (!tunnel_node) {
802 tunnel_node = kzalloc(sizeof(*tunnel_node), GFP_KERNEL);
803 if (!tunnel_node) {
804 rc = -ENOMEM;
805 goto err;
806 }
807
808 tunnel_node->key = *tun_key;
809 tunnel_node->tunnel_handle = INVALID_TUNNEL_HANDLE;
810 rc = rhashtable_insert_fast(tunnel_table, &tunnel_node->node,
811 *ht_params);
812 if (rc) {
813 kfree_rcu(tunnel_node, rcu);
814 goto err;
815 }
816 }
817 tunnel_node->refcount++;
818 return tunnel_node;
819err:
820 netdev_info(bp->dev, "error rc=%d", rc);
821 return NULL;
822}
823
824static int bnxt_tc_get_ref_decap_handle(struct bnxt *bp,
825 struct bnxt_tc_flow *flow,
826 struct bnxt_tc_l2_key *l2_key,
827 struct bnxt_tc_flow_node *flow_node,
828 __le32 *ref_decap_handle)
829{
Sathya Perlacd663582017-10-26 11:51:32 -0400830 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -0400831 struct bnxt_tc_flow_node *ref_flow_node;
832 struct bnxt_tc_l2_node *decap_l2_node;
833
834 decap_l2_node = bnxt_tc_get_l2_node(bp, &tc_info->decap_l2_table,
835 tc_info->decap_l2_ht_params,
836 l2_key);
837 if (!decap_l2_node)
838 return -1;
839
840 /* If any other flow is using this decap_l2_node, use it's decap_handle
841 * as the ref_decap_handle
842 */
843 if (decap_l2_node->refcount > 0) {
844 ref_flow_node =
845 list_first_entry(&decap_l2_node->common_l2_flows,
846 struct bnxt_tc_flow_node,
847 decap_l2_list_node);
848 *ref_decap_handle = ref_flow_node->decap_node->tunnel_handle;
849 } else {
850 *ref_decap_handle = INVALID_TUNNEL_HANDLE;
851 }
852
853 /* Insert the l2_node into the flow_node so that subsequent flows
854 * with a matching decap l2 key can use the decap_filter_handle of
855 * this flow as their ref_decap_handle
856 */
857 flow_node->decap_l2_node = decap_l2_node;
858 list_add(&flow_node->decap_l2_list_node,
859 &decap_l2_node->common_l2_flows);
860 decap_l2_node->refcount++;
861 return 0;
862}
863
864static void bnxt_tc_put_decap_l2_node(struct bnxt *bp,
865 struct bnxt_tc_flow_node *flow_node)
866{
867 struct bnxt_tc_l2_node *decap_l2_node = flow_node->decap_l2_node;
Sathya Perlacd663582017-10-26 11:51:32 -0400868 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -0400869 int rc;
870
871 /* remove flow_node from the decap L2 sharing flow list */
872 list_del(&flow_node->decap_l2_list_node);
873 if (--decap_l2_node->refcount == 0) {
874 rc = rhashtable_remove_fast(&tc_info->decap_l2_table,
875 &decap_l2_node->node,
876 tc_info->decap_l2_ht_params);
877 if (rc)
878 netdev_err(bp->dev, "rhashtable_remove_fast rc=%d", rc);
879 kfree_rcu(decap_l2_node, rcu);
880 }
881}
882
883static void bnxt_tc_put_decap_handle(struct bnxt *bp,
884 struct bnxt_tc_flow_node *flow_node)
885{
886 __le32 decap_handle = flow_node->decap_node->tunnel_handle;
Sathya Perlacd663582017-10-26 11:51:32 -0400887 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -0400888 int rc;
889
890 if (flow_node->decap_l2_node)
891 bnxt_tc_put_decap_l2_node(bp, flow_node);
892
893 rc = bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
894 &tc_info->decap_ht_params,
895 flow_node->decap_node);
896 if (!rc && decap_handle != INVALID_TUNNEL_HANDLE)
897 hwrm_cfa_decap_filter_free(bp, decap_handle);
898}
899
900static int bnxt_tc_resolve_tunnel_hdrs(struct bnxt *bp,
901 struct ip_tunnel_key *tun_key,
902 struct bnxt_tc_l2_key *l2_info,
903 struct net_device *real_dst_dev)
904{
Michael Chan952c5712017-10-28 01:56:10 -0400905#ifdef CONFIG_INET
Sathya Perla8c95f772017-10-26 11:51:29 -0400906 struct flowi4 flow = { {0} };
907 struct net_device *dst_dev;
908 struct neighbour *nbr;
909 struct rtable *rt;
910 int rc;
911
912 flow.flowi4_proto = IPPROTO_UDP;
913 flow.fl4_dport = tun_key->tp_dst;
914 flow.daddr = tun_key->u.ipv4.dst;
915
916 rt = ip_route_output_key(dev_net(real_dst_dev), &flow);
917 if (IS_ERR(rt)) {
918 netdev_info(bp->dev, "no route to %pI4b", &flow.daddr);
919 return -EOPNOTSUPP;
920 }
921
922 /* The route must either point to the real_dst_dev or a dst_dev that
923 * uses the real_dst_dev.
924 */
925 dst_dev = rt->dst.dev;
926 if (is_vlan_dev(dst_dev)) {
Michael Chan952c5712017-10-28 01:56:10 -0400927#if IS_ENABLED(CONFIG_VLAN_8021Q)
Sathya Perla8c95f772017-10-26 11:51:29 -0400928 struct vlan_dev_priv *vlan = vlan_dev_priv(dst_dev);
929
930 if (vlan->real_dev != real_dst_dev) {
931 netdev_info(bp->dev,
932 "dst_dev(%s) doesn't use PF-if(%s)",
933 netdev_name(dst_dev),
934 netdev_name(real_dst_dev));
935 rc = -EOPNOTSUPP;
936 goto put_rt;
937 }
938 l2_info->inner_vlan_tci = htons(vlan->vlan_id);
939 l2_info->inner_vlan_tpid = vlan->vlan_proto;
940 l2_info->num_vlans = 1;
Michael Chan952c5712017-10-28 01:56:10 -0400941#endif
Sathya Perla8c95f772017-10-26 11:51:29 -0400942 } else if (dst_dev != real_dst_dev) {
943 netdev_info(bp->dev,
944 "dst_dev(%s) for %pI4b is not PF-if(%s)",
945 netdev_name(dst_dev), &flow.daddr,
946 netdev_name(real_dst_dev));
947 rc = -EOPNOTSUPP;
948 goto put_rt;
949 }
950
951 nbr = dst_neigh_lookup(&rt->dst, &flow.daddr);
952 if (!nbr) {
953 netdev_info(bp->dev, "can't lookup neighbor for %pI4b",
954 &flow.daddr);
955 rc = -EOPNOTSUPP;
956 goto put_rt;
957 }
958
959 tun_key->u.ipv4.src = flow.saddr;
960 tun_key->ttl = ip4_dst_hoplimit(&rt->dst);
961 neigh_ha_snapshot(l2_info->dmac, nbr, dst_dev);
962 ether_addr_copy(l2_info->smac, dst_dev->dev_addr);
963 neigh_release(nbr);
964 ip_rt_put(rt);
965
966 return 0;
967put_rt:
968 ip_rt_put(rt);
969 return rc;
Michael Chan952c5712017-10-28 01:56:10 -0400970#else
971 return -EOPNOTSUPP;
972#endif
Sathya Perla8c95f772017-10-26 11:51:29 -0400973}
974
975static int bnxt_tc_get_decap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
976 struct bnxt_tc_flow_node *flow_node,
977 __le32 *decap_filter_handle)
978{
979 struct ip_tunnel_key *decap_key = &flow->tun_key;
Sathya Perlacd663582017-10-26 11:51:32 -0400980 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -0400981 struct bnxt_tc_l2_key l2_info = { {0} };
982 struct bnxt_tc_tunnel_node *decap_node;
983 struct ip_tunnel_key tun_key = { 0 };
984 struct bnxt_tc_l2_key *decap_l2_info;
985 __le32 ref_decap_handle;
986 int rc;
987
988 /* Check if there's another flow using the same tunnel decap.
989 * If not, add this tunnel to the table and resolve the other
990 * tunnel header fileds
991 */
992 decap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->decap_table,
993 &tc_info->decap_ht_params,
994 decap_key);
995 if (!decap_node)
996 return -ENOMEM;
997
998 flow_node->decap_node = decap_node;
999
1000 if (decap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1001 goto done;
1002
1003 /* Resolve the L2 fields for tunnel decap
1004 * Resolve the route for remote vtep (saddr) of the decap key
1005 * Find it's next-hop mac addrs
1006 */
1007 tun_key.u.ipv4.dst = flow->tun_key.u.ipv4.src;
1008 tun_key.tp_dst = flow->tun_key.tp_dst;
1009 rc = bnxt_tc_resolve_tunnel_hdrs(bp, &tun_key, &l2_info, bp->dev);
1010 if (rc)
1011 goto put_decap;
1012
Sathya Perla8c95f772017-10-26 11:51:29 -04001013 decap_l2_info = &decap_node->l2_info;
Sunil Challac8fb7b82017-12-01 03:13:03 -05001014 /* decap smac is wildcarded */
Sathya Perla8c95f772017-10-26 11:51:29 -04001015 ether_addr_copy(decap_l2_info->dmac, l2_info.smac);
Sathya Perla8c95f772017-10-26 11:51:29 -04001016 if (l2_info.num_vlans) {
1017 decap_l2_info->num_vlans = l2_info.num_vlans;
1018 decap_l2_info->inner_vlan_tpid = l2_info.inner_vlan_tpid;
1019 decap_l2_info->inner_vlan_tci = l2_info.inner_vlan_tci;
1020 }
1021 flow->flags |= BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS;
1022
1023 /* For getting a decap_filter_handle we first need to check if
1024 * there are any other decap flows that share the same tunnel L2
1025 * key and if so, pass that flow's decap_filter_handle as the
1026 * ref_decap_handle for this flow.
1027 */
1028 rc = bnxt_tc_get_ref_decap_handle(bp, flow, decap_l2_info, flow_node,
1029 &ref_decap_handle);
1030 if (rc)
1031 goto put_decap;
1032
1033 /* Issue the hwrm cmd to allocate a decap filter handle */
1034 rc = hwrm_cfa_decap_filter_alloc(bp, flow, decap_l2_info,
1035 ref_decap_handle,
1036 &decap_node->tunnel_handle);
1037 if (rc)
1038 goto put_decap_l2;
1039
1040done:
1041 *decap_filter_handle = decap_node->tunnel_handle;
1042 return 0;
1043
1044put_decap_l2:
1045 bnxt_tc_put_decap_l2_node(bp, flow_node);
1046put_decap:
1047 bnxt_tc_put_tunnel_node(bp, &tc_info->decap_table,
1048 &tc_info->decap_ht_params,
1049 flow_node->decap_node);
1050 return rc;
1051}
1052
1053static void bnxt_tc_put_encap_handle(struct bnxt *bp,
1054 struct bnxt_tc_tunnel_node *encap_node)
1055{
1056 __le32 encap_handle = encap_node->tunnel_handle;
Sathya Perlacd663582017-10-26 11:51:32 -04001057 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -04001058 int rc;
1059
1060 rc = bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1061 &tc_info->encap_ht_params, encap_node);
1062 if (!rc && encap_handle != INVALID_TUNNEL_HANDLE)
1063 hwrm_cfa_encap_record_free(bp, encap_handle);
1064}
1065
1066/* Lookup the tunnel encap table and check if there's an encap_handle
1067 * alloc'd already.
1068 * If not, query L2 info via a route lookup and issue an encap_record_alloc
1069 * cmd to FW.
1070 */
1071static int bnxt_tc_get_encap_handle(struct bnxt *bp, struct bnxt_tc_flow *flow,
1072 struct bnxt_tc_flow_node *flow_node,
1073 __le32 *encap_handle)
1074{
1075 struct ip_tunnel_key *encap_key = &flow->actions.tun_encap_key;
Sathya Perlacd663582017-10-26 11:51:32 -04001076 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla8c95f772017-10-26 11:51:29 -04001077 struct bnxt_tc_tunnel_node *encap_node;
1078 int rc;
1079
1080 /* Check if there's another flow using the same tunnel encap.
1081 * If not, add this tunnel to the table and resolve the other
1082 * tunnel header fileds
1083 */
1084 encap_node = bnxt_tc_get_tunnel_node(bp, &tc_info->encap_table,
1085 &tc_info->encap_ht_params,
1086 encap_key);
1087 if (!encap_node)
1088 return -ENOMEM;
1089
1090 flow_node->encap_node = encap_node;
1091
1092 if (encap_node->tunnel_handle != INVALID_TUNNEL_HANDLE)
1093 goto done;
1094
1095 rc = bnxt_tc_resolve_tunnel_hdrs(bp, encap_key, &encap_node->l2_info,
1096 flow->actions.dst_dev);
1097 if (rc)
1098 goto put_encap;
1099
1100 /* Allocate a new tunnel encap record */
1101 rc = hwrm_cfa_encap_record_alloc(bp, encap_key, &encap_node->l2_info,
1102 &encap_node->tunnel_handle);
1103 if (rc)
1104 goto put_encap;
1105
1106done:
1107 *encap_handle = encap_node->tunnel_handle;
1108 return 0;
1109
1110put_encap:
1111 bnxt_tc_put_tunnel_node(bp, &tc_info->encap_table,
1112 &tc_info->encap_ht_params, encap_node);
1113 return rc;
1114}
1115
1116static void bnxt_tc_put_tunnel_handle(struct bnxt *bp,
1117 struct bnxt_tc_flow *flow,
1118 struct bnxt_tc_flow_node *flow_node)
1119{
1120 if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1121 bnxt_tc_put_decap_handle(bp, flow_node);
1122 else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1123 bnxt_tc_put_encap_handle(bp, flow_node->encap_node);
1124}
1125
1126static int bnxt_tc_get_tunnel_handle(struct bnxt *bp,
1127 struct bnxt_tc_flow *flow,
1128 struct bnxt_tc_flow_node *flow_node,
1129 __le32 *tunnel_handle)
1130{
1131 if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP)
1132 return bnxt_tc_get_decap_handle(bp, flow, flow_node,
1133 tunnel_handle);
1134 else if (flow->actions.flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP)
1135 return bnxt_tc_get_encap_handle(bp, flow, flow_node,
1136 tunnel_handle);
1137 else
1138 return 0;
1139}
Sathya Perla2ae74082017-08-28 13:40:33 -04001140static int __bnxt_tc_del_flow(struct bnxt *bp,
1141 struct bnxt_tc_flow_node *flow_node)
1142{
Sathya Perlacd663582017-10-26 11:51:32 -04001143 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001144 int rc;
1145
1146 /* send HWRM cmd to free the flow-id */
1147 bnxt_hwrm_cfa_flow_free(bp, flow_node->flow_handle);
1148
1149 mutex_lock(&tc_info->lock);
1150
Sathya Perla8c95f772017-10-26 11:51:29 -04001151 /* release references to any tunnel encap/decap nodes */
1152 bnxt_tc_put_tunnel_handle(bp, &flow_node->flow, flow_node);
1153
Sathya Perla2ae74082017-08-28 13:40:33 -04001154 /* release reference to l2 node */
1155 bnxt_tc_put_l2_node(bp, flow_node);
1156
1157 mutex_unlock(&tc_info->lock);
1158
1159 rc = rhashtable_remove_fast(&tc_info->flow_table, &flow_node->node,
1160 tc_info->flow_ht_params);
1161 if (rc)
1162 netdev_err(bp->dev, "Error: %s: rhashtable_remove_fast rc=%d",
1163 __func__, rc);
1164
1165 kfree_rcu(flow_node, rcu);
1166 return 0;
1167}
1168
1169/* Add a new flow or replace an existing flow.
1170 * Notes on locking:
1171 * There are essentially two critical sections here.
1172 * 1. while adding a new flow
1173 * a) lookup l2-key
1174 * b) issue HWRM cmd and get flow_handle
1175 * c) link l2-key with flow
1176 * 2. while deleting a flow
1177 * a) unlinking l2-key from flow
1178 * A lock is needed to protect these two critical sections.
1179 *
1180 * The hash-tables are already protected by the rhashtable API.
1181 */
1182static int bnxt_tc_add_flow(struct bnxt *bp, u16 src_fid,
1183 struct tc_cls_flower_offload *tc_flow_cmd)
1184{
1185 struct bnxt_tc_flow_node *new_node, *old_node;
Sathya Perlacd663582017-10-26 11:51:32 -04001186 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001187 struct bnxt_tc_flow *flow;
Sathya Perla8c95f772017-10-26 11:51:29 -04001188 __le32 tunnel_handle = 0;
Sathya Perla2ae74082017-08-28 13:40:33 -04001189 __le16 ref_flow_handle;
1190 int rc;
1191
1192 /* allocate memory for the new flow and it's node */
1193 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
1194 if (!new_node) {
1195 rc = -ENOMEM;
1196 goto done;
1197 }
1198 new_node->cookie = tc_flow_cmd->cookie;
1199 flow = &new_node->flow;
1200
1201 rc = bnxt_tc_parse_flow(bp, tc_flow_cmd, flow);
1202 if (rc)
1203 goto free_node;
1204 flow->src_fid = src_fid;
1205
1206 if (!bnxt_tc_can_offload(bp, flow)) {
1207 rc = -ENOSPC;
1208 goto free_node;
1209 }
1210
1211 /* If a flow exists with the same cookie, delete it */
1212 old_node = rhashtable_lookup_fast(&tc_info->flow_table,
1213 &tc_flow_cmd->cookie,
1214 tc_info->flow_ht_params);
1215 if (old_node)
1216 __bnxt_tc_del_flow(bp, old_node);
1217
1218 /* Check if the L2 part of the flow has been offloaded already.
1219 * If so, bump up it's refcnt and get it's reference handle.
1220 */
1221 mutex_lock(&tc_info->lock);
1222 rc = bnxt_tc_get_ref_flow_handle(bp, flow, new_node, &ref_flow_handle);
1223 if (rc)
1224 goto unlock;
1225
Sathya Perla8c95f772017-10-26 11:51:29 -04001226 /* If the flow involves tunnel encap/decap, get tunnel_handle */
1227 rc = bnxt_tc_get_tunnel_handle(bp, flow, new_node, &tunnel_handle);
Sathya Perla2ae74082017-08-28 13:40:33 -04001228 if (rc)
1229 goto put_l2;
1230
Sathya Perla8c95f772017-10-26 11:51:29 -04001231 /* send HWRM cmd to alloc the flow */
1232 rc = bnxt_hwrm_cfa_flow_alloc(bp, flow, ref_flow_handle,
1233 tunnel_handle, &new_node->flow_handle);
1234 if (rc)
1235 goto put_tunnel;
1236
Sathya Perla5a84acb2017-10-26 11:51:31 -04001237 flow->lastused = jiffies;
1238 spin_lock_init(&flow->stats_lock);
Sathya Perla2ae74082017-08-28 13:40:33 -04001239 /* add new flow to flow-table */
1240 rc = rhashtable_insert_fast(&tc_info->flow_table, &new_node->node,
1241 tc_info->flow_ht_params);
1242 if (rc)
1243 goto hwrm_flow_free;
1244
1245 mutex_unlock(&tc_info->lock);
1246 return 0;
1247
1248hwrm_flow_free:
1249 bnxt_hwrm_cfa_flow_free(bp, new_node->flow_handle);
Sathya Perla8c95f772017-10-26 11:51:29 -04001250put_tunnel:
1251 bnxt_tc_put_tunnel_handle(bp, flow, new_node);
Sathya Perla2ae74082017-08-28 13:40:33 -04001252put_l2:
1253 bnxt_tc_put_l2_node(bp, new_node);
1254unlock:
1255 mutex_unlock(&tc_info->lock);
1256free_node:
Sathya Perla8c95f772017-10-26 11:51:29 -04001257 kfree_rcu(new_node, rcu);
Sathya Perla2ae74082017-08-28 13:40:33 -04001258done:
1259 netdev_err(bp->dev, "Error: %s: cookie=0x%lx error=%d",
1260 __func__, tc_flow_cmd->cookie, rc);
1261 return rc;
1262}
1263
1264static int bnxt_tc_del_flow(struct bnxt *bp,
1265 struct tc_cls_flower_offload *tc_flow_cmd)
1266{
Sathya Perlacd663582017-10-26 11:51:32 -04001267 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001268 struct bnxt_tc_flow_node *flow_node;
1269
1270 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1271 &tc_flow_cmd->cookie,
1272 tc_info->flow_ht_params);
1273 if (!flow_node) {
1274 netdev_info(bp->dev, "ERROR: no flow_node for cookie %lx",
1275 tc_flow_cmd->cookie);
1276 return -EINVAL;
1277 }
1278
1279 return __bnxt_tc_del_flow(bp, flow_node);
1280}
1281
1282static int bnxt_tc_get_flow_stats(struct bnxt *bp,
1283 struct tc_cls_flower_offload *tc_flow_cmd)
1284{
Sathya Perla5a84acb2017-10-26 11:51:31 -04001285 struct bnxt_tc_flow_stats stats, *curr_stats, *prev_stats;
Sathya Perlacd663582017-10-26 11:51:32 -04001286 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perlad7bc7302017-08-28 13:40:35 -04001287 struct bnxt_tc_flow_node *flow_node;
Sathya Perla5a84acb2017-10-26 11:51:31 -04001288 struct bnxt_tc_flow *flow;
1289 unsigned long lastused;
Sathya Perlad7bc7302017-08-28 13:40:35 -04001290
1291 flow_node = rhashtable_lookup_fast(&tc_info->flow_table,
1292 &tc_flow_cmd->cookie,
1293 tc_info->flow_ht_params);
1294 if (!flow_node) {
1295 netdev_info(bp->dev, "Error: no flow_node for cookie %lx",
1296 tc_flow_cmd->cookie);
1297 return -1;
1298 }
1299
Sathya Perla5a84acb2017-10-26 11:51:31 -04001300 flow = &flow_node->flow;
1301 curr_stats = &flow->stats;
1302 prev_stats = &flow->prev_stats;
1303
1304 spin_lock(&flow->stats_lock);
1305 stats.packets = curr_stats->packets - prev_stats->packets;
1306 stats.bytes = curr_stats->bytes - prev_stats->bytes;
1307 *prev_stats = *curr_stats;
1308 lastused = flow->lastused;
1309 spin_unlock(&flow->stats_lock);
1310
1311 tcf_exts_stats_update(tc_flow_cmd->exts, stats.bytes, stats.packets,
1312 lastused);
1313 return 0;
1314}
1315
1316static int
1317bnxt_hwrm_cfa_flow_stats_get(struct bnxt *bp, int num_flows,
1318 struct bnxt_tc_stats_batch stats_batch[])
1319{
1320 struct hwrm_cfa_flow_stats_output *resp = bp->hwrm_cmd_resp_addr;
1321 struct hwrm_cfa_flow_stats_input req = { 0 };
1322 __le16 *req_flow_handles = &req.flow_handle_0;
1323 int rc, i;
1324
1325 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_STATS, -1, -1);
1326 req.num_flows = cpu_to_le16(num_flows);
1327 for (i = 0; i < num_flows; i++) {
1328 struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1329
1330 req_flow_handles[i] = flow_node->flow_handle;
1331 }
1332
1333 mutex_lock(&bp->hwrm_cmd_lock);
1334 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
1335 if (!rc) {
1336 __le64 *resp_packets = &resp->packet_0;
1337 __le64 *resp_bytes = &resp->byte_0;
1338
1339 for (i = 0; i < num_flows; i++) {
1340 stats_batch[i].hw_stats.packets =
1341 le64_to_cpu(resp_packets[i]);
1342 stats_batch[i].hw_stats.bytes =
1343 le64_to_cpu(resp_bytes[i]);
1344 }
1345 } else {
1346 netdev_info(bp->dev, "error rc=%d", rc);
1347 }
1348
1349 mutex_unlock(&bp->hwrm_cmd_lock);
1350 return rc;
1351}
1352
1353/* Add val to accum while handling a possible wraparound
1354 * of val. Eventhough val is of type u64, its actual width
1355 * is denoted by mask and will wrap-around beyond that width.
1356 */
1357static void accumulate_val(u64 *accum, u64 val, u64 mask)
1358{
1359#define low_bits(x, mask) ((x) & (mask))
1360#define high_bits(x, mask) ((x) & ~(mask))
1361 bool wrapped = val < low_bits(*accum, mask);
1362
1363 *accum = high_bits(*accum, mask) + val;
1364 if (wrapped)
1365 *accum += (mask + 1);
1366}
1367
1368/* The HW counters' width is much less than 64bits.
1369 * Handle possible wrap-around while updating the stat counters
1370 */
1371static void bnxt_flow_stats_accum(struct bnxt_tc_info *tc_info,
1372 struct bnxt_tc_flow_stats *acc_stats,
1373 struct bnxt_tc_flow_stats *hw_stats)
1374{
1375 accumulate_val(&acc_stats->bytes, hw_stats->bytes, tc_info->bytes_mask);
1376 accumulate_val(&acc_stats->packets, hw_stats->packets,
1377 tc_info->packets_mask);
1378}
1379
1380static int
1381bnxt_tc_flow_stats_batch_update(struct bnxt *bp, int num_flows,
1382 struct bnxt_tc_stats_batch stats_batch[])
1383{
Sathya Perlacd663582017-10-26 11:51:32 -04001384 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla5a84acb2017-10-26 11:51:31 -04001385 int rc, i;
1386
1387 rc = bnxt_hwrm_cfa_flow_stats_get(bp, num_flows, stats_batch);
Sathya Perlad7bc7302017-08-28 13:40:35 -04001388 if (rc)
1389 return rc;
1390
Sathya Perla5a84acb2017-10-26 11:51:31 -04001391 for (i = 0; i < num_flows; i++) {
1392 struct bnxt_tc_flow_node *flow_node = stats_batch[i].flow_node;
1393 struct bnxt_tc_flow *flow = &flow_node->flow;
1394
1395 spin_lock(&flow->stats_lock);
1396 bnxt_flow_stats_accum(tc_info, &flow->stats,
1397 &stats_batch[i].hw_stats);
1398 if (flow->stats.packets != flow->prev_stats.packets)
1399 flow->lastused = jiffies;
1400 spin_unlock(&flow->stats_lock);
1401 }
1402
Sathya Perla2ae74082017-08-28 13:40:33 -04001403 return 0;
1404}
1405
Sathya Perla5a84acb2017-10-26 11:51:31 -04001406static int
1407bnxt_tc_flow_stats_batch_prep(struct bnxt *bp,
1408 struct bnxt_tc_stats_batch stats_batch[],
1409 int *num_flows)
1410{
Sathya Perlacd663582017-10-26 11:51:32 -04001411 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla5a84acb2017-10-26 11:51:31 -04001412 struct rhashtable_iter *iter = &tc_info->iter;
1413 void *flow_node;
1414 int rc, i;
1415
1416 rc = rhashtable_walk_start(iter);
1417 if (rc && rc != -EAGAIN) {
1418 i = 0;
1419 goto done;
1420 }
1421
1422 rc = 0;
1423 for (i = 0; i < BNXT_FLOW_STATS_BATCH_MAX; i++) {
1424 flow_node = rhashtable_walk_next(iter);
1425 if (IS_ERR(flow_node)) {
1426 i = 0;
1427 if (PTR_ERR(flow_node) == -EAGAIN) {
1428 continue;
1429 } else {
1430 rc = PTR_ERR(flow_node);
1431 goto done;
1432 }
1433 }
1434
1435 /* No more flows */
1436 if (!flow_node)
1437 goto done;
1438
1439 stats_batch[i].flow_node = flow_node;
1440 }
1441done:
1442 rhashtable_walk_stop(iter);
1443 *num_flows = i;
1444 return rc;
1445}
1446
1447void bnxt_tc_flow_stats_work(struct bnxt *bp)
1448{
Sathya Perlacd663582017-10-26 11:51:32 -04001449 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla5a84acb2017-10-26 11:51:31 -04001450 int num_flows, rc;
1451
1452 num_flows = atomic_read(&tc_info->flow_table.nelems);
1453 if (!num_flows)
1454 return;
1455
1456 rhashtable_walk_enter(&tc_info->flow_table, &tc_info->iter);
1457
1458 for (;;) {
1459 rc = bnxt_tc_flow_stats_batch_prep(bp, tc_info->stats_batch,
1460 &num_flows);
1461 if (rc) {
1462 if (rc == -EAGAIN)
1463 continue;
1464 break;
1465 }
1466
1467 if (!num_flows)
1468 break;
1469
1470 bnxt_tc_flow_stats_batch_update(bp, num_flows,
1471 tc_info->stats_batch);
1472 }
1473
1474 rhashtable_walk_exit(&tc_info->iter);
1475}
1476
Sathya Perla2ae74082017-08-28 13:40:33 -04001477int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
1478 struct tc_cls_flower_offload *cls_flower)
1479{
1480 int rc = 0;
1481
Jiri Pirko9e0fd152017-10-19 15:50:39 +02001482 if (cls_flower->common.chain_index)
Sathya Perla1e3c5ec2017-09-18 17:05:37 +05301483 return -EOPNOTSUPP;
1484
Sathya Perla2ae74082017-08-28 13:40:33 -04001485 switch (cls_flower->command) {
1486 case TC_CLSFLOWER_REPLACE:
1487 rc = bnxt_tc_add_flow(bp, src_fid, cls_flower);
1488 break;
1489
1490 case TC_CLSFLOWER_DESTROY:
1491 rc = bnxt_tc_del_flow(bp, cls_flower);
1492 break;
1493
1494 case TC_CLSFLOWER_STATS:
1495 rc = bnxt_tc_get_flow_stats(bp, cls_flower);
1496 break;
1497 }
1498 return rc;
1499}
1500
1501static const struct rhashtable_params bnxt_tc_flow_ht_params = {
1502 .head_offset = offsetof(struct bnxt_tc_flow_node, node),
1503 .key_offset = offsetof(struct bnxt_tc_flow_node, cookie),
1504 .key_len = sizeof(((struct bnxt_tc_flow_node *)0)->cookie),
1505 .automatic_shrinking = true
1506};
1507
1508static const struct rhashtable_params bnxt_tc_l2_ht_params = {
1509 .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1510 .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1511 .key_len = BNXT_TC_L2_KEY_LEN,
1512 .automatic_shrinking = true
1513};
1514
Sathya Perla8c95f772017-10-26 11:51:29 -04001515static const struct rhashtable_params bnxt_tc_decap_l2_ht_params = {
1516 .head_offset = offsetof(struct bnxt_tc_l2_node, node),
1517 .key_offset = offsetof(struct bnxt_tc_l2_node, key),
1518 .key_len = BNXT_TC_L2_KEY_LEN,
1519 .automatic_shrinking = true
1520};
1521
1522static const struct rhashtable_params bnxt_tc_tunnel_ht_params = {
1523 .head_offset = offsetof(struct bnxt_tc_tunnel_node, node),
1524 .key_offset = offsetof(struct bnxt_tc_tunnel_node, key),
1525 .key_len = sizeof(struct ip_tunnel_key),
1526 .automatic_shrinking = true
1527};
1528
Sathya Perla2ae74082017-08-28 13:40:33 -04001529/* convert counter width in bits to a mask */
1530#define mask(width) ((u64)~0 >> (64 - (width)))
1531
1532int bnxt_init_tc(struct bnxt *bp)
1533{
Sathya Perlacd663582017-10-26 11:51:32 -04001534 struct bnxt_tc_info *tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001535 int rc;
1536
Sathya Perla8c95f772017-10-26 11:51:29 -04001537 if (bp->hwrm_spec_code < 0x10803) {
Sathya Perla2ae74082017-08-28 13:40:33 -04001538 netdev_warn(bp->dev,
1539 "Firmware does not support TC flower offload.\n");
1540 return -ENOTSUPP;
1541 }
Sathya Perlacd663582017-10-26 11:51:32 -04001542
1543 tc_info = kzalloc(sizeof(*tc_info), GFP_KERNEL);
1544 if (!tc_info)
1545 return -ENOMEM;
Sathya Perla2ae74082017-08-28 13:40:33 -04001546 mutex_init(&tc_info->lock);
1547
1548 /* Counter widths are programmed by FW */
1549 tc_info->bytes_mask = mask(36);
1550 tc_info->packets_mask = mask(28);
1551
1552 tc_info->flow_ht_params = bnxt_tc_flow_ht_params;
1553 rc = rhashtable_init(&tc_info->flow_table, &tc_info->flow_ht_params);
1554 if (rc)
Sathya Perlacd663582017-10-26 11:51:32 -04001555 goto free_tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001556
1557 tc_info->l2_ht_params = bnxt_tc_l2_ht_params;
1558 rc = rhashtable_init(&tc_info->l2_table, &tc_info->l2_ht_params);
1559 if (rc)
1560 goto destroy_flow_table;
1561
Sathya Perla8c95f772017-10-26 11:51:29 -04001562 tc_info->decap_l2_ht_params = bnxt_tc_decap_l2_ht_params;
1563 rc = rhashtable_init(&tc_info->decap_l2_table,
1564 &tc_info->decap_l2_ht_params);
1565 if (rc)
1566 goto destroy_l2_table;
1567
1568 tc_info->decap_ht_params = bnxt_tc_tunnel_ht_params;
1569 rc = rhashtable_init(&tc_info->decap_table,
1570 &tc_info->decap_ht_params);
1571 if (rc)
1572 goto destroy_decap_l2_table;
1573
1574 tc_info->encap_ht_params = bnxt_tc_tunnel_ht_params;
1575 rc = rhashtable_init(&tc_info->encap_table,
1576 &tc_info->encap_ht_params);
1577 if (rc)
1578 goto destroy_decap_table;
1579
Sathya Perla2ae74082017-08-28 13:40:33 -04001580 tc_info->enabled = true;
1581 bp->dev->hw_features |= NETIF_F_HW_TC;
1582 bp->dev->features |= NETIF_F_HW_TC;
Sathya Perlacd663582017-10-26 11:51:32 -04001583 bp->tc_info = tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001584 return 0;
1585
Sathya Perla8c95f772017-10-26 11:51:29 -04001586destroy_decap_table:
1587 rhashtable_destroy(&tc_info->decap_table);
1588destroy_decap_l2_table:
1589 rhashtable_destroy(&tc_info->decap_l2_table);
1590destroy_l2_table:
1591 rhashtable_destroy(&tc_info->l2_table);
Sathya Perla2ae74082017-08-28 13:40:33 -04001592destroy_flow_table:
1593 rhashtable_destroy(&tc_info->flow_table);
Sathya Perlacd663582017-10-26 11:51:32 -04001594free_tc_info:
1595 kfree(tc_info);
Sathya Perla2ae74082017-08-28 13:40:33 -04001596 return rc;
1597}
1598
1599void bnxt_shutdown_tc(struct bnxt *bp)
1600{
Sathya Perlacd663582017-10-26 11:51:32 -04001601 struct bnxt_tc_info *tc_info = bp->tc_info;
Sathya Perla2ae74082017-08-28 13:40:33 -04001602
Sathya Perlacd663582017-10-26 11:51:32 -04001603 if (!bnxt_tc_flower_enabled(bp))
Sathya Perla2ae74082017-08-28 13:40:33 -04001604 return;
1605
1606 rhashtable_destroy(&tc_info->flow_table);
1607 rhashtable_destroy(&tc_info->l2_table);
Sathya Perla8c95f772017-10-26 11:51:29 -04001608 rhashtable_destroy(&tc_info->decap_l2_table);
1609 rhashtable_destroy(&tc_info->decap_table);
1610 rhashtable_destroy(&tc_info->encap_table);
Sathya Perlacd663582017-10-26 11:51:32 -04001611 kfree(tc_info);
1612 bp->tc_info = NULL;
Sathya Perla2ae74082017-08-28 13:40:33 -04001613}