qtest: Fix rtas dependencies
[qemu.git] / backends / cryptodev.c
blob5a9735684e05b40cbc95548f178e61116d571b93
1 /*
2 * QEMU Crypto Device Implementation
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Authors:
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 "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "qemu/config-file.h"
29 #include "qom/object_interfaces.h"
30 #include "hw/virtio/virtio-crypto.h"
33 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
36 CryptoDevBackendClient *
37 cryptodev_backend_new_client(const char *model,
38 const char *name)
40 CryptoDevBackendClient *cc;
42 cc = g_malloc0(sizeof(CryptoDevBackendClient));
43 cc->model = g_strdup(model);
44 if (name) {
45 cc->name = g_strdup(name);
48 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
50 return cc;
53 void cryptodev_backend_free_client(
54 CryptoDevBackendClient *cc)
56 QTAILQ_REMOVE(&crypto_clients, cc, next);
57 g_free(cc->name);
58 g_free(cc->model);
59 g_free(cc->info_str);
60 g_free(cc);
63 void cryptodev_backend_cleanup(
64 CryptoDevBackend *backend,
65 Error **errp)
67 CryptoDevBackendClass *bc =
68 CRYPTODEV_BACKEND_GET_CLASS(backend);
70 if (bc->cleanup) {
71 bc->cleanup(backend, errp);
75 int64_t cryptodev_backend_sym_create_session(
76 CryptoDevBackend *backend,
77 CryptoDevBackendSymSessionInfo *sess_info,
78 uint32_t queue_index, Error **errp)
80 CryptoDevBackendClass *bc =
81 CRYPTODEV_BACKEND_GET_CLASS(backend);
83 if (bc->create_session) {
84 return bc->create_session(backend, sess_info, queue_index, errp);
87 return -1;
90 int cryptodev_backend_sym_close_session(
91 CryptoDevBackend *backend,
92 uint64_t session_id,
93 uint32_t queue_index, Error **errp)
95 CryptoDevBackendClass *bc =
96 CRYPTODEV_BACKEND_GET_CLASS(backend);
98 if (bc->close_session) {
99 return bc->close_session(backend, session_id, queue_index, errp);
102 return -1;
105 static int cryptodev_backend_sym_operation(
106 CryptoDevBackend *backend,
107 CryptoDevBackendSymOpInfo *op_info,
108 uint32_t queue_index, Error **errp)
110 CryptoDevBackendClass *bc =
111 CRYPTODEV_BACKEND_GET_CLASS(backend);
113 if (bc->do_sym_op) {
114 return bc->do_sym_op(backend, op_info, queue_index, errp);
117 return -VIRTIO_CRYPTO_ERR;
120 int cryptodev_backend_crypto_operation(
121 CryptoDevBackend *backend,
122 void *opaque,
123 uint32_t queue_index, Error **errp)
125 VirtIOCryptoReq *req = opaque;
127 if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
128 CryptoDevBackendSymOpInfo *op_info;
129 op_info = req->u.sym_op_info;
131 return cryptodev_backend_sym_operation(backend,
132 op_info, queue_index, errp);
133 } else {
134 error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
135 req->flags);
136 return -VIRTIO_CRYPTO_NOTSUPP;
139 return -VIRTIO_CRYPTO_ERR;
142 static void
143 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
144 void *opaque, Error **errp)
146 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
147 uint32_t value = backend->conf.peers.queues;
149 visit_type_uint32(v, name, &value, errp);
152 static void
153 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
154 void *opaque, Error **errp)
156 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
157 Error *local_err = NULL;
158 uint32_t value;
160 visit_type_uint32(v, name, &value, &local_err);
161 if (local_err) {
162 goto out;
164 if (!value) {
165 error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
166 PRIu32 "'", object_get_typename(obj), name, value);
167 goto out;
169 backend->conf.peers.queues = value;
170 out:
171 error_propagate(errp, local_err);
174 static void
175 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
177 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
178 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
180 if (bc->init) {
181 bc->init(backend, errp);
185 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
187 backend->is_used = used;
190 bool cryptodev_backend_is_used(CryptoDevBackend *backend)
192 return backend->is_used;
195 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
197 backend->ready = ready;
200 bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
202 return backend->ready;
205 static bool
206 cryptodev_backend_can_be_deleted(UserCreatable *uc)
208 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
211 static void cryptodev_backend_instance_init(Object *obj)
213 object_property_add(obj, "queues", "uint32",
214 cryptodev_backend_get_queues,
215 cryptodev_backend_set_queues,
216 NULL, NULL, NULL);
217 /* Initialize devices' queues property to 1 */
218 object_property_set_int(obj, 1, "queues", NULL);
221 static void cryptodev_backend_finalize(Object *obj)
223 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
225 cryptodev_backend_cleanup(backend, NULL);
228 static void
229 cryptodev_backend_class_init(ObjectClass *oc, void *data)
231 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
233 ucc->complete = cryptodev_backend_complete;
234 ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
236 QTAILQ_INIT(&crypto_clients);
239 static const TypeInfo cryptodev_backend_info = {
240 .name = TYPE_CRYPTODEV_BACKEND,
241 .parent = TYPE_OBJECT,
242 .instance_size = sizeof(CryptoDevBackend),
243 .instance_init = cryptodev_backend_instance_init,
244 .instance_finalize = cryptodev_backend_finalize,
245 .class_size = sizeof(CryptoDevBackendClass),
246 .class_init = cryptodev_backend_class_init,
247 .interfaces = (InterfaceInfo[]) {
248 { TYPE_USER_CREATABLE },
253 static void
254 cryptodev_backend_register_types(void)
256 type_register_static(&cryptodev_backend_info);
259 type_init(cryptodev_backend_register_types);