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 #define SPDK_VHOST_MAX_VQ_SIZE 1024 65 66 #define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8 67 68 #define SPDK_VHOST_IOVS_MAX 128 69 70 /* 71 * Rate at which stats are checked for interrupt coalescing. 72 */ 73 #define SPDK_VHOST_DEV_STATS_CHECK_INTERVAL_MS 10 74 /* 75 * Default threshold at which interrupts start to be coalesced. 76 */ 77 #define SPDK_VHOST_VQ_IOPS_COALESCING_THRESHOLD 60000 78 79 /* 80 * Currently coalescing is not used by default. 81 * Setting this to value > 0 here or by RPC will enable coalescing. 82 */ 83 #define SPDK_VHOST_COALESCING_DELAY_BASE_US 0 84 85 86 #define SPDK_VHOST_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \ 87 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \ 88 (1ULL << VIRTIO_F_VERSION_1) | \ 89 (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ 90 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 91 (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) 92 93 #define SPDK_VHOST_DISABLED_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \ 94 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 95 (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) 96 97 enum spdk_vhost_dev_type { 98 SPDK_VHOST_DEV_T_SCSI,//!< SPDK_VHOST_DEV_T_SCSI 99 SPDK_VHOST_DEV_T_BLK, //!< SPDK_VHOST_DEV_T_BLK 100 }; 101 102 struct spdk_vhost_virtqueue { 103 struct rte_vhost_vring vring; 104 void *tasks; 105 106 /* Request count from last stats check */ 107 uint32_t req_cnt; 108 109 /* Request count from last event */ 110 uint16_t used_req_cnt; 111 112 /* How long interrupt is delayed */ 113 uint32_t irq_delay_time; 114 115 /* Next time when we need to send event */ 116 uint64_t next_event_time; 117 118 } __attribute((aligned(SPDK_CACHE_LINE_SIZE))); 119 120 struct spdk_vhost_dev_backend { 121 uint64_t virtio_features; 122 uint64_t disabled_features; 123 124 /** 125 * Callbacks for starting and pausing the device. 126 * The first param is struct spdk_vhost_dev *. 127 * The second one is event context that has to be 128 * passed to spdk_vhost_dev_backend_event_done(). 129 */ 130 spdk_vhost_event_fn start_device; 131 spdk_vhost_event_fn stop_device; 132 133 void (*dump_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 134 int (*vhost_remove_controller)(struct spdk_vhost_dev *vdev); 135 }; 136 137 struct spdk_vhost_dev { 138 struct rte_vhost_memory *mem; 139 char *name; 140 char *path; 141 142 int vid; 143 int task_cnt; 144 int32_t lcore; 145 uint64_t cpumask; 146 147 enum spdk_vhost_dev_type type; 148 const struct spdk_vhost_dev_backend *backend; 149 150 uint32_t coalescing_delay_time_base; 151 152 /* Threshold when event coalescing for virtqueue will be turned on. */ 153 uint32_t coalescing_io_rate_threshold; 154 155 /* Next time when stats for event coalescing will be checked. */ 156 uint64_t next_stats_check_time; 157 158 /* Interval used for event coalescing checking. */ 159 uint64_t stats_check_interval; 160 161 uint16_t num_queues; 162 163 uint64_t negotiated_features; 164 165 struct spdk_vhost_virtqueue virtqueue[SPDK_VHOST_MAX_VQUEUES]; 166 }; 167 168 struct spdk_vhost_dev *spdk_vhost_dev_find(const char *ctrlr_name); 169 void spdk_vhost_dev_mem_register(struct spdk_vhost_dev *vdev); 170 void spdk_vhost_dev_mem_unregister(struct spdk_vhost_dev *vdev); 171 172 void *spdk_vhost_gpa_to_vva(struct spdk_vhost_dev *vdev, uint64_t addr); 173 174 uint16_t spdk_vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *vq, uint16_t *reqs, 175 uint16_t reqs_len); 176 bool spdk_vhost_vq_should_notify(struct spdk_vhost_dev *vdev, struct spdk_vhost_virtqueue *vq); 177 178 /** 179 * Get a virtio descriptor at given index in given virtqueue. 180 * The descriptor will provide access to the entire descriptor 181 * chain. The subsequent descriptors are accesible via 182 * \c spdk_vhost_vring_desc_get_next. 183 * \param vdev vhost device 184 * \param vq virtqueue 185 * \param req_idx descriptor index 186 * \param desc pointer to be set to the descriptor 187 * \param desc_table descriptor table to be used with 188 * \c spdk_vhost_vring_desc_get_next. This might be either 189 * default virtqueue descriptor table or per-chain indirect 190 * table. 191 * \param desc_table_size size of the *desc_table* 192 * \return 0 on success, -1 if given index is invalid. 193 * If -1 is returned, the content of params is undefined. 194 */ 195 int spdk_vhost_vq_get_desc(struct spdk_vhost_dev *vdev, struct spdk_vhost_virtqueue *vq, 196 uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table, 197 uint32_t *desc_table_size); 198 199 /** 200 * Send IRQ/call client (if pending) for \c vq. 201 * \param vdev vhost device 202 * \param vq virtqueue 203 * \return 204 * 0 - if no interrupt was signalled 205 * 1 - if interrupt was signalled 206 */ 207 int spdk_vhost_vq_used_signal(struct spdk_vhost_dev *vdev, struct spdk_vhost_virtqueue *vq); 208 209 210 /** 211 * Send IRQs for all queues that need to be signaled. 212 * \param vdev vhost device 213 * \param vq virtqueue 214 */ 215 void spdk_vhost_dev_used_signal(struct spdk_vhost_dev *vdev); 216 217 void spdk_vhost_vq_used_ring_enqueue(struct spdk_vhost_dev *vdev, struct spdk_vhost_virtqueue *vq, 218 uint16_t id, uint32_t len); 219 220 /** 221 * Get subsequent descriptor from given table. 222 * \param desc current descriptor, will be set to the 223 * next descriptor (NULL in case this is the last 224 * descriptor in the chain or the next desc is invalid) 225 * \param desc_table descriptor table 226 * \param desc_table_size size of the *desc_table* 227 * \return 0 on success, -1 if given index is invalid 228 * The *desc* param will be set regardless of the 229 * return value. 230 */ 231 int spdk_vhost_vring_desc_get_next(struct vring_desc **desc, 232 struct vring_desc *desc_table, uint32_t desc_table_size); 233 bool spdk_vhost_vring_desc_is_wr(struct vring_desc *cur_desc); 234 235 int spdk_vhost_vring_desc_to_iov(struct spdk_vhost_dev *vdev, struct iovec *iov, 236 uint16_t *iov_index, const struct vring_desc *desc); 237 bool spdk_vhost_dev_has_feature(struct spdk_vhost_dev *vdev, unsigned feature_id); 238 239 int spdk_vhost_dev_construct(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, 240 enum spdk_vhost_dev_type type, const struct spdk_vhost_dev_backend *backend); 241 int spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev); 242 243 int spdk_vhost_scsi_controller_construct(void); 244 int spdk_vhost_blk_controller_construct(void); 245 void spdk_vhost_dump_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 246 void spdk_vhost_dev_backend_event_done(void *event_ctx, int response); 247 void spdk_vhost_lock(void); 248 void spdk_vhost_unlock(void); 249 int spdk_remove_vhost_controller(struct spdk_vhost_dev *vdev); 250 251 #endif /* SPDK_VHOST_INTERNAL_H */ 252