Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / net / mlx4 / en_selftest.c
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
34 #include <linux/kernel.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/delay.h>
38 #include <linux/mlx4/driver.h>
39
40 #include "mlx4_en.h"
41
42
43 static int mlx4_en_test_registers(struct mlx4_en_priv *priv)
44 {
45         return mlx4_cmd(priv->mdev->dev, 0, 0, 0, MLX4_CMD_HW_HEALTH_CHECK,
46                         MLX4_CMD_TIME_CLASS_A);
47 }
48
49 static int mlx4_en_test_loopback_xmit(struct mlx4_en_priv *priv)
50 {
51         struct sk_buff *skb;
52         struct ethhdr *ethh;
53         unsigned char *packet;
54         unsigned int packet_size = MLX4_LOOPBACK_TEST_PAYLOAD;
55         unsigned int i;
56         int err;
57
58
59         /* build the pkt before xmit */
60         skb = netdev_alloc_skb(priv->dev, MLX4_LOOPBACK_TEST_PAYLOAD + ETH_HLEN + NET_IP_ALIGN);
61         if (!skb) {
62                 en_err(priv, "-LOOPBACK_TEST_XMIT- failed to create skb for xmit\n");
63                 return -ENOMEM;
64         }
65         skb_reserve(skb, NET_IP_ALIGN);
66
67         ethh = (struct ethhdr *)skb_put(skb, sizeof(struct ethhdr));
68         packet  = (unsigned char *)skb_put(skb, packet_size);
69         memcpy(ethh->h_dest, priv->dev->dev_addr, ETH_ALEN);
70         memset(ethh->h_source, 0, ETH_ALEN);
71         ethh->h_proto = htons(ETH_P_ARP);
72         skb_set_mac_header(skb, 0);
73         for (i = 0; i < packet_size; ++i)       /* fill our packet */
74                 packet[i] = (unsigned char)(i & 0xff);
75
76         /* xmit the pkt */
77         err = mlx4_en_xmit(skb, priv->dev);
78         return err;
79 }
80
81 static int mlx4_en_test_loopback(struct mlx4_en_priv *priv)
82 {
83         u32 loopback_ok = 0;
84         int i;
85
86
87         priv->loopback_ok = 0;
88         priv->validate_loopback = 1;
89
90         /* xmit */
91         if (mlx4_en_test_loopback_xmit(priv)) {
92                 en_err(priv, "Transmitting loopback packet failed\n");
93                 goto mlx4_en_test_loopback_exit;
94         }
95
96         /* polling for result */
97         for (i = 0; i < MLX4_EN_LOOPBACK_RETRIES; ++i) {
98                 msleep(MLX4_EN_LOOPBACK_TIMEOUT);
99                 if (priv->loopback_ok) {
100                         loopback_ok = 1;
101                         break;
102                 }
103         }
104         if (!loopback_ok)
105                 en_err(priv, "Loopback packet didn't arrive\n");
106
107 mlx4_en_test_loopback_exit:
108
109         priv->validate_loopback = 0;
110         return !loopback_ok;
111 }
112
113
114 static int mlx4_en_test_link(struct mlx4_en_priv *priv)
115 {
116         if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
117                 return -ENOMEM;
118         if (priv->port_state.link_state == 1)
119                 return 0;
120         else
121                 return 1;
122 }
123
124 static int mlx4_en_test_speed(struct mlx4_en_priv *priv)
125 {
126
127         if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
128                 return -ENOMEM;
129
130         /* The device currently only supports 10G speed */
131         if (priv->port_state.link_speed != SPEED_10000)
132                 return priv->port_state.link_speed;
133         return 0;
134 }
135
136
137 void mlx4_en_ex_selftest(struct net_device *dev, u32 *flags, u64 *buf)
138 {
139         struct mlx4_en_priv *priv = netdev_priv(dev);
140         struct mlx4_en_dev *mdev = priv->mdev;
141         struct mlx4_en_tx_ring *tx_ring;
142         int i, carrier_ok;
143
144         memset(buf, 0, sizeof(u64) * MLX4_EN_NUM_SELF_TEST);
145
146         if (*flags & ETH_TEST_FL_OFFLINE) {
147                 /* disable the interface */
148                 carrier_ok = netif_carrier_ok(dev);
149
150                 netif_carrier_off(dev);
151 retry_tx:
152                 /* Wait until all tx queues are empty.
153                  * there should not be any additional incoming traffic
154                  * since we turned the carrier off */
155                 msleep(200);
156                 for (i = 0; i < priv->tx_ring_num && carrier_ok; i++) {
157                         tx_ring = &priv->tx_ring[i];
158                         if (tx_ring->prod != (tx_ring->cons + tx_ring->last_nr_txbb))
159                                 goto retry_tx;
160                 }
161
162                 if (priv->mdev->dev->caps.flags &
163                                         MLX4_DEV_CAP_FLAG_UC_LOOPBACK) {
164                         buf[3] = mlx4_en_test_registers(priv);
165                         buf[4] = mlx4_en_test_loopback(priv);
166                 }
167
168                 if (carrier_ok)
169                         netif_carrier_on(dev);
170
171         }
172         buf[0] = mlx4_test_interrupts(mdev->dev);
173         buf[1] = mlx4_en_test_link(priv);
174         buf[2] = mlx4_en_test_speed(priv);
175
176         for (i = 0; i < MLX4_EN_NUM_SELF_TEST; i++) {
177                 if (buf[i])
178                         *flags |= ETH_TEST_FL_FAILED;
179         }
180 }