raw/skeleton: fix test of attribute set/get
[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, set_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 = &set_value;
285         *dummy_value = 200;
286         ret = rte_rawdev_set_attr(TEST_DEV_ID, "Test2", (uintptr_t)dummy_value);
287
288         /* Check if attributes have been set */
289         ret = rte_rawdev_get_attr(TEST_DEV_ID, "Test1", &ret_value);
290         RTE_TEST_ASSERT_EQUAL(ret_value, 100,
291                               "Attribute (Test1) not set correctly (%" PRIu64 ")",
292                               ret_value);
293
294         ret_value = 0;
295         ret = rte_rawdev_get_attr(TEST_DEV_ID, "Test2", &ret_value);
296         RTE_TEST_ASSERT_EQUAL(*((int *)(uintptr_t)ret_value), set_value,
297                               "Attribute (Test2) not set correctly (%" PRIu64 ")",
298                               ret_value);
299
300         return TEST_SUCCESS;
301 }
302
303 static int
304 test_rawdev_start_stop(void)
305 {
306         int ret;
307         struct rte_rawdev_info rdev_info = {0};
308         struct skeleton_rawdev_conf rdev_conf_get = {0};
309         char *dummy_firmware = NULL;
310
311         /* Get the current configuration */
312         rdev_info.dev_private = &rdev_conf_get;
313
314         /* Load a firmware using a dummy address area */
315         dummy_firmware = rte_zmalloc("RAWDEV SKELETON", sizeof(int) * 10, 0);
316         RTE_TEST_ASSERT(dummy_firmware != NULL,
317                         "Failed to create firmware memory backing");
318
319         ret = rte_rawdev_firmware_load(TEST_DEV_ID, dummy_firmware);
320         RTE_TEST_ASSERT_SUCCESS(ret, "Firmware loading failed (%d)", ret);
321
322         /* Skeleton doesn't do anything with the firmware area - that is dummy
323          * and can be removed.
324          */
325         rte_free(dummy_firmware);
326         dummy_firmware = NULL;
327
328         rte_rawdev_start(TEST_DEV_ID);
329         ret = rte_rawdev_info_get(TEST_DEV_ID, (rte_rawdev_obj_t)&rdev_info);
330         RTE_TEST_ASSERT_SUCCESS(ret,
331                                 "Failed to obtain rawdev configuration (%d)",
332                                 ret);
333         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_RUNNING,
334                               "Device start failed. State is (%d)",
335                               rdev_conf_get.device_state);
336
337         rte_rawdev_stop(TEST_DEV_ID);
338         ret = rte_rawdev_info_get(TEST_DEV_ID, (rte_rawdev_obj_t)&rdev_info);
339         RTE_TEST_ASSERT_SUCCESS(ret,
340                                 "Failed to obtain rawdev configuration (%d)",
341                                 ret);
342         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_STOPPED,
343                               "Device stop failed. State is (%d)",
344                               rdev_conf_get.device_state);
345
346         /* Unloading the firmware once device is stopped */
347         ret = rte_rawdev_firmware_unload(TEST_DEV_ID);
348         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to unload firmware (%d)", ret);
349
350         return TEST_SUCCESS;
351 }
352
353 static int
354 test_rawdev_enqdeq(void)
355 {
356         int ret;
357         unsigned int count = 1;
358         uint16_t queue_id = 0;
359         struct rte_rawdev_buf buffers[1];
360         struct rte_rawdev_buf *deq_buffers = NULL;
361
362         buffers[0].buf_addr = malloc(strlen(TEST_DEV_NAME) + 3);
363         if (!buffers[0].buf_addr)
364                 goto cleanup;
365         snprintf(buffers[0].buf_addr, strlen(TEST_DEV_NAME) + 2, "%s%d",
366                  TEST_DEV_NAME, 0);
367
368         ret = rte_rawdev_enqueue_buffers(TEST_DEV_ID,
369                                          (struct rte_rawdev_buf **)&buffers,
370                                          count, &queue_id);
371         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
372                               "Unable to enqueue buffers");
373
374         deq_buffers = malloc(sizeof(struct rte_rawdev_buf) * count);
375         if (!deq_buffers)
376                 goto cleanup;
377
378         ret = rte_rawdev_dequeue_buffers(TEST_DEV_ID,
379                                         (struct rte_rawdev_buf **)&deq_buffers,
380                                         count, &queue_id);
381         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
382                               "Unable to dequeue buffers");
383
384         if (deq_buffers)
385                 free(deq_buffers);
386
387         return TEST_SUCCESS;
388 cleanup:
389         if (buffers[0].buf_addr)
390                 free(buffers[0].buf_addr);
391
392         return TEST_FAILED;
393 }
394
395 static void skeldev_test_run(int (*setup)(void),
396                              void (*teardown)(void),
397                              int (*test)(void),
398                              const char *name)
399 {
400         int ret = 0;
401
402         if (setup) {
403                 ret = setup();
404                 if (ret < 0) {
405                         SKELDEV_TEST_INFO("Error setting up test %s", name);
406                         unsupported++;
407                 }
408         }
409
410         if (test) {
411                 ret = test();
412                 if (ret < 0) {
413                         failed++;
414                         SKELDEV_TEST_INFO("%s Failed", name);
415                 } else {
416                         passed++;
417                         SKELDEV_TEST_DEBUG("%s Passed", name);
418                 }
419         }
420
421         if (teardown)
422                 teardown();
423
424         total++;
425 }
426
427 int
428 test_rawdev_skeldev(void)
429 {
430         testsuite_setup();
431
432         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_count);
433         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_get_dev_id);
434         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_socket_id);
435         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_info_get);
436         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_configure);
437         SKELDEV_TEST_RUN(test_rawdev_configure, NULL,
438                          test_rawdev_queue_default_conf_get);
439         SKELDEV_TEST_RUN(test_rawdev_configure, NULL, test_rawdev_queue_setup);
440         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_queue_count);
441         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL,
442                          test_rawdev_queue_release);
443         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_attr_set_get);
444         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_start_stop);
445         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL, test_rawdev_enqdeq);
446
447         testsuite_teardown();
448
449         SKELDEV_TEST_INFO("Total tests   : %d", total);
450         SKELDEV_TEST_INFO("Passed        : %d", passed);
451         SKELDEV_TEST_INFO("Failed        : %d", failed);
452         SKELDEV_TEST_INFO("Not supported : %d", unsupported);
453
454         if (failed)
455                 return -1;
456
457         return 0;
458 };