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 static void 79 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name, 80 void *opaque, Error **errp) 81 { 82 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); 83 uint32_t value = backend->conf.peers.queues; 84 85 visit_type_uint32(v, name, &value, errp); 86 } 87 88 static void 89 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name, 90 void *opaque, Error **errp) 91 { 92 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); 93 Error *local_err = NULL; 94 uint32_t value; 95 96 visit_type_uint32(v, name, &value, &local_err); 97 if (local_err) { 98 goto out; 99 } 100 if (!value) { 101 error_setg(&local_err, "Property '%s.%s' doesn't take value '%" 102 PRIu32 "'", object_get_typename(obj), name, value); 103 goto out; 104 } 105 backend->conf.peers.queues = value; 106 out: 107 error_propagate(errp, local_err); 108 } 109 110 static void 111 cryptodev_backend_complete(UserCreatable *uc, Error **errp) 112 { 113 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc); 114 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc); 115 Error *local_err = NULL; 116 117 if (bc->init) { 118 bc->init(backend, &local_err); 119 if (local_err) { 120 goto out; 121 } 122 } 123 backend->ready = true; 124 return; 125 126 out: 127 backend->ready = false; 128 error_propagate(errp, local_err); 129 } 130 131 static void cryptodev_backend_instance_init(Object *obj) 132 { 133 object_property_add(obj, "queues", "int", 134 cryptodev_backend_get_queues, 135 cryptodev_backend_set_queues, 136 NULL, NULL, NULL); 137 /* Initialize devices' queues property to 1 */ 138 object_property_set_int(obj, 1, "queues", NULL); 139 } 140 141 static void cryptodev_backend_finalize(Object *obj) 142 { 143 144 } 145 146 static void 147 cryptodev_backend_class_init(ObjectClass *oc, void *data) 148 { 149 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 150 151 ucc->complete = cryptodev_backend_complete; 152 153 QTAILQ_INIT(&crypto_clients); 154 } 155 156 static const TypeInfo cryptodev_backend_info = { 157 .name = TYPE_CRYPTODEV_BACKEND, 158 .parent = TYPE_OBJECT, 159 .instance_size = sizeof(CryptoDevBackend), 160 .instance_init = cryptodev_backend_instance_init, 161 .instance_finalize = cryptodev_backend_finalize, 162 .class_size = sizeof(CryptoDevBackendClass), 163 .class_init = cryptodev_backend_class_init, 164 .interfaces = (InterfaceInfo[]) { 165 { TYPE_USER_CREATABLE }, 166 { } 167 } 168 }; 169 170 static void 171 cryptodev_backend_register_types(void) 172 { 173 type_register_static(&cryptodev_backend_info); 174 } 175 176 type_init(cryptodev_backend_register_types); 177