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