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_BDEV_RAID_INTERNAL_H 35 #define SPDK_BDEV_RAID_INTERNAL_H 36 37 #include "spdk/bdev_module.h" 38 39 enum raid_level { 40 INVALID_RAID_LEVEL = -1, 41 RAID0 = 0, 42 }; 43 44 /* 45 * Raid state describes the state of the raid. This raid bdev can be either in 46 * configured list or configuring list 47 */ 48 enum raid_bdev_state { 49 /* raid bdev is ready and is seen by upper layers */ 50 RAID_BDEV_STATE_ONLINE, 51 52 /* 53 * raid bdev is configuring, not all underlying bdevs are present. 54 * And can't be seen by upper layers. 55 */ 56 RAID_BDEV_STATE_CONFIGURING, 57 58 /* 59 * In offline state, raid bdev layer will complete all incoming commands without 60 * submitting to underlying base nvme bdevs 61 */ 62 RAID_BDEV_STATE_OFFLINE, 63 64 /* raid bdev max, new states should be added before this */ 65 RAID_BDEV_MAX 66 }; 67 68 /* 69 * raid_base_bdev_info contains information for the base bdevs which are part of some 70 * raid. This structure contains the per base bdev information. Whatever is 71 * required per base device for raid bdev will be kept here 72 */ 73 struct raid_base_bdev_info { 74 /* pointer to base spdk bdev */ 75 struct spdk_bdev *bdev; 76 77 /* pointer to base bdev descriptor opened by raid bdev */ 78 struct spdk_bdev_desc *desc; 79 80 /* 81 * When underlying base device calls the hot plug function on drive removal, 82 * this flag will be set and later after doing some processing, base device 83 * descriptor will be closed 84 */ 85 bool remove_scheduled; 86 }; 87 88 /* 89 * raid_bdev_io is the context part of bdev_io. It contains the information 90 * related to bdev_io for a raid bdev 91 */ 92 struct raid_bdev_io { 93 /* The raid bdev associated with this IO */ 94 struct raid_bdev *raid_bdev; 95 96 /* WaitQ entry, used only in waitq logic */ 97 struct spdk_bdev_io_wait_entry waitq_entry; 98 99 /* Context of the original channel for this IO */ 100 struct raid_bdev_io_channel *raid_ch; 101 102 /* Used for tracking progress on io requests sent to member disks. */ 103 uint8_t base_bdev_io_submitted; 104 uint8_t base_bdev_io_completed; 105 uint8_t base_bdev_io_expected; 106 uint8_t base_bdev_io_status; 107 }; 108 109 /* 110 * raid_bdev is the single entity structure which contains SPDK block device 111 * and the information related to any raid bdev either configured or 112 * in configuring list. io device is created on this. 113 */ 114 struct raid_bdev { 115 /* raid bdev device, this will get registered in bdev layer */ 116 struct spdk_bdev bdev; 117 118 /* link of raid bdev to link it to configured, configuring or offline list */ 119 TAILQ_ENTRY(raid_bdev) state_link; 120 121 /* link of raid bdev to link it to global raid bdev list */ 122 TAILQ_ENTRY(raid_bdev) global_link; 123 124 /* pointer to config file entry */ 125 struct raid_bdev_config *config; 126 127 /* array of base bdev info */ 128 struct raid_base_bdev_info *base_bdev_info; 129 130 /* strip size of raid bdev in blocks */ 131 uint32_t strip_size; 132 133 /* strip size of raid bdev in KB */ 134 uint32_t strip_size_kb; 135 136 /* strip size bit shift for optimized calculation */ 137 uint32_t strip_size_shift; 138 139 /* block length bit shift for optimized calculation */ 140 uint32_t blocklen_shift; 141 142 /* state of raid bdev */ 143 enum raid_bdev_state state; 144 145 /* number of base bdevs comprising raid bdev */ 146 uint8_t num_base_bdevs; 147 148 /* number of base bdevs discovered */ 149 uint8_t num_base_bdevs_discovered; 150 151 /* Raid Level of this raid bdev */ 152 enum raid_level level; 153 154 /* Set to true if destruct is called for this raid bdev */ 155 bool destruct_called; 156 157 /* Set to true if destroy of this raid bdev is started. */ 158 bool destroy_started; 159 160 /* Module for RAID-level specific operations */ 161 struct raid_bdev_module *module; 162 }; 163 164 #define RAID_FOR_EACH_BASE_BDEV(r, i) \ 165 for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++) 166 167 /* 168 * raid_base_bdev_config is the per base bdev data structure which contains 169 * information w.r.t to per base bdev during parsing config 170 */ 171 struct raid_base_bdev_config { 172 /* base bdev name from config file */ 173 char *name; 174 }; 175 176 /* 177 * raid_bdev_config contains the raid bdev config related information after 178 * parsing the config file 179 */ 180 struct raid_bdev_config { 181 /* base bdev config per underlying bdev */ 182 struct raid_base_bdev_config *base_bdev; 183 184 /* Points to already created raid bdev */ 185 struct raid_bdev *raid_bdev; 186 187 char *name; 188 189 /* strip size of this raid bdev in kilo bytes */ 190 uint32_t strip_size; 191 192 /* number of base bdevs */ 193 uint8_t num_base_bdevs; 194 195 /* raid level */ 196 enum raid_level level; 197 198 TAILQ_ENTRY(raid_bdev_config) link; 199 }; 200 201 /* 202 * raid_config is the top level structure representing the raid bdev config as read 203 * from config file for all raids 204 */ 205 struct raid_config { 206 /* raid bdev context from config file */ 207 TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head; 208 209 /* total raid bdev from config file */ 210 uint8_t total_raid_bdev; 211 }; 212 213 /* 214 * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It 215 * contains the relationship of raid bdev io channel with base bdev io channels. 216 */ 217 struct raid_bdev_io_channel { 218 /* Array of IO channels of base bdevs */ 219 struct spdk_io_channel **base_channel; 220 221 /* Number of IO channels */ 222 uint8_t num_channels; 223 }; 224 225 /* TAIL heads for various raid bdev lists */ 226 TAILQ_HEAD(raid_configured_tailq, raid_bdev); 227 TAILQ_HEAD(raid_configuring_tailq, raid_bdev); 228 TAILQ_HEAD(raid_all_tailq, raid_bdev); 229 TAILQ_HEAD(raid_offline_tailq, raid_bdev); 230 231 extern struct raid_configured_tailq g_raid_bdev_configured_list; 232 extern struct raid_configuring_tailq g_raid_bdev_configuring_list; 233 extern struct raid_all_tailq g_raid_bdev_list; 234 extern struct raid_offline_tailq g_raid_bdev_offline_list; 235 extern struct raid_config g_raid_config; 236 237 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc); 238 239 int raid_bdev_create(struct raid_bdev_config *raid_cfg); 240 int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg); 241 void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, 242 raid_bdev_destruct_cb cb_fn, void *cb_ctx); 243 int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs, 244 enum raid_level level, struct raid_bdev_config **_raid_cfg); 245 int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, 246 const char *base_bdev_name, uint8_t slot); 247 void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg); 248 struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name); 249 enum raid_level raid_bdev_parse_raid_level(const char *str); 250 const char *raid_bdev_level_to_str(enum raid_level level); 251 252 /* 253 * RAID module descriptor 254 */ 255 struct raid_bdev_module { 256 /* RAID level implemented by this module */ 257 enum raid_level level; 258 259 /* Minimum required number of base bdevs. Must be > 0. */ 260 uint8_t base_bdevs_min; 261 262 /* 263 * Called when the raid is starting, right before changing the state to 264 * online and registering the bdev. Parameters of the bdev like blockcnt 265 * should be set here. 266 * 267 * Non-zero return value will abort the startup process. 268 */ 269 int (*start)(struct raid_bdev *raid_bdev); 270 271 /* 272 * Called when the raid is stopping, right before changing the state to 273 * offline and unregistering the bdev. Optional. 274 */ 275 void (*stop)(struct raid_bdev *raid_bdev); 276 277 /* Handler for R/W requests */ 278 void (*submit_rw_request)(struct raid_bdev_io *raid_io); 279 280 /* Handler for requests without payload (flush, unmap). Optional. */ 281 void (*submit_null_payload_request)(struct raid_bdev_io *raid_io); 282 283 TAILQ_ENTRY(raid_bdev_module) link; 284 }; 285 286 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module); 287 288 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line) 289 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line 290 291 #define RAID_MODULE_REGISTER(_module) \ 292 __attribute__((constructor)) static void \ 293 __RAID_MODULE_REGISTER(__LINE__)(void) \ 294 { \ 295 raid_bdev_module_list_add(_module); \ 296 } 297 298 void 299 raid_bdev_base_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 300 void 301 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 302 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn); 303 void 304 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status); 305 306 #endif /* SPDK_BDEV_RAID_INTERNAL_H */ 307