]> git.apps.os.sepia.ceph.com Git - ceph-client.git/commitdiff
selftests: Move PCI Endpoint tests from tools/pci to Kselftests
authorManivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Thu, 16 Jan 2025 17:16:49 +0000 (22:46 +0530)
committerBjorn Helgaas <bhelgaas@google.com>
Tue, 21 Jan 2025 20:17:55 +0000 (14:17 -0600)
This just moves the existing tests under tools/pci to
tools/testing/selftests/pci_endpoint and adjusts the paths in Makefile
accordingly. Migration to Kselftest framework will be done in subsequent
commits.

Link: https://lore.kernel.org/r/20250116171650.33585-4-manivannan.sadhasivam@linaro.org
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Tested-by: Niklas Cassel <cassel@kernel.org>
Reviewed-by: Niklas Cassel <cassel@kernel.org>
Documentation/PCI/endpoint/pci-test-howto.rst
MAINTAINERS
tools/pci/Build [deleted file]
tools/pci/Makefile [deleted file]
tools/pci/pcitest.c [deleted file]
tools/pci/pcitest.sh [deleted file]
tools/testing/selftests/pci_endpoint/.gitignore [new file with mode: 0644]
tools/testing/selftests/pci_endpoint/Build [new file with mode: 0644]
tools/testing/selftests/pci_endpoint/Makefile [new file with mode: 0644]
tools/testing/selftests/pci_endpoint/pcitest.c [new file with mode: 0644]
tools/testing/selftests/pci_endpoint/pcitest.sh [new file with mode: 0644]

index 909f770a07d654683479e1b6357e6e9558d49516..c4ae7af50ede2870f86c25feeea792fbf1007952 100644 (file)
@@ -123,16 +123,17 @@ above::
 Using Endpoint Test function Device
 -----------------------------------
 
-pcitest.sh added in tools/pci/ can be used to run all the default PCI endpoint
-tests. To compile this tool the following commands should be used::
+pcitest.sh added in tools/testing/selftests/pci_endpoint can be used to run all
+the default PCI endpoint tests. To compile this tool the following commands
+should be used::
 
        # cd <kernel-dir>
-       # make -C tools/pci
+       # make -C tools/testing/selftests/pci_endpoint
 
 or if you desire to compile and install in your system::
 
        # cd <kernel-dir>
-       # make -C tools/pci install
+       # make -C tools/testing/selftests/pci_endpoint install
 
 The tool and script will be located in <rootfs>/usr/bin/
 
index 1e930c7a58b13d8bbe6bf133ba7b36aa24c2b5e0..0e611b845d50cc3b5ed12f7c50588bd2dd388de6 100644 (file)
@@ -18003,7 +18003,7 @@ F:      Documentation/PCI/endpoint/*
 F:     Documentation/misc-devices/pci-endpoint-test.rst
 F:     drivers/misc/pci_endpoint_test.c
 F:     drivers/pci/endpoint/
-F:     tools/pci/
+F:     tools/testing/selftests/pci_endpoint/
 
 PCI ENHANCED ERROR HANDLING (EEH) FOR POWERPC
 M:     Mahesh J Salgaonkar <mahesh@linux.ibm.com>
diff --git a/tools/pci/Build b/tools/pci/Build
deleted file mode 100644 (file)
index c375aea..0000000
+++ /dev/null
@@ -1 +0,0 @@
-pcitest-y += pcitest.o
diff --git a/tools/pci/Makefile b/tools/pci/Makefile
deleted file mode 100644 (file)
index 62d41f1..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-include ../scripts/Makefile.include
-
-bindir ?= /usr/bin
-
-ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-endif
-
-# Do not use make's built-in rules
-# (this improves performance and avoids hard-to-debug behaviour);
-MAKEFLAGS += -r
-
-CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
-
-ALL_TARGETS := pcitest
-ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
-
-SCRIPTS := pcitest.sh
-
-all: $(ALL_PROGRAMS)
-
-export srctree OUTPUT CC LD CFLAGS
-include $(srctree)/tools/build/Makefile.include
-
-#
-# We need the following to be outside of kernel tree
-#
-$(OUTPUT)include/linux/: ../../include/uapi/linux/
-       mkdir -p $(OUTPUT)include/linux/ 2>&1 || true
-       ln -sf $(CURDIR)/../../include/uapi/linux/pcitest.h $@
-
-prepare: $(OUTPUT)include/linux/
-
-PCITEST_IN := $(OUTPUT)pcitest-in.o
-$(PCITEST_IN): prepare FORCE
-       $(Q)$(MAKE) $(build)=pcitest
-$(OUTPUT)pcitest: $(PCITEST_IN)
-       $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
-
-clean:
-       rm -f $(ALL_PROGRAMS)
-       rm -rf $(OUTPUT)include/
-       find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
-
-install: $(ALL_PROGRAMS)
-       install -d -m 755 $(DESTDIR)$(bindir);          \
-       for program in $(ALL_PROGRAMS); do              \
-               install $$program $(DESTDIR)$(bindir);  \
-       done;                                           \
-       for script in $(SCRIPTS); do                    \
-               install $$script $(DESTDIR)$(bindir);   \
-       done
-
-FORCE:
-
-.PHONY: all install clean FORCE prepare
diff --git a/tools/pci/pcitest.c b/tools/pci/pcitest.c
deleted file mode 100644 (file)
index b96cc11..0000000
+++ /dev/null
@@ -1,265 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/**
- * Userspace PCI Endpoint Test Module
- *
- * Copyright (C) 2017 Texas Instruments
- * Author: Kishon Vijay Abraham I <kishon@ti.com>
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-
-#include <linux/pcitest.h>
-
-static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
-
-struct pci_test {
-       char            *device;
-       char            barnum;
-       bool            consecutive_bar_test;
-       bool            legacyirq;
-       unsigned int    msinum;
-       unsigned int    msixnum;
-       int             irqtype;
-       bool            set_irqtype;
-       bool            get_irqtype;
-       bool            clear_irq;
-       bool            read;
-       bool            write;
-       bool            copy;
-       unsigned long   size;
-       bool            use_dma;
-};
-
-static int run_test(struct pci_test *test)
-{
-       struct pci_endpoint_test_xfer_param param = {};
-       int ret = -EINVAL;
-       int fd;
-
-       fd = open(test->device, O_RDWR);
-       if (fd < 0) {
-               perror("can't open PCI Endpoint Test device");
-               return -ENODEV;
-       }
-
-       if (test->barnum >= 0 && test->barnum <= 5) {
-               ret = ioctl(fd, PCITEST_BAR, test->barnum);
-               fprintf(stdout, "BAR%d:\t\t", test->barnum);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->consecutive_bar_test) {
-               ret = ioctl(fd, PCITEST_BARS);
-               fprintf(stdout, "Consecutive BAR test:\t\t");
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->set_irqtype) {
-               ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
-               fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->get_irqtype) {
-               ret = ioctl(fd, PCITEST_GET_IRQTYPE);
-               fprintf(stdout, "GET IRQ TYPE:\t\t");
-               if (ret < 0) {
-                       fprintf(stdout, "NOT OKAY\n");
-               } else {
-                       fprintf(stdout, "%s\n", irq[ret]);
-                       ret = 0;
-               }
-       }
-
-       if (test->clear_irq) {
-               ret = ioctl(fd, PCITEST_CLEAR_IRQ);
-               fprintf(stdout, "CLEAR IRQ:\t\t");
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->legacyirq) {
-               ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
-               fprintf(stdout, "LEGACY IRQ:\t");
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->msinum > 0 && test->msinum <= 32) {
-               ret = ioctl(fd, PCITEST_MSI, test->msinum);
-               fprintf(stdout, "MSI%u:\t\t", test->msinum);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->msixnum > 0 && test->msixnum <= 2048) {
-               ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
-               fprintf(stdout, "MSI-X%u:\t\t", test->msixnum);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->write) {
-               param.size = test->size;
-               if (test->use_dma)
-                       param.flags = PCITEST_FLAGS_USE_DMA;
-               ret = ioctl(fd, PCITEST_WRITE, &param);
-               fprintf(stdout, "WRITE (%7lu bytes):\t\t", test->size);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->read) {
-               param.size = test->size;
-               if (test->use_dma)
-                       param.flags = PCITEST_FLAGS_USE_DMA;
-               ret = ioctl(fd, PCITEST_READ, &param);
-               fprintf(stdout, "READ (%7lu bytes):\t\t", test->size);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       if (test->copy) {
-               param.size = test->size;
-               if (test->use_dma)
-                       param.flags = PCITEST_FLAGS_USE_DMA;
-               ret = ioctl(fd, PCITEST_COPY, &param);
-               fprintf(stdout, "COPY (%7lu bytes):\t\t", test->size);
-               if (ret < 0)
-                       fprintf(stdout, "NOT OKAY\n");
-               else
-                       fprintf(stdout, "OKAY\n");
-       }
-
-       fflush(stdout);
-       close(fd);
-       return ret;
-}
-
-int main(int argc, char **argv)
-{
-       int c;
-       struct pci_test *test;
-
-       test = calloc(1, sizeof(*test));
-       if (!test) {
-               perror("Fail to allocate memory for pci_test\n");
-               return -ENOMEM;
-       }
-
-       /* since '0' is a valid BAR number, initialize it to -1 */
-       test->barnum = -1;
-
-       /* set default size as 100KB */
-       test->size = 0x19000;
-
-       /* set default endpoint device */
-       test->device = "/dev/pci-endpoint-test.0";
-
-       while ((c = getopt(argc, argv, "D:b:Cm:x:i:deIlhrwcs:")) != EOF)
-       switch (c) {
-       case 'D':
-               test->device = optarg;
-               continue;
-       case 'b':
-               test->barnum = atoi(optarg);
-               if (test->barnum < 0 || test->barnum > 5)
-                       goto usage;
-               continue;
-       case 'C':
-               test->consecutive_bar_test = true;
-               continue;
-       case 'l':
-               test->legacyirq = true;
-               continue;
-       case 'm':
-               test->msinum = atoi(optarg);
-               if (test->msinum < 1 || test->msinum > 32)
-                       goto usage;
-               continue;
-       case 'x':
-               test->msixnum = atoi(optarg);
-               if (test->msixnum < 1 || test->msixnum > 2048)
-                       goto usage;
-               continue;
-       case 'i':
-               test->irqtype = atoi(optarg);
-               if (test->irqtype < 0 || test->irqtype > 2)
-                       goto usage;
-               test->set_irqtype = true;
-               continue;
-       case 'I':
-               test->get_irqtype = true;
-               continue;
-       case 'r':
-               test->read = true;
-               continue;
-       case 'w':
-               test->write = true;
-               continue;
-       case 'c':
-               test->copy = true;
-               continue;
-       case 'e':
-               test->clear_irq = true;
-               continue;
-       case 's':
-               test->size = strtoul(optarg, NULL, 0);
-               continue;
-       case 'd':
-               test->use_dma = true;
-               continue;
-       case 'h':
-       default:
-usage:
-               fprintf(stderr,
-                       "usage: %s [options]\n"
-                       "Options:\n"
-                       "\t-D <dev>             PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
-                       "\t-b <bar num>         BAR test (bar number between 0..5)\n"
-                       "\t-C                   Consecutive BAR test\n"
-                       "\t-m <msi num>         MSI test (msi number between 1..32)\n"
-                       "\t-x <msix num>        \tMSI-X test (msix number between 1..2048)\n"
-                       "\t-i <irq type>        \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
-                       "\t-e                   Clear IRQ\n"
-                       "\t-I                   Get current IRQ type configured\n"
-                       "\t-d                   Use DMA\n"
-                       "\t-l                   Legacy IRQ test\n"
-                       "\t-r                   Read buffer test\n"
-                       "\t-w                   Write buffer test\n"
-                       "\t-c                   Copy buffer test\n"
-                       "\t-s <size>            Size of buffer {default: 100KB}\n"
-                       "\t-h                   Print this help message\n",
-                       argv[0]);
-               return -EINVAL;
-       }
-
-       return run_test(test);
-}
diff --git a/tools/pci/pcitest.sh b/tools/pci/pcitest.sh
deleted file mode 100644 (file)
index 770f4d6..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-
-echo "BAR tests"
-echo
-
-bar=0
-
-while [ $bar -lt 6 ]
-do
-       pcitest -b $bar
-       bar=`expr $bar + 1`
-done
-pcitest -C
-echo
-
-echo "Interrupt tests"
-echo
-
-pcitest -i 0
-pcitest -l
-
-pcitest -i 1
-msi=1
-
-while [ $msi -lt 33 ]
-do
-        pcitest -m $msi
-        msi=`expr $msi + 1`
-done
-echo
-
-pcitest -i 2
-msix=1
-
-while [ $msix -lt 2049 ]
-do
-        pcitest -x $msix
-        msix=`expr $msix + 1`
-done
-echo
-
-echo "Read Tests"
-echo
-
-pcitest -i 1
-
-pcitest -r -s 1
-pcitest -r -s 1024
-pcitest -r -s 1025
-pcitest -r -s 1024000
-pcitest -r -s 1024001
-echo
-
-echo "Write Tests"
-echo
-
-pcitest -w -s 1
-pcitest -w -s 1024
-pcitest -w -s 1025
-pcitest -w -s 1024000
-pcitest -w -s 1024001
-echo
-
-echo "Copy Tests"
-echo
-
-pcitest -c -s 1
-pcitest -c -s 1024
-pcitest -c -s 1025
-pcitest -c -s 1024000
-pcitest -c -s 1024001
-echo
diff --git a/tools/testing/selftests/pci_endpoint/.gitignore b/tools/testing/selftests/pci_endpoint/.gitignore
new file mode 100644 (file)
index 0000000..29ab47c
--- /dev/null
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+*.o
+pcitest
diff --git a/tools/testing/selftests/pci_endpoint/Build b/tools/testing/selftests/pci_endpoint/Build
new file mode 100644 (file)
index 0000000..c375aea
--- /dev/null
@@ -0,0 +1 @@
+pcitest-y += pcitest.o
diff --git a/tools/testing/selftests/pci_endpoint/Makefile b/tools/testing/selftests/pci_endpoint/Makefile
new file mode 100644 (file)
index 0000000..3c6fe18
--- /dev/null
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: GPL-2.0
+include ../../../scripts/Makefile.include
+
+bindir ?= /usr/bin
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
+endif
+
+# Do not use make's built-in rules
+# (this improves performance and avoids hard-to-debug behaviour);
+MAKEFLAGS += -r
+
+CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+
+ALL_TARGETS := pcitest
+ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
+
+SCRIPTS := pcitest.sh
+
+all: $(ALL_PROGRAMS)
+
+export srctree OUTPUT CC LD CFLAGS
+include $(srctree)/tools/build/Makefile.include
+
+#
+# We need the following to be outside of kernel tree
+#
+$(OUTPUT)include/linux/: ../../../../include/uapi/linux/
+       mkdir -p $(OUTPUT)include/linux/ 2>&1 || true
+       ln -sf $(CURDIR)/../../../../include/uapi/linux/pcitest.h $@
+
+$(info ${CURDIR})
+prepare: $(OUTPUT)include/linux/
+
+PCITEST_IN := $(OUTPUT)pcitest-in.o
+$(PCITEST_IN): prepare FORCE
+       $(Q)$(MAKE) $(build)=pcitest
+$(OUTPUT)pcitest: $(PCITEST_IN)
+       $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
+
+clean:
+       rm -f $(ALL_PROGRAMS)
+       rm -rf $(OUTPUT)include/
+       find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
+
+install: $(ALL_PROGRAMS)
+       install -d -m 755 $(DESTDIR)$(bindir);          \
+       for program in $(ALL_PROGRAMS); do              \
+               install $$program $(DESTDIR)$(bindir);  \
+       done;                                           \
+       for script in $(SCRIPTS); do                    \
+               install $$script $(DESTDIR)$(bindir);   \
+       done
+
+FORCE:
+
+.PHONY: all install clean FORCE prepare
diff --git a/tools/testing/selftests/pci_endpoint/pcitest.c b/tools/testing/selftests/pci_endpoint/pcitest.c
new file mode 100644 (file)
index 0000000..b96cc11
--- /dev/null
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/**
+ * Userspace PCI Endpoint Test Module
+ *
+ * Copyright (C) 2017 Texas Instruments
+ * Author: Kishon Vijay Abraham I <kishon@ti.com>
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <linux/pcitest.h>
+
+static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
+
+struct pci_test {
+       char            *device;
+       char            barnum;
+       bool            consecutive_bar_test;
+       bool            legacyirq;
+       unsigned int    msinum;
+       unsigned int    msixnum;
+       int             irqtype;
+       bool            set_irqtype;
+       bool            get_irqtype;
+       bool            clear_irq;
+       bool            read;
+       bool            write;
+       bool            copy;
+       unsigned long   size;
+       bool            use_dma;
+};
+
+static int run_test(struct pci_test *test)
+{
+       struct pci_endpoint_test_xfer_param param = {};
+       int ret = -EINVAL;
+       int fd;
+
+       fd = open(test->device, O_RDWR);
+       if (fd < 0) {
+               perror("can't open PCI Endpoint Test device");
+               return -ENODEV;
+       }
+
+       if (test->barnum >= 0 && test->barnum <= 5) {
+               ret = ioctl(fd, PCITEST_BAR, test->barnum);
+               fprintf(stdout, "BAR%d:\t\t", test->barnum);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->consecutive_bar_test) {
+               ret = ioctl(fd, PCITEST_BARS);
+               fprintf(stdout, "Consecutive BAR test:\t\t");
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->set_irqtype) {
+               ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
+               fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->get_irqtype) {
+               ret = ioctl(fd, PCITEST_GET_IRQTYPE);
+               fprintf(stdout, "GET IRQ TYPE:\t\t");
+               if (ret < 0) {
+                       fprintf(stdout, "NOT OKAY\n");
+               } else {
+                       fprintf(stdout, "%s\n", irq[ret]);
+                       ret = 0;
+               }
+       }
+
+       if (test->clear_irq) {
+               ret = ioctl(fd, PCITEST_CLEAR_IRQ);
+               fprintf(stdout, "CLEAR IRQ:\t\t");
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->legacyirq) {
+               ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
+               fprintf(stdout, "LEGACY IRQ:\t");
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->msinum > 0 && test->msinum <= 32) {
+               ret = ioctl(fd, PCITEST_MSI, test->msinum);
+               fprintf(stdout, "MSI%u:\t\t", test->msinum);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->msixnum > 0 && test->msixnum <= 2048) {
+               ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
+               fprintf(stdout, "MSI-X%u:\t\t", test->msixnum);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->write) {
+               param.size = test->size;
+               if (test->use_dma)
+                       param.flags = PCITEST_FLAGS_USE_DMA;
+               ret = ioctl(fd, PCITEST_WRITE, &param);
+               fprintf(stdout, "WRITE (%7lu bytes):\t\t", test->size);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->read) {
+               param.size = test->size;
+               if (test->use_dma)
+                       param.flags = PCITEST_FLAGS_USE_DMA;
+               ret = ioctl(fd, PCITEST_READ, &param);
+               fprintf(stdout, "READ (%7lu bytes):\t\t", test->size);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       if (test->copy) {
+               param.size = test->size;
+               if (test->use_dma)
+                       param.flags = PCITEST_FLAGS_USE_DMA;
+               ret = ioctl(fd, PCITEST_COPY, &param);
+               fprintf(stdout, "COPY (%7lu bytes):\t\t", test->size);
+               if (ret < 0)
+                       fprintf(stdout, "NOT OKAY\n");
+               else
+                       fprintf(stdout, "OKAY\n");
+       }
+
+       fflush(stdout);
+       close(fd);
+       return ret;
+}
+
+int main(int argc, char **argv)
+{
+       int c;
+       struct pci_test *test;
+
+       test = calloc(1, sizeof(*test));
+       if (!test) {
+               perror("Fail to allocate memory for pci_test\n");
+               return -ENOMEM;
+       }
+
+       /* since '0' is a valid BAR number, initialize it to -1 */
+       test->barnum = -1;
+
+       /* set default size as 100KB */
+       test->size = 0x19000;
+
+       /* set default endpoint device */
+       test->device = "/dev/pci-endpoint-test.0";
+
+       while ((c = getopt(argc, argv, "D:b:Cm:x:i:deIlhrwcs:")) != EOF)
+       switch (c) {
+       case 'D':
+               test->device = optarg;
+               continue;
+       case 'b':
+               test->barnum = atoi(optarg);
+               if (test->barnum < 0 || test->barnum > 5)
+                       goto usage;
+               continue;
+       case 'C':
+               test->consecutive_bar_test = true;
+               continue;
+       case 'l':
+               test->legacyirq = true;
+               continue;
+       case 'm':
+               test->msinum = atoi(optarg);
+               if (test->msinum < 1 || test->msinum > 32)
+                       goto usage;
+               continue;
+       case 'x':
+               test->msixnum = atoi(optarg);
+               if (test->msixnum < 1 || test->msixnum > 2048)
+                       goto usage;
+               continue;
+       case 'i':
+               test->irqtype = atoi(optarg);
+               if (test->irqtype < 0 || test->irqtype > 2)
+                       goto usage;
+               test->set_irqtype = true;
+               continue;
+       case 'I':
+               test->get_irqtype = true;
+               continue;
+       case 'r':
+               test->read = true;
+               continue;
+       case 'w':
+               test->write = true;
+               continue;
+       case 'c':
+               test->copy = true;
+               continue;
+       case 'e':
+               test->clear_irq = true;
+               continue;
+       case 's':
+               test->size = strtoul(optarg, NULL, 0);
+               continue;
+       case 'd':
+               test->use_dma = true;
+               continue;
+       case 'h':
+       default:
+usage:
+               fprintf(stderr,
+                       "usage: %s [options]\n"
+                       "Options:\n"
+                       "\t-D <dev>             PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
+                       "\t-b <bar num>         BAR test (bar number between 0..5)\n"
+                       "\t-C                   Consecutive BAR test\n"
+                       "\t-m <msi num>         MSI test (msi number between 1..32)\n"
+                       "\t-x <msix num>        \tMSI-X test (msix number between 1..2048)\n"
+                       "\t-i <irq type>        \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
+                       "\t-e                   Clear IRQ\n"
+                       "\t-I                   Get current IRQ type configured\n"
+                       "\t-d                   Use DMA\n"
+                       "\t-l                   Legacy IRQ test\n"
+                       "\t-r                   Read buffer test\n"
+                       "\t-w                   Write buffer test\n"
+                       "\t-c                   Copy buffer test\n"
+                       "\t-s <size>            Size of buffer {default: 100KB}\n"
+                       "\t-h                   Print this help message\n",
+                       argv[0]);
+               return -EINVAL;
+       }
+
+       return run_test(test);
+}
diff --git a/tools/testing/selftests/pci_endpoint/pcitest.sh b/tools/testing/selftests/pci_endpoint/pcitest.sh
new file mode 100644 (file)
index 0000000..770f4d6
--- /dev/null
@@ -0,0 +1,73 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+echo "BAR tests"
+echo
+
+bar=0
+
+while [ $bar -lt 6 ]
+do
+       pcitest -b $bar
+       bar=`expr $bar + 1`
+done
+pcitest -C
+echo
+
+echo "Interrupt tests"
+echo
+
+pcitest -i 0
+pcitest -l
+
+pcitest -i 1
+msi=1
+
+while [ $msi -lt 33 ]
+do
+        pcitest -m $msi
+        msi=`expr $msi + 1`
+done
+echo
+
+pcitest -i 2
+msix=1
+
+while [ $msix -lt 2049 ]
+do
+        pcitest -x $msix
+        msix=`expr $msix + 1`
+done
+echo
+
+echo "Read Tests"
+echo
+
+pcitest -i 1
+
+pcitest -r -s 1
+pcitest -r -s 1024
+pcitest -r -s 1025
+pcitest -r -s 1024000
+pcitest -r -s 1024001
+echo
+
+echo "Write Tests"
+echo
+
+pcitest -w -s 1
+pcitest -w -s 1024
+pcitest -w -s 1025
+pcitest -w -s 1024000
+pcitest -w -s 1024001
+echo
+
+echo "Copy Tests"
+echo
+
+pcitest -c -s 1
+pcitest -c -s 1024
+pcitest -c -s 1025
+pcitest -c -s 1024000
+pcitest -c -s 1024001
+echo