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.1 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
,
40 CryptoDevBackendClient
*cc
;
42 cc
= g_new0(CryptoDevBackendClient
, 1);
43 cc
->model
= g_strdup(model
);
45 cc
->name
= g_strdup(name
);
48 QTAILQ_INSERT_TAIL(&crypto_clients
, cc
, next
);
53 void cryptodev_backend_free_client(
54 CryptoDevBackendClient
*cc
)
56 QTAILQ_REMOVE(&crypto_clients
, cc
, next
);
63 void cryptodev_backend_cleanup(
64 CryptoDevBackend
*backend
,
67 CryptoDevBackendClass
*bc
=
68 CRYPTODEV_BACKEND_GET_CLASS(backend
);
71 bc
->cleanup(backend
, errp
);
75 int64_t cryptodev_backend_create_session(
76 CryptoDevBackend
*backend
,
77 CryptoDevBackendSessionInfo
*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
);
90 int cryptodev_backend_close_session(
91 CryptoDevBackend
*backend
,
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
);
105 static int cryptodev_backend_operation(
106 CryptoDevBackend
*backend
,
107 CryptoDevBackendOpInfo
*op_info
,
108 uint32_t queue_index
, Error
**errp
)
110 CryptoDevBackendClass
*bc
=
111 CRYPTODEV_BACKEND_GET_CLASS(backend
);
114 return bc
->do_op(backend
, op_info
, queue_index
, errp
);
117 return -VIRTIO_CRYPTO_ERR
;
120 int cryptodev_backend_crypto_operation(
121 CryptoDevBackend
*backend
,
123 uint32_t queue_index
, Error
**errp
)
125 VirtIOCryptoReq
*req
= opaque
;
126 CryptoDevBackendOpInfo
*op_info
= &req
->op_info
;
127 enum CryptoDevBackendAlgType algtype
= req
->flags
;
129 if ((algtype
!= CRYPTODEV_BACKEND_ALG_SYM
)
130 && (algtype
!= CRYPTODEV_BACKEND_ALG_ASYM
)) {
131 error_setg(errp
, "Unsupported cryptodev alg type: %" PRIu32
"",
134 return -VIRTIO_CRYPTO_NOTSUPP
;
137 return cryptodev_backend_operation(backend
, op_info
, queue_index
, errp
);
141 cryptodev_backend_get_queues(Object
*obj
, Visitor
*v
, const char *name
,
142 void *opaque
, Error
**errp
)
144 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
145 uint32_t value
= backend
->conf
.peers
.queues
;
147 visit_type_uint32(v
, name
, &value
, errp
);
151 cryptodev_backend_set_queues(Object
*obj
, Visitor
*v
, const char *name
,
152 void *opaque
, Error
**errp
)
154 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
157 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
161 error_setg(errp
, "Property '%s.%s' doesn't take value '%" PRIu32
"'",
162 object_get_typename(obj
), name
, value
);
165 backend
->conf
.peers
.queues
= value
;
169 cryptodev_backend_complete(UserCreatable
*uc
, Error
**errp
)
171 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(uc
);
172 CryptoDevBackendClass
*bc
= CRYPTODEV_BACKEND_GET_CLASS(uc
);
175 bc
->init(backend
, errp
);
179 void cryptodev_backend_set_used(CryptoDevBackend
*backend
, bool used
)
181 backend
->is_used
= used
;
184 bool cryptodev_backend_is_used(CryptoDevBackend
*backend
)
186 return backend
->is_used
;
189 void cryptodev_backend_set_ready(CryptoDevBackend
*backend
, bool ready
)
191 backend
->ready
= ready
;
194 bool cryptodev_backend_is_ready(CryptoDevBackend
*backend
)
196 return backend
->ready
;
200 cryptodev_backend_can_be_deleted(UserCreatable
*uc
)
202 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc
));
205 static void cryptodev_backend_instance_init(Object
*obj
)
207 /* Initialize devices' queues property to 1 */
208 object_property_set_int(obj
, "queues", 1, NULL
);
211 static void cryptodev_backend_finalize(Object
*obj
)
213 CryptoDevBackend
*backend
= CRYPTODEV_BACKEND(obj
);
215 cryptodev_backend_cleanup(backend
, NULL
);
219 cryptodev_backend_class_init(ObjectClass
*oc
, void *data
)
221 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
223 ucc
->complete
= cryptodev_backend_complete
;
224 ucc
->can_be_deleted
= cryptodev_backend_can_be_deleted
;
226 QTAILQ_INIT(&crypto_clients
);
227 object_class_property_add(oc
, "queues", "uint32",
228 cryptodev_backend_get_queues
,
229 cryptodev_backend_set_queues
,
233 static 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
},
248 cryptodev_backend_register_types(void)
250 type_register_static(&cryptodev_backend_info
);
253 type_init(cryptodev_backend_register_types
);