xref: /spdk/module/bdev/virtio/bdev_virtio.h (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2*a6dbe372Spaul luse  *   Copyright (C) 2017 Intel Corporation.
307fe6a43SSeth Howell  *   All rights reserved.
407fe6a43SSeth Howell  */
507fe6a43SSeth Howell 
607fe6a43SSeth Howell #ifndef SPDK_BDEV_VIRTIO_H
707fe6a43SSeth Howell #define SPDK_BDEV_VIRTIO_H
807fe6a43SSeth Howell 
907fe6a43SSeth Howell #include "spdk/bdev.h"
1007fe6a43SSeth Howell #include "spdk/env.h"
1107fe6a43SSeth Howell 
1207fe6a43SSeth Howell /**
1307fe6a43SSeth Howell  * Callback for creating virtio bdevs.
1407fe6a43SSeth Howell  *
1507fe6a43SSeth Howell  * \param ctx opaque context set by the user
1607fe6a43SSeth Howell  * \param errnum error code. 0 on success, negative errno on error.
1707fe6a43SSeth Howell  * \param bdevs contiguous array of created bdevs
1807fe6a43SSeth Howell  * \param bdev_cnt number of bdevs in the `bdevs` array
1907fe6a43SSeth Howell  */
2007fe6a43SSeth Howell typedef void (*bdev_virtio_create_cb)(void *ctx, int errnum,
2107fe6a43SSeth Howell 				      struct spdk_bdev **bdevs, size_t bdev_cnt);
2207fe6a43SSeth Howell 
2307fe6a43SSeth Howell /**
2407fe6a43SSeth Howell  * Callback for removing virtio devices.
2507fe6a43SSeth Howell  *
2607fe6a43SSeth Howell  * \param ctx opaque context set by the user
2707fe6a43SSeth Howell  * \param errnum error code. 0 on success, negative errno on error.
2807fe6a43SSeth Howell  */
2907fe6a43SSeth Howell typedef void (*bdev_virtio_remove_cb)(void *ctx, int errnum);
3007fe6a43SSeth Howell 
3107fe6a43SSeth Howell /**
3207fe6a43SSeth Howell  * Connect to a vhost-user Unix domain socket and create a Virtio SCSI device.
3307fe6a43SSeth Howell  * If the connection is successful, the device will be automatically scanned.
3407fe6a43SSeth Howell  * The scan consists of probing the targets on the device and will result in
3507fe6a43SSeth Howell  * creating possibly multiple Virtio SCSI bdevs - one for each target. Currently
3607fe6a43SSeth Howell  * only one LUN per target is detected - LUN0. Note that the bdev creation is
3707fe6a43SSeth Howell  * run asynchronously in the background. After it's finished, the `cb_fn`
3807fe6a43SSeth Howell  * callback is called.
3907fe6a43SSeth Howell  *
4007fe6a43SSeth Howell  * \param name name for the virtio device. It will be inherited by all created
4107fe6a43SSeth Howell  * bdevs, which are named in the following format: <name>t<target_id>
4207fe6a43SSeth Howell  * \param path path to the socket
4307fe6a43SSeth Howell  * \param num_queues max number of request virtqueues to use. `vdev` will be
4407fe6a43SSeth Howell  * started successfully even if the host device supports less queues than requested.
4507fe6a43SSeth Howell  * \param queue_size depth of each queue
4607fe6a43SSeth Howell  * \param cb_fn function to be called after scanning all targets on the virtio
4707fe6a43SSeth Howell  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
4807fe6a43SSeth Howell  * \param cb_arg argument for the `cb_fn`
4907fe6a43SSeth Howell  * \return zero on success (device scan is started) or negative error code.
5007fe6a43SSeth Howell  * In case of error the \c cb_fn is not called.
5107fe6a43SSeth Howell  */
5207fe6a43SSeth Howell int bdev_virtio_user_scsi_dev_create(const char *name, const char *path,
5307fe6a43SSeth Howell 				     unsigned num_queues, unsigned queue_size,
5407fe6a43SSeth Howell 				     bdev_virtio_create_cb cb_fn, void *cb_arg);
5507fe6a43SSeth Howell 
5607fe6a43SSeth Howell /**
578b260d5cSChangpeng Liu  * Connect to a vfio-user Unix domain socket and create a Virtio SCSI device.
588b260d5cSChangpeng Liu  * If the connection is successful, the device will be automatically scanned.
598b260d5cSChangpeng Liu  * The scan consists of probing the targets on the device and will result in
608b260d5cSChangpeng Liu  * creating possibly multiple Virtio SCSI bdevs - one for each target. Currently
618b260d5cSChangpeng Liu  * only one LUN per target is detected - LUN0. Note that the bdev creation is
628b260d5cSChangpeng Liu  * run asynchronously in the background. After it's finished, the `cb_fn`
638b260d5cSChangpeng Liu  * callback is called.
648b260d5cSChangpeng Liu  *
658b260d5cSChangpeng Liu  * \param name name for the virtio device. It will be inherited by all created
668b260d5cSChangpeng Liu  * bdevs, which are named in the following format: <name>t<target_id>
678b260d5cSChangpeng Liu  * \param path path to the socket
688b260d5cSChangpeng Liu  * \param cb_fn function to be called after scanning all targets on the virtio
698b260d5cSChangpeng Liu  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
708b260d5cSChangpeng Liu  * \param cb_arg argument for the `cb_fn`
718b260d5cSChangpeng Liu  * \return zero on success (device scan is started) or negative error code.
728b260d5cSChangpeng Liu  * In case of error the \c cb_fn is not called.
738b260d5cSChangpeng Liu  */
748b260d5cSChangpeng Liu int bdev_vfio_user_scsi_dev_create(const char *base_name, const char *path,
758b260d5cSChangpeng Liu 				   bdev_virtio_create_cb cb_fn, void *cb_arg);
768b260d5cSChangpeng Liu 
778b260d5cSChangpeng Liu /**
7807fe6a43SSeth Howell  * Attach virtio-pci device. This creates a Virtio SCSI device with the same
7907fe6a43SSeth Howell  * capabilities as the vhost-user equivalent. The device will be automatically
8007fe6a43SSeth Howell  * scanned for exposed SCSI targets. This will result in creating possibly multiple
8107fe6a43SSeth Howell  * Virtio SCSI bdevs - one for each target. Currently only one LUN per target is
8207fe6a43SSeth Howell  * detected - LUN0. Note that the bdev creation is run asynchronously in the
8307fe6a43SSeth Howell  * background. After it's finished, the `cb_fn` callback is called.
8407fe6a43SSeth Howell  *
8507fe6a43SSeth Howell  * \param name name for the virtio device. It will be inherited by all created
8607fe6a43SSeth Howell  * bdevs, which are named in the following format: <name>t<target_id>
8707fe6a43SSeth Howell  * \param pci_addr PCI address of the device to attach
8807fe6a43SSeth Howell  * \param cb_fn function to be called after scanning all targets on the virtio
8907fe6a43SSeth Howell  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb.
9007fe6a43SSeth Howell  * \param cb_arg argument for the `cb_fn`
9107fe6a43SSeth Howell  * \return zero on success (device scan is started) or negative error code.
9207fe6a43SSeth Howell  * In case of error the \c cb_fn is not called.
9307fe6a43SSeth Howell  */
9407fe6a43SSeth Howell int bdev_virtio_pci_scsi_dev_create(const char *name, struct spdk_pci_addr *pci_addr,
9507fe6a43SSeth Howell 				    bdev_virtio_create_cb cb_fn, void *cb_arg);
9607fe6a43SSeth Howell 
9707fe6a43SSeth Howell /**
9807fe6a43SSeth Howell  * Remove a Virtio device with given name. This will destroy all bdevs exposed
9907fe6a43SSeth Howell  * by this device.
10007fe6a43SSeth Howell  *
10107fe6a43SSeth Howell  * \param name virtio device name
10207fe6a43SSeth Howell  * \param cb_fn function to be called after scanning all targets on the virtio
10307fe6a43SSeth Howell  * device. It's optional, can be NULL. See \c bdev_virtio_create_cb. Possible
10407fe6a43SSeth Howell  * error codes are:
10507fe6a43SSeth Howell  *  * ENODEV - couldn't find device with given name
10607fe6a43SSeth Howell  *  * EBUSY - device is already being removed
10707fe6a43SSeth Howell  * \param cb_arg argument for the `cb_fn`
10807fe6a43SSeth Howell  * \return zero on success or -ENODEV if scsi dev does not exist
10907fe6a43SSeth Howell  */
11007fe6a43SSeth Howell int bdev_virtio_scsi_dev_remove(const char *name,
11107fe6a43SSeth Howell 				bdev_virtio_remove_cb cb_fn, void *cb_arg);
11207fe6a43SSeth Howell 
11307fe6a43SSeth Howell /**
11407fe6a43SSeth Howell  * Remove a Virtio device with given name.
11507fe6a43SSeth Howell  *
11607fe6a43SSeth Howell  * \param bdev virtio blk device bdev
11707fe6a43SSeth Howell  * \param cb_fn function to be called after removing bdev
11807fe6a43SSeth Howell  * \param cb_arg argument for the `cb_fn`
11907fe6a43SSeth Howell  * \return zero on success, -ENODEV if bdev with 'name' does not exist or
12007fe6a43SSeth Howell  * -EINVAL if bdev with 'name' is not a virtio blk device.
12107fe6a43SSeth Howell  */
12207fe6a43SSeth Howell int bdev_virtio_blk_dev_remove(const char *name,
12307fe6a43SSeth Howell 			       bdev_virtio_remove_cb cb_fn, void *cb_arg);
12407fe6a43SSeth Howell 
12507fe6a43SSeth Howell /**
12607fe6a43SSeth Howell  * List all created Virtio-SCSI devices.
12707fe6a43SSeth Howell  *
12807fe6a43SSeth Howell  * \param write_ctx JSON context to write into
12907fe6a43SSeth Howell  */
13007fe6a43SSeth Howell void bdev_virtio_scsi_dev_list(struct spdk_json_write_ctx *write_ctx);
13107fe6a43SSeth Howell 
13207fe6a43SSeth Howell /**
13307fe6a43SSeth Howell  * Connect to a vhost-user Unix domain socket and create a Virtio BLK bdev.
13407fe6a43SSeth Howell  *
13507fe6a43SSeth Howell  * \param name name for the virtio bdev
13607fe6a43SSeth Howell  * \param path path to the socket
13707fe6a43SSeth Howell  * \param num_queues max number of request virtqueues to use. `vdev` will be
13807fe6a43SSeth Howell  * started successfully even if the host device supports less queues than requested.
13907fe6a43SSeth Howell  * \param queue_size depth of each queue
14007fe6a43SSeth Howell  * \return virtio-blk bdev or NULL
14107fe6a43SSeth Howell  */
14207fe6a43SSeth Howell struct spdk_bdev *bdev_virtio_user_blk_dev_create(const char *name, const char *path,
14307fe6a43SSeth Howell 		unsigned num_queues, unsigned queue_size);
14407fe6a43SSeth Howell 
14507fe6a43SSeth Howell /**
146295e54d1SChangpeng Liu  * Connect to a vfio-user Unix domain socket and create a Virtio BLK bdev.
147295e54d1SChangpeng Liu  *
148295e54d1SChangpeng Liu  * \param name name for the virtio bdev
149295e54d1SChangpeng Liu  * \param path path to the socket
150295e54d1SChangpeng Liu  * \return virtio-blk bdev or NULL
151295e54d1SChangpeng Liu  */
152295e54d1SChangpeng Liu struct spdk_bdev *
153295e54d1SChangpeng Liu bdev_virtio_vfio_user_blk_dev_create(const char *name, const char *path);
154295e54d1SChangpeng Liu 
155295e54d1SChangpeng Liu /**
15607fe6a43SSeth Howell  * Attach virtio-pci device. This creates a Virtio BLK device with the same
15707fe6a43SSeth Howell  * capabilities as the vhost-user equivalent.
15807fe6a43SSeth Howell  *
15907fe6a43SSeth Howell  * \param name name for the virtio device. It will be inherited by all created
16007fe6a43SSeth Howell  * bdevs, which are named in the following format: <name>t<target_id>
16107fe6a43SSeth Howell  * \param pci_addr PCI address of the device to attach
16207fe6a43SSeth Howell  * \return virtio-blk bdev or NULL
16307fe6a43SSeth Howell  */
16407fe6a43SSeth Howell struct spdk_bdev *bdev_virtio_pci_blk_dev_create(const char *name,
16507fe6a43SSeth Howell 		struct spdk_pci_addr *pci_addr);
16607fe6a43SSeth Howell 
167ebea4dd6SJin Yu /**
168ebea4dd6SJin Yu  * Enable/Disable the virtio blk hotplug monitor or
169ebea4dd6SJin Yu  * change the monitor period time
170ebea4dd6SJin Yu  *
171ebea4dd6SJin Yu  * \param enabled True means to enable the hotplug monitor and the monitor
172ebea4dd6SJin Yu  * period time is period_us. False means to disable the hotplug monitor
173ebea4dd6SJin Yu  * \param period_us The period time of the hotplug monitor in us
174ebea4dd6SJin Yu  * \return 0 for success otherwise failure
175ebea4dd6SJin Yu  */
1768dd1cd21SBen Walker int bdev_virtio_pci_blk_set_hotplug(bool enabled, uint64_t period_us);
177ebea4dd6SJin Yu 
17807fe6a43SSeth Howell #endif /* SPDK_BDEV_VIRTIO_H */
179