1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/netdevice.h>
10 #include <linux/ethtool.h>
11 
12 #include "octep_config.h"
13 #include "octep_main.h"
14 #include "octep_ctrl_net.h"
15 
16 static const char octep_gstrings_global_stats[][ETH_GSTRING_LEN] = {
17 	"rx_packets",
18 	"tx_packets",
19 	"rx_bytes",
20 	"tx_bytes",
21 	"rx_alloc_errors",
22 	"tx_busy_errors",
23 	"rx_dropped",
24 	"tx_dropped",
25 	"tx_hw_pkts",
26 	"tx_hw_octs",
27 	"tx_hw_bcast",
28 	"tx_hw_mcast",
29 	"tx_hw_underflow",
30 	"tx_hw_control",
31 	"tx_less_than_64",
32 	"tx_equal_64",
33 	"tx_equal_65_to_127",
34 	"tx_equal_128_to_255",
35 	"tx_equal_256_to_511",
36 	"tx_equal_512_to_1023",
37 	"tx_equal_1024_to_1518",
38 	"tx_greater_than_1518",
39 	"rx_hw_pkts",
40 	"rx_hw_bytes",
41 	"rx_hw_bcast",
42 	"rx_hw_mcast",
43 	"rx_pause_pkts",
44 	"rx_pause_bytes",
45 	"rx_dropped_pkts_fifo_full",
46 	"rx_dropped_bytes_fifo_full",
47 	"rx_err_pkts",
48 };
49 
50 #define OCTEP_GLOBAL_STATS_CNT (sizeof(octep_gstrings_global_stats) / ETH_GSTRING_LEN)
51 
52 static const char octep_gstrings_tx_q_stats[][ETH_GSTRING_LEN] = {
53 	"tx_packets_posted[Q-%u]",
54 	"tx_packets_completed[Q-%u]",
55 	"tx_bytes[Q-%u]",
56 	"tx_busy[Q-%u]",
57 };
58 
59 #define OCTEP_TX_Q_STATS_CNT (sizeof(octep_gstrings_tx_q_stats) / ETH_GSTRING_LEN)
60 
61 static const char octep_gstrings_rx_q_stats[][ETH_GSTRING_LEN] = {
62 	"rx_packets[Q-%u]",
63 	"rx_bytes[Q-%u]",
64 	"rx_alloc_errors[Q-%u]",
65 };
66 
67 #define OCTEP_RX_Q_STATS_CNT (sizeof(octep_gstrings_rx_q_stats) / ETH_GSTRING_LEN)
68 
69 static void octep_get_drvinfo(struct net_device *netdev,
70 			      struct ethtool_drvinfo *info)
71 {
72 	struct octep_device *oct = netdev_priv(netdev);
73 
74 	strscpy(info->driver, OCTEP_DRV_NAME, sizeof(info->driver));
75 	strscpy(info->bus_info, pci_name(oct->pdev), sizeof(info->bus_info));
76 }
77 
78 static void octep_get_strings(struct net_device *netdev,
79 			      u32 stringset, u8 *data)
80 {
81 	struct octep_device *oct = netdev_priv(netdev);
82 	u16 num_queues = CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf);
83 	char *strings = (char *)data;
84 	int i, j;
85 
86 	switch (stringset) {
87 	case ETH_SS_STATS:
88 		for (i = 0; i < OCTEP_GLOBAL_STATS_CNT; i++) {
89 			snprintf(strings, ETH_GSTRING_LEN,
90 				 octep_gstrings_global_stats[i]);
91 			strings += ETH_GSTRING_LEN;
92 		}
93 
94 		for (i = 0; i < num_queues; i++) {
95 			for (j = 0; j < OCTEP_TX_Q_STATS_CNT; j++) {
96 				snprintf(strings, ETH_GSTRING_LEN,
97 					 octep_gstrings_tx_q_stats[j], i);
98 				strings += ETH_GSTRING_LEN;
99 			}
100 		}
101 
102 		for (i = 0; i < num_queues; i++) {
103 			for (j = 0; j < OCTEP_RX_Q_STATS_CNT; j++) {
104 				snprintf(strings, ETH_GSTRING_LEN,
105 					 octep_gstrings_rx_q_stats[j], i);
106 				strings += ETH_GSTRING_LEN;
107 			}
108 		}
109 		break;
110 	default:
111 		break;
112 	}
113 }
114 
115 static int octep_get_sset_count(struct net_device *netdev, int sset)
116 {
117 	struct octep_device *oct = netdev_priv(netdev);
118 	u16 num_queues = CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf);
119 
120 	switch (sset) {
121 	case ETH_SS_STATS:
122 		return OCTEP_GLOBAL_STATS_CNT + (num_queues *
123 		       (OCTEP_TX_Q_STATS_CNT + OCTEP_RX_Q_STATS_CNT));
124 		break;
125 	default:
126 		return -EOPNOTSUPP;
127 	}
128 }
129 
130 static void
131 octep_get_ethtool_stats(struct net_device *netdev,
132 			struct ethtool_stats *stats, u64 *data)
133 {
134 	struct octep_device *oct = netdev_priv(netdev);
135 	struct octep_iface_tx_stats *iface_tx_stats;
136 	struct octep_iface_rx_stats *iface_rx_stats;
137 	u64 rx_packets, rx_bytes;
138 	u64 tx_packets, tx_bytes;
139 	u64 rx_alloc_errors, tx_busy_errors;
140 	int q, i;
141 
142 	rx_packets = 0;
143 	rx_bytes = 0;
144 	tx_packets = 0;
145 	tx_bytes = 0;
146 	rx_alloc_errors = 0;
147 	tx_busy_errors = 0;
148 	tx_packets = 0;
149 	tx_bytes = 0;
150 	rx_packets = 0;
151 	rx_bytes = 0;
152 
153 	octep_get_if_stats(oct);
154 	iface_tx_stats = &oct->iface_tx_stats;
155 	iface_rx_stats = &oct->iface_rx_stats;
156 
157 	for (q = 0; q < oct->num_oqs; q++) {
158 		struct octep_iq *iq = oct->iq[q];
159 		struct octep_oq *oq = oct->oq[q];
160 
161 		tx_packets += iq->stats.instr_completed;
162 		tx_bytes += iq->stats.bytes_sent;
163 		tx_busy_errors += iq->stats.tx_busy;
164 
165 		rx_packets += oq->stats.packets;
166 		rx_bytes += oq->stats.bytes;
167 		rx_alloc_errors += oq->stats.alloc_failures;
168 	}
169 	i = 0;
170 	data[i++] = rx_packets;
171 	data[i++] = tx_packets;
172 	data[i++] = rx_bytes;
173 	data[i++] = tx_bytes;
174 	data[i++] = rx_alloc_errors;
175 	data[i++] = tx_busy_errors;
176 	data[i++] = iface_rx_stats->dropped_pkts_fifo_full +
177 		    iface_rx_stats->err_pkts;
178 	data[i++] = iface_tx_stats->xscol +
179 		    iface_tx_stats->xsdef;
180 	data[i++] = iface_tx_stats->pkts;
181 	data[i++] = iface_tx_stats->octs;
182 	data[i++] = iface_tx_stats->bcst;
183 	data[i++] = iface_tx_stats->mcst;
184 	data[i++] = iface_tx_stats->undflw;
185 	data[i++] = iface_tx_stats->ctl;
186 	data[i++] = iface_tx_stats->hist_lt64;
187 	data[i++] = iface_tx_stats->hist_eq64;
188 	data[i++] = iface_tx_stats->hist_65to127;
189 	data[i++] = iface_tx_stats->hist_128to255;
190 	data[i++] = iface_tx_stats->hist_256to511;
191 	data[i++] = iface_tx_stats->hist_512to1023;
192 	data[i++] = iface_tx_stats->hist_1024to1518;
193 	data[i++] = iface_tx_stats->hist_gt1518;
194 	data[i++] = iface_rx_stats->pkts;
195 	data[i++] = iface_rx_stats->octets;
196 	data[i++] = iface_rx_stats->mcast_pkts;
197 	data[i++] = iface_rx_stats->bcast_pkts;
198 	data[i++] = iface_rx_stats->pause_pkts;
199 	data[i++] = iface_rx_stats->pause_octets;
200 	data[i++] = iface_rx_stats->dropped_pkts_fifo_full;
201 	data[i++] = iface_rx_stats->dropped_octets_fifo_full;
202 	data[i++] = iface_rx_stats->err_pkts;
203 
204 	/* Per Tx Queue stats */
205 	for (q = 0; q < oct->num_iqs; q++) {
206 		struct octep_iq *iq = oct->iq[q];
207 
208 		data[i++] = iq->stats.instr_posted;
209 		data[i++] = iq->stats.instr_completed;
210 		data[i++] = iq->stats.bytes_sent;
211 		data[i++] = iq->stats.tx_busy;
212 	}
213 
214 	/* Per Rx Queue stats */
215 	for (q = 0; q < oct->num_oqs; q++) {
216 		struct octep_oq *oq = oct->oq[q];
217 
218 		data[i++] = oq->stats.packets;
219 		data[i++] = oq->stats.bytes;
220 		data[i++] = oq->stats.alloc_failures;
221 	}
222 }
223 
224 #define OCTEP_SET_ETHTOOL_LINK_MODES_BITMAP(octep_speeds, ksettings, name) \
225 { \
226 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_T)) \
227 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseT_Full); \
228 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_R)) \
229 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseR_FEC); \
230 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_CR)) \
231 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseCR_Full); \
232 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_KR)) \
233 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseKR_Full); \
234 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_LR)) \
235 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseLR_Full); \
236 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_10GBASE_SR)) \
237 		ethtool_link_ksettings_add_link_mode(ksettings, name, 10000baseSR_Full); \
238 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_25GBASE_CR)) \
239 		ethtool_link_ksettings_add_link_mode(ksettings, name, 25000baseCR_Full); \
240 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_25GBASE_KR)) \
241 		ethtool_link_ksettings_add_link_mode(ksettings, name, 25000baseKR_Full); \
242 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_25GBASE_SR)) \
243 		ethtool_link_ksettings_add_link_mode(ksettings, name, 25000baseSR_Full); \
244 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_40GBASE_CR4)) \
245 		ethtool_link_ksettings_add_link_mode(ksettings, name, 40000baseCR4_Full); \
246 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_40GBASE_KR4)) \
247 		ethtool_link_ksettings_add_link_mode(ksettings, name, 40000baseKR4_Full); \
248 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_40GBASE_LR4)) \
249 		ethtool_link_ksettings_add_link_mode(ksettings, name, 40000baseLR4_Full); \
250 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_40GBASE_SR4)) \
251 		ethtool_link_ksettings_add_link_mode(ksettings, name, 40000baseSR4_Full); \
252 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_CR2)) \
253 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseCR2_Full); \
254 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_KR2)) \
255 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseKR2_Full); \
256 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_SR2)) \
257 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseSR2_Full); \
258 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_CR)) \
259 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseCR_Full); \
260 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_KR)) \
261 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseKR_Full); \
262 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_LR)) \
263 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseLR_ER_FR_Full); \
264 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_50GBASE_SR)) \
265 		ethtool_link_ksettings_add_link_mode(ksettings, name, 50000baseSR_Full); \
266 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_100GBASE_CR4)) \
267 		ethtool_link_ksettings_add_link_mode(ksettings, name, 100000baseCR4_Full); \
268 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_100GBASE_KR4)) \
269 		ethtool_link_ksettings_add_link_mode(ksettings, name, 100000baseKR4_Full); \
270 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_100GBASE_LR4)) \
271 		ethtool_link_ksettings_add_link_mode(ksettings, name, 100000baseLR4_ER4_Full); \
272 	if ((octep_speeds) & BIT(OCTEP_LINK_MODE_100GBASE_SR4)) \
273 		ethtool_link_ksettings_add_link_mode(ksettings, name, 100000baseSR4_Full); \
274 }
275 
276 static int octep_get_link_ksettings(struct net_device *netdev,
277 				    struct ethtool_link_ksettings *cmd)
278 {
279 	struct octep_device *oct = netdev_priv(netdev);
280 	struct octep_iface_link_info *link_info;
281 	u32 advertised_modes, supported_modes;
282 
283 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
284 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
285 
286 	octep_get_link_info(oct);
287 
288 	advertised_modes = oct->link_info.advertised_modes;
289 	supported_modes = oct->link_info.supported_modes;
290 	link_info = &oct->link_info;
291 
292 	OCTEP_SET_ETHTOOL_LINK_MODES_BITMAP(supported_modes, cmd, supported);
293 	OCTEP_SET_ETHTOOL_LINK_MODES_BITMAP(advertised_modes, cmd, advertising);
294 
295 	if (link_info->autoneg) {
296 		if (link_info->autoneg & OCTEP_LINK_MODE_AUTONEG_SUPPORTED)
297 			ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
298 		if (link_info->autoneg & OCTEP_LINK_MODE_AUTONEG_ADVERTISED) {
299 			ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
300 			cmd->base.autoneg = AUTONEG_ENABLE;
301 		} else {
302 			cmd->base.autoneg = AUTONEG_DISABLE;
303 		}
304 	} else {
305 		cmd->base.autoneg = AUTONEG_DISABLE;
306 	}
307 
308 	if (link_info->pause) {
309 		if (link_info->pause & OCTEP_LINK_MODE_PAUSE_SUPPORTED)
310 			ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
311 		if (link_info->pause & OCTEP_LINK_MODE_PAUSE_ADVERTISED)
312 			ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
313 	}
314 
315 	cmd->base.port = PORT_FIBRE;
316 	ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
317 	ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
318 
319 	if (netif_carrier_ok(netdev)) {
320 		cmd->base.speed = link_info->speed;
321 		cmd->base.duplex = DUPLEX_FULL;
322 	} else {
323 		cmd->base.speed = SPEED_UNKNOWN;
324 		cmd->base.duplex = DUPLEX_UNKNOWN;
325 	}
326 	return 0;
327 }
328 
329 static int octep_set_link_ksettings(struct net_device *netdev,
330 				    const struct ethtool_link_ksettings *cmd)
331 {
332 	struct octep_device *oct = netdev_priv(netdev);
333 	struct octep_iface_link_info link_info_new;
334 	struct octep_iface_link_info *link_info;
335 	u64 advertised = 0;
336 	u8 autoneg = 0;
337 	int err;
338 
339 	link_info = &oct->link_info;
340 	memcpy(&link_info_new, link_info, sizeof(struct octep_iface_link_info));
341 
342 	/* Only Full duplex is supported;
343 	 * Assume full duplex when duplex is unknown.
344 	 */
345 	if (cmd->base.duplex != DUPLEX_FULL &&
346 	    cmd->base.duplex != DUPLEX_UNKNOWN)
347 		return -EOPNOTSUPP;
348 
349 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
350 		if (!(link_info->autoneg & OCTEP_LINK_MODE_AUTONEG_SUPPORTED))
351 			return -EOPNOTSUPP;
352 		autoneg = 1;
353 	}
354 
355 	if (!bitmap_subset(cmd->link_modes.advertising,
356 			   cmd->link_modes.supported,
357 			   __ETHTOOL_LINK_MODE_MASK_NBITS))
358 		return -EINVAL;
359 
360 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
361 						  10000baseT_Full))
362 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_T);
363 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
364 						  10000baseR_FEC))
365 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_R);
366 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
367 						  10000baseCR_Full))
368 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_CR);
369 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
370 						  10000baseKR_Full))
371 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_KR);
372 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
373 						  10000baseLR_Full))
374 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_LR);
375 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
376 						  10000baseSR_Full))
377 		advertised |= BIT(OCTEP_LINK_MODE_10GBASE_SR);
378 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
379 						  25000baseCR_Full))
380 		advertised |= BIT(OCTEP_LINK_MODE_25GBASE_CR);
381 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
382 						  25000baseKR_Full))
383 		advertised |= BIT(OCTEP_LINK_MODE_25GBASE_KR);
384 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
385 						  25000baseSR_Full))
386 		advertised |= BIT(OCTEP_LINK_MODE_25GBASE_SR);
387 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
388 						  40000baseCR4_Full))
389 		advertised |= BIT(OCTEP_LINK_MODE_40GBASE_CR4);
390 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
391 						  40000baseKR4_Full))
392 		advertised |= BIT(OCTEP_LINK_MODE_40GBASE_KR4);
393 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
394 						  40000baseLR4_Full))
395 		advertised |= BIT(OCTEP_LINK_MODE_40GBASE_LR4);
396 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
397 						  40000baseSR4_Full))
398 		advertised |= BIT(OCTEP_LINK_MODE_40GBASE_SR4);
399 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
400 						  50000baseCR2_Full))
401 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_CR2);
402 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
403 						  50000baseKR2_Full))
404 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_KR2);
405 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
406 						  50000baseSR2_Full))
407 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_SR2);
408 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
409 						  50000baseCR_Full))
410 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_CR);
411 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
412 						  50000baseKR_Full))
413 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_KR);
414 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
415 						  50000baseLR_ER_FR_Full))
416 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_LR);
417 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
418 						  50000baseSR_Full))
419 		advertised |= BIT(OCTEP_LINK_MODE_50GBASE_SR);
420 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
421 						  100000baseCR4_Full))
422 		advertised |= BIT(OCTEP_LINK_MODE_100GBASE_CR4);
423 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
424 						  100000baseKR4_Full))
425 		advertised |= BIT(OCTEP_LINK_MODE_100GBASE_KR4);
426 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
427 						  100000baseLR4_ER4_Full))
428 		advertised |= BIT(OCTEP_LINK_MODE_100GBASE_LR4);
429 	if (ethtool_link_ksettings_test_link_mode(cmd, advertising,
430 						  100000baseSR4_Full))
431 		advertised |= BIT(OCTEP_LINK_MODE_100GBASE_SR4);
432 
433 	if (advertised == link_info->advertised_modes &&
434 	    cmd->base.speed == link_info->speed &&
435 	    cmd->base.autoneg == link_info->autoneg)
436 		return 0;
437 
438 	link_info_new.advertised_modes = advertised;
439 	link_info_new.speed = cmd->base.speed;
440 	link_info_new.autoneg = autoneg;
441 
442 	err = octep_set_link_info(oct, &link_info_new);
443 	if (err)
444 		return err;
445 
446 	memcpy(link_info, &link_info_new, sizeof(struct octep_iface_link_info));
447 	return 0;
448 }
449 
450 static const struct ethtool_ops octep_ethtool_ops = {
451 	.get_drvinfo = octep_get_drvinfo,
452 	.get_link = ethtool_op_get_link,
453 	.get_strings = octep_get_strings,
454 	.get_sset_count = octep_get_sset_count,
455 	.get_ethtool_stats = octep_get_ethtool_stats,
456 	.get_link_ksettings = octep_get_link_ksettings,
457 	.set_link_ksettings = octep_set_link_ksettings,
458 };
459 
460 void octep_set_ethtool_ops(struct net_device *netdev)
461 {
462 	netdev->ethtool_ops = &octep_ethtool_ops;
463 }
464