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.threshold:	ECN marking threshold
116  * @red.stats:		current stats of the RED Qdisc
117  * @red.prev_stats:	previously reported @red.stats
118  * @red.xstats:		extended stats for RED - current
119  * @red.prev_xstats:	extended stats for RED - previously reported
120  */
121 struct nfp_qdisc {
122 	struct net_device *netdev;
123 	enum nfp_qdisc_type type;
124 	u32 handle;
125 	u32 parent_handle;
126 	unsigned int use_cnt;
127 	unsigned int num_children;
128 	struct nfp_qdisc **children;
129 
130 	bool params_ok;
131 	bool offload_mark;
132 	bool offloaded;
133 
134 	union {
135 		/* NFP_QDISC_MQ */
136 		struct {
137 			struct nfp_alink_stats stats;
138 			struct nfp_alink_stats prev_stats;
139 		} mq;
140 		/* TC_SETUP_QDISC_RED */
141 		struct {
142 			u32 threshold;
143 			struct nfp_alink_stats stats;
144 			struct nfp_alink_stats prev_stats;
145 			struct nfp_alink_xstats xstats;
146 			struct nfp_alink_xstats prev_xstats;
147 		} red;
148 	};
149 };
150 
151 /**
152  * struct nfp_abm_link - port tuple of a ABM NIC
153  * @abm:	back pointer to nfp_abm
154  * @vnic:	data vNIC
155  * @id:		id of the data vNIC
156  * @queue_base:	id of base to host queue within PCIe (not QC idx)
157  * @total_queues:	number of PF queues
158  *
159  * @last_stats_update:	ktime of last stats update
160  *
161  * @root_qdisc:	pointer to the current root of the Qdisc hierarchy
162  * @qdiscs:	all qdiscs recorded by major part of the handle
163  */
164 struct nfp_abm_link {
165 	struct nfp_abm *abm;
166 	struct nfp_net *vnic;
167 	unsigned int id;
168 	unsigned int queue_base;
169 	unsigned int total_queues;
170 
171 	u64 last_stats_update;
172 
173 	struct nfp_qdisc *root_qdisc;
174 	struct radix_tree_root qdiscs;
175 };
176 
177 static inline bool nfp_abm_has_prio(struct nfp_abm *abm)
178 {
179 	return abm->num_bands > 1;
180 }
181 
182 void nfp_abm_qdisc_offload_update(struct nfp_abm_link *alink);
183 int nfp_abm_setup_root(struct net_device *netdev, struct nfp_abm_link *alink,
184 		       struct tc_root_qopt_offload *opt);
185 int nfp_abm_setup_tc_red(struct net_device *netdev, struct nfp_abm_link *alink,
186 			 struct tc_red_qopt_offload *opt);
187 int nfp_abm_setup_tc_mq(struct net_device *netdev, struct nfp_abm_link *alink,
188 			struct tc_mq_qopt_offload *opt);
189 
190 void nfp_abm_ctrl_read_params(struct nfp_abm_link *alink);
191 int nfp_abm_ctrl_find_addrs(struct nfp_abm *abm);
192 int __nfp_abm_ctrl_set_q_lvl(struct nfp_abm *abm, unsigned int id, u32 val);
193 int nfp_abm_ctrl_set_q_lvl(struct nfp_abm_link *alink, unsigned int band,
194 			   unsigned int queue, u32 val);
195 int nfp_abm_ctrl_read_q_stats(struct nfp_abm_link *alink,
196 			      unsigned int band, unsigned int queue,
197 			      struct nfp_alink_stats *stats);
198 int nfp_abm_ctrl_read_q_xstats(struct nfp_abm_link *alink,
199 			       unsigned int band, unsigned int queue,
200 			       struct nfp_alink_xstats *xstats);
201 u64 nfp_abm_ctrl_stat_non_sto(struct nfp_abm_link *alink, unsigned int i);
202 u64 nfp_abm_ctrl_stat_sto(struct nfp_abm_link *alink, unsigned int i);
203 int nfp_abm_ctrl_qm_enable(struct nfp_abm *abm);
204 int nfp_abm_ctrl_qm_disable(struct nfp_abm *abm);
205 #endif
206