1 /* 2 * QEMU Crypto Device Implementation 3 * 4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. 5 * 6 * Authors: 7 * Gonglei <arei.gonglei@huawei.com> 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 21 * 22 */ 23 24 #include "qemu/osdep.h" 25 #include "sysemu/cryptodev.h" 26 #include "hw/boards.h" 27 #include "qapi/error.h" 28 #include "qapi/visitor.h" 29 #include "qapi-types.h" 30 #include "qapi-visit.h" 31 #include "qemu/config-file.h" 32 #include "qom/object_interfaces.h" 33 34 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients; 35 36 37 CryptoDevBackendClient * 38 cryptodev_backend_new_client(const char *model, 39 const char *name) 40 { 41 CryptoDevBackendClient *cc; 42 43 cc = g_malloc0(sizeof(CryptoDevBackendClient)); 44 cc->model = g_strdup(model); 45 if (name) { 46 cc->name = g_strdup(name); 47 } 48 49 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next); 50 51 return cc; 52 } 53 54 void cryptodev_backend_free_client( 55 CryptoDevBackendClient *cc) 56 { 57 QTAILQ_REMOVE(&crypto_clients, cc, next); 58 g_free(cc->name); 59 g_free(cc->model); 60 g_free(cc->info_str); 61 g_free(cc); 62 } 63 64 void cryptodev_backend_cleanup( 65 CryptoDevBackend *backend, 66 Error **errp) 67 { 68 CryptoDevBackendClass *bc = 69 CRYPTODEV_BACKEND_GET_CLASS(backend); 70 71 if (bc->cleanup) { 72 bc->cleanup(backend, errp); 73 } 74 75 backend->ready = false; 76 } 77 78 int64_t cryptodev_backend_sym_create_session( 79 CryptoDevBackend *backend, 80 CryptoDevBackendSymSessionInfo *sess_info, 81 uint32_t queue_index, Error **errp) 82 { 83 CryptoDevBackendClass *bc = 84 CRYPTODEV_BACKEND_GET_CLASS(backend); 85 86 if (bc->create_session) { 87 return bc->create_session(backend, sess_info, queue_index, errp); 88 } 89 90 return -1; 91 } 92 93 int cryptodev_backend_sym_close_session( 94 CryptoDevBackend *backend, 95 uint64_t session_id, 96 uint32_t queue_index, Error **errp) 97 { 98 CryptoDevBackendClass *bc = 99 CRYPTODEV_BACKEND_GET_CLASS(backend); 100 101 if (bc->close_session) { 102 return bc->close_session(backend, session_id, queue_index, errp); 103 } 104 105 return -1; 106 } 107 108 int cryptodev_backend_sym_operation( 109 CryptoDevBackend *backend, 110 CryptoDevBackendSymOpInfo *op_info, 111 uint32_t queue_index, Error **errp) 112 { 113 CryptoDevBackendClass *bc = 114 CRYPTODEV_BACKEND_GET_CLASS(backend); 115 116 if (bc->do_sym_op) { 117 return bc->do_sym_op(backend, op_info, queue_index, errp); 118 } 119 120 return -1; 121 } 122 123 static void 124 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name, 125 void *opaque, Error **errp) 126 { 127 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); 128 uint32_t value = backend->conf.peers.queues; 129 130 visit_type_uint32(v, name, &value, errp); 131 } 132 133 static void 134 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name, 135 void *opaque, Error **errp) 136 { 137 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); 138 Error *local_err = NULL; 139 uint32_t value; 140 141 visit_type_uint32(v, name, &value, &local_err); 142 if (local_err) { 143 goto out; 144 } 145 if (!value) { 146 error_setg(&local_err, "Property '%s.%s' doesn't take value '%" 147 PRIu32 "'", object_get_typename(obj), name, value); 148 goto out; 149 } 150 backend->conf.peers.queues = value; 151 out: 152 error_propagate(errp, local_err); 153 } 154 155 static void 156 cryptodev_backend_complete(UserCreatable *uc, Error **errp) 157 { 158 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc); 159 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc); 160 Error *local_err = NULL; 161 162 if (bc->init) { 163 bc->init(backend, &local_err); 164 if (local_err) { 165 goto out; 166 } 167 } 168 backend->ready = true; 169 return; 170 171 out: 172 backend->ready = false; 173 error_propagate(errp, local_err); 174 } 175 176 static void cryptodev_backend_instance_init(Object *obj) 177 { 178 object_property_add(obj, "queues", "int", 179 cryptodev_backend_get_queues, 180 cryptodev_backend_set_queues, 181 NULL, NULL, NULL); 182 /* Initialize devices' queues property to 1 */ 183 object_property_set_int(obj, 1, "queues", NULL); 184 } 185 186 static void cryptodev_backend_finalize(Object *obj) 187 { 188 189 } 190 191 static void 192 cryptodev_backend_class_init(ObjectClass *oc, void *data) 193 { 194 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 195 196 ucc->complete = cryptodev_backend_complete; 197 198 QTAILQ_INIT(&crypto_clients); 199 } 200 201 static const TypeInfo cryptodev_backend_info = { 202 .name = TYPE_CRYPTODEV_BACKEND, 203 .parent = TYPE_OBJECT, 204 .instance_size = sizeof(CryptoDevBackend), 205 .instance_init = cryptodev_backend_instance_init, 206 .instance_finalize = cryptodev_backend_finalize, 207 .class_size = sizeof(CryptoDevBackendClass), 208 .class_init = cryptodev_backend_class_init, 209 .interfaces = (InterfaceInfo[]) { 210 { TYPE_USER_CREATABLE }, 211 { } 212 } 213 }; 214 215 static void 216 cryptodev_backend_register_types(void) 217 { 218 type_register_static(&cryptodev_backend_info); 219 } 220 221 type_init(cryptodev_backend_register_types); 222