lightnvm: pblk: refactor rqd alloc/free
[linux-2.6-microblaze.git] / drivers / lightnvm / pblk.h
1 /*
2  * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
3  * Copyright (C) 2016 CNEX Labs
4  * Initial release: Matias Bjorling <matias@cnexlabs.com>
5  * Write buffering: Javier Gonzalez <javier@cnexlabs.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * Implementation of a Physical Block-device target for Open-channel SSDs.
17  *
18  */
19
20 #ifndef PBLK_H_
21 #define PBLK_H_
22
23 #include <linux/blkdev.h>
24 #include <linux/blk-mq.h>
25 #include <linux/bio.h>
26 #include <linux/module.h>
27 #include <linux/kthread.h>
28 #include <linux/vmalloc.h>
29 #include <linux/crc32.h>
30 #include <linux/uuid.h>
31
32 #include <linux/lightnvm.h>
33
34 /* Run only GC if less than 1/X blocks are free */
35 #define GC_LIMIT_INVERSE 5
36 #define GC_TIME_MSECS 1000
37
38 #define PBLK_SECTOR (512)
39 #define PBLK_EXPOSED_PAGE_SIZE (4096)
40 #define PBLK_MAX_REQ_ADDRS (64)
41 #define PBLK_MAX_REQ_ADDRS_PW (6)
42
43 #define PBLK_NR_CLOSE_JOBS (4)
44
45 #define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
46
47 #define PBLK_COMMAND_TIMEOUT_MS 30000
48
49 /* Max 512 LUNs per device */
50 #define PBLK_MAX_LUNS_BITMAP (4)
51
52 #define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
53
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)])
57
58 /* Static pool sizes */
59 #define PBLK_GEN_WS_POOL_SIZE (2)
60
61 enum {
62         PBLK_READ               = READ,
63         PBLK_WRITE              = WRITE,/* Write from write buffer */
64         PBLK_WRITE_INT,                 /* Internal write - no write buffer */
65         PBLK_ERASE,
66 };
67
68 enum {
69         /* IO Types */
70         PBLK_IOTYPE_USER        = 1 << 0,
71         PBLK_IOTYPE_GC          = 1 << 1,
72
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,
78 };
79
80 enum {
81         PBLK_BLK_ST_OPEN =      0x1,
82         PBLK_BLK_ST_CLOSED =    0x2,
83 };
84
85 struct pblk_sec_meta {
86         u64 reserved;
87         __le64 lba;
88 };
89
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.
92  */
93 #define PBLK_GC_NR_LISTS 3
94
95 enum {
96         PBLK_RL_HIGH = 1,
97         PBLK_RL_MID = 2,
98         PBLK_RL_LOW = 3,
99 };
100
101 #define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
102
103 /* write buffer completion context */
104 struct pblk_c_ctx {
105         struct list_head list;          /* Head for out-of-order completion */
106
107         unsigned long *lun_bitmap;      /* Luns used on current request */
108         unsigned int sentry;
109         unsigned int nr_valid;
110         unsigned int nr_padded;
111 };
112
113 /* generic context */
114 struct pblk_g_ctx {
115         void *private;
116 };
117
118 /* Pad context */
119 struct pblk_pad_rq {
120         struct pblk *pblk;
121         struct completion wait;
122         struct kref ref;
123 };
124
125 /* Recovery context */
126 struct pblk_rec_ctx {
127         struct pblk *pblk;
128         struct nvm_rq *rqd;
129         struct list_head failed;
130         struct work_struct ws_rec;
131 };
132
133 /* Write context */
134 struct pblk_w_ctx {
135         struct bio_list bios;           /* Original bios - used for completion
136                                          * in REQ_FUA, REQ_FLUSH case
137                                          */
138         u64 lba;                        /* Logic addr. associated with entry */
139         struct ppa_addr ppa;            /* Physic addr. associated with entry */
140         int flags;                      /* Write context flags */
141 };
142
143 struct pblk_rb_entry {
144         struct ppa_addr cacheline;      /* Cacheline for this entry */
145         void *data;                     /* Pointer to data on this entry */
146         struct pblk_w_ctx w_ctx;        /* Context for this entry */
147         struct list_head index;         /* List head to enable indexes */
148 };
149
150 #define EMPTY_ENTRY (~0U)
151
152 struct pblk_rb_pages {
153         struct page *pages;
154         int order;
155         struct list_head list;
156 };
157
158 struct pblk_rb {
159         struct pblk_rb_entry *entries;  /* Ring buffer entries */
160         unsigned int mem;               /* Write offset - points to next
161                                          * writable entry in memory
162                                          */
163         unsigned int subm;              /* Read offset - points to last entry
164                                          * that has been submitted to the media
165                                          * to be persisted
166                                          */
167         unsigned int sync;              /* Synced - backpointer that signals
168                                          * the last submitted entry that has
169                                          * been successfully persisted to media
170                                          */
171         unsigned int sync_point;        /* Sync point - last entry that must be
172                                          * flushed to the media. Used with
173                                          * REQ_FLUSH and REQ_FUA
174                                          */
175         unsigned int l2p_update;        /* l2p update point - next entry for
176                                          * which l2p mapping will be updated to
177                                          * contain a device ppa address (instead
178                                          * of a cacheline
179                                          */
180         unsigned int nr_entries;        /* Number of entries in write buffer -
181                                          * must be a power of two
182                                          */
183         unsigned int seg_size;          /* Size of the data segments being
184                                          * stored on each entry. Typically this
185                                          * will be 4KB
186                                          */
187
188         struct list_head pages;         /* List of data pages */
189
190         spinlock_t w_lock;              /* Write lock */
191         spinlock_t s_lock;              /* Sync lock */
192
193 #ifdef CONFIG_NVM_DEBUG
194         atomic_t inflight_sync_point;   /* Not served REQ_FLUSH | REQ_FUA */
195 #endif
196 };
197
198 #define PBLK_RECOVERY_SECTORS 16
199
200 struct pblk_lun {
201         struct ppa_addr bppa;
202
203         u8 *bb_list;                    /* Bad block list for LUN. Only used on
204                                          * bring up. Bad blocks are managed
205                                          * within lines on run-time.
206                                          */
207
208         struct semaphore wr_sem;
209 };
210
211 struct pblk_gc_rq {
212         struct pblk_line *line;
213         void *data;
214         u64 paddr_list[PBLK_MAX_REQ_ADDRS];
215         u64 lba_list[PBLK_MAX_REQ_ADDRS];
216         int nr_secs;
217         int secs_to_gc;
218         struct list_head list;
219 };
220
221 struct pblk_gc {
222         /* These states are not protected by a lock since (i) they are in the
223          * fast path, and (ii) they are not critical.
224          */
225         int gc_active;
226         int gc_enabled;
227         int gc_forced;
228
229         struct task_struct *gc_ts;
230         struct task_struct *gc_writer_ts;
231         struct task_struct *gc_reader_ts;
232
233         struct workqueue_struct *gc_line_reader_wq;
234         struct workqueue_struct *gc_reader_wq;
235
236         struct timer_list gc_timer;
237
238         struct semaphore gc_sem;
239         atomic_t inflight_gc;
240         int w_entries;
241
242         struct list_head w_list;
243         struct list_head r_list;
244
245         spinlock_t lock;
246         spinlock_t w_lock;
247         spinlock_t r_lock;
248 };
249
250 struct pblk_rl {
251         unsigned int high;      /* Upper threshold for rate limiter (free run -
252                                  * user I/O rate limiter
253                                  */
254         unsigned int low;       /* Lower threshold for rate limiter (user I/O
255                                  * rate limiter - stall)
256                                  */
257         unsigned int high_pw;   /* High rounded up as a power of 2 */
258
259 #define PBLK_USER_HIGH_THRS 8   /* Begin write limit at 12% available blks */
260 #define PBLK_USER_LOW_THRS 10   /* Aggressive GC at 10% available blocks */
261
262         int rb_windows_pw;      /* Number of rate windows in the write buffer
263                                  * given as a power-of-2. This guarantees that
264                                  * when user I/O is being rate limited, there
265                                  * will be reserved enough space for the GC to
266                                  * place its payload. A window is of
267                                  * pblk->max_write_pgs size, which in NVMe is
268                                  * 64, i.e., 256kb.
269                                  */
270         int rb_budget;          /* Total number of entries available for I/O */
271         int rb_user_max;        /* Max buffer entries available for user I/O */
272         int rb_gc_max;          /* Max buffer entries available for GC I/O */
273         int rb_gc_rsv;          /* Reserved buffer entries for GC I/O */
274         int rb_state;           /* Rate-limiter current state */
275         int rb_max_io;          /* Maximum size for an I/O giving the config */
276
277         atomic_t rb_user_cnt;   /* User I/O buffer counter */
278         atomic_t rb_gc_cnt;     /* GC I/O buffer counter */
279         atomic_t rb_space;      /* Space limit in case of reaching capacity */
280
281         int rsv_blocks;         /* Reserved blocks for GC */
282
283         int rb_user_active;
284         int rb_gc_active;
285
286         struct timer_list u_timer;
287
288         unsigned long long nr_secs;
289         unsigned long total_blocks;
290         atomic_t free_blocks;
291 };
292
293 #define PBLK_LINE_EMPTY (~0U)
294
295 enum {
296         /* Line Types */
297         PBLK_LINETYPE_FREE = 0,
298         PBLK_LINETYPE_LOG = 1,
299         PBLK_LINETYPE_DATA = 2,
300
301         /* Line state */
302         PBLK_LINESTATE_FREE = 10,
303         PBLK_LINESTATE_OPEN = 11,
304         PBLK_LINESTATE_CLOSED = 12,
305         PBLK_LINESTATE_GC = 13,
306         PBLK_LINESTATE_BAD = 14,
307         PBLK_LINESTATE_CORRUPT = 15,
308
309         /* GC group */
310         PBLK_LINEGC_NONE = 20,
311         PBLK_LINEGC_EMPTY = 21,
312         PBLK_LINEGC_LOW = 22,
313         PBLK_LINEGC_MID = 23,
314         PBLK_LINEGC_HIGH = 24,
315         PBLK_LINEGC_FULL = 25,
316 };
317
318 #define PBLK_MAGIC 0x70626c6b /*pblk*/
319 #define SMETA_VERSION cpu_to_le16(1)
320
321 struct line_header {
322         __le32 crc;
323         __le32 identifier;      /* pblk identifier */
324         __u8 uuid[16];          /* instance uuid */
325         __le16 type;            /* line type */
326         __le16 version;         /* type version */
327         __le32 id;              /* line id for current line */
328 };
329
330 struct line_smeta {
331         struct line_header header;
332
333         __le32 crc;             /* Full structure including struct crc */
334         /* Previous line metadata */
335         __le32 prev_id;         /* Line id for previous line */
336
337         /* Current line metadata */
338         __le64 seq_nr;          /* Sequence number for current line */
339
340         /* Active writers */
341         __le32 window_wr_lun;   /* Number of parallel LUNs to write */
342
343         __le32 rsvd[2];
344
345         __le64 lun_bitmap[];
346 };
347
348 /*
349  * Metadata layout in media:
350  *      First sector:
351  *              1. struct line_emeta
352  *              2. bad block bitmap (u64 * window_wr_lun)
353  *      Mid sectors (start at lbas_sector):
354  *              3. nr_lbas (u64) forming lba list
355  *      Last sectors (start at vsc_sector):
356  *              4. u32 valid sector count (vsc) for all lines (~0U: free line)
357  */
358 struct line_emeta {
359         struct line_header header;
360
361         __le32 crc;             /* Full structure including struct crc */
362
363         /* Previous line metadata */
364         __le32 prev_id;         /* Line id for prev line */
365
366         /* Current line metadata */
367         __le64 seq_nr;          /* Sequence number for current line */
368
369         /* Active writers */
370         __le32 window_wr_lun;   /* Number of parallel LUNs to write */
371
372         /* Bookkeeping for recovery */
373         __le32 next_id;         /* Line id for next line */
374         __le64 nr_lbas;         /* Number of lbas mapped in line */
375         __le64 nr_valid_lbas;   /* Number of valid lbas mapped in line */
376         __le64 bb_bitmap[];     /* Updated bad block bitmap for line */
377 };
378
379 struct pblk_emeta {
380         struct line_emeta *buf;         /* emeta buffer in media format */
381         int mem;                        /* Write offset - points to next
382                                          * writable entry in memory
383                                          */
384         atomic_t sync;                  /* Synced - backpointer that signals the
385                                          * last entry that has been successfully
386                                          * persisted to media
387                                          */
388         unsigned int nr_entries;        /* Number of emeta entries */
389 };
390
391 struct pblk_smeta {
392         struct line_smeta *buf;         /* smeta buffer in persistent format */
393 };
394
395 struct pblk_line {
396         struct pblk *pblk;
397         unsigned int id;                /* Line number corresponds to the
398                                          * block line
399                                          */
400         unsigned int seq_nr;            /* Unique line sequence number */
401
402         int state;                      /* PBLK_LINESTATE_X */
403         int type;                       /* PBLK_LINETYPE_X */
404         int gc_group;                   /* PBLK_LINEGC_X */
405         struct list_head list;          /* Free, GC lists */
406
407         unsigned long *lun_bitmap;      /* Bitmap for LUNs mapped in line */
408
409         struct pblk_smeta *smeta;       /* Start metadata */
410         struct pblk_emeta *emeta;       /* End medatada */
411
412         int meta_line;                  /* Metadata line id */
413         int meta_distance;              /* Distance between data and metadata */
414
415         u64 smeta_ssec;                 /* Sector where smeta starts */
416         u64 emeta_ssec;                 /* Sector where emeta starts */
417
418         unsigned int sec_in_line;       /* Number of usable secs in line */
419
420         atomic_t blk_in_line;           /* Number of good blocks in line */
421         unsigned long *blk_bitmap;      /* Bitmap for valid/invalid blocks */
422         unsigned long *erase_bitmap;    /* Bitmap for erased blocks */
423
424         unsigned long *map_bitmap;      /* Bitmap for mapped sectors in line */
425         unsigned long *invalid_bitmap;  /* Bitmap for invalid sectors in line */
426
427         atomic_t left_eblks;            /* Blocks left for erasing */
428         atomic_t left_seblks;           /* Blocks left for sync erasing */
429
430         int left_msecs;                 /* Sectors left for mapping */
431         unsigned int cur_sec;           /* Sector map pointer */
432         unsigned int nr_valid_lbas;     /* Number of valid lbas in line */
433
434         __le32 *vsc;                    /* Valid sector count in line */
435
436         struct kref ref;                /* Write buffer L2P references */
437
438         spinlock_t lock;                /* Necessary for invalid_bitmap only */
439 };
440
441 #define PBLK_DATA_LINES 4
442
443 enum {
444         PBLK_KMALLOC_META = 1,
445         PBLK_VMALLOC_META = 2,
446 };
447
448 enum {
449         PBLK_EMETA_TYPE_HEADER = 1,     /* struct line_emeta first sector */
450         PBLK_EMETA_TYPE_LLBA = 2,       /* lba list - type: __le64 */
451         PBLK_EMETA_TYPE_VSC = 3,        /* vsc list - type: __le32 */
452 };
453
454 struct pblk_line_mgmt {
455         int nr_lines;                   /* Total number of full lines */
456         int nr_free_lines;              /* Number of full lines in free list */
457
458         /* Free lists - use free_lock */
459         struct list_head free_list;     /* Full lines ready to use */
460         struct list_head corrupt_list;  /* Full lines corrupted */
461         struct list_head bad_list;      /* Full lines bad */
462
463         /* GC lists - use gc_lock */
464         struct list_head *gc_lists[PBLK_GC_NR_LISTS];
465         struct list_head gc_high_list;  /* Full lines ready to GC, high isc */
466         struct list_head gc_mid_list;   /* Full lines ready to GC, mid isc */
467         struct list_head gc_low_list;   /* Full lines ready to GC, low isc */
468
469         struct list_head gc_full_list;  /* Full lines ready to GC, no valid */
470         struct list_head gc_empty_list; /* Full lines close, all valid */
471
472         struct pblk_line *log_line;     /* Current FTL log line */
473         struct pblk_line *data_line;    /* Current data line */
474         struct pblk_line *log_next;     /* Next FTL log line */
475         struct pblk_line *data_next;    /* Next data line */
476
477         struct list_head emeta_list;    /* Lines queued to schedule emeta */
478
479         __le32 *vsc_list;               /* Valid sector counts for all lines */
480
481         /* Metadata allocation type: VMALLOC | KMALLOC */
482         int emeta_alloc_type;
483
484         /* Pre-allocated metadata for data lines */
485         struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
486         struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
487         unsigned long meta_bitmap;
488
489         /* Helpers for fast bitmap calculations */
490         unsigned long *bb_template;
491         unsigned long *bb_aux;
492
493         unsigned long d_seq_nr;         /* Data line unique sequence number */
494         unsigned long l_seq_nr;         /* Log line unique sequence number */
495
496         spinlock_t free_lock;
497         spinlock_t close_lock;
498         spinlock_t gc_lock;
499 };
500
501 struct pblk_line_meta {
502         unsigned int smeta_len;         /* Total length for smeta */
503         unsigned int smeta_sec;         /* Sectors needed for smeta */
504
505         unsigned int emeta_len[4];      /* Lengths for emeta:
506                                          *  [0]: Total length
507                                          *  [1]: struct line_emeta length
508                                          *  [2]: L2P portion length
509                                          *  [3]: vsc list length
510                                          */
511         unsigned int emeta_sec[4];      /* Sectors needed for emeta. Same layout
512                                          * as emeta_len
513                                          */
514
515         unsigned int emeta_bb;          /* Boundary for bb that affects emeta */
516
517         unsigned int vsc_list_len;      /* Length for vsc list */
518         unsigned int sec_bitmap_len;    /* Length for sector bitmap in line */
519         unsigned int blk_bitmap_len;    /* Length for block bitmap in line */
520         unsigned int lun_bitmap_len;    /* Length for lun bitmap in line */
521
522         unsigned int blk_per_line;      /* Number of blocks in a full line */
523         unsigned int sec_per_line;      /* Number of sectors in a line */
524         unsigned int dsec_per_line;     /* Number of data sectors in a line */
525         unsigned int min_blk_line;      /* Min. number of good blocks in line */
526
527         unsigned int mid_thrs;          /* Threshold for GC mid list */
528         unsigned int high_thrs;         /* Threshold for GC high list */
529
530         unsigned int meta_distance;     /* Distance between data and metadata */
531 };
532
533 struct pblk_addr_format {
534         u64     ch_mask;
535         u64     lun_mask;
536         u64     pln_mask;
537         u64     blk_mask;
538         u64     pg_mask;
539         u64     sec_mask;
540         u8      ch_offset;
541         u8      lun_offset;
542         u8      pln_offset;
543         u8      blk_offset;
544         u8      pg_offset;
545         u8      sec_offset;
546 };
547
548 enum {
549         PBLK_STATE_RUNNING = 0,
550         PBLK_STATE_STOPPING = 1,
551         PBLK_STATE_RECOVERING = 2,
552         PBLK_STATE_STOPPED = 3,
553 };
554
555 struct pblk {
556         struct nvm_tgt_dev *dev;
557         struct gendisk *disk;
558
559         struct kobject kobj;
560
561         struct pblk_lun *luns;
562
563         struct pblk_line *lines;                /* Line array */
564         struct pblk_line_mgmt l_mg;             /* Line management */
565         struct pblk_line_meta lm;               /* Line metadata */
566
567         int ppaf_bitsize;
568         struct pblk_addr_format ppaf;
569
570         struct pblk_rb rwb;
571
572         int state;                      /* pblk line state */
573
574         int min_write_pgs; /* Minimum amount of pages required by controller */
575         int max_write_pgs; /* Maximum amount of pages supported by controller */
576         int pgs_in_buffer; /* Number of pages that need to be held in buffer to
577                             * guarantee successful reads.
578                             */
579
580         sector_t capacity; /* Device capacity when bad blocks are subtracted */
581         int over_pct;      /* Percentage of device used for over-provisioning */
582
583         /* pblk provisioning values. Used by rate limiter */
584         struct pblk_rl rl;
585
586         int sec_per_write;
587
588         unsigned char instance_uuid[16];
589 #ifdef CONFIG_NVM_DEBUG
590         /* All debug counters apply to 4kb sector I/Os */
591         atomic_long_t inflight_writes;  /* Inflight writes (user and gc) */
592         atomic_long_t padded_writes;    /* Sectors padded due to flush/fua */
593         atomic_long_t padded_wb;        /* Sectors padded in write buffer */
594         atomic_long_t nr_flush;         /* Number of flush/fua I/O */
595         atomic_long_t req_writes;       /* Sectors stored on write buffer */
596         atomic_long_t sub_writes;       /* Sectors submitted from buffer */
597         atomic_long_t sync_writes;      /* Sectors synced to media */
598         atomic_long_t inflight_reads;   /* Inflight sector read requests */
599         atomic_long_t cache_reads;      /* Read requests that hit the cache */
600         atomic_long_t sync_reads;       /* Completed sector read requests */
601         atomic_long_t recov_writes;     /* Sectors submitted from recovery */
602         atomic_long_t recov_gc_writes;  /* Sectors submitted from write GC */
603         atomic_long_t recov_gc_reads;   /* Sectors submitted from read GC */
604 #endif
605
606         spinlock_t lock;
607
608         atomic_long_t read_failed;
609         atomic_long_t read_empty;
610         atomic_long_t read_high_ecc;
611         atomic_long_t read_failed_gc;
612         atomic_long_t write_failed;
613         atomic_long_t erase_failed;
614
615         atomic_t inflight_io;           /* General inflight I/O counter */
616
617         struct task_struct *writer_ts;
618
619         /* Simple translation map of logical addresses to physical addresses.
620          * The logical addresses is known by the host system, while the physical
621          * addresses are used when writing to the disk block device.
622          */
623         unsigned char *trans_map;
624         spinlock_t trans_lock;
625
626         struct list_head compl_list;
627
628         mempool_t *page_bio_pool;
629         mempool_t *gen_ws_pool;
630         mempool_t *rec_pool;
631         mempool_t *r_rq_pool;
632         mempool_t *w_rq_pool;
633         mempool_t *e_rq_pool;
634
635         struct workqueue_struct *close_wq;
636         struct workqueue_struct *bb_wq;
637
638         struct timer_list wtimer;
639
640         struct pblk_gc gc;
641 };
642
643 struct pblk_line_ws {
644         struct pblk *pblk;
645         struct pblk_line *line;
646         void *priv;
647         struct work_struct ws;
648 };
649
650 #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
651 #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
652
653 /*
654  * pblk ring buffer operations
655  */
656 int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
657                  unsigned int power_size, unsigned int power_seg_sz);
658 unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
659 void *pblk_rb_entries_ref(struct pblk_rb *rb);
660 int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
661                            unsigned int nr_entries, unsigned int *pos);
662 int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
663                          unsigned int *pos);
664 void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
665                               struct pblk_w_ctx w_ctx, unsigned int pos);
666 void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
667                             struct pblk_w_ctx w_ctx, struct pblk_line *line,
668                             u64 paddr, unsigned int pos);
669 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
670 void pblk_rb_flush(struct pblk_rb *rb);
671
672 void pblk_rb_sync_l2p(struct pblk_rb *rb);
673 unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
674                                  unsigned int pos, unsigned int nr_entries,
675                                  unsigned int count);
676 unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
677                                       struct list_head *list,
678                                       unsigned int max);
679 int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
680                         struct ppa_addr ppa, int bio_iter, bool advanced_bio);
681 unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
682
683 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
684 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
685 struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
686                                               struct ppa_addr *ppa);
687 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
688 unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb);
689
690 unsigned int pblk_rb_read_count(struct pblk_rb *rb);
691 unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
692 unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
693
694 int pblk_rb_tear_down_check(struct pblk_rb *rb);
695 int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
696 void pblk_rb_data_free(struct pblk_rb *rb);
697 ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
698
699 /*
700  * pblk core
701  */
702 struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
703 void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
704 void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
705 int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
706                         struct pblk_c_ctx *c_ctx);
707 void pblk_wait_for_meta(struct pblk *pblk);
708 struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba);
709 void pblk_discard(struct pblk *pblk, struct bio *bio);
710 void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
711 void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
712 int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
713 int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
714 struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
715                               unsigned int nr_secs, unsigned int len,
716                               int alloc_type, gfp_t gfp_mask);
717 struct pblk_line *pblk_line_get(struct pblk *pblk);
718 struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
719 void pblk_line_replace_data(struct pblk *pblk);
720 int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
721 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
722 struct pblk_line *pblk_line_get_data(struct pblk *pblk);
723 struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
724 int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
725 int pblk_line_is_full(struct pblk_line *line);
726 void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
727 void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
728 void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
729 void pblk_line_close_meta_sync(struct pblk *pblk);
730 void pblk_line_close_ws(struct work_struct *work);
731 void pblk_pipeline_stop(struct pblk *pblk);
732 void pblk_line_mark_bb(struct work_struct *work);
733 void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
734                      void (*work)(struct work_struct *), gfp_t gfp_mask,
735                      struct workqueue_struct *wq);
736 u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
737 int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
738 int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
739                          void *emeta_buf);
740 int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
741 void pblk_line_put(struct kref *ref);
742 struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
743 u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
744 void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
745 u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
746 u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
747 int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
748                    unsigned long secs_to_flush);
749 void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
750 void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
751                   unsigned long *lun_bitmap);
752 void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
753 void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
754                 unsigned long *lun_bitmap);
755 void pblk_end_bio_sync(struct bio *bio);
756 void pblk_end_io_sync(struct nvm_rq *rqd);
757 int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
758                        int nr_pages);
759 void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
760                          int nr_pages);
761 void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
762 void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
763                            u64 paddr);
764 void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
765 void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
766                            struct ppa_addr ppa);
767 void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
768                          struct ppa_addr ppa, struct ppa_addr entry_line);
769 int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
770                        struct pblk_line *gc_line, u64 paddr);
771 void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
772                           u64 *lba_list, int nr_secs);
773 void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
774                          sector_t blba, int nr_secs);
775
776 /*
777  * pblk user I/O write path
778  */
779 int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
780                         unsigned long flags);
781 int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
782
783 /*
784  * pblk map
785  */
786 void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
787                        unsigned int sentry, unsigned long *lun_bitmap,
788                        unsigned int valid_secs, struct ppa_addr *erase_ppa);
789 void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
790                  unsigned long *lun_bitmap, unsigned int valid_secs,
791                  unsigned int off);
792
793 /*
794  * pblk write thread
795  */
796 int pblk_write_ts(void *data);
797 void pblk_write_timer_fn(unsigned long data);
798 void pblk_write_should_kick(struct pblk *pblk);
799
800 /*
801  * pblk read path
802  */
803 extern struct bio_set *pblk_bio_set;
804 int pblk_submit_read(struct pblk *pblk, struct bio *bio);
805 int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
806 /*
807  * pblk recovery
808  */
809 void pblk_submit_rec(struct work_struct *work);
810 struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
811 int pblk_recov_pad(struct pblk *pblk);
812 __le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta);
813 int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
814                         struct pblk_rec_ctx *recovery, u64 *comp_bits,
815                         unsigned int comp);
816
817 /*
818  * pblk gc
819  */
820 #define PBLK_GC_MAX_READERS 8   /* Max number of outstanding GC reader jobs */
821 #define PBLK_GC_RQ_QD 128       /* Queue depth for inflight GC requests */
822 #define PBLK_GC_L_QD 4          /* Queue depth for inflight GC lines */
823 #define PBLK_GC_RSV_LINE 1      /* Reserved lines for GC */
824
825 int pblk_gc_init(struct pblk *pblk);
826 void pblk_gc_exit(struct pblk *pblk);
827 void pblk_gc_should_start(struct pblk *pblk);
828 void pblk_gc_should_stop(struct pblk *pblk);
829 void pblk_gc_kick(struct pblk *pblk);
830 void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
831                               int *gc_active);
832 int pblk_gc_sysfs_force(struct pblk *pblk, int force);
833
834 /*
835  * pblk rate limiter
836  */
837 void pblk_rl_init(struct pblk_rl *rl, int budget);
838 void pblk_rl_free(struct pblk_rl *rl);
839 int pblk_rl_high_thrs(struct pblk_rl *rl);
840 int pblk_rl_low_thrs(struct pblk_rl *rl);
841 unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
842 int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
843 void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
844 void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
845 int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
846 void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
847 void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
848 int pblk_rl_sysfs_rate_show(struct pblk_rl *rl);
849 int pblk_rl_max_io(struct pblk_rl *rl);
850 void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
851 void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line);
852 void pblk_rl_set_space_limit(struct pblk_rl *rl, int entries_left);
853 int pblk_rl_is_limit(struct pblk_rl *rl);
854
855 /*
856  * pblk sysfs
857  */
858 int pblk_sysfs_init(struct gendisk *tdisk);
859 void pblk_sysfs_exit(struct gendisk *tdisk);
860
861 static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
862 {
863         if (type == PBLK_KMALLOC_META)
864                 return kmalloc(size, flags);
865         return vmalloc(size);
866 }
867
868 static inline void pblk_mfree(void *ptr, int type)
869 {
870         if (type == PBLK_KMALLOC_META)
871                 kfree(ptr);
872         else
873                 vfree(ptr);
874 }
875
876 static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
877 {
878         return c_ctx - sizeof(struct nvm_rq);
879 }
880
881 static inline void *emeta_to_bb(struct line_emeta *emeta)
882 {
883         return emeta->bb_bitmap;
884 }
885
886 static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
887 {
888         return ((void *)emeta + pblk->lm.emeta_len[1]);
889 }
890
891 static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
892 {
893         return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
894 }
895
896 static inline int pblk_line_vsc(struct pblk_line *line)
897 {
898         return le32_to_cpu(*line->vsc);
899 }
900
901 #define NVM_MEM_PAGE_WRITE (8)
902
903 static inline int pblk_pad_distance(struct pblk *pblk)
904 {
905         struct nvm_tgt_dev *dev = pblk->dev;
906         struct nvm_geo *geo = &dev->geo;
907
908         return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl;
909 }
910
911 static inline int pblk_dev_ppa_to_line(struct ppa_addr p)
912 {
913         return p.g.blk;
914 }
915
916 static inline int pblk_tgt_ppa_to_line(struct ppa_addr p)
917 {
918         return p.g.blk;
919 }
920
921 static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
922 {
923         return p.g.lun * geo->nr_chnls + p.g.ch;
924 }
925
926 /* A block within a line corresponds to the lun */
927 static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
928 {
929         return p.g.lun * geo->nr_chnls + p.g.ch;
930 }
931
932 static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
933 {
934         struct ppa_addr ppa64;
935
936         ppa64.ppa = 0;
937
938         if (ppa32 == -1) {
939                 ppa64.ppa = ADDR_EMPTY;
940         } else if (ppa32 & (1U << 31)) {
941                 ppa64.c.line = ppa32 & ((~0U) >> 1);
942                 ppa64.c.is_cached = 1;
943         } else {
944                 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
945                                                         pblk->ppaf.blk_offset;
946                 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
947                                                         pblk->ppaf.pg_offset;
948                 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
949                                                         pblk->ppaf.lun_offset;
950                 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
951                                                         pblk->ppaf.ch_offset;
952                 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
953                                                         pblk->ppaf.pln_offset;
954                 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
955                                                         pblk->ppaf.sec_offset;
956         }
957
958         return ppa64;
959 }
960
961 static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
962                                                                 sector_t lba)
963 {
964         struct ppa_addr ppa;
965
966         if (pblk->ppaf_bitsize < 32) {
967                 u32 *map = (u32 *)pblk->trans_map;
968
969                 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
970         } else {
971                 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
972
973                 ppa = map[lba];
974         }
975
976         return ppa;
977 }
978
979 static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
980 {
981         u32 ppa32 = 0;
982
983         if (ppa64.ppa == ADDR_EMPTY) {
984                 ppa32 = ~0U;
985         } else if (ppa64.c.is_cached) {
986                 ppa32 |= ppa64.c.line;
987                 ppa32 |= 1U << 31;
988         } else {
989                 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
990                 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
991                 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
992                 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
993                 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
994                 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
995         }
996
997         return ppa32;
998 }
999
1000 static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1001                                                 struct ppa_addr ppa)
1002 {
1003         if (pblk->ppaf_bitsize < 32) {
1004                 u32 *map = (u32 *)pblk->trans_map;
1005
1006                 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1007         } else {
1008                 u64 *map = (u64 *)pblk->trans_map;
1009
1010                 map[lba] = ppa.ppa;
1011         }
1012 }
1013
1014 static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1015                                                         struct ppa_addr p)
1016 {
1017         u64 paddr;
1018
1019         paddr = 0;
1020         paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset;
1021         paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
1022         paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
1023         paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
1024         paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
1025
1026         return paddr;
1027 }
1028
1029 static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1030 {
1031         return (ppa_addr.ppa == ADDR_EMPTY);
1032 }
1033
1034 static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1035 {
1036         ppa_addr->ppa = ADDR_EMPTY;
1037 }
1038
1039 static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1040 {
1041         if (lppa.ppa == rppa.ppa)
1042                 return true;
1043
1044         return false;
1045 }
1046
1047 static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1048 {
1049         return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1050 }
1051
1052 static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1053 {
1054         return ppa.c.line;
1055 }
1056
1057 static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1058 {
1059         struct ppa_addr p;
1060
1061         p.c.line = addr;
1062         p.c.is_cached = 1;
1063
1064         return p;
1065 }
1066
1067 static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1068                                               u64 line_id)
1069 {
1070         struct ppa_addr ppa;
1071
1072         ppa.ppa = 0;
1073         ppa.g.blk = line_id;
1074         ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
1075         ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
1076         ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
1077         ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
1078         ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
1079
1080         return ppa;
1081 }
1082
1083 static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr,
1084                                          u64 line_id)
1085 {
1086         struct ppa_addr ppa;
1087
1088         ppa = addr_to_gen_ppa(pblk, paddr, line_id);
1089
1090         return ppa;
1091 }
1092
1093 static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
1094                                             struct line_header *header)
1095 {
1096         u32 crc = ~(u32)0;
1097
1098         crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
1099                                 sizeof(struct line_header) - sizeof(crc));
1100
1101         return crc;
1102 }
1103
1104 static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1105                                       struct line_smeta *smeta)
1106 {
1107         struct pblk_line_meta *lm = &pblk->lm;
1108         u32 crc = ~(u32)0;
1109
1110         crc = crc32_le(crc, (unsigned char *)smeta +
1111                                 sizeof(struct line_header) + sizeof(crc),
1112                                 lm->smeta_len -
1113                                 sizeof(struct line_header) - sizeof(crc));
1114
1115         return crc;
1116 }
1117
1118 static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1119                                       struct line_emeta *emeta)
1120 {
1121         struct pblk_line_meta *lm = &pblk->lm;
1122         u32 crc = ~(u32)0;
1123
1124         crc = crc32_le(crc, (unsigned char *)emeta +
1125                                 sizeof(struct line_header) + sizeof(crc),
1126                                 lm->emeta_len[0] -
1127                                 sizeof(struct line_header) - sizeof(crc));
1128
1129         return crc;
1130 }
1131
1132 static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1133 {
1134         struct nvm_tgt_dev *dev = pblk->dev;
1135         struct nvm_geo *geo = &dev->geo;
1136         int flags;
1137
1138         flags = geo->plane_mode >> 1;
1139
1140         if (type == PBLK_WRITE)
1141                 flags |= NVM_IO_SCRAMBLE_ENABLE;
1142
1143         return flags;
1144 }
1145
1146 enum {
1147         PBLK_READ_RANDOM        = 0,
1148         PBLK_READ_SEQUENTIAL    = 1,
1149 };
1150
1151 static inline int pblk_set_read_mode(struct pblk *pblk, int type)
1152 {
1153         struct nvm_tgt_dev *dev = pblk->dev;
1154         struct nvm_geo *geo = &dev->geo;
1155         int flags;
1156
1157         flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1158         if (type == PBLK_READ_SEQUENTIAL)
1159                 flags |= geo->plane_mode >> 1;
1160
1161         return flags;
1162 }
1163
1164 static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1165 {
1166         return !(nr_secs % pblk->min_write_pgs);
1167 }
1168
1169 #ifdef CONFIG_NVM_DEBUG
1170 static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1171 {
1172         if (p->c.is_cached) {
1173                 pr_err("ppa: (%s: %x) cache line: %llu\n",
1174                                 msg, error, (u64)p->c.line);
1175         } else {
1176                 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1177                         msg, error,
1178                         p->g.ch, p->g.lun, p->g.blk,
1179                         p->g.pg, p->g.pl, p->g.sec);
1180         }
1181 }
1182
1183 static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1184                                          int error)
1185 {
1186         int bit = -1;
1187
1188         if (rqd->nr_ppas ==  1) {
1189                 print_ppa(&rqd->ppa_addr, "rqd", error);
1190                 return;
1191         }
1192
1193         while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1194                                                 bit + 1)) < rqd->nr_ppas) {
1195                 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1196         }
1197
1198         pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1199 }
1200 #endif
1201
1202 static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1203                                        struct ppa_addr *ppas, int nr_ppas)
1204 {
1205         struct nvm_geo *geo = &tgt_dev->geo;
1206         struct ppa_addr *ppa;
1207         int i;
1208
1209         for (i = 0; i < nr_ppas; i++) {
1210                 ppa = &ppas[i];
1211
1212                 if (!ppa->c.is_cached &&
1213                                 ppa->g.ch < geo->nr_chnls &&
1214                                 ppa->g.lun < geo->luns_per_chnl &&
1215                                 ppa->g.pl < geo->nr_planes &&
1216                                 ppa->g.blk < geo->blks_per_lun &&
1217                                 ppa->g.pg < geo->pgs_per_blk &&
1218                                 ppa->g.sec < geo->sec_per_pg)
1219                         continue;
1220
1221 #ifdef CONFIG_NVM_DEBUG
1222                 print_ppa(ppa, "boundary", i);
1223 #endif
1224                 return 1;
1225         }
1226         return 0;
1227 }
1228
1229 static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1230 {
1231         struct pblk_line_meta *lm = &pblk->lm;
1232
1233         if (paddr > lm->sec_per_line)
1234                 return 1;
1235
1236         return 0;
1237 }
1238
1239 static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1240 {
1241         return bio->bi_iter.bi_idx;
1242 }
1243
1244 static inline sector_t pblk_get_lba(struct bio *bio)
1245 {
1246         return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1247 }
1248
1249 static inline unsigned int pblk_get_secs(struct bio *bio)
1250 {
1251         return  bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1252 }
1253
1254 static inline sector_t pblk_get_sector(sector_t lba)
1255 {
1256         return lba * NR_PHY_IN_LOG;
1257 }
1258
1259 static inline void pblk_setup_uuid(struct pblk *pblk)
1260 {
1261         uuid_le uuid;
1262
1263         uuid_le_gen(&uuid);
1264         memcpy(pblk->instance_uuid, uuid.b, 16);
1265 }
1266 #endif /* PBLK_H_ */