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-visit.h"
30 #include "qemu/config-file.h"
31 #include "qom/object_interfaces.h"
32 #include "hw/virtio/virtio-crypto.h"
35 static QTAILQ_HEAD(, CryptoDevBackendClient
) crypto_clients
;
38 CryptoDevBackendClient
*
39 cryptodev_backend_new_client(const char *model
,
42 CryptoDevBackendClient
*cc
;
44 cc
= g_malloc0(sizeof(CryptoDevBackendClient
));
45 cc
->model
= g_strdup(model
);
47 cc
->name
= g_strdup(name
);
50 QTAILQ_INSERT_TAIL(&crypto_clients
, cc
, next
);
55 void cryptodev_backend_free_client(
56 CryptoDevBackendClient
*cc
)
58 QTAILQ_REMOVE(&crypto_clients
, cc
, next
);
65 void cryptodev_backend_cleanup(
66 CryptoDevBackend
*backend
,
69 CryptoDevBackendClass
*bc
=
70 CRYPTODEV_BACKEND_GET_CLASS(backend
);
73 bc
->cleanup(backend
, errp
);
77 int64_t cryptodev_backend_sym_create_session(
78 CryptoDevBackend
*backend
,
79 CryptoDevBackendSymSessionInfo
*sess_info
,
80 uint32_t queue_index
, Error
**errp
)
82 CryptoDevBackendClass
*bc
=
83 CRYPTODEV_BACKEND_GET_CLASS(backend
);
85 if (bc
->create_session
) {
86 return bc
->create_session(backend
, sess_info
, queue_index
, errp
);
92 int cryptodev_backend_sym_close_session(
93 CryptoDevBackend
*backend
,
95 uint32_t queue_index
, Error
**errp
)
97 CryptoDevBackendClass
*bc
=
98 CRYPTODEV_BACKEND_GET_CLASS(backend
);
100 if (bc
->close_session
) {
101 return bc
->close_session(backend
, session_id
, queue_index
, errp
);
107 static int cryptodev_backend_sym_operation(
108 CryptoDevBackend
*backend
,
109 CryptoDevBackendSymOpInfo
*op_info
,
110 uint32_t queue_index
, Error
**errp
)
112 CryptoDevBackendClass
*bc
=
113 CRYPTODEV_BACKEND_GET_CLASS(backend
);
116 return bc
->do_sym_op(backend
, op_info
, queue_index
, errp
);
119 return -VIRTIO_CRYPTO_ERR
;
122 int cryptodev_backend_crypto_operation(
123 CryptoDevBackend
*backend
,
125 uint32_t queue_index
, Error
**errp
)
127 VirtIOCryptoReq
*req
= opaque
;
129 if (req
->flags
== CRYPTODEV_BACKEND_ALG_SYM
) {
130 CryptoDevBackendSymOpInfo
*op_info
;
131 op_info
= req
->u
.sym_op_info
;
133 return cryptodev_backend_sym_operation(backend
,
134 op_info
, queue_index
, errp
);
136 error_setg(errp
, "Unsupported cryptodev alg type: %" PRIu32
"",
138 return -VIRTIO_CRYPTO_NOTSUPP
;
141 return -VIRTIO_CRYPTO_ERR
;
145 cryptodev_backend_get_queues(Object
*obj
, Visitor
*v
, const char *name
,
146 void *opaque
, Error
**errp
)
148 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
149 uint32_t value
= backend
->conf
.peers
.queues
;
151 visit_type_uint32(v
, name
, &value
, errp
);
155 cryptodev_backend_set_queues(Object
*obj
, Visitor
*v
, const char *name
,
156 void *opaque
, Error
**errp
)
158 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
159 Error
*local_err
= NULL
;
162 visit_type_uint32(v
, name
, &value
, &local_err
);
167 error_setg(&local_err
, "Property '%s.%s' doesn't take value '%"
168 PRIu32
"'", object_get_typename(obj
), name
, value
);
171 backend
->conf
.peers
.queues
= value
;
173 error_propagate(errp
, local_err
);
177 cryptodev_backend_complete(UserCreatable
*uc
, Error
**errp
)
179 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(uc
);
180 CryptoDevBackendClass
*bc
= CRYPTODEV_BACKEND_GET_CLASS(uc
);
181 Error
*local_err
= NULL
;
184 bc
->init(backend
, &local_err
);
193 error_propagate(errp
, local_err
);
196 void cryptodev_backend_set_used(CryptoDevBackend
*backend
, bool used
)
198 backend
->is_used
= used
;
201 bool cryptodev_backend_is_used(CryptoDevBackend
*backend
)
203 return backend
->is_used
;
206 void cryptodev_backend_set_ready(CryptoDevBackend
*backend
, bool ready
)
208 backend
->ready
= ready
;
211 bool cryptodev_backend_is_ready(CryptoDevBackend
*backend
)
213 return backend
->ready
;
217 cryptodev_backend_can_be_deleted(UserCreatable
*uc
)
219 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc
));
222 static void cryptodev_backend_instance_init(Object
*obj
)
224 object_property_add(obj
, "queues", "uint32",
225 cryptodev_backend_get_queues
,
226 cryptodev_backend_set_queues
,
228 /* Initialize devices' queues property to 1 */
229 object_property_set_int(obj
, 1, "queues", NULL
);
232 static void cryptodev_backend_finalize(Object
*obj
)
234 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
236 cryptodev_backend_cleanup(backend
, NULL
);
240 cryptodev_backend_class_init(ObjectClass
*oc
, void *data
)
242 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
244 ucc
->complete
= cryptodev_backend_complete
;
245 ucc
->can_be_deleted
= cryptodev_backend_can_be_deleted
;
247 QTAILQ_INIT(&crypto_clients
);
250 static const TypeInfo cryptodev_backend_info
= {
251 .name
= TYPE_CRYPTODEV_BACKEND
,
252 .parent
= TYPE_OBJECT
,
253 .instance_size
= sizeof(CryptoDevBackend
),
254 .instance_init
= cryptodev_backend_instance_init
,
255 .instance_finalize
= cryptodev_backend_finalize
,
256 .class_size
= sizeof(CryptoDevBackendClass
),
257 .class_init
= cryptodev_backend_class_init
,
258 .interfaces
= (InterfaceInfo
[]) {
259 { TYPE_USER_CREATABLE
},
265 cryptodev_backend_register_types(void)
267 type_register_static(&cryptodev_backend_info
);
270 type_init(cryptodev_backend_register_types
);