New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / raw / skeleton_rawdev / skeleton_rawdev_test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 NXP
3  */
4
5 #include <rte_common.h>
6 #include <rte_mbuf.h>
7 #include <rte_malloc.h>
8 #include <rte_memcpy.h>
9 #include <rte_dev.h>
10 #include <rte_rawdev.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_test.h>
13
14 /* Using relative path as skeleton_rawdev is not part of exported headers */
15 #include "skeleton_rawdev.h"
16
17 #define TEST_DEV_ID   0
18 #define TEST_DEV_NAME "rawdev_skeleton"
19
20 #define SKELDEV_LOGS(level, fmt, args...) \
21         rte_log(RTE_LOG_ ## level, skeleton_pmd_logtype, fmt "\n", \
22                 ##args)
23
24 #define SKELDEV_TEST_INFO(fmt, args...) \
25         SKELDEV_LOGS(INFO, fmt, ## args)
26 #define SKELDEV_TEST_DEBUG(fmt, args...) \
27         SKELDEV_LOGS(DEBUG, fmt, ## args)
28
29 #define SKELDEV_TEST_RUN(setup, teardown, test) \
30         skeldev_test_run(setup, teardown, test, #test)
31
32 #define TEST_SUCCESS 0
33 #define TEST_FAILED  -1
34
35 static int total;
36 static int passed;
37 static int failed;
38 static int unsupported;
39
40 static int
41 testsuite_setup(void)
42 {
43         uint8_t count;
44         count = rte_rawdev_count();
45         if (!count) {
46                 SKELDEV_TEST_INFO("\tNo existing rawdev; "
47                                   "Creating 'skeldev_rawdev'");
48                 return rte_vdev_init(TEST_DEV_NAME, NULL);
49         }
50
51         return TEST_SUCCESS;
52 }
53
54 static void local_teardown(void);
55
56 static void
57 testsuite_teardown(void)
58 {
59         local_teardown();
60 }
61
62 static void
63 local_teardown(void)
64 {
65         rte_vdev_uninit(TEST_DEV_NAME);
66 }
67
68 static int
69 test_rawdev_count(void)
70 {
71         uint8_t count;
72         count = rte_rawdev_count();
73         RTE_TEST_ASSERT(count > 0, "Invalid rawdev count %" PRIu8, count);
74         return TEST_SUCCESS;
75 }
76
77 static int
78 test_rawdev_get_dev_id(void)
79 {
80         int ret;
81         ret = rte_rawdev_get_dev_id("invalid_rawdev_device");
82         RTE_TEST_ASSERT_FAIL(ret, "Expected <0 for invalid dev name ret=%d",
83                              ret);
84         return TEST_SUCCESS;
85 }
86
87 static int
88 test_rawdev_socket_id(void)
89 {
90         int socket_id;
91         socket_id = rte_rawdev_socket_id(TEST_DEV_ID);
92         RTE_TEST_ASSERT(socket_id != -EINVAL,
93                         "Failed to get socket_id %d", socket_id);
94         socket_id = rte_rawdev_socket_id(RTE_RAWDEV_MAX_DEVS);
95         RTE_TEST_ASSERT(socket_id == -EINVAL,
96                         "Expected -EINVAL %d", socket_id);
97
98         return TEST_SUCCESS;
99 }
100
101 static int
102 test_rawdev_info_get(void)
103 {
104         int ret;
105         struct rte_rawdev_info rdev_info = {0};
106         struct skeleton_rawdev_conf skel_conf = {0};
107
108         ret = rte_rawdev_info_get(TEST_DEV_ID, NULL);
109         RTE_TEST_ASSERT(ret == -EINVAL, "Expected -EINVAL, %d", ret);
110
111         rdev_info.dev_private = &skel_conf;
112
113         ret = rte_rawdev_info_get(TEST_DEV_ID, &rdev_info);
114         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get raw dev info");
115
116         return TEST_SUCCESS;
117 }
118
119 static int
120 test_rawdev_configure(void)
121 {
122         int ret;
123         struct rte_rawdev_info rdev_info = {0};
124         struct skeleton_rawdev_conf rdev_conf_set = {0};
125         struct skeleton_rawdev_conf rdev_conf_get = {0};
126
127         /* Check invalid configuration */
128         ret = rte_rawdev_configure(TEST_DEV_ID, NULL);
129         RTE_TEST_ASSERT(ret == -EINVAL,
130                         "Null configure; Expected -EINVAL, got %d", ret);
131
132         /* Valid configuration test */
133         rdev_conf_set.num_queues = 1;
134         rdev_conf_set.capabilities = SKELETON_CAPA_FW_LOAD |
135                                      SKELETON_CAPA_FW_RESET;
136
137         rdev_info.dev_private = &rdev_conf_set;
138         ret = rte_rawdev_configure(TEST_DEV_ID,
139                                    (rte_rawdev_obj_t)&rdev_info);
140         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to configure rawdev (%d)", ret);
141
142         rdev_info.dev_private = &rdev_conf_get;
143         ret = rte_rawdev_info_get(TEST_DEV_ID,
144                                   (rte_rawdev_obj_t)&rdev_info);
145         RTE_TEST_ASSERT_SUCCESS(ret,
146                                 "Failed to obtain rawdev configuration (%d)",
147                                 ret);
148
149         RTE_TEST_ASSERT_EQUAL(rdev_conf_set.num_queues,
150                               rdev_conf_get.num_queues,
151                               "Configuration test failed; num_queues (%d)(%d)",
152                               rdev_conf_set.num_queues,
153                               rdev_conf_get.num_queues);
154         RTE_TEST_ASSERT_EQUAL(rdev_conf_set.capabilities,
155                           rdev_conf_get.capabilities,
156                           "Configuration test failed; capabilities");
157
158         return TEST_SUCCESS;
159 }
160
161 static int
162 test_rawdev_queue_default_conf_get(void)
163 {
164         int ret, i;
165         struct rte_rawdev_info rdev_info = {0};
166         struct skeleton_rawdev_conf rdev_conf_get = {0};
167         struct skeleton_rawdev_queue q = {0};
168
169         /* Get the current configuration */
170         rdev_info.dev_private = &rdev_conf_get;
171         ret = rte_rawdev_info_get(TEST_DEV_ID,
172                                   (rte_rawdev_obj_t)&rdev_info);
173         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to obtain rawdev configuration (%d)",
174                                 ret);
175
176         /* call to test_rawdev_configure would have set the num_queues = 1 */
177         RTE_TEST_ASSERT_SUCCESS(!(rdev_conf_get.num_queues > 0),
178                                 "Invalid number of queues (%d). Expected 1",
179                                 rdev_conf_get.num_queues);
180         /* All queues by default should have state = DETACH and
181          * depth = DEF_DEPTH
182          */
183         for (i = 0; i < rdev_conf_get.num_queues; i++) {
184                 rte_rawdev_queue_conf_get(TEST_DEV_ID, i, &q);
185                 RTE_TEST_ASSERT_EQUAL(q.depth, SKELETON_QUEUE_DEF_DEPTH,
186                                       "Invalid default depth of queue (%d)",
187                                       q.depth);
188                 RTE_TEST_ASSERT_EQUAL(q.state, SKELETON_QUEUE_DETACH,
189                                       "Invalid default state of queue (%d)",
190                                       q.state);
191         }
192
193         return TEST_SUCCESS;
194 }
195
196 static int
197 test_rawdev_queue_count(void)
198 {
199         unsigned int q_count;
200
201         /* Get the current configuration */
202         q_count = rte_rawdev_queue_count(TEST_DEV_ID);
203         RTE_TEST_ASSERT_EQUAL(q_count, 1, "Invalid queue count (%d)", q_count);
204
205         return TEST_SUCCESS;
206 }
207
208 static int
209 test_rawdev_queue_setup(void)
210 {
211         int ret;
212         struct rte_rawdev_info rdev_info = {0};
213         struct skeleton_rawdev_conf rdev_conf_get = {0};
214         struct skeleton_rawdev_queue qset = {0};
215         struct skeleton_rawdev_queue qget = {0};
216
217         /* Get the current configuration */
218         rdev_info.dev_private = &rdev_conf_get;
219         ret = rte_rawdev_info_get(TEST_DEV_ID,
220                                   (rte_rawdev_obj_t)&rdev_info);
221         RTE_TEST_ASSERT_SUCCESS(ret,
222                                 "Failed to obtain rawdev configuration (%d)",
223                                 ret);
224
225         /* call to test_rawdev_configure would have set the num_queues = 1 */
226         RTE_TEST_ASSERT_SUCCESS(!(rdev_conf_get.num_queues > 0),
227                                 "Invalid number of queues (%d). Expected 1",
228                                 rdev_conf_get.num_queues);
229
230         /* Modify the queue depth for Queue 0 and attach it */
231         qset.depth = 15;
232         qset.state = SKELETON_QUEUE_ATTACH;
233         ret = rte_rawdev_queue_setup(TEST_DEV_ID, 0, &qset);
234         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to setup queue (%d)", ret);
235
236         /* Now, fetching the queue 0 should show depth as 15 */
237         ret = rte_rawdev_queue_conf_get(TEST_DEV_ID, 0, &qget);
238         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get queue config (%d)", ret);
239
240         RTE_TEST_ASSERT_EQUAL(qset.depth, qget.depth,
241                               "Failed to set queue depth: Need(%d), has(%d)",
242                               qset.depth, qget.depth);
243
244         return TEST_SUCCESS;
245 }
246
247 /* After executing test_rawdev_queue_setup, queue_id=0 would have depth as 15.
248  * Releasing should set it back to default. state would set to DETACH
249  */
250 static int
251 test_rawdev_queue_release(void)
252 {
253         int ret;
254         struct skeleton_rawdev_queue qget = {0};
255
256         /* Now, fetching the queue 0 should show depth as 100 */
257         ret = rte_rawdev_queue_release(TEST_DEV_ID, 0);
258         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to release queue 0; (%d)", ret);
259
260         /* Now, fetching the queue 0 should show depth as default */
261         ret = rte_rawdev_queue_conf_get(TEST_DEV_ID, 0, &qget);
262         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get queue config (%d)", ret);
263
264         RTE_TEST_ASSERT_EQUAL(qget.depth, SKELETON_QUEUE_DEF_DEPTH,
265                               "Release of Queue 0 failed; (depth)");
266
267         RTE_TEST_ASSERT_EQUAL(qget.state, SKELETON_QUEUE_DETACH,
268                               "Release of Queue 0 failed; (state)");
269
270         return TEST_SUCCESS;
271 }
272
273 static int
274 test_rawdev_attr_set_get(void)
275 {
276         int ret;
277         int *dummy_value;
278         uint64_t ret_value;
279
280         /* Set an attribute and fetch it */
281         ret = rte_rawdev_set_attr(TEST_DEV_ID, "Test1", 100);
282         RTE_TEST_ASSERT(!ret, "Unable to set an attribute (Test1)");
283
284         dummy_value = malloc(sizeof(int));
285         if (!dummy_value)
286                 RTE_TEST_ASSERT(1, "Unable to allocate memory (dummy_value)");
287
288         *dummy_value = 200;
289         ret = rte_rawdev_set_attr(TEST_DEV_ID, "Test2", (uintptr_t)dummy_value);
290
291         /* Check if attributes have been set */
292         ret = rte_rawdev_get_attr(TEST_DEV_ID, "Test1", &ret_value);
293         RTE_TEST_ASSERT_EQUAL(ret_value, 100,
294                               "Attribute (Test1) not set correctly (%" PRIu64 ")",
295                               ret_value);
296
297         free(dummy_value);
298
299         ret_value = 0;
300         ret = rte_rawdev_get_attr(TEST_DEV_ID, "Test2", &ret_value);
301         RTE_TEST_ASSERT_EQUAL(*((int *)(uintptr_t)ret_value), 200,
302                               "Attribute (Test2) not set correctly (%" PRIu64 ")",
303                               ret_value);
304
305         return TEST_SUCCESS;
306 }
307
308 static int
309 test_rawdev_start_stop(void)
310 {
311         int ret;
312         struct rte_rawdev_info rdev_info = {0};
313         struct skeleton_rawdev_conf rdev_conf_get = {0};
314         char *dummy_firmware = NULL;
315
316         /* Get the current configuration */
317         rdev_info.dev_private = &rdev_conf_get;
318
319         /* Load a firmware using a dummy address area */
320         dummy_firmware = rte_zmalloc("RAWDEV SKELETON", sizeof(int) * 10, 0);
321         RTE_TEST_ASSERT(dummy_firmware != NULL,
322                         "Failed to create firmware memory backing");
323
324         ret = rte_rawdev_firmware_load(TEST_DEV_ID, dummy_firmware);
325         RTE_TEST_ASSERT_SUCCESS(ret, "Firmware loading failed (%d)", ret);
326
327         /* Skeleton doesn't do anything with the firmware area - that is dummy
328          * and can be removed.
329          */
330         rte_free(dummy_firmware);
331         dummy_firmware = NULL;
332
333         rte_rawdev_start(TEST_DEV_ID);
334         ret = rte_rawdev_info_get(TEST_DEV_ID, (rte_rawdev_obj_t)&rdev_info);
335         RTE_TEST_ASSERT_SUCCESS(ret,
336                                 "Failed to obtain rawdev configuration (%d)",
337                                 ret);
338         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_RUNNING,
339                               "Device start failed. State is (%d)",
340                               rdev_conf_get.device_state);
341
342         rte_rawdev_stop(TEST_DEV_ID);
343         ret = rte_rawdev_info_get(TEST_DEV_ID, (rte_rawdev_obj_t)&rdev_info);
344         RTE_TEST_ASSERT_SUCCESS(ret,
345                                 "Failed to obtain rawdev configuration (%d)",
346                                 ret);
347         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_STOPPED,
348                               "Device stop failed. State is (%d)",
349                               rdev_conf_get.device_state);
350
351         /* Unloading the firmware once device is stopped */
352         ret = rte_rawdev_firmware_unload(TEST_DEV_ID);
353         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to unload firmware (%d)", ret);
354
355         return TEST_SUCCESS;
356 }
357
358 static int
359 test_rawdev_enqdeq(void)
360 {
361         int ret;
362         unsigned int count = 1;
363         uint16_t queue_id = 0;
364         struct rte_rawdev_buf buffers[1];
365         struct rte_rawdev_buf *deq_buffers = NULL;
366
367         buffers[0].buf_addr = malloc(strlen(TEST_DEV_NAME) + 3);
368         if (!buffers[0].buf_addr)
369                 goto cleanup;
370         snprintf(buffers[0].buf_addr, strlen(TEST_DEV_NAME) + 2, "%s%d",
371                  TEST_DEV_NAME, 0);
372
373         ret = rte_rawdev_enqueue_buffers(TEST_DEV_ID,
374                                          (struct rte_rawdev_buf **)&buffers,
375                                          count, &queue_id);
376         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
377                               "Unable to enqueue buffers");
378
379         deq_buffers = malloc(sizeof(struct rte_rawdev_buf) * count);
380         if (!deq_buffers)
381                 goto cleanup;
382
383         ret = rte_rawdev_dequeue_buffers(TEST_DEV_ID,
384                                         (struct rte_rawdev_buf **)&deq_buffers,
385                                         count, &queue_id);
386         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
387                               "Unable to dequeue buffers");
388
389         if (deq_buffers)
390                 free(deq_buffers);
391
392         return TEST_SUCCESS;
393 cleanup:
394         if (buffers[0].buf_addr)
395                 free(buffers[0].buf_addr);
396
397         return TEST_FAILED;
398 }
399
400 static void skeldev_test_run(int (*setup)(void),
401                              void (*teardown)(void),
402                              int (*test)(void),
403                              const char *name)
404 {
405         int ret = 0;
406
407         if (setup) {
408                 ret = setup();
409                 if (ret < 0) {
410                         SKELDEV_TEST_INFO("Error setting up test %s", name);
411                         unsupported++;
412                 }
413         }
414
415         if (test) {
416                 ret = test();
417                 if (ret < 0) {
418                         failed++;
419                         SKELDEV_TEST_INFO("%s Failed", name);
420                 } else {
421                         passed++;
422                         SKELDEV_TEST_DEBUG("%s Passed", name);
423                 }
424         }
425
426         if (teardown)
427                 teardown();
428
429         total++;
430 }
431
432 int
433 test_rawdev_skeldev(void)
434 {
435         testsuite_setup();
436
437         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_count);
438         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_get_dev_id);
439         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_socket_id);
440         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_info_get);
441         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_configure);
442         SKELDEV_TEST_RUN(test_rawdev_configure, NULL,
443                          test_rawdev_queue_default_conf_get);
444         SKELDEV_TEST_RUN(test_rawdev_configure, NULL, test_rawdev_queue_setup);
445         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_queue_count);
446         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL,
447                          test_rawdev_queue_release);
448         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_attr_set_get);
449         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_start_stop);
450         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL, test_rawdev_enqdeq);
451
452         testsuite_teardown();
453
454         SKELDEV_TEST_INFO("Total tests   : %d", total);
455         SKELDEV_TEST_INFO("Passed        : %d", passed);
456         SKELDEV_TEST_INFO("Failed        : %d", failed);
457         SKELDEV_TEST_INFO("Not supported : %d", unsupported);
458
459         if (failed)
460                 return -1;
461
462         return 0;
463 };