blob: b351c7f002de0509b7bca75a0f64a616eec7ee32 [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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020050static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
51{
52 struct pblk *pblk = q->queuedata;
53
54 if (bio_op(bio) == REQ_OP_DISCARD) {
55 pblk_discard(pblk, bio);
56 if (!(bio->bi_opf & REQ_PREFLUSH)) {
57 bio_endio(bio);
58 return BLK_QC_T_NONE;
59 }
60 }
61
Chansol Kim05038712019-05-04 20:37:56 +020062 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
63 * constraint. Writes can be of arbitrary size.
64 */
65 if (bio_data_dir(bio) == READ) {
66 blk_queue_split(q, &bio);
Igor Konopko3e03f632019-05-04 20:38:06 +020067 pblk_submit_read(pblk, bio);
Chansol Kim05038712019-05-04 20:37:56 +020068 } else {
69 /* Prevent deadlock in the case of a modest LUN configuration
70 * and large user I/Os. Unless stalled, the rate limiter
71 * leaves at least 256KB available for user I/O.
72 */
73 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
74 blk_queue_split(q, &bio);
75
Igor Konopko3e03f632019-05-04 20:38:06 +020076 pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020077 }
78
79 return BLK_QC_T_NONE;
80}
81
Hans Holmbergc5586192017-10-13 14:46:40 +020082static size_t pblk_trans_map_size(struct pblk *pblk)
83{
84 int entry_size = 8;
85
Javier Gonzálezbb845ae42018-03-30 00:05:19 +020086 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +020087 entry_size = 4;
88
Igor Konopko847a3a22019-05-04 20:37:48 +020089 return entry_size * pblk->capacity;
Hans Holmbergc5586192017-10-13 14:46:40 +020090}
91
Matias Bjørling880eda52018-07-13 10:48:37 +020092#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergc5586192017-10-13 14:46:40 +020093static u32 pblk_l2p_crc(struct pblk *pblk)
94{
95 size_t map_size;
96 u32 crc = ~(u32)0;
97
98 map_size = pblk_trans_map_size(pblk);
99 crc = crc32_le(crc, pblk->trans_map, map_size);
100 return crc;
101}
102#endif
103
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104static void pblk_l2p_free(struct pblk *pblk)
105{
106 vfree(pblk->trans_map);
107}
108
Javier González43d47122018-03-30 00:05:09 +0200109static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
110{
111 struct pblk_line *line = NULL;
112
113 if (factory_init) {
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100114 guid_gen(&pblk->instance_uuid);
Javier González43d47122018-03-30 00:05:09 +0200115 } else {
116 line = pblk_recov_l2p(pblk);
117 if (IS_ERR(line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200118 pblk_err(pblk, "could not recover l2p table\n");
Javier González43d47122018-03-30 00:05:09 +0200119 return -EFAULT;
120 }
121 }
122
Matias Bjørling880eda52018-07-13 10:48:37 +0200123#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200124 pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Javier González43d47122018-03-30 00:05:09 +0200125#endif
126
127 /* Free full lines directly as GC has not been started yet */
128 pblk_gc_free_full_lines(pblk);
129
130 if (!line) {
131 /* Configure next line for user data */
132 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200133 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200134 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200135 }
136
137 return 0;
138}
139
140static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200141{
142 sector_t i;
143 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200144 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200145 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200146
Hans Holmbergc5586192017-10-13 14:46:40 +0200147 map_size = pblk_trans_map_size(pblk);
Igor Konopko6e46b8b2019-05-04 20:37:53 +0200148 pblk->trans_map = __vmalloc(map_size, GFP_KERNEL | __GFP_NOWARN
149 | __GFP_RETRY_MAYFAIL | __GFP_HIGHMEM,
150 PAGE_KERNEL);
151 if (!pblk->trans_map) {
152 pblk_err(pblk, "failed to allocate L2P (need %zu of memory)\n",
153 map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200154 return -ENOMEM;
Igor Konopko6e46b8b2019-05-04 20:37:53 +0200155 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200156
157 pblk_ppa_set_empty(&ppa);
158
Igor Konopko847a3a22019-05-04 20:37:48 +0200159 for (i = 0; i < pblk->capacity; i++)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200160 pblk_trans_map_set(pblk, i, ppa);
161
Javier González1d8b33e2018-06-01 15:04:16 +0200162 ret = pblk_l2p_recover(pblk, factory_init);
163 if (ret)
164 vfree(pblk->trans_map);
165
166 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200167}
168
169static void pblk_rwb_free(struct pblk *pblk)
170{
171 if (pblk_rb_tear_down_check(&pblk->rwb))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200172 pblk_err(pblk, "write buffer error on tear down\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200173
Javier González9bd1f872018-10-09 13:12:10 +0200174 pblk_rb_free(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200175}
176
177static int pblk_rwb_init(struct pblk *pblk)
178{
179 struct nvm_tgt_dev *dev = pblk->dev;
180 struct nvm_geo *geo = &dev->geo;
Javier González9bd1f872018-10-09 13:12:10 +0200181 unsigned long buffer_size;
Javier González766c8ce2018-10-09 13:12:15 +0200182 int pgs_in_buffer, threshold;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183
Javier González766c8ce2018-10-09 13:12:15 +0200184 threshold = geo->mw_cunits * geo->all_luns;
Javier Gonzálezd672d922018-10-09 13:12:11 +0200185 pgs_in_buffer = (max(geo->mw_cunits, geo->ws_opt) + geo->ws_opt)
186 * geo->all_luns;
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200187
188 if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200189 buffer_size = write_buffer_size;
190 else
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200191 buffer_size = pgs_in_buffer;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200192
Javier González766c8ce2018-10-09 13:12:15 +0200193 return pblk_rb_init(&pblk->rwb, buffer_size, threshold, geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194}
195
Matias Bjørling4e495a42018-07-13 10:48:42 +0200196static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
197 struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200198{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200199 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
200 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201
202 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200203 power_len = get_count_order(geo->num_ch);
204 if (1 << power_len != geo->num_ch) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200205 pblk_err(pblk, "supports only power-of-two channel config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206 return -EINVAL;
207 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200208 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200210 power_len = get_count_order(geo->num_lun);
211 if (1 << power_len != geo->num_lun) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200212 pblk_err(pblk, "supports only power-of-two LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200213 return -EINVAL;
214 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200215 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200217 dst->blk_len = src->blk_len;
218 dst->pg_len = src->pg_len;
219 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200220 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200222 dst->sec_offset = 0;
223 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200224 dst->ch_offset = dst->pln_offset + dst->pln_len;
225 dst->lun_offset = dst->ch_offset + dst->ch_len;
226 dst->pg_offset = dst->lun_offset + dst->lun_len;
227 dst->blk_offset = dst->pg_offset + dst->pg_len;
228
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200229 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200230 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
231 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
232 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
233 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
234 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
235
236 return dst->blk_offset + src->blk_len;
237}
238
Javier González3b2a3ad2018-03-30 00:05:21 +0200239static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
240 struct pblk_addrf *udst)
241{
242 struct nvm_addrf *src = &geo->addrf;
243
244 adst->ch_len = get_count_order(geo->num_ch);
245 adst->lun_len = get_count_order(geo->num_lun);
246 adst->chk_len = src->chk_len;
247 adst->sec_len = src->sec_len;
248
249 adst->sec_offset = 0;
250 adst->ch_offset = adst->sec_len;
251 adst->lun_offset = adst->ch_offset + adst->ch_len;
252 adst->chk_offset = adst->lun_offset + adst->lun_len;
253
254 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
255 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
256 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
257 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
258
259 udst->sec_stripe = geo->ws_opt;
260 udst->ch_stripe = geo->num_ch;
261 udst->lun_stripe = geo->num_lun;
262
263 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
264 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
265
266 return adst->chk_offset + adst->chk_len;
267}
268
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200269static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200270{
271 struct nvm_tgt_dev *dev = pblk->dev;
272 struct nvm_geo *geo = &dev->geo;
273 int mod;
274
Javier González3b2a3ad2018-03-30 00:05:21 +0200275 switch (geo->version) {
276 case NVM_OCSSD_SPEC_12:
277 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
278 if (mod) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200279 pblk_err(pblk, "bad configuration of sectors/pages\n");
Javier González3b2a3ad2018-03-30 00:05:21 +0200280 return -EINVAL;
281 }
282
Matias Bjørling4e495a42018-07-13 10:48:42 +0200283 pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
284 (void *)&pblk->addrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200285 break;
286 case NVM_OCSSD_SPEC_20:
287 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
Matias Bjørling4e495a42018-07-13 10:48:42 +0200288 &pblk->uaddrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200289 break;
290 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200291 pblk_err(pblk, "OCSSD revision not supported (%d)\n",
Javier González3b2a3ad2018-03-30 00:05:21 +0200292 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200293 return -EINVAL;
294 }
295
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296 return 0;
297}
298
Hans Holmberg1864de92018-10-09 13:11:58 +0200299static int pblk_create_global_caches(void)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300{
Hans Holmberg1864de92018-10-09 13:11:58 +0200301
302 pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200303 sizeof(struct pblk_line_ws), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200304 if (!pblk_caches.ws)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200306
Hans Holmberg1864de92018-10-09 13:11:58 +0200307 pblk_caches.rec = kmem_cache_create("pblk_rec",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200308 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200309 if (!pblk_caches.rec)
310 goto fail_destroy_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200311
Hans Holmberg1864de92018-10-09 13:11:58 +0200312 pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200314 if (!pblk_caches.g_rq)
315 goto fail_destroy_rec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200316
Hans Holmberg1864de92018-10-09 13:11:58 +0200317 pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200318 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200319 if (!pblk_caches.w_rq)
320 goto fail_destroy_g_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200321
322 return 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200323
324fail_destroy_g_rq:
325 kmem_cache_destroy(pblk_caches.g_rq);
326fail_destroy_rec:
327 kmem_cache_destroy(pblk_caches.rec);
328fail_destroy_ws:
329 kmem_cache_destroy(pblk_caches.ws);
330
331 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200332}
333
Hans Holmberg1864de92018-10-09 13:11:58 +0200334static int pblk_get_global_caches(void)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200335{
Javier González42bd0382018-12-11 20:16:21 +0100336 int ret = 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200337
338 mutex_lock(&pblk_caches.mutex);
339
Javier González42bd0382018-12-11 20:16:21 +0100340 if (kref_get_unless_zero(&pblk_caches.kref))
341 goto out;
Hans Holmberg1864de92018-10-09 13:11:58 +0200342
343 ret = pblk_create_global_caches();
Hans Holmberg1864de92018-10-09 13:11:58 +0200344 if (!ret)
Javier González42bd0382018-12-11 20:16:21 +0100345 kref_init(&pblk_caches.kref);
Hans Holmberg1864de92018-10-09 13:11:58 +0200346
Javier González42bd0382018-12-11 20:16:21 +0100347out:
Hans Holmberg1864de92018-10-09 13:11:58 +0200348 mutex_unlock(&pblk_caches.mutex);
Hans Holmberg1864de92018-10-09 13:11:58 +0200349 return ret;
350}
351
352static void pblk_destroy_global_caches(struct kref *ref)
353{
354 struct pblk_global_caches *c;
355
356 c = container_of(ref, struct pblk_global_caches, kref);
357
358 kmem_cache_destroy(c->ws);
359 kmem_cache_destroy(c->rec);
360 kmem_cache_destroy(c->g_rq);
361 kmem_cache_destroy(c->w_rq);
362}
363
364static void pblk_put_global_caches(void)
365{
366 mutex_lock(&pblk_caches.mutex);
367 kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
368 mutex_unlock(&pblk_caches.mutex);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200369}
370
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200371static int pblk_core_init(struct pblk *pblk)
372{
373 struct nvm_tgt_dev *dev = pblk->dev;
374 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400375 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200376
377 atomic64_set(&pblk->user_wa, 0);
378 atomic64_set(&pblk->pad_wa, 0);
379 atomic64_set(&pblk->gc_wa, 0);
380 pblk->user_rst_wa = 0;
381 pblk->pad_rst_wa = 0;
382 pblk->gc_rst_wa = 0;
383
384 atomic64_set(&pblk->nr_flush, 0);
385 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200386
Matias Bjørling8bbd45d2018-10-09 13:11:38 +0200387 pblk->min_write_pgs = geo->ws_opt;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100388 pblk->min_write_pgs_data = pblk->min_write_pgs;
Javier González43d47122018-03-30 00:05:09 +0200389 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
390 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
Zhoujie Wu8a57fc32018-10-09 13:12:14 +0200391 pblk->max_write_pgs = min_t(int, pblk->max_write_pgs,
392 queue_max_hw_sectors(dev->q) / (geo->csecs >> SECTOR_SHIFT));
Javier González43d47122018-03-30 00:05:09 +0200393 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
394
Igor Konopkofaa79f22018-12-11 20:16:23 +0100395 pblk->oob_meta_size = geo->sos;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100396 if (!pblk_is_oob_meta_supported(pblk)) {
397 /* For drives which does not have OOB metadata feature
398 * in order to support recovery feature we need to use
399 * so called packed metadata. Packed metada will store
400 * the same information as OOB metadata (l2p table mapping,
401 * but in the form of the single page at the end of
402 * every write request.
403 */
404 if (pblk->min_write_pgs
405 * sizeof(struct pblk_sec_meta) > PAGE_SIZE) {
406 /* We want to keep all the packed metadata on single
407 * page per write requests. So we need to ensure that
408 * it will fit.
409 *
410 * This is more like sanity check, since there is
411 * no device with such a big minimal write size
412 * (above 1 metabytes).
413 */
414 pblk_err(pblk, "Not supported min write size\n");
415 return -EINVAL;
416 }
417 /* For packed meta approach we do some simplification.
418 * On read path we always issue requests which size
419 * equal to max_write_pgs, with all pages filled with
420 * user payload except of last one page which will be
421 * filled with packed metadata.
422 */
423 pblk->max_write_pgs = pblk->min_write_pgs;
424 pblk->min_write_pgs_data = pblk->min_write_pgs - 1;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100425 }
426
Kees Cook6396bb22018-06-12 14:03:40 -0700427 pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
Javier González43d47122018-03-30 00:05:09 +0200428 GFP_KERNEL);
429 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430 return -ENOMEM;
431
Hans Holmberg1864de92018-10-09 13:11:58 +0200432 if (pblk_get_global_caches())
Javier González43d47122018-03-30 00:05:09 +0200433 goto fail_free_pad_dist;
434
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200435 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400436 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
437 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200438 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400440 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
Hans Holmberg1864de92018-10-09 13:11:58 +0200441 pblk_caches.ws);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400442 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200443 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400445 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200446 pblk_caches.rec);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400447 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200448 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400450 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200451 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400452 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453 goto free_rec_pool;
454
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400455 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200456 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400457 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200458 goto free_r_rq_pool;
459
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400460 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200461 pblk_caches.w_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400462 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200463 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464
Javier Gonzálezef576492017-06-26 11:57:28 +0200465 pblk->close_wq = alloc_workqueue("pblk-close-wq",
466 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
467 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200468 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200469
Javier Gonzálezef576492017-06-26 11:57:28 +0200470 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
471 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
472 if (!pblk->bb_wq)
473 goto free_close_wq;
474
Javier González7bd4d372017-10-13 14:46:23 +0200475 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
476 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
477 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200478 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200479
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200480 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200481 goto free_r_end_wq;
482
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200483 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200484 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200485
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200486 return 0;
487
Javier González7bd4d372017-10-13 14:46:23 +0200488free_r_end_wq:
489 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200490free_bb_wq:
491 destroy_workqueue(pblk->bb_wq);
492free_close_wq:
493 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400495 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200496free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400497 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200498free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400499 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400501 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200502free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400503 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200504free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400505 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200506free_global_caches:
Hans Holmberg1864de92018-10-09 13:11:58 +0200507 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200508fail_free_pad_dist:
509 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200510 return -ENOMEM;
511}
512
513static void pblk_core_free(struct pblk *pblk)
514{
Javier Gonzálezef576492017-06-26 11:57:28 +0200515 if (pblk->close_wq)
516 destroy_workqueue(pblk->close_wq);
517
Javier González7bd4d372017-10-13 14:46:23 +0200518 if (pblk->r_end_wq)
519 destroy_workqueue(pblk->r_end_wq);
520
Javier Gonzálezef576492017-06-26 11:57:28 +0200521 if (pblk->bb_wq)
522 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400524 mempool_exit(&pblk->page_bio_pool);
525 mempool_exit(&pblk->gen_ws_pool);
526 mempool_exit(&pblk->rec_pool);
527 mempool_exit(&pblk->r_rq_pool);
528 mempool_exit(&pblk->e_rq_pool);
529 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200530
Hans Holmberg1864de92018-10-09 13:11:58 +0200531 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200532 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200533}
534
Javier Gonzáleze411b332018-03-30 00:04:59 +0200535static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536{
537 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
538 int i;
539
540 kfree(l_mg->bb_template);
541 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200542 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200543
544 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200545 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200546 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200547 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200549
550 mempool_destroy(l_mg->bitmap_pool);
551 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552}
553
Hans Holmberg48b8d202018-06-01 16:41:06 +0200554static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
555 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200557 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
558
Javier Gonzáleze411b332018-03-30 00:04:59 +0200559 kfree(line->blk_bitmap);
560 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200561 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200562
563 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
564 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200565}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566
Javier Gonzáleze411b332018-03-30 00:04:59 +0200567static void pblk_lines_free(struct pblk *pblk)
568{
569 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
570 struct pblk_line *line;
571 int i;
572
Javier Gonzáleze411b332018-03-30 00:04:59 +0200573 for (i = 0; i < l_mg->nr_lines; i++) {
574 line = &pblk->lines[i];
575
Javier González8e55c072018-06-01 15:04:22 +0200576 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200577 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200578 }
Javier González43d47122018-03-30 00:05:09 +0200579
580 pblk_line_mg_free(pblk);
581
582 kfree(pblk->luns);
583 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200584}
585
Javier González43d47122018-03-30 00:05:09 +0200586static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587{
588 struct nvm_tgt_dev *dev = pblk->dev;
589 struct nvm_geo *geo = &dev->geo;
590 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200591 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592
593 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200594 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200595 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200596 return -EINVAL;
597 }
598
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100599 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
600 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601 if (!pblk->luns)
602 return -ENOMEM;
603
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100604 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200606 int ch = i % geo->num_ch;
607 int lun_raw = i / geo->num_ch;
608 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609
610 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200611 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612
613 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200614 }
615
616 return 0;
617}
618
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200620static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200621{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200622 struct pblk_line_meta *lm = &pblk->lm;
623 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
624 struct nvm_tgt_dev *dev = pblk->dev;
625 struct nvm_geo *geo = &dev->geo;
626
627 /* Round to sector size so that lba_list starts on its own sector */
628 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200629 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200630 sizeof(struct wa_counters), geo->csecs);
631 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200632
633 /* Round to sector size so that vsc_list starts on its own sector */
634 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
635 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200636 geo->csecs);
637 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200638
639 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200640 geo->csecs);
641 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200642
643 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
644
645 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200646}
647
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100648static int pblk_set_provision(struct pblk *pblk, int nr_free_chks)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649{
650 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100651 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
652 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200653 struct nvm_geo *geo = &dev->geo;
654 sector_t provisioned;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100655 int sec_meta, blk_meta, clba;
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100656 int minimum;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657
Javier Gonzáleze5392732018-01-05 14:16:14 +0100658 if (geo->op == NVM_TARGET_DEFAULT_OP)
659 pblk->op = PBLK_DEFAULT_OP;
660 else
661 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100663 minimum = pblk_get_min_chks(pblk);
664 provisioned = nr_free_chks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100665 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200666 sector_div(provisioned, 100);
667
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100668 if ((nr_free_chks - provisioned) < minimum) {
669 if (geo->op != NVM_TARGET_DEFAULT_OP) {
670 pblk_err(pblk, "OP too small to create a sane instance\n");
671 return -EINTR;
672 }
673
674 /* If the user did not specify an OP value, and PBLK_DEFAULT_OP
675 * is not enough, calculate and set sane value
676 */
677
678 provisioned = nr_free_chks - minimum;
679 pblk->op = (100 * minimum) / nr_free_chks;
680 pblk_info(pblk, "Default OP insufficient, adjusting OP to %d\n",
681 pblk->op);
682 }
683
684 pblk->op_blks = nr_free_chks - provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100685
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200686 /* Internally pblk manages all free blocks, but all calculations based
687 * on user capacity consider only provisioned blocks
688 */
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100689 pblk->rl.total_blocks = nr_free_chks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100690
691 /* Consider sectors used for metadata */
692 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200693 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100694
Igor Konopko55d8ec32018-12-11 20:16:26 +0100695 clba = (geo->clba / pblk->min_write_pgs) * pblk->min_write_pgs_data;
696 pblk->capacity = (provisioned - blk_meta) * clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100697
Hans Holmberg3bcebc52018-12-11 20:16:13 +0100698 atomic_set(&pblk->rl.free_blocks, nr_free_chks);
699 atomic_set(&pblk->rl.free_user_blocks, nr_free_chks);
700
701 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200702}
703
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200704static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200705 struct nvm_chk_meta *meta)
706{
707 struct nvm_tgt_dev *dev = pblk->dev;
708 struct nvm_geo *geo = &dev->geo;
709 struct pblk_line_meta *lm = &pblk->lm;
710 int i, nr_bad_chks = 0;
711
712 for (i = 0; i < lm->blk_per_line; i++) {
713 struct pblk_lun *rlun = &pblk->luns[i];
714 struct nvm_chk_meta *chunk;
715 struct nvm_chk_meta *chunk_meta;
716 struct ppa_addr ppa;
717 int pos;
718
719 ppa = rlun->bppa;
720 pos = pblk_ppa_to_pos(geo, ppa);
721 chunk = &line->chks[pos];
722
723 ppa.m.chk = line->id;
724 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
725
726 chunk->state = chunk_meta->state;
727 chunk->type = chunk_meta->type;
728 chunk->wi = chunk_meta->wi;
729 chunk->slba = chunk_meta->slba;
730 chunk->cnlb = chunk_meta->cnlb;
731 chunk->wp = chunk_meta->wp;
732
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200733 trace_pblk_chunk_state(pblk_disk_name(pblk), &ppa,
734 chunk->state);
735
Javier González32ef9412018-03-30 00:05:20 +0200736 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
737 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
738 continue;
739 }
740
Javier González6f9c9602018-06-01 15:04:23 +0200741 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
742 continue;
743
Javier González32ef9412018-03-30 00:05:20 +0200744 set_bit(pos, line->blk_bitmap);
745 nr_bad_chks++;
746 }
747
748 return nr_bad_chks;
749}
750
751static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
752 void *chunk_meta, int line_id)
753{
Javier González32ef9412018-03-30 00:05:20 +0200754 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
755 struct pblk_line_meta *lm = &pblk->lm;
756 long nr_bad_chks, chk_in_line;
757
758 line->pblk = pblk;
759 line->id = line_id;
760 line->type = PBLK_LINETYPE_FREE;
761 line->state = PBLK_LINESTATE_NEW;
762 line->gc_group = PBLK_LINEGC_NONE;
763 line->vsc = &l_mg->vsc_list[line_id];
764 spin_lock_init(&line->lock);
765
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200766 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200767
768 chk_in_line = lm->blk_per_line - nr_bad_chks;
769 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
770 chk_in_line < lm->min_blk_line) {
771 line->state = PBLK_LINESTATE_BAD;
772 list_add_tail(&line->list, &l_mg->bad_list);
773 return 0;
774 }
775
776 atomic_set(&line->blk_in_line, chk_in_line);
777 list_add_tail(&line->list, &l_mg->free_list);
778 l_mg->nr_free_lines++;
779
780 return chk_in_line;
781}
782
783static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200784{
Javier González43d47122018-03-30 00:05:09 +0200785 struct pblk_line_meta *lm = &pblk->lm;
786
787 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
788 if (!line->blk_bitmap)
789 return -ENOMEM;
790
791 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200792 if (!line->erase_bitmap)
793 goto free_blk_bitmap;
794
Javier González43d47122018-03-30 00:05:09 +0200795
Kees Cook6da2ec52018-06-12 13:55:00 -0700796 line->chks = kmalloc_array(lm->blk_per_line,
797 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200798 if (!line->chks)
799 goto free_erase_bitmap;
800
801 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
802 if (!line->w_err_gc)
803 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200804
805 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200806
807free_chks:
808 kfree(line->chks);
809free_erase_bitmap:
810 kfree(line->erase_bitmap);
811free_blk_bitmap:
812 kfree(line->blk_bitmap);
813 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200814}
815
816static int pblk_line_mg_init(struct pblk *pblk)
817{
818 struct nvm_tgt_dev *dev = pblk->dev;
819 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200820 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
821 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200822 int i, bb_distance;
823
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200824 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200825 l_mg->log_line = l_mg->data_line = NULL;
826 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
827 l_mg->nr_free_lines = 0;
828 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
829
830 INIT_LIST_HEAD(&l_mg->free_list);
831 INIT_LIST_HEAD(&l_mg->corrupt_list);
832 INIT_LIST_HEAD(&l_mg->bad_list);
833 INIT_LIST_HEAD(&l_mg->gc_full_list);
834 INIT_LIST_HEAD(&l_mg->gc_high_list);
835 INIT_LIST_HEAD(&l_mg->gc_mid_list);
836 INIT_LIST_HEAD(&l_mg->gc_low_list);
837 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200838 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200839
840 INIT_LIST_HEAD(&l_mg->emeta_list);
841
Hans Holmberg48b8d202018-06-01 16:41:06 +0200842 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
843 l_mg->gc_lists[1] = &l_mg->gc_high_list;
844 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
845 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200846
847 spin_lock_init(&l_mg->free_lock);
848 spin_lock_init(&l_mg->close_lock);
849 spin_lock_init(&l_mg->gc_lock);
850
851 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
852 if (!l_mg->vsc_list)
853 goto fail;
854
855 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
856 if (!l_mg->bb_template)
857 goto fail_free_vsc_list;
858
859 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
860 if (!l_mg->bb_aux)
861 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200862
863 /* smeta is always small enough to fit on a kmalloc memory allocation,
864 * emeta depends on the number of LUNs allocated to the pblk instance
865 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200866 for (i = 0; i < PBLK_DATA_LINES; i++) {
867 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
868 if (!l_mg->sline_meta[i])
869 goto fail_free_smeta;
870 }
871
Hans Holmberg53d82db2018-10-09 13:11:47 +0200872 l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
873 lm->sec_bitmap_len, 0, 0, NULL);
874 if (!l_mg->bitmap_cache)
875 goto fail_free_smeta;
876
877 /* the bitmap pool is used for both valid and map bitmaps */
878 l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
879 l_mg->bitmap_cache);
880 if (!l_mg->bitmap_pool)
881 goto fail_destroy_bitmap_cache;
882
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200883 /* emeta allocates three different buffers for managing metadata with
884 * in-memory and in-media layouts
885 */
886 for (i = 0; i < PBLK_DATA_LINES; i++) {
887 struct pblk_emeta *emeta;
888
889 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
890 if (!emeta)
891 goto fail_free_emeta;
892
893 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
894 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
895
896 emeta->buf = vmalloc(lm->emeta_len[0]);
897 if (!emeta->buf) {
898 kfree(emeta);
899 goto fail_free_emeta;
900 }
901
902 emeta->nr_entries = lm->emeta_sec[0];
903 l_mg->eline_meta[i] = emeta;
904 } else {
905 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
906
907 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
908 if (!emeta->buf) {
909 kfree(emeta);
910 goto fail_free_emeta;
911 }
912
913 emeta->nr_entries = lm->emeta_sec[0];
914 l_mg->eline_meta[i] = emeta;
915 }
916 }
917
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200918 for (i = 0; i < l_mg->nr_lines; i++)
919 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
920
Javier González43d47122018-03-30 00:05:09 +0200921 bb_distance = (geo->all_luns) * geo->ws_opt;
922 for (i = 0; i < lm->sec_per_line; i += bb_distance)
923 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
924
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200925 return 0;
926
927fail_free_emeta:
928 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200929 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
930 vfree(l_mg->eline_meta[i]->buf);
931 else
932 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200933 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200934 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200935
936 mempool_destroy(l_mg->bitmap_pool);
937fail_destroy_bitmap_cache:
938 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200939fail_free_smeta:
940 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200941 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200942 kfree(l_mg->bb_aux);
943fail_free_bb_template:
944 kfree(l_mg->bb_template);
945fail_free_vsc_list:
946 kfree(l_mg->vsc_list);
947fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200948 return -ENOMEM;
949}
950
Javier González43d47122018-03-30 00:05:09 +0200951static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200952{
953 struct nvm_tgt_dev *dev = pblk->dev;
954 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200955 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200956 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200957 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200958
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200959 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100960 lm->blk_per_line = geo->all_luns;
961 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200962 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100963 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200964 lm->mid_thrs = lm->sec_per_line / 2;
965 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100966 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200967
968 /* Calculate necessary pages for smeta. See comment over struct
969 * line_smeta definition
970 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200971 i = 1;
972add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200973 lm->smeta_sec = i * geo->ws_opt;
974 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200975
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200976 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200977 if (smeta_len > lm->smeta_len) {
978 i++;
979 goto add_smeta_page;
980 }
981
982 /* Calculate necessary pages for emeta. See comment over struct
983 * line_emeta definition
984 */
985 i = 1;
986add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200987 lm->emeta_sec[0] = i * geo->ws_opt;
988 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200989
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200990 emeta_len = calc_emeta_len(pblk);
991 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200992 i++;
993 goto add_emeta_page;
994 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200995
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100996 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200997
998 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100999 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +02001000 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001001 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +02001002
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001003 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001004 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001005 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +02001006 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001007 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001008
Javier González43d47122018-03-30 00:05:09 +02001009 return 0;
1010}
1011
1012static int pblk_lines_init(struct pblk *pblk)
1013{
1014 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +02001015 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +02001016 void *chunk_meta;
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001017 int nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +02001018 int i, ret;
1019
1020 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001021 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001022 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001023
Javier González43d47122018-03-30 00:05:09 +02001024 ret = pblk_line_mg_init(pblk);
1025 if (ret)
1026 return ret;
1027
1028 ret = pblk_luns_init(pblk);
1029 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001030 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001031
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001032 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +02001033 if (IS_ERR(chunk_meta)) {
1034 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001035 goto fail_free_luns;
1036 }
1037
1038 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1039 GFP_KERNEL);
1040 if (!pblk->lines) {
1041 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001042 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001043 }
1044
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001045 for (i = 0; i < l_mg->nr_lines; i++) {
1046 line = &pblk->lines[i];
1047
Javier González32ef9412018-03-30 00:05:20 +02001048 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001049 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001050 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001051
Javier González32ef9412018-03-30 00:05:20 +02001052 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Hans Holmbergf2937232018-10-09 13:11:53 +02001053
1054 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1055 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001056 }
1057
Javier González2deeefc2018-06-01 15:04:15 +02001058 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001059 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Wei Yongjuna70985f2018-10-09 13:12:13 +02001060 ret = -EINTR;
1061 goto fail_free_lines;
Javier González2deeefc2018-06-01 15:04:15 +02001062 }
1063
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001064 ret = pblk_set_provision(pblk, nr_free_chks);
1065 if (ret)
1066 goto fail_free_lines;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001067
Javier González090ee262018-10-09 13:12:01 +02001068 vfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001069 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001070
Javier González43d47122018-03-30 00:05:09 +02001071fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001072 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +02001073 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001074 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001075fail_free_chunk_meta:
Hans Holmberg0934ce82018-12-11 20:16:15 +01001076 vfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001077fail_free_luns:
1078 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001079fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001080 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081
1082 return ret;
1083}
1084
1085static int pblk_writer_init(struct pblk *pblk)
1086{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001087 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1088 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001089 int err = PTR_ERR(pblk->writer_ts);
1090
1091 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001092 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001093 err);
1094 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001095 }
1096
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001097 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1098 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1099
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001100 return 0;
1101}
1102
1103static void pblk_writer_stop(struct pblk *pblk)
1104{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001105 /* The pipeline must be stopped and the write buffer emptied before the
1106 * write thread is stopped
1107 */
1108 WARN(pblk_rb_read_count(&pblk->rwb),
1109 "Stopping not fully persisted write buffer\n");
1110
1111 WARN(pblk_rb_sync_count(&pblk->rwb),
1112 "Stopping not fully synced write buffer\n");
1113
Hans Holmberg7be970b2018-03-30 00:04:56 +02001114 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001115 if (pblk->writer_ts)
1116 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117}
1118
1119static void pblk_free(struct pblk *pblk)
1120{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001121 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001122 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001123 pblk_rwb_free(pblk);
1124 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001125
1126 kfree(pblk);
1127}
1128
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001129static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001130{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001131 if (graceful)
1132 __pblk_pipeline_flush(pblk);
1133 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001134 pblk_writer_stop(pblk);
1135 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001136 pblk_rl_free(&pblk->rl);
1137
Matias Bjørling4e495a42018-07-13 10:48:42 +02001138 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001139}
1140
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001141static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001142{
1143 struct pblk *pblk = private;
1144
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001145 pblk_gc_exit(pblk, graceful);
1146 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001147
Matias Bjørling880eda52018-07-13 10:48:37 +02001148#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001149 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001150#endif
1151
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001152 pblk_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153}
1154
1155static sector_t pblk_capacity(void *private)
1156{
1157 struct pblk *pblk = private;
1158
1159 return pblk->capacity * NR_PHY_IN_LOG;
1160}
1161
1162static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1163 int flags)
1164{
1165 struct nvm_geo *geo = &dev->geo;
1166 struct request_queue *bqueue = dev->q;
1167 struct request_queue *tqueue = tdisk->queue;
1168 struct pblk *pblk;
1169 int ret;
1170
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001171 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1172 if (!pblk)
1173 return ERR_PTR(-ENOMEM);
1174
1175 pblk->dev = dev;
1176 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001177 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001178 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001179 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001180
Matias Bjørling4e495a42018-07-13 10:48:42 +02001181 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1182 geo->version == NVM_OCSSD_SPEC_20)) {
1183 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1184 geo->version);
1185 kfree(pblk);
1186 return ERR_PTR(-EINVAL);
1187 }
1188
Igor Konopkoa16816b2018-12-11 20:16:25 +01001189 if (geo->ext) {
1190 pblk_err(pblk, "extended metadata not supported\n");
1191 kfree(pblk);
1192 return ERR_PTR(-EINVAL);
1193 }
1194
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001195 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001196 spin_lock_init(&pblk->trans_lock);
1197 spin_lock_init(&pblk->lock);
1198
Matias Bjørling880eda52018-07-13 10:48:37 +02001199#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001200 atomic_long_set(&pblk->inflight_writes, 0);
1201 atomic_long_set(&pblk->padded_writes, 0);
1202 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001203 atomic_long_set(&pblk->req_writes, 0);
1204 atomic_long_set(&pblk->sub_writes, 0);
1205 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001206 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001207 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001208 atomic_long_set(&pblk->sync_reads, 0);
1209 atomic_long_set(&pblk->recov_writes, 0);
1210 atomic_long_set(&pblk->recov_writes, 0);
1211 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001212 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001213#endif
1214
1215 atomic_long_set(&pblk->read_failed, 0);
1216 atomic_long_set(&pblk->read_empty, 0);
1217 atomic_long_set(&pblk->read_high_ecc, 0);
1218 atomic_long_set(&pblk->read_failed_gc, 0);
1219 atomic_long_set(&pblk->write_failed, 0);
1220 atomic_long_set(&pblk->erase_failed, 0);
1221
Javier González43d47122018-03-30 00:05:09 +02001222 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001223 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001224 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001225 goto fail;
1226 }
1227
1228 ret = pblk_lines_init(pblk);
1229 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001230 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001231 goto fail_free_core;
1232 }
1233
Javier González43d47122018-03-30 00:05:09 +02001234 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001235 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001236 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001237 goto fail_free_lines;
1238 }
1239
1240 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1241 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001242 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001243 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244 }
1245
1246 ret = pblk_writer_init(pblk);
1247 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001248 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001249 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001250 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001251 }
1252
1253 ret = pblk_gc_init(pblk);
1254 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001255 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001256 goto fail_stop_writer;
1257 }
1258
1259 /* inherit the size from the underlying device */
1260 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1261 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1262
1263 blk_queue_write_cache(tqueue, true, false);
1264
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001265 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001266 tqueue->limits.discard_alignment = 0;
1267 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001268 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001269
Matias Bjørling4e495a42018-07-13 10:48:42 +02001270 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001271 geo->all_luns, pblk->l_mg.nr_lines,
Igor Konopko847a3a22019-05-04 20:37:48 +02001272 (unsigned long long)pblk->capacity,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001273 pblk->rwb.nr_entries);
1274
1275 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001276
1277 /* Check if we need to start GC */
1278 pblk_gc_should_kick(pblk);
1279
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001280 return pblk;
1281
1282fail_stop_writer:
1283 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001284fail_free_l2p:
1285 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001286fail_free_rwb:
1287 pblk_rwb_free(pblk);
1288fail_free_lines:
1289 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001290fail_free_core:
1291 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001292fail:
1293 kfree(pblk);
1294 return ERR_PTR(ret);
1295}
1296
1297/* physical block device target */
1298static struct nvm_tgt_type tt_pblk = {
1299 .name = "pblk",
1300 .version = {1, 0, 0},
1301
1302 .make_rq = pblk_make_rq,
1303 .capacity = pblk_capacity,
1304
1305 .init = pblk_init,
1306 .exit = pblk_exit,
1307
1308 .sysfs_init = pblk_sysfs_init,
1309 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001310 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001311};
1312
1313static int __init pblk_module_init(void)
1314{
NeilBrownb25d5232017-06-18 14:38:58 +10001315 int ret;
1316
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001317 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1318 if (ret)
1319 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001320 ret = nvm_register_tgt_type(&tt_pblk);
1321 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001322 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001323 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001324}
1325
1326static void pblk_module_exit(void)
1327{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001328 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001329 nvm_unregister_tgt_type(&tt_pblk);
1330}
1331
1332module_init(pblk_module_init);
1333module_exit(pblk_module_exit);
1334MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1335MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1336MODULE_LICENSE("GPL v2");
1337MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");