xref: /dpdk/app/test-mldev/test_device_ops.c (revision 7c4da16289b957ffa83f9ddb901434025c4c351e)
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
test_device_cap_check(struct ml_options * opt)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
test_device_opt_check(struct ml_options * opt)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
test_device_opt_dump(struct ml_options * opt)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
test_device_setup(struct ml_test * test,struct ml_options * opt)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
test_device_destroy(struct ml_test * test,struct ml_options * opt)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
test_device_reconfigure(struct ml_test * test,struct ml_options * opt)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 queue pairs with nb_user options */
106 	for (qp_id = 0; qp_id < opt->queue_pairs; qp_id++) {
107 		qp_conf.nb_desc = opt->queue_size;
108 		qp_conf.cb = NULL;
109 
110 		ret = rte_ml_dev_queue_pair_setup(opt->dev_id, qp_id, &qp_conf, opt->socket_id);
111 		if (ret != 0) {
112 			ml_err("Failed to setup ML device queue-pair, dev_id = %d, qp_id = %u\n",
113 			       opt->dev_id, qp_id);
114 			goto error;
115 		}
116 	}
117 
118 	/* start device */
119 	ret = ml_test_device_start(test, opt);
120 	if (ret != 0)
121 		goto error;
122 
123 	/* stop device */
124 	ret = ml_test_device_stop(test, opt);
125 	if (ret != 0) {
126 		ml_err("Failed to stop device");
127 		goto error;
128 	}
129 
130 	/* reconfigure device based on dev_info */
131 	dev_config.socket_id = opt->socket_id;
132 	dev_config.nb_models = t->cmn.dev_info.max_models;
133 	dev_config.nb_queue_pairs = t->cmn.dev_info.max_queue_pairs;
134 	ret = rte_ml_dev_configure(opt->dev_id, &dev_config);
135 	if (ret != 0) {
136 		ml_err("Failed to reconfigure ML device, dev_id = %d\n", opt->dev_id);
137 		return ret;
138 	}
139 
140 	/* setup queue pairs */
141 	for (qp_id = 0; qp_id < t->cmn.dev_info.max_queue_pairs; qp_id++) {
142 		qp_conf.nb_desc = t->cmn.dev_info.max_desc;
143 		qp_conf.cb = NULL;
144 
145 		ret = rte_ml_dev_queue_pair_setup(opt->dev_id, qp_id, &qp_conf, opt->socket_id);
146 		if (ret != 0) {
147 			ml_err("Failed to setup ML device queue-pair, dev_id = %d, qp_id = %u\n",
148 			       opt->dev_id, qp_id);
149 			goto error;
150 		}
151 	}
152 
153 	/* start device */
154 	ret = ml_test_device_start(test, opt);
155 	if (ret != 0)
156 		goto error;
157 
158 	/* stop device */
159 	ret = ml_test_device_stop(test, opt);
160 	if (ret != 0)
161 		goto error;
162 
163 	/* close device */
164 	ret = ml_test_device_close(test, opt);
165 	if (ret != 0)
166 		return ret;
167 
168 	return 0;
169 
170 error:
171 	ml_test_device_close(test, opt);
172 
173 	return ret;
174 }
175 
176 static int
test_device_driver(struct ml_test * test,struct ml_options * opt)177 test_device_driver(struct ml_test *test, struct ml_options *opt)
178 {
179 	struct test_device *t;
180 	int ret = 0;
181 
182 	t = ml_test_priv(test);
183 
184 	/* sub-test: device reconfigure */
185 	ret = test_device_reconfigure(test, opt);
186 	if (ret != 0) {
187 		printf("\n");
188 		printf("Model Device Reconfigure Test: " CLRED "%s" CLNRM "\n", "Failed");
189 		goto error;
190 	} else {
191 		printf("\n");
192 		printf("Model Device Reconfigure Test: " CLYEL "%s" CLNRM "\n", "Passed");
193 	}
194 
195 	printf("\n");
196 
197 	t->cmn.result = ML_TEST_SUCCESS;
198 
199 	return 0;
200 
201 error:
202 	t->cmn.result = ML_TEST_FAILED;
203 	return -1;
204 }
205 
206 static int
test_device_result(struct ml_test * test,struct ml_options * opt)207 test_device_result(struct ml_test *test, struct ml_options *opt)
208 {
209 	struct test_device *t;
210 
211 	RTE_SET_USED(opt);
212 
213 	t = ml_test_priv(test);
214 
215 	return t->cmn.result;
216 }
217 
218 static const struct ml_test_ops device_ops = {
219 	.cap_check = test_device_cap_check,
220 	.opt_check = test_device_opt_check,
221 	.opt_dump = test_device_opt_dump,
222 	.test_setup = test_device_setup,
223 	.test_destroy = test_device_destroy,
224 	.test_driver = test_device_driver,
225 	.test_result = test_device_result,
226 };
227 
228 ML_TEST_REGISTER(device_ops);
229