// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 Synopsys, Inc. and/or its affiliates.
* stmmac Selftests Support
*
* Author: Jose Abreu <joabreu@synopsys.com>
*/
#include <linux/completion.h>
#include <linux/ethtool.h>
#include <linux/ip.h>
#include <linux/phy.h>
#include <linux/udp.h>
#include <net/tcp.h>
#include <net/udp.h>
#include "stmmac.h"
struct stmmachdr {
__be32 version;
__be64 magic;
u8 id;
} __packed;
#define STMMAC_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
sizeof(struct stmmachdr))
#define STMMAC_TEST_PKT_MAGIC 0xdeadcafecafedeadULL
#define STMMAC_LB_TIMEOUT msecs_to_jiffies(200)
struct stmmac_packet_attrs {
int vlan;
int vlan_id_in;
int vlan_id_out;
unsigned char *src;
unsigned char *dst;
u32 ip_src;
u32 ip_dst;
int tcp;
int sport;
int dport;
u32 exp_hash;
int dont_wait;
int timeout;
int size;
int remove_sa;
u8 id;
};
static u8 stmmac_test_next_id;
static struct sk_buff *stmmac_test_get_udp_skb(struct stmmac_priv *priv,
struct stmmac_packet_attrs *attr)
{
struct sk_buff *skb = NULL;
struct udphdr *uhdr = NULL;
struct tcphdr *thdr = NULL;
struct stmmachdr *shdr;
struct ethhdr *ehdr;
struct iphdr *ihdr;
int iplen, size;
size = attr->size + STMMAC_TEST_PKT_SIZE;
if (attr->vlan) {
size += 4;
if (attr->vlan > 1)
size += 4;
}
if (attr->tcp)
size += sizeof(struct tcphdr);
else
size += sizeof(struct udphdr);
skb = netdev_alloc_skb(priv->dev, size);
if (!skb)
return NULL;
prefetchw(skb->data);
skb_reserve(skb, NET_IP_ALIGN);
if (attr->vlan > 1)
ehdr = skb_push(skb, ETH_HLEN + 8);
else if (attr->vlan)
ehdr = skb_push(skb, ETH_HLEN + 4);
else if (attr->remove_sa)
ehdr = skb_push(skb, ETH_HLEN - 6);
else
ehdr = skb_push(skb, ETH_HLEN);
skb_reset_mac_header(skb);
skb_set_network_header(skb, skb->len);
ihdr = skb_put(skb, sizeof(*ihdr));
skb_set_transport_header(skb, skb->len);
if (attr->tcp)
thdr = skb_put(skb, sizeof(*thdr));
else
uhdr = skb_put(skb, sizeof(*uhdr));
if (!attr->remove_sa)
eth_zero_addr(ehdr->h_source);
eth_zero_addr(ehdr->h_dest);
if (attr->src && !attr->remove_sa)
ether_addr_copy(ehdr->h_source, attr->src);
if (attr->dst)
ether_addr_copy(ehdr->h_dest, attr->dst);
if (!attr->remove_sa) {
ehdr->h_proto = htons(ETH_P_IP);
} else {
__be16 *ptr = (__be16 *)ehdr;
/* HACK */
ptr[3] = htons(ETH_P_IP);
}
if (attr->vlan) {
__be16 *tag, *proto;
if (!attr->remove_sa) {
tag = (void *)ehdr + ETH_HLEN;
proto = (void *)ehdr + (2 * ETH_ALEN);
} else {
tag = (void *)ehdr + ETH_HLEN - 6;
proto = (void *)ehdr + ETH_ALEN;
}
proto[0] = htons(ETH_P_8021Q);
tag[0] = htons(attr->vlan_id_out);
tag[1] = htons(ETH_P_IP);
if (attr->vlan > 1) {
proto[0] = htons(ETH_P_8021AD);
tag[1] = htons(ETH_P_8021Q);
tag[2] = htons(attr->vlan_id_in);
tag[3] = htons(ETH_P_IP);
}
}
if (attr->tcp) {
thdr->source = htons(attr->sport);
thdr->dest = htons(attr->dport);
thdr->doff = sizeof(struct tcphdr) / 4;
thdr->check = 0;
} else {
uhdr->source = htons(attr->sport);
uhdr->dest = htons(attr->dport);
uhdr->len = htons(sizeof(*shdr) + sizeof(*uhdr) + attr->size);
uhdr->check = 0;
}
ihdr->ihl = 5;
i