1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017-2018 Intel Corporation 3 */ 4 5 #include <stdlib.h> 6 7 #include <rte_string_fns.h> 8 #include <rte_malloc.h> 9 #include <rte_kvargs.h> 10 #include <dev_driver.h> 11 #include <rte_eal.h> 12 13 #include "rte_compressdev_internal.h" 14 #include "rte_compressdev_pmd.h" 15 16 /** 17 * Parse name from argument 18 */ 19 static int 20 rte_compressdev_pmd_parse_name_arg(const char *key __rte_unused, 21 const char *value, void *extra_args) 22 { 23 struct rte_compressdev_pmd_init_params *params = extra_args; 24 int n; 25 26 if (value == NULL || extra_args == NULL) 27 return -EINVAL; 28 29 n = strlcpy(params->name, value, RTE_COMPRESSDEV_NAME_MAX_LEN); 30 if (n >= RTE_COMPRESSDEV_NAME_MAX_LEN) 31 return -EINVAL; 32 33 return 0; 34 } 35 36 /** 37 * Parse unsigned integer from argument 38 */ 39 static int 40 rte_compressdev_pmd_parse_uint_arg(const char *key __rte_unused, 41 const char *value, void *extra_args) 42 { 43 int i; 44 char *end; 45 46 if (value == NULL || extra_args == NULL) 47 return -EINVAL; 48 49 errno = 0; 50 i = strtol(value, &end, 10); 51 if (*end != 0 || errno != 0 || i < 0) 52 return -EINVAL; 53 54 *((uint32_t *)extra_args) = i; 55 return 0; 56 } 57 58 int 59 rte_compressdev_pmd_parse_input_args( 60 struct rte_compressdev_pmd_init_params *params, 61 const char *args) 62 { 63 struct rte_kvargs *kvlist = NULL; 64 int ret = 0; 65 66 if (params == NULL) 67 return -EINVAL; 68 69 if (args) { 70 kvlist = rte_kvargs_parse(args, compressdev_pmd_valid_params); 71 if (kvlist == NULL) 72 return -EINVAL; 73 74 ret = rte_kvargs_process(kvlist, 75 RTE_COMPRESSDEV_PMD_SOCKET_ID_ARG, 76 &rte_compressdev_pmd_parse_uint_arg, 77 ¶ms->socket_id); 78 if (ret < 0) 79 goto free_kvlist; 80 81 ret = rte_kvargs_process(kvlist, 82 RTE_COMPRESSDEV_PMD_NAME_ARG, 83 &rte_compressdev_pmd_parse_name_arg, 84 params); 85 if (ret < 0) 86 goto free_kvlist; 87 } 88 89 free_kvlist: 90 rte_kvargs_free(kvlist); 91 return ret; 92 } 93 94 struct rte_compressdev * 95 rte_compressdev_pmd_create(const char *name, 96 struct rte_device *device, 97 size_t private_data_size, 98 struct rte_compressdev_pmd_init_params *params) 99 { 100 struct rte_compressdev *compressdev; 101 102 if (params->name[0] != '\0') { 103 COMPRESSDEV_LOG(INFO, "User specified device name = %s", 104 params->name); 105 name = params->name; 106 } 107 108 COMPRESSDEV_LOG(INFO, "Creating compressdev %s", name); 109 110 COMPRESSDEV_LOG(INFO, "Init parameters - name: %s, socket id: %d", 111 name, params->socket_id); 112 113 /* allocate device structure */ 114 compressdev = rte_compressdev_pmd_allocate(name, params->socket_id); 115 if (compressdev == NULL) { 116 COMPRESSDEV_LOG(ERR, "Failed to allocate comp device %s", name); 117 return NULL; 118 } 119 120 /* allocate private device structure */ 121 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 122 compressdev->data->dev_private = 123 rte_zmalloc_socket("compressdev device private", 124 private_data_size, 125 RTE_CACHE_LINE_SIZE, 126 params->socket_id); 127 128 if (compressdev->data->dev_private == NULL) { 129 COMPRESSDEV_LOG(ERR, 130 "Cannot allocate memory for compressdev" 131 " %s private data", name); 132 133 rte_compressdev_pmd_release_device(compressdev); 134 return NULL; 135 } 136 } 137 138 compressdev->device = device; 139 140 return compressdev; 141 } 142 143 int 144 rte_compressdev_pmd_destroy(struct rte_compressdev *compressdev) 145 { 146 int retval; 147 148 COMPRESSDEV_LOG(INFO, "Closing comp device %s", 149 compressdev->device->name); 150 151 /* free comp device */ 152 retval = rte_compressdev_pmd_release_device(compressdev); 153 if (retval) 154 return retval; 155 156 if (rte_eal_process_type() == RTE_PROC_PRIMARY) 157 rte_free(compressdev->data->dev_private); 158 159 compressdev->device = NULL; 160 compressdev->data = NULL; 161 162 return 0; 163 } 164