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