xref: /dpdk/app/test-mldev/test_device_ops.c (revision c9f073cdca95c75404fb7aa3d46ff7bd29e92dd1)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2022 Marvell.
3  */
4 
5 #include <errno.h>
6 
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_mldev.h>
10 
11 #include "test_device_ops.h"
12 
13 static bool
14 test_device_cap_check(struct ml_options *opt)
15 {
16 	if (!ml_test_cap_check(opt))
17 		return false;
18 
19 	return true;
20 }
21 
22 static int
23 test_device_opt_check(struct ml_options *opt)
24 {
25 	int ret;
26 
27 	/* check common opts */
28 	ret = ml_test_opt_check(opt);
29 	if (ret != 0)
30 		return ret;
31 
32 	return 0;
33 }
34 
35 static void
36 test_device_opt_dump(struct ml_options *opt)
37 {
38 	/* dump common opts */
39 	ml_test_opt_dump(opt);
40 }
41 
42 static int
43 test_device_setup(struct ml_test *test, struct ml_options *opt)
44 {
45 	struct test_device *t;
46 	void *test_device;
47 	int ret = 0;
48 
49 	/* allocate for test structure */
50 	test_device = rte_zmalloc_socket(test->name, sizeof(struct test_device),
51 					 RTE_CACHE_LINE_SIZE, opt->socket_id);
52 	if (test_device == NULL) {
53 		ml_err("failed to allocate memory for test_model");
54 		ret = -ENOMEM;
55 		goto error;
56 	}
57 	test->test_priv = test_device;
58 	t = ml_test_priv(test);
59 
60 	t->cmn.result = ML_TEST_FAILED;
61 	t->cmn.opt = opt;
62 
63 	/* get device info */
64 	ret = rte_ml_dev_info_get(opt->dev_id, &t->cmn.dev_info);
65 	if (ret < 0) {
66 		ml_err("failed to get device info");
67 		goto error;
68 	}
69 
70 	return 0;
71 
72 error:
73 	rte_free(test_device);
74 
75 	return ret;
76 }
77 
78 static void
79 test_device_destroy(struct ml_test *test, struct ml_options *opt)
80 {
81 	struct test_device *t;
82 
83 	RTE_SET_USED(opt);
84 
85 	t = ml_test_priv(test);
86 	rte_free(t);
87 }
88 
89 static int
90 test_device_reconfigure(struct ml_test *test, struct ml_options *opt)
91 {
92 	struct rte_ml_dev_config dev_config;
93 	struct rte_ml_dev_qp_conf qp_conf;
94 	struct test_device *t;
95 	uint16_t qp_id = 0;
96 	int ret = 0;
97 
98 	t = ml_test_priv(test);
99 
100 	/* configure with default options */
101 	ret = ml_test_device_configure(test, opt);
102 	if (ret != 0)
103 		return ret;
104 
105 	/* setup one queue pair with nb_desc = 1 */
106 	qp_conf.nb_desc = 1;
107 	qp_conf.cb = NULL;
108 
109 	ret = rte_ml_dev_queue_pair_setup(opt->dev_id, qp_id, &qp_conf, opt->socket_id);
110 	if (ret != 0) {
111 		ml_err("Failed to setup ML device queue-pair, dev_id = %d, qp_id = %u\n",
112 		       opt->dev_id, qp_id);
113 		goto error;
114 	}
115 
116 	/* start device */
117 	ret = ml_test_device_start(test, opt);
118 	if (ret != 0)
119 		goto error;
120 
121 	/* stop device */
122 	ret = ml_test_device_stop(test, opt);
123 	if (ret != 0) {
124 		ml_err("Failed to stop device");
125 		goto error;
126 	}
127 
128 	/* reconfigure device based on dev_info */
129 	dev_config.socket_id = opt->socket_id;
130 	dev_config.nb_models = t->cmn.dev_info.max_models;
131 	dev_config.nb_queue_pairs = t->cmn.dev_info.max_queue_pairs;
132 	ret = rte_ml_dev_configure(opt->dev_id, &dev_config);
133 	if (ret != 0) {
134 		ml_err("Failed to reconfigure ML device, dev_id = %d\n", opt->dev_id);
135 		return ret;
136 	}
137 
138 	/* setup queue pairs */
139 	for (qp_id = 0; qp_id < t->cmn.dev_info.max_queue_pairs; qp_id++) {
140 		qp_conf.nb_desc = t->cmn.dev_info.max_desc;
141 		qp_conf.cb = NULL;
142 
143 		ret = rte_ml_dev_queue_pair_setup(opt->dev_id, qp_id, &qp_conf, opt->socket_id);
144 		if (ret != 0) {
145 			ml_err("Failed to setup ML device queue-pair, dev_id = %d, qp_id = %u\n",
146 			       opt->dev_id, qp_id);
147 			goto error;
148 		}
149 	}
150 
151 	/* start device */
152 	ret = ml_test_device_start(test, opt);
153 	if (ret != 0)
154 		goto error;
155 
156 	/* stop device */
157 	ret = ml_test_device_stop(test, opt);
158 	if (ret != 0)
159 		goto error;
160 
161 	/* close device */
162 	ret = ml_test_device_close(test, opt);
163 	if (ret != 0)
164 		return ret;
165 
166 	return 0;
167 
168 error:
169 	ml_test_device_close(test, opt);
170 
171 	return ret;
172 }
173 
174 static int
175 test_device_driver(struct ml_test *test, struct ml_options *opt)
176 {
177 	struct test_device *t;
178 	int ret = 0;
179 
180 	t = ml_test_priv(test);
181 
182 	/* sub-test: device reconfigure */
183 	ret = test_device_reconfigure(test, opt);
184 	if (ret != 0) {
185 		printf("\n");
186 		printf("Model Device Reconfigure Test: " CLRED "%s" CLNRM "\n", "Failed");
187 		goto error;
188 	} else {
189 		printf("\n");
190 		printf("Model Device Reconfigure Test: " CLYEL "%s" CLNRM "\n", "Passed");
191 	}
192 
193 	printf("\n");
194 
195 	t->cmn.result = ML_TEST_SUCCESS;
196 
197 	return 0;
198 
199 error:
200 	t->cmn.result = ML_TEST_FAILED;
201 	return -1;
202 }
203 
204 static int
205 test_device_result(struct ml_test *test, struct ml_options *opt)
206 {
207 	struct test_device *t;
208 
209 	RTE_SET_USED(opt);
210 
211 	t = ml_test_priv(test);
212 
213 	return t->cmn.result;
214 }
215 
216 static const struct ml_test_ops device_ops = {
217 	.cap_check = test_device_cap_check,
218 	.opt_check = test_device_opt_check,
219 	.opt_dump = test_device_opt_dump,
220 	.test_setup = test_device_setup,
221 	.test_destroy = test_device_destroy,
222 	.test_driver = test_device_driver,
223 	.test_result = test_device_result,
224 };
225 
226 ML_TEST_REGISTER(device_ops);
227