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