xref: /spdk/module/bdev/virtio/bdev_virtio.h (revision 400aadf83f642a88ccee358411e52f6642bce89a)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) 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  * Attach virtio-pci device. This creates a Virtio SCSI device with the same
58  * capabilities as the vhost-user equivalent. The device will be automatically
59  * scanned for exposed SCSI targets. This will result in creating possibly multiple
60  * Virtio SCSI bdevs - one for each target. Currently only one LUN per target is
61  * detected - LUN0. Note that the bdev creation is run asynchronously in the
62  * background. After it's finished, the `cb_fn` callback is called.
63  *
64  * \param name name for the virtio device. It will be inherited by all created
65  * bdevs, which are named in the following format: <name>t<target_id>
66  * \param pci_addr PCI address of the device to attach
67  * \param cb_fn function to be called after scanning all targets on the virtio
68  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
69  * \param cb_arg argument for the `cb_fn`
70  * \return zero on success (device scan is started) or negative error code.
71  * In case of error the \c cb_fn is not called.
72  */
73 int bdev_virtio_pci_scsi_dev_create(const char *name, struct spdk_pci_addr *pci_addr,
74 				    bdev_virtio_create_cb cb_fn, void *cb_arg);
75 
76 /**
77  * Remove a Virtio device with given name. This will destroy all bdevs exposed
78  * by this device.
79  *
80  * \param name virtio device name
81  * \param cb_fn function to be called after scanning all targets on the virtio
82  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb. Possible
83  * error codes are:
84  *  * ENODEV - couldn't find device with given name
85  *  * EBUSY - device is already being removed
86  * \param cb_arg argument for the `cb_fn`
87  * \return zero on success or -ENODEV if scsi dev does not exist
88  */
89 int bdev_virtio_scsi_dev_remove(const char *name,
90 				bdev_virtio_remove_cb cb_fn, void *cb_arg);
91 
92 /**
93  * Remove a Virtio device with given name.
94  *
95  * \param bdev virtio blk device bdev
96  * \param cb_fn function to be called after removing bdev
97  * \param cb_arg argument for the `cb_fn`
98  * \return zero on success, -ENODEV if bdev with 'name' does not exist or
99  * -EINVAL if bdev with 'name' is not a virtio blk device.
100  */
101 int bdev_virtio_blk_dev_remove(const char *name,
102 			       bdev_virtio_remove_cb cb_fn, void *cb_arg);
103 
104 /**
105  * List all created Virtio-SCSI devices.
106  *
107  * \param write_ctx JSON context to write into
108  */
109 void bdev_virtio_scsi_dev_list(struct spdk_json_write_ctx *write_ctx);
110 
111 /**
112  * Connect to a vhost-user Unix domain socket and create a Virtio BLK bdev.
113  *
114  * \param name name for the virtio bdev
115  * \param path path to the socket
116  * \param num_queues max number of request virtqueues to use. `vdev` will be
117  * started successfully even if the host device supports less queues than requested.
118  * \param queue_size depth of each queue
119  * \return virtio-blk bdev or NULL
120  */
121 struct spdk_bdev *bdev_virtio_user_blk_dev_create(const char *name, const char *path,
122 		unsigned num_queues, unsigned queue_size);
123 
124 /**
125  * Attach virtio-pci device. This creates a Virtio BLK device with the same
126  * capabilities as the vhost-user equivalent.
127  *
128  * \param name name for the virtio device. It will be inherited by all created
129  * bdevs, which are named in the following format: <name>t<target_id>
130  * \param pci_addr PCI address of the device to attach
131  * \return virtio-blk bdev or NULL
132  */
133 struct spdk_bdev *bdev_virtio_pci_blk_dev_create(const char *name,
134 		struct spdk_pci_addr *pci_addr);
135 
136 /**
137  * Enable/Disable the virtio blk hotplug monitor or
138  * change the monitor period time
139  *
140  * \param enabled True means to enable the hotplug monitor and the monitor
141  * period time is period_us. False means to disable the hotplug monitor
142  * \param period_us The period time of the hotplug monitor in us
143  * \return 0 for success otherwise failure
144  */
145 int bdev_virtio_pci_blk_set_hotplug(bool enabled, uint64_t period_us);
146 
147 #endif /* SPDK_BDEV_VIRTIO_H */
148