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