opa_vnic_ethtool.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright(c) 2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. /*
  48. * This file contains OPA VNIC ethtool functions
  49. */
  50. #include <linux/ethtool.h>
  51. #include "opa_vnic_internal.h"
  52. enum {NETDEV_STATS, VNIC_STATS};
  53. struct vnic_stats {
  54. char stat_string[ETH_GSTRING_LEN];
  55. struct {
  56. int sizeof_stat;
  57. int stat_offset;
  58. };
  59. };
  60. #define VNIC_STAT(m) { FIELD_SIZEOF(struct opa_vnic_stats, m), \
  61. offsetof(struct opa_vnic_stats, m) }
  62. static struct vnic_stats vnic_gstrings_stats[] = {
  63. /* NETDEV stats */
  64. {"rx_packets", VNIC_STAT(netstats.rx_packets)},
  65. {"tx_packets", VNIC_STAT(netstats.tx_packets)},
  66. {"rx_bytes", VNIC_STAT(netstats.rx_bytes)},
  67. {"tx_bytes", VNIC_STAT(netstats.tx_bytes)},
  68. {"rx_errors", VNIC_STAT(netstats.rx_errors)},
  69. {"tx_errors", VNIC_STAT(netstats.tx_errors)},
  70. {"rx_dropped", VNIC_STAT(netstats.rx_dropped)},
  71. {"tx_dropped", VNIC_STAT(netstats.tx_dropped)},
  72. /* SUMMARY counters */
  73. {"tx_unicast", VNIC_STAT(tx_grp.unicast)},
  74. {"tx_mcastbcast", VNIC_STAT(tx_grp.mcastbcast)},
  75. {"tx_untagged", VNIC_STAT(tx_grp.untagged)},
  76. {"tx_vlan", VNIC_STAT(tx_grp.vlan)},
  77. {"tx_64_size", VNIC_STAT(tx_grp.s_64)},
  78. {"tx_65_127", VNIC_STAT(tx_grp.s_65_127)},
  79. {"tx_128_255", VNIC_STAT(tx_grp.s_128_255)},
  80. {"tx_256_511", VNIC_STAT(tx_grp.s_256_511)},
  81. {"tx_512_1023", VNIC_STAT(tx_grp.s_512_1023)},
  82. {"tx_1024_1518", VNIC_STAT(tx_grp.s_1024_1518)},
  83. {"tx_1519_max", VNIC_STAT(tx_grp.s_1519_max)},
  84. {"rx_unicast", VNIC_STAT(rx_grp.unicast)},
  85. {"rx_mcastbcast", VNIC_STAT(rx_grp.mcastbcast)},
  86. {"rx_untagged", VNIC_STAT(rx_grp.untagged)},
  87. {"rx_vlan", VNIC_STAT(rx_grp.vlan)},
  88. {"rx_64_size", VNIC_STAT(rx_grp.s_64)},
  89. {"rx_65_127", VNIC_STAT(rx_grp.s_65_127)},
  90. {"rx_128_255", VNIC_STAT(rx_grp.s_128_255)},
  91. {"rx_256_511", VNIC_STAT(rx_grp.s_256_511)},
  92. {"rx_512_1023", VNIC_STAT(rx_grp.s_512_1023)},
  93. {"rx_1024_1518", VNIC_STAT(rx_grp.s_1024_1518)},
  94. {"rx_1519_max", VNIC_STAT(rx_grp.s_1519_max)},
  95. /* ERROR counters */
  96. {"rx_fifo_errors", VNIC_STAT(netstats.rx_fifo_errors)},
  97. {"rx_length_errors", VNIC_STAT(netstats.rx_length_errors)},
  98. {"tx_fifo_errors", VNIC_STAT(netstats.tx_fifo_errors)},
  99. {"tx_carrier_errors", VNIC_STAT(netstats.tx_carrier_errors)},
  100. {"tx_dlid_zero", VNIC_STAT(tx_dlid_zero)},
  101. {"tx_drop_state", VNIC_STAT(tx_drop_state)},
  102. {"rx_drop_state", VNIC_STAT(rx_drop_state)},
  103. {"rx_oversize", VNIC_STAT(rx_oversize)},
  104. {"rx_runt", VNIC_STAT(rx_runt)},
  105. };
  106. #define VNIC_STATS_LEN ARRAY_SIZE(vnic_gstrings_stats)
  107. /* vnic_get_drvinfo - get driver info */
  108. static void vnic_get_drvinfo(struct net_device *netdev,
  109. struct ethtool_drvinfo *drvinfo)
  110. {
  111. strlcpy(drvinfo->driver, opa_vnic_driver_name, sizeof(drvinfo->driver));
  112. strlcpy(drvinfo->version, opa_vnic_driver_version,
  113. sizeof(drvinfo->version));
  114. strlcpy(drvinfo->bus_info, dev_name(netdev->dev.parent),
  115. sizeof(drvinfo->bus_info));
  116. }
  117. /* vnic_get_sset_count - get string set count */
  118. static int vnic_get_sset_count(struct net_device *netdev, int sset)
  119. {
  120. return (sset == ETH_SS_STATS) ? VNIC_STATS_LEN : -EOPNOTSUPP;
  121. }
  122. /* vnic_get_ethtool_stats - get statistics */
  123. static void vnic_get_ethtool_stats(struct net_device *netdev,
  124. struct ethtool_stats *stats, u64 *data)
  125. {
  126. struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
  127. struct opa_vnic_stats vstats;
  128. int i;
  129. memset(&vstats, 0, sizeof(vstats));
  130. spin_lock(&adapter->stats_lock);
  131. adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats);
  132. spin_unlock(&adapter->stats_lock);
  133. for (i = 0; i < VNIC_STATS_LEN; i++) {
  134. char *p = (char *)&vstats + vnic_gstrings_stats[i].stat_offset;
  135. data[i] = (vnic_gstrings_stats[i].sizeof_stat ==
  136. sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  137. }
  138. }
  139. /* vnic_get_strings - get strings */
  140. static void vnic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  141. {
  142. int i;
  143. if (stringset != ETH_SS_STATS)
  144. return;
  145. for (i = 0; i < VNIC_STATS_LEN; i++)
  146. memcpy(data + i * ETH_GSTRING_LEN,
  147. vnic_gstrings_stats[i].stat_string,
  148. ETH_GSTRING_LEN);
  149. }
  150. /* ethtool ops */
  151. static const struct ethtool_ops opa_vnic_ethtool_ops = {
  152. .get_drvinfo = vnic_get_drvinfo,
  153. .get_link = ethtool_op_get_link,
  154. .get_strings = vnic_get_strings,
  155. .get_sset_count = vnic_get_sset_count,
  156. .get_ethtool_stats = vnic_get_ethtool_stats,
  157. };
  158. /* opa_vnic_set_ethtool_ops - set ethtool ops */
  159. void opa_vnic_set_ethtool_ops(struct net_device *netdev)
  160. {
  161. netdev->ethtool_ops = &opa_vnic_ethtool_ops;
  162. }