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