xref: /linux/drivers/net/ethernet/atheros/atl1e/atl1e_ethtool.c (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
1 /*
2  * Copyright(c) 2007 Atheros Corporation. All rights reserved.
3  *
4  * Derived from Intel e1000 driver
5  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 59
19  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  */
22 
23 #include <linux/netdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/slab.h>
26 
27 #include "atl1e.h"
28 
29 static int atl1e_get_link_ksettings(struct net_device *netdev,
30 				    struct ethtool_link_ksettings *cmd)
31 {
32 	struct atl1e_adapter *adapter = netdev_priv(netdev);
33 	struct atl1e_hw *hw = &adapter->hw;
34 	u32 supported, advertising;
35 
36 	supported = (SUPPORTED_10baseT_Half  |
37 			   SUPPORTED_10baseT_Full  |
38 			   SUPPORTED_100baseT_Half |
39 			   SUPPORTED_100baseT_Full |
40 			   SUPPORTED_Autoneg       |
41 			   SUPPORTED_TP);
42 	if (hw->nic_type == athr_l1e)
43 		supported |= SUPPORTED_1000baseT_Full;
44 
45 	advertising = ADVERTISED_TP;
46 
47 	advertising |= ADVERTISED_Autoneg;
48 	advertising |= hw->autoneg_advertised;
49 
50 	cmd->base.port = PORT_TP;
51 	cmd->base.phy_address = 0;
52 
53 	if (adapter->link_speed != SPEED_0) {
54 		cmd->base.speed = adapter->link_speed;
55 		if (adapter->link_duplex == FULL_DUPLEX)
56 			cmd->base.duplex = DUPLEX_FULL;
57 		else
58 			cmd->base.duplex = DUPLEX_HALF;
59 	} else {
60 		cmd->base.speed = SPEED_UNKNOWN;
61 		cmd->base.duplex = DUPLEX_UNKNOWN;
62 	}
63 
64 	cmd->base.autoneg = AUTONEG_ENABLE;
65 
66 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
67 						supported);
68 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
69 						advertising);
70 
71 	return 0;
72 }
73 
74 static int atl1e_set_link_ksettings(struct net_device *netdev,
75 				    const struct ethtool_link_ksettings *cmd)
76 {
77 	struct atl1e_adapter *adapter = netdev_priv(netdev);
78 	struct atl1e_hw *hw = &adapter->hw;
79 	u32 advertising;
80 
81 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
82 						cmd->link_modes.advertising);
83 
84 	while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
85 		msleep(1);
86 
87 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
88 		u16 adv4, adv9;
89 
90 		if (advertising & ADVERTISE_1000_FULL) {
91 			if (hw->nic_type == athr_l1e) {
92 				hw->autoneg_advertised =
93 					advertising & AT_ADV_MASK;
94 			} else {
95 				clear_bit(__AT_RESETTING, &adapter->flags);
96 				return -EINVAL;
97 			}
98 		} else if (advertising & ADVERTISE_1000_HALF) {
99 			clear_bit(__AT_RESETTING, &adapter->flags);
100 			return -EINVAL;
101 		} else {
102 			hw->autoneg_advertised =
103 				advertising & AT_ADV_MASK;
104 		}
105 		advertising = hw->autoneg_advertised |
106 				    ADVERTISED_TP | ADVERTISED_Autoneg;
107 
108 		adv4 = hw->mii_autoneg_adv_reg & ~ADVERTISE_ALL;
109 		adv9 = hw->mii_1000t_ctrl_reg & ~MII_AT001_CR_1000T_SPEED_MASK;
110 		if (hw->autoneg_advertised & ADVERTISE_10_HALF)
111 			adv4 |= ADVERTISE_10HALF;
112 		if (hw->autoneg_advertised & ADVERTISE_10_FULL)
113 			adv4 |= ADVERTISE_10FULL;
114 		if (hw->autoneg_advertised & ADVERTISE_100_HALF)
115 			adv4 |= ADVERTISE_100HALF;
116 		if (hw->autoneg_advertised & ADVERTISE_100_FULL)
117 			adv4 |= ADVERTISE_100FULL;
118 		if (hw->autoneg_advertised & ADVERTISE_1000_FULL)
119 			adv9 |= ADVERTISE_1000FULL;
120 
121 		if (adv4 != hw->mii_autoneg_adv_reg ||
122 				adv9 != hw->mii_1000t_ctrl_reg) {
123 			hw->mii_autoneg_adv_reg = adv4;
124 			hw->mii_1000t_ctrl_reg = adv9;
125 			hw->re_autoneg = true;
126 		}
127 
128 	} else {
129 		clear_bit(__AT_RESETTING, &adapter->flags);
130 		return -EINVAL;
131 	}
132 
133 	/* reset the link */
134 
135 	if (netif_running(adapter->netdev)) {
136 		atl1e_down(adapter);
137 		atl1e_up(adapter);
138 	} else
139 		atl1e_reset_hw(&adapter->hw);
140 
141 	clear_bit(__AT_RESETTING, &adapter->flags);
142 	return 0;
143 }
144 
145 static u32 atl1e_get_msglevel(struct net_device *netdev)
146 {
147 #ifdef DBG
148 	return 1;
149 #else
150 	return 0;
151 #endif
152 }
153 
154 static int atl1e_get_regs_len(struct net_device *netdev)
155 {
156 	return AT_REGS_LEN * sizeof(u32);
157 }
158 
159 static void atl1e_get_regs(struct net_device *netdev,
160 			   struct ethtool_regs *regs, void *p)
161 {
162 	struct atl1e_adapter *adapter = netdev_priv(netdev);
163 	struct atl1e_hw *hw = &adapter->hw;
164 	u32 *regs_buff = p;
165 	u16 phy_data;
166 
167 	memset(p, 0, AT_REGS_LEN * sizeof(u32));
168 
169 	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
170 
171 	regs_buff[0]  = AT_READ_REG(hw, REG_VPD_CAP);
172 	regs_buff[1]  = AT_READ_REG(hw, REG_SPI_FLASH_CTRL);
173 	regs_buff[2]  = AT_READ_REG(hw, REG_SPI_FLASH_CONFIG);
174 	regs_buff[3]  = AT_READ_REG(hw, REG_TWSI_CTRL);
175 	regs_buff[4]  = AT_READ_REG(hw, REG_PCIE_DEV_MISC_CTRL);
176 	regs_buff[5]  = AT_READ_REG(hw, REG_MASTER_CTRL);
177 	regs_buff[6]  = AT_READ_REG(hw, REG_MANUAL_TIMER_INIT);
178 	regs_buff[7]  = AT_READ_REG(hw, REG_IRQ_MODU_TIMER_INIT);
179 	regs_buff[8]  = AT_READ_REG(hw, REG_GPHY_CTRL);
180 	regs_buff[9]  = AT_READ_REG(hw, REG_CMBDISDMA_TIMER);
181 	regs_buff[10] = AT_READ_REG(hw, REG_IDLE_STATUS);
182 	regs_buff[11] = AT_READ_REG(hw, REG_MDIO_CTRL);
183 	regs_buff[12] = AT_READ_REG(hw, REG_SERDES_LOCK);
184 	regs_buff[13] = AT_READ_REG(hw, REG_MAC_CTRL);
185 	regs_buff[14] = AT_READ_REG(hw, REG_MAC_IPG_IFG);
186 	regs_buff[15] = AT_READ_REG(hw, REG_MAC_STA_ADDR);
187 	regs_buff[16] = AT_READ_REG(hw, REG_MAC_STA_ADDR+4);
188 	regs_buff[17] = AT_READ_REG(hw, REG_RX_HASH_TABLE);
189 	regs_buff[18] = AT_READ_REG(hw, REG_RX_HASH_TABLE+4);
190 	regs_buff[19] = AT_READ_REG(hw, REG_MAC_HALF_DUPLX_CTRL);
191 	regs_buff[20] = AT_READ_REG(hw, REG_MTU);
192 	regs_buff[21] = AT_READ_REG(hw, REG_WOL_CTRL);
193 	regs_buff[22] = AT_READ_REG(hw, REG_SRAM_TRD_ADDR);
194 	regs_buff[23] = AT_READ_REG(hw, REG_SRAM_TRD_LEN);
195 	regs_buff[24] = AT_READ_REG(hw, REG_SRAM_RXF_ADDR);
196 	regs_buff[25] = AT_READ_REG(hw, REG_SRAM_RXF_LEN);
197 	regs_buff[26] = AT_READ_REG(hw, REG_SRAM_TXF_ADDR);
198 	regs_buff[27] = AT_READ_REG(hw, REG_SRAM_TXF_LEN);
199 	regs_buff[28] = AT_READ_REG(hw, REG_SRAM_TCPH_ADDR);
200 	regs_buff[29] = AT_READ_REG(hw, REG_SRAM_PKTH_ADDR);
201 
202 	atl1e_read_phy_reg(hw, MII_BMCR, &phy_data);
203 	regs_buff[73] = (u32)phy_data;
204 	atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
205 	regs_buff[74] = (u32)phy_data;
206 }
207 
208 static int atl1e_get_eeprom_len(struct net_device *netdev)
209 {
210 	struct atl1e_adapter *adapter = netdev_priv(netdev);
211 
212 	if (!atl1e_check_eeprom_exist(&adapter->hw))
213 		return AT_EEPROM_LEN;
214 	else
215 		return 0;
216 }
217 
218 static int atl1e_get_eeprom(struct net_device *netdev,
219 		struct ethtool_eeprom *eeprom, u8 *bytes)
220 {
221 	struct atl1e_adapter *adapter = netdev_priv(netdev);
222 	struct atl1e_hw *hw = &adapter->hw;
223 	u32 *eeprom_buff;
224 	int first_dword, last_dword;
225 	int ret_val = 0;
226 	int i;
227 
228 	if (eeprom->len == 0)
229 		return -EINVAL;
230 
231 	if (atl1e_check_eeprom_exist(hw)) /* not exist */
232 		return -EINVAL;
233 
234 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
235 
236 	first_dword = eeprom->offset >> 2;
237 	last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
238 
239 	eeprom_buff = kmalloc_array(last_dword - first_dword + 1, sizeof(u32),
240 				    GFP_KERNEL);
241 	if (eeprom_buff == NULL)
242 		return -ENOMEM;
243 
244 	for (i = first_dword; i < last_dword; i++) {
245 		if (!atl1e_read_eeprom(hw, i * 4, &(eeprom_buff[i-first_dword]))) {
246 			kfree(eeprom_buff);
247 			return -EIO;
248 		}
249 	}
250 
251 	memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 3),
252 			eeprom->len);
253 	kfree(eeprom_buff);
254 
255 	return ret_val;
256 }
257 
258 static int atl1e_set_eeprom(struct net_device *netdev,
259 			    struct ethtool_eeprom *eeprom, u8 *bytes)
260 {
261 	struct atl1e_adapter *adapter = netdev_priv(netdev);
262 	struct atl1e_hw *hw = &adapter->hw;
263 	u32 *eeprom_buff;
264 	u32 *ptr;
265 	int first_dword, last_dword;
266 	int ret_val = 0;
267 	int i;
268 
269 	if (eeprom->len == 0)
270 		return -EOPNOTSUPP;
271 
272 	if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
273 		return -EINVAL;
274 
275 	first_dword = eeprom->offset >> 2;
276 	last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
277 	eeprom_buff = kmalloc(AT_EEPROM_LEN, GFP_KERNEL);
278 	if (eeprom_buff == NULL)
279 		return -ENOMEM;
280 
281 	ptr = eeprom_buff;
282 
283 	if (eeprom->offset & 3) {
284 		/* need read/modify/write of first changed EEPROM word */
285 		/* only the second byte of the word is being modified */
286 		if (!atl1e_read_eeprom(hw, first_dword * 4, &(eeprom_buff[0]))) {
287 			ret_val = -EIO;
288 			goto out;
289 		}
290 		ptr++;
291 	}
292 	if (((eeprom->offset + eeprom->len) & 3)) {
293 		/* need read/modify/write of last changed EEPROM word */
294 		/* only the first byte of the word is being modified */
295 
296 		if (!atl1e_read_eeprom(hw, last_dword * 4,
297 				&(eeprom_buff[last_dword - first_dword]))) {
298 			ret_val = -EIO;
299 			goto out;
300 		}
301 	}
302 
303 	/* Device's eeprom is always little-endian, word addressable */
304 	memcpy(ptr, bytes, eeprom->len);
305 
306 	for (i = 0; i < last_dword - first_dword + 1; i++) {
307 		if (!atl1e_write_eeprom(hw, ((first_dword + i) * 4),
308 				  eeprom_buff[i])) {
309 			ret_val = -EIO;
310 			goto out;
311 		}
312 	}
313 out:
314 	kfree(eeprom_buff);
315 	return ret_val;
316 }
317 
318 static void atl1e_get_drvinfo(struct net_device *netdev,
319 		struct ethtool_drvinfo *drvinfo)
320 {
321 	struct atl1e_adapter *adapter = netdev_priv(netdev);
322 
323 	strlcpy(drvinfo->driver,  atl1e_driver_name, sizeof(drvinfo->driver));
324 	strlcpy(drvinfo->version, atl1e_driver_version,
325 		sizeof(drvinfo->version));
326 	strlcpy(drvinfo->fw_version, "L1e", sizeof(drvinfo->fw_version));
327 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
328 		sizeof(drvinfo->bus_info));
329 }
330 
331 static void atl1e_get_wol(struct net_device *netdev,
332 			  struct ethtool_wolinfo *wol)
333 {
334 	struct atl1e_adapter *adapter = netdev_priv(netdev);
335 
336 	wol->supported = WAKE_MAGIC | WAKE_PHY;
337 	wol->wolopts = 0;
338 
339 	if (adapter->wol & AT_WUFC_EX)
340 		wol->wolopts |= WAKE_UCAST;
341 	if (adapter->wol & AT_WUFC_MC)
342 		wol->wolopts |= WAKE_MCAST;
343 	if (adapter->wol & AT_WUFC_BC)
344 		wol->wolopts |= WAKE_BCAST;
345 	if (adapter->wol & AT_WUFC_MAG)
346 		wol->wolopts |= WAKE_MAGIC;
347 	if (adapter->wol & AT_WUFC_LNKC)
348 		wol->wolopts |= WAKE_PHY;
349 }
350 
351 static int atl1e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
352 {
353 	struct atl1e_adapter *adapter = netdev_priv(netdev);
354 
355 	if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE |
356 			    WAKE_UCAST | WAKE_MCAST | WAKE_BCAST))
357 		return -EOPNOTSUPP;
358 	/* these settings will always override what we currently have */
359 	adapter->wol = 0;
360 
361 	if (wol->wolopts & WAKE_MAGIC)
362 		adapter->wol |= AT_WUFC_MAG;
363 	if (wol->wolopts & WAKE_PHY)
364 		adapter->wol |= AT_WUFC_LNKC;
365 
366 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
367 
368 	return 0;
369 }
370 
371 static int atl1e_nway_reset(struct net_device *netdev)
372 {
373 	struct atl1e_adapter *adapter = netdev_priv(netdev);
374 	if (netif_running(netdev))
375 		atl1e_reinit_locked(adapter);
376 	return 0;
377 }
378 
379 static const struct ethtool_ops atl1e_ethtool_ops = {
380 	.get_drvinfo            = atl1e_get_drvinfo,
381 	.get_regs_len           = atl1e_get_regs_len,
382 	.get_regs               = atl1e_get_regs,
383 	.get_wol                = atl1e_get_wol,
384 	.set_wol                = atl1e_set_wol,
385 	.get_msglevel           = atl1e_get_msglevel,
386 	.nway_reset             = atl1e_nway_reset,
387 	.get_link               = ethtool_op_get_link,
388 	.get_eeprom_len         = atl1e_get_eeprom_len,
389 	.get_eeprom             = atl1e_get_eeprom,
390 	.set_eeprom             = atl1e_set_eeprom,
391 	.get_link_ksettings     = atl1e_get_link_ksettings,
392 	.set_link_ksettings     = atl1e_set_link_ksettings,
393 };
394 
395 void atl1e_set_ethtool_ops(struct net_device *netdev)
396 {
397 	netdev->ethtool_ops = &atl1e_ethtool_ops;
398 }
399