blob: 93ec4fd2c26ee1fa4d19ca0cef34884018e340ce [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
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álezef576492017-06-26 11:57:28 +020043#define PBLK_NR_CLOSE_JOBS (4)
44
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045#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ález0d880392017-10-13 14:46:08 +020054/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020055#define PBLK_GEN_WS_POOL_SIZE (2)
56
Javier Gonzáleze5392732018-01-05 14:16:14 +010057#define PBLK_DEFAULT_OP (11)
58
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020060 PBLK_READ = READ,
61 PBLK_WRITE = WRITE,/* Write from write buffer */
62 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010063 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020064 PBLK_ERASE,
65};
66
67enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 /* 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
79enum {
80 PBLK_BLK_ST_OPEN = 0x1,
81 PBLK_BLK_ST_CLOSED = 0x2,
82};
83
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020084struct pblk_sec_meta {
85 u64 reserved;
86 __le64 lba;
87};
88
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020089/* 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álezb20ba1b2017-06-26 11:57:27 +020092#define PBLK_GC_NR_LISTS 3
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093
94enum {
95 PBLK_RL_HIGH = 1,
96 PBLK_RL_MID = 2,
97 PBLK_RL_LOW = 3,
98};
99
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200101#define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200102
Javier González084ec9b2017-06-26 16:27:13 -0600103/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104struct 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áleza4809fe2017-10-13 14:46:22 +0200113/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600114struct pblk_g_ctx {
115 void *private;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200116 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200117};
118
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200119/* Pad context */
120struct pblk_pad_rq {
121 struct pblk *pblk;
122 struct completion wait;
123 struct kref ref;
124};
125
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200126/* Recovery context */
127struct pblk_rec_ctx {
128 struct pblk *pblk;
129 struct nvm_rq *rqd;
130 struct list_head failed;
131 struct work_struct ws_rec;
132};
133
134/* Write context */
135struct pblk_w_ctx {
136 struct bio_list bios; /* Original bios - used for completion
137 * in REQ_FUA, REQ_FLUSH case
138 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200139 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200140 struct ppa_addr ppa; /* Physic addr. associated with entry */
141 int flags; /* Write context flags */
142};
143
144struct 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
153struct pblk_rb_pages {
154 struct page *pages;
155 int order;
156 struct list_head list;
157};
158
159struct 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 Holmberg8154d292018-01-05 14:16:09 +0100172 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200173 * 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 Holmberg8154d292018-01-05 14:16:09 +0100195 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200196#endif
197};
198
199#define PBLK_RECOVERY_SECTORS 16
200
201struct pblk_lun {
202 struct ppa_addr bppa;
203
204 u8 *bb_list; /* Bad block list for LUN. Only used on
205 * bring up. Bad blocks are managed
206 * within lines on run-time.
207 */
208
209 struct semaphore wr_sem;
210};
211
212struct pblk_gc_rq {
213 struct pblk_line *line;
214 void *data;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200215 u64 paddr_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200216 u64 lba_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200217 int nr_secs;
218 int secs_to_gc;
219 struct list_head list;
220};
221
222struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200223 /* These states are not protected by a lock since (i) they are in the
224 * fast path, and (ii) they are not critical.
225 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200226 int gc_active;
227 int gc_enabled;
228 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229
230 struct task_struct *gc_ts;
231 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200232 struct task_struct *gc_reader_ts;
233
234 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200236
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200237 struct timer_list gc_timer;
238
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200240 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
241 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
242 * started reads to finished writes
243 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200244 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200245
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200246 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200247 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200248
249 spinlock_t lock;
250 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252};
253
254struct pblk_rl {
255 unsigned int high; /* Upper threshold for rate limiter (free run -
256 * user I/O rate limiter
257 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200258 unsigned int high_pw; /* High rounded up as a power of 2 */
259
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200260#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
261#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200262
263 int rb_windows_pw; /* Number of rate windows in the write buffer
264 * given as a power-of-2. This guarantees that
265 * when user I/O is being rate limited, there
266 * will be reserved enough space for the GC to
267 * place its payload. A window is of
268 * pblk->max_write_pgs size, which in NVMe is
269 * 64, i.e., 256kb.
270 */
271 int rb_budget; /* Total number of entries available for I/O */
272 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273 int rb_gc_max; /* Max buffer entries available for GC I/O */
274 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
275 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200276 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200277
278 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200280 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200281
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200282 int rsv_blocks; /* Reserved blocks for GC */
283
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200284 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200285 int rb_gc_active;
286
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200287 struct timer_list u_timer;
288
289 unsigned long long nr_secs;
290 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100291
292 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
293 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200294};
295
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296#define PBLK_LINE_EMPTY (~0U)
297
298enum {
299 /* Line Types */
300 PBLK_LINETYPE_FREE = 0,
301 PBLK_LINETYPE_LOG = 1,
302 PBLK_LINETYPE_DATA = 2,
303
304 /* Line state */
305 PBLK_LINESTATE_FREE = 10,
306 PBLK_LINESTATE_OPEN = 11,
307 PBLK_LINESTATE_CLOSED = 12,
308 PBLK_LINESTATE_GC = 13,
309 PBLK_LINESTATE_BAD = 14,
310 PBLK_LINESTATE_CORRUPT = 15,
311
312 /* GC group */
313 PBLK_LINEGC_NONE = 20,
314 PBLK_LINEGC_EMPTY = 21,
315 PBLK_LINEGC_LOW = 22,
316 PBLK_LINEGC_MID = 23,
317 PBLK_LINEGC_HIGH = 24,
318 PBLK_LINEGC_FULL = 25,
319};
320
321#define PBLK_MAGIC 0x70626c6b /*pblk*/
Rakesh Panditc79819b2017-10-13 14:45:59 +0200322#define SMETA_VERSION cpu_to_le16(1)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323
324struct line_header {
325 __le32 crc;
326 __le32 identifier; /* pblk identifier */
327 __u8 uuid[16]; /* instance uuid */
328 __le16 type; /* line type */
329 __le16 version; /* type version */
330 __le32 id; /* line id for current line */
331};
332
333struct line_smeta {
334 struct line_header header;
335
336 __le32 crc; /* Full structure including struct crc */
337 /* Previous line metadata */
338 __le32 prev_id; /* Line id for previous line */
339
340 /* Current line metadata */
341 __le64 seq_nr; /* Sequence number for current line */
342
343 /* Active writers */
344 __le32 window_wr_lun; /* Number of parallel LUNs to write */
345
346 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200347
348 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349};
350
351/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200352 * Metadata layout in media:
353 * First sector:
354 * 1. struct line_emeta
355 * 2. bad block bitmap (u64 * window_wr_lun)
356 * Mid sectors (start at lbas_sector):
357 * 3. nr_lbas (u64) forming lba list
358 * Last sectors (start at vsc_sector):
359 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360 */
361struct line_emeta {
362 struct line_header header;
363
364 __le32 crc; /* Full structure including struct crc */
365
366 /* Previous line metadata */
367 __le32 prev_id; /* Line id for prev line */
368
369 /* Current line metadata */
370 __le64 seq_nr; /* Sequence number for current line */
371
372 /* Active writers */
373 __le32 window_wr_lun; /* Number of parallel LUNs to write */
374
375 /* Bookkeeping for recovery */
376 __le32 next_id; /* Line id for next line */
377 __le64 nr_lbas; /* Number of lbas mapped in line */
378 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200379 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
380};
381
382struct pblk_emeta {
383 struct line_emeta *buf; /* emeta buffer in media format */
384 int mem; /* Write offset - points to next
385 * writable entry in memory
386 */
387 atomic_t sync; /* Synced - backpointer that signals the
388 * last entry that has been successfully
389 * persisted to media
390 */
391 unsigned int nr_entries; /* Number of emeta entries */
392};
393
394struct pblk_smeta {
395 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200396};
397
398struct pblk_line {
399 struct pblk *pblk;
400 unsigned int id; /* Line number corresponds to the
401 * block line
402 */
403 unsigned int seq_nr; /* Unique line sequence number */
404
405 int state; /* PBLK_LINESTATE_X */
406 int type; /* PBLK_LINETYPE_X */
407 int gc_group; /* PBLK_LINEGC_X */
408 struct list_head list; /* Free, GC lists */
409
410 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
411
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200412 struct pblk_smeta *smeta; /* Start metadata */
413 struct pblk_emeta *emeta; /* End medatada */
414
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200415 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200416 int meta_distance; /* Distance between data and metadata */
417
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418 u64 smeta_ssec; /* Sector where smeta starts */
419 u64 emeta_ssec; /* Sector where emeta starts */
420
421 unsigned int sec_in_line; /* Number of usable secs in line */
422
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200423 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
425 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
426
427 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
428 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
429
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200430 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 atomic_t left_seblks; /* Blocks left for sync erasing */
432
433 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200434 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200435 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
436
437 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438
439 struct kref ref; /* Write buffer L2P references */
440
441 spinlock_t lock; /* Necessary for invalid_bitmap only */
442};
443
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200444#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200445
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200446enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447 PBLK_KMALLOC_META = 1,
448 PBLK_VMALLOC_META = 2,
449};
450
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200451enum {
452 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
453 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
454 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455};
456
457struct pblk_line_mgmt {
458 int nr_lines; /* Total number of full lines */
459 int nr_free_lines; /* Number of full lines in free list */
460
461 /* Free lists - use free_lock */
462 struct list_head free_list; /* Full lines ready to use */
463 struct list_head corrupt_list; /* Full lines corrupted */
464 struct list_head bad_list; /* Full lines bad */
465
466 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200467 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
469 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
470 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
471
472 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
473 struct list_head gc_empty_list; /* Full lines close, all valid */
474
475 struct pblk_line *log_line; /* Current FTL log line */
476 struct pblk_line *data_line; /* Current data line */
477 struct pblk_line *log_next; /* Next FTL log line */
478 struct pblk_line *data_next; /* Next data line */
479
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200480 struct list_head emeta_list; /* Lines queued to schedule emeta */
481
482 __le32 *vsc_list; /* Valid sector counts for all lines */
483
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200485 int emeta_alloc_type;
486
487 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200488 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
489 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200490 unsigned long meta_bitmap;
491
492 /* Helpers for fast bitmap calculations */
493 unsigned long *bb_template;
494 unsigned long *bb_aux;
495
496 unsigned long d_seq_nr; /* Data line unique sequence number */
497 unsigned long l_seq_nr; /* Log line unique sequence number */
498
499 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200500 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501 spinlock_t gc_lock;
502};
503
504struct pblk_line_meta {
505 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200506 unsigned int smeta_sec; /* Sectors needed for smeta */
507
508 unsigned int emeta_len[4]; /* Lengths for emeta:
509 * [0]: Total length
510 * [1]: struct line_emeta length
511 * [2]: L2P portion length
512 * [3]: vsc list length
513 */
514 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
515 * as emeta_len
516 */
517
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200519
520 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200521 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
522 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
523 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
524
525 unsigned int blk_per_line; /* Number of blocks in a full line */
526 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200527 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200528 unsigned int min_blk_line; /* Min. number of good blocks in line */
529
530 unsigned int mid_thrs; /* Threshold for GC mid list */
531 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200532
533 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200534};
535
536struct pblk_addr_format {
537 u64 ch_mask;
538 u64 lun_mask;
539 u64 pln_mask;
540 u64 blk_mask;
541 u64 pg_mask;
542 u64 sec_mask;
543 u8 ch_offset;
544 u8 lun_offset;
545 u8 pln_offset;
546 u8 blk_offset;
547 u8 pg_offset;
548 u8 sec_offset;
549};
550
Javier González588726d32017-06-26 11:57:29 +0200551enum {
552 PBLK_STATE_RUNNING = 0,
553 PBLK_STATE_STOPPING = 1,
554 PBLK_STATE_RECOVERING = 2,
555 PBLK_STATE_STOPPED = 3,
556};
557
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558struct pblk {
559 struct nvm_tgt_dev *dev;
560 struct gendisk *disk;
561
562 struct kobject kobj;
563
564 struct pblk_lun *luns;
565
566 struct pblk_line *lines; /* Line array */
567 struct pblk_line_mgmt l_mg; /* Line management */
568 struct pblk_line_meta lm; /* Line metadata */
569
570 int ppaf_bitsize;
571 struct pblk_addr_format ppaf;
572
573 struct pblk_rb rwb;
574
Javier González588726d32017-06-26 11:57:29 +0200575 int state; /* pblk line state */
576
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577 int min_write_pgs; /* Minimum amount of pages required by controller */
578 int max_write_pgs; /* Maximum amount of pages supported by controller */
579 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
580 * guarantee successful reads.
581 */
582
583 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100584
585 int op; /* Percentage of device used for over-provisioning */
586 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587
588 /* pblk provisioning values. Used by rate limiter */
589 struct pblk_rl rl;
590
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200591 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592
593 unsigned char instance_uuid[16];
594#ifdef CONFIG_NVM_DEBUG
595 /* All debug counters apply to 4kb sector I/Os */
596 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
597 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
598 atomic_long_t padded_wb; /* Sectors padded in write buffer */
599 atomic_long_t nr_flush; /* Number of flush/fua I/O */
600 atomic_long_t req_writes; /* Sectors stored on write buffer */
601 atomic_long_t sub_writes; /* Sectors submitted from buffer */
602 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200604 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605 atomic_long_t sync_reads; /* Completed sector read requests */
606 atomic_long_t recov_writes; /* Sectors submitted from recovery */
607 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
608 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
609#endif
610
611 spinlock_t lock;
612
613 atomic_long_t read_failed;
614 atomic_long_t read_empty;
615 atomic_long_t read_high_ecc;
616 atomic_long_t read_failed_gc;
617 atomic_long_t write_failed;
618 atomic_long_t erase_failed;
619
Javier González588726d32017-06-26 11:57:29 +0200620 atomic_t inflight_io; /* General inflight I/O counter */
621
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200622 struct task_struct *writer_ts;
623
624 /* Simple translation map of logical addresses to physical addresses.
625 * The logical addresses is known by the host system, while the physical
626 * addresses are used when writing to the disk block device.
627 */
628 unsigned char *trans_map;
629 spinlock_t trans_lock;
630
631 struct list_head compl_list;
632
Javier Gonzálezbd432412017-10-13 14:46:06 +0200633 mempool_t *page_bio_pool;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200634 mempool_t *gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 mempool_t *rec_pool;
Javier González0d880392017-10-13 14:46:08 +0200636 mempool_t *r_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200637 mempool_t *w_rq_pool;
Javier González0d880392017-10-13 14:46:08 +0200638 mempool_t *e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639
Javier Gonzálezef576492017-06-26 11:57:28 +0200640 struct workqueue_struct *close_wq;
641 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200642 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200643
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200644 struct timer_list wtimer;
645
646 struct pblk_gc gc;
647};
648
649struct pblk_line_ws {
650 struct pblk *pblk;
651 struct pblk_line *line;
652 void *priv;
653 struct work_struct ws;
654};
655
Javier González084ec9b2017-06-26 16:27:13 -0600656#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
658
659/*
660 * pblk ring buffer operations
661 */
662int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
663 unsigned int power_size, unsigned int power_seg_sz);
664unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
665void *pblk_rb_entries_ref(struct pblk_rb *rb);
666int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
667 unsigned int nr_entries, unsigned int *pos);
668int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
669 unsigned int *pos);
670void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
671 struct pblk_w_ctx w_ctx, unsigned int pos);
672void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200673 struct pblk_w_ctx w_ctx, struct pblk_line *line,
674 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200675struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200676void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200677
678void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200679unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200680 unsigned int pos, unsigned int nr_entries,
681 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200682unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
683 struct list_head *list,
684 unsigned int max);
685int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200686 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200687unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
688
689unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
690unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
691struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
692 struct ppa_addr *ppa);
693void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100694unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200695
696unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200697unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200698unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
699
700int pblk_rb_tear_down_check(struct pblk_rb *rb);
701int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
702void pblk_rb_data_free(struct pblk_rb *rb);
703ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
704
705/*
706 * pblk core
707 */
Javier González67bf26a2017-10-13 14:46:20 +0200708struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
709void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200710void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200711int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
712 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200713void pblk_discard(struct pblk *pblk, struct bio *bio);
714void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
715void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
716int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200717int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200718int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200719struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
720 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200721 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200722struct pblk_line *pblk_line_get(struct pblk *pblk);
723struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200724struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200725int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
726void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
727struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200728struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200729int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
730int pblk_line_is_full(struct pblk_line *line);
731void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200732void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200733void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200734void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200735void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200736void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
737 void (*work)(struct work_struct *), gfp_t gfp_mask,
738 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200739u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
740int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200741int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
742 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200743int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
744void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200745void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200747u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
748void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200749u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200750u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200751int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
752 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200753void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200754void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
755 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200756void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
758 unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200759void pblk_end_io_sync(struct nvm_rq *rqd);
760int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
761 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
763 int nr_pages);
764void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200765void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
766 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200767void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
768void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
769 struct ppa_addr ppa);
770void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
771 struct ppa_addr ppa, struct ppa_addr entry_line);
772int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200773 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200774void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
775 u64 *lba_list, int nr_secs);
776void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
777 sector_t blba, int nr_secs);
778
779/*
780 * pblk user I/O write path
781 */
782int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
783 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200784int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200785
786/*
787 * pblk map
788 */
789void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
790 unsigned int sentry, unsigned long *lun_bitmap,
791 unsigned int valid_secs, struct ppa_addr *erase_ppa);
792void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
793 unsigned long *lun_bitmap, unsigned int valid_secs,
794 unsigned int off);
795
796/*
797 * pblk write thread
798 */
799int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700800void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200801void pblk_write_should_kick(struct pblk *pblk);
802
803/*
804 * pblk read path
805 */
NeilBrownb25d5232017-06-18 14:38:58 +1000806extern struct bio_set *pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200807int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200808int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200809/*
810 * pblk recovery
811 */
812void pblk_submit_rec(struct work_struct *work);
813struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200814int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100815int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200816int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
817 struct pblk_rec_ctx *recovery, u64 *comp_bits,
818 unsigned int comp);
819
820/*
821 * pblk gc
822 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200823#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200824#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200825#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
826#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200827
828int pblk_gc_init(struct pblk *pblk);
829void pblk_gc_exit(struct pblk *pblk);
830void pblk_gc_should_start(struct pblk *pblk);
831void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200832void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200833void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200834void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
835 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200836int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200837
838/*
839 * pblk rate limiter
840 */
841void pblk_rl_init(struct pblk_rl *rl, int budget);
842void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200843void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200844int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200845unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100846unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200848void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200849void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
850int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
851void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
852void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200853int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100855void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
856 bool used);
Javier González588726d32017-06-26 11:57:29 +0200857int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200858
859/*
860 * pblk sysfs
861 */
862int pblk_sysfs_init(struct gendisk *tdisk);
863void pblk_sysfs_exit(struct gendisk *tdisk);
864
865static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
866{
867 if (type == PBLK_KMALLOC_META)
868 return kmalloc(size, flags);
869 return vmalloc(size);
870}
871
872static inline void pblk_mfree(void *ptr, int type)
873{
874 if (type == PBLK_KMALLOC_META)
875 kfree(ptr);
876 else
877 vfree(ptr);
878}
879
880static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
881{
882 return c_ctx - sizeof(struct nvm_rq);
883}
884
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200885static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200887 return emeta->bb_bitmap;
888}
889
890static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
891{
892 return ((void *)emeta + pblk->lm.emeta_len[1]);
893}
894
895static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
896{
897 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898}
899
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200900static inline int pblk_line_vsc(struct pblk_line *line)
901{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200902 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200903}
904
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905#define NVM_MEM_PAGE_WRITE (8)
906
907static inline int pblk_pad_distance(struct pblk *pblk)
908{
909 struct nvm_tgt_dev *dev = pblk->dev;
910 struct nvm_geo *geo = &dev->geo;
911
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100912 return NVM_MEM_PAGE_WRITE * geo->all_luns * geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200913}
914
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100915static inline int pblk_ppa_to_line(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200916{
917 return p.g.blk;
918}
919
920static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
921{
922 return p.g.lun * geo->nr_chnls + p.g.ch;
923}
924
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100925static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
926 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927{
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100928 struct ppa_addr ppa;
929
930 ppa.ppa = 0;
931 ppa.g.blk = line_id;
932 ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
933 ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
934 ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
935 ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
936 ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
937
938 return ppa;
939}
940
941static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
942 struct ppa_addr p)
943{
944 u64 paddr;
945
946 paddr = (u64)p.g.pg << pblk->ppaf.pg_offset;
947 paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
948 paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
949 paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
950 paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
951
952 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200953}
954
955static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
956{
957 struct ppa_addr ppa64;
958
959 ppa64.ppa = 0;
960
961 if (ppa32 == -1) {
962 ppa64.ppa = ADDR_EMPTY;
963 } else if (ppa32 & (1U << 31)) {
964 ppa64.c.line = ppa32 & ((~0U) >> 1);
965 ppa64.c.is_cached = 1;
966 } else {
967 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
968 pblk->ppaf.blk_offset;
969 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
970 pblk->ppaf.pg_offset;
971 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
972 pblk->ppaf.lun_offset;
973 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
974 pblk->ppaf.ch_offset;
975 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
976 pblk->ppaf.pln_offset;
977 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
978 pblk->ppaf.sec_offset;
979 }
980
981 return ppa64;
982}
983
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200984static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
985{
986 u32 ppa32 = 0;
987
988 if (ppa64.ppa == ADDR_EMPTY) {
989 ppa32 = ~0U;
990 } else if (ppa64.c.is_cached) {
991 ppa32 |= ppa64.c.line;
992 ppa32 |= 1U << 31;
993 } else {
994 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
995 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
996 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
997 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
998 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
999 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
1000 }
1001
1002 return ppa32;
1003}
1004
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001005static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1006 sector_t lba)
1007{
1008 struct ppa_addr ppa;
1009
1010 if (pblk->ppaf_bitsize < 32) {
1011 u32 *map = (u32 *)pblk->trans_map;
1012
1013 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1014 } else {
1015 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1016
1017 ppa = map[lba];
1018 }
1019
1020 return ppa;
1021}
1022
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001023static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1024 struct ppa_addr ppa)
1025{
1026 if (pblk->ppaf_bitsize < 32) {
1027 u32 *map = (u32 *)pblk->trans_map;
1028
1029 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1030 } else {
1031 u64 *map = (u64 *)pblk->trans_map;
1032
1033 map[lba] = ppa.ppa;
1034 }
1035}
1036
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001037static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1038{
1039 return (ppa_addr.ppa == ADDR_EMPTY);
1040}
1041
1042static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1043{
1044 ppa_addr->ppa = ADDR_EMPTY;
1045}
1046
Javier González076984662017-06-30 17:56:42 +02001047static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1048{
1049 if (lppa.ppa == rppa.ppa)
1050 return true;
1051
1052 return false;
1053}
1054
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001055static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1056{
1057 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1058}
1059
1060static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1061{
1062 return ppa.c.line;
1063}
1064
1065static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1066{
1067 struct ppa_addr p;
1068
1069 p.c.line = addr;
1070 p.c.is_cached = 1;
1071
1072 return p;
1073}
1074
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001075static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001076 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077{
1078 u32 crc = ~(u32)0;
1079
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001080 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081 sizeof(struct line_header) - sizeof(crc));
1082
1083 return crc;
1084}
1085
1086static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1087 struct line_smeta *smeta)
1088{
1089 struct pblk_line_meta *lm = &pblk->lm;
1090 u32 crc = ~(u32)0;
1091
1092 crc = crc32_le(crc, (unsigned char *)smeta +
1093 sizeof(struct line_header) + sizeof(crc),
1094 lm->smeta_len -
1095 sizeof(struct line_header) - sizeof(crc));
1096
1097 return crc;
1098}
1099
1100static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1101 struct line_emeta *emeta)
1102{
1103 struct pblk_line_meta *lm = &pblk->lm;
1104 u32 crc = ~(u32)0;
1105
1106 crc = crc32_le(crc, (unsigned char *)emeta +
1107 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001108 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001109 sizeof(struct line_header) - sizeof(crc));
1110
1111 return crc;
1112}
1113
1114static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1115{
1116 struct nvm_tgt_dev *dev = pblk->dev;
1117 struct nvm_geo *geo = &dev->geo;
1118 int flags;
1119
1120 flags = geo->plane_mode >> 1;
1121
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001122 if (type == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123 flags |= NVM_IO_SCRAMBLE_ENABLE;
1124
1125 return flags;
1126}
1127
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001128enum {
1129 PBLK_READ_RANDOM = 0,
1130 PBLK_READ_SEQUENTIAL = 1,
1131};
1132
1133static inline int pblk_set_read_mode(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001134{
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001135 struct nvm_tgt_dev *dev = pblk->dev;
1136 struct nvm_geo *geo = &dev->geo;
1137 int flags;
1138
1139 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1140 if (type == PBLK_READ_SEQUENTIAL)
1141 flags |= geo->plane_mode >> 1;
1142
1143 return flags;
1144}
1145
1146static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1147{
1148 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001149}
1150
1151#ifdef CONFIG_NVM_DEBUG
1152static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1153{
1154 if (p->c.is_cached) {
1155 pr_err("ppa: (%s: %x) cache line: %llu\n",
1156 msg, error, (u64)p->c.line);
1157 } else {
1158 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1159 msg, error,
1160 p->g.ch, p->g.lun, p->g.blk,
1161 p->g.pg, p->g.pl, p->g.sec);
1162 }
1163}
1164
1165static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1166 int error)
1167{
1168 int bit = -1;
1169
1170 if (rqd->nr_ppas == 1) {
1171 print_ppa(&rqd->ppa_addr, "rqd", error);
1172 return;
1173 }
1174
1175 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1176 bit + 1)) < rqd->nr_ppas) {
1177 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1178 }
1179
1180 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1181}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182
1183static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1184 struct ppa_addr *ppas, int nr_ppas)
1185{
1186 struct nvm_geo *geo = &tgt_dev->geo;
1187 struct ppa_addr *ppa;
1188 int i;
1189
1190 for (i = 0; i < nr_ppas; i++) {
1191 ppa = &ppas[i];
1192
1193 if (!ppa->c.is_cached &&
1194 ppa->g.ch < geo->nr_chnls &&
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001195 ppa->g.lun < geo->nr_luns &&
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001196 ppa->g.pl < geo->nr_planes &&
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001197 ppa->g.blk < geo->nr_chks &&
1198 ppa->g.pg < geo->ws_per_chk &&
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001199 ppa->g.sec < geo->sec_per_pg)
1200 continue;
1201
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001202 print_ppa(ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001203
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001204 return 1;
1205 }
1206 return 0;
1207}
1208
Javier González1a94b2d2017-10-13 14:46:47 +02001209static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1210{
1211 struct nvm_tgt_dev *dev = pblk->dev;
1212 struct ppa_addr *ppa_list;
1213
1214 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1215
1216 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1217 WARN_ON(1);
1218 return -EINVAL;
1219 }
1220
1221 if (rqd->opcode == NVM_OP_PWRITE) {
1222 struct pblk_line *line;
1223 struct ppa_addr ppa;
1224 int i;
1225
1226 for (i = 0; i < rqd->nr_ppas; i++) {
1227 ppa = ppa_list[i];
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001228 line = &pblk->lines[pblk_ppa_to_line(ppa)];
Javier González1a94b2d2017-10-13 14:46:47 +02001229
1230 spin_lock(&line->lock);
1231 if (line->state != PBLK_LINESTATE_OPEN) {
1232 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1233 line->id, line->state);
1234 WARN_ON(1);
1235 spin_unlock(&line->lock);
1236 return -EINVAL;
1237 }
1238 spin_unlock(&line->lock);
1239 }
1240 }
1241
1242 return 0;
1243}
1244#endif
1245
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001246static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1247{
1248 struct pblk_line_meta *lm = &pblk->lm;
1249
1250 if (paddr > lm->sec_per_line)
1251 return 1;
1252
1253 return 0;
1254}
1255
1256static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1257{
1258 return bio->bi_iter.bi_idx;
1259}
1260
1261static inline sector_t pblk_get_lba(struct bio *bio)
1262{
1263 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1264}
1265
1266static inline unsigned int pblk_get_secs(struct bio *bio)
1267{
1268 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1269}
1270
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001271static inline void pblk_setup_uuid(struct pblk *pblk)
1272{
1273 uuid_le uuid;
1274
1275 uuid_le_gen(&uuid);
1276 memcpy(pblk->instance_uuid, uuid.b, 16);
1277}
1278#endif /* PBLK_H_ */