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 if (test_device != NULL) 74 rte_free(test_device); 75 76 return ret; 77 } 78 79 static void 80 test_device_destroy(struct ml_test *test, struct ml_options *opt) 81 { 82 struct test_device *t; 83 84 RTE_SET_USED(opt); 85 86 t = ml_test_priv(test); 87 if (t != NULL) 88 rte_free(t); 89 } 90 91 static int 92 test_device_reconfigure(struct ml_test *test, struct ml_options *opt) 93 { 94 struct rte_ml_dev_config dev_config; 95 struct rte_ml_dev_qp_conf qp_conf; 96 struct test_device *t; 97 uint16_t qp_id = 0; 98 int ret = 0; 99 100 t = ml_test_priv(test); 101 102 /* configure with default options */ 103 ret = ml_test_device_configure(test, opt); 104 if (ret != 0) 105 return ret; 106 107 /* setup one queue pair with nb_desc = 1 */ 108 qp_conf.nb_desc = 1; 109 qp_conf.cb = NULL; 110 111 ret = rte_ml_dev_queue_pair_setup(opt->dev_id, qp_id, &qp_conf, opt->socket_id); 112 if (ret != 0) { 113 ml_err("Failed to setup ML device queue-pair, dev_id = %d, qp_id = %u\n", 114 opt->dev_id, qp_id); 115 goto error; 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 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 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