]> git.proxmox.com Git - mirror_qemu.git/blame - backends/cryptodev.c
test/acpi/bios-tables-test: SSDT: update golden master binaries
[mirror_qemu.git] / backends / cryptodev.c
CommitLineData
d0ee7a13
GA
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
0dda001b 12 * version 2.1 of the License, or (at your option) any later version.
d0ee7a13
GA
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"
d0ee7a13
GA
26#include "qapi/error.h"
27#include "qapi/visitor.h"
d0ee7a13
GA
28#include "qemu/config-file.h"
29#include "qom/object_interfaces.h"
d6634ac0
GA
30#include "hw/virtio/virtio-crypto.h"
31
d0ee7a13
GA
32
33static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
34
35
36CryptoDevBackendClient *
37cryptodev_backend_new_client(const char *model,
38 const char *name)
39{
40 CryptoDevBackendClient *cc;
41
b21e2380 42 cc = g_new0(CryptoDevBackendClient, 1);
d0ee7a13
GA
43 cc->model = g_strdup(model);
44 if (name) {
45 cc->name = g_strdup(name);
46 }
47
48 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
49
50 return cc;
51}
52
53void cryptodev_backend_free_client(
54 CryptoDevBackendClient *cc)
55{
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);
61}
62
63void cryptodev_backend_cleanup(
64 CryptoDevBackend *backend,
65 Error **errp)
66{
67 CryptoDevBackendClass *bc =
68 CRYPTODEV_BACKEND_GET_CLASS(backend);
69
70 if (bc->cleanup) {
71 bc->cleanup(backend, errp);
72 }
d0ee7a13
GA
73}
74
0e660a6f 75int64_t cryptodev_backend_create_session(
9e4f86a8 76 CryptoDevBackend *backend,
0e660a6f 77 CryptoDevBackendSessionInfo *sess_info,
9e4f86a8
GA
78 uint32_t queue_index, Error **errp)
79{
80 CryptoDevBackendClass *bc =
81 CRYPTODEV_BACKEND_GET_CLASS(backend);
82
83 if (bc->create_session) {
84 return bc->create_session(backend, sess_info, queue_index, errp);
85 }
86
87 return -1;
88}
89
0e660a6f 90int cryptodev_backend_close_session(
9e4f86a8
GA
91 CryptoDevBackend *backend,
92 uint64_t session_id,
93 uint32_t queue_index, Error **errp)
94{
95 CryptoDevBackendClass *bc =
96 CRYPTODEV_BACKEND_GET_CLASS(backend);
97
98 if (bc->close_session) {
99 return bc->close_session(backend, session_id, queue_index, errp);
100 }
101
102 return -1;
103}
104
0e660a6f 105static int cryptodev_backend_operation(
9e4f86a8 106 CryptoDevBackend *backend,
0e660a6f 107 CryptoDevBackendOpInfo *op_info,
9e4f86a8
GA
108 uint32_t queue_index, Error **errp)
109{
110 CryptoDevBackendClass *bc =
111 CRYPTODEV_BACKEND_GET_CLASS(backend);
112
0e660a6f
ZP
113 if (bc->do_op) {
114 return bc->do_op(backend, op_info, queue_index, errp);
9e4f86a8
GA
115 }
116
d6634ac0
GA
117 return -VIRTIO_CRYPTO_ERR;
118}
119
120int cryptodev_backend_crypto_operation(
121 CryptoDevBackend *backend,
122 void *opaque,
123 uint32_t queue_index, Error **errp)
124{
125 VirtIOCryptoReq *req = opaque;
0e660a6f
ZP
126 CryptoDevBackendOpInfo *op_info = &req->op_info;
127 enum CryptoDevBackendAlgType algtype = req->flags;
d6634ac0 128
0e660a6f
ZP
129 if ((algtype != CRYPTODEV_BACKEND_ALG_SYM)
130 && (algtype != CRYPTODEV_BACKEND_ALG_ASYM)) {
d6634ac0 131 error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
0e660a6f
ZP
132 algtype);
133
134 return -VIRTIO_CRYPTO_NOTSUPP;
d6634ac0
GA
135 }
136
0e660a6f 137 return cryptodev_backend_operation(backend, op_info, queue_index, errp);
9e4f86a8
GA
138}
139
d0ee7a13
GA
140static void
141cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
142 void *opaque, Error **errp)
143{
144 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
145 uint32_t value = backend->conf.peers.queues;
146
147 visit_type_uint32(v, name, &value, errp);
148}
149
150static void
151cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
152 void *opaque, Error **errp)
153{
154 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
d0ee7a13
GA
155 uint32_t value;
156
668f62ec 157 if (!visit_type_uint32(v, name, &value, errp)) {
dcfe4805 158 return;
d0ee7a13
GA
159 }
160 if (!value) {
dcfe4805
MA
161 error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
162 object_get_typename(obj), name, value);
163 return;
d0ee7a13
GA
164 }
165 backend->conf.peers.queues = value;
d0ee7a13
GA
166}
167
168static void
169cryptodev_backend_complete(UserCreatable *uc, Error **errp)
170{
171 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
172 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
d0ee7a13
GA
173
174 if (bc->init) {
7dc75edb 175 bc->init(backend, errp);
d0ee7a13 176 }
d0ee7a13
GA
177}
178
46fd1705
GA
179void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
180{
181 backend->is_used = used;
182}
183
184bool cryptodev_backend_is_used(CryptoDevBackend *backend)
185{
186 return backend->is_used;
187}
188
6138dbda
GA
189void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
190{
191 backend->ready = ready;
192}
193
194bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
195{
196 return backend->ready;
197}
198
46fd1705 199static bool
3beacfb9 200cryptodev_backend_can_be_deleted(UserCreatable *uc)
46fd1705
GA
201{
202 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
203}
204
d0ee7a13
GA
205static void cryptodev_backend_instance_init(Object *obj)
206{
d0ee7a13 207 /* Initialize devices' queues property to 1 */
5325cc34 208 object_property_set_int(obj, "queues", 1, NULL);
d0ee7a13
GA
209}
210
211static void cryptodev_backend_finalize(Object *obj)
212{
46fd1705 213 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
d0ee7a13 214
46fd1705 215 cryptodev_backend_cleanup(backend, NULL);
d0ee7a13
GA
216}
217
218static void
219cryptodev_backend_class_init(ObjectClass *oc, void *data)
220{
221 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
222
223 ucc->complete = cryptodev_backend_complete;
46fd1705 224 ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
d0ee7a13
GA
225
226 QTAILQ_INIT(&crypto_clients);
1f14e388
EH
227 object_class_property_add(oc, "queues", "uint32",
228 cryptodev_backend_get_queues,
229 cryptodev_backend_set_queues,
230 NULL, NULL);
d0ee7a13
GA
231}
232
233static const TypeInfo cryptodev_backend_info = {
234 .name = TYPE_CRYPTODEV_BACKEND,
235 .parent = TYPE_OBJECT,
236 .instance_size = sizeof(CryptoDevBackend),
237 .instance_init = cryptodev_backend_instance_init,
238 .instance_finalize = cryptodev_backend_finalize,
239 .class_size = sizeof(CryptoDevBackendClass),
240 .class_init = cryptodev_backend_class_init,
241 .interfaces = (InterfaceInfo[]) {
242 { TYPE_USER_CREATABLE },
243 { }
244 }
245};
246
247static void
248cryptodev_backend_register_types(void)
249{
250 type_register_static(&cryptodev_backend_info);
251}
252
253type_init(cryptodev_backend_register_types);