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