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_memory.h> 10 #include <rte_mldev.h> 11 12 #include "ml_common.h" 13 #include "test_common.h" 14 15 #include <fcntl.h> 16 #include <sys/mman.h> 17 #include <sys/stat.h> 18 #include <unistd.h> 19 20 int 21 ml_read_file(char *file, size_t *size, char **buffer) 22 { 23 char *file_buffer = NULL; 24 struct stat file_stat; 25 char *file_map; 26 int ret; 27 int fd; 28 29 fd = open(file, O_RDONLY); 30 if (fd == -1) { 31 ml_err("Failed to open file: %s\n", file); 32 return -errno; 33 } 34 35 if (fstat(fd, &file_stat) != 0) { 36 ml_err("fstat failed for file: %s\n", file); 37 close(fd); 38 return -errno; 39 } 40 41 file_buffer = malloc(file_stat.st_size); 42 if (file_buffer == NULL) { 43 ml_err("Failed to allocate memory: %s\n", file); 44 ret = -ENOMEM; 45 goto error; 46 } 47 48 file_map = mmap(0, file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 49 if (file_map == MAP_FAILED) { 50 ml_err("Failed to map file: %s\n", file); 51 ret = -errno; 52 goto error; 53 } 54 55 rte_memcpy(file_buffer, file_map, file_stat.st_size); 56 munmap(file_map, file_stat.st_size); 57 close(fd); 58 59 *size = file_stat.st_size; 60 *buffer = file_buffer; 61 62 return 0; 63 64 error: 65 free(file_buffer); 66 close(fd); 67 68 return ret; 69 } 70 71 bool 72 ml_test_cap_check(struct ml_options *opt) 73 { 74 struct rte_ml_dev_info dev_info; 75 76 rte_ml_dev_info_get(opt->dev_id, &dev_info); 77 if (dev_info.max_models == 0) { 78 ml_err("Not enough mldev models supported = %u", dev_info.max_models); 79 return false; 80 } 81 82 return true; 83 } 84 85 int 86 ml_test_opt_check(struct ml_options *opt) 87 { 88 uint16_t dev_count; 89 int socket_id; 90 91 RTE_SET_USED(opt); 92 93 dev_count = rte_ml_dev_count(); 94 if (dev_count == 0) { 95 ml_err("No ML devices found"); 96 return -ENODEV; 97 } 98 99 if ((opt->dev_id >= dev_count) || (opt->dev_id < 0)) { 100 ml_err("Invalid option, dev_id = %d", opt->dev_id); 101 return -EINVAL; 102 } 103 104 socket_id = rte_ml_dev_socket_id(opt->dev_id); 105 if ((opt->socket_id != SOCKET_ID_ANY) && (opt->socket_id != socket_id)) { 106 ml_err("Invalid option, socket_id = %d\n", opt->socket_id); 107 return -EINVAL; 108 } 109 110 if (opt->queue_pairs == 0) { 111 ml_err("Invalid option, queue_pairs = %d", opt->queue_pairs); 112 return -EINVAL; 113 } 114 115 if (opt->queue_size == 0) { 116 ml_err("Invalid option, queue_size = %d", opt->queue_size); 117 return -EINVAL; 118 } 119 120 return 0; 121 } 122 123 void 124 ml_test_opt_dump(struct ml_options *opt) 125 { 126 ml_options_dump(opt); 127 } 128 129 int 130 ml_test_device_configure(struct ml_test *test, struct ml_options *opt) 131 { 132 struct test_common *t = ml_test_priv(test); 133 struct rte_ml_dev_config dev_config; 134 int ret; 135 136 ret = rte_ml_dev_info_get(opt->dev_id, &t->dev_info); 137 if (ret != 0) { 138 ml_err("Failed to get mldev info, dev_id = %d\n", opt->dev_id); 139 return ret; 140 } 141 142 /* configure device */ 143 dev_config.socket_id = opt->socket_id; 144 dev_config.nb_models = t->dev_info.max_models; 145 dev_config.nb_queue_pairs = opt->queue_pairs; 146 ret = rte_ml_dev_configure(opt->dev_id, &dev_config); 147 if (ret != 0) { 148 ml_err("Failed to configure ml device, dev_id = %d\n", opt->dev_id); 149 return ret; 150 } 151 152 return 0; 153 } 154 155 int 156 ml_test_device_close(struct ml_test *test, struct ml_options *opt) 157 { 158 struct test_common *t = ml_test_priv(test); 159 int ret = 0; 160 161 RTE_SET_USED(t); 162 163 /* close device */ 164 ret = rte_ml_dev_close(opt->dev_id); 165 if (ret != 0) 166 ml_err("Failed to close ML device, dev_id = %d\n", opt->dev_id); 167 168 return ret; 169 } 170 171 int 172 ml_test_device_start(struct ml_test *test, struct ml_options *opt) 173 { 174 struct test_common *t = ml_test_priv(test); 175 int ret; 176 177 RTE_SET_USED(t); 178 179 /* start device */ 180 ret = rte_ml_dev_start(opt->dev_id); 181 if (ret != 0) { 182 ml_err("Failed to start ml device, dev_id = %d\n", opt->dev_id); 183 return ret; 184 } 185 186 return 0; 187 } 188 189 int 190 ml_test_device_stop(struct ml_test *test, struct ml_options *opt) 191 { 192 struct test_common *t = ml_test_priv(test); 193 int ret = 0; 194 195 RTE_SET_USED(t); 196 197 /* stop device */ 198 ret = rte_ml_dev_stop(opt->dev_id); 199 if (ret != 0) 200 ml_err("Failed to stop ML device, dev_id = %d\n", opt->dev_id); 201 202 return ret; 203 } 204