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/radix-tree.h>
9 #include <net/devlink.h>
10 #include <net/pkt_cls.h>
11 
12 /* Dump of 64 PRIOs and 256 REDs seems to take 850us on Xeon v4 @ 2.20GHz;
13  * 2.5ms / 400Hz seems more than sufficient for stats resolution.
14  */
15 #define NFP_ABM_STATS_REFRESH_IVAL	(2500 * 1000) /* ns */
16 
17 #define NFP_ABM_LVL_INFINITY		S32_MAX
18 
19 struct nfp_app;
20 struct nfp_net;
21 
22 #define NFP_ABM_PORTID_TYPE	GENMASK(23, 16)
23 #define NFP_ABM_PORTID_ID	GENMASK(7, 0)
24 
25 /**
26  * struct nfp_abm - ABM NIC app structure
27  * @app:	back pointer to nfp_app
28  * @pf_id:	ID of our PF link
29  *
30  * @num_prios:	number of supported DSCP priorities
31  * @num_bands:	number of supported DSCP priority bands
32  *
33  * @thresholds:		current threshold configuration
34  * @threshold_undef:	bitmap of thresholds which have not been set
35  * @num_thresholds:	number of @thresholds and bits in @threshold_undef
36  *
37  * @eswitch_mode:	devlink eswitch mode, advanced functions only visible
38  *			in switchdev mode
39  * @q_lvls:	queue level control area
40  * @qm_stats:	queue statistics symbol
41  * @q_stats:	basic queue statistics (only in per-band case)
42  */
43 struct nfp_abm {
44 	struct nfp_app *app;
45 	unsigned int pf_id;
46 
47 	unsigned int num_prios;
48 	unsigned int num_bands;
49 
50 	u32 *thresholds;
51 	unsigned long *threshold_undef;
52 	size_t num_thresholds;
53 
54 	enum devlink_eswitch_mode eswitch_mode;
55 	const struct nfp_rtsym *q_lvls;
56 	const struct nfp_rtsym *qm_stats;
57 	const struct nfp_rtsym *q_stats;
58 };
59 
60 /**
61  * struct nfp_alink_stats - ABM NIC statistics
62  * @tx_pkts:		number of TXed packets
63  * @tx_bytes:		number of TXed bytes
64  * @backlog_pkts:	momentary backlog length (packets)
65  * @backlog_bytes:	momentary backlog length (bytes)
66  * @overlimits:		number of ECN marked TXed packets (accumulative)
67  * @drops:		number of tail-dropped packets (accumulative)
68  */
69 struct nfp_alink_stats {
70 	u64 tx_pkts;
71 	u64 tx_bytes;
72 	u64 backlog_pkts;
73 	u64 backlog_bytes;
74 	u64 overlimits;
75 	u64 drops;
76 };
77 
78 /**
79  * struct nfp_alink_xstats - extended ABM NIC statistics
80  * @ecn_marked:		number of ECN marked TXed packets
81  * @pdrop:		number of hard drops due to queue limit
82  */
83 struct nfp_alink_xstats {
84 	u64 ecn_marked;
85 	u64 pdrop;
86 };
87 
88 enum nfp_qdisc_type {
89 	NFP_QDISC_NONE = 0,
90 	NFP_QDISC_MQ,
91 	NFP_QDISC_RED,
92 };
93 
94 #define NFP_QDISC_UNTRACKED	((struct nfp_qdisc *)1UL)
95 
96 /**
97  * struct nfp_qdisc - tracked TC Qdisc
98  * @netdev:		netdev on which Qdisc was created
99  * @type:		Qdisc type
100  * @handle:		handle of this Qdisc
101  * @parent_handle:	handle of the parent (unreliable if Qdisc was grafted)
102  * @use_cnt:		number of attachment points in the hierarchy
103  * @num_children:	current size of the @children array
104  * @children:		pointers to children
105  *
106  * @params_ok:		parameters of this Qdisc are OK for offload
107  * @offload_mark:	offload refresh state - selected for offload
108  * @offloaded:		Qdisc is currently offloaded to the HW
109  *
110  * @mq:			MQ Qdisc specific parameters and state
111  * @mq.stats:		current stats of the MQ Qdisc
112  * @mq.prev_stats:	previously reported @mq.stats
113  *
114  * @red:		RED Qdisc specific parameters and state
115  * @red.num_bands:	Number of valid entries in the @red.band table
116  * @red.band:		Per-band array of RED instances
117  * @red.band.threshold:		ECN marking threshold
118  * @red.band.stats:		current stats of the RED Qdisc
119  * @red.band.prev_stats:	previously reported @red.stats
120  * @red.band.xstats:		extended stats for RED - current
121  * @red.band.prev_xstats:	extended stats for RED - previously reported
122  */
123 struct nfp_qdisc {
124 	struct net_device *netdev;
125 	enum nfp_qdisc_type type;
126 	u32 handle;
127 	u32 parent_handle;
128 	unsigned int use_cnt;
129 	unsigned int num_children;
130 	struct nfp_qdisc **children;
131 
132 	bool params_ok;
133 	bool offload_mark;
134 	bool offloaded;
135 
136 	union {
137 		/* NFP_QDISC_MQ */
138 		struct {
139 			struct nfp_alink_stats stats;
140 			struct nfp_alink_stats prev_stats;
141 		} mq;
142 		/* TC_SETUP_QDISC_RED */
143 		struct {
144 			unsigned int num_bands;
145 
146 			struct {
147 				u32 threshold;
148 				struct nfp_alink_stats stats;
149 				struct nfp_alink_stats prev_stats;
150 				struct nfp_alink_xstats xstats;
151 				struct nfp_alink_xstats prev_xstats;
152 			} band[1];
153 		} red;
154 	};
155 };
156 
157 /**
158  * struct nfp_abm_link - port tuple of a ABM NIC
159  * @abm:	back pointer to nfp_abm
160  * @vnic:	data vNIC
161  * @id:		id of the data vNIC
162  * @queue_base:	id of base to host queue within PCIe (not QC idx)
163  * @total_queues:	number of PF queues
164  *
165  * @last_stats_update:	ktime of last stats update
166  *
167  * @root_qdisc:	pointer to the current root of the Qdisc hierarchy
168  * @qdiscs:	all qdiscs recorded by major part of the handle
169  */
170 struct nfp_abm_link {
171 	struct nfp_abm *abm;
172 	struct nfp_net *vnic;
173 	unsigned int id;
174 	unsigned int queue_base;
175 	unsigned int total_queues;
176 
177 	u64 last_stats_update;
178 
179 	struct nfp_qdisc *root_qdisc;
180 	struct radix_tree_root qdiscs;
181 };
182 
183 static inline bool nfp_abm_has_prio(struct nfp_abm *abm)
184 {
185 	return abm->num_bands > 1;
186 }
187 
188 void nfp_abm_qdisc_offload_update(struct nfp_abm_link *alink);
189 int nfp_abm_setup_root(struct net_device *netdev, struct nfp_abm_link *alink,
190 		       struct tc_root_qopt_offload *opt);
191 int nfp_abm_setup_tc_red(struct net_device *netdev, struct nfp_abm_link *alink,
192 			 struct tc_red_qopt_offload *opt);
193 int nfp_abm_setup_tc_mq(struct net_device *netdev, struct nfp_abm_link *alink,
194 			struct tc_mq_qopt_offload *opt);
195 
196 void nfp_abm_ctrl_read_params(struct nfp_abm_link *alink);
197 int nfp_abm_ctrl_find_addrs(struct nfp_abm *abm);
198 int __nfp_abm_ctrl_set_q_lvl(struct nfp_abm *abm, unsigned int id, u32 val);
199 int nfp_abm_ctrl_set_q_lvl(struct nfp_abm_link *alink, unsigned int band,
200 			   unsigned int queue, u32 val);
201 int nfp_abm_ctrl_read_q_stats(struct nfp_abm_link *alink,
202 			      unsigned int band, unsigned int queue,
203 			      struct nfp_alink_stats *stats);
204 int nfp_abm_ctrl_read_q_xstats(struct nfp_abm_link *alink,
205 			       unsigned int band, unsigned int queue,
206 			       struct nfp_alink_xstats *xstats);
207 u64 nfp_abm_ctrl_stat_non_sto(struct nfp_abm_link *alink, unsigned int i);
208 u64 nfp_abm_ctrl_stat_sto(struct nfp_abm_link *alink, unsigned int i);
209 int nfp_abm_ctrl_qm_enable(struct nfp_abm *abm);
210 int nfp_abm_ctrl_qm_disable(struct nfp_abm *abm);
211 #endif
212