blob: 155e42a26293cc525c00958c507964aa4d88360f [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-core.c - pblk's core functionality
16 *
17 */
18
19#include "pblk.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020020
Javier González8bd40022017-10-13 14:46:44 +020021static void pblk_line_mark_bb(struct work_struct *work)
22{
23 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
24 ws);
25 struct pblk *pblk = line_ws->pblk;
26 struct nvm_tgt_dev *dev = pblk->dev;
27 struct ppa_addr *ppa = line_ws->priv;
28 int ret;
29
30 ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
31 if (ret) {
32 struct pblk_line *line;
33 int pos;
34
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010035 line = &pblk->lines[pblk_ppa_to_line(*ppa)];
36 pos = pblk_ppa_to_pos(&dev->geo, *ppa);
Javier González8bd40022017-10-13 14:46:44 +020037
38 pr_err("pblk: failed to mark bb, line:%d, pos:%d\n",
39 line->id, pos);
40 }
41
42 kfree(ppa);
43 mempool_free(line_ws, pblk->gen_ws_pool);
44}
45
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
47 struct ppa_addr *ppa)
48{
49 struct nvm_tgt_dev *dev = pblk->dev;
50 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010051 int pos = pblk_ppa_to_pos(geo, *ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020052
53 pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos);
54 atomic_long_inc(&pblk->erase_failed);
55
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020056 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057 if (test_and_set_bit(pos, line->blk_bitmap))
58 pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n",
59 line->id, pos);
60
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020061 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
62 GFP_ATOMIC, pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020063}
64
65static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
66{
67 struct pblk_line *line;
68
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010069 line = &pblk->lines[pblk_ppa_to_line(rqd->ppa_addr)];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020070 atomic_dec(&line->left_seblks);
71
72 if (rqd->error) {
73 struct ppa_addr *ppa;
74
75 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
76 if (!ppa)
77 return;
78
79 *ppa = rqd->ppa_addr;
80 pblk_mark_bb(pblk, line, ppa);
81 }
Javier González588726d32017-06-26 11:57:29 +020082
83 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020084}
85
86/* Erase completion assumes that only one block is erased at the time */
87static void pblk_end_io_erase(struct nvm_rq *rqd)
88{
89 struct pblk *pblk = rqd->private;
90
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 __pblk_end_io_erase(pblk, rqd);
Javier González0d880392017-10-13 14:46:08 +020092 mempool_free(rqd, pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093}
94
Javier González0880a9a2017-06-26 11:57:19 +020095void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
96 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020097{
98 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
99 struct list_head *move_list = NULL;
100
101 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
102 * table is modified with reclaimed sectors, a check is done to endure
103 * that newer updates are not overwritten.
104 */
105 spin_lock(&line->lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200106 WARN_ON(line->state == PBLK_LINESTATE_FREE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200107
108 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
109 WARN_ONCE(1, "pblk: double invalidate\n");
110 spin_unlock(&line->lock);
111 return;
112 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200113 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200114
115 if (line->state == PBLK_LINESTATE_CLOSED)
116 move_list = pblk_line_gc_list(pblk, line);
117 spin_unlock(&line->lock);
118
119 if (move_list) {
120 spin_lock(&l_mg->gc_lock);
121 spin_lock(&line->lock);
122 /* Prevent moving a line that has just been chosen for GC */
Javier Gonzálezd3401212017-10-13 14:46:14 +0200123 if (line->state == PBLK_LINESTATE_GC) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200124 spin_unlock(&line->lock);
125 spin_unlock(&l_mg->gc_lock);
126 return;
127 }
128 spin_unlock(&line->lock);
129
130 list_move_tail(&line->list, move_list);
131 spin_unlock(&l_mg->gc_lock);
132 }
133}
134
135void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
136{
137 struct pblk_line *line;
138 u64 paddr;
139 int line_id;
140
141#ifdef CONFIG_NVM_DEBUG
142 /* Callers must ensure that the ppa points to a device address */
143 BUG_ON(pblk_addr_in_cache(ppa));
144 BUG_ON(pblk_ppa_empty(ppa));
145#endif
146
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100147 line_id = pblk_ppa_to_line(ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200148 line = &pblk->lines[line_id];
149 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
150
151 __pblk_map_invalidate(pblk, line, paddr);
152}
153
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200154static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
155 unsigned int nr_secs)
156{
157 sector_t lba;
158
159 spin_lock(&pblk->trans_lock);
160 for (lba = slba; lba < slba + nr_secs; lba++) {
161 struct ppa_addr ppa;
162
163 ppa = pblk_trans_map_get(pblk, lba);
164
165 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
166 pblk_map_invalidate(pblk, ppa);
167
168 pblk_ppa_set_empty(&ppa);
169 pblk_trans_map_set(pblk, lba, ppa);
170 }
171 spin_unlock(&pblk->trans_lock);
172}
173
Javier González67bf26a2017-10-13 14:46:20 +0200174/* Caller must guarantee that the request is a valid type */
175struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200176{
177 mempool_t *pool;
178 struct nvm_rq *rqd;
179 int rq_size;
180
Javier González67bf26a2017-10-13 14:46:20 +0200181 switch (type) {
182 case PBLK_WRITE:
183 case PBLK_WRITE_INT:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200184 pool = pblk->w_rq_pool;
185 rq_size = pblk_w_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200186 break;
187 case PBLK_READ:
Javier González0d880392017-10-13 14:46:08 +0200188 pool = pblk->r_rq_pool;
Javier González084ec9b2017-06-26 16:27:13 -0600189 rq_size = pblk_g_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200190 break;
191 default:
192 pool = pblk->e_rq_pool;
193 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194 }
195
196 rqd = mempool_alloc(pool, GFP_KERNEL);
197 memset(rqd, 0, rq_size);
198
199 return rqd;
200}
201
Javier González67bf26a2017-10-13 14:46:20 +0200202/* Typically used on completion path. Cannot guarantee request consistency */
203void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200204{
Javier González67bf26a2017-10-13 14:46:20 +0200205 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206 mempool_t *pool;
207
Javier González67bf26a2017-10-13 14:46:20 +0200208 switch (type) {
209 case PBLK_WRITE:
210 kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
211 case PBLK_WRITE_INT:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212 pool = pblk->w_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200213 break;
214 case PBLK_READ:
Javier González0d880392017-10-13 14:46:08 +0200215 pool = pblk->r_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200216 break;
217 case PBLK_ERASE:
218 pool = pblk->e_rq_pool;
219 break;
220 default:
221 pr_err("pblk: trying to free unknown rqd type\n");
222 return;
223 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224
Javier González67bf26a2017-10-13 14:46:20 +0200225 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200226 mempool_free(rqd, pool);
227}
228
229void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
230 int nr_pages)
231{
232 struct bio_vec bv;
233 int i;
234
235 WARN_ON(off + nr_pages != bio->bi_vcnt);
236
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200237 for (i = off; i < nr_pages + off; i++) {
238 bv = bio->bi_io_vec[i];
Javier Gonzálezbd432412017-10-13 14:46:06 +0200239 mempool_free(bv.bv_page, pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200240 }
241}
242
243int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
244 int nr_pages)
245{
246 struct request_queue *q = pblk->dev->q;
247 struct page *page;
248 int i, ret;
249
250 for (i = 0; i < nr_pages; i++) {
Javier Gonzálezbd432412017-10-13 14:46:06 +0200251 page = mempool_alloc(pblk->page_bio_pool, flags);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252
253 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
254 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
255 pr_err("pblk: could not add page to bio\n");
Javier Gonzálezbd432412017-10-13 14:46:06 +0200256 mempool_free(page, pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200257 goto err;
258 }
259 }
260
261 return 0;
262err:
263 pblk_bio_free_pages(pblk, bio, 0, i - 1);
264 return -1;
265}
266
267static void pblk_write_kick(struct pblk *pblk)
268{
269 wake_up_process(pblk->writer_ts);
270 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
271}
272
Kees Cook87c1d2d2017-10-17 21:10:19 -0700273void pblk_write_timer_fn(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700275 struct pblk *pblk = from_timer(pblk, t, wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200276
277 /* kick the write thread every tick to flush outstanding data */
278 pblk_write_kick(pblk);
279}
280
281void pblk_write_should_kick(struct pblk *pblk)
282{
283 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
284
285 if (secs_avail >= pblk->min_write_pgs)
286 pblk_write_kick(pblk);
287}
288
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200289void pblk_end_io_sync(struct nvm_rq *rqd)
290{
291 struct completion *waiting = rqd->private;
292
293 complete(waiting);
294}
295
Javier González8bd40022017-10-13 14:46:44 +0200296static void pblk_wait_for_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297{
Javier González588726d32017-06-26 11:57:29 +0200298 do {
299 if (!atomic_read(&pblk->inflight_io))
300 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301
Javier González588726d32017-06-26 11:57:29 +0200302 schedule();
303 } while (1);
304}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305
Javier González588726d32017-06-26 11:57:29 +0200306static void pblk_flush_writer(struct pblk *pblk)
307{
308 pblk_rb_flush(&pblk->rwb);
309 do {
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200310 if (!pblk_rb_sync_count(&pblk->rwb))
Javier González588726d32017-06-26 11:57:29 +0200311 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200312
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200313 pblk_write_kick(pblk);
Javier González588726d32017-06-26 11:57:29 +0200314 schedule();
315 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200316}
317
318struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
319{
320 struct pblk_line_meta *lm = &pblk->lm;
321 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
322 struct list_head *move_list = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200323 int vsc = le32_to_cpu(*line->vsc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200324
Javier González476118c2017-06-26 11:57:26 +0200325 lockdep_assert_held(&line->lock);
326
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200327 if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200328 if (line->gc_group != PBLK_LINEGC_FULL) {
329 line->gc_group = PBLK_LINEGC_FULL;
330 move_list = &l_mg->gc_full_list;
331 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200332 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200333 if (line->gc_group != PBLK_LINEGC_HIGH) {
334 line->gc_group = PBLK_LINEGC_HIGH;
335 move_list = &l_mg->gc_high_list;
336 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200337 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200338 if (line->gc_group != PBLK_LINEGC_MID) {
339 line->gc_group = PBLK_LINEGC_MID;
340 move_list = &l_mg->gc_mid_list;
341 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200342 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200343 if (line->gc_group != PBLK_LINEGC_LOW) {
344 line->gc_group = PBLK_LINEGC_LOW;
345 move_list = &l_mg->gc_low_list;
346 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200347 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200348 if (line->gc_group != PBLK_LINEGC_EMPTY) {
349 line->gc_group = PBLK_LINEGC_EMPTY;
350 move_list = &l_mg->gc_empty_list;
351 }
352 } else {
353 line->state = PBLK_LINESTATE_CORRUPT;
354 line->gc_group = PBLK_LINEGC_NONE;
355 move_list = &l_mg->corrupt_list;
356 pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200357 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358 line->sec_in_line,
359 lm->high_thrs, lm->mid_thrs);
360 }
361
362 return move_list;
363}
364
365void pblk_discard(struct pblk *pblk, struct bio *bio)
366{
367 sector_t slba = pblk_get_lba(bio);
368 sector_t nr_secs = pblk_get_secs(bio);
369
370 pblk_invalidate_range(pblk, slba, nr_secs);
371}
372
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200373void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
374{
375 atomic_long_inc(&pblk->write_failed);
376#ifdef CONFIG_NVM_DEBUG
377 pblk_print_failed_rqd(pblk, rqd, rqd->error);
378#endif
379}
380
381void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
382{
383 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
384 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
385 atomic_long_inc(&pblk->read_empty);
386 return;
387 }
388
389 switch (rqd->error) {
390 case NVM_RSP_WARN_HIGHECC:
391 atomic_long_inc(&pblk->read_high_ecc);
392 break;
393 case NVM_RSP_ERR_FAILECC:
394 case NVM_RSP_ERR_FAILCRC:
395 atomic_long_inc(&pblk->read_failed);
396 break;
397 default:
398 pr_err("pblk: unknown read error:%d\n", rqd->error);
399 }
400#ifdef CONFIG_NVM_DEBUG
401 pblk_print_failed_rqd(pblk, rqd, rqd->error);
402#endif
403}
404
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200405void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
406{
407 pblk->sec_per_write = sec_per_write;
408}
409
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200410int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
411{
412 struct nvm_tgt_dev *dev = pblk->dev;
413
414#ifdef CONFIG_NVM_DEBUG
Javier González1a94b2d2017-10-13 14:46:47 +0200415 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416
Javier González1a94b2d2017-10-13 14:46:47 +0200417 ret = pblk_check_io(pblk, rqd);
418 if (ret)
419 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200420#endif
Javier González588726d32017-06-26 11:57:29 +0200421
422 atomic_inc(&pblk->inflight_io);
423
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424 return nvm_submit_io(dev, rqd);
425}
426
Javier González1a94b2d2017-10-13 14:46:47 +0200427int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
428{
429 struct nvm_tgt_dev *dev = pblk->dev;
430
431#ifdef CONFIG_NVM_DEBUG
432 int ret;
433
434 ret = pblk_check_io(pblk, rqd);
435 if (ret)
436 return ret;
437#endif
438
439 atomic_inc(&pblk->inflight_io);
440
441 return nvm_submit_io_sync(dev, rqd);
442}
443
Javier González55e836d2017-10-13 14:46:16 +0200444static void pblk_bio_map_addr_endio(struct bio *bio)
445{
446 bio_put(bio);
447}
448
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
450 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200451 int alloc_type, gfp_t gfp_mask)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452{
453 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 void *kaddr = data;
455 struct page *page;
456 struct bio *bio;
457 int i, ret;
458
Javier Gonzálezde54e702017-06-30 17:56:39 +0200459 if (alloc_type == PBLK_KMALLOC_META)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200460 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
461
462 bio = bio_kmalloc(gfp_mask, nr_secs);
463 if (!bio)
464 return ERR_PTR(-ENOMEM);
465
466 for (i = 0; i < nr_secs; i++) {
467 page = vmalloc_to_page(kaddr);
468 if (!page) {
469 pr_err("pblk: could not map vmalloc bio\n");
470 bio_put(bio);
471 bio = ERR_PTR(-ENOMEM);
472 goto out;
473 }
474
475 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
476 if (ret != PAGE_SIZE) {
477 pr_err("pblk: could not add page to bio\n");
478 bio_put(bio);
479 bio = ERR_PTR(-ENOMEM);
480 goto out;
481 }
482
483 kaddr += PAGE_SIZE;
484 }
Javier González55e836d2017-10-13 14:46:16 +0200485
486 bio->bi_end_io = pblk_bio_map_addr_endio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487out:
488 return bio;
489}
490
491int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
492 unsigned long secs_to_flush)
493{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200494 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495 int min = pblk->min_write_pgs;
496 int secs_to_sync = 0;
497
498 if (secs_avail >= max)
499 secs_to_sync = max;
500 else if (secs_avail >= min)
501 secs_to_sync = min * (secs_avail / min);
502 else if (secs_to_flush)
503 secs_to_sync = min;
504
505 return secs_to_sync;
506}
507
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200508void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
509{
510 u64 addr;
511 int i;
512
Rakesh Pandite57903f2017-10-13 14:45:56 +0200513 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200514 addr = find_next_zero_bit(line->map_bitmap,
515 pblk->lm.sec_per_line, line->cur_sec);
516 line->cur_sec = addr - nr_secs;
517
518 for (i = 0; i < nr_secs; i++, line->cur_sec--)
519 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
Rakesh Pandite57903f2017-10-13 14:45:56 +0200520 spin_unlock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200521}
522
523u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524{
525 u64 addr;
526 int i;
527
Javier González476118c2017-06-26 11:57:26 +0200528 lockdep_assert_held(&line->lock);
529
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200530 /* logic error: ppa out-of-bounds. Prevent generating bad address */
531 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
532 WARN(1, "pblk: page allocation out of bounds\n");
533 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
534 }
535
536 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
537 pblk->lm.sec_per_line, line->cur_sec);
538 for (i = 0; i < nr_secs; i++, line->cur_sec++)
539 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
540
541 return addr;
542}
543
544u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
545{
546 u64 addr;
547
548 /* Lock needed in case a write fails and a recovery needs to remap
549 * failed write buffer entries
550 */
551 spin_lock(&line->lock);
552 addr = __pblk_alloc_page(pblk, line, nr_secs);
553 line->left_msecs -= nr_secs;
554 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
555 spin_unlock(&line->lock);
556
557 return addr;
558}
559
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200560u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
561{
562 u64 paddr;
563
564 spin_lock(&line->lock);
565 paddr = find_next_zero_bit(line->map_bitmap,
566 pblk->lm.sec_per_line, line->cur_sec);
567 spin_unlock(&line->lock);
568
569 return paddr;
570}
571
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200572/*
573 * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when
574 * taking the per LUN semaphore.
575 */
576static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200577 void *emeta_buf, u64 paddr, int dir)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578{
579 struct nvm_tgt_dev *dev = pblk->dev;
580 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezde54e702017-06-30 17:56:39 +0200581 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582 struct pblk_line_meta *lm = &pblk->lm;
Javier González63e38092017-06-26 11:57:24 +0200583 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200584 struct bio *bio;
585 struct nvm_rq rqd;
Javier González63e38092017-06-26 11:57:24 +0200586 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200588 int left_ppas = lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200589 int id = line->id;
590 int rq_ppas, rq_len;
591 int cmd_op, bio_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592 int i, j;
593 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200595 if (dir == PBLK_WRITE) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200596 bio_op = REQ_OP_WRITE;
597 cmd_op = NVM_OP_PWRITE;
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200598 } else if (dir == PBLK_READ) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200599 bio_op = REQ_OP_READ;
600 cmd_op = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601 } else
602 return -EINVAL;
603
Javier González63e38092017-06-26 11:57:24 +0200604 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
605 &dma_meta_list);
606 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607 return -ENOMEM;
608
Javier González63e38092017-06-26 11:57:24 +0200609 ppa_list = meta_list + pblk_dma_meta_size;
610 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
611
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612next_rq:
613 memset(&rqd, 0, sizeof(struct nvm_rq));
614
615 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
616 rq_len = rq_ppas * geo->sec_size;
617
Javier Gonzálezde54e702017-06-30 17:56:39 +0200618 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
619 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620 if (IS_ERR(bio)) {
621 ret = PTR_ERR(bio);
622 goto free_rqd_dma;
623 }
624
625 bio->bi_iter.bi_sector = 0; /* internal bio */
626 bio_set_op_attrs(bio, bio_op, 0);
627
628 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200629 rqd.meta_list = meta_list;
630 rqd.ppa_list = ppa_list;
631 rqd.dma_meta_list = dma_meta_list;
632 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633 rqd.opcode = cmd_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200636 if (dir == PBLK_WRITE) {
Javier González63e38092017-06-26 11:57:24 +0200637 struct pblk_sec_meta *meta_list = rqd.meta_list;
638
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200639 rqd.flags = pblk_set_progr_mode(pblk, PBLK_WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200640 for (i = 0; i < rqd.nr_ppas; ) {
641 spin_lock(&line->lock);
642 paddr = __pblk_alloc_page(pblk, line, min);
643 spin_unlock(&line->lock);
Javier González63e38092017-06-26 11:57:24 +0200644 for (j = 0; j < min; j++, i++, paddr++) {
645 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200646 rqd.ppa_list[i] =
647 addr_to_gen_ppa(pblk, paddr, id);
Javier González63e38092017-06-26 11:57:24 +0200648 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649 }
650 } else {
651 for (i = 0; i < rqd.nr_ppas; ) {
652 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100653 int pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200654 int read_type = PBLK_READ_RANDOM;
655
656 if (pblk_io_aligned(pblk, rq_ppas))
657 read_type = PBLK_READ_SEQUENTIAL;
658 rqd.flags = pblk_set_read_mode(pblk, read_type);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200659
660 while (test_bit(pos, line->blk_bitmap)) {
661 paddr += min;
662 if (pblk_boundary_paddr_checks(pblk, paddr)) {
663 pr_err("pblk: corrupt emeta line:%d\n",
664 line->id);
665 bio_put(bio);
666 ret = -EINTR;
667 goto free_rqd_dma;
668 }
669
670 ppa = addr_to_gen_ppa(pblk, paddr, id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100671 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 }
673
674 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
675 pr_err("pblk: corrupt emeta line:%d\n",
676 line->id);
677 bio_put(bio);
678 ret = -EINTR;
679 goto free_rqd_dma;
680 }
681
682 for (j = 0; j < min; j++, i++, paddr++)
683 rqd.ppa_list[i] =
684 addr_to_gen_ppa(pblk, paddr, line->id);
685 }
686 }
687
Javier González1a94b2d2017-10-13 14:46:47 +0200688 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200689 if (ret) {
690 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
691 bio_put(bio);
692 goto free_rqd_dma;
693 }
694
Javier González588726d32017-06-26 11:57:29 +0200695 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200696
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200697 if (rqd.error) {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200698 if (dir == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200699 pblk_log_write_err(pblk, &rqd);
700 else
701 pblk_log_read_err(pblk, &rqd);
702 }
703
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200704 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200705 left_ppas -= rq_ppas;
706 if (left_ppas)
707 goto next_rq;
708free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200709 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200710 return ret;
711}
712
713u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
714{
715 struct nvm_tgt_dev *dev = pblk->dev;
716 struct nvm_geo *geo = &dev->geo;
717 struct pblk_line_meta *lm = &pblk->lm;
718 int bit;
719
720 /* This usually only happens on bad lines */
721 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
722 if (bit >= lm->blk_per_line)
723 return -1;
724
725 return bit * geo->sec_per_pl;
726}
727
728static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
729 u64 paddr, int dir)
730{
731 struct nvm_tgt_dev *dev = pblk->dev;
732 struct pblk_line_meta *lm = &pblk->lm;
733 struct bio *bio;
734 struct nvm_rq rqd;
735 __le64 *lba_list = NULL;
736 int i, ret;
737 int cmd_op, bio_op;
738 int flags;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200739
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200740 if (dir == PBLK_WRITE) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200741 bio_op = REQ_OP_WRITE;
742 cmd_op = NVM_OP_PWRITE;
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200743 flags = pblk_set_progr_mode(pblk, PBLK_WRITE);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200744 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier González8f554592018-01-05 14:16:16 +0100745 } else if (dir == PBLK_READ_RECOV || dir == PBLK_READ) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746 bio_op = REQ_OP_READ;
747 cmd_op = NVM_OP_PREAD;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200748 flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200749 } else
750 return -EINVAL;
751
752 memset(&rqd, 0, sizeof(struct nvm_rq));
753
Javier González63e38092017-06-26 11:57:24 +0200754 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
755 &rqd.dma_meta_list);
756 if (!rqd.meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757 return -ENOMEM;
758
Javier González63e38092017-06-26 11:57:24 +0200759 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
760 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
761
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
763 if (IS_ERR(bio)) {
764 ret = PTR_ERR(bio);
765 goto free_ppa_list;
766 }
767
768 bio->bi_iter.bi_sector = 0; /* internal bio */
769 bio_set_op_attrs(bio, bio_op, 0);
770
771 rqd.bio = bio;
772 rqd.opcode = cmd_op;
773 rqd.flags = flags;
774 rqd.nr_ppas = lm->smeta_sec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200775
776 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
Javier González63e38092017-06-26 11:57:24 +0200777 struct pblk_sec_meta *meta_list = rqd.meta_list;
778
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200779 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
Javier González63e38092017-06-26 11:57:24 +0200780
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200781 if (dir == PBLK_WRITE) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200782 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier González63e38092017-06-26 11:57:24 +0200783
784 meta_list[i].lba = lba_list[paddr] = addr_empty;
785 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200786 }
787
788 /*
789 * This I/O is sent by the write thread when a line is replace. Since
790 * the write thread is the only one sending write and erase commands,
791 * there is no need to take the LUN semaphore.
792 */
Javier González1a94b2d2017-10-13 14:46:47 +0200793 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794 if (ret) {
795 pr_err("pblk: smeta I/O submission failed: %d\n", ret);
796 bio_put(bio);
797 goto free_ppa_list;
798 }
799
Javier González588726d32017-06-26 11:57:29 +0200800 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200801
802 if (rqd.error) {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200803 if (dir == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200804 pblk_log_write_err(pblk, &rqd);
Javier González8f554592018-01-05 14:16:16 +0100805 else if (dir == PBLK_READ)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200806 pblk_log_read_err(pblk, &rqd);
807 }
808
809free_ppa_list:
Javier González63e38092017-06-26 11:57:24 +0200810 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200811
812 return ret;
813}
814
815int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
816{
817 u64 bpaddr = pblk_line_smeta_start(pblk, line);
818
Javier González8f554592018-01-05 14:16:16 +0100819 return pblk_line_submit_smeta_io(pblk, line, bpaddr, PBLK_READ_RECOV);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200820}
821
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200822int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
823 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200824{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200825 return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200826 line->emeta_ssec, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200827}
828
829static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
830 struct ppa_addr ppa)
831{
832 rqd->opcode = NVM_OP_ERASE;
833 rqd->ppa_addr = ppa;
834 rqd->nr_ppas = 1;
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200835 rqd->flags = pblk_set_progr_mode(pblk, PBLK_ERASE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836 rqd->bio = NULL;
837}
838
839static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
840{
841 struct nvm_rq rqd;
Javier González588726d32017-06-26 11:57:29 +0200842 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200843
844 memset(&rqd, 0, sizeof(struct nvm_rq));
845
846 pblk_setup_e_rq(pblk, &rqd, ppa);
847
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200848 /* The write thread schedules erases so that it minimizes disturbances
849 * with writes. Thus, there is no need to take the LUN semaphore.
850 */
Javier González1a94b2d2017-10-13 14:46:47 +0200851 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852 if (ret) {
853 struct nvm_tgt_dev *dev = pblk->dev;
854 struct nvm_geo *geo = &dev->geo;
855
856 pr_err("pblk: could not sync erase line:%d,blk:%d\n",
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100857 pblk_ppa_to_line(ppa),
858 pblk_ppa_to_pos(geo, ppa));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200859
860 rqd.error = ret;
861 goto out;
862 }
863
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200864out:
865 rqd.private = pblk;
866 __pblk_end_io_erase(pblk, &rqd);
867
Javier González588726d32017-06-26 11:57:29 +0200868 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869}
870
871int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
872{
873 struct pblk_line_meta *lm = &pblk->lm;
874 struct ppa_addr ppa;
Javier González588726d32017-06-26 11:57:29 +0200875 int ret, bit = -1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200876
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200877 /* Erase only good blocks, one at a time */
878 do {
879 spin_lock(&line->lock);
880 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
881 bit + 1);
882 if (bit >= lm->blk_per_line) {
883 spin_unlock(&line->lock);
884 break;
885 }
886
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200887 ppa = pblk->luns[bit].bppa; /* set ch and lun */
888 ppa.g.blk = line->id;
889
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200890 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200891 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200892 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893
Javier González588726d32017-06-26 11:57:29 +0200894 ret = pblk_blk_erase_sync(pblk, ppa);
895 if (ret) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200896 pr_err("pblk: failed to erase line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +0200897 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200899 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200900
901 return 0;
902}
903
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200904static void pblk_line_setup_metadata(struct pblk_line *line,
905 struct pblk_line_mgmt *l_mg,
906 struct pblk_line_meta *lm)
907{
908 int meta_line;
909
Javier González588726d32017-06-26 11:57:29 +0200910 lockdep_assert_held(&l_mg->free_lock);
911
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200912retry_meta:
913 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
914 if (meta_line == PBLK_DATA_LINES) {
915 spin_unlock(&l_mg->free_lock);
916 io_schedule();
917 spin_lock(&l_mg->free_lock);
918 goto retry_meta;
919 }
920
921 set_bit(meta_line, &l_mg->meta_bitmap);
922 line->meta_line = meta_line;
923
924 line->smeta = l_mg->sline_meta[meta_line];
925 line->emeta = l_mg->eline_meta[meta_line];
926
927 memset(line->smeta, 0, lm->smeta_len);
928 memset(line->emeta->buf, 0, lm->emeta_len[0]);
929
930 line->emeta->mem = 0;
931 atomic_set(&line->emeta->sync, 0);
932}
933
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200934/* For now lines are always assumed full lines. Thus, smeta former and current
935 * lun bitmaps are omitted.
936 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200937static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200938 struct pblk_line *cur)
939{
940 struct nvm_tgt_dev *dev = pblk->dev;
941 struct nvm_geo *geo = &dev->geo;
942 struct pblk_line_meta *lm = &pblk->lm;
943 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200944 struct pblk_emeta *emeta = line->emeta;
945 struct line_emeta *emeta_buf = emeta->buf;
946 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200947 int nr_blk_line;
948
949 /* After erasing the line, new bad blocks might appear and we risk
950 * having an invalid line
951 */
952 nr_blk_line = lm->blk_per_line -
953 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
954 if (nr_blk_line < lm->min_blk_line) {
955 spin_lock(&l_mg->free_lock);
956 spin_lock(&line->lock);
957 line->state = PBLK_LINESTATE_BAD;
958 spin_unlock(&line->lock);
959
960 list_add_tail(&line->list, &l_mg->bad_list);
961 spin_unlock(&l_mg->free_lock);
962
963 pr_debug("pblk: line %d is bad\n", line->id);
964
965 return 0;
966 }
967
968 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200969 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200970
971 /* Mark LUNs allocated in this line (all for now) */
972 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
973
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200974 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
975 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
976 smeta_buf->header.id = cpu_to_le32(line->id);
977 smeta_buf->header.type = cpu_to_le16(line->type);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200978 smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
979 smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200980
981 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200982 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100983 smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200984
985 /* Fill metadata among lines */
986 if (cur) {
987 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200988 smeta_buf->prev_id = cpu_to_le32(cur->id);
989 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200990 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200991 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200992 }
993
994 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200995 smeta_buf->header.crc = cpu_to_le32(
996 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
997 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200998
999 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001000 memcpy(&emeta_buf->header, &smeta_buf->header,
1001 sizeof(struct line_header));
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001002
1003 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1004 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1005 emeta_buf->header.crc = cpu_to_le32(
1006 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1007
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001008 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1009 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1010 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1011 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1012 emeta_buf->crc = cpu_to_le32(0);
1013 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001014
1015 return 1;
1016}
1017
1018/* For now lines are always assumed full lines. Thus, smeta former and current
1019 * lun bitmaps are omitted.
1020 */
1021static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1022 int init)
1023{
1024 struct nvm_tgt_dev *dev = pblk->dev;
1025 struct nvm_geo *geo = &dev->geo;
1026 struct pblk_line_meta *lm = &pblk->lm;
1027 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1028 int nr_bb = 0;
1029 u64 off;
1030 int bit = -1;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001031 int emeta_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001032
1033 line->sec_in_line = lm->sec_per_line;
1034
1035 /* Capture bad block information on line mapping bitmaps */
1036 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1037 bit + 1)) < lm->blk_per_line) {
1038 off = bit * geo->sec_per_pl;
1039 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1040 lm->sec_per_line);
1041 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1042 lm->sec_per_line);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001043 line->sec_in_line -= geo->sec_per_chk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001044 if (bit >= lm->emeta_bb)
1045 nr_bb++;
1046 }
1047
1048 /* Mark smeta metadata sectors as bad sectors */
1049 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1050 off = bit * geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001051 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1052 line->sec_in_line -= lm->smeta_sec;
1053 line->smeta_ssec = off;
1054 line->cur_sec = off + lm->smeta_sec;
1055
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001056 if (init && pblk_line_submit_smeta_io(pblk, line, off, PBLK_WRITE)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001057 pr_debug("pblk: line smeta I/O failed. Retry\n");
Javier González588726d32017-06-26 11:57:29 +02001058 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001059 }
1060
1061 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1062
1063 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1064 * blocks to make sure that there are enough sectors to store emeta
1065 */
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001066 emeta_secs = lm->emeta_sec[0];
1067 off = lm->sec_per_line;
1068 while (emeta_secs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001069 off -= geo->sec_per_pl;
1070 if (!test_bit(off, line->invalid_bitmap)) {
1071 bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl);
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001072 emeta_secs -= geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001073 }
1074 }
1075
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001076 line->emeta_ssec = off;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001077 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001078 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001079 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001080 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081
1082 if (lm->sec_per_line - line->sec_in_line !=
1083 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1084 spin_lock(&line->lock);
1085 line->state = PBLK_LINESTATE_BAD;
1086 spin_unlock(&line->lock);
1087
1088 list_add_tail(&line->list, &l_mg->bad_list);
1089 pr_err("pblk: unexpected line %d is bad\n", line->id);
1090
1091 return 0;
1092 }
1093
1094 return 1;
1095}
1096
1097static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1098{
1099 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001100 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001101
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001102 line->map_bitmap = kzalloc(lm->sec_bitmap_len, GFP_ATOMIC);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001103 if (!line->map_bitmap)
1104 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001106 /* will be initialized using bb info from map_bitmap */
1107 line->invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_ATOMIC);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001108 if (!line->invalid_bitmap) {
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001109 kfree(line->map_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110 return -ENOMEM;
1111 }
1112
1113 spin_lock(&line->lock);
1114 if (line->state != PBLK_LINESTATE_FREE) {
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001115 kfree(line->map_bitmap);
1116 kfree(line->invalid_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117 spin_unlock(&line->lock);
Javier González588726d32017-06-26 11:57:29 +02001118 WARN(1, "pblk: corrupted line %d, state %d\n",
1119 line->id, line->state);
1120 return -EAGAIN;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001121 }
Javier González588726d32017-06-26 11:57:29 +02001122
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123 line->state = PBLK_LINESTATE_OPEN;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001124
1125 atomic_set(&line->left_eblks, blk_in_line);
1126 atomic_set(&line->left_seblks, blk_in_line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001127
1128 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001129 spin_unlock(&line->lock);
1130
1131 /* Bad blocks do not need to be erased */
1132 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001133
1134 kref_init(&line->ref);
1135
1136 return 0;
1137}
1138
1139int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1140{
1141 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1142 int ret;
1143
1144 spin_lock(&l_mg->free_lock);
1145 l_mg->data_line = line;
1146 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001147
1148 ret = pblk_line_prepare(pblk, line);
1149 if (ret) {
1150 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001151 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001152 return ret;
1153 }
Javier González3dc001f2017-04-22 01:32:45 +02001154 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001155
Javier Gonzáleza7689932018-01-05 14:16:13 +01001156 pblk_rl_free_lines_dec(&pblk->rl, line, true);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001157
1158 if (!pblk_line_init_bb(pblk, line, 0)) {
1159 list_add(&line->list, &l_mg->free_list);
1160 return -EINTR;
1161 }
1162
1163 return 0;
1164}
1165
1166void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1167{
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001168 kfree(line->map_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001169 line->map_bitmap = NULL;
1170 line->smeta = NULL;
1171 line->emeta = NULL;
1172}
1173
1174struct pblk_line *pblk_line_get(struct pblk *pblk)
1175{
1176 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1177 struct pblk_line_meta *lm = &pblk->lm;
Javier González588726d32017-06-26 11:57:29 +02001178 struct pblk_line *line;
1179 int ret, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001180
1181 lockdep_assert_held(&l_mg->free_lock);
1182
Javier González588726d32017-06-26 11:57:29 +02001183retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001184 if (list_empty(&l_mg->free_list)) {
1185 pr_err("pblk: no free lines\n");
Javier González588726d32017-06-26 11:57:29 +02001186 return NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001187 }
1188
1189 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1190 list_del(&line->list);
1191 l_mg->nr_free_lines--;
1192
1193 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1194 if (unlikely(bit >= lm->blk_per_line)) {
1195 spin_lock(&line->lock);
1196 line->state = PBLK_LINESTATE_BAD;
1197 spin_unlock(&line->lock);
1198
1199 list_add_tail(&line->list, &l_mg->bad_list);
1200
1201 pr_debug("pblk: line %d is bad\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001202 goto retry;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001203 }
1204
Javier González588726d32017-06-26 11:57:29 +02001205 ret = pblk_line_prepare(pblk, line);
1206 if (ret) {
1207 if (ret == -EAGAIN) {
1208 list_add(&line->list, &l_mg->corrupt_list);
1209 goto retry;
1210 } else {
1211 pr_err("pblk: failed to prepare line %d\n", line->id);
1212 list_add(&line->list, &l_mg->free_list);
1213 l_mg->nr_free_lines++;
1214 return NULL;
1215 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001216 }
1217
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001218 return line;
1219}
1220
1221static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1222 struct pblk_line *line)
1223{
1224 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1225 struct pblk_line *retry_line;
1226
Javier González588726d32017-06-26 11:57:29 +02001227retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001228 spin_lock(&l_mg->free_lock);
1229 retry_line = pblk_line_get(pblk);
1230 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001231 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001232 spin_unlock(&l_mg->free_lock);
1233 return NULL;
1234 }
1235
1236 retry_line->smeta = line->smeta;
1237 retry_line->emeta = line->emeta;
1238 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001239
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001240 pblk_line_free(pblk, line);
Javier González3dc001f2017-04-22 01:32:45 +02001241 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001242 spin_unlock(&l_mg->free_lock);
1243
Javier Gonzáleza7689932018-01-05 14:16:13 +01001244 pblk_rl_free_lines_dec(&pblk->rl, line, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001245
Javier González588726d32017-06-26 11:57:29 +02001246 if (pblk_line_erase(pblk, retry_line))
1247 goto retry;
1248
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001249 return retry_line;
1250}
1251
Javier González588726d32017-06-26 11:57:29 +02001252static void pblk_set_space_limit(struct pblk *pblk)
1253{
1254 struct pblk_rl *rl = &pblk->rl;
1255
1256 atomic_set(&rl->rb_space, 0);
1257}
1258
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001259struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1260{
1261 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1262 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001263
1264 spin_lock(&l_mg->free_lock);
1265 line = pblk_line_get(pblk);
1266 if (!line) {
1267 spin_unlock(&l_mg->free_lock);
1268 return NULL;
1269 }
1270
1271 line->seq_nr = l_mg->d_seq_nr++;
1272 line->type = PBLK_LINETYPE_DATA;
1273 l_mg->data_line = line;
1274
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001275 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001276
1277 /* Allocate next line for preparation */
1278 l_mg->data_next = pblk_line_get(pblk);
Javier González588726d32017-06-26 11:57:29 +02001279 if (!l_mg->data_next) {
1280 /* If we cannot get a new line, we need to stop the pipeline.
1281 * Only allow as many writes in as we can store safely and then
1282 * fail gracefully
1283 */
1284 pblk_set_space_limit(pblk);
1285
1286 l_mg->data_next = NULL;
1287 } else {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001288 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1289 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001290 }
1291 spin_unlock(&l_mg->free_lock);
1292
Javier González588726d32017-06-26 11:57:29 +02001293 if (pblk_line_erase(pblk, line)) {
1294 line = pblk_line_retry(pblk, line);
1295 if (!line)
1296 return NULL;
1297 }
1298
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001299retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001300 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001301 line = pblk_line_retry(pblk, line);
1302 if (!line)
1303 return NULL;
1304
1305 goto retry_setup;
1306 }
1307
1308 if (!pblk_line_init_bb(pblk, line, 1)) {
1309 line = pblk_line_retry(pblk, line);
1310 if (!line)
1311 return NULL;
1312
1313 goto retry_setup;
1314 }
1315
Javier Gonzáleza7689932018-01-05 14:16:13 +01001316 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1317
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001318 return line;
1319}
1320
Javier González588726d32017-06-26 11:57:29 +02001321static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1322{
1323 lockdep_assert_held(&pblk->l_mg.free_lock);
1324
1325 pblk_set_space_limit(pblk);
1326 pblk->state = PBLK_STATE_STOPPING;
1327}
1328
Javier González8bd40022017-10-13 14:46:44 +02001329static void pblk_line_close_meta_sync(struct pblk *pblk)
1330{
1331 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1332 struct pblk_line_meta *lm = &pblk->lm;
1333 struct pblk_line *line, *tline;
1334 LIST_HEAD(list);
1335
1336 spin_lock(&l_mg->close_lock);
1337 if (list_empty(&l_mg->emeta_list)) {
1338 spin_unlock(&l_mg->close_lock);
1339 return;
1340 }
1341
1342 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1343 spin_unlock(&l_mg->close_lock);
1344
1345 list_for_each_entry_safe(line, tline, &list, list) {
1346 struct pblk_emeta *emeta = line->emeta;
1347
1348 while (emeta->mem < lm->emeta_len[0]) {
1349 int ret;
1350
1351 ret = pblk_submit_meta_io(pblk, line);
1352 if (ret) {
1353 pr_err("pblk: sync meta line %d failed (%d)\n",
1354 line->id, ret);
1355 return;
1356 }
1357 }
1358 }
1359
1360 pblk_wait_for_meta(pblk);
1361 flush_workqueue(pblk->close_wq);
1362}
1363
Javier González588726d32017-06-26 11:57:29 +02001364void pblk_pipeline_stop(struct pblk *pblk)
1365{
1366 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1367 int ret;
1368
1369 spin_lock(&l_mg->free_lock);
1370 if (pblk->state == PBLK_STATE_RECOVERING ||
1371 pblk->state == PBLK_STATE_STOPPED) {
1372 spin_unlock(&l_mg->free_lock);
1373 return;
1374 }
1375 pblk->state = PBLK_STATE_RECOVERING;
1376 spin_unlock(&l_mg->free_lock);
1377
1378 pblk_flush_writer(pblk);
1379 pblk_wait_for_meta(pblk);
1380
1381 ret = pblk_recov_pad(pblk);
1382 if (ret) {
1383 pr_err("pblk: could not close data on teardown(%d)\n", ret);
1384 return;
1385 }
1386
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001387 flush_workqueue(pblk->bb_wq);
Javier González588726d32017-06-26 11:57:29 +02001388 pblk_line_close_meta_sync(pblk);
1389
1390 spin_lock(&l_mg->free_lock);
1391 pblk->state = PBLK_STATE_STOPPED;
1392 l_mg->data_line = NULL;
1393 l_mg->data_next = NULL;
1394 spin_unlock(&l_mg->free_lock);
1395}
1396
Javier González21d22872017-10-13 14:46:26 +02001397struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001398{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001399 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González21d22872017-10-13 14:46:26 +02001400 struct pblk_line *cur, *new = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001401 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001402
1403 cur = l_mg->data_line;
1404 new = l_mg->data_next;
1405 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001406 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001407 l_mg->data_line = new;
1408
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001409 spin_lock(&l_mg->free_lock);
Javier González588726d32017-06-26 11:57:29 +02001410 if (pblk->state != PBLK_STATE_RUNNING) {
1411 l_mg->data_line = NULL;
1412 l_mg->data_next = NULL;
1413 spin_unlock(&l_mg->free_lock);
Javier González21d22872017-10-13 14:46:26 +02001414 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001415 }
1416
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001417 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001418 spin_unlock(&l_mg->free_lock);
1419
Javier González588726d32017-06-26 11:57:29 +02001420retry_erase:
1421 left_seblks = atomic_read(&new->left_seblks);
1422 if (left_seblks) {
1423 /* If line is not fully erased, erase it */
1424 if (atomic_read(&new->left_eblks)) {
1425 if (pblk_line_erase(pblk, new))
Javier González21d22872017-10-13 14:46:26 +02001426 goto out;
Javier González588726d32017-06-26 11:57:29 +02001427 } else {
1428 io_schedule();
1429 }
1430 goto retry_erase;
1431 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001432
1433retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001434 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001435 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001436 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001437 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001438
1439 goto retry_setup;
1440 }
1441
1442 if (!pblk_line_init_bb(pblk, new, 1)) {
1443 new = pblk_line_retry(pblk, new);
1444 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001445 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001446
1447 goto retry_setup;
1448 }
1449
Javier Gonzáleza7689932018-01-05 14:16:13 +01001450 pblk_rl_free_lines_dec(&pblk->rl, new, true);
1451
Javier González588726d32017-06-26 11:57:29 +02001452 /* Allocate next line for preparation */
1453 spin_lock(&l_mg->free_lock);
1454 l_mg->data_next = pblk_line_get(pblk);
1455 if (!l_mg->data_next) {
1456 /* If we cannot get a new line, we need to stop the pipeline.
1457 * Only allow as many writes in as we can store safely and then
1458 * fail gracefully
1459 */
1460 pblk_stop_writes(pblk, new);
1461 l_mg->data_next = NULL;
1462 } else {
1463 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1464 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier González588726d32017-06-26 11:57:29 +02001465 }
1466 spin_unlock(&l_mg->free_lock);
1467
Javier González21d22872017-10-13 14:46:26 +02001468out:
1469 return new;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001470}
1471
1472void pblk_line_free(struct pblk *pblk, struct pblk_line *line)
1473{
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001474 kfree(line->map_bitmap);
1475 kfree(line->invalid_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001476
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001477 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1478
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001479 line->map_bitmap = NULL;
1480 line->invalid_bitmap = NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001481 line->smeta = NULL;
1482 line->emeta = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001483}
1484
Javier González7bd4d372017-10-13 14:46:23 +02001485static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001486{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001487 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001488 struct pblk_gc *gc = &pblk->gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001489
1490 spin_lock(&line->lock);
1491 WARN_ON(line->state != PBLK_LINESTATE_GC);
1492 line->state = PBLK_LINESTATE_FREE;
1493 line->gc_group = PBLK_LINEGC_NONE;
1494 pblk_line_free(pblk, line);
1495 spin_unlock(&line->lock);
1496
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001497 atomic_dec(&gc->pipeline_gc);
1498
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001499 spin_lock(&l_mg->free_lock);
1500 list_add_tail(&line->list, &l_mg->free_list);
1501 l_mg->nr_free_lines++;
1502 spin_unlock(&l_mg->free_lock);
1503
1504 pblk_rl_free_lines_inc(&pblk->rl, line);
1505}
1506
Javier González7bd4d372017-10-13 14:46:23 +02001507static void pblk_line_put_ws(struct work_struct *work)
1508{
1509 struct pblk_line_ws *line_put_ws = container_of(work,
1510 struct pblk_line_ws, ws);
1511 struct pblk *pblk = line_put_ws->pblk;
1512 struct pblk_line *line = line_put_ws->line;
1513
1514 __pblk_line_put(pblk, line);
1515 mempool_free(line_put_ws, pblk->gen_ws_pool);
1516}
1517
1518void pblk_line_put(struct kref *ref)
1519{
1520 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1521 struct pblk *pblk = line->pblk;
1522
1523 __pblk_line_put(pblk, line);
1524}
1525
1526void pblk_line_put_wq(struct kref *ref)
1527{
1528 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1529 struct pblk *pblk = line->pblk;
1530 struct pblk_line_ws *line_put_ws;
1531
1532 line_put_ws = mempool_alloc(pblk->gen_ws_pool, GFP_ATOMIC);
1533 if (!line_put_ws)
1534 return;
1535
1536 line_put_ws->pblk = pblk;
1537 line_put_ws->line = line;
1538 line_put_ws->priv = NULL;
1539
1540 INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
1541 queue_work(pblk->r_end_wq, &line_put_ws->ws);
1542}
1543
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001544int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1545{
1546 struct nvm_rq *rqd;
1547 int err;
1548
Javier González67bf26a2017-10-13 14:46:20 +02001549 rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001550
1551 pblk_setup_e_rq(pblk, rqd, ppa);
1552
1553 rqd->end_io = pblk_end_io_erase;
1554 rqd->private = pblk;
1555
1556 /* The write thread schedules erases so that it minimizes disturbances
1557 * with writes. Thus, there is no need to take the LUN semaphore.
1558 */
1559 err = pblk_submit_io(pblk, rqd);
1560 if (err) {
1561 struct nvm_tgt_dev *dev = pblk->dev;
1562 struct nvm_geo *geo = &dev->geo;
1563
1564 pr_err("pblk: could not async erase line:%d,blk:%d\n",
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001565 pblk_ppa_to_line(ppa),
1566 pblk_ppa_to_pos(geo, ppa));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001567 }
1568
1569 return err;
1570}
1571
1572struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1573{
1574 return pblk->l_mg.data_line;
1575}
1576
Javier Gonzálezd624f372017-06-26 11:57:15 +02001577/* For now, always erase next line */
1578struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001579{
1580 return pblk->l_mg.data_next;
1581}
1582
1583int pblk_line_is_full(struct pblk_line *line)
1584{
1585 return (line->left_msecs == 0);
1586}
1587
Javier González588726d32017-06-26 11:57:29 +02001588static void pblk_line_should_sync_meta(struct pblk *pblk)
1589{
1590 if (pblk_rl_is_limit(&pblk->rl))
1591 pblk_line_close_meta_sync(pblk);
1592}
1593
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001594void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1595{
1596 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1597 struct list_head *move_list;
1598
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001599#ifdef CONFIG_NVM_DEBUG
1600 struct pblk_line_meta *lm = &pblk->lm;
1601
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001602 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001603 "pblk: corrupt closed line %d\n", line->id);
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001604#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001605
1606 spin_lock(&l_mg->free_lock);
1607 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1608 spin_unlock(&l_mg->free_lock);
1609
1610 spin_lock(&l_mg->gc_lock);
1611 spin_lock(&line->lock);
1612 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1613 line->state = PBLK_LINESTATE_CLOSED;
1614 move_list = pblk_line_gc_list(pblk, line);
1615
1616 list_add_tail(&line->list, move_list);
1617
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +02001618 kfree(line->map_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001619 line->map_bitmap = NULL;
1620 line->smeta = NULL;
1621 line->emeta = NULL;
1622
1623 spin_unlock(&line->lock);
1624 spin_unlock(&l_mg->gc_lock);
1625}
1626
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001627void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1628{
1629 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1630 struct pblk_line_meta *lm = &pblk->lm;
1631 struct pblk_emeta *emeta = line->emeta;
1632 struct line_emeta *emeta_buf = emeta->buf;
1633
Javier González588726d32017-06-26 11:57:29 +02001634 /* No need for exact vsc value; avoid a big line lock and take aprox. */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001635 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1636 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1637
1638 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1639 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1640
1641 spin_lock(&l_mg->close_lock);
1642 spin_lock(&line->lock);
1643 list_add_tail(&line->list, &l_mg->emeta_list);
1644 spin_unlock(&line->lock);
1645 spin_unlock(&l_mg->close_lock);
Javier González588726d32017-06-26 11:57:29 +02001646
1647 pblk_line_should_sync_meta(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001648}
1649
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001650void pblk_line_close_ws(struct work_struct *work)
1651{
1652 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1653 ws);
1654 struct pblk *pblk = line_ws->pblk;
1655 struct pblk_line *line = line_ws->line;
1656
1657 pblk_line_close(pblk, line);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001658 mempool_free(line_ws, pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001659}
1660
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001661void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1662 void (*work)(struct work_struct *), gfp_t gfp_mask,
Javier Gonzálezef576492017-06-26 11:57:28 +02001663 struct workqueue_struct *wq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001664{
1665 struct pblk_line_ws *line_ws;
1666
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001667 line_ws = mempool_alloc(pblk->gen_ws_pool, gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001668
1669 line_ws->pblk = pblk;
1670 line_ws->line = line;
1671 line_ws->priv = priv;
1672
1673 INIT_WORK(&line_ws->ws, work);
Javier Gonzálezef576492017-06-26 11:57:28 +02001674 queue_work(wq, &line_ws->ws);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001675}
1676
Javier González3eaa11e2017-07-07 21:08:52 +02001677static void __pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list,
1678 int nr_ppas, int pos)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001679{
Javier González3eaa11e2017-07-07 21:08:52 +02001680 struct pblk_lun *rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001681 int ret;
1682
1683 /*
1684 * Only send one inflight I/O per LUN. Since we map at a page
1685 * granurality, all ppas in the I/O will map to the same LUN
1686 */
1687#ifdef CONFIG_NVM_DEBUG
1688 int i;
1689
1690 for (i = 1; i < nr_ppas; i++)
1691 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1692 ppa_list[0].g.ch != ppa_list[i].g.ch);
1693#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001694
Javier González3eaa11e2017-07-07 21:08:52 +02001695 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
Rakesh Panditc5493842017-10-13 14:45:58 +02001696 if (ret == -ETIME || ret == -EINTR)
1697 pr_err("pblk: taking lun semaphore timed out: err %d\n", -ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001698}
1699
Javier González3eaa11e2017-07-07 21:08:52 +02001700void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
1701{
1702 struct nvm_tgt_dev *dev = pblk->dev;
1703 struct nvm_geo *geo = &dev->geo;
1704 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1705
1706 __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
1707}
1708
1709void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1710 unsigned long *lun_bitmap)
1711{
1712 struct nvm_tgt_dev *dev = pblk->dev;
1713 struct nvm_geo *geo = &dev->geo;
1714 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1715
1716 /* If the LUN has been locked for this same request, do no attempt to
1717 * lock it again
1718 */
1719 if (test_and_set_bit(pos, lun_bitmap))
1720 return;
1721
1722 __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
1723}
1724
1725void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
1726{
1727 struct nvm_tgt_dev *dev = pblk->dev;
1728 struct nvm_geo *geo = &dev->geo;
1729 struct pblk_lun *rlun;
1730 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1731
1732#ifdef CONFIG_NVM_DEBUG
1733 int i;
1734
1735 for (i = 1; i < nr_ppas; i++)
1736 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1737 ppa_list[0].g.ch != ppa_list[i].g.ch);
1738#endif
1739
1740 rlun = &pblk->luns[pos];
1741 up(&rlun->wr_sem);
1742}
1743
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001744void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1745 unsigned long *lun_bitmap)
1746{
1747 struct nvm_tgt_dev *dev = pblk->dev;
1748 struct nvm_geo *geo = &dev->geo;
1749 struct pblk_lun *rlun;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001750 int nr_luns = geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001751 int bit = -1;
1752
1753 while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) {
1754 rlun = &pblk->luns[bit];
1755 up(&rlun->wr_sem);
1756 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001757}
1758
1759void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1760{
Javier González9f6cb132017-10-13 14:46:12 +02001761 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001762
1763 /* logic error: lba out-of-bounds. Ignore update */
1764 if (!(lba < pblk->rl.nr_secs)) {
1765 WARN(1, "pblk: corrupted L2P map request\n");
1766 return;
1767 }
1768
1769 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02001770 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001771
Javier González9f6cb132017-10-13 14:46:12 +02001772 if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
1773 pblk_map_invalidate(pblk, ppa_l2p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001774
1775 pblk_trans_map_set(pblk, lba, ppa);
1776 spin_unlock(&pblk->trans_lock);
1777}
1778
1779void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1780{
Javier Gonzálezd3401212017-10-13 14:46:14 +02001781
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001782#ifdef CONFIG_NVM_DEBUG
1783 /* Callers must ensure that the ppa points to a cache address */
1784 BUG_ON(!pblk_addr_in_cache(ppa));
1785 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1786#endif
1787
1788 pblk_update_map(pblk, lba, ppa);
1789}
1790
Javier González9f6cb132017-10-13 14:46:12 +02001791int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
Javier Gonzálezd3401212017-10-13 14:46:14 +02001792 struct pblk_line *gc_line, u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001793{
Javier Gonzálezd3401212017-10-13 14:46:14 +02001794 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001795 int ret = 1;
1796
1797#ifdef CONFIG_NVM_DEBUG
1798 /* Callers must ensure that the ppa points to a cache address */
Javier González9f6cb132017-10-13 14:46:12 +02001799 BUG_ON(!pblk_addr_in_cache(ppa_new));
1800 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001801#endif
1802
1803 /* logic error: lba out-of-bounds. Ignore update */
1804 if (!(lba < pblk->rl.nr_secs)) {
1805 WARN(1, "pblk: corrupted L2P map request\n");
1806 return 0;
1807 }
1808
1809 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02001810 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzálezd3401212017-10-13 14:46:14 +02001811 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001812
Javier Gonzálezd3401212017-10-13 14:46:14 +02001813 if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
1814 spin_lock(&gc_line->lock);
1815 WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
1816 "pblk: corrupted GC update");
1817 spin_unlock(&gc_line->lock);
Javier González9f6cb132017-10-13 14:46:12 +02001818
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001819 ret = 0;
1820 goto out;
1821 }
1822
Javier González9f6cb132017-10-13 14:46:12 +02001823 pblk_trans_map_set(pblk, lba, ppa_new);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001824out:
1825 spin_unlock(&pblk->trans_lock);
1826 return ret;
1827}
1828
Javier González9f6cb132017-10-13 14:46:12 +02001829void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
1830 struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001831{
Javier González9f6cb132017-10-13 14:46:12 +02001832 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001833
1834#ifdef CONFIG_NVM_DEBUG
1835 /* Callers must ensure that the ppa points to a device address */
Javier González9f6cb132017-10-13 14:46:12 +02001836 BUG_ON(pblk_addr_in_cache(ppa_mapped));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001837#endif
1838 /* Invalidate and discard padded entries */
1839 if (lba == ADDR_EMPTY) {
1840#ifdef CONFIG_NVM_DEBUG
1841 atomic_long_inc(&pblk->padded_wb);
1842#endif
Javier González9f6cb132017-10-13 14:46:12 +02001843 if (!pblk_ppa_empty(ppa_mapped))
1844 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001845 return;
1846 }
1847
1848 /* logic error: lba out-of-bounds. Ignore update */
1849 if (!(lba < pblk->rl.nr_secs)) {
1850 WARN(1, "pblk: corrupted L2P map request\n");
1851 return;
1852 }
1853
1854 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02001855 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001856
1857 /* Do not update L2P if the cacheline has been updated. In this case,
1858 * the mapped ppa must be invalidated
1859 */
Javier González9f6cb132017-10-13 14:46:12 +02001860 if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
1861 if (!pblk_ppa_empty(ppa_mapped))
1862 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001863 goto out;
1864 }
1865
1866#ifdef CONFIG_NVM_DEBUG
Javier González9f6cb132017-10-13 14:46:12 +02001867 WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001868#endif
1869
Javier González9f6cb132017-10-13 14:46:12 +02001870 pblk_trans_map_set(pblk, lba, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001871out:
1872 spin_unlock(&pblk->trans_lock);
1873}
1874
1875void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
1876 sector_t blba, int nr_secs)
1877{
1878 int i;
1879
1880 spin_lock(&pblk->trans_lock);
Javier González7bd4d372017-10-13 14:46:23 +02001881 for (i = 0; i < nr_secs; i++) {
1882 struct ppa_addr ppa;
1883
1884 ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
1885
1886 /* If the L2P entry maps to a line, the reference is valid */
1887 if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001888 int line_id = pblk_ppa_to_line(ppa);
Javier González7bd4d372017-10-13 14:46:23 +02001889 struct pblk_line *line = &pblk->lines[line_id];
1890
1891 kref_get(&line->ref);
1892 }
1893 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001894 spin_unlock(&pblk->trans_lock);
1895}
1896
1897void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
1898 u64 *lba_list, int nr_secs)
1899{
Javier Gonzálezd3401212017-10-13 14:46:14 +02001900 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001901 int i;
1902
1903 spin_lock(&pblk->trans_lock);
1904 for (i = 0; i < nr_secs; i++) {
1905 lba = lba_list[i];
Javier Gonzálezd3401212017-10-13 14:46:14 +02001906 if (lba != ADDR_EMPTY) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001907 /* logic error: lba out-of-bounds. Ignore update */
1908 if (!(lba < pblk->rl.nr_secs)) {
1909 WARN(1, "pblk: corrupted L2P map request\n");
1910 continue;
1911 }
1912 ppas[i] = pblk_trans_map_get(pblk, lba);
1913 }
1914 }
1915 spin_unlock(&pblk->trans_lock);
1916}