raw/ioat: add separate API for fence call
[dpdk.git] / drivers / raw / ioat / ioat_rawdev_test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <inttypes.h>
7 #include <rte_mbuf.h>
8 #include "rte_rawdev.h"
9 #include "rte_ioat_rawdev.h"
10
11 #define MAX_SUPPORTED_RAWDEVS 64
12 #define TEST_SKIPPED 77
13
14 int ioat_rawdev_test(uint16_t dev_id); /* pre-define to keep compiler happy */
15
16 static struct rte_mempool *pool;
17 static unsigned short expected_ring_size[MAX_SUPPORTED_RAWDEVS];
18
19 #define PRINT_ERR(...) print_err(__func__, __LINE__, __VA_ARGS__)
20
21 static inline int
22 __rte_format_printf(3, 4)
23 print_err(const char *func, int lineno, const char *format, ...)
24 {
25         va_list ap;
26         int ret;
27
28         ret = fprintf(stderr, "In %s:%d - ", func, lineno);
29         va_start(ap, format);
30         ret += vfprintf(stderr, format, ap);
31         va_end(ap);
32
33         return ret;
34 }
35
36 static int
37 test_enqueue_copies(int dev_id)
38 {
39         const unsigned int length = 1024;
40         unsigned int i;
41
42         do {
43                 struct rte_mbuf *src, *dst;
44                 char *src_data, *dst_data;
45                 struct rte_mbuf *completed[2] = {0};
46
47                 /* test doing a single copy */
48                 src = rte_pktmbuf_alloc(pool);
49                 dst = rte_pktmbuf_alloc(pool);
50                 src->data_len = src->pkt_len = length;
51                 dst->data_len = dst->pkt_len = length;
52                 src_data = rte_pktmbuf_mtod(src, char *);
53                 dst_data = rte_pktmbuf_mtod(dst, char *);
54
55                 for (i = 0; i < length; i++)
56                         src_data[i] = rand() & 0xFF;
57
58                 if (rte_ioat_enqueue_copy(dev_id,
59                                 src->buf_iova + src->data_off,
60                                 dst->buf_iova + dst->data_off,
61                                 length,
62                                 (uintptr_t)src,
63                                 (uintptr_t)dst) != 1) {
64                         PRINT_ERR("Error with rte_ioat_enqueue_copy\n");
65                         return -1;
66                 }
67                 rte_ioat_perform_ops(dev_id);
68                 usleep(10);
69
70                 if (rte_ioat_completed_ops(dev_id, 1, (void *)&completed[0],
71                                 (void *)&completed[1]) != 1) {
72                         PRINT_ERR("Error with rte_ioat_completed_ops\n");
73                         return -1;
74                 }
75                 if (completed[0] != src || completed[1] != dst) {
76                         PRINT_ERR("Error with completions: got (%p, %p), not (%p,%p)\n",
77                                         completed[0], completed[1], src, dst);
78                         return -1;
79                 }
80
81                 for (i = 0; i < length; i++)
82                         if (dst_data[i] != src_data[i]) {
83                                 PRINT_ERR("Data mismatch at char %u\n", i);
84                                 return -1;
85                         }
86                 rte_pktmbuf_free(src);
87                 rte_pktmbuf_free(dst);
88         } while (0);
89
90         /* test doing multiple copies */
91         do {
92                 struct rte_mbuf *srcs[32], *dsts[32];
93                 struct rte_mbuf *completed_src[64];
94                 struct rte_mbuf *completed_dst[64];
95                 unsigned int j;
96
97                 for (i = 0; i < RTE_DIM(srcs); i++) {
98                         char *src_data;
99
100                         srcs[i] = rte_pktmbuf_alloc(pool);
101                         dsts[i] = rte_pktmbuf_alloc(pool);
102                         srcs[i]->data_len = srcs[i]->pkt_len = length;
103                         dsts[i]->data_len = dsts[i]->pkt_len = length;
104                         src_data = rte_pktmbuf_mtod(srcs[i], char *);
105
106                         for (j = 0; j < length; j++)
107                                 src_data[j] = rand() & 0xFF;
108
109                         if (rte_ioat_enqueue_copy(dev_id,
110                                         srcs[i]->buf_iova + srcs[i]->data_off,
111                                         dsts[i]->buf_iova + dsts[i]->data_off,
112                                         length,
113                                         (uintptr_t)srcs[i],
114                                         (uintptr_t)dsts[i]) != 1) {
115                                 PRINT_ERR("Error with rte_ioat_enqueue_copy for buffer %u\n",
116                                                 i);
117                                 return -1;
118                         }
119                 }
120                 rte_ioat_perform_ops(dev_id);
121                 usleep(100);
122
123                 if (rte_ioat_completed_ops(dev_id, 64, (void *)completed_src,
124                                 (void *)completed_dst) != RTE_DIM(srcs)) {
125                         PRINT_ERR("Error with rte_ioat_completed_ops\n");
126                         return -1;
127                 }
128                 for (i = 0; i < RTE_DIM(srcs); i++) {
129                         char *src_data, *dst_data;
130
131                         if (completed_src[i] != srcs[i]) {
132                                 PRINT_ERR("Error with source pointer %u\n", i);
133                                 return -1;
134                         }
135                         if (completed_dst[i] != dsts[i]) {
136                                 PRINT_ERR("Error with dest pointer %u\n", i);
137                                 return -1;
138                         }
139
140                         src_data = rte_pktmbuf_mtod(srcs[i], char *);
141                         dst_data = rte_pktmbuf_mtod(dsts[i], char *);
142                         for (j = 0; j < length; j++)
143                                 if (src_data[j] != dst_data[j]) {
144                                         PRINT_ERR("Error with copy of packet %u, byte %u\n",
145                                                         i, j);
146                                         return -1;
147                                 }
148                         rte_pktmbuf_free(srcs[i]);
149                         rte_pktmbuf_free(dsts[i]);
150                 }
151
152         } while (0);
153
154         return 0;
155 }
156
157 int
158 ioat_rawdev_test(uint16_t dev_id)
159 {
160 #define IOAT_TEST_RINGSIZE 512
161         struct rte_ioat_rawdev_config p = { .ring_size = -1 };
162         struct rte_rawdev_info info = { .dev_private = &p };
163         struct rte_rawdev_xstats_name *snames = NULL;
164         uint64_t *stats = NULL;
165         unsigned int *ids = NULL;
166         unsigned int nb_xstats;
167         unsigned int i;
168
169         if (dev_id >= MAX_SUPPORTED_RAWDEVS) {
170                 printf("Skipping test. Cannot test rawdevs with id's greater than %d\n",
171                                 MAX_SUPPORTED_RAWDEVS);
172                 return TEST_SKIPPED;
173         }
174
175         rte_rawdev_info_get(dev_id, &info, sizeof(p));
176         if (p.ring_size != expected_ring_size[dev_id]) {
177                 PRINT_ERR("Error, initial ring size is not as expected (Actual: %d, Expected: %d)\n",
178                                 (int)p.ring_size, expected_ring_size[dev_id]);
179                 return -1;
180         }
181
182         p.ring_size = IOAT_TEST_RINGSIZE;
183         if (rte_rawdev_configure(dev_id, &info, sizeof(p)) != 0) {
184                 PRINT_ERR("Error with rte_rawdev_configure()\n");
185                 return -1;
186         }
187         rte_rawdev_info_get(dev_id, &info, sizeof(p));
188         if (p.ring_size != IOAT_TEST_RINGSIZE) {
189                 PRINT_ERR("Error, ring size is not %d (%d)\n",
190                                 IOAT_TEST_RINGSIZE, (int)p.ring_size);
191                 return -1;
192         }
193         expected_ring_size[dev_id] = p.ring_size;
194
195         if (rte_rawdev_start(dev_id) != 0) {
196                 PRINT_ERR("Error with rte_rawdev_start()\n");
197                 return -1;
198         }
199
200         pool = rte_pktmbuf_pool_create("TEST_IOAT_POOL",
201                         256, /* n == num elements */
202                         32,  /* cache size */
203                         0,   /* priv size */
204                         2048, /* data room size */
205                         info.socket_id);
206         if (pool == NULL) {
207                 PRINT_ERR("Error with mempool creation\n");
208                 return -1;
209         }
210
211         /* allocate memory for xstats names and values */
212         nb_xstats = rte_rawdev_xstats_names_get(dev_id, NULL, 0);
213
214         snames = malloc(sizeof(*snames) * nb_xstats);
215         if (snames == NULL) {
216                 PRINT_ERR("Error allocating xstat names memory\n");
217                 goto err;
218         }
219         rte_rawdev_xstats_names_get(dev_id, snames, nb_xstats);
220
221         ids = malloc(sizeof(*ids) * nb_xstats);
222         if (ids == NULL) {
223                 PRINT_ERR("Error allocating xstat ids memory\n");
224                 goto err;
225         }
226         for (i = 0; i < nb_xstats; i++)
227                 ids[i] = i;
228
229         stats = malloc(sizeof(*stats) * nb_xstats);
230         if (stats == NULL) {
231                 PRINT_ERR("Error allocating xstat memory\n");
232                 goto err;
233         }
234
235         /* run the test cases */
236         for (i = 0; i < 100; i++) {
237                 unsigned int j;
238
239                 if (test_enqueue_copies(dev_id) != 0)
240                         goto err;
241
242                 rte_rawdev_xstats_get(dev_id, ids, stats, nb_xstats);
243                 for (j = 0; j < nb_xstats; j++)
244                         printf("%s: %"PRIu64"   ", snames[j].name, stats[j]);
245                 printf("\r");
246         }
247         printf("\n");
248
249         rte_rawdev_stop(dev_id);
250         if (rte_rawdev_xstats_reset(dev_id, NULL, 0) != 0) {
251                 PRINT_ERR("Error resetting xstat values\n");
252                 goto err;
253         }
254
255         rte_mempool_free(pool);
256         free(snames);
257         free(stats);
258         free(ids);
259         return 0;
260
261 err:
262         rte_rawdev_stop(dev_id);
263         rte_rawdev_xstats_reset(dev_id, NULL, 0);
264         rte_mempool_free(pool);
265         free(snames);
266         free(stats);
267         free(ids);
268         return -1;
269 }