blob: 141036bd6afa1d118e4f773379123102611d66c0 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@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 * pblk-init.c - pblk's initialization.
19 */
20
21#include "pblk.h"
22
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020023static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +020024 *pblk_w_rq_cache;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025static DECLARE_RWSEM(pblk_lock);
NeilBrownb25d5232017-06-18 14:38:58 +100026struct bio_set *pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027
28static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
29 struct bio *bio)
30{
31 int ret;
32
33 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
34 * constraint. Writes can be of arbitrary size.
35 */
36 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100037 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020038 ret = pblk_submit_read(pblk, bio);
39 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
40 bio_put(bio);
41
42 return ret;
43 }
44
45 /* Prevent deadlock in the case of a modest LUN configuration and large
46 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
47 * available for user I/O.
48 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020049 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100050 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051
52 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
53}
54
55static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
56{
57 struct pblk *pblk = q->queuedata;
58
59 if (bio_op(bio) == REQ_OP_DISCARD) {
60 pblk_discard(pblk, bio);
61 if (!(bio->bi_opf & REQ_PREFLUSH)) {
62 bio_endio(bio);
63 return BLK_QC_T_NONE;
64 }
65 }
66
67 switch (pblk_rw_io(q, pblk, bio)) {
68 case NVM_IO_ERR:
69 bio_io_error(bio);
70 break;
71 case NVM_IO_DONE:
72 bio_endio(bio);
73 break;
74 }
75
76 return BLK_QC_T_NONE;
77}
78
Hans Holmbergc5586192017-10-13 14:46:40 +020079static size_t pblk_trans_map_size(struct pblk *pblk)
80{
81 int entry_size = 8;
82
83 if (pblk->ppaf_bitsize < 32)
84 entry_size = 4;
85
86 return entry_size * pblk->rl.nr_secs;
87}
88
89#ifdef CONFIG_NVM_DEBUG
90static u32 pblk_l2p_crc(struct pblk *pblk)
91{
92 size_t map_size;
93 u32 crc = ~(u32)0;
94
95 map_size = pblk_trans_map_size(pblk);
96 crc = crc32_le(crc, pblk->trans_map, map_size);
97 return crc;
98}
99#endif
100
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200101static void pblk_l2p_free(struct pblk *pblk)
102{
103 vfree(pblk->trans_map);
104}
105
106static int pblk_l2p_init(struct pblk *pblk)
107{
108 sector_t i;
109 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200110 size_t map_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200111
Hans Holmbergc5586192017-10-13 14:46:40 +0200112 map_size = pblk_trans_map_size(pblk);
113 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200114 if (!pblk->trans_map)
115 return -ENOMEM;
116
117 pblk_ppa_set_empty(&ppa);
118
119 for (i = 0; i < pblk->rl.nr_secs; i++)
120 pblk_trans_map_set(pblk, i, ppa);
121
122 return 0;
123}
124
125static void pblk_rwb_free(struct pblk *pblk)
126{
127 if (pblk_rb_tear_down_check(&pblk->rwb))
128 pr_err("pblk: write buffer error on tear down\n");
129
130 pblk_rb_data_free(&pblk->rwb);
131 vfree(pblk_rb_entries_ref(&pblk->rwb));
132}
133
134static int pblk_rwb_init(struct pblk *pblk)
135{
136 struct nvm_tgt_dev *dev = pblk->dev;
137 struct nvm_geo *geo = &dev->geo;
138 struct pblk_rb_entry *entries;
139 unsigned long nr_entries;
140 unsigned int power_size, power_seg_sz;
141
142 nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
143
144 entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
145 if (!entries)
146 return -ENOMEM;
147
148 power_size = get_count_order(nr_entries);
149 power_seg_sz = get_count_order(geo->sec_size);
150
151 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
152}
153
154/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200155#define ADDR_POOL_SIZE 64
156
157static int pblk_set_ppaf(struct pblk *pblk)
158{
159 struct nvm_tgt_dev *dev = pblk->dev;
160 struct nvm_geo *geo = &dev->geo;
161 struct nvm_addr_format ppaf = geo->ppaf;
162 int power_len;
163
164 /* Re-calculate channel and lun format to adapt to configuration */
165 power_len = get_count_order(geo->nr_chnls);
166 if (1 << power_len != geo->nr_chnls) {
167 pr_err("pblk: supports only power-of-two channel config.\n");
168 return -EINVAL;
169 }
170 ppaf.ch_len = power_len;
171
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100172 power_len = get_count_order(geo->nr_luns);
173 if (1 << power_len != geo->nr_luns) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 pr_err("pblk: supports only power-of-two LUN config.\n");
175 return -EINVAL;
176 }
177 ppaf.lun_len = power_len;
178
179 pblk->ppaf.sec_offset = 0;
180 pblk->ppaf.pln_offset = ppaf.sect_len;
181 pblk->ppaf.ch_offset = pblk->ppaf.pln_offset + ppaf.pln_len;
182 pblk->ppaf.lun_offset = pblk->ppaf.ch_offset + ppaf.ch_len;
183 pblk->ppaf.pg_offset = pblk->ppaf.lun_offset + ppaf.lun_len;
184 pblk->ppaf.blk_offset = pblk->ppaf.pg_offset + ppaf.pg_len;
185 pblk->ppaf.sec_mask = (1ULL << ppaf.sect_len) - 1;
186 pblk->ppaf.pln_mask = ((1ULL << ppaf.pln_len) - 1) <<
187 pblk->ppaf.pln_offset;
188 pblk->ppaf.ch_mask = ((1ULL << ppaf.ch_len) - 1) <<
189 pblk->ppaf.ch_offset;
190 pblk->ppaf.lun_mask = ((1ULL << ppaf.lun_len) - 1) <<
191 pblk->ppaf.lun_offset;
192 pblk->ppaf.pg_mask = ((1ULL << ppaf.pg_len) - 1) <<
193 pblk->ppaf.pg_offset;
194 pblk->ppaf.blk_mask = ((1ULL << ppaf.blk_len) - 1) <<
195 pblk->ppaf.blk_offset;
196
197 pblk->ppaf_bitsize = pblk->ppaf.blk_offset + ppaf.blk_len;
198
199 return 0;
200}
201
202static int pblk_init_global_caches(struct pblk *pblk)
203{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200204 down_write(&pblk_lock);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200205 pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206 sizeof(struct pblk_line_ws), 0, 0, NULL);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200207 if (!pblk_ws_cache) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208 up_write(&pblk_lock);
209 return -ENOMEM;
210 }
211
212 pblk_rec_cache = kmem_cache_create("pblk_rec",
213 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
214 if (!pblk_rec_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200215 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216 up_write(&pblk_lock);
217 return -ENOMEM;
218 }
219
Javier González084ec9b2017-06-26 16:27:13 -0600220 pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221 0, 0, NULL);
Javier González084ec9b2017-06-26 16:27:13 -0600222 if (!pblk_g_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200223 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224 kmem_cache_destroy(pblk_rec_cache);
225 up_write(&pblk_lock);
226 return -ENOMEM;
227 }
228
229 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
230 0, 0, NULL);
231 if (!pblk_w_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200232 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200233 kmem_cache_destroy(pblk_rec_cache);
Javier González084ec9b2017-06-26 16:27:13 -0600234 kmem_cache_destroy(pblk_g_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235 up_write(&pblk_lock);
236 return -ENOMEM;
237 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 up_write(&pblk_lock);
239
240 return 0;
241}
242
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200243static void pblk_free_global_caches(struct pblk *pblk)
244{
245 kmem_cache_destroy(pblk_ws_cache);
246 kmem_cache_destroy(pblk_rec_cache);
247 kmem_cache_destroy(pblk_g_rq_cache);
248 kmem_cache_destroy(pblk_w_rq_cache);
249}
250
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251static int pblk_core_init(struct pblk *pblk)
252{
253 struct nvm_tgt_dev *dev = pblk->dev;
254 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256 pblk->pgs_in_buffer = NVM_MEM_PAGE_WRITE * geo->sec_per_pg *
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100257 geo->nr_planes * geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200258
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259 if (pblk_init_global_caches(pblk))
260 return -ENOMEM;
261
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200262 /* Internal bios can be at most the sectors signaled by the device. */
Javier Gonzálezbd432412017-10-13 14:46:06 +0200263 pblk->page_bio_pool = mempool_create_page_pool(nvm_max_phys_sects(dev),
264 0);
265 if (!pblk->page_bio_pool)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200266 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200267
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200268 pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
269 pblk_ws_cache);
270 if (!pblk->gen_ws_pool)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200271 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200272
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100273 pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
274 pblk_rec_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200275 if (!pblk->rec_pool)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200276 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200277
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100278 pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier Gonzálezef576492017-06-26 11:57:28 +0200279 pblk_g_rq_cache);
Javier González0d880392017-10-13 14:46:08 +0200280 if (!pblk->r_rq_pool)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200281 goto free_rec_pool;
282
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100283 pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier González0d880392017-10-13 14:46:08 +0200284 pblk_g_rq_cache);
285 if (!pblk->e_rq_pool)
286 goto free_r_rq_pool;
287
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100288 pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier Gonzálezef576492017-06-26 11:57:28 +0200289 pblk_w_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200290 if (!pblk->w_rq_pool)
Javier González0d880392017-10-13 14:46:08 +0200291 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200292
Javier Gonzálezef576492017-06-26 11:57:28 +0200293 pblk->close_wq = alloc_workqueue("pblk-close-wq",
294 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
295 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200296 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297
Javier Gonzálezef576492017-06-26 11:57:28 +0200298 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
299 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
300 if (!pblk->bb_wq)
301 goto free_close_wq;
302
Javier González7bd4d372017-10-13 14:46:23 +0200303 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
304 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
305 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200306 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200307
Javier González7bd4d372017-10-13 14:46:23 +0200308 if (pblk_set_ppaf(pblk))
309 goto free_r_end_wq;
310
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200311 if (pblk_rwb_init(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200312 goto free_r_end_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313
314 INIT_LIST_HEAD(&pblk->compl_list);
315 return 0;
316
Javier González7bd4d372017-10-13 14:46:23 +0200317free_r_end_wq:
318 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200319free_bb_wq:
320 destroy_workqueue(pblk->bb_wq);
321free_close_wq:
322 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323free_w_rq_pool:
324 mempool_destroy(pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200325free_e_rq_pool:
326 mempool_destroy(pblk->e_rq_pool);
327free_r_rq_pool:
328 mempool_destroy(pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200329free_rec_pool:
330 mempool_destroy(pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200331free_gen_ws_pool:
332 mempool_destroy(pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200333free_page_bio_pool:
334 mempool_destroy(pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200335free_global_caches:
336 pblk_free_global_caches(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337 return -ENOMEM;
338}
339
340static void pblk_core_free(struct pblk *pblk)
341{
Javier Gonzálezef576492017-06-26 11:57:28 +0200342 if (pblk->close_wq)
343 destroy_workqueue(pblk->close_wq);
344
Javier González7bd4d372017-10-13 14:46:23 +0200345 if (pblk->r_end_wq)
346 destroy_workqueue(pblk->r_end_wq);
347
Javier Gonzálezef576492017-06-26 11:57:28 +0200348 if (pblk->bb_wq)
349 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200350
Javier Gonzálezbd432412017-10-13 14:46:06 +0200351 mempool_destroy(pblk->page_bio_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200352 mempool_destroy(pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200353 mempool_destroy(pblk->rec_pool);
Javier González0d880392017-10-13 14:46:08 +0200354 mempool_destroy(pblk->r_rq_pool);
355 mempool_destroy(pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200356 mempool_destroy(pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200357
Javier Gonzálezc6847e42018-01-05 14:16:18 +0100358 pblk_rwb_free(pblk);
359
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200360 pblk_free_global_caches(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200361}
362
363static void pblk_luns_free(struct pblk *pblk)
364{
365 kfree(pblk->luns);
366}
367
Javier Gonzáleze411b332018-03-30 00:04:59 +0200368static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369{
370 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
371 int i;
372
373 kfree(l_mg->bb_template);
374 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200375 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200376
377 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200378 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200379 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200380 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200381 }
382
383 kfree(pblk->lines);
384}
385
Javier Gonzáleze411b332018-03-30 00:04:59 +0200386static void pblk_line_meta_free(struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387{
Javier Gonzáleze411b332018-03-30 00:04:59 +0200388 kfree(line->blk_bitmap);
389 kfree(line->erase_bitmap);
390}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200391
Javier Gonzáleze411b332018-03-30 00:04:59 +0200392static void pblk_lines_free(struct pblk *pblk)
393{
394 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
395 struct pblk_line *line;
396 int i;
397
398 spin_lock(&l_mg->free_lock);
399 for (i = 0; i < l_mg->nr_lines; i++) {
400 line = &pblk->lines[i];
401
402 pblk_line_free(pblk, line);
403 pblk_line_meta_free(line);
404 }
405 spin_unlock(&l_mg->free_lock);
406}
407
408static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
409 u8 *blks, int nr_blks)
410{
411 struct ppa_addr ppa;
412 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200413
414 ppa.ppa = 0;
415 ppa.g.ch = rlun->bppa.g.ch;
416 ppa.g.lun = rlun->bppa.g.lun;
417
418 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
419 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200420 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200421
422 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200423 if (nr_blks < 0)
424 return -EIO;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425
Wei Yongjun5136a4f2017-04-25 16:15:30 +0000426 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200427}
428
Javier Gonzáleze411b332018-03-30 00:04:59 +0200429static void *pblk_bb_get_log(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430{
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200431 struct nvm_tgt_dev *dev = pblk->dev;
432 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200433 u8 *log;
434 int i, nr_blks, blk_per_lun;
435 int ret;
436
437 blk_per_lun = geo->nr_chks * geo->plane_mode;
438 nr_blks = blk_per_lun * geo->all_luns;
439
440 log = kmalloc(nr_blks, GFP_KERNEL);
441 if (!log)
442 return ERR_PTR(-ENOMEM);
443
444 for (i = 0; i < geo->all_luns; i++) {
445 struct pblk_lun *rlun = &pblk->luns[i];
446 u8 *log_pos = log + i * blk_per_lun;
447
448 ret = pblk_bb_get_tbl(dev, rlun, log_pos, blk_per_lun);
449 if (ret) {
450 kfree(log);
451 return ERR_PTR(-EIO);
452 }
453 }
454
455 return log;
456}
457
458static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line,
459 u8 *bb_log, int blk_per_line)
460{
461 struct nvm_tgt_dev *dev = pblk->dev;
462 struct nvm_geo *geo = &dev->geo;
463 int i, bb_cnt = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200465 for (i = 0; i < blk_per_line; i++) {
Javier Gonzáleze411b332018-03-30 00:04:59 +0200466 struct pblk_lun *rlun = &pblk->luns[i];
467 u8 *lun_bb_log = bb_log + i * blk_per_line;
468
469 if (lun_bb_log[line->id] == NVM_BLK_T_FREE)
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200470 continue;
471
472 set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap);
473 bb_cnt++;
474 }
475
476 return bb_cnt;
477}
478
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200479static int pblk_luns_init(struct pblk *pblk, struct ppa_addr *luns)
480{
481 struct nvm_tgt_dev *dev = pblk->dev;
482 struct nvm_geo *geo = &dev->geo;
483 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200484 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200485
486 /* TODO: Implement unbalanced LUN support */
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100487 if (geo->nr_luns < 0) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488 pr_err("pblk: unbalanced LUN config.\n");
489 return -EINVAL;
490 }
491
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100492 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
493 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 if (!pblk->luns)
495 return -ENOMEM;
496
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100497 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200498 /* Stripe across channels */
499 int ch = i % geo->nr_chnls;
500 int lun_raw = i / geo->nr_chnls;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100501 int lunid = lun_raw + ch * geo->nr_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502
503 rlun = &pblk->luns[i];
504 rlun->bppa = luns[lunid];
505
506 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507 }
508
509 return 0;
510}
511
512static int pblk_lines_configure(struct pblk *pblk, int flags)
513{
514 struct pblk_line *line = NULL;
515 int ret = 0;
516
517 if (!(flags & NVM_TARGET_FACTORY)) {
518 line = pblk_recov_l2p(pblk);
519 if (IS_ERR(line)) {
520 pr_err("pblk: could not recover l2p table\n");
521 ret = -EFAULT;
522 }
523 }
524
Hans Holmbergc5586192017-10-13 14:46:40 +0200525#ifdef CONFIG_NVM_DEBUG
526 pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
527#endif
528
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200529 /* Free full lines directly as GC has not been started yet */
530 pblk_gc_free_full_lines(pblk);
531
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200532 if (!line) {
533 /* Configure next line for user data */
534 line = pblk_line_get_first_data(pblk);
535 if (!line) {
536 pr_err("pblk: line list corrupted\n");
537 ret = -EFAULT;
538 }
539 }
540
541 return ret;
542}
543
544/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200545static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200546{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200547 struct pblk_line_meta *lm = &pblk->lm;
548 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
549 struct nvm_tgt_dev *dev = pblk->dev;
550 struct nvm_geo *geo = &dev->geo;
551
552 /* Round to sector size so that lba_list starts on its own sector */
553 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200554 sizeof(struct line_emeta) + lm->blk_bitmap_len +
555 sizeof(struct wa_counters), geo->sec_size);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200556 lm->emeta_len[1] = lm->emeta_sec[1] * geo->sec_size;
557
558 /* Round to sector size so that vsc_list starts on its own sector */
559 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
560 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
561 geo->sec_size);
562 lm->emeta_len[2] = lm->emeta_sec[2] * geo->sec_size;
563
564 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
565 geo->sec_size);
566 lm->emeta_len[3] = lm->emeta_sec[3] * geo->sec_size;
567
568 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
569
570 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571}
572
573static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
574{
575 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100576 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
577 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578 struct nvm_geo *geo = &dev->geo;
579 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100580 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200581
Javier Gonzáleze5392732018-01-05 14:16:14 +0100582 if (geo->op == NVM_TARGET_DEFAULT_OP)
583 pblk->op = PBLK_DEFAULT_OP;
584 else
585 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200586
587 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100588 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200589 sector_div(provisioned, 100);
590
Javier Gonzáleza7689932018-01-05 14:16:13 +0100591 pblk->op_blks = nr_free_blks - provisioned;
592
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 /* Internally pblk manages all free blocks, but all calculations based
594 * on user capacity consider only provisioned blocks
595 */
596 pblk->rl.total_blocks = nr_free_blks;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100597 pblk->rl.nr_secs = nr_free_blks * geo->sec_per_chk;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100598
599 /* Consider sectors used for metadata */
600 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
601 blk_meta = DIV_ROUND_UP(sec_meta, geo->sec_per_chk);
602
603 pblk->capacity = (provisioned - blk_meta) * geo->sec_per_chk;
604
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100606 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607}
608
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200609static int pblk_lines_alloc_metadata(struct pblk *pblk)
610{
611 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
612 struct pblk_line_meta *lm = &pblk->lm;
613 int i;
614
615 /* smeta is always small enough to fit on a kmalloc memory allocation,
616 * emeta depends on the number of LUNs allocated to the pblk instance
617 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200618 for (i = 0; i < PBLK_DATA_LINES; i++) {
619 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
620 if (!l_mg->sline_meta[i])
621 goto fail_free_smeta;
622 }
623
624 /* emeta allocates three different buffers for managing metadata with
625 * in-memory and in-media layouts
626 */
627 for (i = 0; i < PBLK_DATA_LINES; i++) {
628 struct pblk_emeta *emeta;
629
630 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
631 if (!emeta)
632 goto fail_free_emeta;
633
634 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
635 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
636
637 emeta->buf = vmalloc(lm->emeta_len[0]);
638 if (!emeta->buf) {
639 kfree(emeta);
640 goto fail_free_emeta;
641 }
642
643 emeta->nr_entries = lm->emeta_sec[0];
644 l_mg->eline_meta[i] = emeta;
645 } else {
646 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
647
648 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
649 if (!emeta->buf) {
650 kfree(emeta);
651 goto fail_free_emeta;
652 }
653
654 emeta->nr_entries = lm->emeta_sec[0];
655 l_mg->eline_meta[i] = emeta;
656 }
657 }
658
659 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
660 if (!l_mg->vsc_list)
661 goto fail_free_emeta;
662
663 for (i = 0; i < l_mg->nr_lines; i++)
664 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
665
666 return 0;
667
668fail_free_emeta:
669 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200670 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
671 vfree(l_mg->eline_meta[i]->buf);
672 else
673 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200674 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200675 }
676
677fail_free_smeta:
678 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200679 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200680
681 return -ENOMEM;
682}
683
Javier Gonzáleze411b332018-03-30 00:04:59 +0200684static int pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
685 void *chunk_log, long *nr_bad_blks)
686{
687 struct pblk_line_meta *lm = &pblk->lm;
688
689 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
690 if (!line->blk_bitmap)
691 return -ENOMEM;
692
693 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
694 if (!line->erase_bitmap) {
695 kfree(line->blk_bitmap);
696 return -ENOMEM;
697 }
698
699 *nr_bad_blks = pblk_bb_line(pblk, line, chunk_log, lm->blk_per_line);
700
701 return 0;
702}
703
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200704static int pblk_lines_init(struct pblk *pblk)
705{
706 struct nvm_tgt_dev *dev = pblk->dev;
707 struct nvm_geo *geo = &dev->geo;
708 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
709 struct pblk_line_meta *lm = &pblk->lm;
710 struct pblk_line *line;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200711 void *chunk_log;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200712 unsigned int smeta_len, emeta_len;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200713 long nr_bad_blks = 0, nr_free_blks = 0;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200714 int bb_distance, max_write_ppas, mod;
715 int i, ret;
716
717 pblk->min_write_pgs = geo->sec_per_pl * (geo->sec_size / PAGE_SIZE);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100718 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200719 pblk->max_write_pgs = (max_write_ppas < nvm_max_phys_sects(dev)) ?
720 max_write_ppas : nvm_max_phys_sects(dev);
721 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
722
723 if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
724 pr_err("pblk: cannot support device max_phys_sect\n");
725 return -EINVAL;
726 }
727
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100728 div_u64_rem(geo->sec_per_chk, pblk->min_write_pgs, &mod);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200729 if (mod) {
730 pr_err("pblk: bad configuration of sectors/pages\n");
731 return -EINVAL;
732 }
733
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100734 l_mg->nr_lines = geo->nr_chks;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200735 l_mg->log_line = l_mg->data_line = NULL;
736 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
737 l_mg->nr_free_lines = 0;
738 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200739
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100740 lm->sec_per_line = geo->sec_per_chk * geo->all_luns;
741 lm->blk_per_line = geo->all_luns;
742 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200743 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100744 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200745 lm->mid_thrs = lm->sec_per_line / 2;
746 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100747 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200748
749 /* Calculate necessary pages for smeta. See comment over struct
750 * line_smeta definition
751 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200752 i = 1;
753add_smeta_page:
754 lm->smeta_sec = i * geo->sec_per_pl;
755 lm->smeta_len = lm->smeta_sec * geo->sec_size;
756
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200757 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200758 if (smeta_len > lm->smeta_len) {
759 i++;
760 goto add_smeta_page;
761 }
762
763 /* Calculate necessary pages for emeta. See comment over struct
764 * line_emeta definition
765 */
766 i = 1;
767add_emeta_page:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200768 lm->emeta_sec[0] = i * geo->sec_per_pl;
769 lm->emeta_len[0] = lm->emeta_sec[0] * geo->sec_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200770
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200771 emeta_len = calc_emeta_len(pblk);
772 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200773 i++;
774 goto add_emeta_page;
775 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200776
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100777 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200778
779 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100780 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200781 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100782 lm->emeta_sec[0], geo->sec_per_chk);
Javier González21d22872017-10-13 14:46:26 +0200783
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200784 if (lm->min_blk_line > lm->blk_per_line) {
785 pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
786 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200787 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200788 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200790 ret = pblk_lines_alloc_metadata(pblk);
791 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200792 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200793
794 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200795 if (!l_mg->bb_template) {
796 ret = -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200797 goto fail_free_meta;
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200798 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200799
800 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200801 if (!l_mg->bb_aux) {
802 ret = -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200803 goto fail_free_bb_template;
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200804 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100806 bb_distance = (geo->all_luns) * geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200807 for (i = 0; i < lm->sec_per_line; i += bb_distance)
808 bitmap_set(l_mg->bb_template, i, geo->sec_per_pl);
809
810 INIT_LIST_HEAD(&l_mg->free_list);
811 INIT_LIST_HEAD(&l_mg->corrupt_list);
812 INIT_LIST_HEAD(&l_mg->bad_list);
813 INIT_LIST_HEAD(&l_mg->gc_full_list);
814 INIT_LIST_HEAD(&l_mg->gc_high_list);
815 INIT_LIST_HEAD(&l_mg->gc_mid_list);
816 INIT_LIST_HEAD(&l_mg->gc_low_list);
817 INIT_LIST_HEAD(&l_mg->gc_empty_list);
818
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200819 INIT_LIST_HEAD(&l_mg->emeta_list);
820
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200821 l_mg->gc_lists[0] = &l_mg->gc_high_list;
822 l_mg->gc_lists[1] = &l_mg->gc_mid_list;
823 l_mg->gc_lists[2] = &l_mg->gc_low_list;
824
825 spin_lock_init(&l_mg->free_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200826 spin_lock_init(&l_mg->close_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200827 spin_lock_init(&l_mg->gc_lock);
828
829 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
830 GFP_KERNEL);
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200831 if (!pblk->lines) {
832 ret = -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200833 goto fail_free_bb_aux;
Dan Carpenter1c6286f2017-04-15 20:55:53 +0200834 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200835
Javier Gonzáleze411b332018-03-30 00:04:59 +0200836 chunk_log = pblk_bb_get_log(pblk);
837 if (IS_ERR(chunk_log)) {
838 pr_err("pblk: could not get bad block log (%lu)\n",
839 PTR_ERR(chunk_log));
840 ret = PTR_ERR(chunk_log);
841 goto fail_free_bb_aux;
842 }
843
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200844 for (i = 0; i < l_mg->nr_lines; i++) {
Javier Gonzáleze411b332018-03-30 00:04:59 +0200845 int chk_in_line;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200846
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847 line = &pblk->lines[i];
848
849 line->pblk = pblk;
850 line->id = i;
851 line->type = PBLK_LINETYPE_FREE;
852 line->state = PBLK_LINESTATE_FREE;
853 line->gc_group = PBLK_LINEGC_NONE;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200854 line->vsc = &l_mg->vsc_list[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200855 spin_lock_init(&line->lock);
856
Javier Gonzáleze411b332018-03-30 00:04:59 +0200857 ret = pblk_setup_line_meta(pblk, line, chunk_log, &nr_bad_blks);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200858 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200859 goto fail_free_chunk_log;
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200860
Javier Gonzáleze411b332018-03-30 00:04:59 +0200861 chk_in_line = lm->blk_per_line - nr_bad_blks;
862 if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line ||
863 chk_in_line < lm->min_blk_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200864 line->state = PBLK_LINESTATE_BAD;
865 list_add_tail(&line->list, &l_mg->bad_list);
866 continue;
867 }
868
Javier Gonzáleze411b332018-03-30 00:04:59 +0200869 nr_free_blks += chk_in_line;
870 atomic_set(&line->blk_in_line, chk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200871
872 l_mg->nr_free_lines++;
873 list_add_tail(&line->list, &l_mg->free_list);
874 }
875
876 pblk_set_provision(pblk, nr_free_blks);
877
Javier Gonzáleze411b332018-03-30 00:04:59 +0200878 kfree(chunk_log);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200879 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200880
881fail_free_chunk_log:
882 kfree(chunk_log);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200883 while (--i >= 0)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200884 pblk_line_meta_free(&pblk->lines[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200885fail_free_bb_aux:
886 kfree(l_mg->bb_aux);
887fail_free_bb_template:
888 kfree(l_mg->bb_template);
889fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +0200890 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200891
892 return ret;
893}
894
895static int pblk_writer_init(struct pblk *pblk)
896{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200897 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
898 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +0100899 int err = PTR_ERR(pblk->writer_ts);
900
901 if (err != -EINTR)
902 pr_err("pblk: could not allocate writer kthread (%d)\n",
903 err);
904 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905 }
906
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +0100907 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
908 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
909
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200910 return 0;
911}
912
913static void pblk_writer_stop(struct pblk *pblk)
914{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200915 /* The pipeline must be stopped and the write buffer emptied before the
916 * write thread is stopped
917 */
918 WARN(pblk_rb_read_count(&pblk->rwb),
919 "Stopping not fully persisted write buffer\n");
920
921 WARN(pblk_rb_sync_count(&pblk->rwb),
922 "Stopping not fully synced write buffer\n");
923
Hans Holmberg7be970b2018-03-30 00:04:56 +0200924 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200925 if (pblk->writer_ts)
926 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927}
928
929static void pblk_free(struct pblk *pblk)
930{
931 pblk_luns_free(pblk);
932 pblk_lines_free(pblk);
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200933 kfree(pblk->pad_dist);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200934 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200935 pblk_core_free(pblk);
936 pblk_l2p_free(pblk);
937
938 kfree(pblk);
939}
940
941static void pblk_tear_down(struct pblk *pblk)
942{
Javier González588726d32017-06-26 11:57:29 +0200943 pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200944 pblk_writer_stop(pblk);
945 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200946 pblk_rl_free(&pblk->rl);
947
948 pr_debug("pblk: consistent tear down\n");
949}
950
951static void pblk_exit(void *private)
952{
953 struct pblk *pblk = private;
954
955 down_write(&pblk_lock);
956 pblk_gc_exit(pblk);
957 pblk_tear_down(pblk);
Hans Holmbergc5586192017-10-13 14:46:40 +0200958
959#ifdef CONFIG_NVM_DEBUG
960 pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
961#endif
962
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200963 pblk_free(pblk);
964 up_write(&pblk_lock);
965}
966
967static sector_t pblk_capacity(void *private)
968{
969 struct pblk *pblk = private;
970
971 return pblk->capacity * NR_PHY_IN_LOG;
972}
973
974static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
975 int flags)
976{
977 struct nvm_geo *geo = &dev->geo;
978 struct request_queue *bqueue = dev->q;
979 struct request_queue *tqueue = tdisk->queue;
980 struct pblk *pblk;
981 int ret;
982
983 if (dev->identity.dom & NVM_RSP_L2P) {
Rakesh Pandit4e76af52017-10-13 14:45:57 +0200984 pr_err("pblk: host-side L2P table not supported. (%x)\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985 dev->identity.dom);
986 return ERR_PTR(-EINVAL);
987 }
988
989 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
990 if (!pblk)
991 return ERR_PTR(-ENOMEM);
992
993 pblk->dev = dev;
994 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +0200995 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200996 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200997
998 spin_lock_init(&pblk->trans_lock);
999 spin_lock_init(&pblk->lock);
1000
1001 if (flags & NVM_TARGET_FACTORY)
1002 pblk_setup_uuid(pblk);
1003
Hans Holmberg76758392018-03-30 00:04:52 +02001004 atomic64_set(&pblk->user_wa, 0);
1005 atomic64_set(&pblk->pad_wa, 0);
1006 atomic64_set(&pblk->gc_wa, 0);
1007 pblk->user_rst_wa = 0;
1008 pblk->pad_rst_wa = 0;
1009 pblk->gc_rst_wa = 0;
1010
Hans Holmberg5d149bf2018-03-30 00:04:55 +02001011 atomic64_set(&pblk->nr_flush, 0);
1012 pblk->nr_flush_rst = 0;
1013
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001014#ifdef CONFIG_NVM_DEBUG
1015 atomic_long_set(&pblk->inflight_writes, 0);
1016 atomic_long_set(&pblk->padded_writes, 0);
1017 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001018 atomic_long_set(&pblk->req_writes, 0);
1019 atomic_long_set(&pblk->sub_writes, 0);
1020 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001021 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001022 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001023 atomic_long_set(&pblk->sync_reads, 0);
1024 atomic_long_set(&pblk->recov_writes, 0);
1025 atomic_long_set(&pblk->recov_writes, 0);
1026 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001027 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001028#endif
1029
1030 atomic_long_set(&pblk->read_failed, 0);
1031 atomic_long_set(&pblk->read_empty, 0);
1032 atomic_long_set(&pblk->read_high_ecc, 0);
1033 atomic_long_set(&pblk->read_failed_gc, 0);
1034 atomic_long_set(&pblk->write_failed, 0);
1035 atomic_long_set(&pblk->erase_failed, 0);
1036
1037 ret = pblk_luns_init(pblk, dev->luns);
1038 if (ret) {
1039 pr_err("pblk: could not initialize luns\n");
1040 goto fail;
1041 }
1042
1043 ret = pblk_lines_init(pblk);
1044 if (ret) {
1045 pr_err("pblk: could not initialize lines\n");
1046 goto fail_free_luns;
1047 }
1048
Hans Holmberg5d149bf2018-03-30 00:04:55 +02001049 pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
1050 GFP_KERNEL);
1051 if (!pblk->pad_dist) {
1052 ret = -ENOMEM;
1053 goto fail_free_line_meta;
1054 }
1055
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001056 ret = pblk_core_init(pblk);
1057 if (ret) {
1058 pr_err("pblk: could not initialize core\n");
Hans Holmberg5d149bf2018-03-30 00:04:55 +02001059 goto fail_free_pad_dist;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001060 }
1061
1062 ret = pblk_l2p_init(pblk);
1063 if (ret) {
1064 pr_err("pblk: could not initialize maps\n");
1065 goto fail_free_core;
1066 }
1067
1068 ret = pblk_lines_configure(pblk, flags);
1069 if (ret) {
1070 pr_err("pblk: could not configure lines\n");
1071 goto fail_free_l2p;
1072 }
1073
1074 ret = pblk_writer_init(pblk);
1075 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001076 if (ret != -EINTR)
1077 pr_err("pblk: could not initialize write thread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001078 goto fail_free_lines;
1079 }
1080
1081 ret = pblk_gc_init(pblk);
1082 if (ret) {
1083 pr_err("pblk: could not initialize gc\n");
1084 goto fail_stop_writer;
1085 }
1086
1087 /* inherit the size from the underlying device */
1088 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1089 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1090
1091 blk_queue_write_cache(tqueue, true, false);
1092
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001093 tqueue->limits.discard_granularity = geo->sec_per_chk * geo->sec_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001094 tqueue->limits.discard_alignment = 0;
1095 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001096 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001097
Javier González30d82a862018-01-05 14:16:19 +01001098 pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1099 tdisk->disk_name,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001100 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001101 (unsigned long long)pblk->rl.nr_secs,
1102 pblk->rwb.nr_entries);
1103
1104 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001105
1106 /* Check if we need to start GC */
1107 pblk_gc_should_kick(pblk);
1108
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001109 return pblk;
1110
1111fail_stop_writer:
1112 pblk_writer_stop(pblk);
1113fail_free_lines:
1114 pblk_lines_free(pblk);
1115fail_free_l2p:
1116 pblk_l2p_free(pblk);
1117fail_free_core:
1118 pblk_core_free(pblk);
Hans Holmberg5d149bf2018-03-30 00:04:55 +02001119fail_free_pad_dist:
1120 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001121fail_free_line_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001122 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123fail_free_luns:
1124 pblk_luns_free(pblk);
1125fail:
1126 kfree(pblk);
1127 return ERR_PTR(ret);
1128}
1129
1130/* physical block device target */
1131static struct nvm_tgt_type tt_pblk = {
1132 .name = "pblk",
1133 .version = {1, 0, 0},
1134
1135 .make_rq = pblk_make_rq,
1136 .capacity = pblk_capacity,
1137
1138 .init = pblk_init,
1139 .exit = pblk_exit,
1140
1141 .sysfs_init = pblk_sysfs_init,
1142 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001143 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001144};
1145
1146static int __init pblk_module_init(void)
1147{
NeilBrownb25d5232017-06-18 14:38:58 +10001148 int ret;
1149
1150 pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
1151 if (!pblk_bio_set)
1152 return -ENOMEM;
1153 ret = nvm_register_tgt_type(&tt_pblk);
1154 if (ret)
1155 bioset_free(pblk_bio_set);
1156 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001157}
1158
1159static void pblk_module_exit(void)
1160{
NeilBrownb25d5232017-06-18 14:38:58 +10001161 bioset_free(pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001162 nvm_unregister_tgt_type(&tt_pblk);
1163}
1164
1165module_init(pblk_module_init);
1166module_exit(pblk_module_exit);
1167MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1168MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1169MODULE_LICENSE("GPL v2");
1170MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");