1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
4 * Copyright (C) 2016 CNEX Labs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * Implementation of a Physical Block-device target for Open-channel SSDs.
24 #include <linux/blkdev.h>
25 #include <linux/blk-mq.h>
26 #include <linux/bio.h>
27 #include <linux/module.h>
28 #include <linux/kthread.h>
29 #include <linux/vmalloc.h>
30 #include <linux/crc32.h>
31 #include <linux/uuid.h>
33 #include <linux/lightnvm.h>
35 /* Run only GC if less than 1/X blocks are free */
36 #define GC_LIMIT_INVERSE 5
37 #define GC_TIME_MSECS 1000
39 #define PBLK_SECTOR (512)
40 #define PBLK_EXPOSED_PAGE_SIZE (4096)
42 #define PBLK_NR_CLOSE_JOBS (4)
44 #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
46 #define PBLK_COMMAND_TIMEOUT_MS 30000
48 /* Max 512 LUNs per device */
49 #define PBLK_MAX_LUNS_BITMAP (4)
51 #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
53 /* Static pool sizes */
54 #define PBLK_GEN_WS_POOL_SIZE (2)
56 #define PBLK_DEFAULT_OP (11)
60 PBLK_WRITE = WRITE,/* Write from write buffer */
61 PBLK_WRITE_INT, /* Internal write - no write buffer */
62 PBLK_READ_RECOV, /* Recovery read - errors allowed */
68 PBLK_IOTYPE_USER = 1 << 0,
69 PBLK_IOTYPE_GC = 1 << 1,
71 /* Write buffer flags */
72 PBLK_FLUSH_ENTRY = 1 << 2,
73 PBLK_WRITTEN_DATA = 1 << 3,
74 PBLK_SUBMITTED_ENTRY = 1 << 4,
75 PBLK_WRITABLE_ENTRY = 1 << 5,
79 PBLK_BLK_ST_OPEN = 0x1,
80 PBLK_BLK_ST_CLOSED = 0x2,
84 PBLK_CHUNK_RESET_START,
85 PBLK_CHUNK_RESET_DONE,
86 PBLK_CHUNK_RESET_FAILED,
89 struct pblk_sec_meta {
94 /* The number of GC lists and the rate-limiter states go together. This way the
95 * rate-limiter can dictate how much GC is needed based on resource utilization.
97 #define PBLK_GC_NR_LISTS 4
107 #define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA)
109 /* write buffer completion context */
111 struct list_head list; /* Head for out-of-order completion */
113 unsigned long *lun_bitmap; /* Luns used on current request */
115 unsigned int nr_valid;
116 unsigned int nr_padded;
122 unsigned long start_time;
126 /* partial read context */
128 struct bio *orig_bio;
129 DECLARE_BITMAP(bitmap, NVM_MAX_VLBA);
130 unsigned int orig_nr_secs;
131 unsigned int bio_init_idx;
133 dma_addr_t dma_ppa_list;
134 u64 lba_list_mem[NVM_MAX_VLBA];
135 u64 lba_list_media[NVM_MAX_VLBA];
141 struct completion wait;
145 /* Recovery context */
146 struct pblk_rec_ctx {
149 struct work_struct ws_rec;
154 struct bio_list bios; /* Original bios - used for completion
155 * in REQ_FUA, REQ_FLUSH case
157 u64 lba; /* Logic addr. associated with entry */
158 struct ppa_addr ppa; /* Physic addr. associated with entry */
159 int flags; /* Write context flags */
162 struct pblk_rb_entry {
163 struct ppa_addr cacheline; /* Cacheline for this entry */
164 void *data; /* Pointer to data on this entry */
165 struct pblk_w_ctx w_ctx; /* Context for this entry */
166 struct list_head index; /* List head to enable indexes */
169 #define EMPTY_ENTRY (~0U)
171 struct pblk_rb_pages {
174 struct list_head list;
178 struct pblk_rb_entry *entries; /* Ring buffer entries */
179 unsigned int mem; /* Write offset - points to next
180 * writable entry in memory
182 unsigned int subm; /* Read offset - points to last entry
183 * that has been submitted to the media
186 unsigned int sync; /* Synced - backpointer that signals
187 * the last submitted entry that has
188 * been successfully persisted to media
190 unsigned int flush_point; /* Sync point - last entry that must be
191 * flushed to the media. Used with
192 * REQ_FLUSH and REQ_FUA
194 unsigned int l2p_update; /* l2p update point - next entry for
195 * which l2p mapping will be updated to
196 * contain a device ppa address (instead
199 unsigned int nr_entries; /* Number of entries in write buffer -
200 * must be a power of two
202 unsigned int seg_size; /* Size of the data segments being
203 * stored on each entry. Typically this
207 unsigned int back_thres; /* Threshold that shall be maintained by
208 * the backpointer in order to respect
209 * geo->mw_cunits on a per chunk basis
212 struct list_head pages; /* List of data pages */
214 spinlock_t w_lock; /* Write lock */
215 spinlock_t s_lock; /* Sync lock */
217 #ifdef CONFIG_NVM_PBLK_DEBUG
218 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
222 #define PBLK_RECOVERY_SECTORS 16
225 struct ppa_addr bppa;
226 struct semaphore wr_sem;
230 struct pblk_line *line;
232 u64 paddr_list[NVM_MAX_VLBA];
233 u64 lba_list[NVM_MAX_VLBA];
236 struct list_head list;
240 /* These states are not protected by a lock since (i) they are in the
241 * fast path, and (ii) they are not critical.
247 struct task_struct *gc_ts;
248 struct task_struct *gc_writer_ts;
249 struct task_struct *gc_reader_ts;
251 struct workqueue_struct *gc_line_reader_wq;
252 struct workqueue_struct *gc_reader_wq;
254 struct timer_list gc_timer;
256 struct semaphore gc_sem;
257 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
258 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
259 * started reads to finished writes
263 struct list_head w_list;
264 struct list_head r_list;
272 unsigned int high; /* Upper threshold for rate limiter (free run -
273 * user I/O rate limiter
275 unsigned int high_pw; /* High rounded up as a power of 2 */
277 #define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
278 #define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
280 int rb_windows_pw; /* Number of rate windows in the write buffer
281 * given as a power-of-2. This guarantees that
282 * when user I/O is being rate limited, there
283 * will be reserved enough space for the GC to
284 * place its payload. A window is of
285 * pblk->max_write_pgs size, which in NVMe is
288 int rb_budget; /* Total number of entries available for I/O */
289 int rb_user_max; /* Max buffer entries available for user I/O */
290 int rb_gc_max; /* Max buffer entries available for GC I/O */
291 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
292 int rb_state; /* Rate-limiter current state */
293 int rb_max_io; /* Maximum size for an I/O giving the config */
295 atomic_t rb_user_cnt; /* User I/O buffer counter */
296 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
297 atomic_t rb_space; /* Space limit in case of reaching capacity */
299 int rsv_blocks; /* Reserved blocks for GC */
304 atomic_t werr_lines; /* Number of write error lines that needs gc */
306 struct timer_list u_timer;
308 unsigned long long nr_secs;
309 unsigned long total_blocks;
311 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
312 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
315 #define PBLK_LINE_EMPTY (~0U)
319 PBLK_LINETYPE_FREE = 0,
320 PBLK_LINETYPE_LOG = 1,
321 PBLK_LINETYPE_DATA = 2,
324 PBLK_LINESTATE_NEW = 9,
325 PBLK_LINESTATE_FREE = 10,
326 PBLK_LINESTATE_OPEN = 11,
327 PBLK_LINESTATE_CLOSED = 12,
328 PBLK_LINESTATE_GC = 13,
329 PBLK_LINESTATE_BAD = 14,
330 PBLK_LINESTATE_CORRUPT = 15,
333 PBLK_LINEGC_NONE = 20,
334 PBLK_LINEGC_EMPTY = 21,
335 PBLK_LINEGC_LOW = 22,
336 PBLK_LINEGC_MID = 23,
337 PBLK_LINEGC_HIGH = 24,
338 PBLK_LINEGC_FULL = 25,
339 PBLK_LINEGC_WERR = 26
342 #define PBLK_MAGIC 0x70626c6b /*pblk*/
344 /* emeta/smeta persistent storage format versions:
345 * Changes in major version requires offline migration.
346 * Changes in minor version are handled automatically during
350 #define SMETA_VERSION_MAJOR (0)
351 #define SMETA_VERSION_MINOR (1)
353 #define EMETA_VERSION_MAJOR (0)
354 #define EMETA_VERSION_MINOR (2)
358 __le32 identifier; /* pblk identifier */
359 __u8 uuid[16]; /* instance uuid */
360 __le16 type; /* line type */
361 __u8 version_major; /* version major */
362 __u8 version_minor; /* version minor */
363 __le32 id; /* line id for current line */
367 struct line_header header;
369 __le32 crc; /* Full structure including struct crc */
370 /* Previous line metadata */
371 __le32 prev_id; /* Line id for previous line */
373 /* Current line metadata */
374 __le64 seq_nr; /* Sequence number for current line */
377 __le32 window_wr_lun; /* Number of parallel LUNs to write */
386 * Metadata layout in media:
388 * 1. struct line_emeta
389 * 2. bad block bitmap (u64 * window_wr_lun)
390 * 3. write amplification counters
391 * Mid sectors (start at lbas_sector):
392 * 3. nr_lbas (u64) forming lba list
393 * Last sectors (start at vsc_sector):
394 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
397 struct line_header header;
399 __le32 crc; /* Full structure including struct crc */
401 /* Previous line metadata */
402 __le32 prev_id; /* Line id for prev line */
404 /* Current line metadata */
405 __le64 seq_nr; /* Sequence number for current line */
408 __le32 window_wr_lun; /* Number of parallel LUNs to write */
410 /* Bookkeeping for recovery */
411 __le32 next_id; /* Line id for next line */
412 __le64 nr_lbas; /* Number of lbas mapped in line */
413 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
414 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
418 /* Write amplification counters stored on media */
420 __le64 user; /* Number of user written sectors */
421 __le64 gc; /* Number of sectors written by GC*/
422 __le64 pad; /* Number of padded sectors */
426 struct line_emeta *buf; /* emeta buffer in media format */
427 int mem; /* Write offset - points to next
428 * writable entry in memory
430 atomic_t sync; /* Synced - backpointer that signals the
431 * last entry that has been successfully
434 unsigned int nr_entries; /* Number of emeta entries */
438 struct line_smeta *buf; /* smeta buffer in persistent format */
441 struct pblk_w_err_gc {
448 unsigned int id; /* Line number corresponds to the
451 unsigned int seq_nr; /* Unique line sequence number */
453 int state; /* PBLK_LINESTATE_X */
454 int type; /* PBLK_LINETYPE_X */
455 int gc_group; /* PBLK_LINEGC_X */
456 struct list_head list; /* Free, GC lists */
458 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
460 struct nvm_chk_meta *chks; /* Chunks forming line */
462 struct pblk_smeta *smeta; /* Start metadata */
463 struct pblk_emeta *emeta; /* End medatada */
465 int meta_line; /* Metadata line id */
466 int meta_distance; /* Distance between data and metadata */
468 u64 smeta_ssec; /* Sector where smeta starts */
469 u64 emeta_ssec; /* Sector where emeta starts */
471 unsigned int sec_in_line; /* Number of usable secs in line */
473 atomic_t blk_in_line; /* Number of good blocks in line */
474 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
475 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
477 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
478 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
480 atomic_t left_eblks; /* Blocks left for erasing */
481 atomic_t left_seblks; /* Blocks left for sync erasing */
483 int left_msecs; /* Sectors left for mapping */
484 unsigned int cur_sec; /* Sector map pointer */
485 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
487 __le32 *vsc; /* Valid sector count in line */
489 struct kref ref; /* Write buffer L2P references */
490 atomic_t sec_to_update; /* Outstanding L2P updates to ppa */
492 struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
494 spinlock_t lock; /* Necessary for invalid_bitmap only */
497 #define PBLK_DATA_LINES 4
500 PBLK_KMALLOC_META = 1,
501 PBLK_VMALLOC_META = 2,
505 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
506 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
507 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
510 struct pblk_line_mgmt {
511 int nr_lines; /* Total number of full lines */
512 int nr_free_lines; /* Number of full lines in free list */
514 /* Free lists - use free_lock */
515 struct list_head free_list; /* Full lines ready to use */
516 struct list_head corrupt_list; /* Full lines corrupted */
517 struct list_head bad_list; /* Full lines bad */
519 /* GC lists - use gc_lock */
520 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
521 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
522 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
523 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
525 struct list_head gc_werr_list; /* Write err recovery list */
527 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
528 struct list_head gc_empty_list; /* Full lines close, all valid */
530 struct pblk_line *log_line; /* Current FTL log line */
531 struct pblk_line *data_line; /* Current data line */
532 struct pblk_line *log_next; /* Next FTL log line */
533 struct pblk_line *data_next; /* Next data line */
535 struct list_head emeta_list; /* Lines queued to schedule emeta */
537 __le32 *vsc_list; /* Valid sector counts for all lines */
539 /* Metadata allocation type: VMALLOC | KMALLOC */
540 int emeta_alloc_type;
542 /* Pre-allocated metadata for data lines */
543 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
544 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
545 unsigned long meta_bitmap;
547 /* Cache and mempool for map/invalid bitmaps */
548 struct kmem_cache *bitmap_cache;
549 mempool_t *bitmap_pool;
551 /* Helpers for fast bitmap calculations */
552 unsigned long *bb_template;
553 unsigned long *bb_aux;
555 unsigned long d_seq_nr; /* Data line unique sequence number */
556 unsigned long l_seq_nr; /* Log line unique sequence number */
558 spinlock_t free_lock;
559 spinlock_t close_lock;
563 struct pblk_line_meta {
564 unsigned int smeta_len; /* Total length for smeta */
565 unsigned int smeta_sec; /* Sectors needed for smeta */
567 unsigned int emeta_len[4]; /* Lengths for emeta:
569 * [1]: struct line_emeta +
570 * bb_bitmap + struct wa_counters
574 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
578 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
580 unsigned int vsc_list_len; /* Length for vsc list */
581 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
582 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
583 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
585 unsigned int blk_per_line; /* Number of blocks in a full line */
586 unsigned int sec_per_line; /* Number of sectors in a line */
587 unsigned int dsec_per_line; /* Number of data sectors in a line */
588 unsigned int min_blk_line; /* Min. number of good blocks in line */
590 unsigned int mid_thrs; /* Threshold for GC mid list */
591 unsigned int high_thrs; /* Threshold for GC high list */
593 unsigned int meta_distance; /* Distance between data and metadata */
597 PBLK_STATE_RUNNING = 0,
598 PBLK_STATE_STOPPING = 1,
599 PBLK_STATE_RECOVERING = 2,
600 PBLK_STATE_STOPPED = 3,
603 /* Internal format to support not power-of-2 device formats */
616 struct nvm_tgt_dev *dev;
617 struct gendisk *disk;
621 struct pblk_lun *luns;
623 struct pblk_line *lines; /* Line array */
624 struct pblk_line_mgmt l_mg; /* Line management */
625 struct pblk_line_meta lm; /* Line metadata */
627 struct nvm_addrf addrf; /* Aligned address format */
628 struct pblk_addrf uaddrf; /* Unaligned address format */
633 int state; /* pblk line state */
635 int min_write_pgs; /* Minimum amount of pages required by controller */
636 int min_write_pgs_data; /* Minimum amount of payload pages */
637 int max_write_pgs; /* Maximum amount of pages supported by controller */
638 int oob_meta_size; /* Size of OOB sector metadata */
640 sector_t capacity; /* Device capacity when bad blocks are subtracted */
642 int op; /* Percentage of device used for over-provisioning */
643 int op_blks; /* Number of blocks used for over-provisioning */
645 /* pblk provisioning values. Used by rate limiter */
650 guid_t instance_uuid;
652 /* Persistent write amplification counters, 4kb sector I/Os */
653 atomic64_t user_wa; /* Sectors written by user */
654 atomic64_t gc_wa; /* Sectors written by GC */
655 atomic64_t pad_wa; /* Padded sectors written */
657 /* Reset values for delta write amplification measurements */
662 /* Counters used for calculating padding distribution */
663 atomic64_t *pad_dist; /* Padding distribution buckets */
664 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
665 atomic64_t nr_flush; /* Number of flush/fua I/O */
667 #ifdef CONFIG_NVM_PBLK_DEBUG
668 /* Non-persistent debug counters, 4kb sector I/Os */
669 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
670 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
671 atomic_long_t padded_wb; /* Sectors padded in write buffer */
672 atomic_long_t req_writes; /* Sectors stored on write buffer */
673 atomic_long_t sub_writes; /* Sectors submitted from buffer */
674 atomic_long_t sync_writes; /* Sectors synced to media */
675 atomic_long_t inflight_reads; /* Inflight sector read requests */
676 atomic_long_t cache_reads; /* Read requests that hit the cache */
677 atomic_long_t sync_reads; /* Completed sector read requests */
678 atomic_long_t recov_writes; /* Sectors submitted from recovery */
679 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
680 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
685 atomic_long_t read_failed;
686 atomic_long_t read_empty;
687 atomic_long_t read_high_ecc;
688 atomic_long_t read_failed_gc;
689 atomic_long_t write_failed;
690 atomic_long_t erase_failed;
692 atomic_t inflight_io; /* General inflight I/O counter */
694 struct task_struct *writer_ts;
696 /* Simple translation map of logical addresses to physical addresses.
697 * The logical addresses is known by the host system, while the physical
698 * addresses are used when writing to the disk block device.
700 unsigned char *trans_map;
701 spinlock_t trans_lock;
703 struct list_head compl_list;
705 spinlock_t resubmit_lock; /* Resubmit list lock */
706 struct list_head resubmit_list; /* Resubmit list for failed writes*/
708 mempool_t page_bio_pool;
709 mempool_t gen_ws_pool;
715 struct workqueue_struct *close_wq;
716 struct workqueue_struct *bb_wq;
717 struct workqueue_struct *r_end_wq;
719 struct timer_list wtimer;
724 struct pblk_line_ws {
726 struct pblk_line *line;
728 struct work_struct ws;
731 #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
732 #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
734 #define pblk_err(pblk, fmt, ...) \
735 pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
736 #define pblk_info(pblk, fmt, ...) \
737 pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
738 #define pblk_warn(pblk, fmt, ...) \
739 pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
740 #define pblk_debug(pblk, fmt, ...) \
741 pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
744 * pblk ring buffer operations
746 int pblk_rb_init(struct pblk_rb *rb, unsigned int size, unsigned int threshold,
747 unsigned int seg_sz);
748 int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
749 unsigned int nr_entries, unsigned int *pos);
750 int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
752 void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
753 struct pblk_w_ctx w_ctx, unsigned int pos);
754 void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
755 struct pblk_w_ctx w_ctx, struct pblk_line *line,
756 u64 paddr, unsigned int pos);
757 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
758 void pblk_rb_flush(struct pblk_rb *rb);
760 void pblk_rb_sync_l2p(struct pblk_rb *rb);
761 unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
762 unsigned int pos, unsigned int nr_entries,
764 int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
765 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
766 unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
768 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
769 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
770 unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p,
771 unsigned int nr_entries);
772 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
773 unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
775 unsigned int pblk_rb_read_count(struct pblk_rb *rb);
776 unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
777 unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
779 int pblk_rb_tear_down_check(struct pblk_rb *rb);
780 int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
781 void pblk_rb_free(struct pblk_rb *rb);
782 ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
787 struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
788 void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
789 int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
790 void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
791 void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
792 int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
793 struct pblk_c_ctx *c_ctx);
794 void pblk_discard(struct pblk *pblk, struct bio *bio);
795 struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk);
796 struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
797 struct nvm_chk_meta *lp,
798 struct ppa_addr ppa);
799 void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
800 void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
801 int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
802 int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
803 int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd);
804 int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
805 void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd);
806 struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
807 unsigned int nr_secs, unsigned int len,
808 int alloc_type, gfp_t gfp_mask);
809 struct pblk_line *pblk_line_get(struct pblk *pblk);
810 struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
811 struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
812 void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa);
813 void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd);
814 int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
815 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
816 struct pblk_line *pblk_line_get_data(struct pblk *pblk);
817 struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
818 int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
819 int pblk_line_is_full(struct pblk_line *line);
820 void pblk_line_free(struct pblk_line *line);
821 void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
822 void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
823 void pblk_line_close_ws(struct work_struct *work);
824 void pblk_pipeline_stop(struct pblk *pblk);
825 void __pblk_pipeline_stop(struct pblk *pblk);
826 void __pblk_pipeline_flush(struct pblk *pblk);
827 void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
828 void (*work)(struct work_struct *), gfp_t gfp_mask,
829 struct workqueue_struct *wq);
830 u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
831 int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line);
832 int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
834 int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
835 void pblk_line_put(struct kref *ref);
836 void pblk_line_put_wq(struct kref *ref);
837 struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
838 u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
839 void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
840 u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
841 u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
842 int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
843 unsigned long secs_to_flush, bool skip_meta);
844 void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
845 unsigned long *lun_bitmap);
846 void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa);
847 void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa);
848 void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap);
849 int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
851 void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
853 void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
854 void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
856 void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
857 void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
858 struct ppa_addr ppa);
859 void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
860 struct ppa_addr ppa, struct ppa_addr entry_line);
861 int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
862 struct pblk_line *gc_line, u64 paddr);
863 void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
864 u64 *lba_list, int nr_secs);
865 void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
866 sector_t blba, int nr_secs);
867 void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd);
868 void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd);
871 * pblk user I/O write path
873 int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
874 unsigned long flags);
875 int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
880 int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
881 unsigned int sentry, unsigned long *lun_bitmap,
882 unsigned int valid_secs, struct ppa_addr *erase_ppa);
883 int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
884 unsigned long *lun_bitmap, unsigned int valid_secs,
890 int pblk_write_ts(void *data);
891 void pblk_write_timer_fn(struct timer_list *t);
892 void pblk_write_should_kick(struct pblk *pblk);
893 void pblk_write_kick(struct pblk *pblk);
898 extern struct bio_set pblk_bio_set;
899 int pblk_submit_read(struct pblk *pblk, struct bio *bio);
900 int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
904 struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
905 int pblk_recov_pad(struct pblk *pblk);
906 int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
911 #define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
912 #define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
913 #define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
915 int pblk_gc_init(struct pblk *pblk);
916 void pblk_gc_exit(struct pblk *pblk, bool graceful);
917 void pblk_gc_should_start(struct pblk *pblk);
918 void pblk_gc_should_stop(struct pblk *pblk);
919 void pblk_gc_should_kick(struct pblk *pblk);
920 void pblk_gc_free_full_lines(struct pblk *pblk);
921 void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
923 int pblk_gc_sysfs_force(struct pblk *pblk, int force);
928 void pblk_rl_init(struct pblk_rl *rl, int budget, int threshold);
929 void pblk_rl_free(struct pblk_rl *rl);
930 void pblk_rl_update_rates(struct pblk_rl *rl);
931 int pblk_rl_high_thrs(struct pblk_rl *rl);
932 unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
933 unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
934 int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
935 void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
936 void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
937 int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
938 void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
939 void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
940 int pblk_rl_max_io(struct pblk_rl *rl);
941 void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
942 void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
944 int pblk_rl_is_limit(struct pblk_rl *rl);
946 void pblk_rl_werr_line_in(struct pblk_rl *rl);
947 void pblk_rl_werr_line_out(struct pblk_rl *rl);
952 int pblk_sysfs_init(struct gendisk *tdisk);
953 void pblk_sysfs_exit(struct gendisk *tdisk);
955 static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
957 if (type == PBLK_KMALLOC_META)
958 return kmalloc(size, flags);
959 return vmalloc(size);
962 static inline void pblk_mfree(void *ptr, int type)
964 if (type == PBLK_KMALLOC_META)
970 static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
972 return c_ctx - sizeof(struct nvm_rq);
975 static inline void *emeta_to_bb(struct line_emeta *emeta)
977 return emeta->bb_bitmap;
980 static inline void *emeta_to_wa(struct pblk_line_meta *lm,
981 struct line_emeta *emeta)
983 return emeta->bb_bitmap + lm->blk_bitmap_len;
986 static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
988 return ((void *)emeta + pblk->lm.emeta_len[1]);
991 static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
993 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
996 static inline int pblk_line_vsc(struct pblk_line *line)
998 return le32_to_cpu(*line->vsc);
1001 static inline int pblk_ppa_to_line_id(struct ppa_addr p)
1006 static inline struct pblk_line *pblk_ppa_to_line(struct pblk *pblk,
1009 return &pblk->lines[pblk_ppa_to_line_id(p)];
1012 static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
1014 return p.a.lun * geo->num_ch + p.a.ch;
1017 static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1020 struct nvm_tgt_dev *dev = pblk->dev;
1021 struct nvm_geo *geo = &dev->geo;
1022 struct ppa_addr ppa;
1024 if (geo->version == NVM_OCSSD_SPEC_12) {
1025 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1028 ppa.g.blk = line_id;
1029 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
1030 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
1031 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
1032 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
1033 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
1035 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1036 int secs, chnls, luns;
1040 ppa.m.chk = line_id;
1042 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
1045 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
1048 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
1051 ppa.m.sec += uaddrf->sec_stripe * paddr;
1057 static inline struct nvm_chk_meta *pblk_dev_ppa_to_chunk(struct pblk *pblk,
1060 struct nvm_tgt_dev *dev = pblk->dev;
1061 struct nvm_geo *geo = &dev->geo;
1062 struct pblk_line *line = pblk_ppa_to_line(pblk, p);
1063 int pos = pblk_ppa_to_pos(geo, p);
1065 return &line->chks[pos];
1068 static inline u64 pblk_dev_ppa_to_chunk_addr(struct pblk *pblk,
1071 struct nvm_tgt_dev *dev = pblk->dev;
1073 return dev_to_chunk_addr(dev->parent, &pblk->addrf, p);
1076 static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1079 struct nvm_tgt_dev *dev = pblk->dev;
1080 struct nvm_geo *geo = &dev->geo;
1083 if (geo->version == NVM_OCSSD_SPEC_12) {
1084 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1086 paddr = (u64)p.g.ch << ppaf->ch_offset;
1087 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1088 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1089 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1090 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1092 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1096 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1097 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1099 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1100 paddr += secs * uaddrf->sec_ws_stripe;
1101 paddr += sec_stripe;
1107 static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1109 struct nvm_tgt_dev *dev = pblk->dev;
1111 return nvm_ppa32_to_ppa64(dev->parent, &pblk->addrf, ppa32);
1114 static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1116 struct nvm_tgt_dev *dev = pblk->dev;
1118 return nvm_ppa64_to_ppa32(dev->parent, &pblk->addrf, ppa64);
1121 static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1124 struct ppa_addr ppa;
1126 if (pblk->addrf_len < 32) {
1127 u32 *map = (u32 *)pblk->trans_map;
1129 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1131 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1139 static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1140 struct ppa_addr ppa)
1142 if (pblk->addrf_len < 32) {
1143 u32 *map = (u32 *)pblk->trans_map;
1145 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1147 u64 *map = (u64 *)pblk->trans_map;
1153 static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1155 return (ppa_addr.ppa == ADDR_EMPTY);
1158 static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1160 ppa_addr->ppa = ADDR_EMPTY;
1163 static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1165 return (lppa.ppa == rppa.ppa);
1168 static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1170 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1173 static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1178 static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1188 static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
1189 struct line_header *header)
1193 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
1194 sizeof(struct line_header) - sizeof(crc));
1199 static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1200 struct line_smeta *smeta)
1202 struct pblk_line_meta *lm = &pblk->lm;
1205 crc = crc32_le(crc, (unsigned char *)smeta +
1206 sizeof(struct line_header) + sizeof(crc),
1208 sizeof(struct line_header) - sizeof(crc));
1213 static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1214 struct line_emeta *emeta)
1216 struct pblk_line_meta *lm = &pblk->lm;
1219 crc = crc32_le(crc, (unsigned char *)emeta +
1220 sizeof(struct line_header) + sizeof(crc),
1222 sizeof(struct line_header) - sizeof(crc));
1227 static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1229 return !(nr_secs % pblk->min_write_pgs);
1232 #ifdef CONFIG_NVM_PBLK_DEBUG
1233 static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p,
1234 char *msg, int error)
1236 struct nvm_geo *geo = &pblk->dev->geo;
1238 if (p->c.is_cached) {
1239 pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n",
1240 msg, error, (u64)p->c.line);
1241 } else if (geo->version == NVM_OCSSD_SPEC_12) {
1242 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1244 p->g.ch, p->g.lun, p->g.blk,
1245 p->g.pg, p->g.pl, p->g.sec);
1247 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
1249 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
1253 static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1258 if (rqd->nr_ppas == 1) {
1259 print_ppa(pblk, &rqd->ppa_addr, "rqd", error);
1263 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1264 bit + 1)) < rqd->nr_ppas) {
1265 print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error);
1268 pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1271 static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1272 struct ppa_addr *ppas, int nr_ppas)
1274 struct nvm_geo *geo = &tgt_dev->geo;
1275 struct ppa_addr *ppa;
1278 for (i = 0; i < nr_ppas; i++) {
1281 if (geo->version == NVM_OCSSD_SPEC_12) {
1282 if (!ppa->c.is_cached &&
1283 ppa->g.ch < geo->num_ch &&
1284 ppa->g.lun < geo->num_lun &&
1285 ppa->g.pl < geo->num_pln &&
1286 ppa->g.blk < geo->num_chk &&
1287 ppa->g.pg < geo->num_pg &&
1288 ppa->g.sec < geo->ws_min)
1291 if (!ppa->c.is_cached &&
1292 ppa->m.grp < geo->num_ch &&
1293 ppa->m.pu < geo->num_lun &&
1294 ppa->m.chk < geo->num_chk &&
1295 ppa->m.sec < geo->clba)
1299 print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i);
1306 static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1308 struct nvm_tgt_dev *dev = pblk->dev;
1309 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
1311 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1316 if (rqd->opcode == NVM_OP_PWRITE) {
1317 struct pblk_line *line;
1320 for (i = 0; i < rqd->nr_ppas; i++) {
1321 line = pblk_ppa_to_line(pblk, ppa_list[i]);
1323 spin_lock(&line->lock);
1324 if (line->state != PBLK_LINESTATE_OPEN) {
1325 pblk_err(pblk, "bad ppa: line:%d,state:%d\n",
1326 line->id, line->state);
1328 spin_unlock(&line->lock);
1331 spin_unlock(&line->lock);
1339 static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1341 struct pblk_line_meta *lm = &pblk->lm;
1343 if (paddr > lm->sec_per_line)
1349 static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1351 return bio->bi_iter.bi_idx;
1354 static inline sector_t pblk_get_lba(struct bio *bio)
1356 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1359 static inline unsigned int pblk_get_secs(struct bio *bio)
1361 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1364 static inline char *pblk_disk_name(struct pblk *pblk)
1366 struct gendisk *disk = pblk->disk;
1368 return disk->disk_name;
1371 static inline unsigned int pblk_get_min_chks(struct pblk *pblk)
1373 struct pblk_line_meta *lm = &pblk->lm;
1374 /* In a worst-case scenario every line will have OP invalid sectors.
1375 * We will then need a minimum of 1/OP lines to free up a single line
1378 return DIV_ROUND_UP(100, pblk->op) * lm->blk_per_line;
1381 static inline struct pblk_sec_meta *pblk_get_meta(struct pblk *pblk,
1382 void *meta, int index)
1385 max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1389 static inline int pblk_dma_meta_size(struct pblk *pblk)
1391 return max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1395 static inline int pblk_is_oob_meta_supported(struct pblk *pblk)
1397 return pblk->oob_meta_size >= sizeof(struct pblk_sec_meta);
1399 #endif /* PBLK_H_ */