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