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