examples/vhost_blk: use common macro for minimum
[dpdk.git] / examples / vhost_blk / vhost_blk.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _VHOST_BLK_H_
6 #define _VHOST_BLK_H_
7
8 #include <stdio.h>
9 #include <sys/uio.h>
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include <linux/virtio_blk.h>
13 #include <linux/virtio_ring.h>
14
15 #include <rte_vhost.h>
16
17 #ifndef VIRTIO_F_RING_PACKED
18 #define VIRTIO_F_RING_PACKED 34
19
20 struct vring_packed_desc {
21         /* Buffer Address. */
22         __le64 addr;
23         /* Buffer Length. */
24         __le32 len;
25         /* Buffer ID. */
26         __le16 id;
27         /* The flags depending on descriptor type. */
28         __le16 flags;
29 };
30 #endif
31
32 struct vhost_blk_queue {
33         struct rte_vhost_vring vq;
34         struct rte_vhost_ring_inflight inflight_vq;
35         uint16_t last_avail_idx;
36         uint16_t last_used_idx;
37         bool avail_wrap_counter;
38         bool used_wrap_counter;
39 };
40
41 #define NUM_OF_BLK_QUEUES 1
42
43 struct vhost_block_dev {
44         /** ID for vhost library. */
45         int vid;
46         /** Queues for the block device */
47         struct vhost_blk_queue queues[NUM_OF_BLK_QUEUES];
48         /** Unique name for this block device. */
49         char name[64];
50
51         /** Unique product name for this kind of block device. */
52         char product_name[256];
53
54         /** Size in bytes of a logical block for the backend */
55         uint32_t blocklen;
56
57         /** Number of blocks */
58         uint64_t blockcnt;
59
60         /** write cache enabled, not used at the moment */
61         int write_cache;
62
63         /** use memory as disk storage space */
64         uint8_t *data;
65 };
66
67 struct vhost_blk_ctrlr {
68         uint8_t started;
69         uint8_t packed_ring;
70         uint8_t need_restart;
71         /** Only support 1 LUN for the example */
72         struct vhost_block_dev *bdev;
73         /** VM memory region */
74         struct rte_vhost_memory *mem;
75 } __rte_cache_aligned;
76
77 #define VHOST_BLK_MAX_IOVS 128
78
79 enum blk_data_dir {
80         BLK_DIR_NONE = 0,
81         BLK_DIR_TO_DEV = 1,
82         BLK_DIR_FROM_DEV = 2,
83 };
84
85 struct vhost_blk_task {
86         uint8_t readtype;
87         uint8_t req_idx;
88         uint16_t head_idx;
89         uint16_t last_idx;
90         uint16_t inflight_idx;
91         uint16_t buffer_id;
92         uint32_t dxfer_dir;
93         uint32_t data_len;
94         struct virtio_blk_outhdr *req;
95
96         volatile uint8_t *status;
97
98         struct iovec iovs[VHOST_BLK_MAX_IOVS];
99         uint32_t iovs_cnt;
100         struct vring_packed_desc *desc_packed;
101         struct vring_desc *desc_split;
102         struct rte_vhost_vring *vq;
103         struct vhost_block_dev *bdev;
104         struct vhost_blk_ctrlr *ctrlr;
105 };
106
107 struct inflight_blk_task {
108         struct vhost_blk_task blk_task;
109         struct rte_vhost_inflight_desc_packed *inflight_desc;
110         struct rte_vhost_inflight_info_packed *inflight_packed;
111 };
112
113 extern struct vhost_blk_ctrlr *g_vhost_ctrlr;
114 extern struct vhost_device_ops vhost_blk_device_ops;
115
116 int vhost_bdev_process_blk_commands(struct vhost_block_dev *bdev,
117                                      struct vhost_blk_task *task);
118
119 void vhost_session_install_rte_compat_hooks(uint32_t vid);
120
121 void vhost_dev_install_rte_compat_hooks(const char *path);
122
123 struct vhost_blk_ctrlr *vhost_blk_ctrlr_find(const char *ctrlr_name);
124
125 #endif /* _VHOST_blk_H_ */