blob: 4de378532bfe0d144add3921037a8e1e91d4210f [file] [log] [blame]
Gongleid0ee7a12016-10-28 16:33:20 +08001/*
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"
Gongleid0ee7a12016-10-28 16:33:20 +080026#include "qapi/error.h"
27#include "qapi/visitor.h"
Gongleid0ee7a12016-10-28 16:33:20 +080028#include "qemu/config-file.h"
29#include "qom/object_interfaces.h"
Gongleid6634ac2016-10-28 16:33:29 +080030#include "hw/virtio/virtio-crypto.h"
31
Gongleid0ee7a12016-10-28 16:33:20 +080032
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
42 cc = g_malloc0(sizeof(CryptoDevBackendClient));
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 }
Gongleid0ee7a12016-10-28 16:33:20 +080073}
74
Gonglei9e4f86a2016-10-28 16:33:21 +080075int64_t cryptodev_backend_sym_create_session(
76 CryptoDevBackend *backend,
77 CryptoDevBackendSymSessionInfo *sess_info,
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
90int cryptodev_backend_sym_close_session(
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
Gongleid6634ac2016-10-28 16:33:29 +0800105static int cryptodev_backend_sym_operation(
Gonglei9e4f86a2016-10-28 16:33:21 +0800106 CryptoDevBackend *backend,
107 CryptoDevBackendSymOpInfo *op_info,
108 uint32_t queue_index, Error **errp)
109{
110 CryptoDevBackendClass *bc =
111 CRYPTODEV_BACKEND_GET_CLASS(backend);
112
113 if (bc->do_sym_op) {
114 return bc->do_sym_op(backend, op_info, queue_index, errp);
115 }
116
Gongleid6634ac2016-10-28 16:33:29 +0800117 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;
126
127 if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
128 CryptoDevBackendSymOpInfo *op_info;
129 op_info = req->u.sym_op_info;
130
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;
137 }
138
139 return -VIRTIO_CRYPTO_ERR;
Gonglei9e4f86a2016-10-28 16:33:21 +0800140}
141
Gongleid0ee7a12016-10-28 16:33:20 +0800142static void
143cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
144 void *opaque, Error **errp)
145{
146 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
147 uint32_t value = backend->conf.peers.queues;
148
149 visit_type_uint32(v, name, &value, errp);
150}
151
152static void
153cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
154 void *opaque, Error **errp)
155{
156 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
157 Error *local_err = NULL;
158 uint32_t value;
159
Markus Armbruster62a35aa2020-07-07 18:05:46 +0200160 if (!visit_type_uint32(v, name, &value, &local_err)) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200161 error_propagate(errp, local_err);
162 return;
Gongleid0ee7a12016-10-28 16:33:20 +0800163 }
164 if (!value) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200165 error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
166 object_get_typename(obj), name, value);
167 return;
Gongleid0ee7a12016-10-28 16:33:20 +0800168 }
169 backend->conf.peers.queues = value;
Gongleid0ee7a12016-10-28 16:33:20 +0800170}
171
172static void
173cryptodev_backend_complete(UserCreatable *uc, Error **errp)
174{
175 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
176 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
Gongleid0ee7a12016-10-28 16:33:20 +0800177
178 if (bc->init) {
Vladimir Sementsov-Ogievskiy7dc75ed2019-12-05 20:46:33 +0300179 bc->init(backend, errp);
Gongleid0ee7a12016-10-28 16:33:20 +0800180 }
Gongleid0ee7a12016-10-28 16:33:20 +0800181}
182
Gonglei46fd1702016-12-22 11:12:38 +0800183void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
184{
185 backend->is_used = used;
186}
187
188bool cryptodev_backend_is_used(CryptoDevBackend *backend)
189{
190 return backend->is_used;
191}
192
Gonglei6138dbd2016-12-22 11:12:39 +0800193void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
194{
195 backend->ready = ready;
196}
197
198bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
199{
200 return backend->ready;
201}
202
Gonglei46fd1702016-12-22 11:12:38 +0800203static bool
Eduardo Habkost3beacfb2017-08-29 19:03:37 -0300204cryptodev_backend_can_be_deleted(UserCreatable *uc)
Gonglei46fd1702016-12-22 11:12:38 +0800205{
206 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
207}
208
Gongleid0ee7a12016-10-28 16:33:20 +0800209static void cryptodev_backend_instance_init(Object *obj)
210{
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400211 object_property_add(obj, "queues", "uint32",
Gongleid0ee7a12016-10-28 16:33:20 +0800212 cryptodev_backend_get_queues,
213 cryptodev_backend_set_queues,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200214 NULL, NULL);
Gongleid0ee7a12016-10-28 16:33:20 +0800215 /* Initialize devices' queues property to 1 */
Markus Armbruster5325cc32020-07-07 18:05:54 +0200216 object_property_set_int(obj, "queues", 1, NULL);
Gongleid0ee7a12016-10-28 16:33:20 +0800217}
218
219static void cryptodev_backend_finalize(Object *obj)
220{
Gonglei46fd1702016-12-22 11:12:38 +0800221 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
Gongleid0ee7a12016-10-28 16:33:20 +0800222
Gonglei46fd1702016-12-22 11:12:38 +0800223 cryptodev_backend_cleanup(backend, NULL);
Gongleid0ee7a12016-10-28 16:33:20 +0800224}
225
226static void
227cryptodev_backend_class_init(ObjectClass *oc, void *data)
228{
229 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
230
231 ucc->complete = cryptodev_backend_complete;
Gonglei46fd1702016-12-22 11:12:38 +0800232 ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
Gongleid0ee7a12016-10-28 16:33:20 +0800233
234 QTAILQ_INIT(&crypto_clients);
235}
236
237static const TypeInfo cryptodev_backend_info = {
238 .name = TYPE_CRYPTODEV_BACKEND,
239 .parent = TYPE_OBJECT,
240 .instance_size = sizeof(CryptoDevBackend),
241 .instance_init = cryptodev_backend_instance_init,
242 .instance_finalize = cryptodev_backend_finalize,
243 .class_size = sizeof(CryptoDevBackendClass),
244 .class_init = cryptodev_backend_class_init,
245 .interfaces = (InterfaceInfo[]) {
246 { TYPE_USER_CREATABLE },
247 { }
248 }
249};
250
251static void
252cryptodev_backend_register_types(void)
253{
254 type_register_static(&cryptodev_backend_info);
255}
256
257type_init(cryptodev_backend_register_types);