ethtool.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /* The order of these strings must match the order of the fields in
  47. * struct alx_hw_stats
  48. * See hw.h
  49. */
  50. static const char alx_gstrings_stats[][ETH_GSTRING_LEN] = {
  51. "rx_packets",
  52. "rx_bcast_packets",
  53. "rx_mcast_packets",
  54. "rx_pause_packets",
  55. "rx_ctrl_packets",
  56. "rx_fcs_errors",
  57. "rx_length_errors",
  58. "rx_bytes",
  59. "rx_runt_packets",
  60. "rx_fragments",
  61. "rx_64B_or_less_packets",
  62. "rx_65B_to_127B_packets",
  63. "rx_128B_to_255B_packets",
  64. "rx_256B_to_511B_packets",
  65. "rx_512B_to_1023B_packets",
  66. "rx_1024B_to_1518B_packets",
  67. "rx_1519B_to_mtu_packets",
  68. "rx_oversize_packets",
  69. "rx_rxf_ov_drop_packets",
  70. "rx_rrd_ov_drop_packets",
  71. "rx_align_errors",
  72. "rx_bcast_bytes",
  73. "rx_mcast_bytes",
  74. "rx_address_errors",
  75. "tx_packets",
  76. "tx_bcast_packets",
  77. "tx_mcast_packets",
  78. "tx_pause_packets",
  79. "tx_exc_defer_packets",
  80. "tx_ctrl_packets",
  81. "tx_defer_packets",
  82. "tx_bytes",
  83. "tx_64B_or_less_packets",
  84. "tx_65B_to_127B_packets",
  85. "tx_128B_to_255B_packets",
  86. "tx_256B_to_511B_packets",
  87. "tx_512B_to_1023B_packets",
  88. "tx_1024B_to_1518B_packets",
  89. "tx_1519B_to_mtu_packets",
  90. "tx_single_collision",
  91. "tx_multiple_collisions",
  92. "tx_late_collision",
  93. "tx_abort_collision",
  94. "tx_underrun",
  95. "tx_trd_eop",
  96. "tx_length_errors",
  97. "tx_trunc_packets",
  98. "tx_bcast_bytes",
  99. "tx_mcast_bytes",
  100. "tx_update",
  101. };
  102. #define ALX_NUM_STATS ARRAY_SIZE(alx_gstrings_stats)
  103. static u32 alx_get_supported_speeds(struct alx_hw *hw)
  104. {
  105. u32 supported = SUPPORTED_10baseT_Half |
  106. SUPPORTED_10baseT_Full |
  107. SUPPORTED_100baseT_Half |
  108. SUPPORTED_100baseT_Full;
  109. if (alx_hw_giga(hw))
  110. supported |= SUPPORTED_1000baseT_Full;
  111. BUILD_BUG_ON(SUPPORTED_10baseT_Half != ADVERTISED_10baseT_Half);
  112. BUILD_BUG_ON(SUPPORTED_10baseT_Full != ADVERTISED_10baseT_Full);
  113. BUILD_BUG_ON(SUPPORTED_100baseT_Half != ADVERTISED_100baseT_Half);
  114. BUILD_BUG_ON(SUPPORTED_100baseT_Full != ADVERTISED_100baseT_Full);
  115. BUILD_BUG_ON(SUPPORTED_1000baseT_Full != ADVERTISED_1000baseT_Full);
  116. return supported;
  117. }
  118. static int alx_get_link_ksettings(struct net_device *netdev,
  119. struct ethtool_link_ksettings *cmd)
  120. {
  121. struct alx_priv *alx = netdev_priv(netdev);
  122. struct alx_hw *hw = &alx->hw;
  123. u32 supported, advertising;
  124. supported = SUPPORTED_Autoneg |
  125. SUPPORTED_TP |
  126. SUPPORTED_Pause |
  127. SUPPORTED_Asym_Pause;
  128. if (alx_hw_giga(hw))
  129. supported |= SUPPORTED_1000baseT_Full;
  130. supported |= alx_get_supported_speeds(hw);
  131. advertising = ADVERTISED_TP;
  132. if (hw->adv_cfg & ADVERTISED_Autoneg)
  133. advertising |= hw->adv_cfg;
  134. cmd->base.port = PORT_TP;
  135. cmd->base.phy_address = 0;
  136. if (hw->adv_cfg & ADVERTISED_Autoneg)
  137. cmd->base.autoneg = AUTONEG_ENABLE;
  138. else
  139. cmd->base.autoneg = AUTONEG_DISABLE;
  140. if (hw->flowctrl & ALX_FC_ANEG && hw->adv_cfg & ADVERTISED_Autoneg) {
  141. if (hw->flowctrl & ALX_FC_RX) {
  142. advertising |= ADVERTISED_Pause;
  143. if (!(hw->flowctrl & ALX_FC_TX))
  144. advertising |= ADVERTISED_Asym_Pause;
  145. } else if (hw->flowctrl & ALX_FC_TX) {
  146. advertising |= ADVERTISED_Asym_Pause;
  147. }
  148. }
  149. cmd->base.speed = hw->link_speed;
  150. cmd->base.duplex = hw->duplex;
  151. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  152. supported);
  153. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  154. advertising);
  155. return 0;
  156. }
  157. static int alx_set_link_ksettings(struct net_device *netdev,
  158. const struct ethtool_link_ksettings *cmd)
  159. {
  160. struct alx_priv *alx = netdev_priv(netdev);
  161. struct alx_hw *hw = &alx->hw;
  162. u32 adv_cfg;
  163. u32 advertising;
  164. ASSERT_RTNL();
  165. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  166. cmd->link_modes.advertising);
  167. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  168. if (advertising & ~alx_get_supported_speeds(hw))
  169. return -EINVAL;
  170. adv_cfg = advertising | ADVERTISED_Autoneg;
  171. } else {
  172. adv_cfg = alx_speed_to_ethadv(cmd->base.speed,
  173. cmd->base.duplex);
  174. if (!adv_cfg || adv_cfg == ADVERTISED_1000baseT_Full)
  175. return -EINVAL;
  176. }
  177. hw->adv_cfg = adv_cfg;
  178. return alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);
  179. }
  180. static void alx_get_pauseparam(struct net_device *netdev,
  181. struct ethtool_pauseparam *pause)
  182. {
  183. struct alx_priv *alx = netdev_priv(netdev);
  184. struct alx_hw *hw = &alx->hw;
  185. pause->autoneg = !!(hw->flowctrl & ALX_FC_ANEG &&
  186. hw->adv_cfg & ADVERTISED_Autoneg);
  187. pause->tx_pause = !!(hw->flowctrl & ALX_FC_TX);
  188. pause->rx_pause = !!(hw->flowctrl & ALX_FC_RX);
  189. }
  190. static int alx_set_pauseparam(struct net_device *netdev,
  191. struct ethtool_pauseparam *pause)
  192. {
  193. struct alx_priv *alx = netdev_priv(netdev);
  194. struct alx_hw *hw = &alx->hw;
  195. int err = 0;
  196. bool reconfig_phy = false;
  197. u8 fc = 0;
  198. if (pause->tx_pause)
  199. fc |= ALX_FC_TX;
  200. if (pause->rx_pause)
  201. fc |= ALX_FC_RX;
  202. if (pause->autoneg)
  203. fc |= ALX_FC_ANEG;
  204. ASSERT_RTNL();
  205. /* restart auto-neg for auto-mode */
  206. if (hw->adv_cfg & ADVERTISED_Autoneg) {
  207. if (!((fc ^ hw->flowctrl) & ALX_FC_ANEG))
  208. reconfig_phy = true;
  209. if (fc & hw->flowctrl & ALX_FC_ANEG &&
  210. (fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  211. reconfig_phy = true;
  212. }
  213. if (reconfig_phy) {
  214. err = alx_setup_speed_duplex(hw, hw->adv_cfg, fc);
  215. if (err)
  216. return err;
  217. }
  218. /* flow control on mac */
  219. if ((fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  220. alx_cfg_mac_flowcontrol(hw, fc);
  221. hw->flowctrl = fc;
  222. return 0;
  223. }
  224. static u32 alx_get_msglevel(struct net_device *netdev)
  225. {
  226. struct alx_priv *alx = netdev_priv(netdev);
  227. return alx->msg_enable;
  228. }
  229. static void alx_set_msglevel(struct net_device *netdev, u32 data)
  230. {
  231. struct alx_priv *alx = netdev_priv(netdev);
  232. alx->msg_enable = data;
  233. }
  234. static void alx_get_ethtool_stats(struct net_device *netdev,
  235. struct ethtool_stats *estats, u64 *data)
  236. {
  237. struct alx_priv *alx = netdev_priv(netdev);
  238. struct alx_hw *hw = &alx->hw;
  239. spin_lock(&alx->stats_lock);
  240. alx_update_hw_stats(hw);
  241. BUILD_BUG_ON(sizeof(hw->stats) - offsetof(struct alx_hw_stats, rx_ok) <
  242. ALX_NUM_STATS * sizeof(u64));
  243. memcpy(data, &hw->stats.rx_ok, ALX_NUM_STATS * sizeof(u64));
  244. spin_unlock(&alx->stats_lock);
  245. }
  246. static void alx_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
  247. {
  248. switch (stringset) {
  249. case ETH_SS_STATS:
  250. memcpy(buf, &alx_gstrings_stats, sizeof(alx_gstrings_stats));
  251. break;
  252. default:
  253. WARN_ON(1);
  254. break;
  255. }
  256. }
  257. static int alx_get_sset_count(struct net_device *netdev, int sset)
  258. {
  259. switch (sset) {
  260. case ETH_SS_STATS:
  261. return ALX_NUM_STATS;
  262. default:
  263. return -EINVAL;
  264. }
  265. }
  266. const struct ethtool_ops alx_ethtool_ops = {
  267. .get_pauseparam = alx_get_pauseparam,
  268. .set_pauseparam = alx_set_pauseparam,
  269. .get_msglevel = alx_get_msglevel,
  270. .set_msglevel = alx_set_msglevel,
  271. .get_link = ethtool_op_get_link,
  272. .get_strings = alx_get_strings,
  273. .get_sset_count = alx_get_sset_count,
  274. .get_ethtool_stats = alx_get_ethtool_stats,
  275. .get_link_ksettings = alx_get_link_ksettings,
  276. .set_link_ksettings = alx_set_link_ksettings,
  277. };