test/dma: add basic dmadev instance tests
[dpdk.git] / app / test / test_dmadev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021 HiSilicon Limited
3  * Copyright(c) 2021 Intel Corporation
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_dmadev.h>
9 #include <rte_bus_vdev.h>
10 #include <rte_dmadev_pmd.h>
11
12 #include "test.h"
13 #include "test_dmadev_api.h"
14
15 #define ERR_RETURN(...) do { print_err(__func__, __LINE__, __VA_ARGS__); return -1; } while (0)
16
17 static void
18 __rte_format_printf(3, 4)
19 print_err(const char *func, int lineno, const char *format, ...)
20 {
21         va_list ap;
22
23         fprintf(stderr, "In %s:%d - ", func, lineno);
24         va_start(ap, format);
25         vfprintf(stderr, format, ap);
26         va_end(ap);
27 }
28
29 static int
30 test_dmadev_instance(int16_t dev_id)
31 {
32 #define TEST_RINGSIZE 512
33         struct rte_dma_stats stats;
34         struct rte_dma_info info;
35         const struct rte_dma_conf conf = { .nb_vchans = 1};
36         const struct rte_dma_vchan_conf qconf = {
37                         .direction = RTE_DMA_DIR_MEM_TO_MEM,
38                         .nb_desc = TEST_RINGSIZE,
39         };
40         const int vchan = 0;
41
42         printf("\n### Test dmadev instance %u [%s]\n",
43                         dev_id, rte_dma_devices[dev_id].data->dev_name);
44
45         rte_dma_info_get(dev_id, &info);
46         if (info.max_vchans < 1)
47                 ERR_RETURN("Error, no channels available on device id %u\n", dev_id);
48
49         if (rte_dma_configure(dev_id, &conf) != 0)
50                 ERR_RETURN("Error with rte_dma_configure()\n");
51
52         if (rte_dma_vchan_setup(dev_id, vchan, &qconf) < 0)
53                 ERR_RETURN("Error with queue configuration\n");
54
55         rte_dma_info_get(dev_id, &info);
56         if (info.nb_vchans != 1)
57                 ERR_RETURN("Error, no configured queues reported on device id %u\n", dev_id);
58
59         if (rte_dma_start(dev_id) != 0)
60                 ERR_RETURN("Error with rte_dma_start()\n");
61
62         if (rte_dma_stats_get(dev_id, vchan, &stats) != 0)
63                 ERR_RETURN("Error with rte_dma_stats_get()\n");
64
65         if (stats.completed != 0 || stats.submitted != 0 || stats.errors != 0)
66                 ERR_RETURN("Error device stats are not all zero: completed = %"PRIu64", "
67                                 "submitted = %"PRIu64", errors = %"PRIu64"\n",
68                                 stats.completed, stats.submitted, stats.errors);
69
70         rte_dma_stop(dev_id);
71         rte_dma_stats_reset(dev_id, vchan);
72         return 0;
73 }
74
75 static int
76 test_apis(void)
77 {
78         const char *pmd = "dma_skeleton";
79         int id;
80         int ret;
81
82         if (rte_vdev_init(pmd, NULL) < 0)
83                 return TEST_SKIPPED;
84         id = rte_dma_get_dev_id_by_name(pmd);
85         if (id < 0)
86                 return TEST_SKIPPED;
87         printf("\n### Test dmadev infrastructure using skeleton driver\n");
88         ret = test_dma_api(id);
89         rte_vdev_uninit(pmd);
90
91         return ret;
92 }
93
94 static int
95 test_dma(void)
96 {
97         int i;
98
99         /* basic sanity on dmadev infrastructure */
100         if (test_apis() < 0)
101                 ERR_RETURN("Error performing API tests\n");
102
103         if (rte_dma_count_avail() == 0)
104                 return TEST_SKIPPED;
105
106         RTE_DMA_FOREACH_DEV(i)
107                 if (test_dmadev_instance(i) < 0)
108                         ERR_RETURN("Error, test failure for device %d\n", i);
109
110         return 0;
111 }
112
113 REGISTER_TEST_COMMAND(dmadev_autotest, test_dma);