1 // SPDX-License-Identifier: GPL-2.0
3 * DPAA2 Ethernet Switch ethtool support
5 * Copyright 2014-2016 Freescale Semiconductor Inc.
6 * Copyright 2017-2018 NXP
10 #include <linux/ethtool.h>
16 char name[ETH_GSTRING_LEN];
17 } dpaa2_switch_ethtool_counters[] = {
18 {DPSW_CNT_ING_FRAME, "rx frames"},
19 {DPSW_CNT_ING_BYTE, "rx bytes"},
20 {DPSW_CNT_ING_FLTR_FRAME, "rx filtered frames"},
21 {DPSW_CNT_ING_FRAME_DISCARD, "rx discarded frames"},
22 {DPSW_CNT_ING_BCAST_FRAME, "rx b-cast frames"},
23 {DPSW_CNT_ING_BCAST_BYTES, "rx b-cast bytes"},
24 {DPSW_CNT_ING_MCAST_FRAME, "rx m-cast frames"},
25 {DPSW_CNT_ING_MCAST_BYTE, "rx m-cast bytes"},
26 {DPSW_CNT_EGR_FRAME, "tx frames"},
27 {DPSW_CNT_EGR_BYTE, "tx bytes"},
28 {DPSW_CNT_EGR_FRAME_DISCARD, "tx discarded frames"},
29 {DPSW_CNT_ING_NO_BUFF_DISCARD, "rx discarded no buffer frames"},
32 #define DPAA2_SWITCH_NUM_COUNTERS ARRAY_SIZE(dpaa2_switch_ethtool_counters)
34 static void dpaa2_switch_get_drvinfo(struct net_device *netdev,
35 struct ethtool_drvinfo *drvinfo)
37 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
38 u16 version_major, version_minor;
41 strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
43 err = dpsw_get_api_version(port_priv->ethsw_data->mc_io, 0,
47 strlcpy(drvinfo->fw_version, "N/A",
48 sizeof(drvinfo->fw_version));
50 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
51 "%u.%u", version_major, version_minor);
53 strlcpy(drvinfo->bus_info, dev_name(netdev->dev.parent->parent),
54 sizeof(drvinfo->bus_info));
58 dpaa2_switch_get_link_ksettings(struct net_device *netdev,
59 struct ethtool_link_ksettings *link_ksettings)
61 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
62 struct dpsw_link_state state = {0};
65 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
66 port_priv->ethsw_data->dpsw_handle,
70 netdev_err(netdev, "ERROR %d getting link state\n", err);
74 /* At the moment, we have no way of interrogating the DPMAC
75 * from the DPSW side or there may not exist a DPMAC at all.
76 * Report only autoneg state, duplexity and speed.
78 if (state.options & DPSW_LINK_OPT_AUTONEG)
79 link_ksettings->base.autoneg = AUTONEG_ENABLE;
80 if (!(state.options & DPSW_LINK_OPT_HALF_DUPLEX))
81 link_ksettings->base.duplex = DUPLEX_FULL;
82 link_ksettings->base.speed = state.rate;
89 dpaa2_switch_set_link_ksettings(struct net_device *netdev,
90 const struct ethtool_link_ksettings *link_ksettings)
92 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
93 struct ethsw_core *ethsw = port_priv->ethsw_data;
94 struct dpsw_link_cfg cfg = {0};
98 /* Interface needs to be down to change link settings */
99 if_running = netif_running(netdev);
101 err = dpsw_if_disable(ethsw->mc_io, 0,
105 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
110 cfg.rate = link_ksettings->base.speed;
111 if (link_ksettings->base.autoneg == AUTONEG_ENABLE)
112 cfg.options |= DPSW_LINK_OPT_AUTONEG;
114 cfg.options &= ~DPSW_LINK_OPT_AUTONEG;
115 if (link_ksettings->base.duplex == DUPLEX_HALF)
116 cfg.options |= DPSW_LINK_OPT_HALF_DUPLEX;
118 cfg.options &= ~DPSW_LINK_OPT_HALF_DUPLEX;
120 err = dpsw_if_set_link_cfg(port_priv->ethsw_data->mc_io, 0,
121 port_priv->ethsw_data->dpsw_handle,
126 ret = dpsw_if_enable(ethsw->mc_io, 0,
130 netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
137 static int dpaa2_switch_ethtool_get_sset_count(struct net_device *dev, int sset)
141 return DPAA2_SWITCH_NUM_COUNTERS;
147 static void dpaa2_switch_ethtool_get_strings(struct net_device *netdev,
148 u32 stringset, u8 *data)
154 for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++)
155 memcpy(data + i * ETH_GSTRING_LEN,
156 dpaa2_switch_ethtool_counters[i].name,
162 static void dpaa2_switch_ethtool_get_stats(struct net_device *netdev,
163 struct ethtool_stats *stats,
166 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
169 for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++) {
170 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
171 port_priv->ethsw_data->dpsw_handle,
173 dpaa2_switch_ethtool_counters[i].id,
176 netdev_err(netdev, "dpsw_if_get_counter[%s] err %d\n",
177 dpaa2_switch_ethtool_counters[i].name, err);
181 const struct ethtool_ops dpaa2_switch_port_ethtool_ops = {
182 .get_drvinfo = dpaa2_switch_get_drvinfo,
183 .get_link = ethtool_op_get_link,
184 .get_link_ksettings = dpaa2_switch_get_link_ksettings,
185 .set_link_ksettings = dpaa2_switch_set_link_ksettings,
186 .get_strings = dpaa2_switch_ethtool_get_strings,
187 .get_ethtool_stats = dpaa2_switch_ethtool_get_stats,
188 .get_sset_count = dpaa2_switch_ethtool_get_sset_count,