1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #ifndef SPDK_VBDEV_OCF_H 7 #define SPDK_VBDEV_OCF_H 8 9 #include <ocf/ocf.h> 10 11 #include "spdk/bdev.h" 12 #include "spdk/bdev_module.h" 13 14 #define VBDEV_OCF_MD_MAX_LEN 4096 15 16 struct vbdev_ocf; 17 18 /* Context for OCF queue poller 19 * Used for mapping SPDK threads to OCF queues */ 20 struct vbdev_ocf_qctx { 21 /* OCF queue. Contains OCF requests */ 22 struct ocf_queue *queue; 23 /* Poller for OCF queue. Runs OCF requests */ 24 struct spdk_poller *poller; 25 /* Reference to parent vbdev */ 26 struct vbdev_ocf *vbdev; 27 /* Base devices channels */ 28 struct spdk_io_channel *cache_ch; 29 struct spdk_io_channel *core_ch; 30 /* If true, we have to free this context on queue stop */ 31 bool allocated; 32 /* Link to per-bdev list of queue contexts */ 33 TAILQ_ENTRY(vbdev_ocf_qctx) tailq; 34 }; 35 36 /* Important states */ 37 struct vbdev_ocf_state { 38 /* From the moment when clean delete started */ 39 bool doing_clean_delete; 40 /* From the moment when finish started */ 41 bool doing_finish; 42 /* From the moment when reset IO received, until it is completed */ 43 bool doing_reset; 44 /* From the moment when exp_bdev is registered */ 45 bool started; 46 /* From the moment when register path started */ 47 bool starting; 48 /* Status of last attempt for stopping this device */ 49 int stop_status; 50 }; 51 52 /* 53 * OCF cache configuration options 54 */ 55 struct vbdev_ocf_config { 56 /* Initial cache configuration */ 57 struct ocf_mngt_cache_config cache; 58 59 /* Cache device config */ 60 struct ocf_mngt_cache_attach_config attach; 61 62 /* Core initial config */ 63 struct ocf_mngt_core_config core; 64 65 /* Load flag, if set to true, then we will try load cache instance from disk, 66 * otherwise we will create new cache on that disk */ 67 bool loadq; 68 }; 69 70 /* Types for management operations */ 71 typedef void (*vbdev_ocf_mngt_fn)(struct vbdev_ocf *); 72 typedef void (*vbdev_ocf_mngt_callback)(int, struct vbdev_ocf *, void *); 73 74 /* Context for asynchronous management operations 75 * Single management operation usually contains a list of sub procedures, 76 * this structure handles sharing between those sub procedures */ 77 struct vbdev_ocf_mngt_ctx { 78 /* Pointer to function that is currently being executed 79 * It gets incremented on each step until it dereferences to NULL */ 80 vbdev_ocf_mngt_fn *current_step; 81 82 /* Function that gets invoked by poller on each iteration */ 83 vbdev_ocf_mngt_fn poller_fn; 84 /* Poller timeout time stamp - when the poller should stop with error */ 85 uint64_t timeout_ts; 86 87 /* Status of management operation */ 88 int status; 89 90 /* External callback and its argument */ 91 vbdev_ocf_mngt_callback cb; 92 void *cb_arg; 93 }; 94 95 /* Base device info */ 96 struct vbdev_ocf_base { 97 /* OCF internal name */ 98 char *name; 99 100 /* True if this is a caching device */ 101 bool is_cache; 102 103 /* Connected SPDK block device */ 104 struct spdk_bdev *bdev; 105 106 /* SPDK device io handle */ 107 struct spdk_bdev_desc *desc; 108 109 /* True if SPDK bdev has been claimed and opened for writing */ 110 bool attached; 111 112 /* Channel for cleaner operations */ 113 struct spdk_io_channel *management_channel; 114 115 /* Reference to main vbdev */ 116 struct vbdev_ocf *parent; 117 118 /* thread where base device is opened */ 119 struct spdk_thread *thread; 120 }; 121 122 /* 123 * The main information provider 124 * It's also registered as io_device 125 */ 126 struct vbdev_ocf { 127 /* Exposed unique name */ 128 char *name; 129 130 /* Base bdevs */ 131 struct vbdev_ocf_base cache; 132 struct vbdev_ocf_base core; 133 134 /* Base bdevs OCF objects */ 135 ocf_cache_t ocf_cache; 136 ocf_core_t ocf_core; 137 138 /* Parameters */ 139 struct vbdev_ocf_config cfg; 140 struct vbdev_ocf_state state; 141 142 /* Management context */ 143 struct vbdev_ocf_mngt_ctx mngt_ctx; 144 145 /* Cache context */ 146 struct vbdev_ocf_cache_ctx *cache_ctx; 147 148 /* Status of flushing operation */ 149 struct { 150 bool in_progress; 151 int status; 152 } flush; 153 154 /* Exposed SPDK bdev. Registered in bdev layer */ 155 struct spdk_bdev exp_bdev; 156 157 /* OCF uuid for core device of this vbdev */ 158 char uuid[VBDEV_OCF_MD_MAX_LEN]; 159 160 /* Link to global list of this type structures */ 161 TAILQ_ENTRY(vbdev_ocf) tailq; 162 }; 163 164 void vbdev_ocf_construct( 165 const char *vbdev_name, 166 const char *cache_mode_name, 167 const uint64_t cache_line_size, 168 const char *cache_name, 169 const char *core_name, 170 bool loadq, 171 void (*cb)(int, struct vbdev_ocf *, void *), 172 void *cb_arg); 173 174 /* If vbdev is online, return its object */ 175 struct vbdev_ocf *vbdev_ocf_get_by_name(const char *name); 176 177 /* Return matching base if parent vbdev is online */ 178 struct vbdev_ocf_base *vbdev_ocf_get_base_by_name(const char *name); 179 180 /* Stop OCF cache and unregister SPDK bdev */ 181 int vbdev_ocf_delete(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg); 182 183 int vbdev_ocf_delete_clean(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg); 184 185 /* Set new cache mode on OCF cache */ 186 void vbdev_ocf_set_cache_mode( 187 struct vbdev_ocf *vbdev, 188 const char *cache_mode_name, 189 void (*cb)(int, struct vbdev_ocf *, void *), 190 void *cb_arg); 191 192 /* Set sequential cutoff parameters on OCF cache */ 193 void vbdev_ocf_set_seqcutoff( 194 struct vbdev_ocf *vbdev, 195 const char *policy_name, 196 uint32_t threshold, 197 uint32_t promotion_count, 198 void (*cb)(int, void *), 199 void *cb_arg); 200 201 typedef void (*vbdev_ocf_foreach_fn)(struct vbdev_ocf *, void *); 202 203 /* Execute fn for each OCF device that is online or waits for base devices */ 204 void vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn, void *ctx); 205 206 #endif 207