1de6cc651SThomas Gleixner// SPDX-License-Identifier: GPL-2.0-or-later
27a291083SJan-Bernd Themann/*
33396c782SPaul Gortmaker *  linux/drivers/net/ethernet/ibm/ehea/ehea_ethtool.c
47a291083SJan-Bernd Themann *
57a291083SJan-Bernd Themann *  eHEA ethernet device driver for IBM eServer System p
67a291083SJan-Bernd Themann *
77a291083SJan-Bernd Themann *  (C) Copyright IBM Corp. 2006
87a291083SJan-Bernd Themann *
97a291083SJan-Bernd Themann *  Authors:
107a291083SJan-Bernd Themann *       Christoph Raisch <raisch@de.ibm.com>
117a291083SJan-Bernd Themann *       Jan-Bernd Themann <themann@de.ibm.com>
127a291083SJan-Bernd Themann *       Thomas Klein <tklein@de.ibm.com>
137a291083SJan-Bernd Themann */
147a291083SJan-Bernd Themann
158c4877a4SJoe Perches#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
168c4877a4SJoe Perches
177a291083SJan-Bernd Themann#include "ehea.h"
187a291083SJan-Bernd Themann#include "ehea_phyp.h"
197a291083SJan-Bernd Themann
20cecf62d6SPhilippe Reynesstatic int ehea_get_link_ksettings(struct net_device *dev,
21cecf62d6SPhilippe Reynes				   struct ethtool_link_ksettings *cmd)
227a291083SJan-Bernd Themann{
237a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
24cecf62d6SPhilippe Reynes	u32 supported, advertising;
2570739497SDavid Decotigny	u32 speed;
267a291083SJan-Bernd Themann	int ret;
277a291083SJan-Bernd Themann
287a291083SJan-Bernd Themann	ret = ehea_sense_port_attr(port);
297a291083SJan-Bernd Themann
307a291083SJan-Bernd Themann	if (ret)
317a291083SJan-Bernd Themann		return ret;
327a291083SJan-Bernd Themann
337a291083SJan-Bernd Themann	if (netif_carrier_ok(dev)) {
3413da93d4SDoug Maxey		switch (port->port_speed) {
3570739497SDavid Decotigny		case EHEA_SPEED_10M:
3670739497SDavid Decotigny			speed = SPEED_10;
3770739497SDavid Decotigny			break;
3870739497SDavid Decotigny		case EHEA_SPEED_100M:
3970739497SDavid Decotigny			speed = SPEED_100;
4070739497SDavid Decotigny			break;
4170739497SDavid Decotigny		case EHEA_SPEED_1G:
4270739497SDavid Decotigny			speed = SPEED_1000;
4370739497SDavid Decotigny			break;
4470739497SDavid Decotigny		case EHEA_SPEED_10G:
4570739497SDavid Decotigny			speed = SPEED_10000;
4670739497SDavid Decotigny			break;
4770739497SDavid Decotigny		default:
4870739497SDavid Decotigny			speed = -1;
4970739497SDavid Decotigny			break; /* BUG */
507a291083SJan-Bernd Themann		}
51cecf62d6SPhilippe Reynes		cmd->base.duplex = port->full_duplex == 1 ?
527a291083SJan-Bernd Themann						     DUPLEX_FULL : DUPLEX_HALF;
537a291083SJan-Bernd Themann	} else {
54537fae01SJiri Pirko		speed = SPEED_UNKNOWN;
55cecf62d6SPhilippe Reynes		cmd->base.duplex = DUPLEX_UNKNOWN;
567a291083SJan-Bernd Themann	}
57cecf62d6SPhilippe Reynes	cmd->base.speed = speed;
587a291083SJan-Bernd Themann
59cecf62d6SPhilippe Reynes	if (cmd->base.speed == SPEED_10000) {
60cecf62d6SPhilippe Reynes		supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
61cecf62d6SPhilippe Reynes		advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
62cecf62d6SPhilippe Reynes		cmd->base.port = PORT_FIBRE;
63dcbe14b9SKleber Sacilotto de Souza	} else {
64cecf62d6SPhilippe Reynes		supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full
65dcbe14b9SKleber Sacilotto de Souza			       | SUPPORTED_100baseT_Half | SUPPORTED_10baseT_Full
66dcbe14b9SKleber Sacilotto de Souza			       | SUPPORTED_10baseT_Half | SUPPORTED_Autoneg
67dcbe14b9SKleber Sacilotto de Souza			       | SUPPORTED_TP);
68cecf62d6SPhilippe Reynes		advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg
69dcbe14b9SKleber Sacilotto de Souza				 | ADVERTISED_TP);
70cecf62d6SPhilippe Reynes		cmd->base.port = PORT_TP;
71dcbe14b9SKleber Sacilotto de Souza	}
727a291083SJan-Bernd Themann
73cecf62d6SPhilippe Reynes	cmd->base.autoneg = port->autoneg == 1 ?
74cecf62d6SPhilippe Reynes		AUTONEG_ENABLE : AUTONEG_DISABLE;
75cecf62d6SPhilippe Reynes
76cecf62d6SPhilippe Reynes	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
77cecf62d6SPhilippe Reynes						supported);
78cecf62d6SPhilippe Reynes	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
79cecf62d6SPhilippe Reynes						advertising);
807a291083SJan-Bernd Themann
817a291083SJan-Bernd Themann	return 0;
827a291083SJan-Bernd Themann}
837a291083SJan-Bernd Themann
84cecf62d6SPhilippe Reynesstatic int ehea_set_link_ksettings(struct net_device *dev,
85cecf62d6SPhilippe Reynes				   const struct ethtool_link_ksettings *cmd)
867a291083SJan-Bernd Themann{
877a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
887a291083SJan-Bernd Themann	int ret = 0;
897a291083SJan-Bernd Themann	u32 sp;
907a291083SJan-Bernd Themann
91cecf62d6SPhilippe Reynes	if (cmd->base.autoneg == AUTONEG_ENABLE) {
927a291083SJan-Bernd Themann		sp = EHEA_SPEED_AUTONEG;
937a291083SJan-Bernd Themann		goto doit;
947a291083SJan-Bernd Themann	}
957a291083SJan-Bernd Themann
96cecf62d6SPhilippe Reynes	switch (cmd->base.speed) {
977a291083SJan-Bernd Themann	case SPEED_10:
98cecf62d6SPhilippe Reynes		if (cmd->base.duplex == DUPLEX_FULL)
997a291083SJan-Bernd Themann			sp = H_SPEED_10M_F;
1007a291083SJan-Bernd Themann		else
1017a291083SJan-Bernd Themann			sp = H_SPEED_10M_H;
1027a291083SJan-Bernd Themann		break;
1037a291083SJan-Bernd Themann
1047a291083SJan-Bernd Themann	case SPEED_100:
105cecf62d6SPhilippe Reynes		if (cmd->base.duplex == DUPLEX_FULL)
1067a291083SJan-Bernd Themann			sp = H_SPEED_100M_F;
1077a291083SJan-Bernd Themann		else
1087a291083SJan-Bernd Themann			sp = H_SPEED_100M_H;
1097a291083SJan-Bernd Themann		break;
1107a291083SJan-Bernd Themann
1117a291083SJan-Bernd Themann	case SPEED_1000:
112cecf62d6SPhilippe Reynes		if (cmd->base.duplex == DUPLEX_FULL)
1137a291083SJan-Bernd Themann			sp = H_SPEED_1G_F;
1147a291083SJan-Bernd Themann		else
1157a291083SJan-Bernd Themann			ret = -EINVAL;
1167a291083SJan-Bernd Themann		break;
1177a291083SJan-Bernd Themann
1187a291083SJan-Bernd Themann	case SPEED_10000:
119cecf62d6SPhilippe Reynes		if (cmd->base.duplex == DUPLEX_FULL)
1207a291083SJan-Bernd Themann			sp = H_SPEED_10G_F;
1217a291083SJan-Bernd Themann		else
1227a291083SJan-Bernd Themann			ret = -EINVAL;
1237a291083SJan-Bernd Themann		break;
1247a291083SJan-Bernd Themann
1257a291083SJan-Bernd Themann	default:
1267a291083SJan-Bernd Themann			ret = -EINVAL;
1277a291083SJan-Bernd Themann		break;
1287a291083SJan-Bernd Themann	}
1297a291083SJan-Bernd Themann
1307a291083SJan-Bernd Themann	if (ret)
1317a291083SJan-Bernd Themann		goto out;
1327a291083SJan-Bernd Themanndoit:
1337a291083SJan-Bernd Themann	ret = ehea_set_portspeed(port, sp);
1347a291083SJan-Bernd Themann
1357a291083SJan-Bernd Themann	if (!ret)
1368c4877a4SJoe Perches		netdev_info(dev,
1378c4877a4SJoe Perches			    "Port speed successfully set: %dMbps %s Duplex\n",
1388c4877a4SJoe Perches			    port->port_speed,
1398c4877a4SJoe Perches			    port->full_duplex == 1 ? "Full" : "Half");
1407a291083SJan-Bernd Themannout:
1417a291083SJan-Bernd Themann	return ret;
1427a291083SJan-Bernd Themann}
1437a291083SJan-Bernd Themann
1447a291083SJan-Bernd Themannstatic int ehea_nway_reset(struct net_device *dev)
1457a291083SJan-Bernd Themann{
1467a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
1477a291083SJan-Bernd Themann	int ret;
1487a291083SJan-Bernd Themann
1497a291083SJan-Bernd Themann	ret = ehea_set_portspeed(port, EHEA_SPEED_AUTONEG);
1507a291083SJan-Bernd Themann
1517a291083SJan-Bernd Themann	if (!ret)
1528c4877a4SJoe Perches		netdev_info(port->netdev,
1538c4877a4SJoe Perches			    "Port speed successfully set: %dMbps %s Duplex\n",
1548c4877a4SJoe Perches			    port->port_speed,
1558c4877a4SJoe Perches			    port->full_duplex == 1 ? "Full" : "Half");
1567a291083SJan-Bernd Themann	return ret;
1577a291083SJan-Bernd Themann}
1587a291083SJan-Bernd Themann
1597a291083SJan-Bernd Themannstatic void ehea_get_drvinfo(struct net_device *dev,
1607a291083SJan-Bernd Themann			       struct ethtool_drvinfo *info)
1617a291083SJan-Bernd Themann{
1624eccccb3SJean Delvare	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1634eccccb3SJean Delvare	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1647a291083SJan-Bernd Themann}
1657a291083SJan-Bernd Themann
1667a291083SJan-Bernd Themannstatic u32 ehea_get_msglevel(struct net_device *dev)
1677a291083SJan-Bernd Themann{
1687a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
1697a291083SJan-Bernd Themann	return port->msg_enable;
1707a291083SJan-Bernd Themann}
1717a291083SJan-Bernd Themann
1727a291083SJan-Bernd Themannstatic void ehea_set_msglevel(struct net_device *dev, u32 value)
1737a291083SJan-Bernd Themann{
1747a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
1757a291083SJan-Bernd Themann	port->msg_enable = value;
1767a291083SJan-Bernd Themann}
1777a291083SJan-Bernd Themann
178b9564468SAnton Blanchardstatic const char ehea_ethtool_stats_keys[][ETH_GSTRING_LEN] = {
1797a291083SJan-Bernd Themann	{"sig_comp_iv"},
1807a291083SJan-Bernd Themann	{"swqe_refill_th"},
1817a291083SJan-Bernd Themann	{"port resets"},
182acbddb59SJan-Bernd Themann	{"Receive errors"},
183acbddb59SJan-Bernd Themann	{"TCP cksum errors"},
184acbddb59SJan-Bernd Themann	{"IP cksum errors"},
185acbddb59SJan-Bernd Themann	{"Frame cksum errors"},
186acbddb59SJan-Bernd Themann	{"num SQ stopped"},
187acbddb59SJan-Bernd Themann	{"PR0 free_swqes"},
188acbddb59SJan-Bernd Themann	{"PR1 free_swqes"},
189acbddb59SJan-Bernd Themann	{"PR2 free_swqes"},
190acbddb59SJan-Bernd Themann	{"PR3 free_swqes"},
191acbddb59SJan-Bernd Themann	{"PR4 free_swqes"},
192acbddb59SJan-Bernd Themann	{"PR5 free_swqes"},
193acbddb59SJan-Bernd Themann	{"PR6 free_swqes"},
194acbddb59SJan-Bernd Themann	{"PR7 free_swqes"},
195b9564468SAnton Blanchard	{"PR8 free_swqes"},
196b9564468SAnton Blanchard	{"PR9 free_swqes"},
197b9564468SAnton Blanchard	{"PR10 free_swqes"},
198b9564468SAnton Blanchard	{"PR11 free_swqes"},
199b9564468SAnton Blanchard	{"PR12 free_swqes"},
200b9564468SAnton Blanchard	{"PR13 free_swqes"},
201b9564468SAnton Blanchard	{"PR14 free_swqes"},
202b9564468SAnton Blanchard	{"PR15 free_swqes"},
2037a291083SJan-Bernd Themann};
2047a291083SJan-Bernd Themann
2057a291083SJan-Bernd Themannstatic void ehea_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2067a291083SJan-Bernd Themann{
2077a291083SJan-Bernd Themann	if (stringset == ETH_SS_STATS) {
2087a291083SJan-Bernd Themann		memcpy(data, &ehea_ethtool_stats_keys,
2097a291083SJan-Bernd Themann		       sizeof(ehea_ethtool_stats_keys));
2107a291083SJan-Bernd Themann	}
2117a291083SJan-Bernd Themann}
2127a291083SJan-Bernd Themann
213b9f2c044SJeff Garzikstatic int ehea_get_sset_count(struct net_device *dev, int sset)
2147a291083SJan-Bernd Themann{
215b9f2c044SJeff Garzik	switch (sset) {
216b9f2c044SJeff Garzik	case ETH_SS_STATS:
217b9f2c044SJeff Garzik		return ARRAY_SIZE(ehea_ethtool_stats_keys);
218b9f2c044SJeff Garzik	default:
219b9f2c044SJeff Garzik		return -EOPNOTSUPP;
220b9f2c044SJeff Garzik	}
2217a291083SJan-Bernd Themann}
2227a291083SJan-Bernd Themann
2237a291083SJan-Bernd Themannstatic void ehea_get_ethtool_stats(struct net_device *dev,
2247a291083SJan-Bernd Themann				     struct ethtool_stats *stats, u64 *data)
2257a291083SJan-Bernd Themann{
226acbddb59SJan-Bernd Themann	int i, k, tmp;
2277a291083SJan-Bernd Themann	struct ehea_port *port = netdev_priv(dev);
2287a291083SJan-Bernd Themann
229b9f2c044SJeff Garzik	for (i = 0; i < ehea_get_sset_count(dev, ETH_SS_STATS); i++)
2307a291083SJan-Bernd Themann		data[i] = 0;
2317a291083SJan-Bernd Themann	i = 0;
2327a291083SJan-Bernd Themann
2337a291083SJan-Bernd Themann	data[i++] = port->sig_comp_iv;
2347a291083SJan-Bernd Themann	data[i++] = port->port_res[0].swqe_refill_th;
2357a291083SJan-Bernd Themann	data[i++] = port->resets;
2367a291083SJan-Bernd Themann
237acbddb59SJan-Bernd Themann	for (k = 0, tmp = 0; k < EHEA_MAX_PORT_RES; k++)
238acbddb59SJan-Bernd Themann		tmp += port->port_res[k].p_stats.poll_receive_errors;
239acbddb59SJan-Bernd Themann	data[i++] = tmp;
240acbddb59SJan-Bernd Themann
241acbddb59SJan-Bernd Themann	for (k = 0, tmp = 0; k < EHEA_MAX_PORT_RES; k++)
242acbddb59SJan-Bernd Themann		tmp += port->port_res[k].p_stats.err_tcp_cksum;
243acbddb59SJan-Bernd Themann	data[i++] = tmp;
244acbddb59SJan-Bernd Themann
245acbddb59SJan-Bernd Themann	for (k = 0, tmp = 0; k < EHEA_MAX_PORT_RES; k++)
246acbddb59SJan-Bernd Themann		tmp += port->port_res[k].p_stats.err_ip_cksum;
247acbddb59SJan-Bernd Themann	data[i++] = tmp;
248acbddb59SJan-Bernd Themann
249acbddb59SJan-Bernd Themann	for (k = 0, tmp = 0; k < EHEA_MAX_PORT_RES; k++)
250acbddb59SJan-Bernd Themann		tmp += port->port_res[k].p_stats.err_frame_crc;
251acbddb59SJan-Bernd Themann	data[i++] = tmp;
252acbddb59SJan-Bernd Themann
253acbddb59SJan-Bernd Themann	for (k = 0, tmp = 0; k < EHEA_MAX_PORT_RES; k++)
254acbddb59SJan-Bernd Themann		tmp += port->port_res[k].p_stats.queue_stopped;
255acbddb59SJan-Bernd Themann	data[i++] = tmp;
256acbddb59SJan-Bernd Themann
257b9564468SAnton Blanchard	for (k = 0; k < 16; k++)
258acbddb59SJan-Bernd Themann		data[i++] = atomic_read(&port->port_res[k].swqe_avail);
2597a291083SJan-Bernd Themann}
2607a291083SJan-Bernd Themann
2611886e5d2SThadeu Lima de Souza Cascardostatic const struct ethtool_ops ehea_ethtool_ops = {
2627a291083SJan-Bernd Themann	.get_drvinfo = ehea_get_drvinfo,
2637a291083SJan-Bernd Themann	.get_msglevel = ehea_get_msglevel,
2647a291083SJan-Bernd Themann	.set_msglevel = ehea_set_msglevel,
2657a291083SJan-Bernd Themann	.get_link = ethtool_op_get_link,
2667a291083SJan-Bernd Themann	.get_strings = ehea_get_strings,
267b9f2c044SJeff Garzik	.get_sset_count = ehea_get_sset_count,
2687a291083SJan-Bernd Themann	.get_ethtool_stats = ehea_get_ethtool_stats,
2697a291083SJan-Bernd Themann	.nway_reset = ehea_nway_reset,		/* Restart autonegotiation */
270cecf62d6SPhilippe Reynes	.get_link_ksettings = ehea_get_link_ksettings,
271cecf62d6SPhilippe Reynes	.set_link_ksettings = ehea_set_link_ksettings,
2727a291083SJan-Bernd Themann};
2737a291083SJan-Bernd Themann
2747a291083SJan-Bernd Themannvoid ehea_set_ethtool_ops(struct net_device *netdev)
2757a291083SJan-Bernd Themann{
2767ad24ea4SWilfried Klaebe	netdev->ethtool_ops = &ehea_ethtool_ops;
2777a291083SJan-Bernd Themann}
278