Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

amd-xgbe: introduce support ethtool selftest

Add support for ethtool selftest for MAC loopback. This includes the
sanity check and helps in finding the misconfiguration of HW. Uses the
existing selftest infrastructure to create test packets.

Signed-off-by: Raju Rangoju <Raju.Rangoju@amd.com>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Link: https://patch.msgid.link/20251031111555.774425-2-Raju.Rangoju@amd.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Raju Rangoju and committed by
Paolo Abeni
862a64c8 6b47af35

+273 -1
+1
drivers/net/ethernet/amd/Kconfig
··· 165 165 select CRC32 166 166 select PHYLIB 167 167 select AMD_XGBE_HAVE_ECC if X86 168 + select NET_SELFTESTS 168 169 help 169 170 This driver supports the AMD 10GbE Ethernet device found on an 170 171 AMD SoC.
+1 -1
drivers/net/ethernet/amd/xgbe/Makefile
··· 5 5 xgbe-desc.o xgbe-ethtool.o xgbe-mdio.o \ 6 6 xgbe-hwtstamp.o xgbe-ptp.o xgbe-pps.o \ 7 7 xgbe-i2c.o xgbe-phy-v1.o xgbe-phy-v2.o \ 8 - xgbe-platform.o 8 + xgbe-platform.o xgbe-selftest.o 9 9 10 10 amd-xgbe-$(CONFIG_PCI) += xgbe-pci.o 11 11 amd-xgbe-$(CONFIG_AMD_XGBE_DCB) += xgbe-dcb.o
+17
drivers/net/ethernet/amd/xgbe/xgbe-dev.c
··· 3578 3578 3579 3579 DBGPR("<--xgbe_init_function_ptrs\n"); 3580 3580 } 3581 + 3582 + int xgbe_enable_mac_loopback(struct xgbe_prv_data *pdata) 3583 + { 3584 + /* Enable MAC loopback mode */ 3585 + XGMAC_IOWRITE_BITS(pdata, MAC_RCR, LM, 1); 3586 + 3587 + /* Wait for loopback to stabilize */ 3588 + usleep_range(10, 15); 3589 + 3590 + return 0; 3591 + } 3592 + 3593 + void xgbe_disable_mac_loopback(struct xgbe_prv_data *pdata) 3594 + { 3595 + /* Disable MAC loopback mode */ 3596 + XGMAC_IOWRITE_BITS(pdata, MAC_RCR, LM, 0); 3597 + }
+7
drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c
··· 85 85 int i; 86 86 87 87 switch (stringset) { 88 + case ETH_SS_TEST: 89 + xgbe_selftest_get_strings(pdata, data); 90 + break; 88 91 case ETH_SS_STATS: 89 92 for (i = 0; i < XGBE_STATS_COUNT; i++) 90 93 ethtool_puts(&data, xgbe_gstring_stats[i].stat_string); ··· 134 131 int ret; 135 132 136 133 switch (stringset) { 134 + case ETH_SS_TEST: 135 + ret = xgbe_selftest_get_count(pdata); 136 + break; 137 137 case ETH_SS_STATS: 138 138 ret = XGBE_STATS_COUNT + 139 139 (pdata->tx_ring_count * 2) + ··· 766 760 .set_ringparam = xgbe_set_ringparam, 767 761 .get_channels = xgbe_get_channels, 768 762 .set_channels = xgbe_set_channels, 763 + .self_test = xgbe_selftest_run, 769 764 }; 770 765 771 766 const struct ethtool_ops *xgbe_get_ethtool_ops(void)
+237
drivers/net/ethernet/amd/xgbe/xgbe-selftest.c
··· 1 + // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-3-Clause) 2 + /* 3 + * Copyright (c) 2014-2025, Advanced Micro Devices, Inc. 4 + * Copyright (c) 2014, Synopsys, Inc. 5 + * All rights reserved 6 + * 7 + * Author: Raju Rangoju <Raju.Rangoju@amd.com> 8 + */ 9 + #include <linux/crc32.h> 10 + #include <linux/ip.h> 11 + #include <linux/udp.h> 12 + #include <net/tcp.h> 13 + #include <net/udp.h> 14 + #include <net/checksum.h> 15 + #include <net/selftests.h> 16 + 17 + #include "xgbe.h" 18 + #include "xgbe-common.h" 19 + 20 + #define XGBE_LOOPBACK_NONE 0 21 + #define XGBE_LOOPBACK_MAC 1 22 + 23 + struct xgbe_test { 24 + char name[ETH_GSTRING_LEN]; 25 + int lb; 26 + int (*fn)(struct xgbe_prv_data *pdata); 27 + }; 28 + 29 + static u8 xgbe_test_id; 30 + 31 + static int xgbe_test_loopback_validate(struct sk_buff *skb, 32 + struct net_device *ndev, 33 + struct packet_type *pt, 34 + struct net_device *orig_ndev) 35 + { 36 + struct net_test_priv *tdata = pt->af_packet_priv; 37 + const unsigned char *dst = tdata->packet->dst; 38 + const unsigned char *src = tdata->packet->src; 39 + struct netsfhdr *hdr; 40 + struct ethhdr *eh; 41 + struct tcphdr *th; 42 + struct udphdr *uh; 43 + struct iphdr *ih; 44 + 45 + skb = skb_unshare(skb, GFP_ATOMIC); 46 + if (!skb) 47 + goto out; 48 + 49 + if (skb_linearize(skb)) 50 + goto out; 51 + 52 + if (skb_headlen(skb) < (NET_TEST_PKT_SIZE - ETH_HLEN)) 53 + goto out; 54 + 55 + eh = (struct ethhdr *)skb_mac_header(skb); 56 + if (dst) { 57 + if (!ether_addr_equal_unaligned(eh->h_dest, dst)) 58 + goto out; 59 + } 60 + if (src) { 61 + if (!ether_addr_equal_unaligned(eh->h_source, src)) 62 + goto out; 63 + } 64 + 65 + ih = ip_hdr(skb); 66 + 67 + if (tdata->packet->tcp) { 68 + if (ih->protocol != IPPROTO_TCP) 69 + goto out; 70 + 71 + th = (struct tcphdr *)((u8 *)ih + 4 * ih->ihl); 72 + if (th->dest != htons(tdata->packet->dport)) 73 + goto out; 74 + 75 + hdr = (struct netsfhdr *)((u8 *)th + sizeof(*th)); 76 + } else { 77 + if (ih->protocol != IPPROTO_UDP) 78 + goto out; 79 + 80 + uh = (struct udphdr *)((u8 *)ih + 4 * ih->ihl); 81 + if (uh->dest != htons(tdata->packet->dport)) 82 + goto out; 83 + 84 + hdr = (struct netsfhdr *)((u8 *)uh + sizeof(*uh)); 85 + } 86 + 87 + if (hdr->magic != cpu_to_be64(NET_TEST_PKT_MAGIC)) 88 + goto out; 89 + if (tdata->packet->id != hdr->id) 90 + goto out; 91 + 92 + tdata->ok = true; 93 + complete(&tdata->comp); 94 + out: 95 + kfree_skb(skb); 96 + return 0; 97 + } 98 + 99 + static int __xgbe_test_loopback(struct xgbe_prv_data *pdata, 100 + struct net_packet_attrs *attr) 101 + { 102 + struct net_test_priv *tdata; 103 + struct sk_buff *skb = NULL; 104 + int ret = 0; 105 + 106 + tdata = kzalloc(sizeof(*tdata), GFP_KERNEL); 107 + if (!tdata) 108 + return -ENOMEM; 109 + 110 + tdata->ok = false; 111 + init_completion(&tdata->comp); 112 + 113 + tdata->pt.type = htons(ETH_P_IP); 114 + tdata->pt.func = xgbe_test_loopback_validate; 115 + tdata->pt.dev = pdata->netdev; 116 + tdata->pt.af_packet_priv = tdata; 117 + tdata->packet = attr; 118 + 119 + dev_add_pack(&tdata->pt); 120 + 121 + skb = net_test_get_skb(pdata->netdev, xgbe_test_id, attr); 122 + if (!skb) { 123 + ret = -ENOMEM; 124 + goto cleanup; 125 + } 126 + 127 + xgbe_test_id++; 128 + ret = dev_direct_xmit(skb, attr->queue_mapping); 129 + if (ret) 130 + goto cleanup; 131 + 132 + if (!attr->timeout) 133 + attr->timeout = NET_LB_TIMEOUT; 134 + 135 + wait_for_completion_timeout(&tdata->comp, attr->timeout); 136 + ret = tdata->ok ? 0 : -ETIMEDOUT; 137 + 138 + if (ret) 139 + netdev_err(pdata->netdev, "Response timedout: ret %d\n", ret); 140 + cleanup: 141 + dev_remove_pack(&tdata->pt); 142 + kfree(tdata); 143 + return ret; 144 + } 145 + 146 + static int xgbe_test_mac_loopback(struct xgbe_prv_data *pdata) 147 + { 148 + struct net_packet_attrs attr = {}; 149 + 150 + attr.dst = pdata->netdev->dev_addr; 151 + return __xgbe_test_loopback(pdata, &attr); 152 + } 153 + 154 + static const struct xgbe_test xgbe_selftests[] = { 155 + { 156 + .name = "MAC Loopback ", 157 + .lb = XGBE_LOOPBACK_MAC, 158 + .fn = xgbe_test_mac_loopback, 159 + }, 160 + }; 161 + 162 + void xgbe_selftest_run(struct net_device *dev, 163 + struct ethtool_test *etest, u64 *buf) 164 + { 165 + struct xgbe_prv_data *pdata = netdev_priv(dev); 166 + int count = xgbe_selftest_get_count(pdata); 167 + int i, ret; 168 + 169 + memset(buf, 0, sizeof(*buf) * count); 170 + xgbe_test_id = 0; 171 + 172 + if (etest->flags != ETH_TEST_FL_OFFLINE) { 173 + netdev_err(pdata->netdev, "Only offline tests are supported\n"); 174 + etest->flags |= ETH_TEST_FL_FAILED; 175 + return; 176 + } else if (!netif_carrier_ok(dev)) { 177 + netdev_err(pdata->netdev, 178 + "Invalid link, cannot execute tests\n"); 179 + etest->flags |= ETH_TEST_FL_FAILED; 180 + return; 181 + } 182 + 183 + /* Wait for queues drain */ 184 + msleep(200); 185 + 186 + for (i = 0; i < count; i++) { 187 + ret = 0; 188 + 189 + switch (xgbe_selftests[i].lb) { 190 + case XGBE_LOOPBACK_MAC: 191 + ret = xgbe_enable_mac_loopback(pdata); 192 + break; 193 + case XGBE_LOOPBACK_NONE: 194 + break; 195 + default: 196 + ret = -EOPNOTSUPP; 197 + break; 198 + } 199 + 200 + /* 201 + * First tests will always be MAC / PHY loopback. 202 + * If any of them is not supported we abort earlier. 203 + */ 204 + if (ret) { 205 + netdev_err(pdata->netdev, "Loopback not supported\n"); 206 + etest->flags |= ETH_TEST_FL_FAILED; 207 + break; 208 + } 209 + 210 + ret = xgbe_selftests[i].fn(pdata); 211 + if (ret && (ret != -EOPNOTSUPP)) 212 + etest->flags |= ETH_TEST_FL_FAILED; 213 + buf[i] = ret; 214 + 215 + switch (xgbe_selftests[i].lb) { 216 + case XGBE_LOOPBACK_MAC: 217 + xgbe_disable_mac_loopback(pdata); 218 + break; 219 + default: 220 + break; 221 + } 222 + } 223 + } 224 + 225 + void xgbe_selftest_get_strings(struct xgbe_prv_data *pdata, u8 *data) 226 + { 227 + u8 *p = data; 228 + int i; 229 + 230 + for (i = 0; i < xgbe_selftest_get_count(pdata); i++) 231 + ethtool_puts(&p, xgbe_selftests[i].name); 232 + } 233 + 234 + int xgbe_selftest_get_count(struct xgbe_prv_data *pdata) 235 + { 236 + return ARRAY_SIZE(xgbe_selftests); 237 + }
+10
drivers/net/ethernet/amd/xgbe/xgbe.h
··· 1322 1322 int xgbe_pps_config(struct xgbe_prv_data *pdata, struct xgbe_pps_config *cfg, 1323 1323 int index, bool on); 1324 1324 1325 + /* Selftest functions */ 1326 + void xgbe_selftest_run(struct net_device *dev, 1327 + struct ethtool_test *etest, u64 *buf); 1328 + void xgbe_selftest_get_strings(struct xgbe_prv_data *pdata, u8 *data); 1329 + int xgbe_selftest_get_count(struct xgbe_prv_data *pdata); 1330 + 1331 + /* Loopback control */ 1332 + int xgbe_enable_mac_loopback(struct xgbe_prv_data *pdata); 1333 + void xgbe_disable_mac_loopback(struct xgbe_prv_data *pdata); 1334 + 1325 1335 #ifdef CONFIG_DEBUG_FS 1326 1336 void xgbe_debugfs_init(struct xgbe_prv_data *); 1327 1337 void xgbe_debugfs_exit(struct xgbe_prv_data *);