1 /* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12 
13 #include <linux/ethtool.h>
14 #include <linux/phy.h>
15 
16 #include "emac.h"
17 
18 static const char * const emac_ethtool_stat_strings[] = {
19 	"rx_ok",
20 	"rx_bcast",
21 	"rx_mcast",
22 	"rx_pause",
23 	"rx_ctrl",
24 	"rx_fcs_err",
25 	"rx_len_err",
26 	"rx_byte_cnt",
27 	"rx_runt",
28 	"rx_frag",
29 	"rx_sz_64",
30 	"rx_sz_65_127",
31 	"rx_sz_128_255",
32 	"rx_sz_256_511",
33 	"rx_sz_512_1023",
34 	"rx_sz_1024_1518",
35 	"rx_sz_1519_max",
36 	"rx_sz_ov",
37 	"rx_rxf_ov",
38 	"rx_align_err",
39 	"rx_bcast_byte_cnt",
40 	"rx_mcast_byte_cnt",
41 	"rx_err_addr",
42 	"rx_crc_align",
43 	"rx_jabbers",
44 	"tx_ok",
45 	"tx_bcast",
46 	"tx_mcast",
47 	"tx_pause",
48 	"tx_exc_defer",
49 	"tx_ctrl",
50 	"tx_defer",
51 	"tx_byte_cnt",
52 	"tx_sz_64",
53 	"tx_sz_65_127",
54 	"tx_sz_128_255",
55 	"tx_sz_256_511",
56 	"tx_sz_512_1023",
57 	"tx_sz_1024_1518",
58 	"tx_sz_1519_max",
59 	"tx_1_col",
60 	"tx_2_col",
61 	"tx_late_col",
62 	"tx_abort_col",
63 	"tx_underrun",
64 	"tx_rd_eop",
65 	"tx_len_err",
66 	"tx_trunc",
67 	"tx_bcast_byte",
68 	"tx_mcast_byte",
69 	"tx_col",
70 };
71 
72 #define EMAC_STATS_LEN	ARRAY_SIZE(emac_ethtool_stat_strings)
73 
74 static u32 emac_get_msglevel(struct net_device *netdev)
75 {
76 	struct emac_adapter *adpt = netdev_priv(netdev);
77 
78 	return adpt->msg_enable;
79 }
80 
81 static void emac_set_msglevel(struct net_device *netdev, u32 data)
82 {
83 	struct emac_adapter *adpt = netdev_priv(netdev);
84 
85 	adpt->msg_enable = data;
86 }
87 
88 static int emac_get_sset_count(struct net_device *netdev, int sset)
89 {
90 	switch (sset) {
91 	case ETH_SS_STATS:
92 		return EMAC_STATS_LEN;
93 	default:
94 		return -EOPNOTSUPP;
95 	}
96 }
97 
98 static void emac_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
99 {
100 	unsigned int i;
101 
102 	switch (stringset) {
103 	case ETH_SS_STATS:
104 		for (i = 0; i < EMAC_STATS_LEN; i++) {
105 			strlcpy(data, emac_ethtool_stat_strings[i],
106 				ETH_GSTRING_LEN);
107 			data += ETH_GSTRING_LEN;
108 		}
109 		break;
110 	}
111 }
112 
113 static void emac_get_ethtool_stats(struct net_device *netdev,
114 				   struct ethtool_stats *stats,
115 				   u64 *data)
116 {
117 	struct emac_adapter *adpt = netdev_priv(netdev);
118 
119 	spin_lock(&adpt->stats.lock);
120 
121 	emac_update_hw_stats(adpt);
122 	memcpy(data, &adpt->stats, EMAC_STATS_LEN * sizeof(u64));
123 
124 	spin_unlock(&adpt->stats.lock);
125 }
126 
127 static int emac_nway_reset(struct net_device *netdev)
128 {
129 	struct phy_device *phydev = netdev->phydev;
130 
131 	if (!phydev)
132 		return -ENODEV;
133 
134 	return genphy_restart_aneg(phydev);
135 }
136 
137 static void emac_get_ringparam(struct net_device *netdev,
138 			       struct ethtool_ringparam *ring)
139 {
140 	struct emac_adapter *adpt = netdev_priv(netdev);
141 
142 	ring->rx_max_pending = EMAC_MAX_RX_DESCS;
143 	ring->tx_max_pending = EMAC_MAX_TX_DESCS;
144 	ring->rx_pending = adpt->rx_desc_cnt;
145 	ring->tx_pending = adpt->tx_desc_cnt;
146 }
147 
148 static int emac_set_ringparam(struct net_device *netdev,
149 			      struct ethtool_ringparam *ring)
150 {
151 	struct emac_adapter *adpt = netdev_priv(netdev);
152 
153 	/* We don't have separate queues/rings for small/large frames, so
154 	 * reject any attempt to specify those values separately.
155 	 */
156 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
157 		return -EINVAL;
158 
159 	adpt->tx_desc_cnt =
160 		clamp_val(ring->tx_pending, EMAC_MIN_TX_DESCS, EMAC_MAX_TX_DESCS);
161 
162 	adpt->rx_desc_cnt =
163 		clamp_val(ring->rx_pending, EMAC_MIN_RX_DESCS, EMAC_MAX_RX_DESCS);
164 
165 	if (netif_running(netdev))
166 		return emac_reinit_locked(adpt);
167 
168 	return 0;
169 }
170 
171 static void emac_get_pauseparam(struct net_device *netdev,
172 				struct ethtool_pauseparam *pause)
173 {
174 	struct emac_adapter *adpt = netdev_priv(netdev);
175 
176 	pause->autoneg = adpt->automatic ? AUTONEG_ENABLE : AUTONEG_DISABLE;
177 	pause->rx_pause = adpt->rx_flow_control ? 1 : 0;
178 	pause->tx_pause = adpt->tx_flow_control ? 1 : 0;
179 }
180 
181 static int emac_set_pauseparam(struct net_device *netdev,
182 			       struct ethtool_pauseparam *pause)
183 {
184 	struct emac_adapter *adpt = netdev_priv(netdev);
185 
186 	adpt->automatic = pause->autoneg == AUTONEG_ENABLE;
187 	adpt->rx_flow_control = pause->rx_pause != 0;
188 	adpt->tx_flow_control = pause->tx_pause != 0;
189 
190 	if (netif_running(netdev))
191 		return emac_reinit_locked(adpt);
192 
193 	return 0;
194 }
195 
196 /* Selected registers that might want to track during runtime. */
197 static const u16 emac_regs[] = {
198 	EMAC_DMA_MAS_CTRL,
199 	EMAC_MAC_CTRL,
200 	EMAC_TXQ_CTRL_0,
201 	EMAC_RXQ_CTRL_0,
202 	EMAC_DMA_CTRL,
203 	EMAC_INT_MASK,
204 	EMAC_AXI_MAST_CTRL,
205 	EMAC_CORE_HW_VERSION,
206 	EMAC_MISC_CTRL,
207 };
208 
209 /* Every time emac_regs[] above is changed, increase this version number. */
210 #define EMAC_REGS_VERSION	0
211 
212 #define EMAC_MAX_REG_SIZE	ARRAY_SIZE(emac_regs)
213 
214 static void emac_get_regs(struct net_device *netdev,
215 			  struct ethtool_regs *regs, void *buff)
216 {
217 	struct emac_adapter *adpt = netdev_priv(netdev);
218 	u32 *val = buff;
219 	unsigned int i;
220 
221 	regs->version = EMAC_REGS_VERSION;
222 	regs->len = EMAC_MAX_REG_SIZE * sizeof(u32);
223 
224 	for (i = 0; i < EMAC_MAX_REG_SIZE; i++)
225 		val[i] = readl(adpt->base + emac_regs[i]);
226 }
227 
228 static int emac_get_regs_len(struct net_device *netdev)
229 {
230 	return EMAC_MAX_REG_SIZE * sizeof(u32);
231 }
232 
233 static const struct ethtool_ops emac_ethtool_ops = {
234 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
235 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
236 
237 	.get_msglevel    = emac_get_msglevel,
238 	.set_msglevel    = emac_set_msglevel,
239 
240 	.get_sset_count  = emac_get_sset_count,
241 	.get_strings = emac_get_strings,
242 	.get_ethtool_stats = emac_get_ethtool_stats,
243 
244 	.get_ringparam = emac_get_ringparam,
245 	.set_ringparam = emac_set_ringparam,
246 
247 	.get_pauseparam = emac_get_pauseparam,
248 	.set_pauseparam = emac_set_pauseparam,
249 
250 	.nway_reset = emac_nway_reset,
251 
252 	.get_link = ethtool_op_get_link,
253 
254 	.get_regs_len    = emac_get_regs_len,
255 	.get_regs        = emac_get_regs,
256 };
257 
258 void emac_set_ethtool_ops(struct net_device *netdev)
259 {
260 	netdev->ethtool_ops = &emac_ethtool_ops;
261 }
262