blob: 81e8ed4d31eab1538c330ec7c3a6d818941e0803 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
4 * Copyright (C) 2016 CNEX Labs
5 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
6 * Matias Bjorling <matias@cnexlabs.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * Implementation of a physical block-device target for Open-channel SSDs.
18 *
19 * pblk-init.c - pblk's initialization.
20 */
21
22#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020023#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020024
Wei Yongjun21ff1392018-06-05 09:15:12 +000025static unsigned int write_buffer_size;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +020026
27module_param(write_buffer_size, uint, 0644);
28MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
29
Hans Holmberg1864de92018-10-09 13:11:58 +020030struct pblk_global_caches {
31 struct kmem_cache *ws;
32 struct kmem_cache *rec;
33 struct kmem_cache *g_rq;
34 struct kmem_cache *w_rq;
35
36 struct kref kref;
37
38 struct mutex mutex; /* Ensures consistency between
39 * caches and kref
40 */
41};
42
43static struct pblk_global_caches pblk_caches = {
44 .mutex = __MUTEX_INITIALIZER(pblk_caches.mutex),
45 .kref = KREF_INIT(0),
46};
47
Kent Overstreetb906bbb2018-05-20 18:25:50 -040048struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049
50static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
51 struct bio *bio)
52{
53 int ret;
54
55 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
56 * constraint. Writes can be of arbitrary size.
57 */
58 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100059 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020060 ret = pblk_submit_read(pblk, bio);
61 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
62 bio_put(bio);
63
64 return ret;
65 }
66
67 /* Prevent deadlock in the case of a modest LUN configuration and large
68 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
69 * available for user I/O.
70 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020071 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100072 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073
74 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
75}
76
77static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
78{
79 struct pblk *pblk = q->queuedata;
80
81 if (bio_op(bio) == REQ_OP_DISCARD) {
82 pblk_discard(pblk, bio);
83 if (!(bio->bi_opf & REQ_PREFLUSH)) {
84 bio_endio(bio);
85 return BLK_QC_T_NONE;
86 }
87 }
88
89 switch (pblk_rw_io(q, pblk, bio)) {
90 case NVM_IO_ERR:
91 bio_io_error(bio);
92 break;
93 case NVM_IO_DONE:
94 bio_endio(bio);
95 break;
96 }
97
98 return BLK_QC_T_NONE;
99}
100
Hans Holmbergc5586192017-10-13 14:46:40 +0200101static size_t pblk_trans_map_size(struct pblk *pblk)
102{
103 int entry_size = 8;
104
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200105 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +0200106 entry_size = 4;
107
Igor Konopko847a3a22019-05-04 20:37:48 +0200108 return entry_size * pblk->capacity;
Hans Holmbergc5586192017-10-13 14:46:40 +0200109}
110
Matias Bjørling880eda52018-07-13 10:48:37 +0200111#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergc5586192017-10-13 14:46:40 +0200112static u32 pblk_l2p_crc(struct pblk *pblk)
113{
114 size_t map_size;
115 u32 crc = ~(u32)0;
116
117 map_size = pblk_trans_map_size(pblk);
118 crc = crc32_le(crc, pblk->trans_map, map_size);
119 return crc;
120}
121#endif
122
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200123static void pblk_l2p_free(struct pblk *pblk)
124{
125 vfree(pblk->trans_map);
126}
127
Javier González43d47122018-03-30 00:05:09 +0200128static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
129{
130 struct pblk_line *line = NULL;
131
132 if (factory_init) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100133 guid_gen(&pblk->instance_uuid);
Javier González43d47122018-03-30 00:05:09 +0200134 } else {
135 line = pblk_recov_l2p(pblk);
136 if (IS_ERR(line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200137 pblk_err(pblk, "could not recover l2p table\n");
Javier González43d47122018-03-30 00:05:09 +0200138 return -EFAULT;
139 }
140 }
141
Matias Bjørling880eda52018-07-13 10:48:37 +0200142#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200143 pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Javier González43d47122018-03-30 00:05:09 +0200144#endif
145
146 /* Free full lines directly as GC has not been started yet */
147 pblk_gc_free_full_lines(pblk);
148
149 if (!line) {
150 /* Configure next line for user data */
151 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200152 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200153 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200154 }
155
156 return 0;
157}
158
159static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200160{
161 sector_t i;
162 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200163 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200164 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200165
Hans Holmbergc5586192017-10-13 14:46:40 +0200166 map_size = pblk_trans_map_size(pblk);
167 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200168 if (!pblk->trans_map)
169 return -ENOMEM;
170
171 pblk_ppa_set_empty(&ppa);
172
Igor Konopko847a3a22019-05-04 20:37:48 +0200173 for (i = 0; i < pblk->capacity; i++)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 pblk_trans_map_set(pblk, i, ppa);
175
Javier González1d8b33e2018-06-01 15:04:16 +0200176 ret = pblk_l2p_recover(pblk, factory_init);
177 if (ret)
178 vfree(pblk->trans_map);
179
180 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200181}
182
183static void pblk_rwb_free(struct pblk *pblk)
184{
185 if (pblk_rb_tear_down_check(&pblk->rwb))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200186 pblk_err(pblk, "write buffer error on tear down\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200187
Javier González9bd1f872018-10-09 13:12:10 +0200188 pblk_rb_free(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189}
190
191static int pblk_rwb_init(struct pblk *pblk)
192{
193 struct nvm_tgt_dev *dev = pblk->dev;
194 struct nvm_geo *geo = &dev->geo;
Javier González9bd1f872018-10-09 13:12:10 +0200195 unsigned long buffer_size;
Javier González766c8ce2018-10-09 13:12:15 +0200196 int pgs_in_buffer, threshold;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197
Javier González766c8ce2018-10-09 13:12:15 +0200198 threshold = geo->mw_cunits * geo->all_luns;
Javier Gonzálezd672d922018-10-09 13:12:11 +0200199 pgs_in_buffer = (max(geo->mw_cunits, geo->ws_opt) + geo->ws_opt)
200 * geo->all_luns;
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200201
202 if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200203 buffer_size = write_buffer_size;
204 else
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200205 buffer_size = pgs_in_buffer;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200206
Javier González766c8ce2018-10-09 13:12:15 +0200207 return pblk_rb_init(&pblk->rwb, buffer_size, threshold, geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208}
209
Matias Bjørling4e495a42018-07-13 10:48:42 +0200210static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
211 struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200213 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
214 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200215
216 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200217 power_len = get_count_order(geo->num_ch);
218 if (1 << power_len != geo->num_ch) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200219 pblk_err(pblk, "supports only power-of-two channel config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220 return -EINVAL;
221 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200222 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200224 power_len = get_count_order(geo->num_lun);
225 if (1 << power_len != geo->num_lun) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200226 pblk_err(pblk, "supports only power-of-two LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200227 return -EINVAL;
228 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200229 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200230
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200231 dst->blk_len = src->blk_len;
232 dst->pg_len = src->pg_len;
233 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200234 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200236 dst->sec_offset = 0;
237 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200238 dst->ch_offset = dst->pln_offset + dst->pln_len;
239 dst->lun_offset = dst->ch_offset + dst->ch_len;
240 dst->pg_offset = dst->lun_offset + dst->lun_len;
241 dst->blk_offset = dst->pg_offset + dst->pg_len;
242
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200243 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200244 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
245 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
246 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
247 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
248 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
249
250 return dst->blk_offset + src->blk_len;
251}
252
Javier González3b2a3ad2018-03-30 00:05:21 +0200253static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
254 struct pblk_addrf *udst)
255{
256 struct nvm_addrf *src = &geo->addrf;
257
258 adst->ch_len = get_count_order(geo->num_ch);
259 adst->lun_len = get_count_order(geo->num_lun);
260 adst->chk_len = src->chk_len;
261 adst->sec_len = src->sec_len;
262
263 adst->sec_offset = 0;
264 adst->ch_offset = adst->sec_len;
265 adst->lun_offset = adst->ch_offset + adst->ch_len;
266 adst->chk_offset = adst->lun_offset + adst->lun_len;
267
268 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
269 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
270 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
271 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
272
273 udst->sec_stripe = geo->ws_opt;
274 udst->ch_stripe = geo->num_ch;
275 udst->lun_stripe = geo->num_lun;
276
277 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
278 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
279
280 return adst->chk_offset + adst->chk_len;
281}
282
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200283static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200284{
285 struct nvm_tgt_dev *dev = pblk->dev;
286 struct nvm_geo *geo = &dev->geo;
287 int mod;
288
Javier González3b2a3ad2018-03-30 00:05:21 +0200289 switch (geo->version) {
290 case NVM_OCSSD_SPEC_12:
291 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
292 if (mod) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200293 pblk_err(pblk, "bad configuration of sectors/pages\n");
Javier González3b2a3ad2018-03-30 00:05:21 +0200294 return -EINVAL;
295 }
296
Matias Bjørling4e495a42018-07-13 10:48:42 +0200297 pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
298 (void *)&pblk->addrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200299 break;
300 case NVM_OCSSD_SPEC_20:
301 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
Matias Bjørling4e495a42018-07-13 10:48:42 +0200302 &pblk->uaddrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200303 break;
304 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200305 pblk_err(pblk, "OCSSD revision not supported (%d)\n",
Javier González3b2a3ad2018-03-30 00:05:21 +0200306 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200307 return -EINVAL;
308 }
309
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200310 return 0;
311}
312
Hans Holmberg1864de92018-10-09 13:11:58 +0200313static int pblk_create_global_caches(void)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200314{
Hans Holmberg1864de92018-10-09 13:11:58 +0200315
316 pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200317 sizeof(struct pblk_line_ws), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200318 if (!pblk_caches.ws)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200319 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320
Hans Holmberg1864de92018-10-09 13:11:58 +0200321 pblk_caches.rec = kmem_cache_create("pblk_rec",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200322 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200323 if (!pblk_caches.rec)
324 goto fail_destroy_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325
Hans Holmberg1864de92018-10-09 13:11:58 +0200326 pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200327 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200328 if (!pblk_caches.g_rq)
329 goto fail_destroy_rec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330
Hans Holmberg1864de92018-10-09 13:11:58 +0200331 pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200332 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200333 if (!pblk_caches.w_rq)
334 goto fail_destroy_g_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200335
336 return 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200337
338fail_destroy_g_rq:
339 kmem_cache_destroy(pblk_caches.g_rq);
340fail_destroy_rec:
341 kmem_cache_destroy(pblk_caches.rec);
342fail_destroy_ws:
343 kmem_cache_destroy(pblk_caches.ws);
344
345 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200346}
347
Hans Holmberg1864de92018-10-09 13:11:58 +0200348static int pblk_get_global_caches(void)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200349{
Javier González42bd0382018-12-11 20:16:21 +0100350 int ret = 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200351
352 mutex_lock(&pblk_caches.mutex);
353
Javier González42bd0382018-12-11 20:16:21 +0100354 if (kref_get_unless_zero(&pblk_caches.kref))
355 goto out;
Hans Holmberg1864de92018-10-09 13:11:58 +0200356
357 ret = pblk_create_global_caches();
Hans Holmberg1864de92018-10-09 13:11:58 +0200358 if (!ret)
Javier González42bd0382018-12-11 20:16:21 +0100359 kref_init(&pblk_caches.kref);
Hans Holmberg1864de92018-10-09 13:11:58 +0200360
Javier González42bd0382018-12-11 20:16:21 +0100361out:
Hans Holmberg1864de92018-10-09 13:11:58 +0200362 mutex_unlock(&pblk_caches.mutex);
Hans Holmberg1864de92018-10-09 13:11:58 +0200363 return ret;
364}
365
366static void pblk_destroy_global_caches(struct kref *ref)
367{
368 struct pblk_global_caches *c;
369
370 c = container_of(ref, struct pblk_global_caches, kref);
371
372 kmem_cache_destroy(c->ws);
373 kmem_cache_destroy(c->rec);
374 kmem_cache_destroy(c->g_rq);
375 kmem_cache_destroy(c->w_rq);
376}
377
378static void pblk_put_global_caches(void)
379{
380 mutex_lock(&pblk_caches.mutex);
381 kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
382 mutex_unlock(&pblk_caches.mutex);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200383}
384
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200385static int pblk_core_init(struct pblk *pblk)
386{
387 struct nvm_tgt_dev *dev = pblk->dev;
388 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400389 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200390
391 atomic64_set(&pblk->user_wa, 0);
392 atomic64_set(&pblk->pad_wa, 0);
393 atomic64_set(&pblk->gc_wa, 0);
394 pblk->user_rst_wa = 0;
395 pblk->pad_rst_wa = 0;
396 pblk->gc_rst_wa = 0;
397
398 atomic64_set(&pblk->nr_flush, 0);
399 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200400
Matias Bjørling8bbd45d2018-10-09 13:11:38 +0200401 pblk->min_write_pgs = geo->ws_opt;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100402 pblk->min_write_pgs_data = pblk->min_write_pgs;
Javier González43d47122018-03-30 00:05:09 +0200403 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
404 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
Zhoujie Wu8a57fc32018-10-09 13:12:14 +0200405 pblk->max_write_pgs = min_t(int, pblk->max_write_pgs,
406 queue_max_hw_sectors(dev->q) / (geo->csecs >> SECTOR_SHIFT));
Javier González43d47122018-03-30 00:05:09 +0200407 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
408
Igor Konopkofaa79f22018-12-11 20:16:23 +0100409 pblk->oob_meta_size = geo->sos;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100410 if (!pblk_is_oob_meta_supported(pblk)) {
411 /* For drives which does not have OOB metadata feature
412 * in order to support recovery feature we need to use
413 * so called packed metadata. Packed metada will store
414 * the same information as OOB metadata (l2p table mapping,
415 * but in the form of the single page at the end of
416 * every write request.
417 */
418 if (pblk->min_write_pgs
419 * sizeof(struct pblk_sec_meta) > PAGE_SIZE) {
420 /* We want to keep all the packed metadata on single
421 * page per write requests. So we need to ensure that
422 * it will fit.
423 *
424 * This is more like sanity check, since there is
425 * no device with such a big minimal write size
426 * (above 1 metabytes).
427 */
428 pblk_err(pblk, "Not supported min write size\n");
429 return -EINVAL;
430 }
431 /* For packed meta approach we do some simplification.
432 * On read path we always issue requests which size
433 * equal to max_write_pgs, with all pages filled with
434 * user payload except of last one page which will be
435 * filled with packed metadata.
436 */
437 pblk->max_write_pgs = pblk->min_write_pgs;
438 pblk->min_write_pgs_data = pblk->min_write_pgs - 1;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100439 }
440
Kees Cook6396bb22018-06-12 14:03:40 -0700441 pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
Javier González43d47122018-03-30 00:05:09 +0200442 GFP_KERNEL);
443 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444 return -ENOMEM;
445
Hans Holmberg1864de92018-10-09 13:11:58 +0200446 if (pblk_get_global_caches())
Javier González43d47122018-03-30 00:05:09 +0200447 goto fail_free_pad_dist;
448
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200449 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400450 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
451 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200452 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400454 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
Hans Holmberg1864de92018-10-09 13:11:58 +0200455 pblk_caches.ws);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400456 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200457 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200458
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400459 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200460 pblk_caches.rec);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400461 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200462 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400464 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200465 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400466 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 goto free_rec_pool;
468
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400469 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200470 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400471 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200472 goto free_r_rq_pool;
473
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400474 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200475 pblk_caches.w_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400476 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200477 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478
Javier Gonzálezef576492017-06-26 11:57:28 +0200479 pblk->close_wq = alloc_workqueue("pblk-close-wq",
480 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
481 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200482 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200483
Javier Gonzálezef576492017-06-26 11:57:28 +0200484 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
485 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
486 if (!pblk->bb_wq)
487 goto free_close_wq;
488
Javier González7bd4d372017-10-13 14:46:23 +0200489 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
490 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
491 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200492 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200493
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200494 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200495 goto free_r_end_wq;
496
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200498 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200499
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500 return 0;
501
Javier González7bd4d372017-10-13 14:46:23 +0200502free_r_end_wq:
503 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200504free_bb_wq:
505 destroy_workqueue(pblk->bb_wq);
506free_close_wq:
507 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400509 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200510free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400511 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200512free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400513 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200514free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400515 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200516free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400517 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200518free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400519 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200520free_global_caches:
Hans Holmberg1864de92018-10-09 13:11:58 +0200521 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200522fail_free_pad_dist:
523 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 return -ENOMEM;
525}
526
527static void pblk_core_free(struct pblk *pblk)
528{
Javier Gonzálezef576492017-06-26 11:57:28 +0200529 if (pblk->close_wq)
530 destroy_workqueue(pblk->close_wq);
531
Javier González7bd4d372017-10-13 14:46:23 +0200532 if (pblk->r_end_wq)
533 destroy_workqueue(pblk->r_end_wq);
534
Javier Gonzálezef576492017-06-26 11:57:28 +0200535 if (pblk->bb_wq)
536 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200537
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400538 mempool_exit(&pblk->page_bio_pool);
539 mempool_exit(&pblk->gen_ws_pool);
540 mempool_exit(&pblk->rec_pool);
541 mempool_exit(&pblk->r_rq_pool);
542 mempool_exit(&pblk->e_rq_pool);
543 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200544
Hans Holmberg1864de92018-10-09 13:11:58 +0200545 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200546 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200547}
548
Javier Gonzáleze411b332018-03-30 00:04:59 +0200549static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200550{
551 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
552 int i;
553
554 kfree(l_mg->bb_template);
555 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200556 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200557
558 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200559 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200560 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200561 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200562 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200563
564 mempool_destroy(l_mg->bitmap_pool);
565 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566}
567
Hans Holmberg48b8d202018-06-01 16:41:06 +0200568static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
569 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200571 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
572
Javier Gonzáleze411b332018-03-30 00:04:59 +0200573 kfree(line->blk_bitmap);
574 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200575 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200576
577 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
578 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200579}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200580
Javier Gonzáleze411b332018-03-30 00:04:59 +0200581static void pblk_lines_free(struct pblk *pblk)
582{
583 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
584 struct pblk_line *line;
585 int i;
586
Javier Gonzáleze411b332018-03-30 00:04:59 +0200587 for (i = 0; i < l_mg->nr_lines; i++) {
588 line = &pblk->lines[i];
589
Javier González8e55c072018-06-01 15:04:22 +0200590 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200591 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200592 }
Javier González43d47122018-03-30 00:05:09 +0200593
594 pblk_line_mg_free(pblk);
595
596 kfree(pblk->luns);
597 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200598}
599
Javier González43d47122018-03-30 00:05:09 +0200600static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601{
602 struct nvm_tgt_dev *dev = pblk->dev;
603 struct nvm_geo *geo = &dev->geo;
604 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200605 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200606
607 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200608 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200609 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610 return -EINVAL;
611 }
612
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100613 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
614 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615 if (!pblk->luns)
616 return -ENOMEM;
617
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100618 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200620 int ch = i % geo->num_ch;
621 int lun_raw = i / geo->num_ch;
622 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200623
624 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200625 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626
627 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628 }
629
630 return 0;
631}
632
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200634static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200636 struct pblk_line_meta *lm = &pblk->lm;
637 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
638 struct nvm_tgt_dev *dev = pblk->dev;
639 struct nvm_geo *geo = &dev->geo;
640
641 /* Round to sector size so that lba_list starts on its own sector */
642 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200643 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200644 sizeof(struct wa_counters), geo->csecs);
645 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200646
647 /* Round to sector size so that vsc_list starts on its own sector */
648 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
649 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200650 geo->csecs);
651 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200652
653 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200654 geo->csecs);
655 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200656
657 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
658
659 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660}
661
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100662static int pblk_set_provision(struct pblk *pblk, int nr_free_chks)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200663{
664 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100665 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
666 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200667 struct nvm_geo *geo = &dev->geo;
668 sector_t provisioned;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100669 int sec_meta, blk_meta, clba;
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100670 int minimum;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200671
Javier Gonzáleze5392732018-01-05 14:16:14 +0100672 if (geo->op == NVM_TARGET_DEFAULT_OP)
673 pblk->op = PBLK_DEFAULT_OP;
674 else
675 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200676
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100677 minimum = pblk_get_min_chks(pblk);
678 provisioned = nr_free_chks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100679 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200680 sector_div(provisioned, 100);
681
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100682 if ((nr_free_chks - provisioned) < minimum) {
683 if (geo->op != NVM_TARGET_DEFAULT_OP) {
684 pblk_err(pblk, "OP too small to create a sane instance\n");
685 return -EINTR;
686 }
687
688 /* If the user did not specify an OP value, and PBLK_DEFAULT_OP
689 * is not enough, calculate and set sane value
690 */
691
692 provisioned = nr_free_chks - minimum;
693 pblk->op = (100 * minimum) / nr_free_chks;
694 pblk_info(pblk, "Default OP insufficient, adjusting OP to %d\n",
695 pblk->op);
696 }
697
698 pblk->op_blks = nr_free_chks - provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100699
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200700 /* Internally pblk manages all free blocks, but all calculations based
701 * on user capacity consider only provisioned blocks
702 */
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100703 pblk->rl.total_blocks = nr_free_chks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100704
705 /* Consider sectors used for metadata */
706 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200707 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100708
Igor Konopko55d8ec32018-12-11 20:16:26 +0100709 clba = (geo->clba / pblk->min_write_pgs) * pblk->min_write_pgs_data;
710 pblk->capacity = (provisioned - blk_meta) * clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100711
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100712 atomic_set(&pblk->rl.free_blocks, nr_free_chks);
713 atomic_set(&pblk->rl.free_user_blocks, nr_free_chks);
714
715 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200716}
717
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200718static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200719 struct nvm_chk_meta *meta)
720{
721 struct nvm_tgt_dev *dev = pblk->dev;
722 struct nvm_geo *geo = &dev->geo;
723 struct pblk_line_meta *lm = &pblk->lm;
724 int i, nr_bad_chks = 0;
725
726 for (i = 0; i < lm->blk_per_line; i++) {
727 struct pblk_lun *rlun = &pblk->luns[i];
728 struct nvm_chk_meta *chunk;
729 struct nvm_chk_meta *chunk_meta;
730 struct ppa_addr ppa;
731 int pos;
732
733 ppa = rlun->bppa;
734 pos = pblk_ppa_to_pos(geo, ppa);
735 chunk = &line->chks[pos];
736
737 ppa.m.chk = line->id;
738 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
739
740 chunk->state = chunk_meta->state;
741 chunk->type = chunk_meta->type;
742 chunk->wi = chunk_meta->wi;
743 chunk->slba = chunk_meta->slba;
744 chunk->cnlb = chunk_meta->cnlb;
745 chunk->wp = chunk_meta->wp;
746
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200747 trace_pblk_chunk_state(pblk_disk_name(pblk), &ppa,
748 chunk->state);
749
Javier González32ef9412018-03-30 00:05:20 +0200750 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
751 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
752 continue;
753 }
754
Javier González6f9c9602018-06-01 15:04:23 +0200755 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
756 continue;
757
Javier González32ef9412018-03-30 00:05:20 +0200758 set_bit(pos, line->blk_bitmap);
759 nr_bad_chks++;
760 }
761
762 return nr_bad_chks;
763}
764
765static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
766 void *chunk_meta, int line_id)
767{
Javier González32ef9412018-03-30 00:05:20 +0200768 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
769 struct pblk_line_meta *lm = &pblk->lm;
770 long nr_bad_chks, chk_in_line;
771
772 line->pblk = pblk;
773 line->id = line_id;
774 line->type = PBLK_LINETYPE_FREE;
775 line->state = PBLK_LINESTATE_NEW;
776 line->gc_group = PBLK_LINEGC_NONE;
777 line->vsc = &l_mg->vsc_list[line_id];
778 spin_lock_init(&line->lock);
779
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200780 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200781
782 chk_in_line = lm->blk_per_line - nr_bad_chks;
783 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
784 chk_in_line < lm->min_blk_line) {
785 line->state = PBLK_LINESTATE_BAD;
786 list_add_tail(&line->list, &l_mg->bad_list);
787 return 0;
788 }
789
790 atomic_set(&line->blk_in_line, chk_in_line);
791 list_add_tail(&line->list, &l_mg->free_list);
792 l_mg->nr_free_lines++;
793
794 return chk_in_line;
795}
796
797static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200798{
Javier González43d47122018-03-30 00:05:09 +0200799 struct pblk_line_meta *lm = &pblk->lm;
800
801 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
802 if (!line->blk_bitmap)
803 return -ENOMEM;
804
805 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200806 if (!line->erase_bitmap)
807 goto free_blk_bitmap;
808
Javier González43d47122018-03-30 00:05:09 +0200809
Kees Cook6da2ec52018-06-12 13:55:00 -0700810 line->chks = kmalloc_array(lm->blk_per_line,
811 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200812 if (!line->chks)
813 goto free_erase_bitmap;
814
815 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
816 if (!line->w_err_gc)
817 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200818
819 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200820
821free_chks:
822 kfree(line->chks);
823free_erase_bitmap:
824 kfree(line->erase_bitmap);
825free_blk_bitmap:
826 kfree(line->blk_bitmap);
827 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200828}
829
830static int pblk_line_mg_init(struct pblk *pblk)
831{
832 struct nvm_tgt_dev *dev = pblk->dev;
833 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200834 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
835 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200836 int i, bb_distance;
837
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200838 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200839 l_mg->log_line = l_mg->data_line = NULL;
840 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
841 l_mg->nr_free_lines = 0;
842 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
843
844 INIT_LIST_HEAD(&l_mg->free_list);
845 INIT_LIST_HEAD(&l_mg->corrupt_list);
846 INIT_LIST_HEAD(&l_mg->bad_list);
847 INIT_LIST_HEAD(&l_mg->gc_full_list);
848 INIT_LIST_HEAD(&l_mg->gc_high_list);
849 INIT_LIST_HEAD(&l_mg->gc_mid_list);
850 INIT_LIST_HEAD(&l_mg->gc_low_list);
851 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200852 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200853
854 INIT_LIST_HEAD(&l_mg->emeta_list);
855
Hans Holmberg48b8d202018-06-01 16:41:06 +0200856 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
857 l_mg->gc_lists[1] = &l_mg->gc_high_list;
858 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
859 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200860
861 spin_lock_init(&l_mg->free_lock);
862 spin_lock_init(&l_mg->close_lock);
863 spin_lock_init(&l_mg->gc_lock);
864
865 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
866 if (!l_mg->vsc_list)
867 goto fail;
868
869 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
870 if (!l_mg->bb_template)
871 goto fail_free_vsc_list;
872
873 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
874 if (!l_mg->bb_aux)
875 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200876
877 /* smeta is always small enough to fit on a kmalloc memory allocation,
878 * emeta depends on the number of LUNs allocated to the pblk instance
879 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200880 for (i = 0; i < PBLK_DATA_LINES; i++) {
881 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
882 if (!l_mg->sline_meta[i])
883 goto fail_free_smeta;
884 }
885
Hans Holmberg53d82db2018-10-09 13:11:47 +0200886 l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
887 lm->sec_bitmap_len, 0, 0, NULL);
888 if (!l_mg->bitmap_cache)
889 goto fail_free_smeta;
890
891 /* the bitmap pool is used for both valid and map bitmaps */
892 l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
893 l_mg->bitmap_cache);
894 if (!l_mg->bitmap_pool)
895 goto fail_destroy_bitmap_cache;
896
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200897 /* emeta allocates three different buffers for managing metadata with
898 * in-memory and in-media layouts
899 */
900 for (i = 0; i < PBLK_DATA_LINES; i++) {
901 struct pblk_emeta *emeta;
902
903 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
904 if (!emeta)
905 goto fail_free_emeta;
906
907 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
908 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
909
910 emeta->buf = vmalloc(lm->emeta_len[0]);
911 if (!emeta->buf) {
912 kfree(emeta);
913 goto fail_free_emeta;
914 }
915
916 emeta->nr_entries = lm->emeta_sec[0];
917 l_mg->eline_meta[i] = emeta;
918 } else {
919 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
920
921 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
922 if (!emeta->buf) {
923 kfree(emeta);
924 goto fail_free_emeta;
925 }
926
927 emeta->nr_entries = lm->emeta_sec[0];
928 l_mg->eline_meta[i] = emeta;
929 }
930 }
931
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200932 for (i = 0; i < l_mg->nr_lines; i++)
933 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
934
Javier González43d47122018-03-30 00:05:09 +0200935 bb_distance = (geo->all_luns) * geo->ws_opt;
936 for (i = 0; i < lm->sec_per_line; i += bb_distance)
937 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
938
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200939 return 0;
940
941fail_free_emeta:
942 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200943 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
944 vfree(l_mg->eline_meta[i]->buf);
945 else
946 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200947 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200948 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200949
950 mempool_destroy(l_mg->bitmap_pool);
951fail_destroy_bitmap_cache:
952 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200953fail_free_smeta:
954 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200955 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200956 kfree(l_mg->bb_aux);
957fail_free_bb_template:
958 kfree(l_mg->bb_template);
959fail_free_vsc_list:
960 kfree(l_mg->vsc_list);
961fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200962 return -ENOMEM;
963}
964
Javier González43d47122018-03-30 00:05:09 +0200965static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200966{
967 struct nvm_tgt_dev *dev = pblk->dev;
968 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200969 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200970 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200971 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200972
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200973 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100974 lm->blk_per_line = geo->all_luns;
975 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100977 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200978 lm->mid_thrs = lm->sec_per_line / 2;
979 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100980 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200981
982 /* Calculate necessary pages for smeta. See comment over struct
983 * line_smeta definition
984 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985 i = 1;
986add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200987 lm->smeta_sec = i * geo->ws_opt;
988 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200989
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200990 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200991 if (smeta_len > lm->smeta_len) {
992 i++;
993 goto add_smeta_page;
994 }
995
996 /* Calculate necessary pages for emeta. See comment over struct
997 * line_emeta definition
998 */
999 i = 1;
1000add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001001 lm->emeta_sec[0] = i * geo->ws_opt;
1002 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001003
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001004 emeta_len = calc_emeta_len(pblk);
1005 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001006 i++;
1007 goto add_emeta_page;
1008 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001009
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001010 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +02001011
1012 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001013 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +02001014 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001015 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +02001016
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001017 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001018 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001019 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +02001020 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001021 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001022
Javier González43d47122018-03-30 00:05:09 +02001023 return 0;
1024}
1025
1026static int pblk_lines_init(struct pblk *pblk)
1027{
1028 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +02001029 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +02001030 void *chunk_meta;
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001031 int nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +02001032 int i, ret;
1033
1034 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001035 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001036 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001037
Javier González43d47122018-03-30 00:05:09 +02001038 ret = pblk_line_mg_init(pblk);
1039 if (ret)
1040 return ret;
1041
1042 ret = pblk_luns_init(pblk);
1043 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001044 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001045
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001046 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +02001047 if (IS_ERR(chunk_meta)) {
1048 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001049 goto fail_free_luns;
1050 }
1051
1052 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1053 GFP_KERNEL);
1054 if (!pblk->lines) {
1055 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001056 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001057 }
1058
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001059 for (i = 0; i < l_mg->nr_lines; i++) {
1060 line = &pblk->lines[i];
1061
Javier González32ef9412018-03-30 00:05:20 +02001062 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001063 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001064 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001065
Javier González32ef9412018-03-30 00:05:20 +02001066 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Hans Holmbergf2937232018-10-09 13:11:53 +02001067
1068 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1069 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001070 }
1071
Javier González2deeefc2018-06-01 15:04:15 +02001072 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001073 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Wei Yongjuna70985f2018-10-09 13:12:13 +02001074 ret = -EINTR;
1075 goto fail_free_lines;
Javier González2deeefc2018-06-01 15:04:15 +02001076 }
1077
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001078 ret = pblk_set_provision(pblk, nr_free_chks);
1079 if (ret)
1080 goto fail_free_lines;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081
Javier González090ee262018-10-09 13:12:01 +02001082 vfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001083 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001084
Javier González43d47122018-03-30 00:05:09 +02001085fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001086 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +02001087 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001088 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001089fail_free_chunk_meta:
Hans Holmberg0934ce82018-12-11 20:16:15 +01001090 vfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001091fail_free_luns:
1092 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001093fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001094 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001095
1096 return ret;
1097}
1098
1099static int pblk_writer_init(struct pblk *pblk)
1100{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001101 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1102 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001103 int err = PTR_ERR(pblk->writer_ts);
1104
1105 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001106 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001107 err);
1108 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001109 }
1110
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001111 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1112 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1113
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001114 return 0;
1115}
1116
1117static void pblk_writer_stop(struct pblk *pblk)
1118{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001119 /* The pipeline must be stopped and the write buffer emptied before the
1120 * write thread is stopped
1121 */
1122 WARN(pblk_rb_read_count(&pblk->rwb),
1123 "Stopping not fully persisted write buffer\n");
1124
1125 WARN(pblk_rb_sync_count(&pblk->rwb),
1126 "Stopping not fully synced write buffer\n");
1127
Hans Holmberg7be970b2018-03-30 00:04:56 +02001128 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001129 if (pblk->writer_ts)
1130 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001131}
1132
1133static void pblk_free(struct pblk *pblk)
1134{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001135 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001136 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001137 pblk_rwb_free(pblk);
1138 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001139
1140 kfree(pblk);
1141}
1142
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001143static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001144{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001145 if (graceful)
1146 __pblk_pipeline_flush(pblk);
1147 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001148 pblk_writer_stop(pblk);
1149 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001150 pblk_rl_free(&pblk->rl);
1151
Matias Bjørling4e495a42018-07-13 10:48:42 +02001152 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153}
1154
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001155static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001156{
1157 struct pblk *pblk = private;
1158
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001159 pblk_gc_exit(pblk, graceful);
1160 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001161
Matias Bjørling880eda52018-07-13 10:48:37 +02001162#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001163 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001164#endif
1165
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001166 pblk_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001167}
1168
1169static sector_t pblk_capacity(void *private)
1170{
1171 struct pblk *pblk = private;
1172
1173 return pblk->capacity * NR_PHY_IN_LOG;
1174}
1175
1176static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1177 int flags)
1178{
1179 struct nvm_geo *geo = &dev->geo;
1180 struct request_queue *bqueue = dev->q;
1181 struct request_queue *tqueue = tdisk->queue;
1182 struct pblk *pblk;
1183 int ret;
1184
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001185 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1186 if (!pblk)
1187 return ERR_PTR(-ENOMEM);
1188
1189 pblk->dev = dev;
1190 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001191 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001192 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001193 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001194
Matias Bjørling4e495a42018-07-13 10:48:42 +02001195 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1196 geo->version == NVM_OCSSD_SPEC_20)) {
1197 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1198 geo->version);
1199 kfree(pblk);
1200 return ERR_PTR(-EINVAL);
1201 }
1202
Igor Konopkoa16816b2018-12-11 20:16:25 +01001203 if (geo->ext) {
1204 pblk_err(pblk, "extended metadata not supported\n");
1205 kfree(pblk);
1206 return ERR_PTR(-EINVAL);
1207 }
1208
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001209 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001210 spin_lock_init(&pblk->trans_lock);
1211 spin_lock_init(&pblk->lock);
1212
Matias Bjørling880eda52018-07-13 10:48:37 +02001213#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001214 atomic_long_set(&pblk->inflight_writes, 0);
1215 atomic_long_set(&pblk->padded_writes, 0);
1216 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001217 atomic_long_set(&pblk->req_writes, 0);
1218 atomic_long_set(&pblk->sub_writes, 0);
1219 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001220 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001221 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001222 atomic_long_set(&pblk->sync_reads, 0);
1223 atomic_long_set(&pblk->recov_writes, 0);
1224 atomic_long_set(&pblk->recov_writes, 0);
1225 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001226 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001227#endif
1228
1229 atomic_long_set(&pblk->read_failed, 0);
1230 atomic_long_set(&pblk->read_empty, 0);
1231 atomic_long_set(&pblk->read_high_ecc, 0);
1232 atomic_long_set(&pblk->read_failed_gc, 0);
1233 atomic_long_set(&pblk->write_failed, 0);
1234 atomic_long_set(&pblk->erase_failed, 0);
1235
Javier González43d47122018-03-30 00:05:09 +02001236 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001238 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001239 goto fail;
1240 }
1241
1242 ret = pblk_lines_init(pblk);
1243 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001244 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001245 goto fail_free_core;
1246 }
1247
Javier González43d47122018-03-30 00:05:09 +02001248 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001249 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001250 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001251 goto fail_free_lines;
1252 }
1253
1254 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1255 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001256 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001257 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001258 }
1259
1260 ret = pblk_writer_init(pblk);
1261 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001262 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001263 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001264 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001265 }
1266
1267 ret = pblk_gc_init(pblk);
1268 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001269 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001270 goto fail_stop_writer;
1271 }
1272
1273 /* inherit the size from the underlying device */
1274 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1275 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1276
1277 blk_queue_write_cache(tqueue, true, false);
1278
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001279 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001280 tqueue->limits.discard_alignment = 0;
1281 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001282 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001283
Matias Bjørling4e495a42018-07-13 10:48:42 +02001284 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001285 geo->all_luns, pblk->l_mg.nr_lines,
Igor Konopko847a3a22019-05-04 20:37:48 +02001286 (unsigned long long)pblk->capacity,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287 pblk->rwb.nr_entries);
1288
1289 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001290
1291 /* Check if we need to start GC */
1292 pblk_gc_should_kick(pblk);
1293
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001294 return pblk;
1295
1296fail_stop_writer:
1297 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001298fail_free_l2p:
1299 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001300fail_free_rwb:
1301 pblk_rwb_free(pblk);
1302fail_free_lines:
1303 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001304fail_free_core:
1305 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001306fail:
1307 kfree(pblk);
1308 return ERR_PTR(ret);
1309}
1310
1311/* physical block device target */
1312static struct nvm_tgt_type tt_pblk = {
1313 .name = "pblk",
1314 .version = {1, 0, 0},
1315
1316 .make_rq = pblk_make_rq,
1317 .capacity = pblk_capacity,
1318
1319 .init = pblk_init,
1320 .exit = pblk_exit,
1321
1322 .sysfs_init = pblk_sysfs_init,
1323 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001324 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001325};
1326
1327static int __init pblk_module_init(void)
1328{
NeilBrownb25d5232017-06-18 14:38:58 +10001329 int ret;
1330
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001331 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1332 if (ret)
1333 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001334 ret = nvm_register_tgt_type(&tt_pblk);
1335 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001336 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001337 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001338}
1339
1340static void pblk_module_exit(void)
1341{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001342 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001343 nvm_unregister_tgt_type(&tt_pblk);
1344}
1345
1346module_init(pblk_module_init);
1347module_exit(pblk_module_exit);
1348MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1349MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1350MODULE_LICENSE("GPL v2");
1351MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");