dpaa2-switch-ethtool.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = 0;
  55. if (dpaa2_switch_port_is_type_phy(port_priv))
  56. return phylink_ethtool_ksettings_get(port_priv->mac->phylink,
  57. link_ksettings);
  58. err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
  59. port_priv->ethsw_data->dpsw_handle,
  60. port_priv->idx,
  61. &state);
  62. if (err) {
  63. netdev_err(netdev, "ERROR %d getting link state\n", err);
  64. goto out;
  65. }
  66. /* At the moment, we have no way of interrogating the DPMAC
  67. * from the DPSW side or there may not exist a DPMAC at all.
  68. * Report only autoneg state, duplexity and speed.
  69. */
  70. if (state.options & DPSW_LINK_OPT_AUTONEG)
  71. link_ksettings->base.autoneg = AUTONEG_ENABLE;
  72. if (!(state.options & DPSW_LINK_OPT_HALF_DUPLEX))
  73. link_ksettings->base.duplex = DUPLEX_FULL;
  74. link_ksettings->base.speed = state.rate;
  75. out:
  76. return err;
  77. }
  78. static int
  79. dpaa2_switch_set_link_ksettings(struct net_device *netdev,
  80. const struct ethtool_link_ksettings *link_ksettings)
  81. {
  82. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  83. struct ethsw_core *ethsw = port_priv->ethsw_data;
  84. struct dpsw_link_cfg cfg = {0};
  85. bool if_running;
  86. int err = 0, ret;
  87. if (dpaa2_switch_port_is_type_phy(port_priv))
  88. return phylink_ethtool_ksettings_set(port_priv->mac->phylink,
  89. link_ksettings);
  90. /* Interface needs to be down to change link settings */
  91. if_running = netif_running(netdev);
  92. if (if_running) {
  93. err = dpsw_if_disable(ethsw->mc_io, 0,
  94. ethsw->dpsw_handle,
  95. port_priv->idx);
  96. if (err) {
  97. netdev_err(netdev, "dpsw_if_disable err %d\n", err);
  98. return err;
  99. }
  100. }
  101. cfg.rate = link_ksettings->base.speed;
  102. if (link_ksettings->base.autoneg == AUTONEG_ENABLE)
  103. cfg.options |= DPSW_LINK_OPT_AUTONEG;
  104. else
  105. cfg.options &= ~DPSW_LINK_OPT_AUTONEG;
  106. if (link_ksettings->base.duplex == DUPLEX_HALF)
  107. cfg.options |= DPSW_LINK_OPT_HALF_DUPLEX;
  108. else
  109. cfg.options &= ~DPSW_LINK_OPT_HALF_DUPLEX;
  110. err = dpsw_if_set_link_cfg(port_priv->ethsw_data->mc_io, 0,
  111. port_priv->ethsw_data->dpsw_handle,
  112. port_priv->idx,
  113. &cfg);
  114. if (if_running) {
  115. ret = dpsw_if_enable(ethsw->mc_io, 0,
  116. ethsw->dpsw_handle,
  117. port_priv->idx);
  118. if (ret) {
  119. netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
  120. return ret;
  121. }
  122. }
  123. return err;
  124. }
  125. static int
  126. dpaa2_switch_ethtool_get_sset_count(struct net_device *netdev, int sset)
  127. {
  128. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  129. int num_ss_stats = DPAA2_SWITCH_NUM_COUNTERS;
  130. switch (sset) {
  131. case ETH_SS_STATS:
  132. if (port_priv->mac)
  133. num_ss_stats += dpaa2_mac_get_sset_count();
  134. return num_ss_stats;
  135. default:
  136. return -EOPNOTSUPP;
  137. }
  138. }
  139. static void dpaa2_switch_ethtool_get_strings(struct net_device *netdev,
  140. u32 stringset, u8 *data)
  141. {
  142. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  143. u8 *p = data;
  144. int i;
  145. switch (stringset) {
  146. case ETH_SS_STATS:
  147. for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++) {
  148. memcpy(p, dpaa2_switch_ethtool_counters[i].name,
  149. ETH_GSTRING_LEN);
  150. p += ETH_GSTRING_LEN;
  151. }
  152. if (port_priv->mac)
  153. dpaa2_mac_get_strings(p);
  154. break;
  155. }
  156. }
  157. static void dpaa2_switch_ethtool_get_stats(struct net_device *netdev,
  158. struct ethtool_stats *stats,
  159. u64 *data)
  160. {
  161. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  162. int i, err;
  163. for (i = 0; i < DPAA2_SWITCH_NUM_COUNTERS; i++) {
  164. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  165. port_priv->ethsw_data->dpsw_handle,
  166. port_priv->idx,
  167. dpaa2_switch_ethtool_counters[i].id,
  168. &data[i]);
  169. if (err)
  170. netdev_err(netdev, "dpsw_if_get_counter[%s] err %d\n",
  171. dpaa2_switch_ethtool_counters[i].name, err);
  172. }
  173. if (port_priv->mac)
  174. dpaa2_mac_get_ethtool_stats(port_priv->mac, data + i);
  175. }
  176. const struct ethtool_ops dpaa2_switch_port_ethtool_ops = {
  177. .get_drvinfo = dpaa2_switch_get_drvinfo,
  178. .get_link = ethtool_op_get_link,
  179. .get_link_ksettings = dpaa2_switch_get_link_ksettings,
  180. .set_link_ksettings = dpaa2_switch_set_link_ksettings,
  181. .get_strings = dpaa2_switch_ethtool_get_strings,
  182. .get_ethtool_stats = dpaa2_switch_ethtool_get_stats,
  183. .get_sset_count = dpaa2_switch_ethtool_get_sset_count,
  184. };