2 * QEMU Crypto Device Implementation
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 * Gonglei <arei.gonglei@huawei.com>
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.
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.
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/>.
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 #include "hw/virtio/virtio-crypto.h"
36 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
39 CryptoDevBackendClient *
40 cryptodev_backend_new_client(const char *model,
43 CryptoDevBackendClient *cc;
45 cc = g_malloc0(sizeof(CryptoDevBackendClient));
46 cc->model = g_strdup(model);
48 cc->name = g_strdup(name);
51 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
56 void cryptodev_backend_free_client(
57 CryptoDevBackendClient *cc)
59 QTAILQ_REMOVE(&crypto_clients, cc, next);
66 void cryptodev_backend_cleanup(
67 CryptoDevBackend *backend,
70 CryptoDevBackendClass *bc =
71 CRYPTODEV_BACKEND_GET_CLASS(backend);
74 bc->cleanup(backend, errp);
77 backend->ready = false;
80 int64_t cryptodev_backend_sym_create_session(
81 CryptoDevBackend *backend,
82 CryptoDevBackendSymSessionInfo *sess_info,
83 uint32_t queue_index, Error **errp)
85 CryptoDevBackendClass *bc =
86 CRYPTODEV_BACKEND_GET_CLASS(backend);
88 if (bc->create_session) {
89 return bc->create_session(backend, sess_info, queue_index, errp);
95 int cryptodev_backend_sym_close_session(
96 CryptoDevBackend *backend,
98 uint32_t queue_index, Error **errp)
100 CryptoDevBackendClass *bc =
101 CRYPTODEV_BACKEND_GET_CLASS(backend);
103 if (bc->close_session) {
104 return bc->close_session(backend, session_id, queue_index, errp);
110 static int cryptodev_backend_sym_operation(
111 CryptoDevBackend *backend,
112 CryptoDevBackendSymOpInfo *op_info,
113 uint32_t queue_index, Error **errp)
115 CryptoDevBackendClass *bc =
116 CRYPTODEV_BACKEND_GET_CLASS(backend);
119 return bc->do_sym_op(backend, op_info, queue_index, errp);
122 return -VIRTIO_CRYPTO_ERR;
125 int cryptodev_backend_crypto_operation(
126 CryptoDevBackend *backend,
128 uint32_t queue_index, Error **errp)
130 VirtIOCryptoReq *req = opaque;
132 if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
133 CryptoDevBackendSymOpInfo *op_info;
134 op_info = req->u.sym_op_info;
136 return cryptodev_backend_sym_operation(backend,
137 op_info, queue_index, errp);
139 error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
141 return -VIRTIO_CRYPTO_NOTSUPP;
144 return -VIRTIO_CRYPTO_ERR;
148 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
149 void *opaque, Error **errp)
151 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
152 uint32_t value = backend->conf.peers.queues;
154 visit_type_uint32(v, name, &value, errp);
158 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
159 void *opaque, Error **errp)
161 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
162 Error *local_err = NULL;
165 visit_type_uint32(v, name, &value, &local_err);
170 error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
171 PRIu32 "'", object_get_typename(obj), name, value);
174 backend->conf.peers.queues = value;
176 error_propagate(errp, local_err);
180 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
182 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
183 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
184 Error *local_err = NULL;
187 bc->init(backend, &local_err);
192 backend->ready = true;
196 backend->ready = false;
197 error_propagate(errp, local_err);
200 static void cryptodev_backend_instance_init(Object *obj)
202 object_property_add(obj, "queues", "int",
203 cryptodev_backend_get_queues,
204 cryptodev_backend_set_queues,
206 /* Initialize devices' queues property to 1 */
207 object_property_set_int(obj, 1, "queues", NULL);
210 static void cryptodev_backend_finalize(Object *obj)
216 cryptodev_backend_class_init(ObjectClass *oc, void *data)
218 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
220 ucc->complete = cryptodev_backend_complete;
222 QTAILQ_INIT(&crypto_clients);
225 static const TypeInfo cryptodev_backend_info = {
226 .name = TYPE_CRYPTODEV_BACKEND,
227 .parent = TYPE_OBJECT,
228 .instance_size = sizeof(CryptoDevBackend),
229 .instance_init = cryptodev_backend_instance_init,
230 .instance_finalize = cryptodev_backend_finalize,
231 .class_size = sizeof(CryptoDevBackendClass),
232 .class_init = cryptodev_backend_class_init,
233 .interfaces = (InterfaceInfo[]) {
234 { TYPE_USER_CREATABLE },
240 cryptodev_backend_register_types(void)
242 type_register_static(&cryptodev_backend_info);
245 type_init(cryptodev_backend_register_types);