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  */
42 struct nfp_abm {
43 	struct nfp_app *app;
44 	unsigned int pf_id;
45 
46 	unsigned int num_prios;
47 	unsigned int num_bands;
48 
49 	u32 *thresholds;
50 	unsigned long *threshold_undef;
51 	size_t num_thresholds;
52 
53 	enum devlink_eswitch_mode eswitch_mode;
54 	const struct nfp_rtsym *q_lvls;
55 	const struct nfp_rtsym *qm_stats;
56 };
57 
58 /**
59  * struct nfp_alink_stats - ABM NIC statistics
60  * @tx_pkts:		number of TXed packets
61  * @tx_bytes:		number of TXed bytes
62  * @backlog_pkts:	momentary backlog length (packets)
63  * @backlog_bytes:	momentary backlog length (bytes)
64  * @overlimits:		number of ECN marked TXed packets (accumulative)
65  * @drops:		number of tail-dropped packets (accumulative)
66  */
67 struct nfp_alink_stats {
68 	u64 tx_pkts;
69 	u64 tx_bytes;
70 	u64 backlog_pkts;
71 	u64 backlog_bytes;
72 	u64 overlimits;
73 	u64 drops;
74 };
75 
76 /**
77  * struct nfp_alink_xstats - extended ABM NIC statistics
78  * @ecn_marked:		number of ECN marked TXed packets
79  * @pdrop:		number of hard drops due to queue limit
80  */
81 struct nfp_alink_xstats {
82 	u64 ecn_marked;
83 	u64 pdrop;
84 };
85 
86 enum nfp_qdisc_type {
87 	NFP_QDISC_NONE = 0,
88 	NFP_QDISC_MQ,
89 	NFP_QDISC_RED,
90 };
91 
92 #define NFP_QDISC_UNTRACKED	((struct nfp_qdisc *)1UL)
93 
94 /**
95  * struct nfp_qdisc - tracked TC Qdisc
96  * @netdev:		netdev on which Qdisc was created
97  * @type:		Qdisc type
98  * @handle:		handle of this Qdisc
99  * @parent_handle:	handle of the parent (unreliable if Qdisc was grafted)
100  * @use_cnt:		number of attachment points in the hierarchy
101  * @num_children:	current size of the @children array
102  * @children:		pointers to children
103  *
104  * @params_ok:		parameters of this Qdisc are OK for offload
105  * @offload_mark:	offload refresh state - selected for offload
106  * @offloaded:		Qdisc is currently offloaded to the HW
107  *
108  * @mq:			MQ Qdisc specific parameters and state
109  * @mq.stats:		current stats of the MQ Qdisc
110  * @mq.prev_stats:	previously reported @mq.stats
111  *
112  * @red:		RED Qdisc specific parameters and state
113  * @red.threshold:	ECN marking threshold
114  * @red.stats:		current stats of the RED Qdisc
115  * @red.prev_stats:	previously reported @red.stats
116  * @red.xstats:		extended stats for RED - current
117  * @red.prev_xstats:	extended stats for RED - previously reported
118  */
119 struct nfp_qdisc {
120 	struct net_device *netdev;
121 	enum nfp_qdisc_type type;
122 	u32 handle;
123 	u32 parent_handle;
124 	unsigned int use_cnt;
125 	unsigned int num_children;
126 	struct nfp_qdisc **children;
127 
128 	bool params_ok;
129 	bool offload_mark;
130 	bool offloaded;
131 
132 	union {
133 		/* NFP_QDISC_MQ */
134 		struct {
135 			struct nfp_alink_stats stats;
136 			struct nfp_alink_stats prev_stats;
137 		} mq;
138 		/* TC_SETUP_QDISC_RED */
139 		struct {
140 			u32 threshold;
141 			struct nfp_alink_stats stats;
142 			struct nfp_alink_stats prev_stats;
143 			struct nfp_alink_xstats xstats;
144 			struct nfp_alink_xstats prev_xstats;
145 		} red;
146 	};
147 };
148 
149 /**
150  * struct nfp_abm_link - port tuple of a ABM NIC
151  * @abm:	back pointer to nfp_abm
152  * @vnic:	data vNIC
153  * @id:		id of the data vNIC
154  * @queue_base:	id of base to host queue within PCIe (not QC idx)
155  * @total_queues:	number of PF queues
156  *
157  * @last_stats_update:	ktime of last stats update
158  *
159  * @root_qdisc:	pointer to the current root of the Qdisc hierarchy
160  * @qdiscs:	all qdiscs recorded by major part of the handle
161  */
162 struct nfp_abm_link {
163 	struct nfp_abm *abm;
164 	struct nfp_net *vnic;
165 	unsigned int id;
166 	unsigned int queue_base;
167 	unsigned int total_queues;
168 
169 	u64 last_stats_update;
170 
171 	struct nfp_qdisc *root_qdisc;
172 	struct radix_tree_root qdiscs;
173 };
174 
175 static inline bool nfp_abm_has_prio(struct nfp_abm *abm)
176 {
177 	return abm->num_bands > 1;
178 }
179 
180 void nfp_abm_qdisc_offload_update(struct nfp_abm_link *alink);
181 int nfp_abm_setup_root(struct net_device *netdev, struct nfp_abm_link *alink,
182 		       struct tc_root_qopt_offload *opt);
183 int nfp_abm_setup_tc_red(struct net_device *netdev, struct nfp_abm_link *alink,
184 			 struct tc_red_qopt_offload *opt);
185 int nfp_abm_setup_tc_mq(struct net_device *netdev, struct nfp_abm_link *alink,
186 			struct tc_mq_qopt_offload *opt);
187 
188 void nfp_abm_ctrl_read_params(struct nfp_abm_link *alink);
189 int nfp_abm_ctrl_find_addrs(struct nfp_abm *abm);
190 int __nfp_abm_ctrl_set_q_lvl(struct nfp_abm *abm, unsigned int id, u32 val);
191 int nfp_abm_ctrl_set_q_lvl(struct nfp_abm_link *alink, unsigned int queue,
192 			   u32 val);
193 int nfp_abm_ctrl_read_q_stats(struct nfp_abm_link *alink, unsigned int i,
194 			      struct nfp_alink_stats *stats);
195 int nfp_abm_ctrl_read_q_xstats(struct nfp_abm_link *alink, unsigned int i,
196 			       struct nfp_alink_xstats *xstats);
197 u64 nfp_abm_ctrl_stat_non_sto(struct nfp_abm_link *alink, unsigned int i);
198 u64 nfp_abm_ctrl_stat_sto(struct nfp_abm_link *alink, unsigned int i);
199 int nfp_abm_ctrl_qm_enable(struct nfp_abm *abm);
200 int nfp_abm_ctrl_qm_disable(struct nfp_abm *abm);
201 #endif
202