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>
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.
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.
16 * Implementation of a Physical Block-device target for Open-channel SSDs.
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>
32 #include <linux/lightnvm.h>
34 /* Run only GC if less than 1/X blocks are free */
35 #define GC_LIMIT_INVERSE 5
36 #define GC_TIME_MSECS 1000
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)
43 #define PBLK_WS_POOL_SIZE (128)
44 #define PBLK_META_POOL_SIZE (128)
45 #define PBLK_READ_REQ_POOL_SIZE (1024)
47 #define PBLK_NR_CLOSE_JOBS (4)
49 #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
51 #define PBLK_COMMAND_TIMEOUT_MS 30000
53 /* Max 512 LUNs per device */
54 #define PBLK_MAX_LUNS_BITMAP (4)
56 #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
58 #define pblk_for_each_lun(pblk, rlun, i) \
59 for ((i) = 0, rlun = &(pblk)->luns[0]; \
60 (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)])
62 #define ERASE 2 /* READ = 0, WRITE = 1 */
66 PBLK_IOTYPE_USER = 1 << 0,
67 PBLK_IOTYPE_GC = 1 << 1,
69 /* Write buffer flags */
70 PBLK_FLUSH_ENTRY = 1 << 2,
71 PBLK_WRITTEN_DATA = 1 << 3,
72 PBLK_SUBMITTED_ENTRY = 1 << 4,
73 PBLK_WRITABLE_ENTRY = 1 << 5,
77 PBLK_BLK_ST_OPEN = 0x1,
78 PBLK_BLK_ST_CLOSED = 0x2,
81 struct pblk_sec_meta {
86 /* The number of GC lists and the rate-limiter states go together. This way the
87 * rate-limiter can dictate how much GC is needed based on resource utilization.
89 #define PBLK_GC_NR_LISTS 3
97 #define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
99 /* write buffer completion context */
101 struct list_head list; /* Head for out-of-order completion */
103 unsigned long *lun_bitmap; /* Luns used on current request */
105 unsigned int nr_valid;
106 unsigned int nr_padded;
109 /* generic context */
114 /* Recovery context */
115 struct pblk_rec_ctx {
118 struct list_head failed;
119 struct work_struct ws_rec;
124 struct bio_list bios; /* Original bios - used for completion
125 * in REQ_FUA, REQ_FLUSH case
127 u64 lba; /* Logic addr. associated with entry */
128 struct ppa_addr ppa; /* Physic addr. associated with entry */
129 int flags; /* Write context flags */
132 struct pblk_rb_entry {
133 struct ppa_addr cacheline; /* Cacheline for this entry */
134 void *data; /* Pointer to data on this entry */
135 struct pblk_w_ctx w_ctx; /* Context for this entry */
136 struct list_head index; /* List head to enable indexes */
139 #define EMPTY_ENTRY (~0U)
141 struct pblk_rb_pages {
144 struct list_head list;
148 struct pblk_rb_entry *entries; /* Ring buffer entries */
149 unsigned int mem; /* Write offset - points to next
150 * writable entry in memory
152 unsigned int subm; /* Read offset - points to last entry
153 * that has been submitted to the media
156 unsigned int sync; /* Synced - backpointer that signals
157 * the last submitted entry that has
158 * been successfully persisted to media
160 unsigned int sync_point; /* Sync point - last entry that must be
161 * flushed to the media. Used with
162 * REQ_FLUSH and REQ_FUA
164 unsigned int l2p_update; /* l2p update point - next entry for
165 * which l2p mapping will be updated to
166 * contain a device ppa address (instead
169 unsigned int nr_entries; /* Number of entries in write buffer -
170 * must be a power of two
172 unsigned int seg_size; /* Size of the data segments being
173 * stored on each entry. Typically this
177 struct list_head pages; /* List of data pages */
179 spinlock_t w_lock; /* Write lock */
180 spinlock_t s_lock; /* Sync lock */
182 #ifdef CONFIG_NVM_DEBUG
183 atomic_t inflight_sync_point; /* Not served REQ_FLUSH | REQ_FUA */
187 #define PBLK_RECOVERY_SECTORS 16
190 struct ppa_addr bppa;
192 u8 *bb_list; /* Bad block list for LUN. Only used on
193 * bring up. Bad blocks are managed
194 * within lines on run-time.
197 struct semaphore wr_sem;
201 struct pblk_line *line;
203 u64 lba_list[PBLK_MAX_REQ_ADDRS];
206 struct list_head list;
210 /* These states are not protected by a lock since (i) they are in the
211 * fast path, and (ii) they are not critical.
217 struct task_struct *gc_ts;
218 struct task_struct *gc_writer_ts;
219 struct task_struct *gc_reader_ts;
221 struct workqueue_struct *gc_line_reader_wq;
222 struct workqueue_struct *gc_reader_wq;
224 struct timer_list gc_timer;
226 struct semaphore gc_sem;
227 atomic_t inflight_gc;
230 struct list_head w_list;
231 struct list_head r_list;
239 unsigned int high; /* Upper threshold for rate limiter (free run -
240 * user I/O rate limiter
242 unsigned int low; /* Lower threshold for rate limiter (user I/O
243 * rate limiter - stall)
245 unsigned int high_pw; /* High rounded up as a power of 2 */
247 #define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
248 #define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
250 int rb_windows_pw; /* Number of rate windows in the write buffer
251 * given as a power-of-2. This guarantees that
252 * when user I/O is being rate limited, there
253 * will be reserved enough space for the GC to
254 * place its payload. A window is of
255 * pblk->max_write_pgs size, which in NVMe is
258 int rb_budget; /* Total number of entries available for I/O */
259 int rb_user_max; /* Max buffer entries available for user I/O */
260 atomic_t rb_user_cnt; /* User I/O buffer counter */
261 int rb_gc_max; /* Max buffer entries available for GC I/O */
262 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
263 int rb_state; /* Rate-limiter current state */
264 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
266 int rsv_blocks; /* Reserved blocks for GC */
271 struct timer_list u_timer;
273 unsigned long long nr_secs;
274 unsigned long total_blocks;
275 atomic_t free_blocks;
278 #define PBLK_LINE_EMPTY (~0U)
282 PBLK_LINETYPE_FREE = 0,
283 PBLK_LINETYPE_LOG = 1,
284 PBLK_LINETYPE_DATA = 2,
287 PBLK_LINESTATE_FREE = 10,
288 PBLK_LINESTATE_OPEN = 11,
289 PBLK_LINESTATE_CLOSED = 12,
290 PBLK_LINESTATE_GC = 13,
291 PBLK_LINESTATE_BAD = 14,
292 PBLK_LINESTATE_CORRUPT = 15,
295 PBLK_LINEGC_NONE = 20,
296 PBLK_LINEGC_EMPTY = 21,
297 PBLK_LINEGC_LOW = 22,
298 PBLK_LINEGC_MID = 23,
299 PBLK_LINEGC_HIGH = 24,
300 PBLK_LINEGC_FULL = 25,
303 #define PBLK_MAGIC 0x70626c6b /*pblk*/
307 __le32 identifier; /* pblk identifier */
308 __u8 uuid[16]; /* instance uuid */
309 __le16 type; /* line type */
310 __le16 version; /* type version */
311 __le32 id; /* line id for current line */
315 struct line_header header;
317 __le32 crc; /* Full structure including struct crc */
318 /* Previous line metadata */
319 __le32 prev_id; /* Line id for previous line */
321 /* Current line metadata */
322 __le64 seq_nr; /* Sequence number for current line */
325 __le32 window_wr_lun; /* Number of parallel LUNs to write */
333 * Metadata layout in media:
335 * 1. struct line_emeta
336 * 2. bad block bitmap (u64 * window_wr_lun)
337 * Mid sectors (start at lbas_sector):
338 * 3. nr_lbas (u64) forming lba list
339 * Last sectors (start at vsc_sector):
340 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
343 struct line_header header;
345 __le32 crc; /* Full structure including struct crc */
347 /* Previous line metadata */
348 __le32 prev_id; /* Line id for prev line */
350 /* Current line metadata */
351 __le64 seq_nr; /* Sequence number for current line */
354 __le32 window_wr_lun; /* Number of parallel LUNs to write */
356 /* Bookkeeping for recovery */
357 __le32 next_id; /* Line id for next line */
358 __le64 nr_lbas; /* Number of lbas mapped in line */
359 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
360 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
364 struct line_emeta *buf; /* emeta buffer in media format */
365 int mem; /* Write offset - points to next
366 * writable entry in memory
368 atomic_t sync; /* Synced - backpointer that signals the
369 * last entry that has been successfully
372 unsigned int nr_entries; /* Number of emeta entries */
376 struct line_smeta *buf; /* smeta buffer in persistent format */
381 unsigned int id; /* Line number corresponds to the
384 unsigned int seq_nr; /* Unique line sequence number */
386 int state; /* PBLK_LINESTATE_X */
387 int type; /* PBLK_LINETYPE_X */
388 int gc_group; /* PBLK_LINEGC_X */
389 struct list_head list; /* Free, GC lists */
391 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
393 struct pblk_smeta *smeta; /* Start metadata */
394 struct pblk_emeta *emeta; /* End medatada */
396 int meta_line; /* Metadata line id */
397 int meta_distance; /* Distance between data and metadata */
399 u64 smeta_ssec; /* Sector where smeta starts */
400 u64 emeta_ssec; /* Sector where emeta starts */
402 unsigned int sec_in_line; /* Number of usable secs in line */
404 atomic_t blk_in_line; /* Number of good blocks in line */
405 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
406 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
408 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
409 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
411 atomic_t left_eblks; /* Blocks left for erasing */
412 atomic_t left_seblks; /* Blocks left for sync erasing */
414 int left_msecs; /* Sectors left for mapping */
415 unsigned int cur_sec; /* Sector map pointer */
416 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
418 __le32 *vsc; /* Valid sector count in line */
420 struct kref ref; /* Write buffer L2P references */
422 spinlock_t lock; /* Necessary for invalid_bitmap only */
425 #define PBLK_DATA_LINES 4
428 PBLK_KMALLOC_META = 1,
429 PBLK_VMALLOC_META = 2,
433 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
434 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
435 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
438 struct pblk_line_mgmt {
439 int nr_lines; /* Total number of full lines */
440 int nr_free_lines; /* Number of full lines in free list */
442 /* Free lists - use free_lock */
443 struct list_head free_list; /* Full lines ready to use */
444 struct list_head corrupt_list; /* Full lines corrupted */
445 struct list_head bad_list; /* Full lines bad */
447 /* GC lists - use gc_lock */
448 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
449 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
450 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
451 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
453 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
454 struct list_head gc_empty_list; /* Full lines close, all valid */
456 struct pblk_line *log_line; /* Current FTL log line */
457 struct pblk_line *data_line; /* Current data line */
458 struct pblk_line *log_next; /* Next FTL log line */
459 struct pblk_line *data_next; /* Next data line */
461 struct list_head emeta_list; /* Lines queued to schedule emeta */
463 __le32 *vsc_list; /* Valid sector counts for all lines */
465 /* Metadata allocation type: VMALLOC | KMALLOC */
466 int emeta_alloc_type;
468 /* Pre-allocated metadata for data lines */
469 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
470 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
471 unsigned long meta_bitmap;
473 /* Helpers for fast bitmap calculations */
474 unsigned long *bb_template;
475 unsigned long *bb_aux;
477 unsigned long d_seq_nr; /* Data line unique sequence number */
478 unsigned long l_seq_nr; /* Log line unique sequence number */
480 spinlock_t free_lock;
481 spinlock_t close_lock;
485 struct pblk_line_meta {
486 unsigned int smeta_len; /* Total length for smeta */
487 unsigned int smeta_sec; /* Sectors needed for smeta */
489 unsigned int emeta_len[4]; /* Lengths for emeta:
491 * [1]: struct line_emeta length
492 * [2]: L2P portion length
493 * [3]: vsc list length
495 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
499 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
501 unsigned int vsc_list_len; /* Length for vsc list */
502 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
503 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
504 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
506 unsigned int blk_per_line; /* Number of blocks in a full line */
507 unsigned int sec_per_line; /* Number of sectors in a line */
508 unsigned int dsec_per_line; /* Number of data sectors in a line */
509 unsigned int min_blk_line; /* Min. number of good blocks in line */
511 unsigned int mid_thrs; /* Threshold for GC mid list */
512 unsigned int high_thrs; /* Threshold for GC high list */
514 unsigned int meta_distance; /* Distance between data and metadata */
517 struct pblk_addr_format {
533 struct nvm_tgt_dev *dev;
534 struct gendisk *disk;
538 struct pblk_lun *luns;
540 struct pblk_line *lines; /* Line array */
541 struct pblk_line_mgmt l_mg; /* Line management */
542 struct pblk_line_meta lm; /* Line metadata */
545 struct pblk_addr_format ppaf;
549 int min_write_pgs; /* Minimum amount of pages required by controller */
550 int max_write_pgs; /* Maximum amount of pages supported by controller */
551 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
552 * guarantee successful reads.
555 sector_t capacity; /* Device capacity when bad blocks are subtracted */
556 int over_pct; /* Percentage of device used for over-provisioning */
558 /* pblk provisioning values. Used by rate limiter */
563 unsigned char instance_uuid[16];
564 #ifdef CONFIG_NVM_DEBUG
565 /* All debug counters apply to 4kb sector I/Os */
566 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
567 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
568 atomic_long_t padded_wb; /* Sectors padded in write buffer */
569 atomic_long_t nr_flush; /* Number of flush/fua I/O */
570 atomic_long_t req_writes; /* Sectors stored on write buffer */
571 atomic_long_t sub_writes; /* Sectors submitted from buffer */
572 atomic_long_t sync_writes; /* Sectors synced to media */
573 atomic_long_t inflight_reads; /* Inflight sector read requests */
574 atomic_long_t cache_reads; /* Read requests that hit the cache */
575 atomic_long_t sync_reads; /* Completed sector read requests */
576 atomic_long_t recov_writes; /* Sectors submitted from recovery */
577 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
578 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
583 atomic_long_t read_failed;
584 atomic_long_t read_empty;
585 atomic_long_t read_high_ecc;
586 atomic_long_t read_failed_gc;
587 atomic_long_t write_failed;
588 atomic_long_t erase_failed;
590 struct task_struct *writer_ts;
592 /* Simple translation map of logical addresses to physical addresses.
593 * The logical addresses is known by the host system, while the physical
594 * addresses are used when writing to the disk block device.
596 unsigned char *trans_map;
597 spinlock_t trans_lock;
599 struct list_head compl_list;
601 mempool_t *page_pool;
602 mempool_t *line_ws_pool;
604 mempool_t *g_rq_pool;
605 mempool_t *w_rq_pool;
606 mempool_t *line_meta_pool;
608 struct workqueue_struct *close_wq;
609 struct workqueue_struct *bb_wq;
611 struct timer_list wtimer;
616 struct pblk_line_ws {
618 struct pblk_line *line;
620 struct work_struct ws;
623 #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
624 #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
627 * pblk ring buffer operations
629 int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
630 unsigned int power_size, unsigned int power_seg_sz);
631 unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
632 void *pblk_rb_entries_ref(struct pblk_rb *rb);
633 int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
634 unsigned int nr_entries, unsigned int *pos);
635 int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
637 void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
638 struct pblk_w_ctx w_ctx, unsigned int pos);
639 void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
640 struct pblk_w_ctx w_ctx, struct pblk_line *gc_line,
642 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
644 void pblk_rb_sync_l2p(struct pblk_rb *rb);
645 unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
646 struct bio *bio, unsigned int pos,
647 unsigned int nr_entries, unsigned int count);
648 unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
649 struct list_head *list,
651 int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
652 u64 pos, int bio_iter);
653 unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
655 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
656 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
657 struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
658 struct ppa_addr *ppa);
659 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
660 unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb);
662 unsigned int pblk_rb_read_count(struct pblk_rb *rb);
663 unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
665 int pblk_rb_tear_down_check(struct pblk_rb *rb);
666 int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
667 void pblk_rb_data_free(struct pblk_rb *rb);
668 ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
673 struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw);
674 void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
675 int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
676 struct pblk_c_ctx *c_ctx);
677 void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw);
678 void pblk_flush_writer(struct pblk *pblk);
679 struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba);
680 void pblk_discard(struct pblk *pblk, struct bio *bio);
681 void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
682 void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
683 int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
684 int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
685 struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
686 unsigned int nr_secs, unsigned int len,
688 struct pblk_line *pblk_line_get(struct pblk *pblk);
689 struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
690 struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
691 int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
692 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
693 struct pblk_line *pblk_line_get_data(struct pblk *pblk);
694 struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
695 int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
696 int pblk_line_is_full(struct pblk_line *line);
697 void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
698 void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
699 void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
700 void pblk_line_close_ws(struct work_struct *work);
701 void pblk_line_mark_bb(struct work_struct *work);
702 void pblk_line_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
703 void (*work)(struct work_struct *),
704 struct workqueue_struct *wq);
705 u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
706 int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
707 int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
709 int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
710 void pblk_line_put(struct kref *ref);
711 struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
712 u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
713 void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
714 u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
715 u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
716 int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
717 unsigned long secs_to_flush);
718 void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
719 unsigned long *lun_bitmap);
720 void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
721 unsigned long *lun_bitmap);
722 void pblk_end_bio_sync(struct bio *bio);
723 void pblk_end_io_sync(struct nvm_rq *rqd);
724 int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
726 void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
728 void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
729 void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
731 void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
732 void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
733 struct ppa_addr ppa);
734 void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
735 struct ppa_addr ppa, struct ppa_addr entry_line);
736 int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
737 struct pblk_line *gc_line);
738 void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
739 u64 *lba_list, int nr_secs);
740 void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
741 sector_t blba, int nr_secs);
744 * pblk user I/O write path
746 int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
747 unsigned long flags);
748 int pblk_write_gc_to_cache(struct pblk *pblk, void *data, u64 *lba_list,
749 unsigned int nr_entries, unsigned int nr_rec_entries,
750 struct pblk_line *gc_line, unsigned long flags);
755 void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
756 unsigned int sentry, unsigned long *lun_bitmap,
757 unsigned int valid_secs, struct ppa_addr *erase_ppa);
758 void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
759 unsigned long *lun_bitmap, unsigned int valid_secs,
765 int pblk_write_ts(void *data);
766 void pblk_write_timer_fn(unsigned long data);
767 void pblk_write_should_kick(struct pblk *pblk);
772 extern struct bio_set *pblk_bio_set;
773 int pblk_submit_read(struct pblk *pblk, struct bio *bio);
774 int pblk_submit_read_gc(struct pblk *pblk, u64 *lba_list, void *data,
775 unsigned int nr_secs, unsigned int *secs_to_gc,
776 struct pblk_line *line);
780 void pblk_submit_rec(struct work_struct *work);
781 struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
782 void pblk_recov_pad(struct pblk *pblk);
783 __le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta);
784 int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
785 struct pblk_rec_ctx *recovery, u64 *comp_bits,
791 #define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
792 #define PBLK_GC_W_QD 1024 /* Queue depth for inflight GC write I/Os */
793 #define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
794 #define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
796 int pblk_gc_init(struct pblk *pblk);
797 void pblk_gc_exit(struct pblk *pblk);
798 void pblk_gc_should_start(struct pblk *pblk);
799 void pblk_gc_should_stop(struct pblk *pblk);
800 void pblk_gc_should_kick(struct pblk *pblk);
801 void pblk_gc_kick(struct pblk *pblk);
802 void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
804 int pblk_gc_sysfs_force(struct pblk *pblk, int force);
809 void pblk_rl_init(struct pblk_rl *rl, int budget);
810 void pblk_rl_free(struct pblk_rl *rl);
811 int pblk_rl_high_thrs(struct pblk_rl *rl);
812 int pblk_rl_low_thrs(struct pblk_rl *rl);
813 unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
814 int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
815 void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
816 int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
817 void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
818 void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
819 int pblk_rl_sysfs_rate_show(struct pblk_rl *rl);
820 void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
821 void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line);
826 int pblk_sysfs_init(struct gendisk *tdisk);
827 void pblk_sysfs_exit(struct gendisk *tdisk);
829 static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
831 if (type == PBLK_KMALLOC_META)
832 return kmalloc(size, flags);
833 return vmalloc(size);
836 static inline void pblk_mfree(void *ptr, int type)
838 if (type == PBLK_KMALLOC_META)
844 static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
846 return c_ctx - sizeof(struct nvm_rq);
849 static inline void *emeta_to_bb(struct line_emeta *emeta)
851 return emeta->bb_bitmap;
854 static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
856 return ((void *)emeta + pblk->lm.emeta_len[1]);
859 static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
861 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
864 static inline int pblk_line_vsc(struct pblk_line *line)
868 spin_lock(&line->lock);
869 vsc = le32_to_cpu(*line->vsc);
870 spin_unlock(&line->lock);
875 #define NVM_MEM_PAGE_WRITE (8)
877 static inline int pblk_pad_distance(struct pblk *pblk)
879 struct nvm_tgt_dev *dev = pblk->dev;
880 struct nvm_geo *geo = &dev->geo;
882 return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl;
885 static inline int pblk_dev_ppa_to_line(struct ppa_addr p)
890 static inline int pblk_tgt_ppa_to_line(struct ppa_addr p)
895 static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
897 return p.g.lun * geo->nr_chnls + p.g.ch;
900 /* A block within a line corresponds to the lun */
901 static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
903 return p.g.lun * geo->nr_chnls + p.g.ch;
906 static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
908 struct ppa_addr ppa64;
913 ppa64.ppa = ADDR_EMPTY;
914 } else if (ppa32 & (1U << 31)) {
915 ppa64.c.line = ppa32 & ((~0U) >> 1);
916 ppa64.c.is_cached = 1;
918 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
919 pblk->ppaf.blk_offset;
920 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
921 pblk->ppaf.pg_offset;
922 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
923 pblk->ppaf.lun_offset;
924 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
925 pblk->ppaf.ch_offset;
926 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
927 pblk->ppaf.pln_offset;
928 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
929 pblk->ppaf.sec_offset;
935 static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
940 if (pblk->ppaf_bitsize < 32) {
941 u32 *map = (u32 *)pblk->trans_map;
943 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
945 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
953 static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
957 if (ppa64.ppa == ADDR_EMPTY) {
959 } else if (ppa64.c.is_cached) {
960 ppa32 |= ppa64.c.line;
963 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
964 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
965 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
966 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
967 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
968 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
974 static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
977 if (pblk->ppaf_bitsize < 32) {
978 u32 *map = (u32 *)pblk->trans_map;
980 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
982 u64 *map = (u64 *)pblk->trans_map;
988 static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
994 paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset;
995 paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
996 paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
997 paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
998 paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
1003 static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1005 return (ppa_addr.ppa == ADDR_EMPTY);
1008 static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1010 ppa_addr->ppa = ADDR_EMPTY;
1013 static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1015 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1018 static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1023 static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1033 static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1036 struct ppa_addr ppa;
1039 ppa.g.blk = line_id;
1040 ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
1041 ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
1042 ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
1043 ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
1044 ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
1049 static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr,
1052 struct ppa_addr ppa;
1054 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
1059 static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
1060 struct line_header *header)
1064 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
1065 sizeof(struct line_header) - sizeof(crc));
1070 static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1071 struct line_smeta *smeta)
1073 struct pblk_line_meta *lm = &pblk->lm;
1076 crc = crc32_le(crc, (unsigned char *)smeta +
1077 sizeof(struct line_header) + sizeof(crc),
1079 sizeof(struct line_header) - sizeof(crc));
1084 static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1085 struct line_emeta *emeta)
1087 struct pblk_line_meta *lm = &pblk->lm;
1090 crc = crc32_le(crc, (unsigned char *)emeta +
1091 sizeof(struct line_header) + sizeof(crc),
1093 sizeof(struct line_header) - sizeof(crc));
1098 static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1100 struct nvm_tgt_dev *dev = pblk->dev;
1101 struct nvm_geo *geo = &dev->geo;
1104 flags = geo->plane_mode >> 1;
1107 flags |= NVM_IO_SCRAMBLE_ENABLE;
1113 PBLK_READ_RANDOM = 0,
1114 PBLK_READ_SEQUENTIAL = 1,
1117 static inline int pblk_set_read_mode(struct pblk *pblk, int type)
1119 struct nvm_tgt_dev *dev = pblk->dev;
1120 struct nvm_geo *geo = &dev->geo;
1123 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1124 if (type == PBLK_READ_SEQUENTIAL)
1125 flags |= geo->plane_mode >> 1;
1130 static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1132 return !(nr_secs % pblk->min_write_pgs);
1135 #ifdef CONFIG_NVM_DEBUG
1136 static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1138 if (p->c.is_cached) {
1139 pr_err("ppa: (%s: %x) cache line: %llu\n",
1140 msg, error, (u64)p->c.line);
1142 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1144 p->g.ch, p->g.lun, p->g.blk,
1145 p->g.pg, p->g.pl, p->g.sec);
1149 static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1154 if (rqd->nr_ppas == 1) {
1155 print_ppa(&rqd->ppa_addr, "rqd", error);
1159 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1160 bit + 1)) < rqd->nr_ppas) {
1161 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1164 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1168 static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1169 struct ppa_addr *ppas, int nr_ppas)
1171 struct nvm_geo *geo = &tgt_dev->geo;
1172 struct ppa_addr *ppa;
1175 for (i = 0; i < nr_ppas; i++) {
1178 if (!ppa->c.is_cached &&
1179 ppa->g.ch < geo->nr_chnls &&
1180 ppa->g.lun < geo->luns_per_chnl &&
1181 ppa->g.pl < geo->nr_planes &&
1182 ppa->g.blk < geo->blks_per_lun &&
1183 ppa->g.pg < geo->pgs_per_blk &&
1184 ppa->g.sec < geo->sec_per_pg)
1187 #ifdef CONFIG_NVM_DEBUG
1188 print_ppa(ppa, "boundary", i);
1195 static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1197 struct pblk_line_meta *lm = &pblk->lm;
1199 if (paddr > lm->sec_per_line)
1205 static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1207 return bio->bi_iter.bi_idx;
1210 static inline sector_t pblk_get_lba(struct bio *bio)
1212 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1215 static inline unsigned int pblk_get_secs(struct bio *bio)
1217 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1220 static inline sector_t pblk_get_sector(sector_t lba)
1222 return lba * NR_PHY_IN_LOG;
1225 static inline void pblk_setup_uuid(struct pblk *pblk)
1230 memcpy(pblk->instance_uuid, uuid.b, 16);
1232 #endif /* PBLK_H_ */