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