]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/atl1e/atl1e_ethtool.c
bb15c51b91806c855856ca5727e7c219c5210996
[net-next-2.6.git] / drivers / net / atl1e / atl1e_ethtool.c
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
26 #include "atl1e.h"
27
28 static int atl1e_get_settings(struct net_device *netdev,
29                               struct ethtool_cmd *ecmd)
30 {
31         struct atl1e_adapter *adapter = netdev_priv(netdev);
32         struct atl1e_hw *hw = &adapter->hw;
33
34         ecmd->supported = (SUPPORTED_10baseT_Half  |
35                            SUPPORTED_10baseT_Full  |
36                            SUPPORTED_100baseT_Half |
37                            SUPPORTED_100baseT_Full |
38                            SUPPORTED_Autoneg       |
39                            SUPPORTED_TP);
40         if (hw->nic_type == athr_l1e)
41                 ecmd->supported |= SUPPORTED_1000baseT_Full;
42
43         ecmd->advertising = ADVERTISED_TP;
44
45         ecmd->advertising |= ADVERTISED_Autoneg;
46         ecmd->advertising |= hw->autoneg_advertised;
47
48         ecmd->port = PORT_TP;
49         ecmd->phy_address = 0;
50         ecmd->transceiver = XCVR_INTERNAL;
51
52         if (adapter->link_speed != SPEED_0) {
53                 ecmd->speed = adapter->link_speed;
54                 if (adapter->link_duplex == FULL_DUPLEX)
55                         ecmd->duplex = DUPLEX_FULL;
56                 else
57                         ecmd->duplex = DUPLEX_HALF;
58         } else {
59                 ecmd->speed = -1;
60                 ecmd->duplex = -1;
61         }
62
63         ecmd->autoneg = AUTONEG_ENABLE;
64         return 0;
65 }
66
67 static int atl1e_set_settings(struct net_device *netdev,
68                               struct ethtool_cmd *ecmd)
69 {
70         struct atl1e_adapter *adapter = netdev_priv(netdev);
71         struct atl1e_hw *hw = &adapter->hw;
72
73         while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
74                 msleep(1);
75
76         if (ecmd->autoneg == AUTONEG_ENABLE) {
77                 u16 adv4, adv9;
78
79                 if ((ecmd->advertising&ADVERTISE_1000_FULL)) {
80                         if (hw->nic_type == athr_l1e) {
81                                 hw->autoneg_advertised =
82                                         ecmd->advertising & AT_ADV_MASK;
83                         } else {
84                                 clear_bit(__AT_RESETTING, &adapter->flags);
85                                 return -EINVAL;
86                         }
87                 } else if (ecmd->advertising&ADVERTISE_1000_HALF) {
88                         clear_bit(__AT_RESETTING, &adapter->flags);
89                         return -EINVAL;
90                 } else {
91                         hw->autoneg_advertised =
92                                 ecmd->advertising & AT_ADV_MASK;
93                 }
94                 ecmd->advertising = hw->autoneg_advertised |
95                                     ADVERTISED_TP | ADVERTISED_Autoneg;
96
97                 adv4 = hw->mii_autoneg_adv_reg & ~MII_AR_SPEED_MASK;
98                 adv9 = hw->mii_1000t_ctrl_reg & ~MII_AT001_CR_1000T_SPEED_MASK;
99                 if (hw->autoneg_advertised & ADVERTISE_10_HALF)
100                         adv4 |= MII_AR_10T_HD_CAPS;
101                 if (hw->autoneg_advertised & ADVERTISE_10_FULL)
102                         adv4 |= MII_AR_10T_FD_CAPS;
103                 if (hw->autoneg_advertised & ADVERTISE_100_HALF)
104                         adv4 |= MII_AR_100TX_HD_CAPS;
105                 if (hw->autoneg_advertised & ADVERTISE_100_FULL)
106                         adv4 |= MII_AR_100TX_FD_CAPS;
107                 if (hw->autoneg_advertised & ADVERTISE_1000_FULL)
108                         adv9 |= MII_AT001_CR_1000T_FD_CAPS;
109
110                 if (adv4 != hw->mii_autoneg_adv_reg ||
111                                 adv9 != hw->mii_1000t_ctrl_reg) {
112                         hw->mii_autoneg_adv_reg = adv4;
113                         hw->mii_1000t_ctrl_reg = adv9;
114                         hw->re_autoneg = true;
115                 }
116
117         } else {
118                 clear_bit(__AT_RESETTING, &adapter->flags);
119                 return -EINVAL;
120         }
121
122         /* reset the link */
123
124         if (netif_running(adapter->netdev)) {
125                 atl1e_down(adapter);
126                 atl1e_up(adapter);
127         } else
128                 atl1e_reset_hw(&adapter->hw);
129
130         clear_bit(__AT_RESETTING, &adapter->flags);
131         return 0;
132 }
133
134 static u32 atl1e_get_msglevel(struct net_device *netdev)
135 {
136 #ifdef DBG
137         return 1;
138 #else
139         return 0;
140 #endif
141 }
142
143 static void atl1e_set_msglevel(struct net_device *netdev, u32 data)
144 {
145 }
146
147 static int atl1e_get_regs_len(struct net_device *netdev)
148 {
149         return AT_REGS_LEN * sizeof(u32);
150 }
151
152 static void atl1e_get_regs(struct net_device *netdev,
153                            struct ethtool_regs *regs, void *p)
154 {
155         struct atl1e_adapter *adapter = netdev_priv(netdev);
156         struct atl1e_hw *hw = &adapter->hw;
157         u32 *regs_buff = p;
158         u16 phy_data;
159
160         memset(p, 0, AT_REGS_LEN * sizeof(u32));
161
162         regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
163
164         regs_buff[0]  = AT_READ_REG(hw, REG_VPD_CAP);
165         regs_buff[1]  = AT_READ_REG(hw, REG_SPI_FLASH_CTRL);
166         regs_buff[2]  = AT_READ_REG(hw, REG_SPI_FLASH_CONFIG);
167         regs_buff[3]  = AT_READ_REG(hw, REG_TWSI_CTRL);
168         regs_buff[4]  = AT_READ_REG(hw, REG_PCIE_DEV_MISC_CTRL);
169         regs_buff[5]  = AT_READ_REG(hw, REG_MASTER_CTRL);
170         regs_buff[6]  = AT_READ_REG(hw, REG_MANUAL_TIMER_INIT);
171         regs_buff[7]  = AT_READ_REG(hw, REG_IRQ_MODU_TIMER_INIT);
172         regs_buff[8]  = AT_READ_REG(hw, REG_GPHY_CTRL);
173         regs_buff[9]  = AT_READ_REG(hw, REG_CMBDISDMA_TIMER);
174         regs_buff[10] = AT_READ_REG(hw, REG_IDLE_STATUS);
175         regs_buff[11] = AT_READ_REG(hw, REG_MDIO_CTRL);
176         regs_buff[12] = AT_READ_REG(hw, REG_SERDES_LOCK);
177         regs_buff[13] = AT_READ_REG(hw, REG_MAC_CTRL);
178         regs_buff[14] = AT_READ_REG(hw, REG_MAC_IPG_IFG);
179         regs_buff[15] = AT_READ_REG(hw, REG_MAC_STA_ADDR);
180         regs_buff[16] = AT_READ_REG(hw, REG_MAC_STA_ADDR+4);
181         regs_buff[17] = AT_READ_REG(hw, REG_RX_HASH_TABLE);
182         regs_buff[18] = AT_READ_REG(hw, REG_RX_HASH_TABLE+4);
183         regs_buff[19] = AT_READ_REG(hw, REG_MAC_HALF_DUPLX_CTRL);
184         regs_buff[20] = AT_READ_REG(hw, REG_MTU);
185         regs_buff[21] = AT_READ_REG(hw, REG_WOL_CTRL);
186         regs_buff[22] = AT_READ_REG(hw, REG_SRAM_TRD_ADDR);
187         regs_buff[23] = AT_READ_REG(hw, REG_SRAM_TRD_LEN);
188         regs_buff[24] = AT_READ_REG(hw, REG_SRAM_RXF_ADDR);
189         regs_buff[25] = AT_READ_REG(hw, REG_SRAM_RXF_LEN);
190         regs_buff[26] = AT_READ_REG(hw, REG_SRAM_TXF_ADDR);
191         regs_buff[27] = AT_READ_REG(hw, REG_SRAM_TXF_LEN);
192         regs_buff[28] = AT_READ_REG(hw, REG_SRAM_TCPH_ADDR);
193         regs_buff[29] = AT_READ_REG(hw, REG_SRAM_PKTH_ADDR);
194
195         atl1e_read_phy_reg(hw, MII_BMCR, &phy_data);
196         regs_buff[73] = (u32)phy_data;
197         atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
198         regs_buff[74] = (u32)phy_data;
199 }
200
201 static int atl1e_get_eeprom_len(struct net_device *netdev)
202 {
203         struct atl1e_adapter *adapter = netdev_priv(netdev);
204
205         if (!atl1e_check_eeprom_exist(&adapter->hw))
206                 return AT_EEPROM_LEN;
207         else
208                 return 0;
209 }
210
211 static int atl1e_get_eeprom(struct net_device *netdev,
212                 struct ethtool_eeprom *eeprom, u8 *bytes)
213 {
214         struct atl1e_adapter *adapter = netdev_priv(netdev);
215         struct atl1e_hw *hw = &adapter->hw;
216         u32 *eeprom_buff;
217         int first_dword, last_dword;
218         int ret_val = 0;
219         int i;
220
221         if (eeprom->len == 0)
222                 return -EINVAL;
223
224         if (atl1e_check_eeprom_exist(hw)) /* not exist */
225                 return -EINVAL;
226
227         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
228
229         first_dword = eeprom->offset >> 2;
230         last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
231
232         eeprom_buff = kmalloc(sizeof(u32) *
233                         (last_dword - first_dword + 1), GFP_KERNEL);
234         if (eeprom_buff == NULL)
235                 return -ENOMEM;
236
237         for (i = first_dword; i < last_dword; i++) {
238                 if (!atl1e_read_eeprom(hw, i * 4, &(eeprom_buff[i-first_dword]))) {
239                         kfree(eeprom_buff);
240                         return -EIO;
241                 }
242         }
243
244         memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 3),
245                         eeprom->len);
246         kfree(eeprom_buff);
247
248         return ret_val;
249 }
250
251 static int atl1e_set_eeprom(struct net_device *netdev,
252                             struct ethtool_eeprom *eeprom, u8 *bytes)
253 {
254         struct atl1e_adapter *adapter = netdev_priv(netdev);
255         struct atl1e_hw *hw = &adapter->hw;
256         u32 *eeprom_buff;
257         u32 *ptr;
258         int first_dword, last_dword;
259         int ret_val = 0;
260         int i;
261
262         if (eeprom->len == 0)
263                 return -EOPNOTSUPP;
264
265         if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
266                 return -EINVAL;
267
268         first_dword = eeprom->offset >> 2;
269         last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
270         eeprom_buff = kmalloc(AT_EEPROM_LEN, GFP_KERNEL);
271         if (eeprom_buff == NULL)
272                 return -ENOMEM;
273
274         ptr = (u32 *)eeprom_buff;
275
276         if (eeprom->offset & 3) {
277                 /* need read/modify/write of first changed EEPROM word */
278                 /* only the second byte of the word is being modified */
279                 if (!atl1e_read_eeprom(hw, first_dword * 4, &(eeprom_buff[0]))) {
280                         ret_val = -EIO;
281                         goto out;
282                 }
283                 ptr++;
284         }
285         if (((eeprom->offset + eeprom->len) & 3)) {
286                 /* need read/modify/write of last changed EEPROM word */
287                 /* only the first byte of the word is being modified */
288
289                 if (!atl1e_read_eeprom(hw, last_dword * 4,
290                                 &(eeprom_buff[last_dword - first_dword]))) {
291                         ret_val = -EIO;
292                         goto out;
293                 }
294         }
295
296         /* Device's eeprom is always little-endian, word addressable */
297         memcpy(ptr, bytes, eeprom->len);
298
299         for (i = 0; i < last_dword - first_dword + 1; i++) {
300                 if (!atl1e_write_eeprom(hw, ((first_dword + i) * 4),
301                                   eeprom_buff[i])) {
302                         ret_val = -EIO;
303                         goto out;
304                 }
305         }
306 out:
307         kfree(eeprom_buff);
308         return ret_val;
309 }
310
311 static void atl1e_get_drvinfo(struct net_device *netdev,
312                 struct ethtool_drvinfo *drvinfo)
313 {
314         struct atl1e_adapter *adapter = netdev_priv(netdev);
315
316         strncpy(drvinfo->driver,  atl1e_driver_name, 32);
317         strncpy(drvinfo->version, atl1e_driver_version, 32);
318         strncpy(drvinfo->fw_version, "L1e", 32);
319         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
320         drvinfo->n_stats = 0;
321         drvinfo->testinfo_len = 0;
322         drvinfo->regdump_len = atl1e_get_regs_len(netdev);
323         drvinfo->eedump_len = atl1e_get_eeprom_len(netdev);
324 }
325
326 static void atl1e_get_wol(struct net_device *netdev,
327                           struct ethtool_wolinfo *wol)
328 {
329         struct atl1e_adapter *adapter = netdev_priv(netdev);
330
331         wol->supported = WAKE_MAGIC | WAKE_PHY;
332         wol->wolopts = 0;
333
334         if (adapter->wol & AT_WUFC_EX)
335                 wol->wolopts |= WAKE_UCAST;
336         if (adapter->wol & AT_WUFC_MC)
337                 wol->wolopts |= WAKE_MCAST;
338         if (adapter->wol & AT_WUFC_BC)
339                 wol->wolopts |= WAKE_BCAST;
340         if (adapter->wol & AT_WUFC_MAG)
341                 wol->wolopts |= WAKE_MAGIC;
342         if (adapter->wol & AT_WUFC_LNKC)
343                 wol->wolopts |= WAKE_PHY;
344
345         return;
346 }
347
348 static int atl1e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
349 {
350         struct atl1e_adapter *adapter = netdev_priv(netdev);
351
352         if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE |
353                             WAKE_UCAST | WAKE_MCAST | WAKE_BCAST))
354                 return -EOPNOTSUPP;
355         /* these settings will always override what we currently have */
356         adapter->wol = 0;
357
358         if (wol->wolopts & WAKE_MAGIC)
359                 adapter->wol |= AT_WUFC_MAG;
360         if (wol->wolopts & WAKE_PHY)
361                 adapter->wol |= AT_WUFC_LNKC;
362
363         device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
364
365         return 0;
366 }
367
368 static int atl1e_nway_reset(struct net_device *netdev)
369 {
370         struct atl1e_adapter *adapter = netdev_priv(netdev);
371         if (netif_running(netdev))
372                 atl1e_reinit_locked(adapter);
373         return 0;
374 }
375
376 static const struct ethtool_ops atl1e_ethtool_ops = {
377         .get_settings           = atl1e_get_settings,
378         .set_settings           = atl1e_set_settings,
379         .get_drvinfo            = atl1e_get_drvinfo,
380         .get_regs_len           = atl1e_get_regs_len,
381         .get_regs               = atl1e_get_regs,
382         .get_wol                = atl1e_get_wol,
383         .set_wol                = atl1e_set_wol,
384         .get_msglevel           = atl1e_get_msglevel,
385         .set_msglevel           = atl1e_set_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         .set_tx_csum            = ethtool_op_set_tx_hw_csum,
392         .set_sg                 = ethtool_op_set_sg,
393         .set_tso                = ethtool_op_set_tso,
394 };
395
396 void atl1e_set_ethtool_ops(struct net_device *netdev)
397 {
398         SET_ETHTOOL_OPS(netdev, &atl1e_ethtool_ops);
399 }