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_NR_CLOSE_JOBS (4)
45 #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
47 #define PBLK_COMMAND_TIMEOUT_MS 30000
49 /* Max 512 LUNs per device */
50 #define PBLK_MAX_LUNS_BITMAP (4)
52 #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
54 #define pblk_for_each_lun(pblk, rlun, i) \
55 for ((i) = 0, rlun = &(pblk)->luns[0]; \
56 (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)])
58 /* Static pool sizes */
59 #define PBLK_GEN_WS_POOL_SIZE (2)
63 PBLK_WRITE
= WRITE
,/* Write from write buffer */
64 PBLK_WRITE_INT
, /* Internal write - no write buffer */
70 PBLK_IOTYPE_USER
= 1 << 0,
71 PBLK_IOTYPE_GC
= 1 << 1,
73 /* Write buffer flags */
74 PBLK_FLUSH_ENTRY
= 1 << 2,
75 PBLK_WRITTEN_DATA
= 1 << 3,
76 PBLK_SUBMITTED_ENTRY
= 1 << 4,
77 PBLK_WRITABLE_ENTRY
= 1 << 5,
81 PBLK_BLK_ST_OPEN
= 0x1,
82 PBLK_BLK_ST_CLOSED
= 0x2,
85 struct pblk_sec_meta
{
90 /* The number of GC lists and the rate-limiter states go together. This way the
91 * rate-limiter can dictate how much GC is needed based on resource utilization.
93 #define PBLK_GC_NR_LISTS 3
101 #define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
102 #define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS)
104 /* write buffer completion context */
106 struct list_head list
; /* Head for out-of-order completion */
108 unsigned long *lun_bitmap
; /* Luns used on current request */
110 unsigned int nr_valid
;
111 unsigned int nr_padded
;
123 struct completion wait
;
127 /* Recovery context */
128 struct pblk_rec_ctx
{
131 struct list_head failed
;
132 struct work_struct ws_rec
;
137 struct bio_list bios
; /* Original bios - used for completion
138 * in REQ_FUA, REQ_FLUSH case
140 u64 lba
; /* Logic addr. associated with entry */
141 struct ppa_addr ppa
; /* Physic addr. associated with entry */
142 int flags
; /* Write context flags */
145 struct pblk_rb_entry
{
146 struct ppa_addr cacheline
; /* Cacheline for this entry */
147 void *data
; /* Pointer to data on this entry */
148 struct pblk_w_ctx w_ctx
; /* Context for this entry */
149 struct list_head index
; /* List head to enable indexes */
152 #define EMPTY_ENTRY (~0U)
154 struct pblk_rb_pages
{
157 struct list_head list
;
161 struct pblk_rb_entry
*entries
; /* Ring buffer entries */
162 unsigned int mem
; /* Write offset - points to next
163 * writable entry in memory
165 unsigned int subm
; /* Read offset - points to last entry
166 * that has been submitted to the media
169 unsigned int sync
; /* Synced - backpointer that signals
170 * the last submitted entry that has
171 * been successfully persisted to media
173 unsigned int sync_point
; /* Sync point - last entry that must be
174 * flushed to the media. Used with
175 * REQ_FLUSH and REQ_FUA
177 unsigned int l2p_update
; /* l2p update point - next entry for
178 * which l2p mapping will be updated to
179 * contain a device ppa address (instead
182 unsigned int nr_entries
; /* Number of entries in write buffer -
183 * must be a power of two
185 unsigned int seg_size
; /* Size of the data segments being
186 * stored on each entry. Typically this
190 struct list_head pages
; /* List of data pages */
192 spinlock_t w_lock
; /* Write lock */
193 spinlock_t s_lock
; /* Sync lock */
195 #ifdef CONFIG_NVM_DEBUG
196 atomic_t inflight_sync_point
; /* Not served REQ_FLUSH | REQ_FUA */
200 #define PBLK_RECOVERY_SECTORS 16
203 struct ppa_addr bppa
;
205 u8
*bb_list
; /* Bad block list for LUN. Only used on
206 * bring up. Bad blocks are managed
207 * within lines on run-time.
210 struct semaphore wr_sem
;
214 struct pblk_line
*line
;
216 u64 paddr_list
[PBLK_MAX_REQ_ADDRS
];
217 u64 lba_list
[PBLK_MAX_REQ_ADDRS
];
220 struct list_head list
;
224 /* These states are not protected by a lock since (i) they are in the
225 * fast path, and (ii) they are not critical.
231 struct task_struct
*gc_ts
;
232 struct task_struct
*gc_writer_ts
;
233 struct task_struct
*gc_reader_ts
;
235 struct workqueue_struct
*gc_line_reader_wq
;
236 struct workqueue_struct
*gc_reader_wq
;
238 struct timer_list gc_timer
;
240 struct semaphore gc_sem
;
241 atomic_t read_inflight_gc
; /* Number of lines with inflight GC reads */
242 atomic_t pipeline_gc
; /* Number of lines in the GC pipeline -
243 * started reads to finished writes
247 struct list_head w_list
;
248 struct list_head r_list
;
256 unsigned int high
; /* Upper threshold for rate limiter (free run -
257 * user I/O rate limiter
259 unsigned int low
; /* Lower threshold for rate limiter (user I/O
260 * rate limiter - stall)
262 unsigned int high_pw
; /* High rounded up as a power of 2 */
264 #define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
265 #define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
267 int rb_windows_pw
; /* Number of rate windows in the write buffer
268 * given as a power-of-2. This guarantees that
269 * when user I/O is being rate limited, there
270 * will be reserved enough space for the GC to
271 * place its payload. A window is of
272 * pblk->max_write_pgs size, which in NVMe is
275 int rb_budget
; /* Total number of entries available for I/O */
276 int rb_user_max
; /* Max buffer entries available for user I/O */
277 int rb_gc_max
; /* Max buffer entries available for GC I/O */
278 int rb_gc_rsv
; /* Reserved buffer entries for GC I/O */
279 int rb_state
; /* Rate-limiter current state */
280 int rb_max_io
; /* Maximum size for an I/O giving the config */
282 atomic_t rb_user_cnt
; /* User I/O buffer counter */
283 atomic_t rb_gc_cnt
; /* GC I/O buffer counter */
284 atomic_t rb_space
; /* Space limit in case of reaching capacity */
286 int rsv_blocks
; /* Reserved blocks for GC */
291 struct timer_list u_timer
;
293 unsigned long long nr_secs
;
294 unsigned long total_blocks
;
295 atomic_t free_blocks
;
298 #define PBLK_LINE_EMPTY (~0U)
302 PBLK_LINETYPE_FREE
= 0,
303 PBLK_LINETYPE_LOG
= 1,
304 PBLK_LINETYPE_DATA
= 2,
307 PBLK_LINESTATE_FREE
= 10,
308 PBLK_LINESTATE_OPEN
= 11,
309 PBLK_LINESTATE_CLOSED
= 12,
310 PBLK_LINESTATE_GC
= 13,
311 PBLK_LINESTATE_BAD
= 14,
312 PBLK_LINESTATE_CORRUPT
= 15,
315 PBLK_LINEGC_NONE
= 20,
316 PBLK_LINEGC_EMPTY
= 21,
317 PBLK_LINEGC_LOW
= 22,
318 PBLK_LINEGC_MID
= 23,
319 PBLK_LINEGC_HIGH
= 24,
320 PBLK_LINEGC_FULL
= 25,
323 #define PBLK_MAGIC 0x70626c6b /*pblk*/
324 #define SMETA_VERSION cpu_to_le16(1)
328 __le32 identifier
; /* pblk identifier */
329 __u8 uuid
[16]; /* instance uuid */
330 __le16 type
; /* line type */
331 __le16 version
; /* type version */
332 __le32 id
; /* line id for current line */
336 struct line_header header
;
338 __le32 crc
; /* Full structure including struct crc */
339 /* Previous line metadata */
340 __le32 prev_id
; /* Line id for previous line */
342 /* Current line metadata */
343 __le64 seq_nr
; /* Sequence number for current line */
346 __le32 window_wr_lun
; /* Number of parallel LUNs to write */
354 * Metadata layout in media:
356 * 1. struct line_emeta
357 * 2. bad block bitmap (u64 * window_wr_lun)
358 * Mid sectors (start at lbas_sector):
359 * 3. nr_lbas (u64) forming lba list
360 * Last sectors (start at vsc_sector):
361 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
364 struct line_header header
;
366 __le32 crc
; /* Full structure including struct crc */
368 /* Previous line metadata */
369 __le32 prev_id
; /* Line id for prev line */
371 /* Current line metadata */
372 __le64 seq_nr
; /* Sequence number for current line */
375 __le32 window_wr_lun
; /* Number of parallel LUNs to write */
377 /* Bookkeeping for recovery */
378 __le32 next_id
; /* Line id for next line */
379 __le64 nr_lbas
; /* Number of lbas mapped in line */
380 __le64 nr_valid_lbas
; /* Number of valid lbas mapped in line */
381 __le64 bb_bitmap
[]; /* Updated bad block bitmap for line */
385 struct line_emeta
*buf
; /* emeta buffer in media format */
386 int mem
; /* Write offset - points to next
387 * writable entry in memory
389 atomic_t sync
; /* Synced - backpointer that signals the
390 * last entry that has been successfully
393 unsigned int nr_entries
; /* Number of emeta entries */
397 struct line_smeta
*buf
; /* smeta buffer in persistent format */
402 unsigned int id
; /* Line number corresponds to the
405 unsigned int seq_nr
; /* Unique line sequence number */
407 int state
; /* PBLK_LINESTATE_X */
408 int type
; /* PBLK_LINETYPE_X */
409 int gc_group
; /* PBLK_LINEGC_X */
410 struct list_head list
; /* Free, GC lists */
412 unsigned long *lun_bitmap
; /* Bitmap for LUNs mapped in line */
414 struct pblk_smeta
*smeta
; /* Start metadata */
415 struct pblk_emeta
*emeta
; /* End medatada */
417 int meta_line
; /* Metadata line id */
418 int meta_distance
; /* Distance between data and metadata */
420 u64 smeta_ssec
; /* Sector where smeta starts */
421 u64 emeta_ssec
; /* Sector where emeta starts */
423 unsigned int sec_in_line
; /* Number of usable secs in line */
425 atomic_t blk_in_line
; /* Number of good blocks in line */
426 unsigned long *blk_bitmap
; /* Bitmap for valid/invalid blocks */
427 unsigned long *erase_bitmap
; /* Bitmap for erased blocks */
429 unsigned long *map_bitmap
; /* Bitmap for mapped sectors in line */
430 unsigned long *invalid_bitmap
; /* Bitmap for invalid sectors in line */
432 atomic_t left_eblks
; /* Blocks left for erasing */
433 atomic_t left_seblks
; /* Blocks left for sync erasing */
435 int left_msecs
; /* Sectors left for mapping */
436 unsigned int cur_sec
; /* Sector map pointer */
437 unsigned int nr_valid_lbas
; /* Number of valid lbas in line */
439 __le32
*vsc
; /* Valid sector count in line */
441 struct kref ref
; /* Write buffer L2P references */
443 spinlock_t lock
; /* Necessary for invalid_bitmap only */
446 #define PBLK_DATA_LINES 4
449 PBLK_KMALLOC_META
= 1,
450 PBLK_VMALLOC_META
= 2,
454 PBLK_EMETA_TYPE_HEADER
= 1, /* struct line_emeta first sector */
455 PBLK_EMETA_TYPE_LLBA
= 2, /* lba list - type: __le64 */
456 PBLK_EMETA_TYPE_VSC
= 3, /* vsc list - type: __le32 */
459 struct pblk_line_mgmt
{
460 int nr_lines
; /* Total number of full lines */
461 int nr_free_lines
; /* Number of full lines in free list */
463 /* Free lists - use free_lock */
464 struct list_head free_list
; /* Full lines ready to use */
465 struct list_head corrupt_list
; /* Full lines corrupted */
466 struct list_head bad_list
; /* Full lines bad */
468 /* GC lists - use gc_lock */
469 struct list_head
*gc_lists
[PBLK_GC_NR_LISTS
];
470 struct list_head gc_high_list
; /* Full lines ready to GC, high isc */
471 struct list_head gc_mid_list
; /* Full lines ready to GC, mid isc */
472 struct list_head gc_low_list
; /* Full lines ready to GC, low isc */
474 struct list_head gc_full_list
; /* Full lines ready to GC, no valid */
475 struct list_head gc_empty_list
; /* Full lines close, all valid */
477 struct pblk_line
*log_line
; /* Current FTL log line */
478 struct pblk_line
*data_line
; /* Current data line */
479 struct pblk_line
*log_next
; /* Next FTL log line */
480 struct pblk_line
*data_next
; /* Next data line */
482 struct list_head emeta_list
; /* Lines queued to schedule emeta */
484 __le32
*vsc_list
; /* Valid sector counts for all lines */
486 /* Metadata allocation type: VMALLOC | KMALLOC */
487 int emeta_alloc_type
;
489 /* Pre-allocated metadata for data lines */
490 struct pblk_smeta
*sline_meta
[PBLK_DATA_LINES
];
491 struct pblk_emeta
*eline_meta
[PBLK_DATA_LINES
];
492 unsigned long meta_bitmap
;
494 /* Helpers for fast bitmap calculations */
495 unsigned long *bb_template
;
496 unsigned long *bb_aux
;
498 unsigned long d_seq_nr
; /* Data line unique sequence number */
499 unsigned long l_seq_nr
; /* Log line unique sequence number */
501 spinlock_t free_lock
;
502 spinlock_t close_lock
;
506 struct pblk_line_meta
{
507 unsigned int smeta_len
; /* Total length for smeta */
508 unsigned int smeta_sec
; /* Sectors needed for smeta */
510 unsigned int emeta_len
[4]; /* Lengths for emeta:
512 * [1]: struct line_emeta length
513 * [2]: L2P portion length
514 * [3]: vsc list length
516 unsigned int emeta_sec
[4]; /* Sectors needed for emeta. Same layout
520 unsigned int emeta_bb
; /* Boundary for bb that affects emeta */
522 unsigned int vsc_list_len
; /* Length for vsc list */
523 unsigned int sec_bitmap_len
; /* Length for sector bitmap in line */
524 unsigned int blk_bitmap_len
; /* Length for block bitmap in line */
525 unsigned int lun_bitmap_len
; /* Length for lun bitmap in line */
527 unsigned int blk_per_line
; /* Number of blocks in a full line */
528 unsigned int sec_per_line
; /* Number of sectors in a line */
529 unsigned int dsec_per_line
; /* Number of data sectors in a line */
530 unsigned int min_blk_line
; /* Min. number of good blocks in line */
532 unsigned int mid_thrs
; /* Threshold for GC mid list */
533 unsigned int high_thrs
; /* Threshold for GC high list */
535 unsigned int meta_distance
; /* Distance between data and metadata */
538 struct pblk_addr_format
{
554 PBLK_STATE_RUNNING
= 0,
555 PBLK_STATE_STOPPING
= 1,
556 PBLK_STATE_RECOVERING
= 2,
557 PBLK_STATE_STOPPED
= 3,
561 struct nvm_tgt_dev
*dev
;
562 struct gendisk
*disk
;
566 struct pblk_lun
*luns
;
568 struct pblk_line
*lines
; /* Line array */
569 struct pblk_line_mgmt l_mg
; /* Line management */
570 struct pblk_line_meta lm
; /* Line metadata */
573 struct pblk_addr_format ppaf
;
577 int state
; /* pblk line state */
579 int min_write_pgs
; /* Minimum amount of pages required by controller */
580 int max_write_pgs
; /* Maximum amount of pages supported by controller */
581 int pgs_in_buffer
; /* Number of pages that need to be held in buffer to
582 * guarantee successful reads.
585 sector_t capacity
; /* Device capacity when bad blocks are subtracted */
586 int over_pct
; /* Percentage of device used for over-provisioning */
588 /* pblk provisioning values. Used by rate limiter */
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 */
603 atomic_long_t inflight_reads
; /* Inflight sector read requests */
604 atomic_long_t cache_reads
; /* Read requests that hit the cache */
605 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 */
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
;
620 atomic_t inflight_io
; /* General inflight I/O counter */
622 struct task_struct
*writer_ts
;
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.
628 unsigned char *trans_map
;
629 spinlock_t trans_lock
;
631 struct list_head compl_list
;
633 mempool_t
*page_bio_pool
;
634 mempool_t
*gen_ws_pool
;
636 mempool_t
*r_rq_pool
;
637 mempool_t
*w_rq_pool
;
638 mempool_t
*e_rq_pool
;
640 struct workqueue_struct
*close_wq
;
641 struct workqueue_struct
*bb_wq
;
642 struct workqueue_struct
*r_end_wq
;
644 struct timer_list wtimer
;
649 struct pblk_line_ws
{
651 struct pblk_line
*line
;
653 struct work_struct ws
;
656 #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
657 #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
660 * pblk ring buffer operations
662 int pblk_rb_init(struct pblk_rb
*rb
, struct pblk_rb_entry
*rb_entry_base
,
663 unsigned int power_size
, unsigned int power_seg_sz
);
664 unsigned int pblk_rb_calculate_size(unsigned int nr_entries
);
665 void *pblk_rb_entries_ref(struct pblk_rb
*rb
);
666 int pblk_rb_may_write_user(struct pblk_rb
*rb
, struct bio
*bio
,
667 unsigned int nr_entries
, unsigned int *pos
);
668 int pblk_rb_may_write_gc(struct pblk_rb
*rb
, unsigned int nr_entries
,
670 void pblk_rb_write_entry_user(struct pblk_rb
*rb
, void *data
,
671 struct pblk_w_ctx w_ctx
, unsigned int pos
);
672 void pblk_rb_write_entry_gc(struct pblk_rb
*rb
, void *data
,
673 struct pblk_w_ctx w_ctx
, struct pblk_line
*line
,
674 u64 paddr
, unsigned int pos
);
675 struct pblk_w_ctx
*pblk_rb_w_ctx(struct pblk_rb
*rb
, unsigned int pos
);
676 void pblk_rb_flush(struct pblk_rb
*rb
);
678 void pblk_rb_sync_l2p(struct pblk_rb
*rb
);
679 unsigned int pblk_rb_read_to_bio(struct pblk_rb
*rb
, struct nvm_rq
*rqd
,
680 unsigned int pos
, unsigned int nr_entries
,
682 unsigned int pblk_rb_read_to_bio_list(struct pblk_rb
*rb
, struct bio
*bio
,
683 struct list_head
*list
,
685 int pblk_rb_copy_to_bio(struct pblk_rb
*rb
, struct bio
*bio
, sector_t lba
,
686 struct ppa_addr ppa
, int bio_iter
, bool advanced_bio
);
687 unsigned int pblk_rb_read_commit(struct pblk_rb
*rb
, unsigned int entries
);
689 unsigned int pblk_rb_sync_init(struct pblk_rb
*rb
, unsigned long *flags
);
690 unsigned int pblk_rb_sync_advance(struct pblk_rb
*rb
, unsigned int nr_entries
);
691 struct pblk_rb_entry
*pblk_rb_sync_scan_entry(struct pblk_rb
*rb
,
692 struct ppa_addr
*ppa
);
693 void pblk_rb_sync_end(struct pblk_rb
*rb
, unsigned long *flags
);
694 unsigned int pblk_rb_sync_point_count(struct pblk_rb
*rb
);
696 unsigned int pblk_rb_read_count(struct pblk_rb
*rb
);
697 unsigned int pblk_rb_sync_count(struct pblk_rb
*rb
);
698 unsigned int pblk_rb_wrap_pos(struct pblk_rb
*rb
, unsigned int pos
);
700 int pblk_rb_tear_down_check(struct pblk_rb
*rb
);
701 int pblk_rb_pos_oob(struct pblk_rb
*rb
, u64 pos
);
702 void pblk_rb_data_free(struct pblk_rb
*rb
);
703 ssize_t
pblk_rb_sysfs(struct pblk_rb
*rb
, char *buf
);
708 struct nvm_rq
*pblk_alloc_rqd(struct pblk
*pblk
, int type
);
709 void pblk_free_rqd(struct pblk
*pblk
, struct nvm_rq
*rqd
, int type
);
710 void pblk_set_sec_per_write(struct pblk
*pblk
, int sec_per_write
);
711 int pblk_setup_w_rec_rq(struct pblk
*pblk
, struct nvm_rq
*rqd
,
712 struct pblk_c_ctx
*c_ctx
);
713 void pblk_discard(struct pblk
*pblk
, struct bio
*bio
);
714 void pblk_log_write_err(struct pblk
*pblk
, struct nvm_rq
*rqd
);
715 void pblk_log_read_err(struct pblk
*pblk
, struct nvm_rq
*rqd
);
716 int pblk_submit_io(struct pblk
*pblk
, struct nvm_rq
*rqd
);
717 int pblk_submit_io_sync(struct pblk
*pblk
, struct nvm_rq
*rqd
);
718 int pblk_submit_meta_io(struct pblk
*pblk
, struct pblk_line
*meta_line
);
719 struct bio
*pblk_bio_map_addr(struct pblk
*pblk
, void *data
,
720 unsigned int nr_secs
, unsigned int len
,
721 int alloc_type
, gfp_t gfp_mask
);
722 struct pblk_line
*pblk_line_get(struct pblk
*pblk
);
723 struct pblk_line
*pblk_line_get_first_data(struct pblk
*pblk
);
724 struct pblk_line
*pblk_line_replace_data(struct pblk
*pblk
);
725 int pblk_line_recov_alloc(struct pblk
*pblk
, struct pblk_line
*line
);
726 void pblk_line_recov_close(struct pblk
*pblk
, struct pblk_line
*line
);
727 struct pblk_line
*pblk_line_get_data(struct pblk
*pblk
);
728 struct pblk_line
*pblk_line_get_erase(struct pblk
*pblk
);
729 int pblk_line_erase(struct pblk
*pblk
, struct pblk_line
*line
);
730 int pblk_line_is_full(struct pblk_line
*line
);
731 void pblk_line_free(struct pblk
*pblk
, struct pblk_line
*line
);
732 void pblk_line_close_meta(struct pblk
*pblk
, struct pblk_line
*line
);
733 void pblk_line_close(struct pblk
*pblk
, struct pblk_line
*line
);
734 void pblk_line_close_ws(struct work_struct
*work
);
735 void pblk_pipeline_stop(struct pblk
*pblk
);
736 void 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
);
739 u64
pblk_line_smeta_start(struct pblk
*pblk
, struct pblk_line
*line
);
740 int pblk_line_read_smeta(struct pblk
*pblk
, struct pblk_line
*line
);
741 int pblk_line_read_emeta(struct pblk
*pblk
, struct pblk_line
*line
,
743 int pblk_blk_erase_async(struct pblk
*pblk
, struct ppa_addr erase_ppa
);
744 void pblk_line_put(struct kref
*ref
);
745 void pblk_line_put_wq(struct kref
*ref
);
746 struct list_head
*pblk_line_gc_list(struct pblk
*pblk
, struct pblk_line
*line
);
747 u64
pblk_lookup_page(struct pblk
*pblk
, struct pblk_line
*line
);
748 void pblk_dealloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
);
749 u64
pblk_alloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
);
750 u64
__pblk_alloc_page(struct pblk
*pblk
, struct pblk_line
*line
, int nr_secs
);
751 int pblk_calc_secs(struct pblk
*pblk
, unsigned long secs_avail
,
752 unsigned long secs_to_flush
);
753 void pblk_up_page(struct pblk
*pblk
, struct ppa_addr
*ppa_list
, int nr_ppas
);
754 void pblk_down_rq(struct pblk
*pblk
, struct ppa_addr
*ppa_list
, int nr_ppas
,
755 unsigned long *lun_bitmap
);
756 void pblk_down_page(struct pblk
*pblk
, struct ppa_addr
*ppa_list
, int nr_ppas
);
757 void pblk_up_rq(struct pblk
*pblk
, struct ppa_addr
*ppa_list
, int nr_ppas
,
758 unsigned long *lun_bitmap
);
759 void pblk_end_io_sync(struct nvm_rq
*rqd
);
760 int pblk_bio_add_pages(struct pblk
*pblk
, struct bio
*bio
, gfp_t flags
,
762 void pblk_bio_free_pages(struct pblk
*pblk
, struct bio
*bio
, int off
,
764 void pblk_map_invalidate(struct pblk
*pblk
, struct ppa_addr ppa
);
765 void __pblk_map_invalidate(struct pblk
*pblk
, struct pblk_line
*line
,
767 void pblk_update_map(struct pblk
*pblk
, sector_t lba
, struct ppa_addr ppa
);
768 void pblk_update_map_cache(struct pblk
*pblk
, sector_t lba
,
769 struct ppa_addr ppa
);
770 void pblk_update_map_dev(struct pblk
*pblk
, sector_t lba
,
771 struct ppa_addr ppa
, struct ppa_addr entry_line
);
772 int pblk_update_map_gc(struct pblk
*pblk
, sector_t lba
, struct ppa_addr ppa
,
773 struct pblk_line
*gc_line
, u64 paddr
);
774 void pblk_lookup_l2p_rand(struct pblk
*pblk
, struct ppa_addr
*ppas
,
775 u64
*lba_list
, int nr_secs
);
776 void pblk_lookup_l2p_seq(struct pblk
*pblk
, struct ppa_addr
*ppas
,
777 sector_t blba
, int nr_secs
);
780 * pblk user I/O write path
782 int pblk_write_to_cache(struct pblk
*pblk
, struct bio
*bio
,
783 unsigned long flags
);
784 int pblk_write_gc_to_cache(struct pblk
*pblk
, struct pblk_gc_rq
*gc_rq
);
789 void 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
);
792 void pblk_map_rq(struct pblk
*pblk
, struct nvm_rq
*rqd
, unsigned int sentry
,
793 unsigned long *lun_bitmap
, unsigned int valid_secs
,
799 int pblk_write_ts(void *data
);
800 void pblk_write_timer_fn(struct timer_list
*t
);
801 void pblk_write_should_kick(struct pblk
*pblk
);
806 extern struct bio_set
*pblk_bio_set
;
807 int pblk_submit_read(struct pblk
*pblk
, struct bio
*bio
);
808 int pblk_submit_read_gc(struct pblk
*pblk
, struct pblk_gc_rq
*gc_rq
);
812 void pblk_submit_rec(struct work_struct
*work
);
813 struct pblk_line
*pblk_recov_l2p(struct pblk
*pblk
);
814 int pblk_recov_pad(struct pblk
*pblk
);
815 __le64
*pblk_recov_get_lba_list(struct pblk
*pblk
, struct line_emeta
*emeta
);
816 int pblk_recov_setup_rq(struct pblk
*pblk
, struct pblk_c_ctx
*c_ctx
,
817 struct pblk_rec_ctx
*recovery
, u64
*comp_bits
,
823 #define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
824 #define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
825 #define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
826 #define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
828 int pblk_gc_init(struct pblk
*pblk
);
829 void pblk_gc_exit(struct pblk
*pblk
);
830 void pblk_gc_should_start(struct pblk
*pblk
);
831 void pblk_gc_should_stop(struct pblk
*pblk
);
832 void pblk_gc_should_kick(struct pblk
*pblk
);
833 void pblk_gc_free_full_lines(struct pblk
*pblk
);
834 void pblk_gc_sysfs_state_show(struct pblk
*pblk
, int *gc_enabled
,
836 int pblk_gc_sysfs_force(struct pblk
*pblk
, int force
);
841 void pblk_rl_init(struct pblk_rl
*rl
, int budget
);
842 void pblk_rl_free(struct pblk_rl
*rl
);
843 void pblk_rl_update_rates(struct pblk_rl
*rl
);
844 int pblk_rl_high_thrs(struct pblk_rl
*rl
);
845 unsigned long pblk_rl_nr_free_blks(struct pblk_rl
*rl
);
846 int pblk_rl_user_may_insert(struct pblk_rl
*rl
, int nr_entries
);
847 void pblk_rl_inserted(struct pblk_rl
*rl
, int nr_entries
);
848 void pblk_rl_user_in(struct pblk_rl
*rl
, int nr_entries
);
849 int pblk_rl_gc_may_insert(struct pblk_rl
*rl
, int nr_entries
);
850 void pblk_rl_gc_in(struct pblk_rl
*rl
, int nr_entries
);
851 void pblk_rl_out(struct pblk_rl
*rl
, int nr_user
, int nr_gc
);
852 int pblk_rl_max_io(struct pblk_rl
*rl
);
853 void pblk_rl_free_lines_inc(struct pblk_rl
*rl
, struct pblk_line
*line
);
854 void pblk_rl_free_lines_dec(struct pblk_rl
*rl
, struct pblk_line
*line
);
855 int pblk_rl_is_limit(struct pblk_rl
*rl
);
860 int pblk_sysfs_init(struct gendisk
*tdisk
);
861 void pblk_sysfs_exit(struct gendisk
*tdisk
);
863 static inline void *pblk_malloc(size_t size
, int type
, gfp_t flags
)
865 if (type
== PBLK_KMALLOC_META
)
866 return kmalloc(size
, flags
);
867 return vmalloc(size
);
870 static inline void pblk_mfree(void *ptr
, int type
)
872 if (type
== PBLK_KMALLOC_META
)
878 static inline struct nvm_rq
*nvm_rq_from_c_ctx(void *c_ctx
)
880 return c_ctx
- sizeof(struct nvm_rq
);
883 static inline void *emeta_to_bb(struct line_emeta
*emeta
)
885 return emeta
->bb_bitmap
;
888 static inline void *emeta_to_lbas(struct pblk
*pblk
, struct line_emeta
*emeta
)
890 return ((void *)emeta
+ pblk
->lm
.emeta_len
[1]);
893 static inline void *emeta_to_vsc(struct pblk
*pblk
, struct line_emeta
*emeta
)
895 return (emeta_to_lbas(pblk
, emeta
) + pblk
->lm
.emeta_len
[2]);
898 static inline int pblk_line_vsc(struct pblk_line
*line
)
900 return le32_to_cpu(*line
->vsc
);
903 #define NVM_MEM_PAGE_WRITE (8)
905 static inline int pblk_pad_distance(struct pblk
*pblk
)
907 struct nvm_tgt_dev
*dev
= pblk
->dev
;
908 struct nvm_geo
*geo
= &dev
->geo
;
910 return NVM_MEM_PAGE_WRITE
* geo
->nr_luns
* geo
->sec_per_pl
;
913 static inline int pblk_dev_ppa_to_line(struct ppa_addr p
)
918 static inline int pblk_tgt_ppa_to_line(struct ppa_addr p
)
923 static inline int pblk_ppa_to_pos(struct nvm_geo
*geo
, struct ppa_addr p
)
925 return p
.g
.lun
* geo
->nr_chnls
+ p
.g
.ch
;
928 /* A block within a line corresponds to the lun */
929 static inline int pblk_dev_ppa_to_pos(struct nvm_geo
*geo
, struct ppa_addr p
)
931 return p
.g
.lun
* geo
->nr_chnls
+ p
.g
.ch
;
934 static inline struct ppa_addr
pblk_ppa32_to_ppa64(struct pblk
*pblk
, u32 ppa32
)
936 struct ppa_addr ppa64
;
941 ppa64
.ppa
= ADDR_EMPTY
;
942 } else if (ppa32
& (1U << 31)) {
943 ppa64
.c
.line
= ppa32
& ((~0U) >> 1);
944 ppa64
.c
.is_cached
= 1;
946 ppa64
.g
.blk
= (ppa32
& pblk
->ppaf
.blk_mask
) >>
947 pblk
->ppaf
.blk_offset
;
948 ppa64
.g
.pg
= (ppa32
& pblk
->ppaf
.pg_mask
) >>
949 pblk
->ppaf
.pg_offset
;
950 ppa64
.g
.lun
= (ppa32
& pblk
->ppaf
.lun_mask
) >>
951 pblk
->ppaf
.lun_offset
;
952 ppa64
.g
.ch
= (ppa32
& pblk
->ppaf
.ch_mask
) >>
953 pblk
->ppaf
.ch_offset
;
954 ppa64
.g
.pl
= (ppa32
& pblk
->ppaf
.pln_mask
) >>
955 pblk
->ppaf
.pln_offset
;
956 ppa64
.g
.sec
= (ppa32
& pblk
->ppaf
.sec_mask
) >>
957 pblk
->ppaf
.sec_offset
;
963 static inline struct ppa_addr
pblk_trans_map_get(struct pblk
*pblk
,
968 if (pblk
->ppaf_bitsize
< 32) {
969 u32
*map
= (u32
*)pblk
->trans_map
;
971 ppa
= pblk_ppa32_to_ppa64(pblk
, map
[lba
]);
973 struct ppa_addr
*map
= (struct ppa_addr
*)pblk
->trans_map
;
981 static inline u32
pblk_ppa64_to_ppa32(struct pblk
*pblk
, struct ppa_addr ppa64
)
985 if (ppa64
.ppa
== ADDR_EMPTY
) {
987 } else if (ppa64
.c
.is_cached
) {
988 ppa32
|= ppa64
.c
.line
;
991 ppa32
|= ppa64
.g
.blk
<< pblk
->ppaf
.blk_offset
;
992 ppa32
|= ppa64
.g
.pg
<< pblk
->ppaf
.pg_offset
;
993 ppa32
|= ppa64
.g
.lun
<< pblk
->ppaf
.lun_offset
;
994 ppa32
|= ppa64
.g
.ch
<< pblk
->ppaf
.ch_offset
;
995 ppa32
|= ppa64
.g
.pl
<< pblk
->ppaf
.pln_offset
;
996 ppa32
|= ppa64
.g
.sec
<< pblk
->ppaf
.sec_offset
;
1002 static inline void pblk_trans_map_set(struct pblk
*pblk
, sector_t lba
,
1003 struct ppa_addr ppa
)
1005 if (pblk
->ppaf_bitsize
< 32) {
1006 u32
*map
= (u32
*)pblk
->trans_map
;
1008 map
[lba
] = pblk_ppa64_to_ppa32(pblk
, ppa
);
1010 u64
*map
= (u64
*)pblk
->trans_map
;
1016 static inline u64
pblk_dev_ppa_to_line_addr(struct pblk
*pblk
,
1022 paddr
|= (u64
)p
.g
.pg
<< pblk
->ppaf
.pg_offset
;
1023 paddr
|= (u64
)p
.g
.lun
<< pblk
->ppaf
.lun_offset
;
1024 paddr
|= (u64
)p
.g
.ch
<< pblk
->ppaf
.ch_offset
;
1025 paddr
|= (u64
)p
.g
.pl
<< pblk
->ppaf
.pln_offset
;
1026 paddr
|= (u64
)p
.g
.sec
<< pblk
->ppaf
.sec_offset
;
1031 static inline int pblk_ppa_empty(struct ppa_addr ppa_addr
)
1033 return (ppa_addr
.ppa
== ADDR_EMPTY
);
1036 static inline void pblk_ppa_set_empty(struct ppa_addr
*ppa_addr
)
1038 ppa_addr
->ppa
= ADDR_EMPTY
;
1041 static inline bool pblk_ppa_comp(struct ppa_addr lppa
, struct ppa_addr rppa
)
1043 if (lppa
.ppa
== rppa
.ppa
)
1049 static inline int pblk_addr_in_cache(struct ppa_addr ppa
)
1051 return (ppa
.ppa
!= ADDR_EMPTY
&& ppa
.c
.is_cached
);
1054 static inline int pblk_addr_to_cacheline(struct ppa_addr ppa
)
1059 static inline struct ppa_addr
pblk_cacheline_to_addr(int addr
)
1069 static inline struct ppa_addr
addr_to_gen_ppa(struct pblk
*pblk
, u64 paddr
,
1072 struct ppa_addr ppa
;
1075 ppa
.g
.blk
= line_id
;
1076 ppa
.g
.pg
= (paddr
& pblk
->ppaf
.pg_mask
) >> pblk
->ppaf
.pg_offset
;
1077 ppa
.g
.lun
= (paddr
& pblk
->ppaf
.lun_mask
) >> pblk
->ppaf
.lun_offset
;
1078 ppa
.g
.ch
= (paddr
& pblk
->ppaf
.ch_mask
) >> pblk
->ppaf
.ch_offset
;
1079 ppa
.g
.pl
= (paddr
& pblk
->ppaf
.pln_mask
) >> pblk
->ppaf
.pln_offset
;
1080 ppa
.g
.sec
= (paddr
& pblk
->ppaf
.sec_mask
) >> pblk
->ppaf
.sec_offset
;
1085 static inline struct ppa_addr
addr_to_pblk_ppa(struct pblk
*pblk
, u64 paddr
,
1088 struct ppa_addr ppa
;
1090 ppa
= addr_to_gen_ppa(pblk
, paddr
, line_id
);
1095 static inline u32
pblk_calc_meta_header_crc(struct pblk
*pblk
,
1096 struct line_header
*header
)
1100 crc
= crc32_le(crc
, (unsigned char *)header
+ sizeof(crc
),
1101 sizeof(struct line_header
) - sizeof(crc
));
1106 static inline u32
pblk_calc_smeta_crc(struct pblk
*pblk
,
1107 struct line_smeta
*smeta
)
1109 struct pblk_line_meta
*lm
= &pblk
->lm
;
1112 crc
= crc32_le(crc
, (unsigned char *)smeta
+
1113 sizeof(struct line_header
) + sizeof(crc
),
1115 sizeof(struct line_header
) - sizeof(crc
));
1120 static inline u32
pblk_calc_emeta_crc(struct pblk
*pblk
,
1121 struct line_emeta
*emeta
)
1123 struct pblk_line_meta
*lm
= &pblk
->lm
;
1126 crc
= crc32_le(crc
, (unsigned char *)emeta
+
1127 sizeof(struct line_header
) + sizeof(crc
),
1129 sizeof(struct line_header
) - sizeof(crc
));
1134 static inline int pblk_set_progr_mode(struct pblk
*pblk
, int type
)
1136 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1137 struct nvm_geo
*geo
= &dev
->geo
;
1140 flags
= geo
->plane_mode
>> 1;
1142 if (type
== PBLK_WRITE
)
1143 flags
|= NVM_IO_SCRAMBLE_ENABLE
;
1149 PBLK_READ_RANDOM
= 0,
1150 PBLK_READ_SEQUENTIAL
= 1,
1153 static inline int pblk_set_read_mode(struct pblk
*pblk
, int type
)
1155 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1156 struct nvm_geo
*geo
= &dev
->geo
;
1159 flags
= NVM_IO_SUSPEND
| NVM_IO_SCRAMBLE_ENABLE
;
1160 if (type
== PBLK_READ_SEQUENTIAL
)
1161 flags
|= geo
->plane_mode
>> 1;
1166 static inline int pblk_io_aligned(struct pblk
*pblk
, int nr_secs
)
1168 return !(nr_secs
% pblk
->min_write_pgs
);
1171 #ifdef CONFIG_NVM_DEBUG
1172 static inline void print_ppa(struct ppa_addr
*p
, char *msg
, int error
)
1174 if (p
->c
.is_cached
) {
1175 pr_err("ppa: (%s: %x) cache line: %llu\n",
1176 msg
, error
, (u64
)p
->c
.line
);
1178 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1180 p
->g
.ch
, p
->g
.lun
, p
->g
.blk
,
1181 p
->g
.pg
, p
->g
.pl
, p
->g
.sec
);
1185 static inline void pblk_print_failed_rqd(struct pblk
*pblk
, struct nvm_rq
*rqd
,
1190 if (rqd
->nr_ppas
== 1) {
1191 print_ppa(&rqd
->ppa_addr
, "rqd", error
);
1195 while ((bit
= find_next_bit((void *)&rqd
->ppa_status
, rqd
->nr_ppas
,
1196 bit
+ 1)) < rqd
->nr_ppas
) {
1197 print_ppa(&rqd
->ppa_list
[bit
], "rqd", error
);
1200 pr_err("error:%d, ppa_status:%llx\n", error
, rqd
->ppa_status
);
1203 static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev
*tgt_dev
,
1204 struct ppa_addr
*ppas
, int nr_ppas
)
1206 struct nvm_geo
*geo
= &tgt_dev
->geo
;
1207 struct ppa_addr
*ppa
;
1210 for (i
= 0; i
< nr_ppas
; i
++) {
1213 if (!ppa
->c
.is_cached
&&
1214 ppa
->g
.ch
< geo
->nr_chnls
&&
1215 ppa
->g
.lun
< geo
->luns_per_chnl
&&
1216 ppa
->g
.pl
< geo
->nr_planes
&&
1217 ppa
->g
.blk
< geo
->blks_per_lun
&&
1218 ppa
->g
.pg
< geo
->pgs_per_blk
&&
1219 ppa
->g
.sec
< geo
->sec_per_pg
)
1222 print_ppa(ppa
, "boundary", i
);
1229 static inline int pblk_check_io(struct pblk
*pblk
, struct nvm_rq
*rqd
)
1231 struct nvm_tgt_dev
*dev
= pblk
->dev
;
1232 struct ppa_addr
*ppa_list
;
1234 ppa_list
= (rqd
->nr_ppas
> 1) ? rqd
->ppa_list
: &rqd
->ppa_addr
;
1236 if (pblk_boundary_ppa_checks(dev
, ppa_list
, rqd
->nr_ppas
)) {
1241 if (rqd
->opcode
== NVM_OP_PWRITE
) {
1242 struct pblk_line
*line
;
1243 struct ppa_addr ppa
;
1246 for (i
= 0; i
< rqd
->nr_ppas
; i
++) {
1248 line
= &pblk
->lines
[pblk_dev_ppa_to_line(ppa
)];
1250 spin_lock(&line
->lock
);
1251 if (line
->state
!= PBLK_LINESTATE_OPEN
) {
1252 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1253 line
->id
, line
->state
);
1255 spin_unlock(&line
->lock
);
1258 spin_unlock(&line
->lock
);
1266 static inline int pblk_boundary_paddr_checks(struct pblk
*pblk
, u64 paddr
)
1268 struct pblk_line_meta
*lm
= &pblk
->lm
;
1270 if (paddr
> lm
->sec_per_line
)
1276 static inline unsigned int pblk_get_bi_idx(struct bio
*bio
)
1278 return bio
->bi_iter
.bi_idx
;
1281 static inline sector_t
pblk_get_lba(struct bio
*bio
)
1283 return bio
->bi_iter
.bi_sector
/ NR_PHY_IN_LOG
;
1286 static inline unsigned int pblk_get_secs(struct bio
*bio
)
1288 return bio
->bi_iter
.bi_size
/ PBLK_EXPOSED_PAGE_SIZE
;
1291 static inline sector_t
pblk_get_sector(sector_t lba
)
1293 return lba
* NR_PHY_IN_LOG
;
1296 static inline void pblk_setup_uuid(struct pblk
*pblk
)
1301 memcpy(pblk
->instance_uuid
, uuid
.b
, 16);
1303 #endif /* PBLK_H_ */