atl1e_ethtool.c 11 KB

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