1 /*
2  * QEMU Crypto Device Common Vhost Implement
3  *
4  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5  *
6  * Authors:
7  *    Gonglei <arei.gonglei@huawei.com>
8  *    Jay Zhou <jianjay.zhou@huawei.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24 #ifndef CRYPTODEV_VHOST_H
25 #define CRYPTODEV_VHOST_H
26 
27 #include "qemu-common.h"
28 #include "hw/virtio/vhost.h"
29 #include "hw/virtio/vhost-backend.h"
30 #include "chardev/char.h"
31 
32 #include "sysemu/cryptodev.h"
33 
34 
35 typedef struct CryptoDevBackendVhostOptions {
36     VhostBackendType backend_type;
37     void *opaque;
38     int total_queues;
39     CryptoDevBackendClient *cc;
40 } CryptoDevBackendVhostOptions;
41 
42 typedef struct CryptoDevBackendVhost {
43     struct vhost_dev dev;
44     struct vhost_virtqueue vqs[1];
45     int backend;
46     CryptoDevBackendClient *cc;
47 } CryptoDevBackendVhost;
48 
49 /**
50  * cryptodev_vhost_get_max_queues:
51  * @crypto: the cryptodev backend common vhost object
52  *
53  * Get the maximum queue number of @crypto.
54  *
55  *
56  * Returns: the maximum queue number
57  */
58 uint64_t
59 cryptodev_vhost_get_max_queues(
60                         CryptoDevBackendVhost *crypto);
61 
62 
63 /**
64  * cryptodev_vhost_init:
65  * @options: the common vhost object's option
66  *
67  * Creates a new cryptodev backend common vhost object
68  *
69  ** The returned object must be released with
70  * cryptodev_vhost_cleanup() when no
71  * longer required
72  *
73  * Returns: the cryptodev backend common vhost object
74  */
75 struct CryptoDevBackendVhost *
76 cryptodev_vhost_init(
77              CryptoDevBackendVhostOptions *options);
78 
79 /**
80  * cryptodev_vhost_cleanup:
81  * @crypto: the cryptodev backend common vhost object
82  *
83  * Clean the resouce associated with @crypto that realizaed
84  * by cryptodev_vhost_init()
85  *
86  */
87 void cryptodev_vhost_cleanup(
88                         CryptoDevBackendVhost *crypto);
89 
90 /**
91  * cryptodev_get_vhost:
92  * @cc: the client object for each queue
93  * @b: the cryptodev backend common vhost object
94  * @queue: the cryptodev backend queue index
95  *
96  * Gets a new cryptodev backend common vhost object based on
97  * @b and @queue
98  *
99  * Returns: the cryptodev backend common vhost object
100  */
101 CryptoDevBackendVhost *
102 cryptodev_get_vhost(CryptoDevBackendClient *cc,
103                             CryptoDevBackend *b,
104                             uint16_t queue);
105 /**
106  * cryptodev_vhost_start:
107  * @dev: the virtio crypto object
108  * @total_queues: the total count of queue
109  *
110  * Starts the vhost crypto logic
111  *
112  * Returns: 0 for success, negative for errors
113  */
114 int cryptodev_vhost_start(VirtIODevice *dev, int total_queues);
115 
116 /**
117  * cryptodev_vhost_stop:
118  * @dev: the virtio crypto object
119  * @total_queues: the total count of queue
120  *
121  * Stops the vhost crypto logic
122  *
123  */
124 void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues);
125 
126 /**
127  * cryptodev_vhost_virtqueue_mask:
128  * @dev: the virtio crypto object
129  * @queue: the cryptodev backend queue index
130  * @idx: the virtqueue index
131  * @mask: mask or not (true or false)
132  *
133  * Mask/unmask events for @idx virtqueue on @dev device
134  *
135  */
136 void cryptodev_vhost_virtqueue_mask(VirtIODevice *dev,
137                                            int queue,
138                                            int idx, bool mask);
139 
140 /**
141  * cryptodev_vhost_virtqueue_pending:
142  * @dev: the virtio crypto object
143  * @queue: the cryptodev backend queue index
144  * @idx: the virtqueue index
145  *
146  * Test and clear event pending status for @idx virtqueue on @dev device.
147  * Should be called after unmask to avoid losing events.
148  *
149  * Returns: true for success, false for errors
150  */
151 bool cryptodev_vhost_virtqueue_pending(VirtIODevice *dev,
152                                               int queue, int idx);
153 
154 #endif /* CRYPTODEV_VHOST_H */
155