linux/drivers/net/ethernet/mellanox/mlx5/core/en_selftest.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016, Mellanox Technologies, Ltd.  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/ip.h>
  34#include <linux/udp.h>
  35#include <net/udp.h>
  36#include "en.h"
  37
  38enum {
  39        MLX5E_ST_LINK_STATE,
  40        MLX5E_ST_LINK_SPEED,
  41        MLX5E_ST_HEALTH_INFO,
  42#ifdef CONFIG_INET
  43        MLX5E_ST_LOOPBACK,
  44#endif
  45        MLX5E_ST_NUM,
  46};
  47
  48const char mlx5e_self_tests[MLX5E_ST_NUM][ETH_GSTRING_LEN] = {
  49        "Link Test",
  50        "Speed Test",
  51        "Health Test",
  52#ifdef CONFIG_INET
  53        "Loopback Test",
  54#endif
  55};
  56
  57int mlx5e_self_test_num(struct mlx5e_priv *priv)
  58{
  59        return ARRAY_SIZE(mlx5e_self_tests);
  60}
  61
  62static int mlx5e_test_health_info(struct mlx5e_priv *priv)
  63{
  64        struct mlx5_core_health *health = &priv->mdev->priv.health;
  65
  66        return health->sick ? 1 : 0;
  67}
  68
  69static int mlx5e_test_link_state(struct mlx5e_priv *priv)
  70{
  71        u8 port_state;
  72
  73        if (!netif_carrier_ok(priv->netdev))
  74                return 1;
  75
  76        port_state = mlx5_query_vport_state(priv->mdev, MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
  77        return port_state == VPORT_STATE_UP ? 0 : 1;
  78}
  79
  80static int mlx5e_test_link_speed(struct mlx5e_priv *priv)
  81{
  82        u32 out[MLX5_ST_SZ_DW(ptys_reg)];
  83        u32 eth_proto_oper;
  84        int i;
  85
  86        if (!netif_carrier_ok(priv->netdev))
  87                return 1;
  88
  89        if (mlx5_query_port_ptys(priv->mdev, out, sizeof(out), MLX5_PTYS_EN, 1))
  90                return 1;
  91
  92        eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
  93        for (i = 0; i < MLX5E_LINK_MODES_NUMBER; i++) {
  94                if (eth_proto_oper & MLX5E_PROT_MASK(i))
  95                        return 0;
  96        }
  97        return 1;
  98}
  99
 100#ifdef CONFIG_INET
 101/* loopback test */
 102#define MLX5E_TEST_PKT_SIZE (MLX5_MPWRQ_SMALL_PACKET_THRESHOLD - NET_IP_ALIGN)
 103static const char mlx5e_test_text[ETH_GSTRING_LEN] = "MLX5E SELF TEST";
 104#define MLX5E_TEST_MAGIC 0x5AEED15C001ULL
 105
 106struct mlx5ehdr {
 107        __be32 version;
 108        __be64 magic;
 109        char   text[ETH_GSTRING_LEN];
 110};
 111
 112static struct sk_buff *mlx5e_test_get_udp_skb(struct mlx5e_priv *priv)
 113{
 114        struct sk_buff *skb = NULL;
 115        struct mlx5ehdr *mlxh;
 116        struct ethhdr *ethh;
 117        struct udphdr *udph;
 118        struct iphdr *iph;
 119        int datalen, iplen;
 120
 121        datalen = MLX5E_TEST_PKT_SIZE -
 122                  (sizeof(*ethh) + sizeof(*iph) + sizeof(*udph));
 123
 124        skb = netdev_alloc_skb(priv->netdev, MLX5E_TEST_PKT_SIZE);
 125        if (!skb) {
 126                netdev_err(priv->netdev, "\tFailed to alloc loopback skb\n");
 127                return NULL;
 128        }
 129
 130        prefetchw(skb->data);
 131        skb_reserve(skb, NET_IP_ALIGN);
 132
 133        /*  Reserve for ethernet and IP header  */
 134        ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
 135        skb_reset_mac_header(skb);
 136
 137        skb_set_network_header(skb, skb->len);
 138        iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
 139
 140        skb_set_transport_header(skb, skb->len);
 141        udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
 142
 143        /* Fill ETH header */
 144        ether_addr_copy(ethh->h_dest, priv->netdev->dev_addr);
 145        eth_zero_addr(ethh->h_source);
 146        ethh->h_proto = htons(ETH_P_IP);
 147
 148        /* Fill UDP header */
 149        udph->source = htons(9);
 150        udph->dest = htons(9); /* Discard Protocol */
 151        udph->len = htons(datalen + sizeof(struct udphdr));
 152        udph->check = 0;
 153
 154        /* Fill IP header */
 155        iph->ihl = 5;
 156        iph->ttl = 32;
 157        iph->version = 4;
 158        iph->protocol = IPPROTO_UDP;
 159        iplen = sizeof(struct iphdr) + sizeof(struct udphdr) + datalen;
 160        iph->tot_len = htons(iplen);
 161        iph->frag_off = 0;
 162        iph->saddr = 0;
 163        iph->daddr = 0;
 164        iph->tos = 0;
 165        iph->id = 0;
 166        ip_send_check(iph);
 167
 168        /* Fill test header and data */
 169        mlxh = (struct mlx5ehdr *)skb_put(skb, sizeof(*mlxh));
 170        mlxh->version = 0;
 171        mlxh->magic = cpu_to_be64(MLX5E_TEST_MAGIC);
 172        strlcpy(mlxh->text, mlx5e_test_text, sizeof(mlxh->text));
 173        datalen -= sizeof(*mlxh);
 174        memset(skb_put(skb, datalen), 0, datalen);
 175
 176        skb->csum = 0;
 177        skb->ip_summed = CHECKSUM_PARTIAL;
 178        udp4_hwcsum(skb, iph->saddr, iph->daddr);
 179
 180        skb->protocol = htons(ETH_P_IP);
 181        skb->pkt_type = PACKET_HOST;
 182        skb->dev = priv->netdev;
 183
 184        return skb;
 185}
 186
 187struct mlx5e_lbt_priv {
 188        struct packet_type pt;
 189        struct completion comp;
 190        bool loopback_ok;
 191};
 192
 193static int
 194mlx5e_test_loopback_validate(struct sk_buff *skb,
 195                             struct net_device *ndev,
 196                             struct packet_type *pt,
 197                             struct net_device *orig_ndev)
 198{
 199        struct mlx5e_lbt_priv *lbtp = pt->af_packet_priv;
 200        struct mlx5ehdr *mlxh;
 201        struct ethhdr *ethh;
 202        struct udphdr *udph;
 203        struct iphdr *iph;
 204
 205        /* We are only going to peek, no need to clone the SKB */
 206        if (skb->protocol != htons(ETH_P_IP))
 207                goto out;
 208
 209        if (MLX5E_TEST_PKT_SIZE - ETH_HLEN > skb_headlen(skb))
 210                goto out;
 211
 212        ethh = (struct ethhdr *)skb_mac_header(skb);
 213        if (!ether_addr_equal(ethh->h_dest, orig_ndev->dev_addr))
 214                goto out;
 215
 216        iph = ip_hdr(skb);
 217        if (iph->protocol != IPPROTO_UDP)
 218                goto out;
 219
 220        udph = udp_hdr(skb);
 221        if (udph->dest != htons(9))
 222                goto out;
 223
 224        mlxh = (struct mlx5ehdr *)((char *)udph + sizeof(*udph));
 225        if (mlxh->magic != cpu_to_be64(MLX5E_TEST_MAGIC))
 226                goto out; /* so close ! */
 227
 228        /* bingo */
 229        lbtp->loopback_ok = true;
 230        complete(&lbtp->comp);
 231out:
 232        kfree_skb(skb);
 233        return 0;
 234}
 235
 236static int mlx5e_test_loopback_setup(struct mlx5e_priv *priv,
 237                                     struct mlx5e_lbt_priv *lbtp)
 238{
 239        int err = 0;
 240
 241        err = mlx5e_refresh_tirs_self_loopback(priv->mdev, true);
 242        if (err) {
 243                netdev_err(priv->netdev,
 244                           "\tFailed to enable UC loopback err(%d)\n", err);
 245                return err;
 246        }
 247
 248        lbtp->loopback_ok = false;
 249        init_completion(&lbtp->comp);
 250
 251        lbtp->pt.type = htons(ETH_P_ALL);
 252        lbtp->pt.func = mlx5e_test_loopback_validate;
 253        lbtp->pt.dev = priv->netdev;
 254        lbtp->pt.af_packet_priv = lbtp;
 255        dev_add_pack(&lbtp->pt);
 256        return err;
 257}
 258
 259static void mlx5e_test_loopback_cleanup(struct mlx5e_priv *priv,
 260                                        struct mlx5e_lbt_priv *lbtp)
 261{
 262        dev_remove_pack(&lbtp->pt);
 263        mlx5e_refresh_tirs_self_loopback(priv->mdev, false);
 264}
 265
 266#define MLX5E_LB_VERIFY_TIMEOUT (msecs_to_jiffies(200))
 267static int mlx5e_test_loopback(struct mlx5e_priv *priv)
 268{
 269        struct mlx5e_lbt_priv *lbtp;
 270        struct sk_buff *skb = NULL;
 271        int err;
 272
 273        if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
 274                netdev_err(priv->netdev,
 275                           "\tCan't perform loobpack test while device is down\n");
 276                return -ENODEV;
 277        }
 278
 279        lbtp = kzalloc(sizeof(*lbtp), GFP_KERNEL);
 280        if (!lbtp)
 281                return -ENOMEM;
 282        lbtp->loopback_ok = false;
 283
 284        err = mlx5e_test_loopback_setup(priv, lbtp);
 285        if (err)
 286                goto out;
 287
 288        skb = mlx5e_test_get_udp_skb(priv);
 289        if (!skb) {
 290                err = -ENOMEM;
 291                goto cleanup;
 292        }
 293
 294        skb_set_queue_mapping(skb, 0);
 295        err = dev_queue_xmit(skb);
 296        if (err) {
 297                netdev_err(priv->netdev,
 298                           "\tFailed to xmit loopback packet err(%d)\n",
 299                           err);
 300                goto cleanup;
 301        }
 302
 303        wait_for_completion_timeout(&lbtp->comp, MLX5E_LB_VERIFY_TIMEOUT);
 304        err = !lbtp->loopback_ok;
 305
 306cleanup:
 307        mlx5e_test_loopback_cleanup(priv, lbtp);
 308out:
 309        kfree(lbtp);
 310        return err;
 311}
 312#endif
 313
 314static int (*mlx5e_st_func[MLX5E_ST_NUM])(struct mlx5e_priv *) = {
 315        mlx5e_test_link_state,
 316        mlx5e_test_link_speed,
 317        mlx5e_test_health_info,
 318#ifdef CONFIG_INET
 319        mlx5e_test_loopback,
 320#endif
 321};
 322
 323void mlx5e_self_test(struct net_device *ndev, struct ethtool_test *etest,
 324                     u64 *buf)
 325{
 326        struct mlx5e_priv *priv = netdev_priv(ndev);
 327        int i;
 328
 329        memset(buf, 0, sizeof(u64) * MLX5E_ST_NUM);
 330
 331        mutex_lock(&priv->state_lock);
 332        netdev_info(ndev, "Self test begin..\n");
 333
 334        for (i = 0; i < MLX5E_ST_NUM; i++) {
 335                netdev_info(ndev, "\t[%d] %s start..\n",
 336                            i, mlx5e_self_tests[i]);
 337                buf[i] = mlx5e_st_func[i](priv);
 338                netdev_info(ndev, "\t[%d] %s end: result(%lld)\n",
 339                            i, mlx5e_self_tests[i], buf[i]);
 340        }
 341
 342        mutex_unlock(&priv->state_lock);
 343
 344        for (i = 0; i < MLX5E_ST_NUM; i++) {
 345                if (buf[i]) {
 346                        etest->flags |= ETH_TEST_FL_FAILED;
 347                        break;
 348                }
 349        }
 350        netdev_info(ndev, "Self test out: status flags(0x%x)\n",
 351                    etest->flags);
 352}
 353