1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
3 
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
7 
8 #include <net/mana/mana.h>
9 
10 static const struct {
11 	char name[ETH_GSTRING_LEN];
12 	u16 offset;
13 } mana_eth_stats[] = {
14 	{"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)},
15 	{"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)},
16 	{"tx_cq_err", offsetof(struct mana_ethtool_stats, tx_cqe_err)},
17 	{"tx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
18 					tx_cqe_unknown_type)},
19 	{"rx_coalesced_err", offsetof(struct mana_ethtool_stats,
20 					rx_coalesced_err)},
21 	{"rx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
22 					rx_cqe_unknown_type)},
23 };
24 
25 static int mana_get_sset_count(struct net_device *ndev, int stringset)
26 {
27 	struct mana_port_context *apc = netdev_priv(ndev);
28 	unsigned int num_queues = apc->num_queues;
29 
30 	if (stringset != ETH_SS_STATS)
31 		return -EINVAL;
32 
33 	return ARRAY_SIZE(mana_eth_stats) + num_queues *
34 				(MANA_STATS_RX_COUNT + MANA_STATS_TX_COUNT);
35 }
36 
37 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
38 {
39 	struct mana_port_context *apc = netdev_priv(ndev);
40 	unsigned int num_queues = apc->num_queues;
41 	u8 *p = data;
42 	int i;
43 
44 	if (stringset != ETH_SS_STATS)
45 		return;
46 
47 	for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++) {
48 		memcpy(p, mana_eth_stats[i].name, ETH_GSTRING_LEN);
49 		p += ETH_GSTRING_LEN;
50 	}
51 
52 	for (i = 0; i < num_queues; i++) {
53 		sprintf(p, "rx_%d_packets", i);
54 		p += ETH_GSTRING_LEN;
55 		sprintf(p, "rx_%d_bytes", i);
56 		p += ETH_GSTRING_LEN;
57 		sprintf(p, "rx_%d_xdp_drop", i);
58 		p += ETH_GSTRING_LEN;
59 		sprintf(p, "rx_%d_xdp_tx", i);
60 		p += ETH_GSTRING_LEN;
61 		sprintf(p, "rx_%d_xdp_redirect", i);
62 		p += ETH_GSTRING_LEN;
63 	}
64 
65 	for (i = 0; i < num_queues; i++) {
66 		sprintf(p, "tx_%d_packets", i);
67 		p += ETH_GSTRING_LEN;
68 		sprintf(p, "tx_%d_bytes", i);
69 		p += ETH_GSTRING_LEN;
70 		sprintf(p, "tx_%d_xdp_xmit", i);
71 		p += ETH_GSTRING_LEN;
72 		sprintf(p, "tx_%d_tso_packets", i);
73 		p += ETH_GSTRING_LEN;
74 		sprintf(p, "tx_%d_tso_bytes", i);
75 		p += ETH_GSTRING_LEN;
76 		sprintf(p, "tx_%d_tso_inner_packets", i);
77 		p += ETH_GSTRING_LEN;
78 		sprintf(p, "tx_%d_tso_inner_bytes", i);
79 		p += ETH_GSTRING_LEN;
80 		sprintf(p, "tx_%d_long_pkt_fmt", i);
81 		p += ETH_GSTRING_LEN;
82 		sprintf(p, "tx_%d_short_pkt_fmt", i);
83 		p += ETH_GSTRING_LEN;
84 		sprintf(p, "tx_%d_csum_partial", i);
85 		p += ETH_GSTRING_LEN;
86 		sprintf(p, "tx_%d_mana_map_err", i);
87 		p += ETH_GSTRING_LEN;
88 	}
89 }
90 
91 static void mana_get_ethtool_stats(struct net_device *ndev,
92 				   struct ethtool_stats *e_stats, u64 *data)
93 {
94 	struct mana_port_context *apc = netdev_priv(ndev);
95 	unsigned int num_queues = apc->num_queues;
96 	void *eth_stats = &apc->eth_stats;
97 	struct mana_stats_rx *rx_stats;
98 	struct mana_stats_tx *tx_stats;
99 	unsigned int start;
100 	u64 packets, bytes;
101 	u64 xdp_redirect;
102 	u64 xdp_xmit;
103 	u64 xdp_drop;
104 	u64 xdp_tx;
105 	u64 tso_packets;
106 	u64 tso_bytes;
107 	u64 tso_inner_packets;
108 	u64 tso_inner_bytes;
109 	u64 long_pkt_fmt;
110 	u64 short_pkt_fmt;
111 	u64 csum_partial;
112 	u64 mana_map_err;
113 	int q, i = 0;
114 
115 	if (!apc->port_is_up)
116 		return;
117 
118 	for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
119 		data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
120 
121 	for (q = 0; q < num_queues; q++) {
122 		rx_stats = &apc->rxqs[q]->stats;
123 
124 		do {
125 			start = u64_stats_fetch_begin(&rx_stats->syncp);
126 			packets = rx_stats->packets;
127 			bytes = rx_stats->bytes;
128 			xdp_drop = rx_stats->xdp_drop;
129 			xdp_tx = rx_stats->xdp_tx;
130 			xdp_redirect = rx_stats->xdp_redirect;
131 		} while (u64_stats_fetch_retry(&rx_stats->syncp, start));
132 
133 		data[i++] = packets;
134 		data[i++] = bytes;
135 		data[i++] = xdp_drop;
136 		data[i++] = xdp_tx;
137 		data[i++] = xdp_redirect;
138 	}
139 
140 	for (q = 0; q < num_queues; q++) {
141 		tx_stats = &apc->tx_qp[q].txq.stats;
142 
143 		do {
144 			start = u64_stats_fetch_begin(&tx_stats->syncp);
145 			packets = tx_stats->packets;
146 			bytes = tx_stats->bytes;
147 			xdp_xmit = tx_stats->xdp_xmit;
148 			tso_packets = tx_stats->tso_packets;
149 			tso_bytes = tx_stats->tso_bytes;
150 			tso_inner_packets = tx_stats->tso_inner_packets;
151 			tso_inner_bytes = tx_stats->tso_inner_bytes;
152 			long_pkt_fmt = tx_stats->long_pkt_fmt;
153 			short_pkt_fmt = tx_stats->short_pkt_fmt;
154 			csum_partial = tx_stats->csum_partial;
155 			mana_map_err = tx_stats->mana_map_err;
156 		} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
157 
158 		data[i++] = packets;
159 		data[i++] = bytes;
160 		data[i++] = xdp_xmit;
161 		data[i++] = tso_packets;
162 		data[i++] = tso_bytes;
163 		data[i++] = tso_inner_packets;
164 		data[i++] = tso_inner_bytes;
165 		data[i++] = long_pkt_fmt;
166 		data[i++] = short_pkt_fmt;
167 		data[i++] = csum_partial;
168 		data[i++] = mana_map_err;
169 	}
170 }
171 
172 static int mana_get_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *cmd,
173 			  u32 *rules)
174 {
175 	struct mana_port_context *apc = netdev_priv(ndev);
176 
177 	switch (cmd->cmd) {
178 	case ETHTOOL_GRXRINGS:
179 		cmd->data = apc->num_queues;
180 		return 0;
181 	}
182 
183 	return -EOPNOTSUPP;
184 }
185 
186 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
187 {
188 	return MANA_HASH_KEY_SIZE;
189 }
190 
191 static u32 mana_rss_indir_size(struct net_device *ndev)
192 {
193 	return MANA_INDIRECT_TABLE_SIZE;
194 }
195 
196 static int mana_get_rxfh(struct net_device *ndev, u32 *indir, u8 *key,
197 			 u8 *hfunc)
198 {
199 	struct mana_port_context *apc = netdev_priv(ndev);
200 	int i;
201 
202 	if (hfunc)
203 		*hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
204 
205 	if (indir) {
206 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
207 			indir[i] = apc->indir_table[i];
208 	}
209 
210 	if (key)
211 		memcpy(key, apc->hashkey, MANA_HASH_KEY_SIZE);
212 
213 	return 0;
214 }
215 
216 static int mana_set_rxfh(struct net_device *ndev, const u32 *indir,
217 			 const u8 *key, const u8 hfunc)
218 {
219 	struct mana_port_context *apc = netdev_priv(ndev);
220 	bool update_hash = false, update_table = false;
221 	u32 save_table[MANA_INDIRECT_TABLE_SIZE];
222 	u8 save_key[MANA_HASH_KEY_SIZE];
223 	int i, err;
224 
225 	if (!apc->port_is_up)
226 		return -EOPNOTSUPP;
227 
228 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
229 		return -EOPNOTSUPP;
230 
231 	if (indir) {
232 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
233 			if (indir[i] >= apc->num_queues)
234 				return -EINVAL;
235 
236 		update_table = true;
237 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
238 			save_table[i] = apc->indir_table[i];
239 			apc->indir_table[i] = indir[i];
240 		}
241 	}
242 
243 	if (key) {
244 		update_hash = true;
245 		memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
246 		memcpy(apc->hashkey, key, MANA_HASH_KEY_SIZE);
247 	}
248 
249 	err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
250 
251 	if (err) { /* recover to original values */
252 		if (update_table) {
253 			for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
254 				apc->indir_table[i] = save_table[i];
255 		}
256 
257 		if (update_hash)
258 			memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
259 
260 		mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
261 	}
262 
263 	return err;
264 }
265 
266 static void mana_get_channels(struct net_device *ndev,
267 			      struct ethtool_channels *channel)
268 {
269 	struct mana_port_context *apc = netdev_priv(ndev);
270 
271 	channel->max_combined = apc->max_queues;
272 	channel->combined_count = apc->num_queues;
273 }
274 
275 static int mana_set_channels(struct net_device *ndev,
276 			     struct ethtool_channels *channels)
277 {
278 	struct mana_port_context *apc = netdev_priv(ndev);
279 	unsigned int new_count = channels->combined_count;
280 	unsigned int old_count = apc->num_queues;
281 	int err, err2;
282 
283 	err = mana_detach(ndev, false);
284 	if (err) {
285 		netdev_err(ndev, "mana_detach failed: %d\n", err);
286 		return err;
287 	}
288 
289 	apc->num_queues = new_count;
290 	err = mana_attach(ndev);
291 	if (!err)
292 		return 0;
293 
294 	netdev_err(ndev, "mana_attach failed: %d\n", err);
295 
296 	/* Try to roll it back to the old configuration. */
297 	apc->num_queues = old_count;
298 	err2 = mana_attach(ndev);
299 	if (err2)
300 		netdev_err(ndev, "mana re-attach failed: %d\n", err2);
301 
302 	return err;
303 }
304 
305 const struct ethtool_ops mana_ethtool_ops = {
306 	.get_ethtool_stats	= mana_get_ethtool_stats,
307 	.get_sset_count		= mana_get_sset_count,
308 	.get_strings		= mana_get_strings,
309 	.get_rxnfc		= mana_get_rxnfc,
310 	.get_rxfh_key_size	= mana_get_rxfh_key_size,
311 	.get_rxfh_indir_size	= mana_rss_indir_size,
312 	.get_rxfh		= mana_get_rxfh,
313 	.set_rxfh		= mana_set_rxfh,
314 	.get_channels		= mana_get_channels,
315 	.set_channels		= mana_set_channels,
316 };
317