1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #ifndef SPDK_BDEV_RAID_INTERNAL_H 7 #define SPDK_BDEV_RAID_INTERNAL_H 8 9 #include "spdk/bdev_module.h" 10 11 enum raid_level { 12 INVALID_RAID_LEVEL = -1, 13 RAID0 = 0, 14 RAID5F = 95, /* 0x5f */ 15 CONCAT = 99, 16 }; 17 18 /* 19 * Raid state describes the state of the raid. This raid bdev can be either in 20 * configured list or configuring list 21 */ 22 enum raid_bdev_state { 23 /* raid bdev is ready and is seen by upper layers */ 24 RAID_BDEV_STATE_ONLINE, 25 26 /* 27 * raid bdev is configuring, not all underlying bdevs are present. 28 * And can't be seen by upper layers. 29 */ 30 RAID_BDEV_STATE_CONFIGURING, 31 32 /* 33 * In offline state, raid bdev layer will complete all incoming commands without 34 * submitting to underlying base nvme bdevs 35 */ 36 RAID_BDEV_STATE_OFFLINE, 37 38 /* raid bdev max, new states should be added before this */ 39 RAID_BDEV_MAX 40 }; 41 42 /* 43 * raid_base_bdev_info contains information for the base bdevs which are part of some 44 * raid. This structure contains the per base bdev information. Whatever is 45 * required per base device for raid bdev will be kept here 46 */ 47 struct raid_base_bdev_info { 48 /* name of the bdev */ 49 char *name; 50 51 /* pointer to base spdk bdev */ 52 struct spdk_bdev *bdev; 53 54 /* pointer to base bdev descriptor opened by raid bdev */ 55 struct spdk_bdev_desc *desc; 56 57 /* 58 * When underlying base device calls the hot plug function on drive removal, 59 * this flag will be set and later after doing some processing, base device 60 * descriptor will be closed 61 */ 62 bool remove_scheduled; 63 64 /* thread where base device is opened */ 65 struct spdk_thread *thread; 66 }; 67 68 /* 69 * raid_bdev_io is the context part of bdev_io. It contains the information 70 * related to bdev_io for a raid bdev 71 */ 72 struct raid_bdev_io { 73 /* The raid bdev associated with this IO */ 74 struct raid_bdev *raid_bdev; 75 76 /* WaitQ entry, used only in waitq logic */ 77 struct spdk_bdev_io_wait_entry waitq_entry; 78 79 /* Context of the original channel for this IO */ 80 struct raid_bdev_io_channel *raid_ch; 81 82 /* Used for tracking progress on io requests sent to member disks. */ 83 uint64_t base_bdev_io_remaining; 84 uint8_t base_bdev_io_submitted; 85 uint8_t base_bdev_io_status; 86 87 /* Private data for the raid module */ 88 void *module_private; 89 }; 90 91 /* 92 * raid_bdev is the single entity structure which contains SPDK block device 93 * and the information related to any raid bdev either configured or 94 * in configuring list. io device is created on this. 95 */ 96 struct raid_bdev { 97 /* raid bdev device, this will get registered in bdev layer */ 98 struct spdk_bdev bdev; 99 100 /* link of raid bdev to link it to global raid bdev list */ 101 TAILQ_ENTRY(raid_bdev) global_link; 102 103 /* array of base bdev info */ 104 struct raid_base_bdev_info *base_bdev_info; 105 106 /* strip size of raid bdev in blocks */ 107 uint32_t strip_size; 108 109 /* strip size of raid bdev in KB */ 110 uint32_t strip_size_kb; 111 112 /* strip size bit shift for optimized calculation */ 113 uint32_t strip_size_shift; 114 115 /* block length bit shift for optimized calculation */ 116 uint32_t blocklen_shift; 117 118 /* state of raid bdev */ 119 enum raid_bdev_state state; 120 121 /* number of base bdevs comprising raid bdev */ 122 uint8_t num_base_bdevs; 123 124 /* number of base bdevs discovered */ 125 uint8_t num_base_bdevs_discovered; 126 127 /* Raid Level of this raid bdev */ 128 enum raid_level level; 129 130 /* Set to true if destroy of this raid bdev is started. */ 131 bool destroy_started; 132 133 /* Module for RAID-level specific operations */ 134 struct raid_bdev_module *module; 135 136 /* Private data for the raid module */ 137 void *module_private; 138 }; 139 140 #define RAID_FOR_EACH_BASE_BDEV(r, i) \ 141 for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++) 142 143 /* 144 * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It 145 * contains the relationship of raid bdev io channel with base bdev io channels. 146 */ 147 struct raid_bdev_io_channel { 148 /* Array of IO channels of base bdevs */ 149 struct spdk_io_channel **base_channel; 150 151 /* Number of IO channels */ 152 uint8_t num_channels; 153 154 /* Private raid module IO channel */ 155 struct spdk_io_channel *module_channel; 156 }; 157 158 /* TAIL head for raid bdev list */ 159 TAILQ_HEAD(raid_all_tailq, raid_bdev); 160 161 extern struct raid_all_tailq g_raid_bdev_list; 162 163 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc); 164 165 int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs, 166 enum raid_level level, struct raid_bdev **raid_bdev_out); 167 void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx); 168 int raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot); 169 struct raid_bdev *raid_bdev_find_by_name(const char *name); 170 enum raid_level raid_bdev_parse_raid_level(const char *str); 171 const char *raid_bdev_level_to_str(enum raid_level level); 172 173 /* 174 * RAID module descriptor 175 */ 176 struct raid_bdev_module { 177 /* RAID level implemented by this module */ 178 enum raid_level level; 179 180 /* Minimum required number of base bdevs. Must be > 0. */ 181 uint8_t base_bdevs_min; 182 183 /* 184 * Maximum number of base bdevs that can be removed without failing 185 * the array. 186 */ 187 uint8_t base_bdevs_max_degraded; 188 189 /* 190 * Called when the raid is starting, right before changing the state to 191 * online and registering the bdev. Parameters of the bdev like blockcnt 192 * should be set here. 193 * 194 * Non-zero return value will abort the startup process. 195 */ 196 int (*start)(struct raid_bdev *raid_bdev); 197 198 /* 199 * Called when the raid is stopping, right before changing the state to 200 * offline and unregistering the bdev. Optional. 201 * 202 * The function should return false if it is asynchronous. Then, after 203 * the async operation has completed and the module is fully stopped 204 * raid_bdev_module_stop_done() must be called. 205 */ 206 bool (*stop)(struct raid_bdev *raid_bdev); 207 208 /* Handler for R/W requests */ 209 void (*submit_rw_request)(struct raid_bdev_io *raid_io); 210 211 /* Handler for requests without payload (flush, unmap). Optional. */ 212 void (*submit_null_payload_request)(struct raid_bdev_io *raid_io); 213 214 /* 215 * Called when the bdev's IO channel is created to get the module's private IO channel. 216 * Optional. 217 */ 218 struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev); 219 220 TAILQ_ENTRY(raid_bdev_module) link; 221 }; 222 223 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module); 224 225 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line) 226 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line 227 228 #define RAID_MODULE_REGISTER(_module) \ 229 __attribute__((constructor)) static void \ 230 __RAID_MODULE_REGISTER(__LINE__)(void) \ 231 { \ 232 raid_bdev_module_list_add(_module); \ 233 } 234 235 bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, 236 enum spdk_bdev_io_status status); 237 void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 238 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn); 239 void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status); 240 void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev); 241 242 #endif /* SPDK_BDEV_RAID_INTERNAL_H */ 243