15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
35a2cc190SJeff Kirsher  *
45a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
55a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
65a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
75a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
85a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
95a2cc190SJeff Kirsher  *
105a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
115a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
125a2cc190SJeff Kirsher  *     conditions are met:
135a2cc190SJeff Kirsher  *
145a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
155a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
165a2cc190SJeff Kirsher  *        disclaimer.
175a2cc190SJeff Kirsher  *
185a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
195a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
205a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
215a2cc190SJeff Kirsher  *        provided with the distribution.
225a2cc190SJeff Kirsher  *
235a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
245a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
255a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
265a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
275a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
285a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
295a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
305a2cc190SJeff Kirsher  * SOFTWARE.
315a2cc190SJeff Kirsher  *
325a2cc190SJeff Kirsher  */
335a2cc190SJeff Kirsher 
345a2cc190SJeff Kirsher #include <linux/kernel.h>
355a2cc190SJeff Kirsher #include <linux/ethtool.h>
365a2cc190SJeff Kirsher #include <linux/netdevice.h>
375a2cc190SJeff Kirsher #include <linux/delay.h>
385a2cc190SJeff Kirsher #include <linux/mlx4/driver.h>
395a2cc190SJeff Kirsher 
405a2cc190SJeff Kirsher #include "mlx4_en.h"
415a2cc190SJeff Kirsher 
425a2cc190SJeff Kirsher 
mlx4_en_test_registers(struct mlx4_en_priv * priv)435a2cc190SJeff Kirsher static int mlx4_en_test_registers(struct mlx4_en_priv *priv)
445a2cc190SJeff Kirsher {
455a2cc190SJeff Kirsher 	return mlx4_cmd(priv->mdev->dev, 0, 0, 0, MLX4_CMD_HW_HEALTH_CHECK,
46f9baff50SJack Morgenstein 			MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
475a2cc190SJeff Kirsher }
485a2cc190SJeff Kirsher 
mlx4_en_test_loopback_xmit(struct mlx4_en_priv * priv)495a2cc190SJeff Kirsher static int mlx4_en_test_loopback_xmit(struct mlx4_en_priv *priv)
505a2cc190SJeff Kirsher {
515a2cc190SJeff Kirsher 	struct sk_buff *skb;
525a2cc190SJeff Kirsher 	struct ethhdr *ethh;
535a2cc190SJeff Kirsher 	unsigned char *packet;
545a2cc190SJeff Kirsher 	unsigned int packet_size = MLX4_LOOPBACK_TEST_PAYLOAD;
555a2cc190SJeff Kirsher 	unsigned int i;
565a2cc190SJeff Kirsher 	int err;
575a2cc190SJeff Kirsher 
585a2cc190SJeff Kirsher 
595a2cc190SJeff Kirsher 	/* build the pkt before xmit */
605a2cc190SJeff Kirsher 	skb = netdev_alloc_skb(priv->dev, MLX4_LOOPBACK_TEST_PAYLOAD + ETH_HLEN + NET_IP_ALIGN);
61720a43efSJoe Perches 	if (!skb)
625a2cc190SJeff Kirsher 		return -ENOMEM;
63720a43efSJoe Perches 
645a2cc190SJeff Kirsher 	skb_reserve(skb, NET_IP_ALIGN);
655a2cc190SJeff Kirsher 
664df864c1SJohannes Berg 	ethh = skb_put(skb, sizeof(struct ethhdr));
674df864c1SJohannes Berg 	packet = skb_put(skb, packet_size);
685a2cc190SJeff Kirsher 	memcpy(ethh->h_dest, priv->dev->dev_addr, ETH_ALEN);
69c7bf7169SJoe Perches 	eth_zero_addr(ethh->h_source);
705a2cc190SJeff Kirsher 	ethh->h_proto = htons(ETH_P_ARP);
7169029109SZhang Shengju 	skb_reset_mac_header(skb);
725a2cc190SJeff Kirsher 	for (i = 0; i < packet_size; ++i)	/* fill our packet */
735a2cc190SJeff Kirsher 		packet[i] = (unsigned char)(i & 0xff);
745a2cc190SJeff Kirsher 
755a2cc190SJeff Kirsher 	/* xmit the pkt */
765a2cc190SJeff Kirsher 	err = mlx4_en_xmit(skb, priv->dev);
775a2cc190SJeff Kirsher 	return err;
785a2cc190SJeff Kirsher }
795a2cc190SJeff Kirsher 
mlx4_en_test_loopback(struct mlx4_en_priv * priv)805a2cc190SJeff Kirsher static int mlx4_en_test_loopback(struct mlx4_en_priv *priv)
815a2cc190SJeff Kirsher {
825a2cc190SJeff Kirsher 	u32 loopback_ok = 0;
835a2cc190SJeff Kirsher 	int i;
845a2cc190SJeff Kirsher 
855a2cc190SJeff Kirsher         priv->loopback_ok = 0;
865a2cc190SJeff Kirsher 	priv->validate_loopback = 1;
875a2cc190SJeff Kirsher 
8879aeaccdSYan Burman 	mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
8979aeaccdSYan Burman 
905a2cc190SJeff Kirsher 	/* xmit */
915a2cc190SJeff Kirsher 	if (mlx4_en_test_loopback_xmit(priv)) {
925a2cc190SJeff Kirsher 		en_err(priv, "Transmitting loopback packet failed\n");
935a2cc190SJeff Kirsher 		goto mlx4_en_test_loopback_exit;
945a2cc190SJeff Kirsher 	}
955a2cc190SJeff Kirsher 
965a2cc190SJeff Kirsher 	/* polling for result */
975a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_EN_LOOPBACK_RETRIES; ++i) {
985a2cc190SJeff Kirsher 		msleep(MLX4_EN_LOOPBACK_TIMEOUT);
995a2cc190SJeff Kirsher 		if (priv->loopback_ok) {
1005a2cc190SJeff Kirsher 			loopback_ok = 1;
1015a2cc190SJeff Kirsher 			break;
1025a2cc190SJeff Kirsher 		}
1035a2cc190SJeff Kirsher 	}
1045a2cc190SJeff Kirsher 	if (!loopback_ok)
1055a2cc190SJeff Kirsher 		en_err(priv, "Loopback packet didn't arrive\n");
1065a2cc190SJeff Kirsher 
1075a2cc190SJeff Kirsher mlx4_en_test_loopback_exit:
1085a2cc190SJeff Kirsher 
1095a2cc190SJeff Kirsher 	priv->validate_loopback = 0;
1101037ebbbSIdo Shamay 
11179aeaccdSYan Burman 	mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
1125a2cc190SJeff Kirsher 	return !loopback_ok;
1135a2cc190SJeff Kirsher }
1145a2cc190SJeff Kirsher 
mlx4_en_test_interrupts(struct mlx4_en_priv * priv)1156f2e0d2cSEugenia Emantayev static int mlx4_en_test_interrupts(struct mlx4_en_priv *priv)
1166f2e0d2cSEugenia Emantayev {
1176f2e0d2cSEugenia Emantayev 	struct mlx4_en_dev *mdev = priv->mdev;
1186f2e0d2cSEugenia Emantayev 	int err = 0;
1196f2e0d2cSEugenia Emantayev 	int i = 0;
1206f2e0d2cSEugenia Emantayev 
1216f2e0d2cSEugenia Emantayev 	err = mlx4_test_async(mdev->dev);
1226f2e0d2cSEugenia Emantayev 	/* When not in MSI_X or slave, test only async */
1236f2e0d2cSEugenia Emantayev 	if (!(mdev->dev->flags & MLX4_FLAG_MSI_X) || mlx4_is_slave(mdev->dev))
1246f2e0d2cSEugenia Emantayev 		return err;
1256f2e0d2cSEugenia Emantayev 
1266f2e0d2cSEugenia Emantayev 	/* A loop over all completion vectors of current port,
1276f2e0d2cSEugenia Emantayev 	 * for each vector check whether it works by mapping command
1286f2e0d2cSEugenia Emantayev 	 * completions to that vector and performing a NOP command
1296f2e0d2cSEugenia Emantayev 	 */
1306f2e0d2cSEugenia Emantayev 	for (i = 0; i < priv->rx_ring_num; i++) {
1316f2e0d2cSEugenia Emantayev 		err = mlx4_test_interrupt(mdev->dev, priv->rx_cq[i]->vector);
1326f2e0d2cSEugenia Emantayev 		if (err)
1336f2e0d2cSEugenia Emantayev 			break;
1346f2e0d2cSEugenia Emantayev 	}
1356f2e0d2cSEugenia Emantayev 
1366f2e0d2cSEugenia Emantayev 	return err;
1376f2e0d2cSEugenia Emantayev }
1385a2cc190SJeff Kirsher 
mlx4_en_test_link(struct mlx4_en_priv * priv)1395a2cc190SJeff Kirsher static int mlx4_en_test_link(struct mlx4_en_priv *priv)
1405a2cc190SJeff Kirsher {
1415a2cc190SJeff Kirsher 	if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
1425a2cc190SJeff Kirsher 		return -ENOMEM;
1435a2cc190SJeff Kirsher 	if (priv->port_state.link_state == 1)
1445a2cc190SJeff Kirsher 		return 0;
1455a2cc190SJeff Kirsher 	else
1465a2cc190SJeff Kirsher 		return 1;
1475a2cc190SJeff Kirsher }
1485a2cc190SJeff Kirsher 
mlx4_en_test_speed(struct mlx4_en_priv * priv)1495a2cc190SJeff Kirsher static int mlx4_en_test_speed(struct mlx4_en_priv *priv)
1505a2cc190SJeff Kirsher {
1515a2cc190SJeff Kirsher 
1525a2cc190SJeff Kirsher 	if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
1535a2cc190SJeff Kirsher 		return -ENOMEM;
1545a2cc190SJeff Kirsher 
155dcf972a3SSaeed Mahameed 	/* The device supports 100M, 1G, 10G, 20G, 40G and 56G speed */
156dcf972a3SSaeed Mahameed 	if (priv->port_state.link_speed != SPEED_100 &&
157dcf972a3SSaeed Mahameed 	    priv->port_state.link_speed != SPEED_1000 &&
158dcf972a3SSaeed Mahameed 	    priv->port_state.link_speed != SPEED_10000 &&
159dcf972a3SSaeed Mahameed 	    priv->port_state.link_speed != SPEED_20000 &&
160dcf972a3SSaeed Mahameed 	    priv->port_state.link_speed != SPEED_40000 &&
161dcf972a3SSaeed Mahameed 	    priv->port_state.link_speed != SPEED_56000)
1625a2cc190SJeff Kirsher 		return priv->port_state.link_speed;
163dcf972a3SSaeed Mahameed 
1645a2cc190SJeff Kirsher 	return 0;
1655a2cc190SJeff Kirsher }
1665a2cc190SJeff Kirsher 
1675a2cc190SJeff Kirsher 
mlx4_en_ex_selftest(struct net_device * dev,u32 * flags,u64 * buf)1685a2cc190SJeff Kirsher void mlx4_en_ex_selftest(struct net_device *dev, u32 *flags, u64 *buf)
1695a2cc190SJeff Kirsher {
1705a2cc190SJeff Kirsher 	struct mlx4_en_priv *priv = netdev_priv(dev);
1715a2cc190SJeff Kirsher 	int i, carrier_ok;
1725a2cc190SJeff Kirsher 
1735a2cc190SJeff Kirsher 	memset(buf, 0, sizeof(u64) * MLX4_EN_NUM_SELF_TEST);
1745a2cc190SJeff Kirsher 
1755a2cc190SJeff Kirsher 	if (*flags & ETH_TEST_FL_OFFLINE) {
1765a2cc190SJeff Kirsher 		/* disable the interface */
1775a2cc190SJeff Kirsher 		carrier_ok = netif_carrier_ok(dev);
1785a2cc190SJeff Kirsher 
1795a2cc190SJeff Kirsher 		netif_carrier_off(dev);
1805a2cc190SJeff Kirsher 		/* Wait until all tx queues are empty.
1815a2cc190SJeff Kirsher 		 * there should not be any additional incoming traffic
1825a2cc190SJeff Kirsher 		 * since we turned the carrier off */
1835a2cc190SJeff Kirsher 		msleep(200);
1845a2cc190SJeff Kirsher 
1855a2cc190SJeff Kirsher 		if (priv->mdev->dev->caps.flags &
1865a2cc190SJeff Kirsher 					MLX4_DEV_CAP_FLAG_UC_LOOPBACK) {
1875a2cc190SJeff Kirsher 			buf[3] = mlx4_en_test_registers(priv);
188*78034f5fSEugenia Emantayev 			if (priv->port_up && dev->mtu >= MLX4_SELFTEST_LB_MIN_MTU)
1895a2cc190SJeff Kirsher 				buf[4] = mlx4_en_test_loopback(priv);
1905a2cc190SJeff Kirsher 		}
1915a2cc190SJeff Kirsher 
1925a2cc190SJeff Kirsher 		if (carrier_ok)
1935a2cc190SJeff Kirsher 			netif_carrier_on(dev);
1945a2cc190SJeff Kirsher 
1955a2cc190SJeff Kirsher 	}
1966f2e0d2cSEugenia Emantayev 	buf[0] = mlx4_en_test_interrupts(priv);
1975a2cc190SJeff Kirsher 	buf[1] = mlx4_en_test_link(priv);
1985a2cc190SJeff Kirsher 	buf[2] = mlx4_en_test_speed(priv);
1995a2cc190SJeff Kirsher 
2005a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_EN_NUM_SELF_TEST; i++) {
2015a2cc190SJeff Kirsher 		if (buf[i])
2025a2cc190SJeff Kirsher 			*flags |= ETH_TEST_FL_FAILED;
2035a2cc190SJeff Kirsher 	}
2045a2cc190SJeff Kirsher }
205