Merge branch 'linux-4.12' of git://github.com/skeggsb/linux into drm-fixes
[linux-2.6-block.git] / drivers / lightnvm / pblk-map.c
CommitLineData
a4bd217b
JG
1/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-map.c - pblk's lba-ppa mapping strategy
16 *
17 */
18
19#include "pblk.h"
20
21static void pblk_map_page_data(struct pblk *pblk, unsigned int sentry,
22 struct ppa_addr *ppa_list,
23 unsigned long *lun_bitmap,
24 struct pblk_sec_meta *meta_list,
25 unsigned int valid_secs)
26{
27 struct pblk_line *line = pblk_line_get_data(pblk);
28 struct line_emeta *emeta = line->emeta;
29 struct pblk_w_ctx *w_ctx;
30 __le64 *lba_list = pblk_line_emeta_to_lbas(emeta);
31 u64 paddr;
32 int nr_secs = pblk->min_write_pgs;
33 int i;
34
35 paddr = pblk_alloc_page(pblk, line, nr_secs);
36
37 for (i = 0; i < nr_secs; i++, paddr++) {
38 /* ppa to be sent to the device */
39 ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
40
41 /* Write context for target bio completion on write buffer. Note
42 * that the write buffer is protected by the sync backpointer,
43 * and a single writer thread have access to each specific entry
44 * at a time. Thus, it is safe to modify the context for the
45 * entry we are setting up for submission without taking any
46 * lock or memory barrier.
47 */
48 if (i < valid_secs) {
49 kref_get(&line->ref);
50 w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i);
51 w_ctx->ppa = ppa_list[i];
52 meta_list[i].lba = cpu_to_le64(w_ctx->lba);
53 lba_list[paddr] = cpu_to_le64(w_ctx->lba);
54 le64_add_cpu(&line->emeta->nr_valid_lbas, 1);
55 } else {
56 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
57 lba_list[paddr] = cpu_to_le64(ADDR_EMPTY);
58 pblk_map_pad_invalidate(pblk, line, paddr);
59 }
60 }
61
62 if (pblk_line_is_full(line)) {
63 line = pblk_line_replace_data(pblk);
64 if (!line)
65 return;
66 }
67
68 pblk_down_rq(pblk, ppa_list, nr_secs, lun_bitmap);
69}
70
71void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
72 unsigned long *lun_bitmap, unsigned int valid_secs,
73 unsigned int off)
74{
75 struct pblk_sec_meta *meta_list = rqd->meta_list;
76 unsigned int map_secs;
77 int min = pblk->min_write_pgs;
78 int i;
79
80 for (i = off; i < rqd->nr_ppas; i += min) {
81 map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
82 pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i],
83 lun_bitmap, &meta_list[i], map_secs);
84 }
85}
86
87/* only if erase_ppa is set, acquire erase semaphore */
88void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
89 unsigned int sentry, unsigned long *lun_bitmap,
90 unsigned int valid_secs, struct ppa_addr *erase_ppa)
91{
92 struct nvm_tgt_dev *dev = pblk->dev;
93 struct nvm_geo *geo = &dev->geo;
94 struct pblk_line *e_line = pblk_line_get_data_next(pblk);
95 struct pblk_sec_meta *meta_list = rqd->meta_list;
96 unsigned int map_secs;
97 int min = pblk->min_write_pgs;
98 int i, erase_lun;
99
100 for (i = 0; i < rqd->nr_ppas; i += min) {
101 map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
102 pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i],
103 lun_bitmap, &meta_list[i], map_secs);
104
105 erase_lun = rqd->ppa_list[i].g.lun * geo->nr_chnls +
106 rqd->ppa_list[i].g.ch;
107
108 if (!test_bit(erase_lun, e_line->erase_bitmap)) {
109 if (down_trylock(&pblk->erase_sem))
110 continue;
111
112 set_bit(erase_lun, e_line->erase_bitmap);
a44f53fa 113 atomic_dec(&e_line->left_eblks);
a4bd217b
JG
114 *erase_ppa = rqd->ppa_list[i];
115 erase_ppa->g.blk = e_line->id;
116
117 /* Avoid evaluating e_line->left_eblks */
118 return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
119 valid_secs, i + min);
120 }
121 }
122
123 /* Erase blocks that are bad in this line but might not be in next */
124 if (unlikely(ppa_empty(*erase_ppa))) {
125 struct pblk_line_meta *lm = &pblk->lm;
126
127 i = find_first_zero_bit(e_line->erase_bitmap, lm->blk_per_line);
128 if (i == lm->blk_per_line)
129 return;
130
131 set_bit(i, e_line->erase_bitmap);
a44f53fa 132 atomic_dec(&e_line->left_eblks);
a4bd217b
JG
133 *erase_ppa = pblk->luns[i].bppa; /* set ch and lun */
134 erase_ppa->g.blk = e_line->id;
135 }
136}