ethtool.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2013 Johannes Berg <johannes@sipsolutions.net>
  3. *
  4. * This file is free software: you may copy, redistribute and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * This file incorporates work covered by the following copyright and
  18. * permission notice:
  19. *
  20. * Copyright (c) 2012 Qualcomm Atheros, Inc.
  21. *
  22. * Permission to use, copy, modify, and/or distribute this software for any
  23. * purpose with or without fee is hereby granted, provided that the above
  24. * copyright notice and this permission notice appear in all copies.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  27. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  29. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  30. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  31. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  32. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33. */
  34. #include <linux/pci.h>
  35. #include <linux/ip.h>
  36. #include <linux/tcp.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mdio.h>
  41. #include <linux/interrupt.h>
  42. #include <asm/byteorder.h>
  43. #include "alx.h"
  44. #include "reg.h"
  45. #include "hw.h"
  46. static int alx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  47. {
  48. struct alx_priv *alx = netdev_priv(netdev);
  49. struct alx_hw *hw = &alx->hw;
  50. ecmd->supported = SUPPORTED_10baseT_Half |
  51. SUPPORTED_10baseT_Full |
  52. SUPPORTED_100baseT_Half |
  53. SUPPORTED_100baseT_Full |
  54. SUPPORTED_Autoneg |
  55. SUPPORTED_TP |
  56. SUPPORTED_Pause;
  57. if (alx_hw_giga(hw))
  58. ecmd->supported |= SUPPORTED_1000baseT_Full;
  59. ecmd->advertising = ADVERTISED_TP;
  60. if (hw->adv_cfg & ADVERTISED_Autoneg)
  61. ecmd->advertising |= hw->adv_cfg;
  62. ecmd->port = PORT_TP;
  63. ecmd->phy_address = 0;
  64. if (hw->adv_cfg & ADVERTISED_Autoneg)
  65. ecmd->autoneg = AUTONEG_ENABLE;
  66. else
  67. ecmd->autoneg = AUTONEG_DISABLE;
  68. ecmd->transceiver = XCVR_INTERNAL;
  69. if (hw->flowctrl & ALX_FC_ANEG && hw->adv_cfg & ADVERTISED_Autoneg) {
  70. if (hw->flowctrl & ALX_FC_RX) {
  71. ecmd->advertising |= ADVERTISED_Pause;
  72. if (!(hw->flowctrl & ALX_FC_TX))
  73. ecmd->advertising |= ADVERTISED_Asym_Pause;
  74. } else if (hw->flowctrl & ALX_FC_TX) {
  75. ecmd->advertising |= ADVERTISED_Asym_Pause;
  76. }
  77. }
  78. if (hw->link_speed != SPEED_UNKNOWN) {
  79. ethtool_cmd_speed_set(ecmd,
  80. hw->link_speed - hw->link_speed % 10);
  81. ecmd->duplex = hw->link_speed % 10;
  82. } else {
  83. ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
  84. ecmd->duplex = DUPLEX_UNKNOWN;
  85. }
  86. return 0;
  87. }
  88. static int alx_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  89. {
  90. struct alx_priv *alx = netdev_priv(netdev);
  91. struct alx_hw *hw = &alx->hw;
  92. u32 adv_cfg;
  93. ASSERT_RTNL();
  94. if (ecmd->autoneg == AUTONEG_ENABLE) {
  95. if (ecmd->advertising & ADVERTISED_1000baseT_Half)
  96. return -EINVAL;
  97. adv_cfg = ecmd->advertising | ADVERTISED_Autoneg;
  98. } else {
  99. int speed = ethtool_cmd_speed(ecmd);
  100. switch (speed + ecmd->duplex) {
  101. case SPEED_10 + DUPLEX_HALF:
  102. adv_cfg = ADVERTISED_10baseT_Half;
  103. break;
  104. case SPEED_10 + DUPLEX_FULL:
  105. adv_cfg = ADVERTISED_10baseT_Full;
  106. break;
  107. case SPEED_100 + DUPLEX_HALF:
  108. adv_cfg = ADVERTISED_100baseT_Half;
  109. break;
  110. case SPEED_100 + DUPLEX_FULL:
  111. adv_cfg = ADVERTISED_100baseT_Full;
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. }
  117. hw->adv_cfg = adv_cfg;
  118. return alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);
  119. }
  120. static void alx_get_pauseparam(struct net_device *netdev,
  121. struct ethtool_pauseparam *pause)
  122. {
  123. struct alx_priv *alx = netdev_priv(netdev);
  124. struct alx_hw *hw = &alx->hw;
  125. if (hw->flowctrl & ALX_FC_ANEG &&
  126. hw->adv_cfg & ADVERTISED_Autoneg)
  127. pause->autoneg = AUTONEG_ENABLE;
  128. else
  129. pause->autoneg = AUTONEG_DISABLE;
  130. if (hw->flowctrl & ALX_FC_TX)
  131. pause->tx_pause = 1;
  132. else
  133. pause->tx_pause = 0;
  134. if (hw->flowctrl & ALX_FC_RX)
  135. pause->rx_pause = 1;
  136. else
  137. pause->rx_pause = 0;
  138. }
  139. static int alx_set_pauseparam(struct net_device *netdev,
  140. struct ethtool_pauseparam *pause)
  141. {
  142. struct alx_priv *alx = netdev_priv(netdev);
  143. struct alx_hw *hw = &alx->hw;
  144. int err = 0;
  145. bool reconfig_phy = false;
  146. u8 fc = 0;
  147. if (pause->tx_pause)
  148. fc |= ALX_FC_TX;
  149. if (pause->rx_pause)
  150. fc |= ALX_FC_RX;
  151. if (pause->autoneg)
  152. fc |= ALX_FC_ANEG;
  153. ASSERT_RTNL();
  154. /* restart auto-neg for auto-mode */
  155. if (hw->adv_cfg & ADVERTISED_Autoneg) {
  156. if (!((fc ^ hw->flowctrl) & ALX_FC_ANEG))
  157. reconfig_phy = true;
  158. if (fc & hw->flowctrl & ALX_FC_ANEG &&
  159. (fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  160. reconfig_phy = true;
  161. }
  162. if (reconfig_phy) {
  163. err = alx_setup_speed_duplex(hw, hw->adv_cfg, fc);
  164. if (err)
  165. return err;
  166. }
  167. /* flow control on mac */
  168. if ((fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  169. alx_cfg_mac_flowcontrol(hw, fc);
  170. hw->flowctrl = fc;
  171. return 0;
  172. }
  173. static u32 alx_get_msglevel(struct net_device *netdev)
  174. {
  175. struct alx_priv *alx = netdev_priv(netdev);
  176. return alx->msg_enable;
  177. }
  178. static void alx_set_msglevel(struct net_device *netdev, u32 data)
  179. {
  180. struct alx_priv *alx = netdev_priv(netdev);
  181. alx->msg_enable = data;
  182. }
  183. static void alx_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  184. {
  185. struct alx_priv *alx = netdev_priv(netdev);
  186. struct alx_hw *hw = &alx->hw;
  187. wol->supported = WAKE_MAGIC | WAKE_PHY;
  188. wol->wolopts = 0;
  189. if (hw->sleep_ctrl & ALX_SLEEP_WOL_MAGIC)
  190. wol->wolopts |= WAKE_MAGIC;
  191. if (hw->sleep_ctrl & ALX_SLEEP_WOL_PHY)
  192. wol->wolopts |= WAKE_PHY;
  193. }
  194. static int alx_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  195. {
  196. struct alx_priv *alx = netdev_priv(netdev);
  197. struct alx_hw *hw = &alx->hw;
  198. if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE |
  199. WAKE_UCAST | WAKE_BCAST | WAKE_MCAST))
  200. return -EOPNOTSUPP;
  201. hw->sleep_ctrl = 0;
  202. if (wol->wolopts & WAKE_MAGIC)
  203. hw->sleep_ctrl |= ALX_SLEEP_WOL_MAGIC;
  204. if (wol->wolopts & WAKE_PHY)
  205. hw->sleep_ctrl |= ALX_SLEEP_WOL_PHY;
  206. device_set_wakeup_enable(&alx->hw.pdev->dev, hw->sleep_ctrl);
  207. return 0;
  208. }
  209. static void alx_get_drvinfo(struct net_device *netdev,
  210. struct ethtool_drvinfo *drvinfo)
  211. {
  212. struct alx_priv *alx = netdev_priv(netdev);
  213. strlcpy(drvinfo->driver, alx_drv_name, sizeof(drvinfo->driver));
  214. strlcpy(drvinfo->bus_info, pci_name(alx->hw.pdev),
  215. sizeof(drvinfo->bus_info));
  216. }
  217. const struct ethtool_ops alx_ethtool_ops = {
  218. .get_settings = alx_get_settings,
  219. .set_settings = alx_set_settings,
  220. .get_pauseparam = alx_get_pauseparam,
  221. .set_pauseparam = alx_set_pauseparam,
  222. .get_drvinfo = alx_get_drvinfo,
  223. .get_msglevel = alx_get_msglevel,
  224. .set_msglevel = alx_set_msglevel,
  225. .get_wol = alx_get_wol,
  226. .set_wol = alx_set_wol,
  227. .get_link = ethtool_op_get_link,
  228. };