en_selftest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/delay.h>
  37. #include <linux/mlx4/driver.h>
  38. #include "mlx4_en.h"
  39. static int mlx4_en_test_registers(struct mlx4_en_priv *priv)
  40. {
  41. return mlx4_cmd(priv->mdev->dev, 0, 0, 0, MLX4_CMD_HW_HEALTH_CHECK,
  42. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  43. }
  44. static int mlx4_en_test_loopback_xmit(struct mlx4_en_priv *priv)
  45. {
  46. struct sk_buff *skb;
  47. struct ethhdr *ethh;
  48. unsigned char *packet;
  49. unsigned int packet_size = MLX4_LOOPBACK_TEST_PAYLOAD;
  50. unsigned int i;
  51. int err;
  52. /* build the pkt before xmit */
  53. skb = netdev_alloc_skb(priv->dev, MLX4_LOOPBACK_TEST_PAYLOAD + ETH_HLEN + NET_IP_ALIGN);
  54. if (!skb)
  55. return -ENOMEM;
  56. skb_reserve(skb, NET_IP_ALIGN);
  57. ethh = (struct ethhdr *)skb_put(skb, sizeof(struct ethhdr));
  58. packet = (unsigned char *)skb_put(skb, packet_size);
  59. memcpy(ethh->h_dest, priv->dev->dev_addr, ETH_ALEN);
  60. eth_zero_addr(ethh->h_source);
  61. ethh->h_proto = htons(ETH_P_ARP);
  62. skb_set_mac_header(skb, 0);
  63. for (i = 0; i < packet_size; ++i) /* fill our packet */
  64. packet[i] = (unsigned char)(i & 0xff);
  65. /* xmit the pkt */
  66. err = mlx4_en_xmit(skb, priv->dev);
  67. return err;
  68. }
  69. static int mlx4_en_test_loopback(struct mlx4_en_priv *priv)
  70. {
  71. u32 loopback_ok = 0;
  72. int i;
  73. bool gro_enabled;
  74. priv->loopback_ok = 0;
  75. priv->validate_loopback = 1;
  76. gro_enabled = priv->dev->features & NETIF_F_GRO;
  77. mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
  78. priv->dev->features &= ~NETIF_F_GRO;
  79. /* xmit */
  80. if (mlx4_en_test_loopback_xmit(priv)) {
  81. en_err(priv, "Transmitting loopback packet failed\n");
  82. goto mlx4_en_test_loopback_exit;
  83. }
  84. /* polling for result */
  85. for (i = 0; i < MLX4_EN_LOOPBACK_RETRIES; ++i) {
  86. msleep(MLX4_EN_LOOPBACK_TIMEOUT);
  87. if (priv->loopback_ok) {
  88. loopback_ok = 1;
  89. break;
  90. }
  91. }
  92. if (!loopback_ok)
  93. en_err(priv, "Loopback packet didn't arrive\n");
  94. mlx4_en_test_loopback_exit:
  95. priv->validate_loopback = 0;
  96. if (gro_enabled)
  97. priv->dev->features |= NETIF_F_GRO;
  98. mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
  99. return !loopback_ok;
  100. }
  101. static int mlx4_en_test_interrupts(struct mlx4_en_priv *priv)
  102. {
  103. struct mlx4_en_dev *mdev = priv->mdev;
  104. int err = 0;
  105. int i = 0;
  106. err = mlx4_test_async(mdev->dev);
  107. /* When not in MSI_X or slave, test only async */
  108. if (!(mdev->dev->flags & MLX4_FLAG_MSI_X) || mlx4_is_slave(mdev->dev))
  109. return err;
  110. /* A loop over all completion vectors of current port,
  111. * for each vector check whether it works by mapping command
  112. * completions to that vector and performing a NOP command
  113. */
  114. for (i = 0; i < priv->rx_ring_num; i++) {
  115. err = mlx4_test_interrupt(mdev->dev, priv->rx_cq[i]->vector);
  116. if (err)
  117. break;
  118. }
  119. return err;
  120. }
  121. static int mlx4_en_test_link(struct mlx4_en_priv *priv)
  122. {
  123. if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
  124. return -ENOMEM;
  125. if (priv->port_state.link_state == 1)
  126. return 0;
  127. else
  128. return 1;
  129. }
  130. static int mlx4_en_test_speed(struct mlx4_en_priv *priv)
  131. {
  132. if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
  133. return -ENOMEM;
  134. /* The device supports 100M, 1G, 10G, 20G, 40G and 56G speed */
  135. if (priv->port_state.link_speed != SPEED_100 &&
  136. priv->port_state.link_speed != SPEED_1000 &&
  137. priv->port_state.link_speed != SPEED_10000 &&
  138. priv->port_state.link_speed != SPEED_20000 &&
  139. priv->port_state.link_speed != SPEED_40000 &&
  140. priv->port_state.link_speed != SPEED_56000)
  141. return priv->port_state.link_speed;
  142. return 0;
  143. }
  144. void mlx4_en_ex_selftest(struct net_device *dev, u32 *flags, u64 *buf)
  145. {
  146. struct mlx4_en_priv *priv = netdev_priv(dev);
  147. int i, carrier_ok;
  148. memset(buf, 0, sizeof(u64) * MLX4_EN_NUM_SELF_TEST);
  149. if (*flags & ETH_TEST_FL_OFFLINE) {
  150. /* disable the interface */
  151. carrier_ok = netif_carrier_ok(dev);
  152. netif_carrier_off(dev);
  153. /* Wait until all tx queues are empty.
  154. * there should not be any additional incoming traffic
  155. * since we turned the carrier off */
  156. msleep(200);
  157. if (priv->mdev->dev->caps.flags &
  158. MLX4_DEV_CAP_FLAG_UC_LOOPBACK) {
  159. buf[3] = mlx4_en_test_registers(priv);
  160. if (priv->port_up)
  161. buf[4] = mlx4_en_test_loopback(priv);
  162. }
  163. if (carrier_ok)
  164. netif_carrier_on(dev);
  165. }
  166. buf[0] = mlx4_en_test_interrupts(priv);
  167. buf[1] = mlx4_en_test_link(priv);
  168. buf[2] = mlx4_en_test_speed(priv);
  169. for (i = 0; i < MLX4_EN_NUM_SELF_TEST; i++) {
  170. if (buf[i])
  171. *flags |= ETH_TEST_FL_FAILED;
  172. }
  173. }