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