xref: /openbmc/linux/drivers/s390/net/qeth_ethtool.c (revision a90bb65a)
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 			       struct kernel_ethtool_ringparam *kernel_param,
149 			       struct netlink_ext_ack *extack)
150 {
151 	struct qeth_card *card = dev->ml_priv;
152 
153 	param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
154 	param->rx_mini_max_pending = 0;
155 	param->rx_jumbo_max_pending = 0;
156 	param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
157 
158 	param->rx_pending = card->qdio.in_buf_pool.buf_count;
159 	param->rx_mini_pending = 0;
160 	param->rx_jumbo_pending = 0;
161 	param->tx_pending = QDIO_MAX_BUFFERS_PER_Q;
162 }
163 
164 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
165 {
166 	struct qeth_card *card = dev->ml_priv;
167 	char prefix[ETH_GSTRING_LEN] = "";
168 	unsigned int i;
169 
170 	switch (stringset) {
171 	case ETH_SS_STATS:
172 		qeth_add_stat_strings(&data, prefix, card_stats,
173 				      CARD_STATS_LEN);
174 		for (i = 0; i < card->qdio.no_out_queues; i++) {
175 			snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i);
176 			qeth_add_stat_strings(&data, prefix, txq_stats,
177 					      TXQ_STATS_LEN);
178 		}
179 		break;
180 	default:
181 		WARN_ON(1);
182 		break;
183 	}
184 }
185 
186 static void qeth_get_drvinfo(struct net_device *dev,
187 			     struct ethtool_drvinfo *info)
188 {
189 	struct qeth_card *card = dev->ml_priv;
190 
191 	strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3",
192 		sizeof(info->driver));
193 	strlcpy(info->fw_version, card->info.mcl_level,
194 		sizeof(info->fw_version));
195 	snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
196 		 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
197 }
198 
199 static void qeth_get_channels(struct net_device *dev,
200 			      struct ethtool_channels *channels)
201 {
202 	struct qeth_card *card = dev->ml_priv;
203 
204 	channels->max_rx = dev->num_rx_queues;
205 	channels->max_tx = card->qdio.no_out_queues;
206 	channels->max_other = 0;
207 	channels->max_combined = 0;
208 	channels->rx_count = dev->real_num_rx_queues;
209 	channels->tx_count = dev->real_num_tx_queues;
210 	channels->other_count = 0;
211 	channels->combined_count = 0;
212 }
213 
214 static int qeth_set_channels(struct net_device *dev,
215 			     struct ethtool_channels *channels)
216 {
217 	struct qeth_priv *priv = netdev_priv(dev);
218 	struct qeth_card *card = dev->ml_priv;
219 	int rc;
220 
221 	if (channels->rx_count == 0 || channels->tx_count == 0)
222 		return -EINVAL;
223 	if (channels->tx_count > card->qdio.no_out_queues)
224 		return -EINVAL;
225 
226 	/* Prio-queueing needs all TX queues: */
227 	if (qeth_uses_tx_prio_queueing(card))
228 		return -EPERM;
229 
230 	if (IS_IQD(card)) {
231 		if (channels->tx_count < QETH_IQD_MIN_TXQ)
232 			return -EINVAL;
233 
234 		/* Reject downgrade while running. It could push displaced
235 		 * ucast flows onto txq0, which is reserved for mcast.
236 		 */
237 		if (netif_running(dev) &&
238 		    channels->tx_count < dev->real_num_tx_queues)
239 			return -EPERM;
240 	}
241 
242 	rc = qeth_set_real_num_tx_queues(card, channels->tx_count);
243 	if (!rc)
244 		priv->tx_wanted_queues = channels->tx_count;
245 
246 	return rc;
247 }
248 
249 static int qeth_get_ts_info(struct net_device *dev,
250 			    struct ethtool_ts_info *info)
251 {
252 	struct qeth_card *card = dev->ml_priv;
253 
254 	if (!IS_IQD(card))
255 		return -EOPNOTSUPP;
256 
257 	return ethtool_op_get_ts_info(dev, info);
258 }
259 
260 static int qeth_get_tunable(struct net_device *dev,
261 			    const struct ethtool_tunable *tuna, void *data)
262 {
263 	struct qeth_priv *priv = netdev_priv(dev);
264 
265 	switch (tuna->id) {
266 	case ETHTOOL_RX_COPYBREAK:
267 		*(u32 *)data = priv->rx_copybreak;
268 		return 0;
269 	default:
270 		return -EOPNOTSUPP;
271 	}
272 }
273 
274 static int qeth_set_tunable(struct net_device *dev,
275 			    const struct ethtool_tunable *tuna,
276 			    const void *data)
277 {
278 	struct qeth_priv *priv = netdev_priv(dev);
279 
280 	switch (tuna->id) {
281 	case ETHTOOL_RX_COPYBREAK:
282 		WRITE_ONCE(priv->rx_copybreak, *(u32 *)data);
283 		return 0;
284 	default:
285 		return -EOPNOTSUPP;
286 	}
287 }
288 
289 static int qeth_get_per_queue_coalesce(struct net_device *dev, u32 __queue,
290 				       struct ethtool_coalesce *coal)
291 {
292 	struct qeth_card *card = dev->ml_priv;
293 	struct qeth_qdio_out_q *queue;
294 
295 	if (!IS_IQD(card))
296 		return -EOPNOTSUPP;
297 
298 	if (__queue >= card->qdio.no_out_queues)
299 		return -EINVAL;
300 
301 	queue = card->qdio.out_qs[__queue];
302 
303 	coal->tx_coalesce_usecs = queue->coalesce_usecs;
304 	coal->tx_max_coalesced_frames = queue->max_coalesced_frames;
305 	return 0;
306 }
307 
308 static int qeth_set_per_queue_coalesce(struct net_device *dev, u32 queue,
309 				       struct ethtool_coalesce *coal)
310 {
311 	struct qeth_card *card = dev->ml_priv;
312 
313 	if (!IS_IQD(card))
314 		return -EOPNOTSUPP;
315 
316 	if (queue >= card->qdio.no_out_queues)
317 		return -EINVAL;
318 
319 	if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames)
320 		return -EINVAL;
321 
322 	__qeth_set_coalesce(dev, card->qdio.out_qs[queue], coal);
323 	return 0;
324 }
325 
326 /* Helper function to fill 'advertising' and 'supported' which are the same. */
327 /* Autoneg and full-duplex are supported and advertised unconditionally.     */
328 /* Always advertise and support all speeds up to specified, and only one     */
329 /* specified port type.							     */
330 static void qeth_set_ethtool_link_modes(struct ethtool_link_ksettings *cmd,
331 					enum qeth_link_mode link_mode)
332 {
333 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
334 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
335 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
336 
337 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
338 	ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
339 
340 	switch (cmd->base.port) {
341 	case PORT_TP:
342 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
343 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
344 
345 		switch (cmd->base.speed) {
346 		case SPEED_10000:
347 			ethtool_link_ksettings_add_link_mode(cmd, supported,
348 							     10000baseT_Full);
349 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
350 							     10000baseT_Full);
351 			fallthrough;
352 		case SPEED_1000:
353 			ethtool_link_ksettings_add_link_mode(cmd, supported,
354 							     1000baseT_Full);
355 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
356 							     1000baseT_Full);
357 			ethtool_link_ksettings_add_link_mode(cmd, supported,
358 							     1000baseT_Half);
359 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
360 							     1000baseT_Half);
361 			fallthrough;
362 		case SPEED_100:
363 			ethtool_link_ksettings_add_link_mode(cmd, supported,
364 							     100baseT_Full);
365 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
366 							     100baseT_Full);
367 			ethtool_link_ksettings_add_link_mode(cmd, supported,
368 							     100baseT_Half);
369 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
370 							     100baseT_Half);
371 			fallthrough;
372 		case SPEED_10:
373 			ethtool_link_ksettings_add_link_mode(cmd, supported,
374 							     10baseT_Full);
375 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
376 							     10baseT_Full);
377 			ethtool_link_ksettings_add_link_mode(cmd, supported,
378 							     10baseT_Half);
379 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
380 							     10baseT_Half);
381 			break;
382 		default:
383 			break;
384 		}
385 
386 		break;
387 	case PORT_FIBRE:
388 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
389 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
390 
391 		switch (cmd->base.speed) {
392 		case SPEED_25000:
393 			ethtool_link_ksettings_add_link_mode(cmd, supported,
394 							     25000baseSR_Full);
395 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
396 							     25000baseSR_Full);
397 			break;
398 		case SPEED_10000:
399 			if (link_mode == QETH_LINK_MODE_FIBRE_LONG) {
400 				ethtool_link_ksettings_add_link_mode(cmd, supported,
401 								     10000baseLR_Full);
402 				ethtool_link_ksettings_add_link_mode(cmd, advertising,
403 								     10000baseLR_Full);
404 			} else if (link_mode == QETH_LINK_MODE_FIBRE_SHORT) {
405 				ethtool_link_ksettings_add_link_mode(cmd, supported,
406 								     10000baseSR_Full);
407 				ethtool_link_ksettings_add_link_mode(cmd, advertising,
408 								     10000baseSR_Full);
409 			}
410 			break;
411 		case SPEED_1000:
412 			ethtool_link_ksettings_add_link_mode(cmd, supported,
413 							     1000baseX_Full);
414 			ethtool_link_ksettings_add_link_mode(cmd, advertising,
415 							     1000baseX_Full);
416 			break;
417 		default:
418 			break;
419 		}
420 
421 		break;
422 	default:
423 		break;
424 	}
425 }
426 
427 static int qeth_get_link_ksettings(struct net_device *netdev,
428 				   struct ethtool_link_ksettings *cmd)
429 {
430 	struct qeth_card *card = netdev->ml_priv;
431 	struct qeth_link_info link_info;
432 
433 	cmd->base.speed = card->info.link_info.speed;
434 	cmd->base.duplex = card->info.link_info.duplex;
435 	cmd->base.port = card->info.link_info.port;
436 	cmd->base.autoneg = AUTONEG_ENABLE;
437 	cmd->base.phy_address = 0;
438 	cmd->base.mdio_support = 0;
439 	cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
440 	cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
441 
442 	/* Check if we can obtain more accurate information.	 */
443 	if (!qeth_query_card_info(card, &link_info)) {
444 		if (link_info.speed != SPEED_UNKNOWN)
445 			cmd->base.speed = link_info.speed;
446 		if (link_info.duplex != DUPLEX_UNKNOWN)
447 			cmd->base.duplex = link_info.duplex;
448 		if (link_info.port != PORT_OTHER)
449 			cmd->base.port = link_info.port;
450 	}
451 
452 	qeth_set_ethtool_link_modes(cmd, card->info.link_info.link_mode);
453 
454 	return 0;
455 }
456 
457 const struct ethtool_ops qeth_ethtool_ops = {
458 	.supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS |
459 				     ETHTOOL_COALESCE_TX_MAX_FRAMES,
460 	.get_link = ethtool_op_get_link,
461 	.set_coalesce = qeth_set_coalesce,
462 	.get_ringparam = qeth_get_ringparam,
463 	.get_strings = qeth_get_strings,
464 	.get_ethtool_stats = qeth_get_ethtool_stats,
465 	.get_sset_count = qeth_get_sset_count,
466 	.get_drvinfo = qeth_get_drvinfo,
467 	.get_channels = qeth_get_channels,
468 	.set_channels = qeth_set_channels,
469 	.get_ts_info = qeth_get_ts_info,
470 	.get_tunable = qeth_get_tunable,
471 	.set_tunable = qeth_set_tunable,
472 	.get_per_queue_coalesce = qeth_get_per_queue_coalesce,
473 	.set_per_queue_coalesce = qeth_set_per_queue_coalesce,
474 	.get_link_ksettings = qeth_get_link_ksettings,
475 };
476