Javier González | 02a1520 | 2018-10-09 13:12:06 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 IT University of Copenhagen (rrpc.h) |
| 4 | * Copyright (C) 2016 CNEX Labs |
| 5 | * Initial release: Matias Bjorling <matias@cnexlabs.com> |
| 6 | * Write buffering: Javier Gonzalez <javier@cnexlabs.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License version |
| 10 | * 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * Implementation of a Physical Block-device target for Open-channel SSDs. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #ifndef PBLK_H_ |
| 22 | #define PBLK_H_ |
| 23 | |
| 24 | #include <linux/blkdev.h> |
| 25 | #include <linux/blk-mq.h> |
| 26 | #include <linux/bio.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/kthread.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/crc32.h> |
| 31 | #include <linux/uuid.h> |
| 32 | |
| 33 | #include <linux/lightnvm.h> |
| 34 | |
| 35 | /* Run only GC if less than 1/X blocks are free */ |
| 36 | #define GC_LIMIT_INVERSE 5 |
| 37 | #define GC_TIME_MSECS 1000 |
| 38 | |
| 39 | #define PBLK_SECTOR (512) |
| 40 | #define PBLK_EXPOSED_PAGE_SIZE (4096) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 41 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 42 | #define PBLK_NR_CLOSE_JOBS (4) |
| 43 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 44 | #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16) |
| 45 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 46 | /* Max 512 LUNs per device */ |
| 47 | #define PBLK_MAX_LUNS_BITMAP (4) |
| 48 | |
| 49 | #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR) |
| 50 | |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 51 | /* Static pool sizes */ |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 52 | #define PBLK_GEN_WS_POOL_SIZE (2) |
| 53 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 54 | #define PBLK_DEFAULT_OP (11) |
| 55 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 56 | enum { |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 57 | PBLK_READ = READ, |
| 58 | PBLK_WRITE = WRITE,/* Write from write buffer */ |
| 59 | PBLK_WRITE_INT, /* Internal write - no write buffer */ |
Javier González | 8f55459 | 2018-01-05 14:16:16 +0100 | [diff] [blame] | 60 | PBLK_READ_RECOV, /* Recovery read - errors allowed */ |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 61 | PBLK_ERASE, |
| 62 | }; |
| 63 | |
| 64 | enum { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 65 | /* IO Types */ |
| 66 | PBLK_IOTYPE_USER = 1 << 0, |
| 67 | PBLK_IOTYPE_GC = 1 << 1, |
| 68 | |
| 69 | /* Write buffer flags */ |
| 70 | PBLK_FLUSH_ENTRY = 1 << 2, |
| 71 | PBLK_WRITTEN_DATA = 1 << 3, |
| 72 | PBLK_SUBMITTED_ENTRY = 1 << 4, |
| 73 | PBLK_WRITABLE_ENTRY = 1 << 5, |
| 74 | }; |
| 75 | |
| 76 | enum { |
| 77 | PBLK_BLK_ST_OPEN = 0x1, |
| 78 | PBLK_BLK_ST_CLOSED = 0x2, |
| 79 | }; |
| 80 | |
Hans Holmberg | 4209c31 | 2018-10-09 13:11:55 +0200 | [diff] [blame] | 81 | enum { |
| 82 | PBLK_CHUNK_RESET_START, |
| 83 | PBLK_CHUNK_RESET_DONE, |
| 84 | PBLK_CHUNK_RESET_FAILED, |
| 85 | }; |
| 86 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 87 | struct pblk_sec_meta { |
| 88 | u64 reserved; |
| 89 | __le64 lba; |
| 90 | }; |
| 91 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 92 | /* The number of GC lists and the rate-limiter states go together. This way the |
| 93 | * rate-limiter can dictate how much GC is needed based on resource utilization. |
| 94 | */ |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 95 | #define PBLK_GC_NR_LISTS 4 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 96 | |
| 97 | enum { |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 98 | PBLK_RL_OFF = 0, |
| 99 | PBLK_RL_WERR = 1, |
| 100 | PBLK_RL_HIGH = 2, |
| 101 | PBLK_RL_MID = 3, |
| 102 | PBLK_RL_LOW = 4 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame] | 105 | #define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 106 | |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 107 | /* write buffer completion context */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 108 | struct pblk_c_ctx { |
| 109 | struct list_head list; /* Head for out-of-order completion */ |
| 110 | |
| 111 | unsigned long *lun_bitmap; /* Luns used on current request */ |
| 112 | unsigned int sentry; |
| 113 | unsigned int nr_valid; |
| 114 | unsigned int nr_padded; |
| 115 | }; |
| 116 | |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 117 | /* read context */ |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 118 | struct pblk_g_ctx { |
| 119 | void *private; |
Javier González | 998ba62 | 2018-01-05 14:16:20 +0100 | [diff] [blame] | 120 | unsigned long start_time; |
Javier González | a4809fe | 2017-10-13 14:46:22 +0200 | [diff] [blame] | 121 | u64 lba; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 124 | /* partial read context */ |
| 125 | struct pblk_pr_ctx { |
| 126 | struct bio *orig_bio; |
| 127 | DECLARE_BITMAP(bitmap, NVM_MAX_VLBA); |
| 128 | unsigned int orig_nr_secs; |
| 129 | unsigned int bio_init_idx; |
| 130 | void *ppa_ptr; |
| 131 | dma_addr_t dma_ppa_list; |
Andy Shevchenko | e74ecf6 | 2019-02-11 13:25:04 +0100 | [diff] [blame] | 132 | u64 lba_list_mem[NVM_MAX_VLBA]; |
| 133 | u64 lba_list_media[NVM_MAX_VLBA]; |
Heiner Litz | 11f6ad6 | 2018-07-13 10:48:44 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 136 | /* Pad context */ |
| 137 | struct pblk_pad_rq { |
| 138 | struct pblk *pblk; |
| 139 | struct completion wait; |
| 140 | struct kref ref; |
| 141 | }; |
| 142 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 143 | /* Recovery context */ |
| 144 | struct pblk_rec_ctx { |
| 145 | struct pblk *pblk; |
| 146 | struct nvm_rq *rqd; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 147 | struct work_struct ws_rec; |
| 148 | }; |
| 149 | |
| 150 | /* Write context */ |
| 151 | struct pblk_w_ctx { |
| 152 | struct bio_list bios; /* Original bios - used for completion |
| 153 | * in REQ_FUA, REQ_FLUSH case |
| 154 | */ |
Arnd Bergmann | ef69790 | 2017-04-19 19:39:13 +0200 | [diff] [blame] | 155 | u64 lba; /* Logic addr. associated with entry */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 156 | struct ppa_addr ppa; /* Physic addr. associated with entry */ |
| 157 | int flags; /* Write context flags */ |
| 158 | }; |
| 159 | |
| 160 | struct pblk_rb_entry { |
| 161 | struct ppa_addr cacheline; /* Cacheline for this entry */ |
| 162 | void *data; /* Pointer to data on this entry */ |
| 163 | struct pblk_w_ctx w_ctx; /* Context for this entry */ |
| 164 | struct list_head index; /* List head to enable indexes */ |
| 165 | }; |
| 166 | |
| 167 | #define EMPTY_ENTRY (~0U) |
| 168 | |
| 169 | struct pblk_rb_pages { |
| 170 | struct page *pages; |
| 171 | int order; |
| 172 | struct list_head list; |
| 173 | }; |
| 174 | |
| 175 | struct pblk_rb { |
| 176 | struct pblk_rb_entry *entries; /* Ring buffer entries */ |
| 177 | unsigned int mem; /* Write offset - points to next |
| 178 | * writable entry in memory |
| 179 | */ |
| 180 | unsigned int subm; /* Read offset - points to last entry |
| 181 | * that has been submitted to the media |
| 182 | * to be persisted |
| 183 | */ |
| 184 | unsigned int sync; /* Synced - backpointer that signals |
| 185 | * the last submitted entry that has |
| 186 | * been successfully persisted to media |
| 187 | */ |
Hans Holmberg | 8154d29 | 2018-01-05 14:16:09 +0100 | [diff] [blame] | 188 | unsigned int flush_point; /* Sync point - last entry that must be |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 189 | * flushed to the media. Used with |
| 190 | * REQ_FLUSH and REQ_FUA |
| 191 | */ |
| 192 | unsigned int l2p_update; /* l2p update point - next entry for |
| 193 | * which l2p mapping will be updated to |
| 194 | * contain a device ppa address (instead |
| 195 | * of a cacheline |
| 196 | */ |
| 197 | unsigned int nr_entries; /* Number of entries in write buffer - |
| 198 | * must be a power of two |
| 199 | */ |
| 200 | unsigned int seg_size; /* Size of the data segments being |
| 201 | * stored on each entry. Typically this |
| 202 | * will be 4KB |
| 203 | */ |
| 204 | |
Javier González | 766c8ce | 2018-10-09 13:12:15 +0200 | [diff] [blame] | 205 | unsigned int back_thres; /* Threshold that shall be maintained by |
| 206 | * the backpointer in order to respect |
| 207 | * geo->mw_cunits on a per chunk basis |
| 208 | */ |
| 209 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 210 | struct list_head pages; /* List of data pages */ |
| 211 | |
| 212 | spinlock_t w_lock; /* Write lock */ |
| 213 | spinlock_t s_lock; /* Sync lock */ |
| 214 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 215 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Hans Holmberg | 8154d29 | 2018-01-05 14:16:09 +0100 | [diff] [blame] | 216 | atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 217 | #endif |
| 218 | }; |
| 219 | |
| 220 | #define PBLK_RECOVERY_SECTORS 16 |
| 221 | |
| 222 | struct pblk_lun { |
| 223 | struct ppa_addr bppa; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 224 | struct semaphore wr_sem; |
| 225 | }; |
| 226 | |
| 227 | struct pblk_gc_rq { |
| 228 | struct pblk_line *line; |
| 229 | void *data; |
Matias Bjørling | afdc23c | 2018-10-09 13:11:37 +0200 | [diff] [blame] | 230 | u64 paddr_list[NVM_MAX_VLBA]; |
| 231 | u64 lba_list[NVM_MAX_VLBA]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 232 | int nr_secs; |
| 233 | int secs_to_gc; |
| 234 | struct list_head list; |
| 235 | }; |
| 236 | |
| 237 | struct pblk_gc { |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 238 | /* These states are not protected by a lock since (i) they are in the |
| 239 | * fast path, and (ii) they are not critical. |
| 240 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 241 | int gc_active; |
| 242 | int gc_enabled; |
| 243 | int gc_forced; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 244 | |
| 245 | struct task_struct *gc_ts; |
| 246 | struct task_struct *gc_writer_ts; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 247 | struct task_struct *gc_reader_ts; |
| 248 | |
| 249 | struct workqueue_struct *gc_line_reader_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 250 | struct workqueue_struct *gc_reader_wq; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 251 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 252 | struct timer_list gc_timer; |
| 253 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 254 | struct semaphore gc_sem; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 255 | atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */ |
| 256 | atomic_t pipeline_gc; /* Number of lines in the GC pipeline - |
| 257 | * started reads to finished writes |
| 258 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 259 | int w_entries; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 260 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 261 | struct list_head w_list; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 262 | struct list_head r_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 263 | |
| 264 | spinlock_t lock; |
| 265 | spinlock_t w_lock; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 266 | spinlock_t r_lock; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | struct pblk_rl { |
| 270 | unsigned int high; /* Upper threshold for rate limiter (free run - |
| 271 | * user I/O rate limiter |
| 272 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 273 | unsigned int high_pw; /* High rounded up as a power of 2 */ |
| 274 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 275 | #define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */ |
| 276 | #define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 277 | |
| 278 | int rb_windows_pw; /* Number of rate windows in the write buffer |
| 279 | * given as a power-of-2. This guarantees that |
| 280 | * when user I/O is being rate limited, there |
| 281 | * will be reserved enough space for the GC to |
| 282 | * place its payload. A window is of |
| 283 | * pblk->max_write_pgs size, which in NVMe is |
| 284 | * 64, i.e., 256kb. |
| 285 | */ |
| 286 | int rb_budget; /* Total number of entries available for I/O */ |
| 287 | int rb_user_max; /* Max buffer entries available for user I/O */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 288 | int rb_gc_max; /* Max buffer entries available for GC I/O */ |
| 289 | int rb_gc_rsv; /* Reserved buffer entries for GC I/O */ |
| 290 | int rb_state; /* Rate-limiter current state */ |
Javier González | da67e68f | 2017-10-13 14:46:05 +0200 | [diff] [blame] | 291 | int rb_max_io; /* Maximum size for an I/O giving the config */ |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 292 | |
| 293 | atomic_t rb_user_cnt; /* User I/O buffer counter */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 294 | atomic_t rb_gc_cnt; /* GC I/O buffer counter */ |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 295 | atomic_t rb_space; /* Space limit in case of reaching capacity */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 296 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 297 | int rsv_blocks; /* Reserved blocks for GC */ |
| 298 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 299 | int rb_user_active; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 300 | int rb_gc_active; |
| 301 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 302 | atomic_t werr_lines; /* Number of write error lines that needs gc */ |
| 303 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 304 | struct timer_list u_timer; |
| 305 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 306 | unsigned long total_blocks; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 307 | |
| 308 | atomic_t free_blocks; /* Total number of free blocks (+ OP) */ |
| 309 | atomic_t free_user_blocks; /* Number of user free blocks (no OP) */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 310 | }; |
| 311 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 312 | #define PBLK_LINE_EMPTY (~0U) |
| 313 | |
| 314 | enum { |
| 315 | /* Line Types */ |
| 316 | PBLK_LINETYPE_FREE = 0, |
| 317 | PBLK_LINETYPE_LOG = 1, |
| 318 | PBLK_LINETYPE_DATA = 2, |
| 319 | |
| 320 | /* Line state */ |
Javier González | 32ef941 | 2018-03-30 00:05:20 +0200 | [diff] [blame] | 321 | PBLK_LINESTATE_NEW = 9, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 322 | PBLK_LINESTATE_FREE = 10, |
| 323 | PBLK_LINESTATE_OPEN = 11, |
| 324 | PBLK_LINESTATE_CLOSED = 12, |
| 325 | PBLK_LINESTATE_GC = 13, |
| 326 | PBLK_LINESTATE_BAD = 14, |
| 327 | PBLK_LINESTATE_CORRUPT = 15, |
| 328 | |
| 329 | /* GC group */ |
| 330 | PBLK_LINEGC_NONE = 20, |
| 331 | PBLK_LINEGC_EMPTY = 21, |
| 332 | PBLK_LINEGC_LOW = 22, |
| 333 | PBLK_LINEGC_MID = 23, |
| 334 | PBLK_LINEGC_HIGH = 24, |
| 335 | PBLK_LINEGC_FULL = 25, |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 336 | PBLK_LINEGC_WERR = 26 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | #define PBLK_MAGIC 0x70626c6b /*pblk*/ |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 340 | |
| 341 | /* emeta/smeta persistent storage format versions: |
| 342 | * Changes in major version requires offline migration. |
| 343 | * Changes in minor version are handled automatically during |
| 344 | * recovery. |
| 345 | */ |
| 346 | |
| 347 | #define SMETA_VERSION_MAJOR (0) |
| 348 | #define SMETA_VERSION_MINOR (1) |
| 349 | |
| 350 | #define EMETA_VERSION_MAJOR (0) |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 351 | #define EMETA_VERSION_MINOR (2) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 352 | |
| 353 | struct line_header { |
| 354 | __le32 crc; |
| 355 | __le32 identifier; /* pblk identifier */ |
| 356 | __u8 uuid[16]; /* instance uuid */ |
| 357 | __le16 type; /* line type */ |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame] | 358 | __u8 version_major; /* version major */ |
| 359 | __u8 version_minor; /* version minor */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 360 | __le32 id; /* line id for current line */ |
| 361 | }; |
| 362 | |
| 363 | struct line_smeta { |
| 364 | struct line_header header; |
| 365 | |
| 366 | __le32 crc; /* Full structure including struct crc */ |
| 367 | /* Previous line metadata */ |
| 368 | __le32 prev_id; /* Line id for previous line */ |
| 369 | |
| 370 | /* Current line metadata */ |
| 371 | __le64 seq_nr; /* Sequence number for current line */ |
| 372 | |
| 373 | /* Active writers */ |
| 374 | __le32 window_wr_lun; /* Number of parallel LUNs to write */ |
| 375 | |
| 376 | __le32 rsvd[2]; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 377 | |
| 378 | __le64 lun_bitmap[]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 379 | }; |
| 380 | |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 381 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 382 | /* |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 383 | * Metadata layout in media: |
| 384 | * First sector: |
| 385 | * 1. struct line_emeta |
| 386 | * 2. bad block bitmap (u64 * window_wr_lun) |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 387 | * 3. write amplification counters |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 388 | * Mid sectors (start at lbas_sector): |
| 389 | * 3. nr_lbas (u64) forming lba list |
| 390 | * Last sectors (start at vsc_sector): |
| 391 | * 4. u32 valid sector count (vsc) for all lines (~0U: free line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 392 | */ |
| 393 | struct line_emeta { |
| 394 | struct line_header header; |
| 395 | |
| 396 | __le32 crc; /* Full structure including struct crc */ |
| 397 | |
| 398 | /* Previous line metadata */ |
| 399 | __le32 prev_id; /* Line id for prev line */ |
| 400 | |
| 401 | /* Current line metadata */ |
| 402 | __le64 seq_nr; /* Sequence number for current line */ |
| 403 | |
| 404 | /* Active writers */ |
| 405 | __le32 window_wr_lun; /* Number of parallel LUNs to write */ |
| 406 | |
| 407 | /* Bookkeeping for recovery */ |
| 408 | __le32 next_id; /* Line id for next line */ |
| 409 | __le64 nr_lbas; /* Number of lbas mapped in line */ |
| 410 | __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */ |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 411 | __le64 bb_bitmap[]; /* Updated bad block bitmap for line */ |
| 412 | }; |
| 413 | |
| 414 | |
| 415 | /* Write amplification counters stored on media */ |
| 416 | struct wa_counters { |
| 417 | __le64 user; /* Number of user written sectors */ |
| 418 | __le64 gc; /* Number of sectors written by GC*/ |
| 419 | __le64 pad; /* Number of padded sectors */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | struct pblk_emeta { |
| 423 | struct line_emeta *buf; /* emeta buffer in media format */ |
| 424 | int mem; /* Write offset - points to next |
| 425 | * writable entry in memory |
| 426 | */ |
| 427 | atomic_t sync; /* Synced - backpointer that signals the |
| 428 | * last entry that has been successfully |
| 429 | * persisted to media |
| 430 | */ |
| 431 | unsigned int nr_entries; /* Number of emeta entries */ |
| 432 | }; |
| 433 | |
| 434 | struct pblk_smeta { |
| 435 | struct line_smeta *buf; /* smeta buffer in persistent format */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 436 | }; |
| 437 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 438 | struct pblk_w_err_gc { |
| 439 | int has_write_err; |
Igor Konopko | f2e0245 | 2019-05-04 20:38:05 +0200 | [diff] [blame] | 440 | int has_gc_err; |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 441 | __le64 *lba_list; |
| 442 | }; |
| 443 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 444 | struct pblk_line { |
| 445 | struct pblk *pblk; |
| 446 | unsigned int id; /* Line number corresponds to the |
| 447 | * block line |
| 448 | */ |
| 449 | unsigned int seq_nr; /* Unique line sequence number */ |
| 450 | |
| 451 | int state; /* PBLK_LINESTATE_X */ |
| 452 | int type; /* PBLK_LINETYPE_X */ |
| 453 | int gc_group; /* PBLK_LINEGC_X */ |
| 454 | struct list_head list; /* Free, GC lists */ |
| 455 | |
| 456 | unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */ |
| 457 | |
Javier González | 32ef941 | 2018-03-30 00:05:20 +0200 | [diff] [blame] | 458 | struct nvm_chk_meta *chks; /* Chunks forming line */ |
| 459 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 460 | struct pblk_smeta *smeta; /* Start metadata */ |
| 461 | struct pblk_emeta *emeta; /* End medatada */ |
| 462 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 463 | int meta_line; /* Metadata line id */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 464 | int meta_distance; /* Distance between data and metadata */ |
| 465 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 466 | u64 emeta_ssec; /* Sector where emeta starts */ |
| 467 | |
| 468 | unsigned int sec_in_line; /* Number of usable secs in line */ |
| 469 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 470 | atomic_t blk_in_line; /* Number of good blocks in line */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 471 | unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */ |
| 472 | unsigned long *erase_bitmap; /* Bitmap for erased blocks */ |
| 473 | |
| 474 | unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */ |
| 475 | unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */ |
| 476 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 477 | atomic_t left_eblks; /* Blocks left for erasing */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 478 | atomic_t left_seblks; /* Blocks left for sync erasing */ |
| 479 | |
| 480 | int left_msecs; /* Sectors left for mapping */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 481 | unsigned int cur_sec; /* Sector map pointer */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 482 | unsigned int nr_valid_lbas; /* Number of valid lbas in line */ |
| 483 | |
| 484 | __le32 *vsc; /* Valid sector count in line */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 485 | |
| 486 | struct kref ref; /* Write buffer L2P references */ |
Heiner Litz | 0586942 | 2019-02-11 13:25:09 +0100 | [diff] [blame] | 487 | atomic_t sec_to_update; /* Outstanding L2P updates to ppa */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 488 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 489 | struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */ |
| 490 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 491 | spinlock_t lock; /* Necessary for invalid_bitmap only */ |
| 492 | }; |
| 493 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 494 | #define PBLK_DATA_LINES 4 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 495 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 496 | enum { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 497 | PBLK_KMALLOC_META = 1, |
| 498 | PBLK_VMALLOC_META = 2, |
| 499 | }; |
| 500 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 501 | enum { |
| 502 | PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */ |
| 503 | PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */ |
| 504 | PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | struct pblk_line_mgmt { |
| 508 | int nr_lines; /* Total number of full lines */ |
| 509 | int nr_free_lines; /* Number of full lines in free list */ |
| 510 | |
| 511 | /* Free lists - use free_lock */ |
| 512 | struct list_head free_list; /* Full lines ready to use */ |
| 513 | struct list_head corrupt_list; /* Full lines corrupted */ |
| 514 | struct list_head bad_list; /* Full lines bad */ |
| 515 | |
| 516 | /* GC lists - use gc_lock */ |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 517 | struct list_head *gc_lists[PBLK_GC_NR_LISTS]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 518 | struct list_head gc_high_list; /* Full lines ready to GC, high isc */ |
| 519 | struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */ |
| 520 | struct list_head gc_low_list; /* Full lines ready to GC, low isc */ |
| 521 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 522 | struct list_head gc_werr_list; /* Write err recovery list */ |
| 523 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 524 | struct list_head gc_full_list; /* Full lines ready to GC, no valid */ |
| 525 | struct list_head gc_empty_list; /* Full lines close, all valid */ |
| 526 | |
| 527 | struct pblk_line *log_line; /* Current FTL log line */ |
| 528 | struct pblk_line *data_line; /* Current data line */ |
| 529 | struct pblk_line *log_next; /* Next FTL log line */ |
| 530 | struct pblk_line *data_next; /* Next data line */ |
| 531 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 532 | struct list_head emeta_list; /* Lines queued to schedule emeta */ |
| 533 | |
| 534 | __le32 *vsc_list; /* Valid sector counts for all lines */ |
| 535 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 536 | /* Metadata allocation type: VMALLOC | KMALLOC */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 537 | int emeta_alloc_type; |
| 538 | |
| 539 | /* Pre-allocated metadata for data lines */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 540 | struct pblk_smeta *sline_meta[PBLK_DATA_LINES]; |
| 541 | struct pblk_emeta *eline_meta[PBLK_DATA_LINES]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 542 | unsigned long meta_bitmap; |
| 543 | |
Hans Holmberg | 53d82db | 2018-10-09 13:11:47 +0200 | [diff] [blame] | 544 | /* Cache and mempool for map/invalid bitmaps */ |
| 545 | struct kmem_cache *bitmap_cache; |
| 546 | mempool_t *bitmap_pool; |
| 547 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 548 | /* Helpers for fast bitmap calculations */ |
| 549 | unsigned long *bb_template; |
| 550 | unsigned long *bb_aux; |
| 551 | |
| 552 | unsigned long d_seq_nr; /* Data line unique sequence number */ |
| 553 | unsigned long l_seq_nr; /* Log line unique sequence number */ |
| 554 | |
| 555 | spinlock_t free_lock; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 556 | spinlock_t close_lock; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 557 | spinlock_t gc_lock; |
| 558 | }; |
| 559 | |
| 560 | struct pblk_line_meta { |
| 561 | unsigned int smeta_len; /* Total length for smeta */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 562 | unsigned int smeta_sec; /* Sectors needed for smeta */ |
| 563 | |
| 564 | unsigned int emeta_len[4]; /* Lengths for emeta: |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 565 | * [0]: Total |
| 566 | * [1]: struct line_emeta + |
| 567 | * bb_bitmap + struct wa_counters |
| 568 | * [2]: L2P portion |
| 569 | * [3]: vsc |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 570 | */ |
| 571 | unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout |
| 572 | * as emeta_len |
| 573 | */ |
| 574 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 575 | unsigned int emeta_bb; /* Boundary for bb that affects emeta */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 576 | |
| 577 | unsigned int vsc_list_len; /* Length for vsc list */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 578 | unsigned int sec_bitmap_len; /* Length for sector bitmap in line */ |
| 579 | unsigned int blk_bitmap_len; /* Length for block bitmap in line */ |
| 580 | unsigned int lun_bitmap_len; /* Length for lun bitmap in line */ |
| 581 | |
| 582 | unsigned int blk_per_line; /* Number of blocks in a full line */ |
| 583 | unsigned int sec_per_line; /* Number of sectors in a line */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 584 | unsigned int dsec_per_line; /* Number of data sectors in a line */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 585 | unsigned int min_blk_line; /* Min. number of good blocks in line */ |
| 586 | |
| 587 | unsigned int mid_thrs; /* Threshold for GC mid list */ |
| 588 | unsigned int high_thrs; /* Threshold for GC high list */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 589 | |
| 590 | unsigned int meta_distance; /* Distance between data and metadata */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 591 | }; |
| 592 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 593 | enum { |
| 594 | PBLK_STATE_RUNNING = 0, |
| 595 | PBLK_STATE_STOPPING = 1, |
| 596 | PBLK_STATE_RECOVERING = 2, |
| 597 | PBLK_STATE_STOPPED = 3, |
| 598 | }; |
| 599 | |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 600 | /* Internal format to support not power-of-2 device formats */ |
| 601 | struct pblk_addrf { |
| 602 | /* gen to dev */ |
| 603 | int sec_stripe; |
| 604 | int ch_stripe; |
| 605 | int lun_stripe; |
| 606 | |
| 607 | /* dev to gen */ |
| 608 | int sec_lun_stripe; |
| 609 | int sec_ws_stripe; |
| 610 | }; |
| 611 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 612 | struct pblk { |
| 613 | struct nvm_tgt_dev *dev; |
| 614 | struct gendisk *disk; |
| 615 | |
| 616 | struct kobject kobj; |
| 617 | |
| 618 | struct pblk_lun *luns; |
| 619 | |
| 620 | struct pblk_line *lines; /* Line array */ |
| 621 | struct pblk_line_mgmt l_mg; /* Line management */ |
| 622 | struct pblk_line_meta lm; /* Line metadata */ |
| 623 | |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 624 | struct nvm_addrf addrf; /* Aligned address format */ |
| 625 | struct pblk_addrf uaddrf; /* Unaligned address format */ |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame] | 626 | int addrf_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 627 | |
| 628 | struct pblk_rb rwb; |
| 629 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 630 | int state; /* pblk line state */ |
| 631 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 632 | int min_write_pgs; /* Minimum amount of pages required by controller */ |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 633 | int min_write_pgs_data; /* Minimum amount of payload pages */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 634 | int max_write_pgs; /* Maximum amount of pages supported by controller */ |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 635 | int oob_meta_size; /* Size of OOB sector metadata */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 636 | |
| 637 | sector_t capacity; /* Device capacity when bad blocks are subtracted */ |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 638 | |
| 639 | int op; /* Percentage of device used for over-provisioning */ |
| 640 | int op_blks; /* Number of blocks used for over-provisioning */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 641 | |
| 642 | /* pblk provisioning values. Used by rate limiter */ |
| 643 | struct pblk_rl rl; |
| 644 | |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 645 | int sec_per_write; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 646 | |
Andy Shevchenko | 7e0a084 | 2019-02-11 13:25:05 +0100 | [diff] [blame] | 647 | guid_t instance_uuid; |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 648 | |
| 649 | /* Persistent write amplification counters, 4kb sector I/Os */ |
| 650 | atomic64_t user_wa; /* Sectors written by user */ |
| 651 | atomic64_t gc_wa; /* Sectors written by GC */ |
| 652 | atomic64_t pad_wa; /* Padded sectors written */ |
| 653 | |
| 654 | /* Reset values for delta write amplification measurements */ |
| 655 | u64 user_rst_wa; |
| 656 | u64 gc_rst_wa; |
| 657 | u64 pad_rst_wa; |
| 658 | |
Hans Holmberg | 5d149bf | 2018-03-30 00:04:55 +0200 | [diff] [blame] | 659 | /* Counters used for calculating padding distribution */ |
| 660 | atomic64_t *pad_dist; /* Padding distribution buckets */ |
| 661 | u64 nr_flush_rst; /* Flushes reset value for pad dist.*/ |
| 662 | atomic64_t nr_flush; /* Number of flush/fua I/O */ |
| 663 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 664 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 665 | /* Non-persistent debug counters, 4kb sector I/Os */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 666 | atomic_long_t inflight_writes; /* Inflight writes (user and gc) */ |
| 667 | atomic_long_t padded_writes; /* Sectors padded due to flush/fua */ |
| 668 | atomic_long_t padded_wb; /* Sectors padded in write buffer */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 669 | atomic_long_t req_writes; /* Sectors stored on write buffer */ |
| 670 | atomic_long_t sub_writes; /* Sectors submitted from buffer */ |
| 671 | atomic_long_t sync_writes; /* Sectors synced to media */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 672 | atomic_long_t inflight_reads; /* Inflight sector read requests */ |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 673 | atomic_long_t cache_reads; /* Read requests that hit the cache */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 674 | atomic_long_t sync_reads; /* Completed sector read requests */ |
| 675 | atomic_long_t recov_writes; /* Sectors submitted from recovery */ |
| 676 | atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */ |
| 677 | atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */ |
| 678 | #endif |
| 679 | |
| 680 | spinlock_t lock; |
| 681 | |
| 682 | atomic_long_t read_failed; |
| 683 | atomic_long_t read_empty; |
| 684 | atomic_long_t read_high_ecc; |
| 685 | atomic_long_t read_failed_gc; |
| 686 | atomic_long_t write_failed; |
| 687 | atomic_long_t erase_failed; |
| 688 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 689 | atomic_t inflight_io; /* General inflight I/O counter */ |
| 690 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 691 | struct task_struct *writer_ts; |
| 692 | |
| 693 | /* Simple translation map of logical addresses to physical addresses. |
| 694 | * The logical addresses is known by the host system, while the physical |
| 695 | * addresses are used when writing to the disk block device. |
| 696 | */ |
| 697 | unsigned char *trans_map; |
| 698 | spinlock_t trans_lock; |
| 699 | |
| 700 | struct list_head compl_list; |
| 701 | |
Hans Holmberg | 6a3abf5 | 2018-06-01 16:41:05 +0200 | [diff] [blame] | 702 | spinlock_t resubmit_lock; /* Resubmit list lock */ |
| 703 | struct list_head resubmit_list; /* Resubmit list for failed writes*/ |
| 704 | |
Kent Overstreet | b906bbb | 2018-05-20 18:25:50 -0400 | [diff] [blame] | 705 | mempool_t page_bio_pool; |
| 706 | mempool_t gen_ws_pool; |
| 707 | mempool_t rec_pool; |
| 708 | mempool_t r_rq_pool; |
| 709 | mempool_t w_rq_pool; |
| 710 | mempool_t e_rq_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 711 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 712 | struct workqueue_struct *close_wq; |
| 713 | struct workqueue_struct *bb_wq; |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 714 | struct workqueue_struct *r_end_wq; |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 715 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 716 | struct timer_list wtimer; |
| 717 | |
| 718 | struct pblk_gc gc; |
| 719 | }; |
| 720 | |
| 721 | struct pblk_line_ws { |
| 722 | struct pblk *pblk; |
| 723 | struct pblk_line *line; |
| 724 | void *priv; |
| 725 | struct work_struct ws; |
| 726 | }; |
| 727 | |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 728 | #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 729 | #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx)) |
| 730 | |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 731 | #define pblk_err(pblk, fmt, ...) \ |
| 732 | pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__) |
| 733 | #define pblk_info(pblk, fmt, ...) \ |
| 734 | pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__) |
| 735 | #define pblk_warn(pblk, fmt, ...) \ |
| 736 | pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__) |
| 737 | #define pblk_debug(pblk, fmt, ...) \ |
| 738 | pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__) |
| 739 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 740 | /* |
| 741 | * pblk ring buffer operations |
| 742 | */ |
Javier González | 766c8ce | 2018-10-09 13:12:15 +0200 | [diff] [blame] | 743 | int pblk_rb_init(struct pblk_rb *rb, unsigned int size, unsigned int threshold, |
| 744 | unsigned int seg_sz); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 745 | int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio, |
| 746 | unsigned int nr_entries, unsigned int *pos); |
| 747 | int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries, |
| 748 | unsigned int *pos); |
| 749 | void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data, |
| 750 | struct pblk_w_ctx w_ctx, unsigned int pos); |
| 751 | void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 752 | struct pblk_w_ctx w_ctx, struct pblk_line *line, |
| 753 | u64 paddr, unsigned int pos); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 754 | struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 755 | void pblk_rb_flush(struct pblk_rb *rb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 756 | |
| 757 | void pblk_rb_sync_l2p(struct pblk_rb *rb); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 758 | unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd, |
Javier González | 875d94f | 2017-10-13 14:46:18 +0200 | [diff] [blame] | 759 | unsigned int pos, unsigned int nr_entries, |
| 760 | unsigned int count); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 761 | int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba, |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 762 | struct ppa_addr ppa, int bio_iter, bool advanced_bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 763 | unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries); |
| 764 | |
| 765 | unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags); |
| 766 | unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries); |
Javier González | 40b8657 | 2018-10-09 13:12:09 +0200 | [diff] [blame] | 767 | unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p, |
| 768 | unsigned int nr_entries); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 769 | void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags); |
Hans Holmberg | 8154d29 | 2018-01-05 14:16:09 +0100 | [diff] [blame] | 770 | unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 771 | |
| 772 | unsigned int pblk_rb_read_count(struct pblk_rb *rb); |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 773 | unsigned int pblk_rb_sync_count(struct pblk_rb *rb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 774 | unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos); |
| 775 | |
| 776 | int pblk_rb_tear_down_check(struct pblk_rb *rb); |
| 777 | int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos); |
Javier González | 9bd1f87 | 2018-10-09 13:12:10 +0200 | [diff] [blame] | 778 | void pblk_rb_free(struct pblk_rb *rb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 779 | ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf); |
| 780 | |
| 781 | /* |
| 782 | * pblk core |
| 783 | */ |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 784 | struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type); |
| 785 | void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type); |
Javier González | 45dcf29 | 2018-10-09 13:12:02 +0200 | [diff] [blame] | 786 | int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd); |
| 787 | void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 788 | void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 789 | int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd, |
| 790 | struct pblk_c_ctx *c_ctx); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 791 | void pblk_discard(struct pblk *pblk, struct bio *bio); |
Matias Bjørling | aff3fb1 | 2018-10-09 13:11:36 +0200 | [diff] [blame] | 792 | struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk); |
Javier González | 32ef941 | 2018-03-30 00:05:20 +0200 | [diff] [blame] | 793 | struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk, |
| 794 | struct nvm_chk_meta *lp, |
| 795 | struct ppa_addr ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 796 | void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd); |
| 797 | void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd); |
| 798 | int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 799 | int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | 253babc | 2018-10-09 13:12:04 +0200 | [diff] [blame] | 800 | int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 801 | int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line); |
Hans Holmberg | 4c44abf | 2018-10-09 13:11:52 +0200 | [diff] [blame] | 802 | void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 803 | struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data, |
| 804 | unsigned int nr_secs, unsigned int len, |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 805 | int alloc_type, gfp_t gfp_mask); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 806 | struct pblk_line *pblk_line_get(struct pblk *pblk); |
| 807 | struct pblk_line *pblk_line_get_first_data(struct pblk *pblk); |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 808 | struct pblk_line *pblk_line_replace_data(struct pblk *pblk); |
Matias Bjørling | ae14cc0 | 2018-10-09 13:11:40 +0200 | [diff] [blame] | 809 | void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa); |
| 810 | void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 811 | int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line); |
| 812 | void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line); |
| 813 | struct pblk_line *pblk_line_get_data(struct pblk *pblk); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 814 | struct pblk_line *pblk_line_get_erase(struct pblk *pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 815 | int pblk_line_erase(struct pblk *pblk, struct pblk_line *line); |
| 816 | int pblk_line_is_full(struct pblk_line *line); |
Javier González | 8e55c07 | 2018-06-01 15:04:22 +0200 | [diff] [blame] | 817 | void pblk_line_free(struct pblk_line *line); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 818 | void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 819 | void pblk_line_close(struct pblk *pblk, struct pblk_line *line); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 820 | void pblk_line_close_ws(struct work_struct *work); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 821 | void pblk_pipeline_stop(struct pblk *pblk); |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 822 | void __pblk_pipeline_stop(struct pblk *pblk); |
| 823 | void __pblk_pipeline_flush(struct pblk *pblk); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 824 | void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv, |
| 825 | void (*work)(struct work_struct *), gfp_t gfp_mask, |
| 826 | struct workqueue_struct *wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 827 | u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line); |
Javier González | af3fac1 | 2018-10-09 13:12:03 +0200 | [diff] [blame] | 828 | int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line); |
| 829 | int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line, |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 830 | void *emeta_buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 831 | int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa); |
| 832 | void pblk_line_put(struct kref *ref); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 833 | void pblk_line_put_wq(struct kref *ref); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 834 | struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 835 | u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line); |
| 836 | void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 837 | u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 838 | u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 839 | int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail, |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 840 | unsigned long secs_to_flush, bool skip_meta); |
Matias Bjørling | 43241cf | 2018-10-09 13:11:51 +0200 | [diff] [blame] | 841 | void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 842 | unsigned long *lun_bitmap); |
Matias Bjørling | 43241cf | 2018-10-09 13:11:51 +0200 | [diff] [blame] | 843 | void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa); |
| 844 | void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa); |
Hans Holmberg | e99e802 | 2018-10-09 13:11:48 +0200 | [diff] [blame] | 845 | void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 846 | int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags, |
| 847 | int nr_pages); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 848 | void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off, |
| 849 | int nr_pages); |
| 850 | void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa); |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 851 | void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line, |
| 852 | u64 paddr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 853 | void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa); |
| 854 | void pblk_update_map_cache(struct pblk *pblk, sector_t lba, |
| 855 | struct ppa_addr ppa); |
| 856 | void pblk_update_map_dev(struct pblk *pblk, sector_t lba, |
| 857 | struct ppa_addr ppa, struct ppa_addr entry_line); |
| 858 | int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 859 | struct pblk_line *gc_line, u64 paddr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 860 | void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas, |
| 861 | u64 *lba_list, int nr_secs); |
| 862 | void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas, |
| 863 | sector_t blba, int nr_secs); |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 864 | void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd); |
| 865 | void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * pblk user I/O write path |
| 869 | */ |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame^] | 870 | void pblk_write_to_cache(struct pblk *pblk, struct bio *bio, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 871 | unsigned long flags); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 872 | int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * pblk map |
| 876 | */ |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 877 | int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 878 | unsigned int sentry, unsigned long *lun_bitmap, |
| 879 | unsigned int valid_secs, struct ppa_addr *erase_ppa); |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 880 | int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 881 | unsigned long *lun_bitmap, unsigned int valid_secs, |
| 882 | unsigned int off); |
| 883 | |
| 884 | /* |
| 885 | * pblk write thread |
| 886 | */ |
| 887 | int pblk_write_ts(void *data); |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 888 | void pblk_write_timer_fn(struct timer_list *t); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 889 | void pblk_write_should_kick(struct pblk *pblk); |
Hans Holmberg | cc9c9a0 | 2018-06-01 16:41:13 +0200 | [diff] [blame] | 890 | void pblk_write_kick(struct pblk *pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 891 | |
| 892 | /* |
| 893 | * pblk read path |
| 894 | */ |
Kent Overstreet | b906bbb | 2018-05-20 18:25:50 -0400 | [diff] [blame] | 895 | extern struct bio_set pblk_bio_set; |
Igor Konopko | 3e03f63 | 2019-05-04 20:38:06 +0200 | [diff] [blame^] | 896 | void pblk_submit_read(struct pblk *pblk, struct bio *bio); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 897 | int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 898 | /* |
| 899 | * pblk recovery |
| 900 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 901 | struct pblk_line *pblk_recov_l2p(struct pblk *pblk); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 902 | int pblk_recov_pad(struct pblk *pblk); |
Hans Holmberg | 06bc072 | 2018-01-05 14:16:08 +0100 | [diff] [blame] | 903 | int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * pblk gc |
| 907 | */ |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 908 | #define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */ |
Javier González | 3627896 | 2017-10-13 14:46:11 +0200 | [diff] [blame] | 909 | #define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */ |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 910 | #define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 911 | |
| 912 | int pblk_gc_init(struct pblk *pblk); |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 913 | void pblk_gc_exit(struct pblk *pblk, bool graceful); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 914 | void pblk_gc_should_start(struct pblk *pblk); |
| 915 | void pblk_gc_should_stop(struct pblk *pblk); |
Hans Holmberg | 03661b5 | 2017-10-13 14:46:37 +0200 | [diff] [blame] | 916 | void pblk_gc_should_kick(struct pblk *pblk); |
Hans Holmberg | 37ce33d | 2017-10-13 14:46:36 +0200 | [diff] [blame] | 917 | void pblk_gc_free_full_lines(struct pblk *pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 918 | void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled, |
| 919 | int *gc_active); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 920 | int pblk_gc_sysfs_force(struct pblk *pblk, int force); |
Igor Konopko | f2e0245 | 2019-05-04 20:38:05 +0200 | [diff] [blame] | 921 | void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 922 | |
| 923 | /* |
| 924 | * pblk rate limiter |
| 925 | */ |
Javier González | b4cdc42 | 2019-02-11 13:25:08 +0100 | [diff] [blame] | 926 | void pblk_rl_init(struct pblk_rl *rl, int budget, int threshold); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 927 | void pblk_rl_free(struct pblk_rl *rl); |
Hans Holmberg | 03661b5 | 2017-10-13 14:46:37 +0200 | [diff] [blame] | 928 | void pblk_rl_update_rates(struct pblk_rl *rl); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 929 | int pblk_rl_high_thrs(struct pblk_rl *rl); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 930 | unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl); |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 931 | unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 932 | int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 933 | void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 934 | void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries); |
| 935 | int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries); |
| 936 | void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries); |
| 937 | void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc); |
Javier González | da67e68f | 2017-10-13 14:46:05 +0200 | [diff] [blame] | 938 | int pblk_rl_max_io(struct pblk_rl *rl); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 939 | void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line); |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 940 | void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line, |
| 941 | bool used); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 942 | int pblk_rl_is_limit(struct pblk_rl *rl); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 943 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 944 | void pblk_rl_werr_line_in(struct pblk_rl *rl); |
| 945 | void pblk_rl_werr_line_out(struct pblk_rl *rl); |
| 946 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 947 | /* |
| 948 | * pblk sysfs |
| 949 | */ |
| 950 | int pblk_sysfs_init(struct gendisk *tdisk); |
| 951 | void pblk_sysfs_exit(struct gendisk *tdisk); |
| 952 | |
| 953 | static inline void *pblk_malloc(size_t size, int type, gfp_t flags) |
| 954 | { |
| 955 | if (type == PBLK_KMALLOC_META) |
| 956 | return kmalloc(size, flags); |
| 957 | return vmalloc(size); |
| 958 | } |
| 959 | |
| 960 | static inline void pblk_mfree(void *ptr, int type) |
| 961 | { |
| 962 | if (type == PBLK_KMALLOC_META) |
| 963 | kfree(ptr); |
| 964 | else |
| 965 | vfree(ptr); |
| 966 | } |
| 967 | |
| 968 | static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx) |
| 969 | { |
| 970 | return c_ctx - sizeof(struct nvm_rq); |
| 971 | } |
| 972 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 973 | static inline void *emeta_to_bb(struct line_emeta *emeta) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 974 | { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 975 | return emeta->bb_bitmap; |
| 976 | } |
| 977 | |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 978 | static inline void *emeta_to_wa(struct pblk_line_meta *lm, |
| 979 | struct line_emeta *emeta) |
| 980 | { |
| 981 | return emeta->bb_bitmap + lm->blk_bitmap_len; |
| 982 | } |
| 983 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 984 | static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta) |
| 985 | { |
| 986 | return ((void *)emeta + pblk->lm.emeta_len[1]); |
| 987 | } |
| 988 | |
| 989 | static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta) |
| 990 | { |
| 991 | return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 994 | static inline int pblk_line_vsc(struct pblk_line *line) |
| 995 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 996 | return le32_to_cpu(*line->vsc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Javier González | cb21665 | 2018-10-09 13:11:42 +0200 | [diff] [blame] | 999 | static inline int pblk_ppa_to_line_id(struct ppa_addr p) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1000 | { |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 1001 | return p.a.blk; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
Javier González | cb21665 | 2018-10-09 13:11:42 +0200 | [diff] [blame] | 1004 | static inline struct pblk_line *pblk_ppa_to_line(struct pblk *pblk, |
| 1005 | struct ppa_addr p) |
| 1006 | { |
| 1007 | return &pblk->lines[pblk_ppa_to_line_id(p)]; |
| 1008 | } |
| 1009 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1010 | static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p) |
| 1011 | { |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 1012 | return p.a.lun * geo->num_ch + p.a.ch; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1015 | static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr, |
| 1016 | u64 line_id) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1017 | { |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1018 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1019 | struct nvm_geo *geo = &dev->geo; |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1020 | struct ppa_addr ppa; |
| 1021 | |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1022 | if (geo->version == NVM_OCSSD_SPEC_12) { |
| 1023 | struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf; |
| 1024 | |
| 1025 | ppa.ppa = 0; |
| 1026 | ppa.g.blk = line_id; |
| 1027 | ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset; |
| 1028 | ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset; |
| 1029 | ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset; |
| 1030 | ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset; |
| 1031 | ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset; |
| 1032 | } else { |
| 1033 | struct pblk_addrf *uaddrf = &pblk->uaddrf; |
| 1034 | int secs, chnls, luns; |
| 1035 | |
| 1036 | ppa.ppa = 0; |
| 1037 | |
| 1038 | ppa.m.chk = line_id; |
| 1039 | |
| 1040 | paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs); |
| 1041 | ppa.m.sec = secs; |
| 1042 | |
| 1043 | paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls); |
| 1044 | ppa.m.grp = chnls; |
| 1045 | |
| 1046 | paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns); |
| 1047 | ppa.m.pu = luns; |
| 1048 | |
| 1049 | ppa.m.sec += uaddrf->sec_stripe * paddr; |
| 1050 | } |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1051 | |
| 1052 | return ppa; |
| 1053 | } |
| 1054 | |
Javier González | 2cf99bb | 2018-10-09 13:11:41 +0200 | [diff] [blame] | 1055 | static inline struct nvm_chk_meta *pblk_dev_ppa_to_chunk(struct pblk *pblk, |
| 1056 | struct ppa_addr p) |
| 1057 | { |
| 1058 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1059 | struct nvm_geo *geo = &dev->geo; |
Javier González | cb21665 | 2018-10-09 13:11:42 +0200 | [diff] [blame] | 1060 | struct pblk_line *line = pblk_ppa_to_line(pblk, p); |
Javier González | 2cf99bb | 2018-10-09 13:11:41 +0200 | [diff] [blame] | 1061 | int pos = pblk_ppa_to_pos(geo, p); |
| 1062 | |
| 1063 | return &line->chks[pos]; |
| 1064 | } |
| 1065 | |
| 1066 | static inline u64 pblk_dev_ppa_to_chunk_addr(struct pblk *pblk, |
| 1067 | struct ppa_addr p) |
| 1068 | { |
| 1069 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1070 | |
| 1071 | return dev_to_chunk_addr(dev->parent, &pblk->addrf, p); |
| 1072 | } |
| 1073 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1074 | static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk, |
| 1075 | struct ppa_addr p) |
| 1076 | { |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1077 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1078 | struct nvm_geo *geo = &dev->geo; |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1079 | u64 paddr; |
| 1080 | |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1081 | if (geo->version == NVM_OCSSD_SPEC_12) { |
| 1082 | struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf; |
| 1083 | |
| 1084 | paddr = (u64)p.g.ch << ppaf->ch_offset; |
| 1085 | paddr |= (u64)p.g.lun << ppaf->lun_offset; |
| 1086 | paddr |= (u64)p.g.pg << ppaf->pg_offset; |
| 1087 | paddr |= (u64)p.g.pl << ppaf->pln_offset; |
| 1088 | paddr |= (u64)p.g.sec << ppaf->sec_offset; |
| 1089 | } else { |
| 1090 | struct pblk_addrf *uaddrf = &pblk->uaddrf; |
| 1091 | u64 secs = p.m.sec; |
| 1092 | int sec_stripe; |
| 1093 | |
| 1094 | paddr = (u64)p.m.grp * uaddrf->sec_stripe; |
| 1095 | paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe; |
| 1096 | |
| 1097 | secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe); |
| 1098 | paddr += secs * uaddrf->sec_ws_stripe; |
| 1099 | paddr += sec_stripe; |
| 1100 | } |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1101 | |
| 1102 | return paddr; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32) |
| 1106 | { |
Javier González | 7f985f9 | 2018-10-09 13:11:56 +0200 | [diff] [blame] | 1107 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1108 | |
Javier González | 7f985f9 | 2018-10-09 13:11:56 +0200 | [diff] [blame] | 1109 | return nvm_ppa32_to_ppa64(dev->parent, &pblk->addrf, ppa32); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1112 | static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64) |
| 1113 | { |
Javier González | 7f985f9 | 2018-10-09 13:11:56 +0200 | [diff] [blame] | 1114 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1115 | |
Javier González | 7f985f9 | 2018-10-09 13:11:56 +0200 | [diff] [blame] | 1116 | return nvm_ppa64_to_ppa32(dev->parent, &pblk->addrf, ppa64); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1119 | static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk, |
| 1120 | sector_t lba) |
| 1121 | { |
| 1122 | struct ppa_addr ppa; |
| 1123 | |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame] | 1124 | if (pblk->addrf_len < 32) { |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1125 | u32 *map = (u32 *)pblk->trans_map; |
| 1126 | |
| 1127 | ppa = pblk_ppa32_to_ppa64(pblk, map[lba]); |
| 1128 | } else { |
| 1129 | struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map; |
| 1130 | |
| 1131 | ppa = map[lba]; |
| 1132 | } |
| 1133 | |
| 1134 | return ppa; |
| 1135 | } |
| 1136 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1137 | static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba, |
| 1138 | struct ppa_addr ppa) |
| 1139 | { |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame] | 1140 | if (pblk->addrf_len < 32) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1141 | u32 *map = (u32 *)pblk->trans_map; |
| 1142 | |
| 1143 | map[lba] = pblk_ppa64_to_ppa32(pblk, ppa); |
| 1144 | } else { |
| 1145 | u64 *map = (u64 *)pblk->trans_map; |
| 1146 | |
| 1147 | map[lba] = ppa.ppa; |
| 1148 | } |
| 1149 | } |
| 1150 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1151 | static inline int pblk_ppa_empty(struct ppa_addr ppa_addr) |
| 1152 | { |
| 1153 | return (ppa_addr.ppa == ADDR_EMPTY); |
| 1154 | } |
| 1155 | |
| 1156 | static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr) |
| 1157 | { |
| 1158 | ppa_addr->ppa = ADDR_EMPTY; |
| 1159 | } |
| 1160 | |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 1161 | static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa) |
| 1162 | { |
Matias Bjørling | 8b7bc84 | 2018-01-05 14:16:21 +0100 | [diff] [blame] | 1163 | return (lppa.ppa == rppa.ppa); |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1166 | static inline int pblk_addr_in_cache(struct ppa_addr ppa) |
| 1167 | { |
| 1168 | return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached); |
| 1169 | } |
| 1170 | |
| 1171 | static inline int pblk_addr_to_cacheline(struct ppa_addr ppa) |
| 1172 | { |
| 1173 | return ppa.c.line; |
| 1174 | } |
| 1175 | |
| 1176 | static inline struct ppa_addr pblk_cacheline_to_addr(int addr) |
| 1177 | { |
| 1178 | struct ppa_addr p; |
| 1179 | |
| 1180 | p.c.line = addr; |
| 1181 | p.c.is_cached = 1; |
| 1182 | |
| 1183 | return p; |
| 1184 | } |
| 1185 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1186 | static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk, |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1187 | struct line_header *header) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1188 | { |
| 1189 | u32 crc = ~(u32)0; |
| 1190 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1191 | crc = crc32_le(crc, (unsigned char *)header + sizeof(crc), |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1192 | sizeof(struct line_header) - sizeof(crc)); |
| 1193 | |
| 1194 | return crc; |
| 1195 | } |
| 1196 | |
| 1197 | static inline u32 pblk_calc_smeta_crc(struct pblk *pblk, |
| 1198 | struct line_smeta *smeta) |
| 1199 | { |
| 1200 | struct pblk_line_meta *lm = &pblk->lm; |
| 1201 | u32 crc = ~(u32)0; |
| 1202 | |
| 1203 | crc = crc32_le(crc, (unsigned char *)smeta + |
| 1204 | sizeof(struct line_header) + sizeof(crc), |
| 1205 | lm->smeta_len - |
| 1206 | sizeof(struct line_header) - sizeof(crc)); |
| 1207 | |
| 1208 | return crc; |
| 1209 | } |
| 1210 | |
| 1211 | static inline u32 pblk_calc_emeta_crc(struct pblk *pblk, |
| 1212 | struct line_emeta *emeta) |
| 1213 | { |
| 1214 | struct pblk_line_meta *lm = &pblk->lm; |
| 1215 | u32 crc = ~(u32)0; |
| 1216 | |
| 1217 | crc = crc32_le(crc, (unsigned char *)emeta + |
| 1218 | sizeof(struct line_header) + sizeof(crc), |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1219 | lm->emeta_len[0] - |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1220 | sizeof(struct line_header) - sizeof(crc)); |
| 1221 | |
| 1222 | return crc; |
| 1223 | } |
| 1224 | |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 1225 | static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs) |
| 1226 | { |
| 1227 | return !(nr_secs % pblk->min_write_pgs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 1230 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1231 | static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p, |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1232 | char *msg, int error) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1233 | { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1234 | struct nvm_geo *geo = &pblk->dev->geo; |
| 1235 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1236 | if (p->c.is_cached) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1237 | pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n", |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1238 | msg, error, (u64)p->c.line); |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1239 | } else if (geo->version == NVM_OCSSD_SPEC_12) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1240 | pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n", |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1241 | msg, error, |
| 1242 | p->g.ch, p->g.lun, p->g.blk, |
| 1243 | p->g.pg, p->g.pl, p->g.sec); |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1244 | } else { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1245 | pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n", |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1246 | msg, error, |
| 1247 | p->m.grp, p->m.pu, p->m.chk, p->m.sec); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd, |
| 1252 | int error) |
| 1253 | { |
| 1254 | int bit = -1; |
| 1255 | |
| 1256 | if (rqd->nr_ppas == 1) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1257 | print_ppa(pblk, &rqd->ppa_addr, "rqd", error); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas, |
| 1262 | bit + 1)) < rqd->nr_ppas) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1263 | print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1266 | pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1267 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1268 | |
| 1269 | static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev, |
| 1270 | struct ppa_addr *ppas, int nr_ppas) |
| 1271 | { |
| 1272 | struct nvm_geo *geo = &tgt_dev->geo; |
| 1273 | struct ppa_addr *ppa; |
| 1274 | int i; |
| 1275 | |
| 1276 | for (i = 0; i < nr_ppas; i++) { |
| 1277 | ppa = &ppas[i]; |
| 1278 | |
Javier González | 3b2a3ad | 2018-03-30 00:05:21 +0200 | [diff] [blame] | 1279 | if (geo->version == NVM_OCSSD_SPEC_12) { |
| 1280 | if (!ppa->c.is_cached && |
| 1281 | ppa->g.ch < geo->num_ch && |
| 1282 | ppa->g.lun < geo->num_lun && |
| 1283 | ppa->g.pl < geo->num_pln && |
| 1284 | ppa->g.blk < geo->num_chk && |
| 1285 | ppa->g.pg < geo->num_pg && |
| 1286 | ppa->g.sec < geo->ws_min) |
| 1287 | continue; |
| 1288 | } else { |
| 1289 | if (!ppa->c.is_cached && |
| 1290 | ppa->m.grp < geo->num_ch && |
| 1291 | ppa->m.pu < geo->num_lun && |
| 1292 | ppa->m.chk < geo->num_chk && |
| 1293 | ppa->m.sec < geo->clba) |
| 1294 | continue; |
| 1295 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1296 | |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1297 | print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1298 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1299 | return 1; |
| 1300 | } |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1304 | static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd) |
| 1305 | { |
| 1306 | struct nvm_tgt_dev *dev = pblk->dev; |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 1307 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1308 | |
| 1309 | if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) { |
| 1310 | WARN_ON(1); |
| 1311 | return -EINVAL; |
| 1312 | } |
| 1313 | |
| 1314 | if (rqd->opcode == NVM_OP_PWRITE) { |
| 1315 | struct pblk_line *line; |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1316 | int i; |
| 1317 | |
| 1318 | for (i = 0; i < rqd->nr_ppas; i++) { |
Javier González | cb21665 | 2018-10-09 13:11:42 +0200 | [diff] [blame] | 1319 | line = pblk_ppa_to_line(pblk, ppa_list[i]); |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1320 | |
| 1321 | spin_lock(&line->lock); |
| 1322 | if (line->state != PBLK_LINESTATE_OPEN) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 1323 | pblk_err(pblk, "bad ppa: line:%d,state:%d\n", |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 1324 | line->id, line->state); |
| 1325 | WARN_ON(1); |
| 1326 | spin_unlock(&line->lock); |
| 1327 | return -EINVAL; |
| 1328 | } |
| 1329 | spin_unlock(&line->lock); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1337 | static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr) |
| 1338 | { |
| 1339 | struct pblk_line_meta *lm = &pblk->lm; |
| 1340 | |
| 1341 | if (paddr > lm->sec_per_line) |
| 1342 | return 1; |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static inline unsigned int pblk_get_bi_idx(struct bio *bio) |
| 1348 | { |
| 1349 | return bio->bi_iter.bi_idx; |
| 1350 | } |
| 1351 | |
| 1352 | static inline sector_t pblk_get_lba(struct bio *bio) |
| 1353 | { |
| 1354 | return bio->bi_iter.bi_sector / NR_PHY_IN_LOG; |
| 1355 | } |
| 1356 | |
| 1357 | static inline unsigned int pblk_get_secs(struct bio *bio) |
| 1358 | { |
| 1359 | return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE; |
| 1360 | } |
| 1361 | |
Hans Holmberg | 4c44abf | 2018-10-09 13:11:52 +0200 | [diff] [blame] | 1362 | static inline char *pblk_disk_name(struct pblk *pblk) |
| 1363 | { |
| 1364 | struct gendisk *disk = pblk->disk; |
| 1365 | |
| 1366 | return disk->disk_name; |
| 1367 | } |
Hans Holmberg | 3bcebc5 | 2018-12-11 20:16:13 +0100 | [diff] [blame] | 1368 | |
| 1369 | static inline unsigned int pblk_get_min_chks(struct pblk *pblk) |
| 1370 | { |
| 1371 | struct pblk_line_meta *lm = &pblk->lm; |
| 1372 | /* In a worst-case scenario every line will have OP invalid sectors. |
| 1373 | * We will then need a minimum of 1/OP lines to free up a single line |
| 1374 | */ |
| 1375 | |
| 1376 | return DIV_ROUND_UP(100, pblk->op) * lm->blk_per_line; |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 1377 | } |
Hans Holmberg | 3bcebc5 | 2018-12-11 20:16:13 +0100 | [diff] [blame] | 1378 | |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 1379 | static inline struct pblk_sec_meta *pblk_get_meta(struct pblk *pblk, |
| 1380 | void *meta, int index) |
| 1381 | { |
Igor Konopko | 2c4d535 | 2018-12-11 20:16:27 +0100 | [diff] [blame] | 1382 | return meta + |
| 1383 | max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size) |
| 1384 | * index; |
Hans Holmberg | 3bcebc5 | 2018-12-11 20:16:13 +0100 | [diff] [blame] | 1385 | } |
Igor Konopko | 24828d0 | 2018-12-11 20:16:24 +0100 | [diff] [blame] | 1386 | |
| 1387 | static inline int pblk_dma_meta_size(struct pblk *pblk) |
| 1388 | { |
Igor Konopko | 2c4d535 | 2018-12-11 20:16:27 +0100 | [diff] [blame] | 1389 | return max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size) |
| 1390 | * NVM_MAX_VLBA; |
Igor Konopko | 24828d0 | 2018-12-11 20:16:24 +0100 | [diff] [blame] | 1391 | } |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 1392 | |
| 1393 | static inline int pblk_is_oob_meta_supported(struct pblk *pblk) |
| 1394 | { |
| 1395 | return pblk->oob_meta_size >= sizeof(struct pblk_sec_meta); |
| 1396 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1397 | #endif /* PBLK_H_ */ |