xref: /spdk/module/bdev/virtio/bdev_virtio.h (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef SPDK_BDEV_VIRTIO_H
35 #define SPDK_BDEV_VIRTIO_H
36 
37 #include "spdk/bdev.h"
38 #include "spdk/env.h"
39 
40 /**
41  * Callback for creating virtio bdevs.
42  *
43  * \param ctx opaque context set by the user
44  * \param errnum error code. 0 on success, negative errno on error.
45  * \param bdevs contiguous array of created bdevs
46  * \param bdev_cnt number of bdevs in the `bdevs` array
47  */
48 typedef void (*bdev_virtio_create_cb)(void *ctx, int errnum,
49 				      struct spdk_bdev **bdevs, size_t bdev_cnt);
50 
51 /**
52  * Callback for removing virtio devices.
53  *
54  * \param ctx opaque context set by the user
55  * \param errnum error code. 0 on success, negative errno on error.
56  */
57 typedef void (*bdev_virtio_remove_cb)(void *ctx, int errnum);
58 
59 /**
60  * Connect to a vhost-user Unix domain socket and create a Virtio SCSI device.
61  * If the connection is successful, the device will be automatically scanned.
62  * The scan consists of probing the targets on the device and will result in
63  * creating possibly multiple Virtio SCSI bdevs - one for each target. Currently
64  * only one LUN per target is detected - LUN0. Note that the bdev creation is
65  * run asynchronously in the background. After it's finished, the `cb_fn`
66  * callback is called.
67  *
68  * \param name name for the virtio device. It will be inherited by all created
69  * bdevs, which are named in the following format: <name>t<target_id>
70  * \param path path to the socket
71  * \param num_queues max number of request virtqueues to use. `vdev` will be
72  * started successfully even if the host device supports less queues than requested.
73  * \param queue_size depth of each queue
74  * \param cb_fn function to be called after scanning all targets on the virtio
75  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
76  * \param cb_arg argument for the `cb_fn`
77  * \return zero on success (device scan is started) or negative error code.
78  * In case of error the \c cb_fn is not called.
79  */
80 int bdev_virtio_user_scsi_dev_create(const char *name, const char *path,
81 				     unsigned num_queues, unsigned queue_size,
82 				     bdev_virtio_create_cb cb_fn, void *cb_arg);
83 
84 /**
85  * Attach virtio-pci device. This creates a Virtio SCSI device with the same
86  * capabilities as the vhost-user equivalent. The device will be automatically
87  * scanned for exposed SCSI targets. This will result in creating possibly multiple
88  * Virtio SCSI bdevs - one for each target. Currently only one LUN per target is
89  * detected - LUN0. Note that the bdev creation is run asynchronously in the
90  * background. After it's finished, the `cb_fn` callback is called.
91  *
92  * \param name name for the virtio device. It will be inherited by all created
93  * bdevs, which are named in the following format: <name>t<target_id>
94  * \param pci_addr PCI address of the device to attach
95  * \param cb_fn function to be called after scanning all targets on the virtio
96  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
97  * \param cb_arg argument for the `cb_fn`
98  * \return zero on success (device scan is started) or negative error code.
99  * In case of error the \c cb_fn is not called.
100  */
101 int bdev_virtio_pci_scsi_dev_create(const char *name, struct spdk_pci_addr *pci_addr,
102 				    bdev_virtio_create_cb cb_fn, void *cb_arg);
103 
104 /**
105  * Remove a Virtio device with given name. This will destroy all bdevs exposed
106  * by this device.
107  *
108  * \param name virtio device name
109  * \param cb_fn function to be called after scanning all targets on the virtio
110  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb. Possible
111  * error codes are:
112  *  * ENODEV - couldn't find device with given name
113  *  * EBUSY - device is already being removed
114  * \param cb_arg argument for the `cb_fn`
115  * \return zero on success or -ENODEV if scsi dev does not exist
116  */
117 int bdev_virtio_scsi_dev_remove(const char *name,
118 				bdev_virtio_remove_cb cb_fn, void *cb_arg);
119 
120 /**
121  * Remove a Virtio device with given name.
122  *
123  * \param bdev virtio blk device bdev
124  * \param cb_fn function to be called after removing bdev
125  * \param cb_arg argument for the `cb_fn`
126  * \return zero on success, -ENODEV if bdev with 'name' does not exist or
127  * -EINVAL if bdev with 'name' is not a virtio blk device.
128  */
129 int bdev_virtio_blk_dev_remove(const char *name,
130 			       bdev_virtio_remove_cb cb_fn, void *cb_arg);
131 
132 /**
133  * List all created Virtio-SCSI devices.
134  *
135  * \param write_ctx JSON context to write into
136  */
137 void bdev_virtio_scsi_dev_list(struct spdk_json_write_ctx *write_ctx);
138 
139 /**
140  * Connect to a vhost-user Unix domain socket and create a Virtio BLK bdev.
141  *
142  * \param name name for the virtio bdev
143  * \param path path to the socket
144  * \param num_queues max number of request virtqueues to use. `vdev` will be
145  * started successfully even if the host device supports less queues than requested.
146  * \param queue_size depth of each queue
147  * \return virtio-blk bdev or NULL
148  */
149 struct spdk_bdev *bdev_virtio_user_blk_dev_create(const char *name, const char *path,
150 		unsigned num_queues, unsigned queue_size);
151 
152 /**
153  * Attach virtio-pci device. This creates a Virtio BLK device with the same
154  * capabilities as the vhost-user equivalent.
155  *
156  * \param name name for the virtio device. It will be inherited by all created
157  * bdevs, which are named in the following format: <name>t<target_id>
158  * \param pci_addr PCI address of the device to attach
159  * \return virtio-blk bdev or NULL
160  */
161 struct spdk_bdev *bdev_virtio_pci_blk_dev_create(const char *name,
162 		struct spdk_pci_addr *pci_addr);
163 
164 /**
165  * Enable/Disable the virtio blk hotplug monitor or
166  * change the monitor period time
167  *
168  * \param enabled True means to enable the hotplug monitor and the monitor
169  * period time is period_us. False means to disable the hotplug monitor
170  * \param period_us The period time of the hotplug monitor in us
171  * \return 0 for success otherwise failure
172  */
173 int
174 bdev_virtio_pci_blk_set_hotplug(bool enabled, uint64_t period_us);
175 
176 #endif /* SPDK_BDEV_VIRTIO_H */
177