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