xref: /openbmc/linux/drivers/s390/net/qeth_ethtool.c (revision cd6d421e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2018
4  */
5 
6 #define KMSG_COMPONENT "qeth"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
8 
9 #include <linux/ethtool.h>
10 #include "qeth_core.h"
11 
12 
13 #define QETH_TXQ_STAT(_name, _stat) { \
14 	.name = _name, \
15 	.offset = offsetof(struct qeth_out_q_stats, _stat) \
16 }
17 
18 #define QETH_CARD_STAT(_name, _stat) { \
19 	.name = _name, \
20 	.offset = offsetof(struct qeth_card_stats, _stat) \
21 }
22 
23 struct qeth_stats {
24 	char name[ETH_GSTRING_LEN];
25 	unsigned int offset;
26 };
27 
28 static const struct qeth_stats txq_stats[] = {
29 	QETH_TXQ_STAT("IO buffers", bufs),
30 	QETH_TXQ_STAT("IO buffer elements", buf_elements),
31 	QETH_TXQ_STAT("packed IO buffers", bufs_pack),
32 	QETH_TXQ_STAT("skbs", tx_packets),
33 	QETH_TXQ_STAT("packed skbs", skbs_pack),
34 	QETH_TXQ_STAT("SG skbs", skbs_sg),
35 	QETH_TXQ_STAT("HW csum skbs", skbs_csum),
36 	QETH_TXQ_STAT("TSO skbs", skbs_tso),
37 	QETH_TXQ_STAT("linearized skbs", skbs_linearized),
38 	QETH_TXQ_STAT("linearized+error skbs", skbs_linearized_fail),
39 	QETH_TXQ_STAT("TSO bytes", tso_bytes),
40 	QETH_TXQ_STAT("Packing mode switches", packing_mode_switch),
41 	QETH_TXQ_STAT("Queue stopped", stopped),
42 	QETH_TXQ_STAT("Doorbell", doorbell),
43 	QETH_TXQ_STAT("IRQ for frames", coal_frames),
44 	QETH_TXQ_STAT("Completion yield", completion_yield),
45 	QETH_TXQ_STAT("Completion timer", completion_timer),
46 };
47 
48 static const struct qeth_stats card_stats[] = {
49 	QETH_CARD_STAT("rx0 IO buffers", rx_bufs),
50 	QETH_CARD_STAT("rx0 HW csum skbs", rx_skb_csum),
51 	QETH_CARD_STAT("rx0 SG skbs", rx_sg_skbs),
52 	QETH_CARD_STAT("rx0 SG page frags", rx_sg_frags),
53 	QETH_CARD_STAT("rx0 SG page allocs", rx_sg_alloc_page),
54 	QETH_CARD_STAT("rx0 dropped, no memory", rx_dropped_nomem),
55 	QETH_CARD_STAT("rx0 dropped, bad format", rx_dropped_notsupp),
56 	QETH_CARD_STAT("rx0 dropped, runt", rx_dropped_runt),
57 };
58 
59 #define TXQ_STATS_LEN	ARRAY_SIZE(txq_stats)
60 #define CARD_STATS_LEN	ARRAY_SIZE(card_stats)
61 
62 static void qeth_add_stat_data(u64 **dst, void *src,
63 			       const struct qeth_stats stats[],
64 			       unsigned int size)
65 {
66 	unsigned int i;
67 	char *stat;
68 
69 	for (i = 0; i < size; i++) {
70 		stat = (char *)src + stats[i].offset;
71 		**dst = *(u64 *)stat;
72 		(*dst)++;
73 	}
74 }
75 
76 static void qeth_add_stat_strings(u8 **data, const char *prefix,
77 				  const struct qeth_stats stats[],
78 				  unsigned int size)
79 {
80 	unsigned int i;
81 
82 	for (i = 0; i < size; i++) {
83 		snprintf(*data, ETH_GSTRING_LEN, "%s%s", prefix, stats[i].name);
84 		*data += ETH_GSTRING_LEN;
85 	}
86 }
87 
88 static int qeth_get_sset_count(struct net_device *dev, int stringset)
89 {
90 	struct qeth_card *card = dev->ml_priv;
91 
92 	switch (stringset) {
93 	case ETH_SS_STATS:
94 		return CARD_STATS_LEN +
95 		       card->qdio.no_out_queues * TXQ_STATS_LEN;
96 	default:
97 		return -EINVAL;
98 	}
99 }
100 
101 static void qeth_get_ethtool_stats(struct net_device *dev,
102 				   struct ethtool_stats *stats, u64 *data)
103 {
104 	struct qeth_card *card = dev->ml_priv;
105 	unsigned int i;
106 
107 	qeth_add_stat_data(&data, &card->stats, card_stats, CARD_STATS_LEN);
108 	for (i = 0; i < card->qdio.no_out_queues; i++)
109 		qeth_add_stat_data(&data, &card->qdio.out_qs[i]->stats,
110 				   txq_stats, TXQ_STATS_LEN);
111 }
112 
113 static void __qeth_set_coalesce(struct net_device *dev,
114 				struct qeth_qdio_out_q *queue,
115 				struct ethtool_coalesce *coal)
116 {
117 	WRITE_ONCE(queue->coalesce_usecs, coal->tx_coalesce_usecs);
118 	WRITE_ONCE(queue->max_coalesced_frames, coal->tx_max_coalesced_frames);
119 
120 	if (coal->tx_coalesce_usecs &&
121 	    netif_running(dev) &&
122 	    !qeth_out_queue_is_empty(queue))
123 		qeth_tx_arm_timer(queue, coal->tx_coalesce_usecs);
124 }
125 
126 static int qeth_set_coalesce(struct net_device *dev,
127 			     struct ethtool_coalesce *coal)
128 {
129 	struct qeth_card *card = dev->ml_priv;
130 	struct qeth_qdio_out_q *queue;
131 	unsigned int i;
132 
133 	if (!IS_IQD(card))
134 		return -EOPNOTSUPP;
135 
136 	if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames)
137 		return -EINVAL;
138 
139 	qeth_for_each_output_queue(card, queue, i)
140 		__qeth_set_coalesce(dev, queue, coal);
141 
142 	return 0;
143 }
144 
145 static void qeth_get_ringparam(struct net_device *dev,
146 			       struct ethtool_ringparam *param)
147 {
148 	struct qeth_card *card = dev->ml_priv;
149 
150 	param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
151 	param->rx_mini_max_pending = 0;
152 	param->rx_jumbo_max_pending = 0;
153 	param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
154 
155 	param->rx_pending = card->qdio.in_buf_pool.buf_count;
156 	param->rx_mini_pending = 0;
157 	param->rx_jumbo_pending = 0;
158 	param->tx_pending = QDIO_MAX_BUFFERS_PER_Q;
159 }
160 
161 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
162 {
163 	struct qeth_card *card = dev->ml_priv;
164 	char prefix[ETH_GSTRING_LEN] = "";
165 	unsigned int i;
166 
167 	switch (stringset) {
168 	case ETH_SS_STATS:
169 		qeth_add_stat_strings(&data, prefix, card_stats,
170 				      CARD_STATS_LEN);
171 		for (i = 0; i < card->qdio.no_out_queues; i++) {
172 			snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i);
173 			qeth_add_stat_strings(&data, prefix, txq_stats,
174 					      TXQ_STATS_LEN);
175 		}
176 		break;
177 	default:
178 		WARN_ON(1);
179 		break;
180 	}
181 }
182 
183 static void qeth_get_drvinfo(struct net_device *dev,
184 			     struct ethtool_drvinfo *info)
185 {
186 	struct qeth_card *card = dev->ml_priv;
187 
188 	strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3",
189 		sizeof(info->driver));
190 	strlcpy(info->fw_version, card->info.mcl_level,
191 		sizeof(info->fw_version));
192 	snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
193 		 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
194 }
195 
196 static void qeth_get_channels(struct net_device *dev,
197 			      struct ethtool_channels *channels)
198 {
199 	struct qeth_card *card = dev->ml_priv;
200 
201 	channels->max_rx = dev->num_rx_queues;
202 	channels->max_tx = card->qdio.no_out_queues;
203 	channels->max_other = 0;
204 	channels->max_combined = 0;
205 	channels->rx_count = dev->real_num_rx_queues;
206 	channels->tx_count = dev->real_num_tx_queues;
207 	channels->other_count = 0;
208 	channels->combined_count = 0;
209 }
210 
211 static int qeth_set_channels(struct net_device *dev,
212 			     struct ethtool_channels *channels)
213 {
214 	struct qeth_priv *priv = netdev_priv(dev);
215 	struct qeth_card *card = dev->ml_priv;
216 	int rc;
217 
218 	if (channels->rx_count == 0 || channels->tx_count == 0)
219 		return -EINVAL;
220 	if (channels->tx_count > card->qdio.no_out_queues)
221 		return -EINVAL;
222 
223 	/* Prio-queueing needs all TX queues: */
224 	if (qeth_uses_tx_prio_queueing(card))
225 		return -EPERM;
226 
227 	if (IS_IQD(card)) {
228 		if (channels->tx_count < QETH_IQD_MIN_TXQ)
229 			return -EINVAL;
230 
231 		/* Reject downgrade while running. It could push displaced
232 		 * ucast flows onto txq0, which is reserved for mcast.
233 		 */
234 		if (netif_running(dev) &&
235 		    channels->tx_count < dev->real_num_tx_queues)
236 			return -EPERM;
237 	}
238 
239 	rc = qeth_set_real_num_tx_queues(card, channels->tx_count);
240 	if (!rc)
241 		priv->tx_wanted_queues = channels->tx_count;
242 
243 	return rc;
244 }
245 
246 static int qeth_get_ts_info(struct net_device *dev,
247 			    struct ethtool_ts_info *info)
248 {
249 	struct qeth_card *card = dev->ml_priv;
250 
251 	if (!IS_IQD(card))
252 		return -EOPNOTSUPP;
253 
254 	return ethtool_op_get_ts_info(dev, info);
255 }
256 
257 static int qeth_get_tunable(struct net_device *dev,
258 			    const struct ethtool_tunable *tuna, void *data)
259 {
260 	struct qeth_priv *priv = netdev_priv(dev);
261 
262 	switch (tuna->id) {
263 	case ETHTOOL_RX_COPYBREAK:
264 		*(u32 *)data = priv->rx_copybreak;
265 		return 0;
266 	default:
267 		return -EOPNOTSUPP;
268 	}
269 }
270 
271 static int qeth_set_tunable(struct net_device *dev,
272 			    const struct ethtool_tunable *tuna,
273 			    const void *data)
274 {
275 	struct qeth_priv *priv = netdev_priv(dev);
276 
277 	switch (tuna->id) {
278 	case ETHTOOL_RX_COPYBREAK:
279 		WRITE_ONCE(priv->rx_copybreak, *(u32 *)data);
280 		return 0;
281 	default:
282 		return -EOPNOTSUPP;
283 	}
284 }
285 
286 static int qeth_get_per_queue_coalesce(struct net_device *dev, u32 __queue,
287 				       struct ethtool_coalesce *coal)
288 {
289 	struct qeth_card *card = dev->ml_priv;
290 	struct qeth_qdio_out_q *queue;
291 
292 	if (!IS_IQD(card))
293 		return -EOPNOTSUPP;
294 
295 	if (__queue >= card->qdio.no_out_queues)
296 		return -EINVAL;
297 
298 	queue = card->qdio.out_qs[__queue];
299 
300 	coal->tx_coalesce_usecs = queue->coalesce_usecs;
301 	coal->tx_max_coalesced_frames = queue->max_coalesced_frames;
302 	return 0;
303 }
304 
305 static int qeth_set_per_queue_coalesce(struct net_device *dev, u32 queue,
306 				       struct ethtool_coalesce *coal)
307 {
308 	struct qeth_card *card = dev->ml_priv;
309 
310 	if (!IS_IQD(card))
311 		return -EOPNOTSUPP;
312 
313 	if (queue >= card->qdio.no_out_queues)
314 		return -EINVAL;
315 
316 	if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames)
317 		return -EINVAL;
318 
319 	__qeth_set_coalesce(dev, card->qdio.out_qs[queue], coal);
320 	return 0;
321 }
322 
323 /* Helper function to fill 'advertising' and 'supported' which are the same. */
324 /* Autoneg and full-duplex are supported and advertised unconditionally.     */
325 /* Always advertise and support all speeds up to specified, and only one     */
326 /* specified port type.							     */
327 static void qeth_set_ethtool_link_modes(struct ethtool_link_ksettings *cmd,
328 					enum qeth_link_mode link_mode)
329 {
330 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
331 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
332 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
333 
334 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
335 	ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
336 
337 	switch (cmd->base.port) {
338 	case PORT_TP:
339 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
340 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
341 
342 		switch (cmd->base.speed) {
343 		case SPEED_10000:
344 			ethtool_link_ksettings_add_link_mode(cmd, supported,
345 							     10000baseT_Full);
346 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
347 							     10000baseT_Full);
348 			fallthrough;
349 		case SPEED_1000:
350 			ethtool_link_ksettings_add_link_mode(cmd, supported,
351 							     1000baseT_Full);
352 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
353 							     1000baseT_Full);
354 			ethtool_link_ksettings_add_link_mode(cmd, supported,
355 							     1000baseT_Half);
356 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
357 							     1000baseT_Half);
358 			fallthrough;
359 		case SPEED_100:
360 			ethtool_link_ksettings_add_link_mode(cmd, supported,
361 							     100baseT_Full);
362 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
363 							     100baseT_Full);
364 			ethtool_link_ksettings_add_link_mode(cmd, supported,
365 							     100baseT_Half);
366 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
367 							     100baseT_Half);
368 			fallthrough;
369 		case SPEED_10:
370 			ethtool_link_ksettings_add_link_mode(cmd, supported,
371 							     10baseT_Full);
372 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
373 							     10baseT_Full);
374 			ethtool_link_ksettings_add_link_mode(cmd, supported,
375 							     10baseT_Half);
376 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
377 							     10baseT_Half);
378 			break;
379 		default:
380 			break;
381 		}
382 
383 		break;
384 	case PORT_FIBRE:
385 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
386 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
387 
388 		switch (cmd->base.speed) {
389 		case SPEED_25000:
390 			ethtool_link_ksettings_add_link_mode(cmd, supported,
391 							     25000baseSR_Full);
392 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
393 							     25000baseSR_Full);
394 			break;
395 		case SPEED_10000:
396 			if (link_mode == QETH_LINK_MODE_FIBRE_LONG) {
397 				ethtool_link_ksettings_add_link_mode(cmd, supported,
398 								     10000baseLR_Full);
399 				ethtool_link_ksettings_add_link_mode(cmd, advertising,
400 								     10000baseLR_Full);
401 			} else if (link_mode == QETH_LINK_MODE_FIBRE_SHORT) {
402 				ethtool_link_ksettings_add_link_mode(cmd, supported,
403 								     10000baseSR_Full);
404 				ethtool_link_ksettings_add_link_mode(cmd, advertising,
405 								     10000baseSR_Full);
406 			}
407 			break;
408 		case SPEED_1000:
409 			ethtool_link_ksettings_add_link_mode(cmd, supported,
410 							     1000baseX_Full);
411 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
412 							     1000baseX_Full);
413 			break;
414 		default:
415 			break;
416 		}
417 
418 		break;
419 	default:
420 		break;
421 	}
422 }
423 
424 static int qeth_get_link_ksettings(struct net_device *netdev,
425 				   struct ethtool_link_ksettings *cmd)
426 {
427 	struct qeth_card *card = netdev->ml_priv;
428 	struct qeth_link_info link_info;
429 
430 	cmd->base.speed = card->info.link_info.speed;
431 	cmd->base.duplex = card->info.link_info.duplex;
432 	cmd->base.port = card->info.link_info.port;
433 	cmd->base.autoneg = AUTONEG_ENABLE;
434 	cmd->base.phy_address = 0;
435 	cmd->base.mdio_support = 0;
436 	cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
437 	cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
438 
439 	/* Check if we can obtain more accurate information.	 */
440 	if (!qeth_query_card_info(card, &link_info)) {
441 		if (link_info.speed != SPEED_UNKNOWN)
442 			cmd->base.speed = link_info.speed;
443 		if (link_info.duplex != DUPLEX_UNKNOWN)
444 			cmd->base.duplex = link_info.duplex;
445 		if (link_info.port != PORT_OTHER)
446 			cmd->base.port = link_info.port;
447 	}
448 
449 	qeth_set_ethtool_link_modes(cmd, card->info.link_info.link_mode);
450 
451 	return 0;
452 }
453 
454 const struct ethtool_ops qeth_ethtool_ops = {
455 	.supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS |
456 				     ETHTOOL_COALESCE_TX_MAX_FRAMES,
457 	.get_link = ethtool_op_get_link,
458 	.set_coalesce = qeth_set_coalesce,
459 	.get_ringparam = qeth_get_ringparam,
460 	.get_strings = qeth_get_strings,
461 	.get_ethtool_stats = qeth_get_ethtool_stats,
462 	.get_sset_count = qeth_get_sset_count,
463 	.get_drvinfo = qeth_get_drvinfo,
464 	.get_channels = qeth_get_channels,
465 	.set_channels = qeth_set_channels,
466 	.get_ts_info = qeth_get_ts_info,
467 	.get_tunable = qeth_get_tunable,
468 	.set_tunable = qeth_set_tunable,
469 	.get_per_queue_coalesce = qeth_get_per_queue_coalesce,
470 	.set_per_queue_coalesce = qeth_set_per_queue_coalesce,
471 	.get_link_ksettings = qeth_get_link_ksettings,
472 };
473 
474 const struct ethtool_ops qeth_osn_ethtool_ops = {
475 	.get_strings = qeth_get_strings,
476 	.get_ethtool_stats = qeth_get_ethtool_stats,
477 	.get_sset_count = qeth_get_sset_count,
478 	.get_drvinfo = qeth_get_drvinfo,
479 };
480