New upstream version 17.08
[deb_dpdk.git] / examples / vhost_scsi / vhost_scsi.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _VHOST_SCSI_H_
35 #define _VHOST_SCSI_H_
36
37 #include <sys/uio.h>
38 #include <stdint.h>
39 #include <linux/virtio_scsi.h>
40 #include <linux/virtio_ring.h>
41
42 #include <rte_vhost.h>
43
44 struct vhost_scsi_queue {
45         struct rte_vhost_vring vq;
46         uint16_t last_avail_idx;
47         uint16_t last_used_idx;
48 };
49
50 #define NUM_OF_SCSI_QUEUES 3
51
52 struct vhost_block_dev {
53         /** ID for vhost library. */
54         int vid;
55         /** Queues for the block device */
56         struct vhost_scsi_queue queues[NUM_OF_SCSI_QUEUES];
57         /** Unique name for this block device. */
58         char name[64];
59
60         /** Unique product name for this kind of block device. */
61         char product_name[256];
62
63         /** Size in bytes of a logical block for the backend */
64         uint32_t blocklen;
65
66         /** Number of blocks */
67         uint64_t blockcnt;
68
69         /** write cache enabled, not used at the moment */
70         int write_cache;
71
72         /** use memory as disk storage space */
73         uint8_t *data;
74 };
75
76 struct vhost_scsi_ctrlr {
77         /** Only support 1 LUN for the example */
78         struct vhost_block_dev *bdev;
79         /** VM memory region */
80         struct rte_vhost_memory *mem;
81 } __rte_cache_aligned;
82
83 #define VHOST_SCSI_MAX_IOVS 128
84
85 enum scsi_data_dir {
86         SCSI_DIR_NONE = 0,
87         SCSI_DIR_TO_DEV = 1,
88         SCSI_DIR_FROM_DEV = 2,
89 };
90
91 struct vhost_scsi_task {
92         int req_idx;
93         uint32_t dxfer_dir;
94         uint32_t data_len;
95         struct virtio_scsi_cmd_req *req;
96         struct virtio_scsi_cmd_resp *resp;
97         struct iovec iovs[VHOST_SCSI_MAX_IOVS];
98         uint32_t iovs_cnt;
99         struct vring_desc *desc;
100         struct rte_vhost_vring *vq;
101         struct vhost_block_dev *bdev;
102         struct vhost_scsi_ctrlr *ctrlr;
103 };
104
105 int vhost_bdev_process_scsi_commands(struct vhost_block_dev *bdev,
106                                      struct vhost_scsi_task *task);
107
108 #endif /* _VHOST_SCSI_H_ */