1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 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 SPDK_VHOST_INTERNAL_H 35 #define SPDK_VHOST_INTERNAL_H 36 37 #include "spdk/stdinc.h" 38 39 #include <rte_vhost.h> 40 41 #include "spdk_internal/log.h" 42 #include "spdk/event.h" 43 #include "spdk/rpc.h" 44 45 #define SPDK_CACHE_LINE_SIZE RTE_CACHE_LINE_SIZE 46 47 #ifndef VHOST_USER_F_PROTOCOL_FEATURES 48 #define VHOST_USER_F_PROTOCOL_FEATURES 30 49 #endif 50 51 #ifndef VIRTIO_F_VERSION_1 52 #define VIRTIO_F_VERSION_1 32 53 #endif 54 55 #ifndef VIRTIO_BLK_F_MQ 56 #define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ 57 #endif 58 59 #ifndef VIRTIO_BLK_F_CONFIG_WCE 60 #define VIRTIO_BLK_F_CONFIG_WCE 11 61 #endif 62 63 #define SPDK_VHOST_MAX_VQUEUES 256 64 65 #define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8 66 67 #define SPDK_VHOST_IOVS_MAX 128 68 69 #define SPDK_VHOST_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \ 70 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \ 71 (1ULL << VIRTIO_F_VERSION_1) | \ 72 (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ 73 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 74 (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) 75 76 #define SPDK_VHOST_DISABLED_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \ 77 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 78 (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) 79 80 enum spdk_vhost_dev_type { 81 SPDK_VHOST_DEV_T_SCSI, 82 SPDK_VHOST_DEV_T_BLK, 83 }; 84 85 struct spdk_vhost_dev_backend { 86 uint64_t virtio_features; 87 uint64_t disabled_features; 88 89 /** 90 * Callbacks for starting and pausing the device. 91 * The first param is struct spdk_vhost_dev *. 92 * The second one is event context that has to be 93 * passed to spdk_vhost_dev_backend_event_done(). 94 */ 95 spdk_vhost_event_fn start_device; 96 spdk_vhost_event_fn stop_device; 97 98 void (*dump_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 99 int (*vhost_remove_controller)(struct spdk_vhost_dev *vdev); 100 }; 101 102 struct spdk_vhost_dev { 103 struct rte_vhost_memory *mem; 104 char *name; 105 char *path; 106 107 int vid; 108 int task_cnt; 109 int32_t lcore; 110 uint64_t cpumask; 111 112 enum spdk_vhost_dev_type type; 113 const struct spdk_vhost_dev_backend *backend; 114 115 uint16_t num_queues; 116 uint64_t negotiated_features; 117 struct rte_vhost_vring virtqueue[SPDK_VHOST_MAX_VQUEUES] __attribute((aligned( 118 SPDK_CACHE_LINE_SIZE))); 119 }; 120 121 struct spdk_vhost_dev *spdk_vhost_dev_find(const char *ctrlr_name); 122 void spdk_vhost_dev_mem_register(struct spdk_vhost_dev *vdev); 123 void spdk_vhost_dev_mem_unregister(struct spdk_vhost_dev *vdev); 124 125 void *spdk_vhost_gpa_to_vva(struct spdk_vhost_dev *vdev, uint64_t addr); 126 127 uint16_t spdk_vhost_vq_avail_ring_get(struct rte_vhost_vring *vq, uint16_t *reqs, 128 uint16_t reqs_len); 129 bool spdk_vhost_vq_should_notify(struct spdk_vhost_dev *vdev, struct rte_vhost_vring *vq); 130 131 /** 132 * Get a virtio descriptor at given index in given virtqueue. 133 * The descriptor will provide access to the entire descriptor 134 * chain. The subsequent descriptors are accesible via 135 * \c spdk_vhost_vring_desc_get_next. 136 * \param vdev vhost device 137 * \param vq virtqueue 138 * \param req_idx descriptor index 139 * \param desc pointer to be set to the descriptor 140 * \param desc_table descriptor table to be used with 141 * \c spdk_vhost_vring_desc_get_next. This might be either 142 * default virtqueue descriptor table or per-chain indirect 143 * table. 144 * \param desc_table_size size of the *desc_table* 145 * \return 0 on success, -1 if given index is invalid. 146 * If -1 is returned, the params won't be changed. 147 */ 148 int spdk_vhost_vq_get_desc(struct spdk_vhost_dev *vdev, struct rte_vhost_vring *vq, 149 uint16_t req_idx, 150 struct vring_desc **desc, struct vring_desc **desc_table, uint32_t *desc_table_size); 151 void spdk_vhost_vq_used_ring_enqueue(struct spdk_vhost_dev *vdev, struct rte_vhost_vring *vq, 152 uint16_t id, uint32_t len); 153 154 /** 155 * Get subsequent descriptor from given table. 156 * \param desc current descriptor, will be set to the 157 * next descriptor (NULL in case this is the last 158 * descriptor in the chain or the next desc is invalid) 159 * \param desc_table descriptor table 160 * \param desc_table_size size of the *desc_table* 161 * \return 0 on success, -1 if given index is invalid 162 * The *desc* param will be set regardless of the 163 * return value. 164 */ 165 int spdk_vhost_vring_desc_get_next(struct vring_desc **desc, 166 struct vring_desc *desc_table, uint32_t desc_table_size); 167 bool spdk_vhost_vring_desc_is_wr(struct vring_desc *cur_desc); 168 169 int spdk_vhost_vring_desc_to_iov(struct spdk_vhost_dev *vdev, struct iovec *iov, 170 uint16_t *iov_index, const struct vring_desc *desc); 171 bool spdk_vhost_dev_has_feature(struct spdk_vhost_dev *vdev, unsigned feature_id); 172 173 int spdk_vhost_dev_construct(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, 174 enum spdk_vhost_dev_type type, const struct spdk_vhost_dev_backend *backend); 175 int spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev); 176 177 int spdk_vhost_scsi_controller_construct(void); 178 int spdk_vhost_blk_controller_construct(void); 179 void spdk_vhost_dump_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 180 void spdk_vhost_dev_backend_event_done(void *event_ctx, int response); 181 void spdk_vhost_lock(void); 182 void spdk_vhost_unlock(void); 183 int spdk_remove_vhost_controller(struct spdk_vhost_dev *vdev); 184 185 #endif /* SPDK_VHOST_INTERNAL_H */ 186