xref: /spdk/module/bdev/virtio/bdev_virtio.h (revision 784b9d48746955f210926648a0131f84f58de76f)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #ifndef SPDK_BDEV_VIRTIO_H
7 #define SPDK_BDEV_VIRTIO_H
8 
9 #include "spdk/bdev.h"
10 #include "spdk/env.h"
11 
12 /**
13  * Callback for creating virtio bdevs.
14  *
15  * \param ctx opaque context set by the user
16  * \param errnum error code. 0 on success, negative errno on error.
17  * \param bdevs contiguous array of created bdevs
18  * \param bdev_cnt number of bdevs in the `bdevs` array
19  */
20 typedef void (*bdev_virtio_create_cb)(void *ctx, int errnum,
21 				      struct spdk_bdev **bdevs, size_t bdev_cnt);
22 
23 /**
24  * Callback for removing virtio devices.
25  *
26  * \param ctx opaque context set by the user
27  * \param errnum error code. 0 on success, negative errno on error.
28  */
29 typedef void (*bdev_virtio_remove_cb)(void *ctx, int errnum);
30 
31 /**
32  * Connect to a vhost-user Unix domain socket and create a Virtio SCSI device.
33  * If the connection is successful, the device will be automatically scanned.
34  * The scan consists of probing the targets on the device and will result in
35  * creating possibly multiple Virtio SCSI bdevs - one for each target. Currently
36  * only one LUN per target is detected - LUN0. Note that the bdev creation is
37  * run asynchronously in the background. After it's finished, the `cb_fn`
38  * callback is called.
39  *
40  * \param name name for the virtio device. It will be inherited by all created
41  * bdevs, which are named in the following format: <name>t<target_id>
42  * \param path path to the socket
43  * \param num_queues max number of request virtqueues to use. `vdev` will be
44  * started successfully even if the host device supports less queues than requested.
45  * \param queue_size depth of each queue
46  * \param cb_fn function to be called after scanning all targets on the virtio
47  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
48  * \param cb_arg argument for the `cb_fn`
49  * \return zero on success (device scan is started) or negative error code.
50  * In case of error the \c cb_fn is not called.
51  */
52 int bdev_virtio_user_scsi_dev_create(const char *name, const char *path,
53 				     unsigned num_queues, unsigned queue_size,
54 				     bdev_virtio_create_cb cb_fn, void *cb_arg);
55 
56 /**
57  * Connect to a vfio-user Unix domain socket and create a Virtio SCSI device.
58  * If the connection is successful, the device will be automatically scanned.
59  * The scan consists of probing the targets on the device and will result in
60  * creating possibly multiple Virtio SCSI bdevs - one for each target. Currently
61  * only one LUN per target is detected - LUN0. Note that the bdev creation is
62  * run asynchronously in the background. After it's finished, the `cb_fn`
63  * callback is called.
64  *
65  * \param name name for the virtio device. It will be inherited by all created
66  * bdevs, which are named in the following format: <name>t<target_id>
67  * \param path path to the socket
68  * \param cb_fn function to be called after scanning all targets on the virtio
69  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
70  * \param cb_arg argument for the `cb_fn`
71  * \return zero on success (device scan is started) or negative error code.
72  * In case of error the \c cb_fn is not called.
73  */
74 int bdev_vfio_user_scsi_dev_create(const char *base_name, const char *path,
75 				   bdev_virtio_create_cb cb_fn, void *cb_arg);
76 
77 /**
78  * Attach virtio-pci device. This creates a Virtio SCSI device with the same
79  * capabilities as the vhost-user equivalent. The device will be automatically
80  * scanned for exposed SCSI targets. This will result in creating possibly multiple
81  * Virtio SCSI bdevs - one for each target. Currently only one LUN per target is
82  * detected - LUN0. Note that the bdev creation is run asynchronously in the
83  * background. After it's finished, the `cb_fn` callback is called.
84  *
85  * \param name name for the virtio device. It will be inherited by all created
86  * bdevs, which are named in the following format: <name>t<target_id>
87  * \param pci_addr PCI address of the device to attach
88  * \param cb_fn function to be called after scanning all targets on the virtio
89  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
90  * \param cb_arg argument for the `cb_fn`
91  * \return zero on success (device scan is started) or negative error code.
92  * In case of error the \c cb_fn is not called.
93  */
94 int bdev_virtio_pci_scsi_dev_create(const char *name, struct spdk_pci_addr *pci_addr,
95 				    bdev_virtio_create_cb cb_fn, void *cb_arg);
96 
97 /**
98  * Remove a Virtio device with given name. This will destroy all bdevs exposed
99  * by this device.
100  *
101  * \param name virtio device name
102  * \param cb_fn function to be called after scanning all targets on the virtio
103  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb. Possible
104  * error codes are:
105  *  * ENODEV - couldn't find device with given name
106  *  * EBUSY - device is already being removed
107  * \param cb_arg argument for the `cb_fn`
108  * \return zero on success or -ENODEV if scsi dev does not exist
109  */
110 int bdev_virtio_scsi_dev_remove(const char *name,
111 				bdev_virtio_remove_cb cb_fn, void *cb_arg);
112 
113 /**
114  * Remove a Virtio device with given name.
115  *
116  * \param bdev virtio blk device bdev
117  * \param cb_fn function to be called after removing bdev
118  * \param cb_arg argument for the `cb_fn`
119  * \return zero on success, -ENODEV if bdev with 'name' does not exist or
120  * -EINVAL if bdev with 'name' is not a virtio blk device.
121  */
122 int bdev_virtio_blk_dev_remove(const char *name,
123 			       bdev_virtio_remove_cb cb_fn, void *cb_arg);
124 
125 /**
126  * List all created Virtio-SCSI devices.
127  *
128  * \param write_ctx JSON context to write into
129  */
130 void bdev_virtio_scsi_dev_list(struct spdk_json_write_ctx *write_ctx);
131 
132 /**
133  * Connect to a vhost-user Unix domain socket and create a Virtio BLK bdev.
134  *
135  * \param name name for the virtio bdev
136  * \param path path to the socket
137  * \param num_queues max number of request virtqueues to use. `vdev` will be
138  * started successfully even if the host device supports less queues than requested.
139  * \param queue_size depth of each queue
140  * \return virtio-blk bdev or NULL
141  */
142 struct spdk_bdev *bdev_virtio_user_blk_dev_create(const char *name, const char *path,
143 		unsigned num_queues, unsigned queue_size);
144 
145 /**
146  * Connect to a vfio-user Unix domain socket and create a Virtio BLK bdev.
147  *
148  * \param name name for the virtio bdev
149  * \param path path to the socket
150  * \return virtio-blk bdev or NULL
151  */
152 struct spdk_bdev *
153 bdev_virtio_vfio_user_blk_dev_create(const char *name, const char *path);
154 
155 /**
156  * Attach virtio-pci device. This creates a Virtio BLK device with the same
157  * capabilities as the vhost-user equivalent.
158  *
159  * \param name name for the virtio device. It will be inherited by all created
160  * bdevs, which are named in the following format: <name>t<target_id>
161  * \param pci_addr PCI address of the device to attach
162  * \return virtio-blk bdev or NULL
163  */
164 struct spdk_bdev *bdev_virtio_pci_blk_dev_create(const char *name,
165 		struct spdk_pci_addr *pci_addr);
166 
167 /**
168  * Enable/Disable the virtio blk hotplug monitor or
169  * change the monitor period time
170  *
171  * \param enabled True means to enable the hotplug monitor and the monitor
172  * period time is period_us. False means to disable the hotplug monitor
173  * \param period_us The period time of the hotplug monitor in us
174  * \return 0 for success otherwise failure
175  */
176 int bdev_virtio_pci_blk_set_hotplug(bool enabled, uint64_t period_us);
177 
178 #endif /* SPDK_BDEV_VIRTIO_H */
179