xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/main.h (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #ifndef __NFP_FLOWER_H__
5 #define __NFP_FLOWER_H__ 1
6 
7 #include "cmsg.h"
8 
9 #include <linux/circ_buf.h>
10 #include <linux/hashtable.h>
11 #include <linux/rhashtable.h>
12 #include <linux/time64.h>
13 #include <linux/types.h>
14 #include <net/pkt_cls.h>
15 #include <net/tcp.h>
16 #include <linux/workqueue.h>
17 #include <linux/idr.h>
18 
19 struct nfp_fl_pre_lag;
20 struct net_device;
21 struct nfp_app;
22 
23 #define NFP_FL_STAT_ID_MU_NUM		GENMASK(31, 22)
24 #define NFP_FL_STAT_ID_STAT		GENMASK(21, 0)
25 
26 #define NFP_FL_STATS_ELEM_RS		FIELD_SIZEOF(struct nfp_fl_stats_id, \
27 						     init_unalloc)
28 #define NFP_FLOWER_MASK_ENTRY_RS	256
29 #define NFP_FLOWER_MASK_ELEMENT_RS	1
30 #define NFP_FLOWER_MASK_HASH_BITS	10
31 
32 #define NFP_FL_META_FLAG_MANAGE_MASK	BIT(7)
33 
34 #define NFP_FL_MASK_REUSE_TIME_NS	40000
35 #define NFP_FL_MASK_ID_LOCATION		1
36 
37 #define NFP_FL_VXLAN_PORT		4789
38 #define NFP_FL_GENEVE_PORT		6081
39 
40 /* Extra features bitmap. */
41 #define NFP_FL_FEATS_GENEVE		BIT(0)
42 #define NFP_FL_NBI_MTU_SETTING		BIT(1)
43 #define NFP_FL_FEATS_GENEVE_OPT		BIT(2)
44 #define NFP_FL_FEATS_VLAN_PCP		BIT(3)
45 #define NFP_FL_FEATS_LAG		BIT(31)
46 
47 struct nfp_fl_mask_id {
48 	struct circ_buf mask_id_free_list;
49 	ktime_t *last_used;
50 	u8 init_unallocated;
51 };
52 
53 struct nfp_fl_stats_id {
54 	struct circ_buf free_list;
55 	u32 init_unalloc;
56 	u8 repeated_em_count;
57 };
58 
59 /**
60  * struct nfp_fl_tunnel_offloads - priv data for tunnel offloads
61  * @offloaded_macs:	Hashtable of the offloaded MAC addresses
62  * @ipv4_off_list:	List of IPv4 addresses to offload
63  * @neigh_off_list:	List of neighbour offloads
64  * @ipv4_off_lock:	Lock for the IPv4 address list
65  * @neigh_off_lock:	Lock for the neighbour address list
66  * @mac_off_ids:	IDA to manage id assignment for offloaded MACs
67  * @neigh_nb:		Notifier to monitor neighbour state
68  */
69 struct nfp_fl_tunnel_offloads {
70 	struct rhashtable offloaded_macs;
71 	struct list_head ipv4_off_list;
72 	struct list_head neigh_off_list;
73 	struct mutex ipv4_off_lock;
74 	spinlock_t neigh_off_lock;
75 	struct ida mac_off_ids;
76 	struct notifier_block neigh_nb;
77 };
78 
79 /**
80  * struct nfp_mtu_conf - manage MTU setting
81  * @portnum:		NFP port number of repr with requested MTU change
82  * @requested_val:	MTU value requested for repr
83  * @ack:		Received ack that MTU has been correctly set
84  * @wait_q:		Wait queue for MTU acknowledgements
85  * @lock:		Lock for setting/reading MTU variables
86  */
87 struct nfp_mtu_conf {
88 	u32 portnum;
89 	unsigned int requested_val;
90 	bool ack;
91 	wait_queue_head_t wait_q;
92 	spinlock_t lock;
93 };
94 
95 /**
96  * struct nfp_fl_lag - Flower APP priv data for link aggregation
97  * @work:		Work queue for writing configs to the HW
98  * @lock:		Lock to protect lag_group_list
99  * @group_list:		List of all master/slave groups offloaded
100  * @ida_handle:		IDA to handle group ids
101  * @pkt_num:		Incremented for each config packet sent
102  * @batch_ver:		Incremented for each batch of config packets
103  * @global_inst:	Instance allocator for groups
104  * @rst_cfg:		Marker to reset HW LAG config
105  * @retrans_skbs:	Cmsgs that could not be processed by HW and require
106  *			retransmission
107  */
108 struct nfp_fl_lag {
109 	struct delayed_work work;
110 	struct mutex lock;
111 	struct list_head group_list;
112 	struct ida ida_handle;
113 	unsigned int pkt_num;
114 	unsigned int batch_ver;
115 	u8 global_inst;
116 	bool rst_cfg;
117 	struct sk_buff_head retrans_skbs;
118 };
119 
120 /**
121  * struct nfp_flower_priv - Flower APP per-vNIC priv data
122  * @app:		Back pointer to app
123  * @nn:			Pointer to vNIC
124  * @mask_id_seed:	Seed used for mask hash table
125  * @flower_version:	HW version of flower
126  * @flower_ext_feats:	Bitmap of extra features the HW supports
127  * @stats_ids:		List of free stats ids
128  * @mask_ids:		List of free mask ids
129  * @mask_table:		Hash table used to store masks
130  * @stats_ring_size:	Maximum number of allowed stats ids
131  * @flow_table:		Hash table used to store flower rules
132  * @stats:		Stored stats updates for flower rules
133  * @stats_lock:		Lock for flower rule stats updates
134  * @cmsg_work:		Workqueue for control messages processing
135  * @cmsg_skbs_high:	List of higher priority skbs for control message
136  *			processing
137  * @cmsg_skbs_low:	List of lower priority skbs for control message
138  *			processing
139  * @tun:		Tunnel offload data
140  * @reify_replies:	atomically stores the number of replies received
141  *			from firmware for repr reify
142  * @reify_wait_queue:	wait queue for repr reify response counting
143  * @mtu_conf:		Configuration of repr MTU value
144  * @nfp_lag:		Link aggregation data block
145  * @indr_block_cb_priv:	List of priv data passed to indirect block cbs
146  * @non_repr_priv:	List of offloaded non-repr ports and their priv data
147  * @active_mem_unit:	Current active memory unit for flower rules
148  * @total_mem_units:	Total number of available memory units for flower rules
149  */
150 struct nfp_flower_priv {
151 	struct nfp_app *app;
152 	struct nfp_net *nn;
153 	u32 mask_id_seed;
154 	u64 flower_version;
155 	u64 flower_ext_feats;
156 	struct nfp_fl_stats_id stats_ids;
157 	struct nfp_fl_mask_id mask_ids;
158 	DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
159 	u32 stats_ring_size;
160 	struct rhashtable flow_table;
161 	struct nfp_fl_stats *stats;
162 	spinlock_t stats_lock; /* lock stats */
163 	struct work_struct cmsg_work;
164 	struct sk_buff_head cmsg_skbs_high;
165 	struct sk_buff_head cmsg_skbs_low;
166 	struct nfp_fl_tunnel_offloads tun;
167 	atomic_t reify_replies;
168 	wait_queue_head_t reify_wait_queue;
169 	struct nfp_mtu_conf mtu_conf;
170 	struct nfp_fl_lag nfp_lag;
171 	struct list_head indr_block_cb_priv;
172 	struct list_head non_repr_priv;
173 	unsigned int active_mem_unit;
174 	unsigned int total_mem_units;
175 };
176 
177 /**
178  * struct nfp_flower_repr_priv - Flower APP per-repr priv data
179  * @nfp_repr:		Back pointer to nfp_repr
180  * @lag_port_flags:	Extended port flags to record lag state of repr
181  * @mac_offloaded:	Flag indicating a MAC address is offloaded for repr
182  * @offloaded_mac_addr:	MAC address that has been offloaded for repr
183  * @mac_list:		List entry of reprs that share the same offloaded MAC
184  */
185 struct nfp_flower_repr_priv {
186 	struct nfp_repr *nfp_repr;
187 	unsigned long lag_port_flags;
188 	bool mac_offloaded;
189 	u8 offloaded_mac_addr[ETH_ALEN];
190 	struct list_head mac_list;
191 };
192 
193 /**
194  * struct nfp_flower_non_repr_priv - Priv data for non-repr offloaded ports
195  * @list:		List entry of offloaded reprs
196  * @netdev:		Pointer to non-repr net_device
197  * @ref_count:		Number of references held for this priv data
198  * @mac_offloaded:	Flag indicating a MAC address is offloaded for device
199  * @offloaded_mac_addr:	MAC address that has been offloaded for dev
200  */
201 struct nfp_flower_non_repr_priv {
202 	struct list_head list;
203 	struct net_device *netdev;
204 	int ref_count;
205 	bool mac_offloaded;
206 	u8 offloaded_mac_addr[ETH_ALEN];
207 };
208 
209 struct nfp_fl_key_ls {
210 	u32 key_layer_two;
211 	u8 key_layer;
212 	int key_size;
213 };
214 
215 struct nfp_fl_rule_metadata {
216 	u8 key_len;
217 	u8 mask_len;
218 	u8 act_len;
219 	u8 flags;
220 	__be32 host_ctx_id;
221 	__be64 host_cookie __packed;
222 	__be64 flow_version __packed;
223 	__be32 shortcut;
224 };
225 
226 struct nfp_fl_stats {
227 	u64 pkts;
228 	u64 bytes;
229 	u64 used;
230 };
231 
232 struct nfp_fl_payload {
233 	struct nfp_fl_rule_metadata meta;
234 	unsigned long tc_flower_cookie;
235 	struct rhash_head fl_node;
236 	struct rcu_head rcu;
237 	__be32 nfp_tun_ipv4_addr;
238 	struct net_device *ingress_dev;
239 	char *unmasked_data;
240 	char *mask_data;
241 	char *action_data;
242 };
243 
244 extern const struct rhashtable_params nfp_flower_table_params;
245 
246 struct nfp_fl_stats_frame {
247 	__be32 stats_con_id;
248 	__be32 pkt_count;
249 	__be64 byte_count;
250 	__be64 stats_cookie;
251 };
252 
253 int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count,
254 			     unsigned int host_ctx_split);
255 void nfp_flower_metadata_cleanup(struct nfp_app *app);
256 
257 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
258 			enum tc_setup_type type, void *type_data);
259 int nfp_flower_compile_flow_match(struct nfp_app *app,
260 				  struct tc_cls_flower_offload *flow,
261 				  struct nfp_fl_key_ls *key_ls,
262 				  struct net_device *netdev,
263 				  struct nfp_fl_payload *nfp_flow,
264 				  enum nfp_flower_tun_type tun_type);
265 int nfp_flower_compile_action(struct nfp_app *app,
266 			      struct tc_cls_flower_offload *flow,
267 			      struct net_device *netdev,
268 			      struct nfp_fl_payload *nfp_flow);
269 int nfp_compile_flow_metadata(struct nfp_app *app,
270 			      struct tc_cls_flower_offload *flow,
271 			      struct nfp_fl_payload *nfp_flow,
272 			      struct net_device *netdev);
273 int nfp_modify_flow_metadata(struct nfp_app *app,
274 			     struct nfp_fl_payload *nfp_flow);
275 
276 struct nfp_fl_payload *
277 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
278 			   struct net_device *netdev);
279 struct nfp_fl_payload *
280 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
281 
282 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
283 
284 int nfp_tunnel_config_start(struct nfp_app *app);
285 void nfp_tunnel_config_stop(struct nfp_app *app);
286 int nfp_tunnel_mac_event_handler(struct nfp_app *app,
287 				 struct net_device *netdev,
288 				 unsigned long event, void *ptr);
289 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
290 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
291 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
292 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
293 void nfp_flower_lag_init(struct nfp_fl_lag *lag);
294 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag);
295 int nfp_flower_lag_reset(struct nfp_fl_lag *lag);
296 int nfp_flower_lag_netdev_event(struct nfp_flower_priv *priv,
297 				struct net_device *netdev,
298 				unsigned long event, void *ptr);
299 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb);
300 int nfp_flower_lag_populate_pre_action(struct nfp_app *app,
301 				       struct net_device *master,
302 				       struct nfp_fl_pre_lag *pre_act);
303 int nfp_flower_lag_get_output_id(struct nfp_app *app,
304 				 struct net_device *master);
305 int nfp_flower_reg_indir_block_handler(struct nfp_app *app,
306 				       struct net_device *netdev,
307 				       unsigned long event);
308 
309 void
310 __nfp_flower_non_repr_priv_get(struct nfp_flower_non_repr_priv *non_repr_priv);
311 struct nfp_flower_non_repr_priv *
312 nfp_flower_non_repr_priv_get(struct nfp_app *app, struct net_device *netdev);
313 void
314 __nfp_flower_non_repr_priv_put(struct nfp_flower_non_repr_priv *non_repr_priv);
315 void
316 nfp_flower_non_repr_priv_put(struct nfp_app *app, struct net_device *netdev);
317 #endif
318