xref: /linux/drivers/net/ethernet/netronome/nfp/abm/main.h (revision cad4977344b35ea116ec5fefe91a76b1dfa113f5)
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
3 
4 #ifndef __NFP_ABM_H__
5 #define __NFP_ABM_H__ 1
6 
7 #include <linux/bits.h>
8 #include <linux/list.h>
9 #include <linux/radix-tree.h>
10 #include <net/devlink.h>
11 #include <net/pkt_cls.h>
12 #include <net/pkt_sched.h>
13 
14 /* Dump of 64 PRIOs and 256 REDs seems to take 850us on Xeon v4 @ 2.20GHz;
15  * 2.5ms / 400Hz seems more than sufficient for stats resolution.
16  */
17 #define NFP_ABM_STATS_REFRESH_IVAL	(2500 * 1000) /* ns */
18 
19 #define NFP_ABM_LVL_INFINITY		S32_MAX
20 
21 struct nfp_app;
22 struct nfp_net;
23 
24 #define NFP_ABM_PORTID_TYPE	GENMASK(23, 16)
25 #define NFP_ABM_PORTID_ID	GENMASK(7, 0)
26 
27 /* The possible actions if thresholds are exceeded */
28 enum nfp_abm_q_action {
29 	/* mark if ECN capable, otherwise drop */
30 	NFP_ABM_ACT_MARK_DROP		= 0,
31 	/* mark if ECN capable, otherwise goto QM */
32 	NFP_ABM_ACT_MARK_QUEUE		= 1,
33 	NFP_ABM_ACT_DROP		= 2,
34 	NFP_ABM_ACT_QUEUE		= 3,
35 	NFP_ABM_ACT_NOQUEUE		= 4,
36 };
37 
38 /**
39  * struct nfp_abm - ABM NIC app structure
40  * @app:	back pointer to nfp_app
41  * @pf_id:	ID of our PF link
42  *
43  * @num_prios:	number of supported DSCP priorities
44  * @num_bands:	number of supported DSCP priority bands
45  * @action_mask:	bitmask of supported actions
46  *
47  * @thresholds:		current threshold configuration
48  * @threshold_undef:	bitmap of thresholds which have not been set
49  * @actions:		current FW action configuration
50  * @num_thresholds:	number of @thresholds and bits in @threshold_undef
51  *
52  * @prio_map_len:	computed length of FW priority map (in bytes)
53  * @dscp_mask:		mask FW will apply on DSCP field
54  *
55  * @eswitch_mode:	devlink eswitch mode, advanced functions only visible
56  *			in switchdev mode
57  *
58  * @q_lvls:	queue level control area
59  * @qm_stats:	queue statistics symbol
60  * @q_stats:	basic queue statistics (only in per-band case)
61  */
62 struct nfp_abm {
63 	struct nfp_app *app;
64 	unsigned int pf_id;
65 
66 	unsigned int num_prios;
67 	unsigned int num_bands;
68 	unsigned int action_mask;
69 
70 	u32 *thresholds;
71 	unsigned long *threshold_undef;
72 	u8 *actions;
73 	size_t num_thresholds;
74 
75 	unsigned int prio_map_len;
76 	u8 dscp_mask;
77 
78 	enum devlink_eswitch_mode eswitch_mode;
79 
80 	const struct nfp_rtsym *q_lvls;
81 	const struct nfp_rtsym *qm_stats;
82 	const struct nfp_rtsym *q_stats;
83 };
84 
85 /**
86  * struct nfp_alink_stats - ABM NIC statistics
87  * @tx_pkts:		number of TXed packets
88  * @tx_bytes:		number of TXed bytes
89  * @backlog_pkts:	momentary backlog length (packets)
90  * @backlog_bytes:	momentary backlog length (bytes)
91  * @overlimits:		number of ECN marked TXed packets (accumulative)
92  * @drops:		number of tail-dropped packets (accumulative)
93  */
94 struct nfp_alink_stats {
95 	u64 tx_pkts;
96 	u64 tx_bytes;
97 	u64 backlog_pkts;
98 	u64 backlog_bytes;
99 	u64 overlimits;
100 	u64 drops;
101 };
102 
103 /**
104  * struct nfp_alink_xstats - extended ABM NIC statistics
105  * @ecn_marked:		number of ECN marked TXed packets
106  * @pdrop:		number of hard drops due to queue limit
107  */
108 struct nfp_alink_xstats {
109 	u64 ecn_marked;
110 	u64 pdrop;
111 };
112 
113 enum nfp_qdisc_type {
114 	NFP_QDISC_NONE = 0,
115 	NFP_QDISC_MQ,
116 	NFP_QDISC_RED,
117 	NFP_QDISC_GRED,
118 };
119 
120 #define NFP_QDISC_UNTRACKED	((struct nfp_qdisc *)1UL)
121 
122 /**
123  * struct nfp_qdisc - tracked TC Qdisc
124  * @netdev:		netdev on which Qdisc was created
125  * @type:		Qdisc type
126  * @handle:		handle of this Qdisc
127  * @parent_handle:	handle of the parent (unreliable if Qdisc was grafted)
128  * @use_cnt:		number of attachment points in the hierarchy
129  * @num_children:	current size of the @children array
130  * @children:		pointers to children
131  *
132  * @params_ok:		parameters of this Qdisc are OK for offload
133  * @offload_mark:	offload refresh state - selected for offload
134  * @offloaded:		Qdisc is currently offloaded to the HW
135  *
136  * @mq:			MQ Qdisc specific parameters and state
137  * @mq.stats:		current stats of the MQ Qdisc
138  * @mq.prev_stats:	previously reported @mq.stats
139  *
140  * @red:		RED Qdisc specific parameters and state
141  * @red.num_bands:	Number of valid entries in the @red.band table
142  * @red.band:		Per-band array of RED instances
143  * @red.band.ecn:		ECN marking is enabled (rather than drop)
144  * @red.band.threshold:		ECN marking threshold
145  * @red.band.stats:		current stats of the RED Qdisc
146  * @red.band.prev_stats:	previously reported @red.stats
147  * @red.band.xstats:		extended stats for RED - current
148  * @red.band.prev_xstats:	extended stats for RED - previously reported
149  */
150 struct nfp_qdisc {
151 	struct net_device *netdev;
152 	enum nfp_qdisc_type type;
153 	u32 handle;
154 	u32 parent_handle;
155 	unsigned int use_cnt;
156 	unsigned int num_children;
157 	struct nfp_qdisc **children;
158 
159 	bool params_ok;
160 	bool offload_mark;
161 	bool offloaded;
162 
163 	union {
164 		/* NFP_QDISC_MQ */
165 		struct {
166 			struct nfp_alink_stats stats;
167 			struct nfp_alink_stats prev_stats;
168 		} mq;
169 		/* TC_SETUP_QDISC_RED, TC_SETUP_QDISC_GRED */
170 		struct {
171 			unsigned int num_bands;
172 
173 			struct {
174 				bool ecn;
175 				u32 threshold;
176 				struct nfp_alink_stats stats;
177 				struct nfp_alink_stats prev_stats;
178 				struct nfp_alink_xstats xstats;
179 				struct nfp_alink_xstats prev_xstats;
180 			} band[MAX_DPs];
181 		} red;
182 	};
183 };
184 
185 /**
186  * struct nfp_abm_link - port tuple of a ABM NIC
187  * @abm:	back pointer to nfp_abm
188  * @vnic:	data vNIC
189  * @id:		id of the data vNIC
190  * @queue_base:	id of base to host queue within PCIe (not QC idx)
191  * @total_queues:	number of PF queues
192  *
193  * @last_stats_update:	ktime of last stats update
194  *
195  * @prio_map:		current map of priorities
196  * @has_prio:		@prio_map is valid
197  *
198  * @def_band:		default band to use
199  * @dscp_map:		list of DSCP to band mappings
200  *
201  * @root_qdisc:	pointer to the current root of the Qdisc hierarchy
202  * @qdiscs:	all qdiscs recorded by major part of the handle
203  */
204 struct nfp_abm_link {
205 	struct nfp_abm *abm;
206 	struct nfp_net *vnic;
207 	unsigned int id;
208 	unsigned int queue_base;
209 	unsigned int total_queues;
210 
211 	u64 last_stats_update;
212 
213 	u32 *prio_map;
214 	bool has_prio;
215 
216 	u8 def_band;
217 	struct list_head dscp_map;
218 
219 	struct nfp_qdisc *root_qdisc;
220 	struct radix_tree_root qdiscs;
221 };
222 
223 static inline bool nfp_abm_has_prio(struct nfp_abm *abm)
224 {
225 	return abm->num_bands > 1;
226 }
227 
228 static inline bool nfp_abm_has_drop(struct nfp_abm *abm)
229 {
230 	return abm->action_mask & BIT(NFP_ABM_ACT_DROP);
231 }
232 
233 static inline bool nfp_abm_has_mark(struct nfp_abm *abm)
234 {
235 	return abm->action_mask & BIT(NFP_ABM_ACT_MARK_DROP);
236 }
237 
238 void nfp_abm_qdisc_offload_update(struct nfp_abm_link *alink);
239 int nfp_abm_setup_root(struct net_device *netdev, struct nfp_abm_link *alink,
240 		       struct tc_root_qopt_offload *opt);
241 int nfp_abm_setup_tc_red(struct net_device *netdev, struct nfp_abm_link *alink,
242 			 struct tc_red_qopt_offload *opt);
243 int nfp_abm_setup_tc_mq(struct net_device *netdev, struct nfp_abm_link *alink,
244 			struct tc_mq_qopt_offload *opt);
245 int nfp_abm_setup_tc_gred(struct net_device *netdev, struct nfp_abm_link *alink,
246 			  struct tc_gred_qopt_offload *opt);
247 int nfp_abm_setup_cls_block(struct net_device *netdev, struct nfp_repr *repr,
248 			    struct tc_block_offload *opt);
249 
250 int nfp_abm_ctrl_read_params(struct nfp_abm_link *alink);
251 int nfp_abm_ctrl_find_addrs(struct nfp_abm *abm);
252 int __nfp_abm_ctrl_set_q_lvl(struct nfp_abm *abm, unsigned int id, u32 val);
253 int nfp_abm_ctrl_set_q_lvl(struct nfp_abm_link *alink, unsigned int band,
254 			   unsigned int queue, u32 val);
255 int __nfp_abm_ctrl_set_q_act(struct nfp_abm *abm, unsigned int id,
256 			     enum nfp_abm_q_action act);
257 int nfp_abm_ctrl_set_q_act(struct nfp_abm_link *alink, unsigned int band,
258 			   unsigned int queue, enum nfp_abm_q_action act);
259 int nfp_abm_ctrl_read_q_stats(struct nfp_abm_link *alink,
260 			      unsigned int band, unsigned int queue,
261 			      struct nfp_alink_stats *stats);
262 int nfp_abm_ctrl_read_q_xstats(struct nfp_abm_link *alink,
263 			       unsigned int band, unsigned int queue,
264 			       struct nfp_alink_xstats *xstats);
265 u64 nfp_abm_ctrl_stat_non_sto(struct nfp_abm_link *alink, unsigned int i);
266 u64 nfp_abm_ctrl_stat_sto(struct nfp_abm_link *alink, unsigned int i);
267 int nfp_abm_ctrl_qm_enable(struct nfp_abm *abm);
268 int nfp_abm_ctrl_qm_disable(struct nfp_abm *abm);
269 void nfp_abm_prio_map_update(struct nfp_abm *abm);
270 int nfp_abm_ctrl_prio_map_update(struct nfp_abm_link *alink, u32 *packed);
271 #endif
272