dpaa2-switch-ethtool.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DPAA2 Ethernet Switch ethtool support
  4. *
  5. * Copyright 2014-2016 Freescale Semiconductor Inc.
  6. * Copyright 2017-2018 NXP
  7. *
  8. */
  9. #include <linux/ethtool.h>
  10. #include "dpaa2-switch.h"
  11. static struct {
  12. enum dpsw_counter id;
  13. char name[ETH_GSTRING_LEN];
  14. } dpaa2_switch_ethtool_counters[] = {
  15. {DPSW_CNT_ING_FRAME, "[hw] rx frames"},
  16. {DPSW_CNT_ING_BYTE, "[hw] rx bytes"},
  17. {DPSW_CNT_ING_FLTR_FRAME, "[hw] rx filtered frames"},
  18. {DPSW_CNT_ING_FRAME_DISCARD, "[hw] rx discarded frames"},
  19. {DPSW_CNT_ING_BCAST_FRAME, "[hw] rx bcast frames"},
  20. {DPSW_CNT_ING_BCAST_BYTES, "[hw] rx bcast bytes"},
  21. {DPSW_CNT_ING_MCAST_FRAME, "[hw] rx mcast frames"},
  22. {DPSW_CNT_ING_MCAST_BYTE, "[hw] rx mcast bytes"},
  23. {DPSW_CNT_EGR_FRAME, "[hw] tx frames"},
  24. {DPSW_CNT_EGR_BYTE, "[hw] tx bytes"},
  25. {DPSW_CNT_EGR_FRAME_DISCARD, "[hw] tx discarded frames"},
  26. {DPSW_CNT_ING_NO_BUFF_DISCARD, "[hw] rx nobuffer discards"},
  27. };
  28. #define DPAA2_SWITCH_NUM_COUNTERS ARRAY_SIZE(dpaa2_switch_ethtool_counters)
  29. static void dpaa2_switch_get_drvinfo(struct net_device *netdev,
  30. struct ethtool_drvinfo *drvinfo)
  31. {
  32. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  33. u16 version_major, version_minor;
  34. int err;
  35. strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
  36. err = dpsw_get_api_version(port_priv->ethsw_data->mc_io, 0,
  37. &version_major,
  38. &version_minor);
  39. if (err)
  40. strscpy(drvinfo->fw_version, "N/A",
  41. sizeof(drvinfo->fw_version));
  42. else
  43. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  44. "%u.%u", version_major, version_minor);
  45. strscpy(drvinfo->bus_info, dev_name(netdev->dev.parent->parent),
  46. sizeof(drvinfo->bus_info));
  47. }
  48. static int
  49. dpaa2_switch_get_link_ksettings(struct net_device *netdev,
  50. struct ethtool_link_ksettings *link_ksettings)
  51. {
  52. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  53. struct dpsw_link_state state = {0};
  54. int err;
  55. mutex_lock(&port_priv->mac_lock);
  56. if (dpaa2_switch_port_is_type_phy(port_priv)) {
  57. err = phylink_ethtool_ksettings_get(port_priv->mac->phylink,
  58. link_ksettings);
  59. mutex_unlock(&port_priv->mac_lock);
  60. return err;
  61. }
  62. mutex_unlock(&port_priv->mac_lock);
  63. err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
  64. port_priv->ethsw_data->dpsw_handle,
  65. port_priv->idx,
  66. &state);
  67. if (err) {
  68. netdev_err(netdev, "ERROR %d getting link state\n", err);
  69. goto out;
  70. }
  71. /* At the moment, we have no way of interrogating the DPMAC
  72. * from the DPSW side or there may not exist a DPMAC at all.
  73. * Report only autoneg state, duplexity and speed.
  74. */
  75. if (state.options & DPSW_LINK_OPT_AUTONEG)
  76. link_ksettings->base.autoneg = AUTONEG_ENABLE;
  77. if (!(state.options & DPSW_LINK_OPT_HALF_DUPLEX))
  78. link_ksettings->base.duplex = DUPLEX_FULL;
  79. link_ksettings->base.speed = state.rate;
  80. out:
  81. return err;
  82. }
  83. static int
  84. dpaa2_switch_set_link_ksettings(struct net_device *netdev,
  85. const struct ethtool_link_ksettings *link_ksettings)
  86. {
  87. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  88. struct ethsw_core *ethsw = port_priv->ethsw_data;
  89. struct dpsw_link_cfg cfg = {0};
  90. bool if_running;
  91. int err = 0, ret;
  92. mutex_lock(&port_priv->mac_lock);
  93. if (dpaa2_switch_port_is_type_phy(port_priv)) {
  94. err = phylink_ethtool_ksettings_set(port_priv->mac->phylink,
  95. link_ksettings);
  96. mutex_unlock(&port_priv->mac_lock);
  97. return err;
  98. }
  99. mutex_unlock(&port_priv->mac_lock);
  100. /* Interface needs to be down to change link settings */
  101. if_running = netif_running(netdev);
  102. if (if_running) {
  103. err = dpsw_if_disable(ethsw->mc_io, 0,
  104. ethsw->dpsw_handle,
  105. port_priv->idx);
  106. if (err) {
  107. netdev_err(netdev, "dpsw_if_disable err %d\n", err);
  108. return err;
  109. }
  110. }
  111. cfg.rate = link_ksettings->base.speed;
  112. if (link_ksettings->base.autoneg == AUTONEG_ENABLE)
  113. cfg.options |= DPSW_LINK_OPT_AUTONEG;
  114. else
  115. cfg.options &= ~DPSW_LINK_OPT_AUTONEG;
  116. if (link_ksettings->base.duplex == DUPLEX_HALF)
  117. cfg.options |= DPSW_LINK_OPT_HALF_DUPLEX;
  118. else
  119. cfg.options &= ~DPSW_LINK_OPT_HALF_DUPLEX;
  120. err = dpsw_if_set_link_cfg(port_priv->ethsw_data->mc_io, 0,
  121. port_priv->ethsw_data->dpsw_handle,
  122. port_priv->idx,
  123. &cfg);
  124. if (if_running) {
  125. ret = dpsw_if_enable(ethsw->mc_io, 0,
  126. ethsw->dpsw_handle,
  127. port_priv->idx);
  128. if (ret) {
  129. netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
  130. return ret;
  131. }
  132. }
  133. return err;
  134. }
  135. static int
  136. dpaa2_switch_ethtool_get_sset_count(struct net_device *netdev, int sset)
  137. {
  138. switch (sset) {
  139. case ETH_SS_STATS:
  140. return DPAA2_SWITCH_NUM_COUNTERS + dpaa2_mac_get_sset_count();
  141. default:
  142. return -EOPNOTSUPP;
  143. }
  144. }
  145. static void dpaa2_switch_ethtool_get_strings(struct net_device *netdev,
  146. u32 stringset, u8 *data)
  147. {
  148. const char *str;
  149. int i;
  150. switch (stringset) {
  151. case ETH_SS_STATS:
  152. for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++) {
  153. str = dpaa2_switch_ethtool_counters[i].name;
  154. ethtool_puts(&data, str);
  155. }
  156. dpaa2_mac_get_strings(&data);
  157. break;
  158. }
  159. }
  160. static void dpaa2_switch_ethtool_get_stats(struct net_device *netdev,
  161. struct ethtool_stats *stats,
  162. u64 *data)
  163. {
  164. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  165. int i, err;
  166. for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++) {
  167. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  168. port_priv->ethsw_data->dpsw_handle,
  169. port_priv->idx,
  170. dpaa2_switch_ethtool_counters[i].id,
  171. &data[i]);
  172. if (err)
  173. netdev_err(netdev, "dpsw_if_get_counter[%s] err %d\n",
  174. dpaa2_switch_ethtool_counters[i].name, err);
  175. }
  176. mutex_lock(&port_priv->mac_lock);
  177. if (dpaa2_switch_port_has_mac(port_priv))
  178. dpaa2_mac_get_ethtool_stats(port_priv->mac, data + i);
  179. mutex_unlock(&port_priv->mac_lock);
  180. }
  181. const struct ethtool_ops dpaa2_switch_port_ethtool_ops = {
  182. .get_drvinfo = dpaa2_switch_get_drvinfo,
  183. .get_link = ethtool_op_get_link,
  184. .get_link_ksettings = dpaa2_switch_get_link_ksettings,
  185. .set_link_ksettings = dpaa2_switch_set_link_ksettings,
  186. .get_strings = dpaa2_switch_ethtool_get_strings,
  187. .get_ethtool_stats = dpaa2_switch_ethtool_get_stats,
  188. .get_sset_count = dpaa2_switch_ethtool_get_sset_count,
  189. };