xref: /openbmc/linux/drivers/s390/net/qeth_ethtool.c (revision 7b73a9c8)
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("Completion yield", completion_yield),
43 	QETH_TXQ_STAT("Completion timer", completion_timer),
44 };
45 
46 static const struct qeth_stats card_stats[] = {
47 	QETH_CARD_STAT("rx0 IO buffers", rx_bufs),
48 	QETH_CARD_STAT("rx0 HW csum skbs", rx_skb_csum),
49 	QETH_CARD_STAT("rx0 SG skbs", rx_sg_skbs),
50 	QETH_CARD_STAT("rx0 SG page frags", rx_sg_frags),
51 	QETH_CARD_STAT("rx0 SG page allocs", rx_sg_alloc_page),
52 	QETH_CARD_STAT("rx0 dropped, no memory", rx_dropped_nomem),
53 	QETH_CARD_STAT("rx0 dropped, bad format", rx_dropped_notsupp),
54 	QETH_CARD_STAT("rx0 dropped, runt", rx_dropped_runt),
55 };
56 
57 #define TXQ_STATS_LEN	ARRAY_SIZE(txq_stats)
58 #define CARD_STATS_LEN	ARRAY_SIZE(card_stats)
59 
60 static void qeth_add_stat_data(u64 **dst, void *src,
61 			       const struct qeth_stats stats[],
62 			       unsigned int size)
63 {
64 	unsigned int i;
65 	char *stat;
66 
67 	for (i = 0; i < size; i++) {
68 		stat = (char *)src + stats[i].offset;
69 		**dst = *(u64 *)stat;
70 		(*dst)++;
71 	}
72 }
73 
74 static void qeth_add_stat_strings(u8 **data, const char *prefix,
75 				  const struct qeth_stats stats[],
76 				  unsigned int size)
77 {
78 	unsigned int i;
79 
80 	for (i = 0; i < size; i++) {
81 		snprintf(*data, ETH_GSTRING_LEN, "%s%s", prefix, stats[i].name);
82 		*data += ETH_GSTRING_LEN;
83 	}
84 }
85 
86 static int qeth_get_sset_count(struct net_device *dev, int stringset)
87 {
88 	struct qeth_card *card = dev->ml_priv;
89 
90 	switch (stringset) {
91 	case ETH_SS_STATS:
92 		return CARD_STATS_LEN +
93 		       card->qdio.no_out_queues * TXQ_STATS_LEN;
94 	default:
95 		return -EINVAL;
96 	}
97 }
98 
99 static void qeth_get_ethtool_stats(struct net_device *dev,
100 				   struct ethtool_stats *stats, u64 *data)
101 {
102 	struct qeth_card *card = dev->ml_priv;
103 	unsigned int i;
104 
105 	qeth_add_stat_data(&data, &card->stats, card_stats, CARD_STATS_LEN);
106 	for (i = 0; i < card->qdio.no_out_queues; i++)
107 		qeth_add_stat_data(&data, &card->qdio.out_qs[i]->stats,
108 				   txq_stats, TXQ_STATS_LEN);
109 }
110 
111 static void qeth_get_ringparam(struct net_device *dev,
112 			       struct ethtool_ringparam *param)
113 {
114 	struct qeth_card *card = dev->ml_priv;
115 
116 	param->rx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
117 	param->rx_mini_max_pending = 0;
118 	param->rx_jumbo_max_pending = 0;
119 	param->tx_max_pending = QDIO_MAX_BUFFERS_PER_Q;
120 
121 	param->rx_pending = card->qdio.in_buf_pool.buf_count;
122 	param->rx_mini_pending = 0;
123 	param->rx_jumbo_pending = 0;
124 	param->tx_pending = QDIO_MAX_BUFFERS_PER_Q;
125 }
126 
127 static void qeth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
128 {
129 	struct qeth_card *card = dev->ml_priv;
130 	char prefix[ETH_GSTRING_LEN] = "";
131 	unsigned int i;
132 
133 	switch (stringset) {
134 	case ETH_SS_STATS:
135 		qeth_add_stat_strings(&data, prefix, card_stats,
136 				      CARD_STATS_LEN);
137 		for (i = 0; i < card->qdio.no_out_queues; i++) {
138 			snprintf(prefix, ETH_GSTRING_LEN, "tx%u ", i);
139 			qeth_add_stat_strings(&data, prefix, txq_stats,
140 					      TXQ_STATS_LEN);
141 		}
142 		break;
143 	default:
144 		WARN_ON(1);
145 		break;
146 	}
147 }
148 
149 static void qeth_get_drvinfo(struct net_device *dev,
150 			     struct ethtool_drvinfo *info)
151 {
152 	struct qeth_card *card = dev->ml_priv;
153 
154 	strlcpy(info->driver, IS_LAYER2(card) ? "qeth_l2" : "qeth_l3",
155 		sizeof(info->driver));
156 	strlcpy(info->version, "1.0", sizeof(info->version));
157 	strlcpy(info->fw_version, card->info.mcl_level,
158 		sizeof(info->fw_version));
159 	snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
160 		 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
161 }
162 
163 static void qeth_get_channels(struct net_device *dev,
164 			      struct ethtool_channels *channels)
165 {
166 	struct qeth_card *card = dev->ml_priv;
167 
168 	channels->max_rx = dev->num_rx_queues;
169 	channels->max_tx = card->qdio.no_out_queues;
170 	channels->max_other = 0;
171 	channels->max_combined = 0;
172 	channels->rx_count = dev->real_num_rx_queues;
173 	channels->tx_count = dev->real_num_tx_queues;
174 	channels->other_count = 0;
175 	channels->combined_count = 0;
176 }
177 
178 /* Helper function to fill 'advertising' and 'supported' which are the same. */
179 /* Autoneg and full-duplex are supported and advertised unconditionally.     */
180 /* Always advertise and support all speeds up to specified, and only one     */
181 /* specified port type.							     */
182 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd,
183 				int maxspeed, int porttype)
184 {
185 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
186 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
187 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
188 
189 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
190 	ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
191 
192 	switch (porttype) {
193 	case PORT_TP:
194 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
195 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
196 		break;
197 	case PORT_FIBRE:
198 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
199 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
200 		break;
201 	default:
202 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
203 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
204 		WARN_ON_ONCE(1);
205 	}
206 
207 	/* partially does fall through, to also select lower speeds */
208 	switch (maxspeed) {
209 	case SPEED_25000:
210 		ethtool_link_ksettings_add_link_mode(cmd, supported,
211 						     25000baseSR_Full);
212 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
213 						     25000baseSR_Full);
214 		break;
215 	case SPEED_10000:
216 		ethtool_link_ksettings_add_link_mode(cmd, supported,
217 						     10000baseT_Full);
218 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
219 						     10000baseT_Full);
220 		/* fall through */
221 	case SPEED_1000:
222 		ethtool_link_ksettings_add_link_mode(cmd, supported,
223 						     1000baseT_Full);
224 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
225 						     1000baseT_Full);
226 		ethtool_link_ksettings_add_link_mode(cmd, supported,
227 						     1000baseT_Half);
228 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
229 						     1000baseT_Half);
230 		/* fall through */
231 	case SPEED_100:
232 		ethtool_link_ksettings_add_link_mode(cmd, supported,
233 						     100baseT_Full);
234 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
235 						     100baseT_Full);
236 		ethtool_link_ksettings_add_link_mode(cmd, supported,
237 						     100baseT_Half);
238 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
239 						     100baseT_Half);
240 		/* fall through */
241 	case SPEED_10:
242 		ethtool_link_ksettings_add_link_mode(cmd, supported,
243 						     10baseT_Full);
244 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
245 						     10baseT_Full);
246 		ethtool_link_ksettings_add_link_mode(cmd, supported,
247 						     10baseT_Half);
248 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
249 						     10baseT_Half);
250 		break;
251 	default:
252 		ethtool_link_ksettings_add_link_mode(cmd, supported,
253 						     10baseT_Full);
254 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
255 						     10baseT_Full);
256 		ethtool_link_ksettings_add_link_mode(cmd, supported,
257 						     10baseT_Half);
258 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
259 						     10baseT_Half);
260 		WARN_ON_ONCE(1);
261 	}
262 }
263 
264 
265 static int qeth_get_link_ksettings(struct net_device *netdev,
266 				   struct ethtool_link_ksettings *cmd)
267 {
268 	struct qeth_card *card = netdev->ml_priv;
269 	enum qeth_link_types link_type;
270 	struct carrier_info carrier_info;
271 	int rc;
272 
273 	if (IS_IQD(card) || IS_VM_NIC(card))
274 		link_type = QETH_LINK_TYPE_10GBIT_ETH;
275 	else
276 		link_type = card->info.link_type;
277 
278 	cmd->base.duplex = DUPLEX_FULL;
279 	cmd->base.autoneg = AUTONEG_ENABLE;
280 	cmd->base.phy_address = 0;
281 	cmd->base.mdio_support = 0;
282 	cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
283 	cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
284 
285 	switch (link_type) {
286 	case QETH_LINK_TYPE_FAST_ETH:
287 	case QETH_LINK_TYPE_LANE_ETH100:
288 		cmd->base.speed = SPEED_100;
289 		cmd->base.port = PORT_TP;
290 		break;
291 	case QETH_LINK_TYPE_GBIT_ETH:
292 	case QETH_LINK_TYPE_LANE_ETH1000:
293 		cmd->base.speed = SPEED_1000;
294 		cmd->base.port = PORT_FIBRE;
295 		break;
296 	case QETH_LINK_TYPE_10GBIT_ETH:
297 		cmd->base.speed = SPEED_10000;
298 		cmd->base.port = PORT_FIBRE;
299 		break;
300 	case QETH_LINK_TYPE_25GBIT_ETH:
301 		cmd->base.speed = SPEED_25000;
302 		cmd->base.port = PORT_FIBRE;
303 		break;
304 	default:
305 		cmd->base.speed = SPEED_10;
306 		cmd->base.port = PORT_TP;
307 	}
308 	qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port);
309 
310 	/* Check if we can obtain more accurate information.	 */
311 	/* If QUERY_CARD_INFO command is not supported or fails, */
312 	/* just return the heuristics that was filled above.	 */
313 	rc = qeth_query_card_info(card, &carrier_info);
314 	if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
315 		return 0;
316 	if (rc) /* report error from the hardware operation */
317 		return rc;
318 	/* on success, fill in the information got from the hardware */
319 
320 	netdev_dbg(netdev,
321 	"card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
322 			carrier_info.card_type,
323 			carrier_info.port_mode,
324 			carrier_info.port_speed);
325 
326 	/* Update attributes for which we've obtained more authoritative */
327 	/* information, leave the rest the way they where filled above.  */
328 	switch (carrier_info.card_type) {
329 	case CARD_INFO_TYPE_1G_COPPER_A:
330 	case CARD_INFO_TYPE_1G_COPPER_B:
331 		cmd->base.port = PORT_TP;
332 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
333 		break;
334 	case CARD_INFO_TYPE_1G_FIBRE_A:
335 	case CARD_INFO_TYPE_1G_FIBRE_B:
336 		cmd->base.port = PORT_FIBRE;
337 		qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
338 		break;
339 	case CARD_INFO_TYPE_10G_FIBRE_A:
340 	case CARD_INFO_TYPE_10G_FIBRE_B:
341 		cmd->base.port = PORT_FIBRE;
342 		qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port);
343 		break;
344 	}
345 
346 	switch (carrier_info.port_mode) {
347 	case CARD_INFO_PORTM_FULLDUPLEX:
348 		cmd->base.duplex = DUPLEX_FULL;
349 		break;
350 	case CARD_INFO_PORTM_HALFDUPLEX:
351 		cmd->base.duplex = DUPLEX_HALF;
352 		break;
353 	}
354 
355 	switch (carrier_info.port_speed) {
356 	case CARD_INFO_PORTS_10M:
357 		cmd->base.speed = SPEED_10;
358 		break;
359 	case CARD_INFO_PORTS_100M:
360 		cmd->base.speed = SPEED_100;
361 		break;
362 	case CARD_INFO_PORTS_1G:
363 		cmd->base.speed = SPEED_1000;
364 		break;
365 	case CARD_INFO_PORTS_10G:
366 		cmd->base.speed = SPEED_10000;
367 		break;
368 	case CARD_INFO_PORTS_25G:
369 		cmd->base.speed = SPEED_25000;
370 		break;
371 	}
372 
373 	return 0;
374 }
375 
376 const struct ethtool_ops qeth_ethtool_ops = {
377 	.get_link = ethtool_op_get_link,
378 	.get_ringparam = qeth_get_ringparam,
379 	.get_strings = qeth_get_strings,
380 	.get_ethtool_stats = qeth_get_ethtool_stats,
381 	.get_sset_count = qeth_get_sset_count,
382 	.get_drvinfo = qeth_get_drvinfo,
383 	.get_channels = qeth_get_channels,
384 	.get_link_ksettings = qeth_get_link_ksettings,
385 };
386 
387 const struct ethtool_ops qeth_osn_ethtool_ops = {
388 	.get_strings = qeth_get_strings,
389 	.get_ethtool_stats = qeth_get_ethtool_stats,
390 	.get_sset_count = qeth_get_sset_count,
391 	.get_drvinfo = qeth_get_drvinfo,
392 };
393