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