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