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 /* 165 * raid_base_bdev_config is the per base bdev data structure which contains 166 * information w.r.t to per base bdev during parsing config 167 */ 168 struct raid_base_bdev_config { 169 /* base bdev name from config file */ 170 char *name; 171 }; 172 173 /* 174 * raid_bdev_config contains the raid bdev config related information after 175 * parsing the config file 176 */ 177 struct raid_bdev_config { 178 /* base bdev config per underlying bdev */ 179 struct raid_base_bdev_config *base_bdev; 180 181 /* Points to already created raid bdev */ 182 struct raid_bdev *raid_bdev; 183 184 char *name; 185 186 /* strip size of this raid bdev in kilo bytes */ 187 uint32_t strip_size; 188 189 /* number of base bdevs */ 190 uint8_t num_base_bdevs; 191 192 /* raid level */ 193 enum raid_level level; 194 195 TAILQ_ENTRY(raid_bdev_config) link; 196 }; 197 198 /* 199 * raid_config is the top level structure representing the raid bdev config as read 200 * from config file for all raids 201 */ 202 struct raid_config { 203 /* raid bdev context from config file */ 204 TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head; 205 206 /* total raid bdev from config file */ 207 uint8_t total_raid_bdev; 208 }; 209 210 /* 211 * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It 212 * contains the relationship of raid bdev io channel with base bdev io channels. 213 */ 214 struct raid_bdev_io_channel { 215 /* Array of IO channels of base bdevs */ 216 struct spdk_io_channel **base_channel; 217 218 /* Number of IO channels */ 219 uint8_t num_channels; 220 }; 221 222 /* TAIL heads for various raid bdev lists */ 223 TAILQ_HEAD(raid_configured_tailq, raid_bdev); 224 TAILQ_HEAD(raid_configuring_tailq, raid_bdev); 225 TAILQ_HEAD(raid_all_tailq, raid_bdev); 226 TAILQ_HEAD(raid_offline_tailq, raid_bdev); 227 228 extern struct raid_configured_tailq g_raid_bdev_configured_list; 229 extern struct raid_configuring_tailq g_raid_bdev_configuring_list; 230 extern struct raid_all_tailq g_raid_bdev_list; 231 extern struct raid_offline_tailq g_raid_bdev_offline_list; 232 extern struct raid_config g_raid_config; 233 234 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc); 235 236 int raid_bdev_create(struct raid_bdev_config *raid_cfg); 237 int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg); 238 void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, 239 raid_bdev_destruct_cb cb_fn, void *cb_ctx); 240 int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs, 241 enum raid_level level, struct raid_bdev_config **_raid_cfg); 242 int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, 243 const char *base_bdev_name, uint8_t slot); 244 void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg); 245 struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name); 246 enum raid_level raid_bdev_parse_raid_level(const char *str); 247 const char *raid_bdev_level_to_str(enum raid_level level); 248 249 /* 250 * RAID module descriptor 251 */ 252 struct raid_bdev_module { 253 /* RAID level implemented by this module */ 254 enum raid_level level; 255 256 /* 257 * Called when the raid is starting, right before changing the state to 258 * online and registering the bdev. Parameters of the bdev like blockcnt 259 * should be set here. 260 * 261 * Non-zero return value will abort the startup process. 262 */ 263 int (*start)(struct raid_bdev *raid_bdev); 264 265 /* 266 * Called when the raid is stopping, right before changing the state to 267 * offline and unregistering the bdev. Optional. 268 */ 269 void (*stop)(struct raid_bdev *raid_bdev); 270 271 /* Handler for R/W requests */ 272 void (*submit_rw_request)(struct raid_bdev_io *raid_io); 273 274 /* Handler for requests without payload (flush, unmap) */ 275 void (*submit_null_payload_request)(struct raid_bdev_io *raid_io); 276 277 TAILQ_ENTRY(raid_bdev_module) link; 278 }; 279 280 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module); 281 282 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line) 283 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line 284 285 #define RAID_MODULE_REGISTER(_module) \ 286 __attribute__((constructor)) static void \ 287 __RAID_MODULE_REGISTER(__LINE__)(void) \ 288 { \ 289 raid_bdev_module_list_add(_module); \ 290 } 291 292 void 293 raid_bdev_base_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 294 void 295 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 296 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn); 297 void 298 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status); 299 300 #endif /* SPDK_BDEV_RAID_INTERNAL_H */ 301