]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/lightnvm/pblk.h
Merge tag 'sound-4.17-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[mirror_ubuntu-hirsute-kernel.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
bd432417 667 mempool_t *page_bio_pool;
b84ae4a8 668 mempool_t *gen_ws_pool;
a4bd217b 669 mempool_t *rec_pool;
0d880398 670 mempool_t *r_rq_pool;
a4bd217b 671 mempool_t *w_rq_pool;
0d880398 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);
769void pblk_line_free(struct pblk *pblk, 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);
b84ae4a8
JG
774void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
775 void (*work)(struct work_struct *), gfp_t gfp_mask,
776 struct workqueue_struct *wq);
a4bd217b
JG
777u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
778int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
dd2a4343
JG
779int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
780 void *emeta_buf);
a4bd217b
JG
781int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
782void pblk_line_put(struct kref *ref);
7bd4d370 783void pblk_line_put_wq(struct kref *ref);
a4bd217b 784struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
dd2a4343
JG
785u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
786void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
a4bd217b 787u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
dd2a4343 788u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
a4bd217b
JG
789int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
790 unsigned long secs_to_flush);
3eaa11e2 791void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
a4bd217b
JG
792void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
793 unsigned long *lun_bitmap);
3eaa11e2 794void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
a4bd217b
JG
795void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
796 unsigned long *lun_bitmap);
a4bd217b
JG
797void pblk_end_io_sync(struct nvm_rq *rqd);
798int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
799 int nr_pages);
a4bd217b
JG
800void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
801 int nr_pages);
802void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
0880a9aa
JG
803void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
804 u64 paddr);
a4bd217b
JG
805void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
806void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
807 struct ppa_addr ppa);
808void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
809 struct ppa_addr ppa, struct ppa_addr entry_line);
810int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
d340121e 811 struct pblk_line *gc_line, u64 paddr);
a4bd217b
JG
812void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
813 u64 *lba_list, int nr_secs);
814void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
815 sector_t blba, int nr_secs);
816
817/*
818 * pblk user I/O write path
819 */
820int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
821 unsigned long flags);
d340121e 822int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
a4bd217b
JG
823
824/*
825 * pblk map
826 */
827void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
828 unsigned int sentry, unsigned long *lun_bitmap,
829 unsigned int valid_secs, struct ppa_addr *erase_ppa);
830void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
831 unsigned long *lun_bitmap, unsigned int valid_secs,
832 unsigned int off);
833
834/*
835 * pblk write thread
836 */
837int pblk_write_ts(void *data);
87c1d2d3 838void pblk_write_timer_fn(struct timer_list *t);
a4bd217b
JG
839void pblk_write_should_kick(struct pblk *pblk);
840
841/*
842 * pblk read path
843 */
b25d5237 844extern struct bio_set *pblk_bio_set;
a4bd217b 845int pblk_submit_read(struct pblk *pblk, struct bio *bio);
d340121e 846int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
a4bd217b
JG
847/*
848 * pblk recovery
849 */
850void pblk_submit_rec(struct work_struct *work);
851struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
588726d3 852int pblk_recov_pad(struct pblk *pblk);
06bc072b 853int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
a4bd217b
JG
854int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
855 struct pblk_rec_ctx *recovery, u64 *comp_bits,
856 unsigned int comp);
857
858/*
859 * pblk gc
860 */
b20ba1bc 861#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
3627896a 862#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
b20ba1bc
JG
863#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
864#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
a4bd217b
JG
865
866int pblk_gc_init(struct pblk *pblk);
867void pblk_gc_exit(struct pblk *pblk);
868void pblk_gc_should_start(struct pblk *pblk);
869void pblk_gc_should_stop(struct pblk *pblk);
03661b5f 870void pblk_gc_should_kick(struct pblk *pblk);
37ce33d5 871void pblk_gc_free_full_lines(struct pblk *pblk);
a4bd217b
JG
872void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
873 int *gc_active);
b20ba1bc 874int pblk_gc_sysfs_force(struct pblk *pblk, int force);
a4bd217b
JG
875
876/*
877 * pblk rate limiter
878 */
879void pblk_rl_init(struct pblk_rl *rl, int budget);
880void pblk_rl_free(struct pblk_rl *rl);
03661b5f 881void pblk_rl_update_rates(struct pblk_rl *rl);
b20ba1bc 882int pblk_rl_high_thrs(struct pblk_rl *rl);
a4bd217b 883unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
a7689938 884unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
a4bd217b 885int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
588726d3 886void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
a4bd217b
JG
887void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
888int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
889void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
890void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
da67e68f 891int pblk_rl_max_io(struct pblk_rl *rl);
a4bd217b 892void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
a7689938
JG
893void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
894 bool used);
588726d3 895int pblk_rl_is_limit(struct pblk_rl *rl);
a4bd217b
JG
896
897/*
898 * pblk sysfs
899 */
900int pblk_sysfs_init(struct gendisk *tdisk);
901void pblk_sysfs_exit(struct gendisk *tdisk);
902
903static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
904{
905 if (type == PBLK_KMALLOC_META)
906 return kmalloc(size, flags);
907 return vmalloc(size);
908}
909
910static inline void pblk_mfree(void *ptr, int type)
911{
912 if (type == PBLK_KMALLOC_META)
913 kfree(ptr);
914 else
915 vfree(ptr);
916}
917
918static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
919{
920 return c_ctx - sizeof(struct nvm_rq);
921}
922
dd2a4343
JG
923static inline void *emeta_to_bb(struct line_emeta *emeta)
924{
925 return emeta->bb_bitmap;
926}
927
76758390
HH
928static inline void *emeta_to_wa(struct pblk_line_meta *lm,
929 struct line_emeta *emeta)
930{
931 return emeta->bb_bitmap + lm->blk_bitmap_len;
932}
933
dd2a4343
JG
934static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
935{
936 return ((void *)emeta + pblk->lm.emeta_len[1]);
937}
938
939static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
a4bd217b 940{
dd2a4343 941 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
a4bd217b
JG
942}
943
b20ba1bc
JG
944static inline int pblk_line_vsc(struct pblk_line *line)
945{
d340121e 946 return le32_to_cpu(*line->vsc);
b20ba1bc
JG
947}
948
a4bd217b
JG
949static inline int pblk_pad_distance(struct pblk *pblk)
950{
951 struct nvm_tgt_dev *dev = pblk->dev;
952 struct nvm_geo *geo = &dev->geo;
953
e46f4e48 954 return geo->mw_cunits * geo->all_luns * geo->ws_opt;
a4bd217b
JG
955}
956
b1bcfda1 957static inline int pblk_ppa_to_line(struct ppa_addr p)
a4bd217b 958{
69471513 959 return p.a.blk;
a4bd217b
JG
960}
961
b1bcfda1 962static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
a4bd217b 963{
69471513 964 return p.a.lun * geo->num_ch + p.a.ch;
a4bd217b
JG
965}
966
b1bcfda1
JG
967static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
968 u64 line_id)
a4bd217b 969{
3b2a3ad1
JG
970 struct nvm_tgt_dev *dev = pblk->dev;
971 struct nvm_geo *geo = &dev->geo;
b1bcfda1
JG
972 struct ppa_addr ppa;
973
3b2a3ad1
JG
974 if (geo->version == NVM_OCSSD_SPEC_12) {
975 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
976
977 ppa.ppa = 0;
978 ppa.g.blk = line_id;
979 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
980 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
981 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
982 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
983 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
984 } else {
985 struct pblk_addrf *uaddrf = &pblk->uaddrf;
986 int secs, chnls, luns;
987
988 ppa.ppa = 0;
989
990 ppa.m.chk = line_id;
991
992 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
993 ppa.m.sec = secs;
994
995 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
996 ppa.m.grp = chnls;
997
998 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
999 ppa.m.pu = luns;
1000
1001 ppa.m.sec += uaddrf->sec_stripe * paddr;
1002 }
b1bcfda1
JG
1003
1004 return ppa;
a4bd217b
JG
1005}
1006
b1bcfda1
JG
1007static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1008 struct ppa_addr p)
a4bd217b 1009{
3b2a3ad1
JG
1010 struct nvm_tgt_dev *dev = pblk->dev;
1011 struct nvm_geo *geo = &dev->geo;
b1bcfda1
JG
1012 u64 paddr;
1013
3b2a3ad1
JG
1014 if (geo->version == NVM_OCSSD_SPEC_12) {
1015 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1016
1017 paddr = (u64)p.g.ch << ppaf->ch_offset;
1018 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1019 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1020 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1021 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1022 } else {
1023 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1024 u64 secs = p.m.sec;
1025 int sec_stripe;
1026
1027 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1028 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1029
1030 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1031 paddr += secs * uaddrf->sec_ws_stripe;
1032 paddr += sec_stripe;
1033 }
b1bcfda1
JG
1034
1035 return paddr;
a4bd217b
JG
1036}
1037
1038static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1039{
1040 struct ppa_addr ppa64;
1041
1042 ppa64.ppa = 0;
1043
1044 if (ppa32 == -1) {
1045 ppa64.ppa = ADDR_EMPTY;
1046 } else if (ppa32 & (1U << 31)) {
1047 ppa64.c.line = ppa32 & ((~0U) >> 1);
1048 ppa64.c.is_cached = 1;
1049 } else {
3b2a3ad1
JG
1050 struct nvm_tgt_dev *dev = pblk->dev;
1051 struct nvm_geo *geo = &dev->geo;
1052
1053 if (geo->version == NVM_OCSSD_SPEC_12) {
1054 struct nvm_addrf_12 *ppaf =
1055 (struct nvm_addrf_12 *)&pblk->addrf;
1056
1057 ppa64.g.ch = (ppa32 & ppaf->ch_mask) >>
1058 ppaf->ch_offset;
1059 ppa64.g.lun = (ppa32 & ppaf->lun_mask) >>
1060 ppaf->lun_offset;
1061 ppa64.g.blk = (ppa32 & ppaf->blk_mask) >>
1062 ppaf->blk_offset;
1063 ppa64.g.pg = (ppa32 & ppaf->pg_mask) >>
1064 ppaf->pg_offset;
1065 ppa64.g.pl = (ppa32 & ppaf->pln_mask) >>
1066 ppaf->pln_offset;
1067 ppa64.g.sec = (ppa32 & ppaf->sec_mask) >>
1068 ppaf->sec_offset;
1069 } else {
1070 struct nvm_addrf *lbaf = &pblk->addrf;
1071
1072 ppa64.m.grp = (ppa32 & lbaf->ch_mask) >>
1073 lbaf->ch_offset;
1074 ppa64.m.pu = (ppa32 & lbaf->lun_mask) >>
1075 lbaf->lun_offset;
1076 ppa64.m.chk = (ppa32 & lbaf->chk_mask) >>
1077 lbaf->chk_offset;
1078 ppa64.m.sec = (ppa32 & lbaf->sec_mask) >>
1079 lbaf->sec_offset;
1080 }
a4bd217b
JG
1081 }
1082
1083 return ppa64;
1084}
1085
a4bd217b
JG
1086static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1087{
1088 u32 ppa32 = 0;
1089
1090 if (ppa64.ppa == ADDR_EMPTY) {
1091 ppa32 = ~0U;
1092 } else if (ppa64.c.is_cached) {
1093 ppa32 |= ppa64.c.line;
1094 ppa32 |= 1U << 31;
1095 } else {
3b2a3ad1
JG
1096 struct nvm_tgt_dev *dev = pblk->dev;
1097 struct nvm_geo *geo = &dev->geo;
1098
1099 if (geo->version == NVM_OCSSD_SPEC_12) {
1100 struct nvm_addrf_12 *ppaf =
1101 (struct nvm_addrf_12 *)&pblk->addrf;
1102
1103 ppa32 |= ppa64.g.ch << ppaf->ch_offset;
1104 ppa32 |= ppa64.g.lun << ppaf->lun_offset;
1105 ppa32 |= ppa64.g.blk << ppaf->blk_offset;
1106 ppa32 |= ppa64.g.pg << ppaf->pg_offset;
1107 ppa32 |= ppa64.g.pl << ppaf->pln_offset;
1108 ppa32 |= ppa64.g.sec << ppaf->sec_offset;
1109 } else {
1110 struct nvm_addrf *lbaf = &pblk->addrf;
1111
1112 ppa32 |= ppa64.m.grp << lbaf->ch_offset;
1113 ppa32 |= ppa64.m.pu << lbaf->lun_offset;
1114 ppa32 |= ppa64.m.chk << lbaf->chk_offset;
1115 ppa32 |= ppa64.m.sec << lbaf->sec_offset;
1116 }
a4bd217b
JG
1117 }
1118
1119 return ppa32;
1120}
1121
b1bcfda1
JG
1122static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1123 sector_t lba)
a4bd217b 1124{
b1bcfda1
JG
1125 struct ppa_addr ppa;
1126
bb845ae4 1127 if (pblk->addrf_len < 32) {
a4bd217b
JG
1128 u32 *map = (u32 *)pblk->trans_map;
1129
b1bcfda1 1130 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
a4bd217b 1131 } else {
b1bcfda1 1132 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
a4bd217b 1133
b1bcfda1 1134 ppa = map[lba];
a4bd217b 1135 }
b1bcfda1
JG
1136
1137 return ppa;
a4bd217b
JG
1138}
1139
b1bcfda1
JG
1140static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1141 struct ppa_addr ppa)
a4bd217b 1142{
bb845ae4 1143 if (pblk->addrf_len < 32) {
b1bcfda1 1144 u32 *map = (u32 *)pblk->trans_map;
a4bd217b 1145
b1bcfda1
JG
1146 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1147 } else {
1148 u64 *map = (u64 *)pblk->trans_map;
a4bd217b 1149
b1bcfda1
JG
1150 map[lba] = ppa.ppa;
1151 }
a4bd217b
JG
1152}
1153
1154static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1155{
1156 return (ppa_addr.ppa == ADDR_EMPTY);
1157}
1158
1159static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1160{
1161 ppa_addr->ppa = ADDR_EMPTY;
1162}
1163
07698466
JG
1164static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1165{
8b7bc849 1166 return (lppa.ppa == rppa.ppa);
07698466
JG
1167}
1168
a4bd217b
JG
1169static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1170{
1171 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1172}
1173
1174static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1175{
1176 return ppa.c.line;
1177}
1178
1179static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1180{
1181 struct ppa_addr p;
1182
1183 p.c.line = addr;
1184 p.c.is_cached = 1;
1185
1186 return p;
1187}
1188
a4bd217b 1189static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
dd2a4343 1190 struct line_header *header)
a4bd217b
JG
1191{
1192 u32 crc = ~(u32)0;
1193
dd2a4343 1194 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
a4bd217b
JG
1195 sizeof(struct line_header) - sizeof(crc));
1196
1197 return crc;
1198}
1199
1200static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1201 struct line_smeta *smeta)
1202{
1203 struct pblk_line_meta *lm = &pblk->lm;
1204 u32 crc = ~(u32)0;
1205
1206 crc = crc32_le(crc, (unsigned char *)smeta +
1207 sizeof(struct line_header) + sizeof(crc),
1208 lm->smeta_len -
1209 sizeof(struct line_header) - sizeof(crc));
1210
1211 return crc;
1212}
1213
1214static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1215 struct line_emeta *emeta)
1216{
1217 struct pblk_line_meta *lm = &pblk->lm;
1218 u32 crc = ~(u32)0;
1219
1220 crc = crc32_le(crc, (unsigned char *)emeta +
1221 sizeof(struct line_header) + sizeof(crc),
dd2a4343 1222 lm->emeta_len[0] -
a4bd217b
JG
1223 sizeof(struct line_header) - sizeof(crc));
1224
1225 return crc;
1226}
1227
1228static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1229{
1230 struct nvm_tgt_dev *dev = pblk->dev;
1231 struct nvm_geo *geo = &dev->geo;
1232 int flags;
1233
3b2a3ad1
JG
1234 if (geo->version == NVM_OCSSD_SPEC_20)
1235 return 0;
1236
a40afad9 1237 flags = geo->pln_mode >> 1;
a4bd217b 1238
e2cddf20 1239 if (type == PBLK_WRITE)
a4bd217b
JG
1240 flags |= NVM_IO_SCRAMBLE_ENABLE;
1241
1242 return flags;
1243}
1244
f9c10152
JG
1245enum {
1246 PBLK_READ_RANDOM = 0,
1247 PBLK_READ_SEQUENTIAL = 1,
1248};
1249
1250static inline int pblk_set_read_mode(struct pblk *pblk, int type)
1251{
1252 struct nvm_tgt_dev *dev = pblk->dev;
1253 struct nvm_geo *geo = &dev->geo;
1254 int flags;
1255
3b2a3ad1
JG
1256 if (geo->version == NVM_OCSSD_SPEC_20)
1257 return 0;
1258
f9c10152
JG
1259 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1260 if (type == PBLK_READ_SEQUENTIAL)
a40afad9 1261 flags |= geo->pln_mode >> 1;
f9c10152
JG
1262
1263 return flags;
1264}
1265
1266static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
a4bd217b 1267{
f9c10152 1268 return !(nr_secs % pblk->min_write_pgs);
a4bd217b
JG
1269}
1270
1271#ifdef CONFIG_NVM_DEBUG
3b2a3ad1
JG
1272static inline void print_ppa(struct nvm_geo *geo, struct ppa_addr *p,
1273 char *msg, int error)
a4bd217b
JG
1274{
1275 if (p->c.is_cached) {
1276 pr_err("ppa: (%s: %x) cache line: %llu\n",
1277 msg, error, (u64)p->c.line);
3b2a3ad1 1278 } else if (geo->version == NVM_OCSSD_SPEC_12) {
a4bd217b
JG
1279 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1280 msg, error,
1281 p->g.ch, p->g.lun, p->g.blk,
1282 p->g.pg, p->g.pl, p->g.sec);
3b2a3ad1
JG
1283 } else {
1284 pr_err("ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
1285 msg, error,
1286 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
a4bd217b
JG
1287 }
1288}
1289
1290static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1291 int error)
1292{
1293 int bit = -1;
1294
1295 if (rqd->nr_ppas == 1) {
3b2a3ad1 1296 print_ppa(&pblk->dev->geo, &rqd->ppa_addr, "rqd", error);
a4bd217b
JG
1297 return;
1298 }
1299
1300 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1301 bit + 1)) < rqd->nr_ppas) {
3b2a3ad1 1302 print_ppa(&pblk->dev->geo, &rqd->ppa_list[bit], "rqd", error);
a4bd217b
JG
1303 }
1304
1305 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1306}
a4bd217b
JG
1307
1308static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1309 struct ppa_addr *ppas, int nr_ppas)
1310{
1311 struct nvm_geo *geo = &tgt_dev->geo;
1312 struct ppa_addr *ppa;
1313 int i;
1314
1315 for (i = 0; i < nr_ppas; i++) {
1316 ppa = &ppas[i];
1317
3b2a3ad1
JG
1318 if (geo->version == NVM_OCSSD_SPEC_12) {
1319 if (!ppa->c.is_cached &&
1320 ppa->g.ch < geo->num_ch &&
1321 ppa->g.lun < geo->num_lun &&
1322 ppa->g.pl < geo->num_pln &&
1323 ppa->g.blk < geo->num_chk &&
1324 ppa->g.pg < geo->num_pg &&
1325 ppa->g.sec < geo->ws_min)
1326 continue;
1327 } else {
1328 if (!ppa->c.is_cached &&
1329 ppa->m.grp < geo->num_ch &&
1330 ppa->m.pu < geo->num_lun &&
1331 ppa->m.chk < geo->num_chk &&
1332 ppa->m.sec < geo->clba)
1333 continue;
1334 }
1335
1336 print_ppa(geo, ppa, "boundary", i);
1a94b2d4 1337
a4bd217b
JG
1338 return 1;
1339 }
1340 return 0;
1341}
1342
1a94b2d4
JG
1343static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1344{
1345 struct nvm_tgt_dev *dev = pblk->dev;
1346 struct ppa_addr *ppa_list;
1347
1348 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1349
1350 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1351 WARN_ON(1);
1352 return -EINVAL;
1353 }
1354
1355 if (rqd->opcode == NVM_OP_PWRITE) {
1356 struct pblk_line *line;
1357 struct ppa_addr ppa;
1358 int i;
1359
1360 for (i = 0; i < rqd->nr_ppas; i++) {
1361 ppa = ppa_list[i];
b1bcfda1 1362 line = &pblk->lines[pblk_ppa_to_line(ppa)];
1a94b2d4
JG
1363
1364 spin_lock(&line->lock);
1365 if (line->state != PBLK_LINESTATE_OPEN) {
1366 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1367 line->id, line->state);
1368 WARN_ON(1);
1369 spin_unlock(&line->lock);
1370 return -EINVAL;
1371 }
1372 spin_unlock(&line->lock);
1373 }
1374 }
1375
1376 return 0;
1377}
1378#endif
1379
a4bd217b
JG
1380static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1381{
1382 struct pblk_line_meta *lm = &pblk->lm;
1383
1384 if (paddr > lm->sec_per_line)
1385 return 1;
1386
1387 return 0;
1388}
1389
1390static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1391{
1392 return bio->bi_iter.bi_idx;
1393}
1394
1395static inline sector_t pblk_get_lba(struct bio *bio)
1396{
1397 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1398}
1399
1400static inline unsigned int pblk_get_secs(struct bio *bio)
1401{
1402 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1403}
1404
a4bd217b
JG
1405static inline void pblk_setup_uuid(struct pblk *pblk)
1406{
1407 uuid_le uuid;
1408
1409 uuid_le_gen(&uuid);
1410 memcpy(pblk->instance_uuid, uuid.b, 16);
1411}
1412#endif /* PBLK_H_ */