1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2017 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #ifndef BNXT_TC_H
11 #define BNXT_TC_H
12 
13 #ifdef CONFIG_BNXT_FLOWER_OFFLOAD
14 
15 #include <net/ip_tunnels.h>
16 
17 /* Structs used for storing the filter/actions of the TC cmd.
18  */
19 struct bnxt_tc_l2_key {
20 	u8		dmac[ETH_ALEN];
21 	u8		smac[ETH_ALEN];
22 	__be16		inner_vlan_tpid;
23 	__be16		inner_vlan_tci;
24 	__be16		ether_type;
25 	u8		num_vlans;
26 };
27 
28 struct bnxt_tc_l3_key {
29 	union {
30 		struct {
31 			struct in_addr daddr;
32 			struct in_addr saddr;
33 		} ipv4;
34 		struct {
35 			struct in6_addr daddr;
36 			struct in6_addr saddr;
37 		} ipv6;
38 	};
39 };
40 
41 struct bnxt_tc_l4_key {
42 	u8  ip_proto;
43 	union {
44 		struct {
45 			__be16 sport;
46 			__be16 dport;
47 		} ports;
48 		struct {
49 			u8 type;
50 			u8 code;
51 		} icmp;
52 	};
53 };
54 
55 struct bnxt_tc_tunnel_key {
56 	struct bnxt_tc_l2_key	l2;
57 	struct bnxt_tc_l3_key	l3;
58 	struct bnxt_tc_l4_key	l4;
59 	__be32			id;
60 };
61 
62 struct bnxt_tc_actions {
63 	u32				flags;
64 #define BNXT_TC_ACTION_FLAG_FWD			BIT(0)
65 #define BNXT_TC_ACTION_FLAG_FWD_VXLAN		BIT(1)
66 #define BNXT_TC_ACTION_FLAG_PUSH_VLAN		BIT(3)
67 #define BNXT_TC_ACTION_FLAG_POP_VLAN		BIT(4)
68 #define BNXT_TC_ACTION_FLAG_DROP		BIT(5)
69 #define BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP	BIT(6)
70 #define BNXT_TC_ACTION_FLAG_TUNNEL_DECAP	BIT(7)
71 
72 	u16				dst_fid;
73 	struct net_device		*dst_dev;
74 	__be16				push_vlan_tpid;
75 	__be16				push_vlan_tci;
76 
77 	/* tunnel encap */
78 	struct ip_tunnel_key		tun_encap_key;
79 };
80 
81 struct bnxt_tc_flow {
82 	u32				flags;
83 #define BNXT_TC_FLOW_FLAGS_ETH_ADDRS		BIT(1)
84 #define BNXT_TC_FLOW_FLAGS_IPV4_ADDRS		BIT(2)
85 #define BNXT_TC_FLOW_FLAGS_IPV6_ADDRS		BIT(3)
86 #define BNXT_TC_FLOW_FLAGS_PORTS		BIT(4)
87 #define BNXT_TC_FLOW_FLAGS_ICMP			BIT(5)
88 #define BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS	BIT(6)
89 #define BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS	BIT(7)
90 #define BNXT_TC_FLOW_FLAGS_TUNL_IPV6_ADDRS	BIT(8)
91 #define BNXT_TC_FLOW_FLAGS_TUNL_PORTS		BIT(9)
92 #define BNXT_TC_FLOW_FLAGS_TUNL_ID		BIT(10)
93 #define BNXT_TC_FLOW_FLAGS_TUNNEL	(BNXT_TC_FLOW_FLAGS_TUNL_ETH_ADDRS | \
94 					 BNXT_TC_FLOW_FLAGS_TUNL_IPV4_ADDRS | \
95 					 BNXT_TC_FLOW_FLAGS_TUNL_IPV6_ADDRS |\
96 					 BNXT_TC_FLOW_FLAGS_TUNL_PORTS |\
97 					 BNXT_TC_FLOW_FLAGS_TUNL_ID)
98 
99 	/* flow applicable to pkts ingressing on this fid */
100 	u16				src_fid;
101 	struct bnxt_tc_l2_key		l2_key;
102 	struct bnxt_tc_l2_key		l2_mask;
103 	struct bnxt_tc_l3_key		l3_key;
104 	struct bnxt_tc_l3_key		l3_mask;
105 	struct bnxt_tc_l4_key		l4_key;
106 	struct bnxt_tc_l4_key		l4_mask;
107 	struct ip_tunnel_key		tun_key;
108 	struct ip_tunnel_key		tun_mask;
109 
110 	struct bnxt_tc_actions		actions;
111 
112 	/* updated stats accounting for hw-counter wrap-around */
113 	struct bnxt_tc_flow_stats	stats;
114 	/* previous snap-shot of stats */
115 	struct bnxt_tc_flow_stats	prev_stats;
116 	unsigned long			lastused; /* jiffies */
117 	/* for calculating delta from prev_stats and
118 	 * updating prev_stats atomically.
119 	 */
120 	spinlock_t			stats_lock;
121 };
122 
123 /* Tunnel encap/decap hash table
124  * This table is used to maintain a list of flows that use
125  * the same tunnel encap/decap params (ip_daddrs, vni, udp_dport)
126  * and the FW returned handle.
127  * A separate table is maintained for encap and decap
128  */
129 struct bnxt_tc_tunnel_node {
130 	struct ip_tunnel_key		key;
131 	struct rhash_head		node;
132 
133 	/* tunnel l2 info */
134 	struct bnxt_tc_l2_key		l2_info;
135 
136 #define	INVALID_TUNNEL_HANDLE		cpu_to_le32(0xffffffff)
137 	/* tunnel handle returned by FW */
138 	__le32				tunnel_handle;
139 
140 	u32				refcount;
141 	struct rcu_head			rcu;
142 };
143 
144 /* L2 hash table
145  * The same data-struct is used for L2-flow table and L2-tunnel table.
146  * The L2 part of a flow or tunnel is stored in a hash table.
147  * A flow that shares the same L2 key/mask with an
148  * already existing flow/tunnel must refer to it's flow handle or
149  * decap_filter_id respectively.
150  */
151 struct bnxt_tc_l2_node {
152 	/* hash key: first 16b of key */
153 #define BNXT_TC_L2_KEY_LEN			16
154 	struct bnxt_tc_l2_key	key;
155 	struct rhash_head	node;
156 
157 	/* a linked list of flows that share the same l2 key */
158 	struct list_head	common_l2_flows;
159 
160 	/* number of flows/tunnels sharing the l2 key */
161 	u16			refcount;
162 
163 	struct rcu_head		rcu;
164 };
165 
166 struct bnxt_tc_flow_node {
167 	/* hash key: provided by TC */
168 	unsigned long			cookie;
169 	struct rhash_head		node;
170 
171 	struct bnxt_tc_flow		flow;
172 
173 	__le16				flow_handle;
174 
175 	/* L2 node in l2 hashtable that shares flow's l2 key */
176 	struct bnxt_tc_l2_node		*l2_node;
177 	/* for the shared_flows list maintained in l2_node */
178 	struct list_head		l2_list_node;
179 
180 	/* tunnel encap related */
181 	struct bnxt_tc_tunnel_node	*encap_node;
182 
183 	/* tunnel decap related */
184 	struct bnxt_tc_tunnel_node	*decap_node;
185 	/* L2 node in tunnel-l2 hashtable that shares flow's tunnel l2 key */
186 	struct bnxt_tc_l2_node		*decap_l2_node;
187 	/* for the shared_flows list maintained in tunnel decap l2_node */
188 	struct list_head		decap_l2_list_node;
189 
190 	struct rcu_head			rcu;
191 };
192 
193 int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
194 			 struct tc_cls_flower_offload *cls_flower);
195 int bnxt_init_tc(struct bnxt *bp);
196 void bnxt_shutdown_tc(struct bnxt *bp);
197 void bnxt_tc_flow_stats_work(struct bnxt *bp);
198 
199 static inline bool bnxt_tc_flower_enabled(struct bnxt *bp)
200 {
201 	return bp->tc_info && bp->tc_info->enabled;
202 }
203 
204 #else /* CONFIG_BNXT_FLOWER_OFFLOAD */
205 
206 static inline int bnxt_tc_setup_flower(struct bnxt *bp, u16 src_fid,
207 				       struct tc_cls_flower_offload *cls_flower)
208 {
209 	return -EOPNOTSUPP;
210 }
211 
212 static inline int bnxt_init_tc(struct bnxt *bp)
213 {
214 	return 0;
215 }
216 
217 static inline void bnxt_shutdown_tc(struct bnxt *bp)
218 {
219 }
220 
221 static inline void bnxt_tc_flow_stats_work(struct bnxt *bp)
222 {
223 }
224 
225 static inline bool bnxt_tc_flower_enabled(struct bnxt *bp)
226 {
227 	return false;
228 }
229 #endif /* CONFIG_BNXT_FLOWER_OFFLOAD */
230 #endif /* BNXT_TC_H */
231