xref: /openbmc/linux/drivers/s390/net/qeth_ethtool.c (revision a3b91d8b)
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_card *card = dev->ml_priv;
215 
216 	if (channels->rx_count == 0 || channels->tx_count == 0)
217 		return -EINVAL;
218 	if (channels->tx_count > card->qdio.no_out_queues)
219 		return -EINVAL;
220 
221 	if (IS_IQD(card)) {
222 		if (channels->tx_count < QETH_IQD_MIN_TXQ)
223 			return -EINVAL;
224 
225 		/* Reject downgrade while running. It could push displaced
226 		 * ucast flows onto txq0, which is reserved for mcast.
227 		 */
228 		if (netif_running(dev) &&
229 		    channels->tx_count < dev->real_num_tx_queues)
230 			return -EPERM;
231 	} else {
232 		/* OSA still uses the legacy prio-queue mechanism: */
233 		if (!IS_VM_NIC(card))
234 			return -EOPNOTSUPP;
235 	}
236 
237 	return qeth_set_real_num_tx_queues(card, channels->tx_count);
238 }
239 
240 static int qeth_get_ts_info(struct net_device *dev,
241 			    struct ethtool_ts_info *info)
242 {
243 	struct qeth_card *card = dev->ml_priv;
244 
245 	if (!IS_IQD(card))
246 		return -EOPNOTSUPP;
247 
248 	return ethtool_op_get_ts_info(dev, info);
249 }
250 
251 static int qeth_get_tunable(struct net_device *dev,
252 			    const struct ethtool_tunable *tuna, void *data)
253 {
254 	struct qeth_priv *priv = netdev_priv(dev);
255 
256 	switch (tuna->id) {
257 	case ETHTOOL_RX_COPYBREAK:
258 		*(u32 *)data = priv->rx_copybreak;
259 		return 0;
260 	default:
261 		return -EOPNOTSUPP;
262 	}
263 }
264 
265 static int qeth_set_tunable(struct net_device *dev,
266 			    const struct ethtool_tunable *tuna,
267 			    const void *data)
268 {
269 	struct qeth_priv *priv = netdev_priv(dev);
270 
271 	switch (tuna->id) {
272 	case ETHTOOL_RX_COPYBREAK:
273 		WRITE_ONCE(priv->rx_copybreak, *(u32 *)data);
274 		return 0;
275 	default:
276 		return -EOPNOTSUPP;
277 	}
278 }
279 
280 static int qeth_get_per_queue_coalesce(struct net_device *dev, u32 __queue,
281 				       struct ethtool_coalesce *coal)
282 {
283 	struct qeth_card *card = dev->ml_priv;
284 	struct qeth_qdio_out_q *queue;
285 
286 	if (!IS_IQD(card))
287 		return -EOPNOTSUPP;
288 
289 	if (__queue >= card->qdio.no_out_queues)
290 		return -EINVAL;
291 
292 	queue = card->qdio.out_qs[__queue];
293 
294 	coal->tx_coalesce_usecs = queue->coalesce_usecs;
295 	coal->tx_max_coalesced_frames = queue->max_coalesced_frames;
296 	return 0;
297 }
298 
299 static int qeth_set_per_queue_coalesce(struct net_device *dev, u32 queue,
300 				       struct ethtool_coalesce *coal)
301 {
302 	struct qeth_card *card = dev->ml_priv;
303 
304 	if (!IS_IQD(card))
305 		return -EOPNOTSUPP;
306 
307 	if (queue >= card->qdio.no_out_queues)
308 		return -EINVAL;
309 
310 	if (!coal->tx_coalesce_usecs && !coal->tx_max_coalesced_frames)
311 		return -EINVAL;
312 
313 	__qeth_set_coalesce(dev, card->qdio.out_qs[queue], coal);
314 	return 0;
315 }
316 
317 /* Helper function to fill 'advertising' and 'supported' which are the same. */
318 /* Autoneg and full-duplex are supported and advertised unconditionally.     */
319 /* Always advertise and support all speeds up to specified, and only one     */
320 /* specified port type.							     */
321 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd,
322 				int maxspeed, int porttype)
323 {
324 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
325 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
326 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
327 
328 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
329 	ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
330 
331 	switch (porttype) {
332 	case PORT_TP:
333 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
334 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
335 		break;
336 	case PORT_FIBRE:
337 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
338 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
339 		break;
340 	default:
341 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
342 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
343 		WARN_ON_ONCE(1);
344 	}
345 
346 	/* partially does fall through, to also select lower speeds */
347 	switch (maxspeed) {
348 	case SPEED_25000:
349 		ethtool_link_ksettings_add_link_mode(cmd, supported,
350 						     25000baseSR_Full);
351 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
352 						     25000baseSR_Full);
353 		break;
354 	case SPEED_10000:
355 		ethtool_link_ksettings_add_link_mode(cmd, supported,
356 						     10000baseT_Full);
357 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
358 						     10000baseT_Full);
359 		/* fall through */
360 	case SPEED_1000:
361 		ethtool_link_ksettings_add_link_mode(cmd, supported,
362 						     1000baseT_Full);
363 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
364 						     1000baseT_Full);
365 		ethtool_link_ksettings_add_link_mode(cmd, supported,
366 						     1000baseT_Half);
367 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
368 						     1000baseT_Half);
369 		/* fall through */
370 	case SPEED_100:
371 		ethtool_link_ksettings_add_link_mode(cmd, supported,
372 						     100baseT_Full);
373 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
374 						     100baseT_Full);
375 		ethtool_link_ksettings_add_link_mode(cmd, supported,
376 						     100baseT_Half);
377 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
378 						     100baseT_Half);
379 		/* fall through */
380 	case SPEED_10:
381 		ethtool_link_ksettings_add_link_mode(cmd, supported,
382 						     10baseT_Full);
383 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
384 						     10baseT_Full);
385 		ethtool_link_ksettings_add_link_mode(cmd, supported,
386 						     10baseT_Half);
387 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
388 						     10baseT_Half);
389 		break;
390 	default:
391 		ethtool_link_ksettings_add_link_mode(cmd, supported,
392 						     10baseT_Full);
393 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
394 						     10baseT_Full);
395 		ethtool_link_ksettings_add_link_mode(cmd, supported,
396 						     10baseT_Half);
397 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
398 						     10baseT_Half);
399 		WARN_ON_ONCE(1);
400 	}
401 }
402 
403 
404 static int qeth_get_link_ksettings(struct net_device *netdev,
405 				   struct ethtool_link_ksettings *cmd)
406 {
407 	struct qeth_card *card = netdev->ml_priv;
408 	enum qeth_link_types link_type;
409 	struct carrier_info carrier_info;
410 	int rc;
411 
412 	if (IS_IQD(card) || IS_VM_NIC(card))
413 		link_type = QETH_LINK_TYPE_10GBIT_ETH;
414 	else
415 		link_type = card->info.link_type;
416 
417 	cmd->base.duplex = DUPLEX_FULL;
418 	cmd->base.autoneg = AUTONEG_ENABLE;
419 	cmd->base.phy_address = 0;
420 	cmd->base.mdio_support = 0;
421 	cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
422 	cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
423 
424 	switch (link_type) {
425 	case QETH_LINK_TYPE_FAST_ETH:
426 	case QETH_LINK_TYPE_LANE_ETH100:
427 		cmd->base.speed = SPEED_100;
428 		cmd->base.port = PORT_TP;
429 		break;
430 	case QETH_LINK_TYPE_GBIT_ETH:
431 	case QETH_LINK_TYPE_LANE_ETH1000:
432 		cmd->base.speed = SPEED_1000;
433 		cmd->base.port = PORT_FIBRE;
434 		break;
435 	case QETH_LINK_TYPE_10GBIT_ETH:
436 		cmd->base.speed = SPEED_10000;
437 		cmd->base.port = PORT_FIBRE;
438 		break;
439 	case QETH_LINK_TYPE_25GBIT_ETH:
440 		cmd->base.speed = SPEED_25000;
441 		cmd->base.port = PORT_FIBRE;
442 		break;
443 	default:
444 		cmd->base.speed = SPEED_10;
445 		cmd->base.port = PORT_TP;
446 	}
447 	qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port);
448 
449 	/* Check if we can obtain more accurate information.	 */
450 	/* If QUERY_CARD_INFO command is not supported or fails, */
451 	/* just return the heuristics that was filled above.	 */
452 	rc = qeth_query_card_info(card, &carrier_info);
453 	if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
454 		return 0;
455 	if (rc) /* report error from the hardware operation */
456 		return rc;
457 	/* on success, fill in the information got from the hardware */
458 
459 	netdev_dbg(netdev,
460 	"card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
461 			carrier_info.card_type,
462 			carrier_info.port_mode,
463 			carrier_info.port_speed);
464 
465 	/* Update attributes for which we've obtained more authoritative */
466 	/* information, leave the rest the way they where filled above.  */
467 	switch (carrier_info.card_type) {
468 	case CARD_INFO_TYPE_1G_COPPER_A:
469 	case CARD_INFO_TYPE_1G_COPPER_B:
470 		cmd->base.port = PORT_TP;
471 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
472 		break;
473 	case CARD_INFO_TYPE_1G_FIBRE_A:
474 	case CARD_INFO_TYPE_1G_FIBRE_B:
475 		cmd->base.port = PORT_FIBRE;
476 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
477 		break;
478 	case CARD_INFO_TYPE_10G_FIBRE_A:
479 	case CARD_INFO_TYPE_10G_FIBRE_B:
480 		cmd->base.port = PORT_FIBRE;
481 		qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port);
482 		break;
483 	}
484 
485 	switch (carrier_info.port_mode) {
486 	case CARD_INFO_PORTM_FULLDUPLEX:
487 		cmd->base.duplex = DUPLEX_FULL;
488 		break;
489 	case CARD_INFO_PORTM_HALFDUPLEX:
490 		cmd->base.duplex = DUPLEX_HALF;
491 		break;
492 	}
493 
494 	switch (carrier_info.port_speed) {
495 	case CARD_INFO_PORTS_10M:
496 		cmd->base.speed = SPEED_10;
497 		break;
498 	case CARD_INFO_PORTS_100M:
499 		cmd->base.speed = SPEED_100;
500 		break;
501 	case CARD_INFO_PORTS_1G:
502 		cmd->base.speed = SPEED_1000;
503 		break;
504 	case CARD_INFO_PORTS_10G:
505 		cmd->base.speed = SPEED_10000;
506 		break;
507 	case CARD_INFO_PORTS_25G:
508 		cmd->base.speed = SPEED_25000;
509 		break;
510 	}
511 
512 	return 0;
513 }
514 
515 const struct ethtool_ops qeth_ethtool_ops = {
516 	.supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS |
517 				     ETHTOOL_COALESCE_TX_MAX_FRAMES,
518 	.get_link = ethtool_op_get_link,
519 	.set_coalesce = qeth_set_coalesce,
520 	.get_ringparam = qeth_get_ringparam,
521 	.get_strings = qeth_get_strings,
522 	.get_ethtool_stats = qeth_get_ethtool_stats,
523 	.get_sset_count = qeth_get_sset_count,
524 	.get_drvinfo = qeth_get_drvinfo,
525 	.get_channels = qeth_get_channels,
526 	.set_channels = qeth_set_channels,
527 	.get_ts_info = qeth_get_ts_info,
528 	.get_tunable = qeth_get_tunable,
529 	.set_tunable = qeth_set_tunable,
530 	.get_per_queue_coalesce = qeth_get_per_queue_coalesce,
531 	.set_per_queue_coalesce = qeth_set_per_queue_coalesce,
532 	.get_link_ksettings = qeth_get_link_ksettings,
533 };
534 
535 const struct ethtool_ops qeth_osn_ethtool_ops = {
536 	.get_strings = qeth_get_strings,
537 	.get_ethtool_stats = qeth_get_ethtool_stats,
538 	.get_sset_count = qeth_get_sset_count,
539 	.get_drvinfo = qeth_get_drvinfo,
540 };
541