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