1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
44
45#define PBLK_COMMAND_TIMEOUT_MS 30000
46
47
48#define PBLK_MAX_LUNS_BITMAP (4)
49
50#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
51
52#define pblk_for_each_lun(pblk, rlun, i) \
53 for ((i) = 0, rlun = &(pblk)->luns[0]; \
54 (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)])
55
56#define ERASE 2
57
58enum {
59
60 PBLK_IOTYPE_USER = 1 << 0,
61 PBLK_IOTYPE_GC = 1 << 1,
62
63
64 PBLK_FLUSH_ENTRY = 1 << 2,
65 PBLK_WRITTEN_DATA = 1 << 3,
66 PBLK_SUBMITTED_ENTRY = 1 << 4,
67 PBLK_WRITABLE_ENTRY = 1 << 5,
68};
69
70enum {
71 PBLK_BLK_ST_OPEN = 0x1,
72 PBLK_BLK_ST_CLOSED = 0x2,
73};
74
75
76
77
78#define PBLK_NR_GC_LISTS 3
79#define PBLK_MAX_GC_JOBS 32
80
81enum {
82 PBLK_RL_HIGH = 1,
83 PBLK_RL_MID = 2,
84 PBLK_RL_LOW = 3,
85};
86
87struct pblk_sec_meta {
88 u64 reserved;
89 __le64 lba;
90};
91
92#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
93
94
95struct pblk_c_ctx {
96 struct list_head list;
97
98 unsigned long *lun_bitmap;
99 unsigned int sentry;
100 unsigned int nr_valid;
101 unsigned int nr_padded;
102};
103
104
105struct pblk_r_ctx {
106 struct bio *orig_bio;
107};
108
109
110struct pblk_rec_ctx {
111 struct pblk *pblk;
112 struct nvm_rq *rqd;
113 struct list_head failed;
114 struct work_struct ws_rec;
115};
116
117
118struct pblk_w_ctx {
119 struct bio_list bios;
120
121
122 u64 lba;
123 struct ppa_addr ppa;
124 int flags;
125};
126
127struct pblk_rb_entry {
128 struct ppa_addr cacheline;
129 void *data;
130 struct pblk_w_ctx w_ctx;
131 struct list_head index;
132};
133
134#define EMPTY_ENTRY (~0U)
135
136struct pblk_rb_pages {
137 struct page *pages;
138 int order;
139 struct list_head list;
140};
141
142struct pblk_rb {
143 struct pblk_rb_entry *entries;
144 unsigned int mem;
145
146
147 unsigned int subm;
148
149
150
151 unsigned int sync;
152
153
154
155 unsigned int sync_point;
156
157
158
159 unsigned int l2p_update;
160
161
162
163
164 unsigned int nr_entries;
165
166
167 unsigned int seg_size;
168
169
170
171
172 struct list_head pages;
173
174 spinlock_t w_lock;
175 spinlock_t s_lock;
176
177#ifdef CONFIG_NVM_DEBUG
178 atomic_t inflight_sync_point;
179#endif
180};
181
182#define PBLK_RECOVERY_SECTORS 16
183
184struct pblk_lun {
185 struct ppa_addr bppa;
186
187 u8 *bb_list;
188
189
190
191
192 struct semaphore wr_sem;
193};
194
195struct pblk_gc_rq {
196 struct pblk_line *line;
197 void *data;
198 u64 *lba_list;
199 int nr_secs;
200 int secs_to_gc;
201 struct list_head list;
202};
203
204struct pblk_gc {
205 int gc_active;
206 int gc_enabled;
207 int gc_forced;
208 int gc_jobs_active;
209 atomic_t inflight_gc;
210
211 struct task_struct *gc_ts;
212 struct task_struct *gc_writer_ts;
213 struct workqueue_struct *gc_reader_wq;
214 struct timer_list gc_timer;
215
216 int w_entries;
217 struct list_head w_list;
218
219 spinlock_t lock;
220 spinlock_t w_lock;
221};
222
223struct pblk_rl {
224 unsigned int high;
225
226
227 unsigned int low;
228
229
230 unsigned int high_pw;
231
232#define PBLK_USER_HIGH_THRS 2
233
234
235#define PBLK_USER_LOW_THRS 20
236
237 int rb_windows_pw;
238
239
240
241
242
243
244
245 int rb_budget;
246 int rb_user_max;
247 atomic_t rb_user_cnt;
248 int rb_gc_max;
249 int rb_gc_rsv;
250 int rb_state;
251 atomic_t rb_gc_cnt;
252
253 int rb_user_active;
254 struct timer_list u_timer;
255
256 unsigned long long nr_secs;
257 unsigned long total_blocks;
258 atomic_t free_blocks;
259};
260
261#define PBLK_LINE_NR_LUN_BITMAP 2
262#define PBLK_LINE_NR_SEC_BITMAP 2
263#define PBLK_LINE_EMPTY (~0U)
264
265enum {
266
267 PBLK_LINETYPE_FREE = 0,
268 PBLK_LINETYPE_LOG = 1,
269 PBLK_LINETYPE_DATA = 2,
270
271
272 PBLK_LINESTATE_FREE = 10,
273 PBLK_LINESTATE_OPEN = 11,
274 PBLK_LINESTATE_CLOSED = 12,
275 PBLK_LINESTATE_GC = 13,
276 PBLK_LINESTATE_BAD = 14,
277 PBLK_LINESTATE_CORRUPT = 15,
278
279
280 PBLK_LINEGC_NONE = 20,
281 PBLK_LINEGC_EMPTY = 21,
282 PBLK_LINEGC_LOW = 22,
283 PBLK_LINEGC_MID = 23,
284 PBLK_LINEGC_HIGH = 24,
285 PBLK_LINEGC_FULL = 25,
286};
287
288#define PBLK_MAGIC 0x70626c6b
289
290struct line_header {
291 __le32 crc;
292 __le32 identifier;
293 __u8 uuid[16];
294 __le16 type;
295 __le16 version;
296 __le32 id;
297};
298
299struct line_smeta {
300 struct line_header header;
301
302 __le32 crc;
303
304 __le32 prev_id;
305
306
307 __le64 seq_nr;
308
309
310 __le32 window_wr_lun;
311
312 __le32 rsvd[2];
313};
314
315
316
317
318
319
320
321
322
323
324struct line_emeta {
325 struct line_header header;
326
327 __le32 crc;
328
329
330 __le32 prev_id;
331
332
333 __le64 seq_nr;
334
335
336 __le32 window_wr_lun;
337
338
339 __le32 next_id;
340 __le64 nr_lbas;
341 __le64 nr_valid_lbas;
342};
343
344struct pblk_line {
345 struct pblk *pblk;
346 unsigned int id;
347
348
349 unsigned int seq_nr;
350
351 int state;
352 int type;
353 int gc_group;
354 struct list_head list;
355
356 unsigned long *lun_bitmap;
357
358 struct line_smeta *smeta;
359 struct line_emeta *emeta;
360 int meta_line;
361 u64 smeta_ssec;
362 u64 emeta_ssec;
363
364 unsigned int sec_in_line;
365
366 atomic_t blk_in_line;
367 unsigned long *blk_bitmap;
368 unsigned long *erase_bitmap;
369
370 unsigned long *map_bitmap;
371 unsigned long *invalid_bitmap;
372
373 atomic_t left_eblks;
374 atomic_t left_seblks;
375
376 int left_msecs;
377 int left_ssecs;
378 unsigned int cur_sec;
379 unsigned int vsc;
380
381 struct kref ref;
382
383 spinlock_t lock;
384};
385
386#define PBLK_DATA_LINES 4
387
388enum{
389 PBLK_KMALLOC_META = 1,
390 PBLK_VMALLOC_META = 2,
391};
392
393struct pblk_line_metadata {
394 void *meta;
395};
396
397struct pblk_line_mgmt {
398 int nr_lines;
399 int nr_free_lines;
400
401
402 struct list_head free_list;
403 struct list_head corrupt_list;
404 struct list_head bad_list;
405
406
407 struct list_head *gc_lists[PBLK_NR_GC_LISTS];
408 struct list_head gc_high_list;
409 struct list_head gc_mid_list;
410 struct list_head gc_low_list;
411
412 struct list_head gc_full_list;
413 struct list_head gc_empty_list;
414
415 struct pblk_line *log_line;
416 struct pblk_line *data_line;
417 struct pblk_line *log_next;
418 struct pblk_line *data_next;
419
420
421 int smeta_alloc_type;
422 int emeta_alloc_type;
423
424
425 struct pblk_line_metadata sline_meta[PBLK_DATA_LINES];
426 struct pblk_line_metadata eline_meta[PBLK_DATA_LINES];
427 unsigned long meta_bitmap;
428
429
430 unsigned long *bb_template;
431 unsigned long *bb_aux;
432
433 unsigned long d_seq_nr;
434 unsigned long l_seq_nr;
435
436 spinlock_t free_lock;
437 spinlock_t gc_lock;
438};
439
440struct pblk_line_meta {
441 unsigned int smeta_len;
442 unsigned int smeta_sec;
443 unsigned int emeta_len;
444 unsigned int emeta_sec;
445 unsigned int emeta_bb;
446 unsigned int sec_bitmap_len;
447 unsigned int blk_bitmap_len;
448 unsigned int lun_bitmap_len;
449
450 unsigned int blk_per_line;
451 unsigned int sec_per_line;
452 unsigned int min_blk_line;
453
454 unsigned int mid_thrs;
455 unsigned int high_thrs;
456};
457
458struct pblk_addr_format {
459 u64 ch_mask;
460 u64 lun_mask;
461 u64 pln_mask;
462 u64 blk_mask;
463 u64 pg_mask;
464 u64 sec_mask;
465 u8 ch_offset;
466 u8 lun_offset;
467 u8 pln_offset;
468 u8 blk_offset;
469 u8 pg_offset;
470 u8 sec_offset;
471};
472
473struct pblk {
474 struct nvm_tgt_dev *dev;
475 struct gendisk *disk;
476
477 struct kobject kobj;
478
479 struct pblk_lun *luns;
480
481 struct pblk_line *lines;
482 struct pblk_line_mgmt l_mg;
483 struct pblk_line_meta lm;
484
485 int ppaf_bitsize;
486 struct pblk_addr_format ppaf;
487
488 struct pblk_rb rwb;
489
490 int min_write_pgs;
491 int max_write_pgs;
492 int pgs_in_buffer;
493
494
495
496 sector_t capacity;
497 int over_pct;
498
499
500 struct pblk_rl rl;
501
502 struct semaphore erase_sem;
503
504 unsigned char instance_uuid[16];
505#ifdef CONFIG_NVM_DEBUG
506
507 atomic_long_t inflight_writes;
508 atomic_long_t padded_writes;
509 atomic_long_t padded_wb;
510 atomic_long_t nr_flush;
511 atomic_long_t req_writes;
512 atomic_long_t sub_writes;
513 atomic_long_t sync_writes;
514 atomic_long_t compl_writes;
515 atomic_long_t inflight_reads;
516 atomic_long_t sync_reads;
517 atomic_long_t recov_writes;
518 atomic_long_t recov_gc_writes;
519 atomic_long_t recov_gc_reads;
520#endif
521
522 spinlock_t lock;
523
524 atomic_long_t read_failed;
525 atomic_long_t read_empty;
526 atomic_long_t read_high_ecc;
527 atomic_long_t read_failed_gc;
528 atomic_long_t write_failed;
529 atomic_long_t erase_failed;
530
531 struct task_struct *writer_ts;
532
533
534
535
536
537 unsigned char *trans_map;
538 spinlock_t trans_lock;
539
540 struct list_head compl_list;
541
542 mempool_t *page_pool;
543 mempool_t *line_ws_pool;
544 mempool_t *rec_pool;
545 mempool_t *r_rq_pool;
546 mempool_t *w_rq_pool;
547 mempool_t *line_meta_pool;
548
549 struct workqueue_struct *kw_wq;
550 struct timer_list wtimer;
551
552 struct pblk_gc gc;
553};
554
555struct pblk_line_ws {
556 struct pblk *pblk;
557 struct pblk_line *line;
558 void *priv;
559 struct work_struct ws;
560};
561
562#define pblk_r_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_r_ctx))
563#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
564
565
566
567
568int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
569 unsigned int power_size, unsigned int power_seg_sz);
570unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
571void *pblk_rb_entries_ref(struct pblk_rb *rb);
572int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
573 unsigned int nr_entries, unsigned int *pos);
574int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
575 unsigned int *pos);
576void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
577 struct pblk_w_ctx w_ctx, unsigned int pos);
578void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
579 struct pblk_w_ctx w_ctx, struct pblk_line *gc_line,
580 unsigned int pos);
581struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
582
583void pblk_rb_sync_l2p(struct pblk_rb *rb);
584unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct bio *bio,
585 struct pblk_c_ctx *c_ctx,
586 unsigned int pos,
587 unsigned int nr_entries,
588 unsigned int count);
589unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
590 struct list_head *list,
591 unsigned int max);
592int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
593 u64 pos, int bio_iter);
594unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
595
596unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
597unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
598struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
599 struct ppa_addr *ppa);
600void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
601unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb);
602
603unsigned int pblk_rb_read_count(struct pblk_rb *rb);
604unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
605
606int pblk_rb_tear_down_check(struct pblk_rb *rb);
607int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
608void pblk_rb_data_free(struct pblk_rb *rb);
609ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
610
611
612
613
614struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw);
615int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
616 struct pblk_c_ctx *c_ctx);
617void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw);
618void pblk_flush_writer(struct pblk *pblk);
619struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba);
620void pblk_discard(struct pblk *pblk, struct bio *bio);
621void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
622void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
623int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
624struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
625 unsigned int nr_secs, unsigned int len,
626 gfp_t gfp_mask);
627struct pblk_line *pblk_line_get(struct pblk *pblk);
628struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
629struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
630int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
631void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
632struct pblk_line *pblk_line_get_data(struct pblk *pblk);
633struct pblk_line *pblk_line_get_data_next(struct pblk *pblk);
634int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
635int pblk_line_is_full(struct pblk_line *line);
636void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
637void pblk_line_close_ws(struct work_struct *work);
638void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
639void pblk_line_mark_bb(struct work_struct *work);
640void pblk_line_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
641 void (*work)(struct work_struct *));
642u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
643int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
644int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line);
645int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
646void pblk_line_put(struct kref *ref);
647struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
648u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
649int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
650 unsigned long secs_to_flush);
651void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
652 unsigned long *lun_bitmap);
653void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
654 unsigned long *lun_bitmap);
655void pblk_end_bio_sync(struct bio *bio);
656void pblk_end_io_sync(struct nvm_rq *rqd);
657int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
658 int nr_pages);
659void pblk_map_pad_invalidate(struct pblk *pblk, struct pblk_line *line,
660 u64 paddr);
661void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
662 int nr_pages);
663void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
664void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
665void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
666 struct ppa_addr ppa);
667void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
668 struct ppa_addr ppa, struct ppa_addr entry_line);
669int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
670 struct pblk_line *gc_line);
671void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
672 u64 *lba_list, int nr_secs);
673void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
674 sector_t blba, int nr_secs);
675
676
677
678
679int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
680 unsigned long flags);
681int pblk_write_gc_to_cache(struct pblk *pblk, void *data, u64 *lba_list,
682 unsigned int nr_entries, unsigned int nr_rec_entries,
683 struct pblk_line *gc_line, unsigned long flags);
684
685
686
687
688void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
689 unsigned int sentry, unsigned long *lun_bitmap,
690 unsigned int valid_secs, struct ppa_addr *erase_ppa);
691void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
692 unsigned long *lun_bitmap, unsigned int valid_secs,
693 unsigned int off);
694
695
696
697
698int pblk_write_ts(void *data);
699void pblk_write_timer_fn(unsigned long data);
700void pblk_write_should_kick(struct pblk *pblk);
701
702
703
704
705int pblk_submit_read(struct pblk *pblk, struct bio *bio);
706int pblk_submit_read_gc(struct pblk *pblk, u64 *lba_list, void *data,
707 unsigned int nr_secs, unsigned int *secs_to_gc,
708 struct pblk_line *line);
709
710
711
712void pblk_submit_rec(struct work_struct *work);
713struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
714void pblk_recov_pad(struct pblk *pblk);
715__le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta);
716int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
717 struct pblk_rec_ctx *recovery, u64 *comp_bits,
718 unsigned int comp);
719
720
721
722
723#define PBLK_GC_TRIES 3
724
725int pblk_gc_init(struct pblk *pblk);
726void pblk_gc_exit(struct pblk *pblk);
727void pblk_gc_should_start(struct pblk *pblk);
728void pblk_gc_should_stop(struct pblk *pblk);
729int pblk_gc_status(struct pblk *pblk);
730void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
731 int *gc_active);
732void pblk_gc_sysfs_force(struct pblk *pblk, int force);
733
734
735
736
737void pblk_rl_init(struct pblk_rl *rl, int budget);
738void pblk_rl_free(struct pblk_rl *rl);
739int pblk_rl_gc_thrs(struct pblk_rl *rl);
740unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
741int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
742void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
743int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
744void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
745void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
746void pblk_rl_set_gc_rsc(struct pblk_rl *rl, int rsv);
747int pblk_rl_sysfs_rate_show(struct pblk_rl *rl);
748void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
749void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line);
750
751
752
753
754int pblk_sysfs_init(struct gendisk *tdisk);
755void pblk_sysfs_exit(struct gendisk *tdisk);
756
757static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
758{
759 if (type == PBLK_KMALLOC_META)
760 return kmalloc(size, flags);
761 return vmalloc(size);
762}
763
764static inline void pblk_mfree(void *ptr, int type)
765{
766 if (type == PBLK_KMALLOC_META)
767 kfree(ptr);
768 else
769 vfree(ptr);
770}
771
772static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
773{
774 return c_ctx - sizeof(struct nvm_rq);
775}
776
777static inline void *pblk_line_emeta_to_lbas(struct line_emeta *emeta)
778{
779 return (emeta) + 1;
780}
781
782#define NVM_MEM_PAGE_WRITE (8)
783
784static inline int pblk_pad_distance(struct pblk *pblk)
785{
786 struct nvm_tgt_dev *dev = pblk->dev;
787 struct nvm_geo *geo = &dev->geo;
788
789 return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl;
790}
791
792static inline int pblk_dev_ppa_to_line(struct ppa_addr p)
793{
794 return p.g.blk;
795}
796
797static inline int pblk_tgt_ppa_to_line(struct ppa_addr p)
798{
799 return p.g.blk;
800}
801
802static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
803{
804 return p.g.lun * geo->nr_chnls + p.g.ch;
805}
806
807
808static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
809{
810 return p.g.lun * geo->nr_chnls + p.g.ch;
811}
812
813static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
814{
815 struct ppa_addr ppa64;
816
817 ppa64.ppa = 0;
818
819 if (ppa32 == -1) {
820 ppa64.ppa = ADDR_EMPTY;
821 } else if (ppa32 & (1U << 31)) {
822 ppa64.c.line = ppa32 & ((~0U) >> 1);
823 ppa64.c.is_cached = 1;
824 } else {
825 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
826 pblk->ppaf.blk_offset;
827 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
828 pblk->ppaf.pg_offset;
829 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
830 pblk->ppaf.lun_offset;
831 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
832 pblk->ppaf.ch_offset;
833 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
834 pblk->ppaf.pln_offset;
835 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
836 pblk->ppaf.sec_offset;
837 }
838
839 return ppa64;
840}
841
842static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
843 sector_t lba)
844{
845 struct ppa_addr ppa;
846
847 if (pblk->ppaf_bitsize < 32) {
848 u32 *map = (u32 *)pblk->trans_map;
849
850 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
851 } else {
852 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
853
854 ppa = map[lba];
855 }
856
857 return ppa;
858}
859
860static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
861{
862 u32 ppa32 = 0;
863
864 if (ppa64.ppa == ADDR_EMPTY) {
865 ppa32 = ~0U;
866 } else if (ppa64.c.is_cached) {
867 ppa32 |= ppa64.c.line;
868 ppa32 |= 1U << 31;
869 } else {
870 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
871 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
872 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
873 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
874 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
875 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
876 }
877
878 return ppa32;
879}
880
881static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
882 struct ppa_addr ppa)
883{
884 if (pblk->ppaf_bitsize < 32) {
885 u32 *map = (u32 *)pblk->trans_map;
886
887 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
888 } else {
889 u64 *map = (u64 *)pblk->trans_map;
890
891 map[lba] = ppa.ppa;
892 }
893}
894
895static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
896 struct ppa_addr p)
897{
898 u64 paddr;
899
900 paddr = 0;
901 paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset;
902 paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
903 paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
904 paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
905 paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
906
907 return paddr;
908}
909
910static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
911{
912 return (ppa_addr.ppa == ADDR_EMPTY);
913}
914
915static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
916{
917 ppa_addr->ppa = ADDR_EMPTY;
918}
919
920static inline int pblk_addr_in_cache(struct ppa_addr ppa)
921{
922 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
923}
924
925static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
926{
927 return ppa.c.line;
928}
929
930static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
931{
932 struct ppa_addr p;
933
934 p.c.line = addr;
935 p.c.is_cached = 1;
936
937 return p;
938}
939
940static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
941 u64 line_id)
942{
943 struct ppa_addr ppa;
944
945 ppa.ppa = 0;
946 ppa.g.blk = line_id;
947 ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
948 ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
949 ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
950 ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
951 ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
952
953 return ppa;
954}
955
956static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr,
957 u64 line_id)
958{
959 struct ppa_addr ppa;
960
961 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
962
963 return ppa;
964}
965
966static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
967 struct line_smeta *smeta)
968{
969 u32 crc = ~(u32)0;
970
971 crc = crc32_le(crc, (unsigned char *)smeta + sizeof(crc),
972 sizeof(struct line_header) - sizeof(crc));
973
974 return crc;
975}
976
977static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
978 struct line_smeta *smeta)
979{
980 struct pblk_line_meta *lm = &pblk->lm;
981 u32 crc = ~(u32)0;
982
983 crc = crc32_le(crc, (unsigned char *)smeta +
984 sizeof(struct line_header) + sizeof(crc),
985 lm->smeta_len -
986 sizeof(struct line_header) - sizeof(crc));
987
988 return crc;
989}
990
991static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
992 struct line_emeta *emeta)
993{
994 struct pblk_line_meta *lm = &pblk->lm;
995 u32 crc = ~(u32)0;
996
997 crc = crc32_le(crc, (unsigned char *)emeta +
998 sizeof(struct line_header) + sizeof(crc),
999 lm->emeta_len -
1000 sizeof(struct line_header) - sizeof(crc));
1001
1002 return crc;
1003}
1004
1005static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1006{
1007 struct nvm_tgt_dev *dev = pblk->dev;
1008 struct nvm_geo *geo = &dev->geo;
1009 int flags;
1010
1011 flags = geo->plane_mode >> 1;
1012
1013 if (type == WRITE)
1014 flags |= NVM_IO_SCRAMBLE_ENABLE;
1015
1016 return flags;
1017}
1018
1019static inline int pblk_set_read_mode(struct pblk *pblk)
1020{
1021 return NVM_IO_SNGL_ACCESS | NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1022}
1023
1024#ifdef CONFIG_NVM_DEBUG
1025static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1026{
1027 if (p->c.is_cached) {
1028 pr_err("ppa: (%s: %x) cache line: %llu\n",
1029 msg, error, (u64)p->c.line);
1030 } else {
1031 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1032 msg, error,
1033 p->g.ch, p->g.lun, p->g.blk,
1034 p->g.pg, p->g.pl, p->g.sec);
1035 }
1036}
1037
1038static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1039 int error)
1040{
1041 int bit = -1;
1042
1043 if (rqd->nr_ppas == 1) {
1044 print_ppa(&rqd->ppa_addr, "rqd", error);
1045 return;
1046 }
1047
1048 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1049 bit + 1)) < rqd->nr_ppas) {
1050 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1051 }
1052
1053 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1054}
1055#endif
1056
1057static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1058 struct ppa_addr *ppas, int nr_ppas)
1059{
1060 struct nvm_geo *geo = &tgt_dev->geo;
1061 struct ppa_addr *ppa;
1062 int i;
1063
1064 for (i = 0; i < nr_ppas; i++) {
1065 ppa = &ppas[i];
1066
1067 if (!ppa->c.is_cached &&
1068 ppa->g.ch < geo->nr_chnls &&
1069 ppa->g.lun < geo->luns_per_chnl &&
1070 ppa->g.pl < geo->nr_planes &&
1071 ppa->g.blk < geo->blks_per_lun &&
1072 ppa->g.pg < geo->pgs_per_blk &&
1073 ppa->g.sec < geo->sec_per_pg)
1074 continue;
1075
1076#ifdef CONFIG_NVM_DEBUG
1077 print_ppa(ppa, "boundary", i);
1078#endif
1079 return 1;
1080 }
1081 return 0;
1082}
1083
1084static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1085{
1086 struct pblk_line_meta *lm = &pblk->lm;
1087
1088 if (paddr > lm->sec_per_line)
1089 return 1;
1090
1091 return 0;
1092}
1093
1094static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1095{
1096 return bio->bi_iter.bi_idx;
1097}
1098
1099static inline sector_t pblk_get_lba(struct bio *bio)
1100{
1101 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1102}
1103
1104static inline unsigned int pblk_get_secs(struct bio *bio)
1105{
1106 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1107}
1108
1109static inline sector_t pblk_get_sector(sector_t lba)
1110{
1111 return lba * NR_PHY_IN_LOG;
1112}
1113
1114static inline void pblk_setup_uuid(struct pblk *pblk)
1115{
1116 uuid_le uuid;
1117
1118 uuid_le_gen(&uuid);
1119 memcpy(pblk->instance_uuid, uuid.b, 16);
1120}
1121#endif
1122