ethtool.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  119. {
  120. struct alx_priv *alx = netdev_priv(netdev);
  121. struct alx_hw *hw = &alx->hw;
  122. ecmd->supported = SUPPORTED_Autoneg |
  123. SUPPORTED_TP |
  124. SUPPORTED_Pause |
  125. SUPPORTED_Asym_Pause;
  126. if (alx_hw_giga(hw))
  127. ecmd->supported |= SUPPORTED_1000baseT_Full;
  128. ecmd->supported |= alx_get_supported_speeds(hw);
  129. ecmd->advertising = ADVERTISED_TP;
  130. if (hw->adv_cfg & ADVERTISED_Autoneg)
  131. ecmd->advertising |= hw->adv_cfg;
  132. ecmd->port = PORT_TP;
  133. ecmd->phy_address = 0;
  134. if (hw->adv_cfg & ADVERTISED_Autoneg)
  135. ecmd->autoneg = AUTONEG_ENABLE;
  136. else
  137. ecmd->autoneg = AUTONEG_DISABLE;
  138. ecmd->transceiver = XCVR_INTERNAL;
  139. if (hw->flowctrl & ALX_FC_ANEG && hw->adv_cfg & ADVERTISED_Autoneg) {
  140. if (hw->flowctrl & ALX_FC_RX) {
  141. ecmd->advertising |= ADVERTISED_Pause;
  142. if (!(hw->flowctrl & ALX_FC_TX))
  143. ecmd->advertising |= ADVERTISED_Asym_Pause;
  144. } else if (hw->flowctrl & ALX_FC_TX) {
  145. ecmd->advertising |= ADVERTISED_Asym_Pause;
  146. }
  147. }
  148. ethtool_cmd_speed_set(ecmd, hw->link_speed);
  149. ecmd->duplex = hw->duplex;
  150. return 0;
  151. }
  152. static int alx_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  153. {
  154. struct alx_priv *alx = netdev_priv(netdev);
  155. struct alx_hw *hw = &alx->hw;
  156. u32 adv_cfg;
  157. ASSERT_RTNL();
  158. if (ecmd->autoneg == AUTONEG_ENABLE) {
  159. if (ecmd->advertising & ~alx_get_supported_speeds(hw))
  160. return -EINVAL;
  161. adv_cfg = ecmd->advertising | ADVERTISED_Autoneg;
  162. } else {
  163. adv_cfg = alx_speed_to_ethadv(ethtool_cmd_speed(ecmd),
  164. ecmd->duplex);
  165. if (!adv_cfg || adv_cfg == ADVERTISED_1000baseT_Full)
  166. return -EINVAL;
  167. }
  168. hw->adv_cfg = adv_cfg;
  169. return alx_setup_speed_duplex(hw, adv_cfg, hw->flowctrl);
  170. }
  171. static void alx_get_pauseparam(struct net_device *netdev,
  172. struct ethtool_pauseparam *pause)
  173. {
  174. struct alx_priv *alx = netdev_priv(netdev);
  175. struct alx_hw *hw = &alx->hw;
  176. pause->autoneg = !!(hw->flowctrl & ALX_FC_ANEG &&
  177. hw->adv_cfg & ADVERTISED_Autoneg);
  178. pause->tx_pause = !!(hw->flowctrl & ALX_FC_TX);
  179. pause->rx_pause = !!(hw->flowctrl & ALX_FC_RX);
  180. }
  181. static int alx_set_pauseparam(struct net_device *netdev,
  182. struct ethtool_pauseparam *pause)
  183. {
  184. struct alx_priv *alx = netdev_priv(netdev);
  185. struct alx_hw *hw = &alx->hw;
  186. int err = 0;
  187. bool reconfig_phy = false;
  188. u8 fc = 0;
  189. if (pause->tx_pause)
  190. fc |= ALX_FC_TX;
  191. if (pause->rx_pause)
  192. fc |= ALX_FC_RX;
  193. if (pause->autoneg)
  194. fc |= ALX_FC_ANEG;
  195. ASSERT_RTNL();
  196. /* restart auto-neg for auto-mode */
  197. if (hw->adv_cfg & ADVERTISED_Autoneg) {
  198. if (!((fc ^ hw->flowctrl) & ALX_FC_ANEG))
  199. reconfig_phy = true;
  200. if (fc & hw->flowctrl & ALX_FC_ANEG &&
  201. (fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  202. reconfig_phy = true;
  203. }
  204. if (reconfig_phy) {
  205. err = alx_setup_speed_duplex(hw, hw->adv_cfg, fc);
  206. if (err)
  207. return err;
  208. }
  209. /* flow control on mac */
  210. if ((fc ^ hw->flowctrl) & (ALX_FC_RX | ALX_FC_TX))
  211. alx_cfg_mac_flowcontrol(hw, fc);
  212. hw->flowctrl = fc;
  213. return 0;
  214. }
  215. static u32 alx_get_msglevel(struct net_device *netdev)
  216. {
  217. struct alx_priv *alx = netdev_priv(netdev);
  218. return alx->msg_enable;
  219. }
  220. static void alx_set_msglevel(struct net_device *netdev, u32 data)
  221. {
  222. struct alx_priv *alx = netdev_priv(netdev);
  223. alx->msg_enable = data;
  224. }
  225. static void alx_get_ethtool_stats(struct net_device *netdev,
  226. struct ethtool_stats *estats, u64 *data)
  227. {
  228. struct alx_priv *alx = netdev_priv(netdev);
  229. struct alx_hw *hw = &alx->hw;
  230. spin_lock(&alx->stats_lock);
  231. alx_update_hw_stats(hw);
  232. BUILD_BUG_ON(sizeof(hw->stats) - offsetof(struct alx_hw_stats, rx_ok) <
  233. ALX_NUM_STATS * sizeof(u64));
  234. memcpy(data, &hw->stats.rx_ok, ALX_NUM_STATS * sizeof(u64));
  235. spin_unlock(&alx->stats_lock);
  236. }
  237. static void alx_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
  238. {
  239. switch (stringset) {
  240. case ETH_SS_STATS:
  241. memcpy(buf, &alx_gstrings_stats, sizeof(alx_gstrings_stats));
  242. break;
  243. default:
  244. WARN_ON(1);
  245. break;
  246. }
  247. }
  248. static int alx_get_sset_count(struct net_device *netdev, int sset)
  249. {
  250. switch (sset) {
  251. case ETH_SS_STATS:
  252. return ALX_NUM_STATS;
  253. default:
  254. return -EINVAL;
  255. }
  256. }
  257. const struct ethtool_ops alx_ethtool_ops = {
  258. .get_settings = alx_get_settings,
  259. .set_settings = alx_set_settings,
  260. .get_pauseparam = alx_get_pauseparam,
  261. .set_pauseparam = alx_set_pauseparam,
  262. .get_msglevel = alx_get_msglevel,
  263. .set_msglevel = alx_set_msglevel,
  264. .get_link = ethtool_op_get_link,
  265. .get_strings = alx_get_strings,
  266. .get_sset_count = alx_get_sset_count,
  267. .get_ethtool_stats = alx_get_ethtool_stats,
  268. };