blob: 0c22e5ccdfdd972f778accc67f02804720a79f98 [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
21static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
22 struct ppa_addr *ppa)
23{
24 struct nvm_tgt_dev *dev = pblk->dev;
25 struct nvm_geo *geo = &dev->geo;
26 int pos = pblk_dev_ppa_to_pos(geo, *ppa);
27
28 pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos);
29 atomic_long_inc(&pblk->erase_failed);
30
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020031 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020032 if (test_and_set_bit(pos, line->blk_bitmap))
33 pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n",
34 line->id, pos);
35
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020036 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
37 GFP_ATOMIC, pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020038}
39
40static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
41{
42 struct pblk_line *line;
43
44 line = &pblk->lines[pblk_dev_ppa_to_line(rqd->ppa_addr)];
45 atomic_dec(&line->left_seblks);
46
47 if (rqd->error) {
48 struct ppa_addr *ppa;
49
50 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
51 if (!ppa)
52 return;
53
54 *ppa = rqd->ppa_addr;
55 pblk_mark_bb(pblk, line, ppa);
56 }
Javier González588726d32017-06-26 11:57:29 +020057
58 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059}
60
61/* Erase completion assumes that only one block is erased at the time */
62static void pblk_end_io_erase(struct nvm_rq *rqd)
63{
64 struct pblk *pblk = rqd->private;
65
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020066 __pblk_end_io_erase(pblk, rqd);
Javier González0d880392017-10-13 14:46:08 +020067 mempool_free(rqd, pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068}
69
Javier González0880a9a2017-06-26 11:57:19 +020070void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
71 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020072{
73 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
74 struct list_head *move_list = NULL;
75
76 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
77 * table is modified with reclaimed sectors, a check is done to endure
78 * that newer updates are not overwritten.
79 */
80 spin_lock(&line->lock);
81 if (line->state == PBLK_LINESTATE_GC ||
82 line->state == PBLK_LINESTATE_FREE) {
83 spin_unlock(&line->lock);
84 return;
85 }
86
87 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
88 WARN_ONCE(1, "pblk: double invalidate\n");
89 spin_unlock(&line->lock);
90 return;
91 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020092 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093
94 if (line->state == PBLK_LINESTATE_CLOSED)
95 move_list = pblk_line_gc_list(pblk, line);
96 spin_unlock(&line->lock);
97
98 if (move_list) {
99 spin_lock(&l_mg->gc_lock);
100 spin_lock(&line->lock);
101 /* Prevent moving a line that has just been chosen for GC */
102 if (line->state == PBLK_LINESTATE_GC ||
103 line->state == PBLK_LINESTATE_FREE) {
104 spin_unlock(&line->lock);
105 spin_unlock(&l_mg->gc_lock);
106 return;
107 }
108 spin_unlock(&line->lock);
109
110 list_move_tail(&line->list, move_list);
111 spin_unlock(&l_mg->gc_lock);
112 }
113}
114
115void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
116{
117 struct pblk_line *line;
118 u64 paddr;
119 int line_id;
120
121#ifdef CONFIG_NVM_DEBUG
122 /* Callers must ensure that the ppa points to a device address */
123 BUG_ON(pblk_addr_in_cache(ppa));
124 BUG_ON(pblk_ppa_empty(ppa));
125#endif
126
127 line_id = pblk_tgt_ppa_to_line(ppa);
128 line = &pblk->lines[line_id];
129 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
130
131 __pblk_map_invalidate(pblk, line, paddr);
132}
133
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200134static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
135 unsigned int nr_secs)
136{
137 sector_t lba;
138
139 spin_lock(&pblk->trans_lock);
140 for (lba = slba; lba < slba + nr_secs; lba++) {
141 struct ppa_addr ppa;
142
143 ppa = pblk_trans_map_get(pblk, lba);
144
145 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
146 pblk_map_invalidate(pblk, ppa);
147
148 pblk_ppa_set_empty(&ppa);
149 pblk_trans_map_set(pblk, lba, ppa);
150 }
151 spin_unlock(&pblk->trans_lock);
152}
153
154struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw)
155{
156 mempool_t *pool;
157 struct nvm_rq *rqd;
158 int rq_size;
159
160 if (rw == WRITE) {
161 pool = pblk->w_rq_pool;
162 rq_size = pblk_w_rq_size;
163 } else {
Javier González0d880392017-10-13 14:46:08 +0200164 pool = pblk->r_rq_pool;
Javier González084ec9b2017-06-26 16:27:13 -0600165 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166 }
167
168 rqd = mempool_alloc(pool, GFP_KERNEL);
169 memset(rqd, 0, rq_size);
170
171 return rqd;
172}
173
174void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw)
175{
176 mempool_t *pool;
177
178 if (rw == WRITE)
179 pool = pblk->w_rq_pool;
180 else
Javier González0d880392017-10-13 14:46:08 +0200181 pool = pblk->r_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200182
183 mempool_free(rqd, pool);
184}
185
186void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
187 int nr_pages)
188{
189 struct bio_vec bv;
190 int i;
191
192 WARN_ON(off + nr_pages != bio->bi_vcnt);
193
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194 for (i = off; i < nr_pages + off; i++) {
195 bv = bio->bi_io_vec[i];
Javier Gonzálezbd432412017-10-13 14:46:06 +0200196 mempool_free(bv.bv_page, pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197 }
198}
199
200int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
201 int nr_pages)
202{
203 struct request_queue *q = pblk->dev->q;
204 struct page *page;
205 int i, ret;
206
207 for (i = 0; i < nr_pages; i++) {
Javier Gonzálezbd432412017-10-13 14:46:06 +0200208 page = mempool_alloc(pblk->page_bio_pool, flags);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209 if (!page)
210 goto err;
211
212 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
213 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
214 pr_err("pblk: could not add page to bio\n");
Javier Gonzálezbd432412017-10-13 14:46:06 +0200215 mempool_free(page, pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216 goto err;
217 }
218 }
219
220 return 0;
221err:
222 pblk_bio_free_pages(pblk, bio, 0, i - 1);
223 return -1;
224}
225
226static void pblk_write_kick(struct pblk *pblk)
227{
228 wake_up_process(pblk->writer_ts);
229 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
230}
231
232void pblk_write_timer_fn(unsigned long data)
233{
234 struct pblk *pblk = (struct pblk *)data;
235
236 /* kick the write thread every tick to flush outstanding data */
237 pblk_write_kick(pblk);
238}
239
240void pblk_write_should_kick(struct pblk *pblk)
241{
242 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
243
244 if (secs_avail >= pblk->min_write_pgs)
245 pblk_write_kick(pblk);
246}
247
248void pblk_end_bio_sync(struct bio *bio)
249{
250 struct completion *waiting = bio->bi_private;
251
252 complete(waiting);
253}
254
255void pblk_end_io_sync(struct nvm_rq *rqd)
256{
257 struct completion *waiting = rqd->private;
258
259 complete(waiting);
260}
261
Javier González588726d32017-06-26 11:57:29 +0200262void pblk_wait_for_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263{
Javier González588726d32017-06-26 11:57:29 +0200264 do {
265 if (!atomic_read(&pblk->inflight_io))
266 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200267
Javier González588726d32017-06-26 11:57:29 +0200268 schedule();
269 } while (1);
270}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200271
Javier González588726d32017-06-26 11:57:29 +0200272static void pblk_flush_writer(struct pblk *pblk)
273{
274 pblk_rb_flush(&pblk->rwb);
275 do {
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200276 if (!pblk_rb_sync_count(&pblk->rwb))
Javier González588726d32017-06-26 11:57:29 +0200277 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200278
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200279 pblk_write_kick(pblk);
Javier González588726d32017-06-26 11:57:29 +0200280 schedule();
281 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282}
283
284struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
285{
286 struct pblk_line_meta *lm = &pblk->lm;
287 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
288 struct list_head *move_list = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200289 int vsc = le32_to_cpu(*line->vsc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200290
Javier González476118c2017-06-26 11:57:26 +0200291 lockdep_assert_held(&line->lock);
292
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200293 if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200294 if (line->gc_group != PBLK_LINEGC_FULL) {
295 line->gc_group = PBLK_LINEGC_FULL;
296 move_list = &l_mg->gc_full_list;
297 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200298 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200299 if (line->gc_group != PBLK_LINEGC_HIGH) {
300 line->gc_group = PBLK_LINEGC_HIGH;
301 move_list = &l_mg->gc_high_list;
302 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200303 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304 if (line->gc_group != PBLK_LINEGC_MID) {
305 line->gc_group = PBLK_LINEGC_MID;
306 move_list = &l_mg->gc_mid_list;
307 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200308 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200309 if (line->gc_group != PBLK_LINEGC_LOW) {
310 line->gc_group = PBLK_LINEGC_LOW;
311 move_list = &l_mg->gc_low_list;
312 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200313 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200314 if (line->gc_group != PBLK_LINEGC_EMPTY) {
315 line->gc_group = PBLK_LINEGC_EMPTY;
316 move_list = &l_mg->gc_empty_list;
317 }
318 } else {
319 line->state = PBLK_LINESTATE_CORRUPT;
320 line->gc_group = PBLK_LINEGC_NONE;
321 move_list = &l_mg->corrupt_list;
322 pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200323 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200324 line->sec_in_line,
325 lm->high_thrs, lm->mid_thrs);
326 }
327
328 return move_list;
329}
330
331void pblk_discard(struct pblk *pblk, struct bio *bio)
332{
333 sector_t slba = pblk_get_lba(bio);
334 sector_t nr_secs = pblk_get_secs(bio);
335
336 pblk_invalidate_range(pblk, slba, nr_secs);
337}
338
339struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba)
340{
341 struct ppa_addr ppa;
342
343 spin_lock(&pblk->trans_lock);
344 ppa = pblk_trans_map_get(pblk, lba);
345 spin_unlock(&pblk->trans_lock);
346
347 return ppa;
348}
349
350void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
351{
352 atomic_long_inc(&pblk->write_failed);
353#ifdef CONFIG_NVM_DEBUG
354 pblk_print_failed_rqd(pblk, rqd, rqd->error);
355#endif
356}
357
358void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
359{
360 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
361 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
362 atomic_long_inc(&pblk->read_empty);
363 return;
364 }
365
366 switch (rqd->error) {
367 case NVM_RSP_WARN_HIGHECC:
368 atomic_long_inc(&pblk->read_high_ecc);
369 break;
370 case NVM_RSP_ERR_FAILECC:
371 case NVM_RSP_ERR_FAILCRC:
372 atomic_long_inc(&pblk->read_failed);
373 break;
374 default:
375 pr_err("pblk: unknown read error:%d\n", rqd->error);
376 }
377#ifdef CONFIG_NVM_DEBUG
378 pblk_print_failed_rqd(pblk, rqd, rqd->error);
379#endif
380}
381
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200382void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
383{
384 pblk->sec_per_write = sec_per_write;
385}
386
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
388{
389 struct nvm_tgt_dev *dev = pblk->dev;
390
391#ifdef CONFIG_NVM_DEBUG
392 struct ppa_addr *ppa_list;
393
394 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
395 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
396 WARN_ON(1);
397 return -EINVAL;
398 }
399
400 if (rqd->opcode == NVM_OP_PWRITE) {
401 struct pblk_line *line;
402 struct ppa_addr ppa;
403 int i;
404
405 for (i = 0; i < rqd->nr_ppas; i++) {
406 ppa = ppa_list[i];
407 line = &pblk->lines[pblk_dev_ppa_to_line(ppa)];
408
409 spin_lock(&line->lock);
410 if (line->state != PBLK_LINESTATE_OPEN) {
411 pr_err("pblk: bad ppa: line:%d,state:%d\n",
412 line->id, line->state);
413 WARN_ON(1);
414 spin_unlock(&line->lock);
415 return -EINVAL;
416 }
417 spin_unlock(&line->lock);
418 }
419 }
420#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
427struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
428 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200429 int alloc_type, gfp_t gfp_mask)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430{
431 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200432 void *kaddr = data;
433 struct page *page;
434 struct bio *bio;
435 int i, ret;
436
Javier Gonzálezde54e702017-06-30 17:56:39 +0200437 if (alloc_type == PBLK_KMALLOC_META)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
439
440 bio = bio_kmalloc(gfp_mask, nr_secs);
441 if (!bio)
442 return ERR_PTR(-ENOMEM);
443
444 for (i = 0; i < nr_secs; i++) {
445 page = vmalloc_to_page(kaddr);
446 if (!page) {
447 pr_err("pblk: could not map vmalloc bio\n");
448 bio_put(bio);
449 bio = ERR_PTR(-ENOMEM);
450 goto out;
451 }
452
453 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
454 if (ret != PAGE_SIZE) {
455 pr_err("pblk: could not add page to bio\n");
456 bio_put(bio);
457 bio = ERR_PTR(-ENOMEM);
458 goto out;
459 }
460
461 kaddr += PAGE_SIZE;
462 }
463out:
464 return bio;
465}
466
467int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
468 unsigned long secs_to_flush)
469{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200470 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200471 int min = pblk->min_write_pgs;
472 int secs_to_sync = 0;
473
474 if (secs_avail >= max)
475 secs_to_sync = max;
476 else if (secs_avail >= min)
477 secs_to_sync = min * (secs_avail / min);
478 else if (secs_to_flush)
479 secs_to_sync = min;
480
481 return secs_to_sync;
482}
483
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200484void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
485{
486 u64 addr;
487 int i;
488
Rakesh Pandite57903f2017-10-13 14:45:56 +0200489 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200490 addr = find_next_zero_bit(line->map_bitmap,
491 pblk->lm.sec_per_line, line->cur_sec);
492 line->cur_sec = addr - nr_secs;
493
494 for (i = 0; i < nr_secs; i++, line->cur_sec--)
495 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
Rakesh Pandite57903f2017-10-13 14:45:56 +0200496 spin_unlock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200497}
498
499u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500{
501 u64 addr;
502 int i;
503
Javier González476118c2017-06-26 11:57:26 +0200504 lockdep_assert_held(&line->lock);
505
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506 /* logic error: ppa out-of-bounds. Prevent generating bad address */
507 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
508 WARN(1, "pblk: page allocation out of bounds\n");
509 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
510 }
511
512 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
513 pblk->lm.sec_per_line, line->cur_sec);
514 for (i = 0; i < nr_secs; i++, line->cur_sec++)
515 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
516
517 return addr;
518}
519
520u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
521{
522 u64 addr;
523
524 /* Lock needed in case a write fails and a recovery needs to remap
525 * failed write buffer entries
526 */
527 spin_lock(&line->lock);
528 addr = __pblk_alloc_page(pblk, line, nr_secs);
529 line->left_msecs -= nr_secs;
530 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
531 spin_unlock(&line->lock);
532
533 return addr;
534}
535
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200536u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
537{
538 u64 paddr;
539
540 spin_lock(&line->lock);
541 paddr = find_next_zero_bit(line->map_bitmap,
542 pblk->lm.sec_per_line, line->cur_sec);
543 spin_unlock(&line->lock);
544
545 return paddr;
546}
547
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548/*
549 * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when
550 * taking the per LUN semaphore.
551 */
552static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200553 void *emeta_buf, u64 paddr, int dir)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200554{
555 struct nvm_tgt_dev *dev = pblk->dev;
556 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezde54e702017-06-30 17:56:39 +0200557 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558 struct pblk_line_meta *lm = &pblk->lm;
Javier González63e38092017-06-26 11:57:24 +0200559 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200560 struct bio *bio;
561 struct nvm_rq rqd;
Javier González63e38092017-06-26 11:57:24 +0200562 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200564 int left_ppas = lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200565 int id = line->id;
566 int rq_ppas, rq_len;
567 int cmd_op, bio_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200568 int i, j;
569 int ret;
570 DECLARE_COMPLETION_ONSTACK(wait);
571
572 if (dir == WRITE) {
573 bio_op = REQ_OP_WRITE;
574 cmd_op = NVM_OP_PWRITE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200575 } else if (dir == READ) {
576 bio_op = REQ_OP_READ;
577 cmd_op = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578 } else
579 return -EINVAL;
580
Javier González63e38092017-06-26 11:57:24 +0200581 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
582 &dma_meta_list);
583 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200584 return -ENOMEM;
585
Javier González63e38092017-06-26 11:57:24 +0200586 ppa_list = meta_list + pblk_dma_meta_size;
587 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
588
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200589next_rq:
590 memset(&rqd, 0, sizeof(struct nvm_rq));
591
592 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
593 rq_len = rq_ppas * geo->sec_size;
594
Javier Gonzálezde54e702017-06-30 17:56:39 +0200595 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
596 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200597 if (IS_ERR(bio)) {
598 ret = PTR_ERR(bio);
599 goto free_rqd_dma;
600 }
601
602 bio->bi_iter.bi_sector = 0; /* internal bio */
603 bio_set_op_attrs(bio, bio_op, 0);
604
605 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200606 rqd.meta_list = meta_list;
607 rqd.ppa_list = ppa_list;
608 rqd.dma_meta_list = dma_meta_list;
609 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610 rqd.opcode = cmd_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612 rqd.end_io = pblk_end_io_sync;
613 rqd.private = &wait;
614
615 if (dir == WRITE) {
Javier González63e38092017-06-26 11:57:24 +0200616 struct pblk_sec_meta *meta_list = rqd.meta_list;
617
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200618 rqd.flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619 for (i = 0; i < rqd.nr_ppas; ) {
620 spin_lock(&line->lock);
621 paddr = __pblk_alloc_page(pblk, line, min);
622 spin_unlock(&line->lock);
Javier González63e38092017-06-26 11:57:24 +0200623 for (j = 0; j < min; j++, i++, paddr++) {
624 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200625 rqd.ppa_list[i] =
626 addr_to_gen_ppa(pblk, paddr, id);
Javier González63e38092017-06-26 11:57:24 +0200627 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628 }
629 } else {
630 for (i = 0; i < rqd.nr_ppas; ) {
631 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
632 int pos = pblk_dev_ppa_to_pos(geo, ppa);
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200633 int read_type = PBLK_READ_RANDOM;
634
635 if (pblk_io_aligned(pblk, rq_ppas))
636 read_type = PBLK_READ_SEQUENTIAL;
637 rqd.flags = pblk_set_read_mode(pblk, read_type);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200638
639 while (test_bit(pos, line->blk_bitmap)) {
640 paddr += min;
641 if (pblk_boundary_paddr_checks(pblk, paddr)) {
642 pr_err("pblk: corrupt emeta line:%d\n",
643 line->id);
644 bio_put(bio);
645 ret = -EINTR;
646 goto free_rqd_dma;
647 }
648
649 ppa = addr_to_gen_ppa(pblk, paddr, id);
650 pos = pblk_dev_ppa_to_pos(geo, ppa);
651 }
652
653 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
654 pr_err("pblk: corrupt emeta line:%d\n",
655 line->id);
656 bio_put(bio);
657 ret = -EINTR;
658 goto free_rqd_dma;
659 }
660
661 for (j = 0; j < min; j++, i++, paddr++)
662 rqd.ppa_list[i] =
663 addr_to_gen_ppa(pblk, paddr, line->id);
664 }
665 }
666
667 ret = pblk_submit_io(pblk, &rqd);
668 if (ret) {
669 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
670 bio_put(bio);
671 goto free_rqd_dma;
672 }
673
674 if (!wait_for_completion_io_timeout(&wait,
675 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
676 pr_err("pblk: emeta I/O timed out\n");
677 }
Javier González588726d32017-06-26 11:57:29 +0200678 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 reinit_completion(&wait);
680
Javier Gonzálezf680f192017-06-26 11:57:21 +0200681 if (likely(pblk->l_mg.emeta_alloc_type == PBLK_VMALLOC_META))
682 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200683
684 if (rqd.error) {
685 if (dir == WRITE)
686 pblk_log_write_err(pblk, &rqd);
687 else
688 pblk_log_read_err(pblk, &rqd);
689 }
690
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200691 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692 left_ppas -= rq_ppas;
693 if (left_ppas)
694 goto next_rq;
695free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200696 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200697 return ret;
698}
699
700u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
701{
702 struct nvm_tgt_dev *dev = pblk->dev;
703 struct nvm_geo *geo = &dev->geo;
704 struct pblk_line_meta *lm = &pblk->lm;
705 int bit;
706
707 /* This usually only happens on bad lines */
708 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
709 if (bit >= lm->blk_per_line)
710 return -1;
711
712 return bit * geo->sec_per_pl;
713}
714
715static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
716 u64 paddr, int dir)
717{
718 struct nvm_tgt_dev *dev = pblk->dev;
719 struct pblk_line_meta *lm = &pblk->lm;
720 struct bio *bio;
721 struct nvm_rq rqd;
722 __le64 *lba_list = NULL;
723 int i, ret;
724 int cmd_op, bio_op;
725 int flags;
726 DECLARE_COMPLETION_ONSTACK(wait);
727
728 if (dir == WRITE) {
729 bio_op = REQ_OP_WRITE;
730 cmd_op = NVM_OP_PWRITE;
731 flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200732 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200733 } else if (dir == READ) {
734 bio_op = REQ_OP_READ;
735 cmd_op = NVM_OP_PREAD;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200736 flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200737 } else
738 return -EINVAL;
739
740 memset(&rqd, 0, sizeof(struct nvm_rq));
741
Javier González63e38092017-06-26 11:57:24 +0200742 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
743 &rqd.dma_meta_list);
744 if (!rqd.meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200745 return -ENOMEM;
746
Javier González63e38092017-06-26 11:57:24 +0200747 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
748 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
749
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200750 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
751 if (IS_ERR(bio)) {
752 ret = PTR_ERR(bio);
753 goto free_ppa_list;
754 }
755
756 bio->bi_iter.bi_sector = 0; /* internal bio */
757 bio_set_op_attrs(bio, bio_op, 0);
758
759 rqd.bio = bio;
760 rqd.opcode = cmd_op;
761 rqd.flags = flags;
762 rqd.nr_ppas = lm->smeta_sec;
763 rqd.end_io = pblk_end_io_sync;
764 rqd.private = &wait;
765
766 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
Javier González63e38092017-06-26 11:57:24 +0200767 struct pblk_sec_meta *meta_list = rqd.meta_list;
768
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200769 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
Javier González63e38092017-06-26 11:57:24 +0200770
771 if (dir == WRITE) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200772 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier González63e38092017-06-26 11:57:24 +0200773
774 meta_list[i].lba = lba_list[paddr] = addr_empty;
775 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200776 }
777
778 /*
779 * This I/O is sent by the write thread when a line is replace. Since
780 * the write thread is the only one sending write and erase commands,
781 * there is no need to take the LUN semaphore.
782 */
783 ret = pblk_submit_io(pblk, &rqd);
784 if (ret) {
785 pr_err("pblk: smeta I/O submission failed: %d\n", ret);
786 bio_put(bio);
787 goto free_ppa_list;
788 }
789
790 if (!wait_for_completion_io_timeout(&wait,
791 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
792 pr_err("pblk: smeta I/O timed out\n");
793 }
Javier González588726d32017-06-26 11:57:29 +0200794 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200795
796 if (rqd.error) {
797 if (dir == WRITE)
798 pblk_log_write_err(pblk, &rqd);
799 else
800 pblk_log_read_err(pblk, &rqd);
801 }
802
803free_ppa_list:
Javier González63e38092017-06-26 11:57:24 +0200804 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805
806 return ret;
807}
808
809int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
810{
811 u64 bpaddr = pblk_line_smeta_start(pblk, line);
812
813 return pblk_line_submit_smeta_io(pblk, line, bpaddr, READ);
814}
815
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200816int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
817 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200818{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200819 return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
820 line->emeta_ssec, READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200821}
822
823static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
824 struct ppa_addr ppa)
825{
826 rqd->opcode = NVM_OP_ERASE;
827 rqd->ppa_addr = ppa;
828 rqd->nr_ppas = 1;
829 rqd->flags = pblk_set_progr_mode(pblk, ERASE);
830 rqd->bio = NULL;
831}
832
833static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
834{
835 struct nvm_rq rqd;
Javier González588726d32017-06-26 11:57:29 +0200836 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200837 DECLARE_COMPLETION_ONSTACK(wait);
838
839 memset(&rqd, 0, sizeof(struct nvm_rq));
840
841 pblk_setup_e_rq(pblk, &rqd, ppa);
842
843 rqd.end_io = pblk_end_io_sync;
844 rqd.private = &wait;
845
846 /* The write thread schedules erases so that it minimizes disturbances
847 * with writes. Thus, there is no need to take the LUN semaphore.
848 */
849 ret = pblk_submit_io(pblk, &rqd);
850 if (ret) {
851 struct nvm_tgt_dev *dev = pblk->dev;
852 struct nvm_geo *geo = &dev->geo;
853
854 pr_err("pblk: could not sync erase line:%d,blk:%d\n",
855 pblk_dev_ppa_to_line(ppa),
856 pblk_dev_ppa_to_pos(geo, ppa));
857
858 rqd.error = ret;
859 goto out;
860 }
861
862 if (!wait_for_completion_io_timeout(&wait,
863 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
864 pr_err("pblk: sync erase timed out\n");
865 }
866
867out:
868 rqd.private = pblk;
869 __pblk_end_io_erase(pblk, &rqd);
870
Javier González588726d32017-06-26 11:57:29 +0200871 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200872}
873
874int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
875{
876 struct pblk_line_meta *lm = &pblk->lm;
877 struct ppa_addr ppa;
Javier González588726d32017-06-26 11:57:29 +0200878 int ret, bit = -1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200879
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200880 /* Erase only good blocks, one at a time */
881 do {
882 spin_lock(&line->lock);
883 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
884 bit + 1);
885 if (bit >= lm->blk_per_line) {
886 spin_unlock(&line->lock);
887 break;
888 }
889
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200890 ppa = pblk->luns[bit].bppa; /* set ch and lun */
891 ppa.g.blk = line->id;
892
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200893 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200894 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200895 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200896
Javier González588726d32017-06-26 11:57:29 +0200897 ret = pblk_blk_erase_sync(pblk, ppa);
898 if (ret) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200899 pr_err("pblk: failed to erase line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +0200900 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200901 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200902 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200903
904 return 0;
905}
906
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200907static void pblk_line_setup_metadata(struct pblk_line *line,
908 struct pblk_line_mgmt *l_mg,
909 struct pblk_line_meta *lm)
910{
911 int meta_line;
912
Javier González588726d32017-06-26 11:57:29 +0200913 lockdep_assert_held(&l_mg->free_lock);
914
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200915retry_meta:
916 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
917 if (meta_line == PBLK_DATA_LINES) {
918 spin_unlock(&l_mg->free_lock);
919 io_schedule();
920 spin_lock(&l_mg->free_lock);
921 goto retry_meta;
922 }
923
924 set_bit(meta_line, &l_mg->meta_bitmap);
925 line->meta_line = meta_line;
926
927 line->smeta = l_mg->sline_meta[meta_line];
928 line->emeta = l_mg->eline_meta[meta_line];
929
930 memset(line->smeta, 0, lm->smeta_len);
931 memset(line->emeta->buf, 0, lm->emeta_len[0]);
932
933 line->emeta->mem = 0;
934 atomic_set(&line->emeta->sync, 0);
935}
936
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200937/* For now lines are always assumed full lines. Thus, smeta former and current
938 * lun bitmaps are omitted.
939 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200940static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200941 struct pblk_line *cur)
942{
943 struct nvm_tgt_dev *dev = pblk->dev;
944 struct nvm_geo *geo = &dev->geo;
945 struct pblk_line_meta *lm = &pblk->lm;
946 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200947 struct pblk_emeta *emeta = line->emeta;
948 struct line_emeta *emeta_buf = emeta->buf;
949 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200950 int nr_blk_line;
951
952 /* After erasing the line, new bad blocks might appear and we risk
953 * having an invalid line
954 */
955 nr_blk_line = lm->blk_per_line -
956 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
957 if (nr_blk_line < lm->min_blk_line) {
958 spin_lock(&l_mg->free_lock);
959 spin_lock(&line->lock);
960 line->state = PBLK_LINESTATE_BAD;
961 spin_unlock(&line->lock);
962
963 list_add_tail(&line->list, &l_mg->bad_list);
964 spin_unlock(&l_mg->free_lock);
965
966 pr_debug("pblk: line %d is bad\n", line->id);
967
968 return 0;
969 }
970
971 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200972 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200973
974 /* Mark LUNs allocated in this line (all for now) */
975 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
976
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200977 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
978 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
979 smeta_buf->header.id = cpu_to_le32(line->id);
980 smeta_buf->header.type = cpu_to_le16(line->type);
Rakesh Panditc79819b2017-10-13 14:45:59 +0200981 smeta_buf->header.version = SMETA_VERSION;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200982
983 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200984 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
985 smeta_buf->window_wr_lun = cpu_to_le32(geo->nr_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200986
987 /* Fill metadata among lines */
988 if (cur) {
989 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200990 smeta_buf->prev_id = cpu_to_le32(cur->id);
991 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200992 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200993 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994 }
995
996 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200997 smeta_buf->header.crc = cpu_to_le32(
998 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
999 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001000
1001 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001002 memcpy(&emeta_buf->header, &smeta_buf->header,
1003 sizeof(struct line_header));
1004 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1005 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1006 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1007 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1008 emeta_buf->crc = cpu_to_le32(0);
1009 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001010
1011 return 1;
1012}
1013
1014/* For now lines are always assumed full lines. Thus, smeta former and current
1015 * lun bitmaps are omitted.
1016 */
1017static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1018 int init)
1019{
1020 struct nvm_tgt_dev *dev = pblk->dev;
1021 struct nvm_geo *geo = &dev->geo;
1022 struct pblk_line_meta *lm = &pblk->lm;
1023 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1024 int nr_bb = 0;
1025 u64 off;
1026 int bit = -1;
1027
1028 line->sec_in_line = lm->sec_per_line;
1029
1030 /* Capture bad block information on line mapping bitmaps */
1031 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1032 bit + 1)) < lm->blk_per_line) {
1033 off = bit * geo->sec_per_pl;
1034 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1035 lm->sec_per_line);
1036 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1037 lm->sec_per_line);
1038 line->sec_in_line -= geo->sec_per_blk;
1039 if (bit >= lm->emeta_bb)
1040 nr_bb++;
1041 }
1042
1043 /* Mark smeta metadata sectors as bad sectors */
1044 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1045 off = bit * geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001046 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1047 line->sec_in_line -= lm->smeta_sec;
1048 line->smeta_ssec = off;
1049 line->cur_sec = off + lm->smeta_sec;
1050
1051 if (init && pblk_line_submit_smeta_io(pblk, line, off, WRITE)) {
1052 pr_debug("pblk: line smeta I/O failed. Retry\n");
Javier González588726d32017-06-26 11:57:29 +02001053 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001054 }
1055
1056 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1057
1058 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1059 * blocks to make sure that there are enough sectors to store emeta
1060 */
1061 bit = lm->sec_per_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001062 off = lm->sec_per_line - lm->emeta_sec[0];
1063 bitmap_set(line->invalid_bitmap, off, lm->emeta_sec[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001064 while (nr_bb) {
1065 off -= geo->sec_per_pl;
1066 if (!test_bit(off, line->invalid_bitmap)) {
1067 bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl);
1068 nr_bb--;
1069 }
1070 }
1071
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001072 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001073 line->emeta_ssec = off;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001074 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001075 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001076 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077
1078 if (lm->sec_per_line - line->sec_in_line !=
1079 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1080 spin_lock(&line->lock);
1081 line->state = PBLK_LINESTATE_BAD;
1082 spin_unlock(&line->lock);
1083
1084 list_add_tail(&line->list, &l_mg->bad_list);
1085 pr_err("pblk: unexpected line %d is bad\n", line->id);
1086
1087 return 0;
1088 }
1089
1090 return 1;
1091}
1092
1093static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1094{
1095 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001096 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001097
1098 line->map_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1099 if (!line->map_bitmap)
1100 return -ENOMEM;
1101 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1102
1103 /* invalid_bitmap is special since it is used when line is closed. No
1104 * need to zeroized; it will be initialized using bb info form
1105 * map_bitmap
1106 */
1107 line->invalid_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1108 if (!line->invalid_bitmap) {
1109 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1110 return -ENOMEM;
1111 }
1112
1113 spin_lock(&line->lock);
1114 if (line->state != PBLK_LINESTATE_FREE) {
Javier González588726d32017-06-26 11:57:29 +02001115 mempool_free(line->invalid_bitmap, pblk->line_meta_pool);
1116 mempool_free(line->map_bitmap, pblk->line_meta_pool);
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
1156 pblk_rl_free_lines_dec(&pblk->rl, line);
1157
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{
1168 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1169 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áleza4bd2172017-04-15 20:55:50 +02001244 pblk_rl_free_lines_dec(&pblk->rl, retry_line);
1245
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 int is_next = 0;
1264
1265 spin_lock(&l_mg->free_lock);
1266 line = pblk_line_get(pblk);
1267 if (!line) {
1268 spin_unlock(&l_mg->free_lock);
1269 return NULL;
1270 }
1271
1272 line->seq_nr = l_mg->d_seq_nr++;
1273 line->type = PBLK_LINETYPE_DATA;
1274 l_mg->data_line = line;
1275
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001276 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001277
1278 /* Allocate next line for preparation */
1279 l_mg->data_next = pblk_line_get(pblk);
Javier González588726d32017-06-26 11:57:29 +02001280 if (!l_mg->data_next) {
1281 /* If we cannot get a new line, we need to stop the pipeline.
1282 * Only allow as many writes in as we can store safely and then
1283 * fail gracefully
1284 */
1285 pblk_set_space_limit(pblk);
1286
1287 l_mg->data_next = NULL;
1288 } else {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001289 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1290 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1291 is_next = 1;
1292 }
1293 spin_unlock(&l_mg->free_lock);
1294
Javier González588726d32017-06-26 11:57:29 +02001295 if (pblk_line_erase(pblk, line)) {
1296 line = pblk_line_retry(pblk, line);
1297 if (!line)
1298 return NULL;
1299 }
1300
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001301 pblk_rl_free_lines_dec(&pblk->rl, line);
1302 if (is_next)
1303 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
1304
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001305retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001306 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001307 line = pblk_line_retry(pblk, line);
1308 if (!line)
1309 return NULL;
1310
1311 goto retry_setup;
1312 }
1313
1314 if (!pblk_line_init_bb(pblk, line, 1)) {
1315 line = pblk_line_retry(pblk, line);
1316 if (!line)
1317 return NULL;
1318
1319 goto retry_setup;
1320 }
1321
1322 return line;
1323}
1324
Javier González588726d32017-06-26 11:57:29 +02001325static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1326{
1327 lockdep_assert_held(&pblk->l_mg.free_lock);
1328
1329 pblk_set_space_limit(pblk);
1330 pblk->state = PBLK_STATE_STOPPING;
1331}
1332
1333void pblk_pipeline_stop(struct pblk *pblk)
1334{
1335 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1336 int ret;
1337
1338 spin_lock(&l_mg->free_lock);
1339 if (pblk->state == PBLK_STATE_RECOVERING ||
1340 pblk->state == PBLK_STATE_STOPPED) {
1341 spin_unlock(&l_mg->free_lock);
1342 return;
1343 }
1344 pblk->state = PBLK_STATE_RECOVERING;
1345 spin_unlock(&l_mg->free_lock);
1346
1347 pblk_flush_writer(pblk);
1348 pblk_wait_for_meta(pblk);
1349
1350 ret = pblk_recov_pad(pblk);
1351 if (ret) {
1352 pr_err("pblk: could not close data on teardown(%d)\n", ret);
1353 return;
1354 }
1355
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001356 flush_workqueue(pblk->bb_wq);
Javier González588726d32017-06-26 11:57:29 +02001357 pblk_line_close_meta_sync(pblk);
1358
1359 spin_lock(&l_mg->free_lock);
1360 pblk->state = PBLK_STATE_STOPPED;
1361 l_mg->data_line = NULL;
1362 l_mg->data_next = NULL;
1363 spin_unlock(&l_mg->free_lock);
1364}
1365
1366void pblk_line_replace_data(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001367{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001368 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1369 struct pblk_line *cur, *new;
1370 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001371 int is_next = 0;
1372
1373 cur = l_mg->data_line;
1374 new = l_mg->data_next;
1375 if (!new)
Javier González588726d32017-06-26 11:57:29 +02001376 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001377 l_mg->data_line = new;
1378
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001379 spin_lock(&l_mg->free_lock);
Javier González588726d32017-06-26 11:57:29 +02001380 if (pblk->state != PBLK_STATE_RUNNING) {
1381 l_mg->data_line = NULL;
1382 l_mg->data_next = NULL;
1383 spin_unlock(&l_mg->free_lock);
1384 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001385 }
1386
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001387 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001388 spin_unlock(&l_mg->free_lock);
1389
Javier González588726d32017-06-26 11:57:29 +02001390retry_erase:
1391 left_seblks = atomic_read(&new->left_seblks);
1392 if (left_seblks) {
1393 /* If line is not fully erased, erase it */
1394 if (atomic_read(&new->left_eblks)) {
1395 if (pblk_line_erase(pblk, new))
1396 return;
1397 } else {
1398 io_schedule();
1399 }
1400 goto retry_erase;
1401 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001402
1403retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001404 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001405 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001406 if (!new)
Javier González588726d32017-06-26 11:57:29 +02001407 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001408
1409 goto retry_setup;
1410 }
1411
1412 if (!pblk_line_init_bb(pblk, new, 1)) {
1413 new = pblk_line_retry(pblk, new);
1414 if (!new)
Javier González588726d32017-06-26 11:57:29 +02001415 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001416
1417 goto retry_setup;
1418 }
1419
Javier González588726d32017-06-26 11:57:29 +02001420 /* Allocate next line for preparation */
1421 spin_lock(&l_mg->free_lock);
1422 l_mg->data_next = pblk_line_get(pblk);
1423 if (!l_mg->data_next) {
1424 /* If we cannot get a new line, we need to stop the pipeline.
1425 * Only allow as many writes in as we can store safely and then
1426 * fail gracefully
1427 */
1428 pblk_stop_writes(pblk, new);
1429 l_mg->data_next = NULL;
1430 } else {
1431 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1432 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1433 is_next = 1;
1434 }
1435 spin_unlock(&l_mg->free_lock);
1436
1437 if (is_next)
1438 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001439}
1440
1441void pblk_line_free(struct pblk *pblk, struct pblk_line *line)
1442{
1443 if (line->map_bitmap)
1444 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1445 if (line->invalid_bitmap)
1446 mempool_free(line->invalid_bitmap, pblk->line_meta_pool);
1447
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001448 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1449
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001450 line->map_bitmap = NULL;
1451 line->invalid_bitmap = NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001452 line->smeta = NULL;
1453 line->emeta = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001454}
1455
1456void pblk_line_put(struct kref *ref)
1457{
1458 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1459 struct pblk *pblk = line->pblk;
1460 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1461
1462 spin_lock(&line->lock);
1463 WARN_ON(line->state != PBLK_LINESTATE_GC);
1464 line->state = PBLK_LINESTATE_FREE;
1465 line->gc_group = PBLK_LINEGC_NONE;
1466 pblk_line_free(pblk, line);
1467 spin_unlock(&line->lock);
1468
1469 spin_lock(&l_mg->free_lock);
1470 list_add_tail(&line->list, &l_mg->free_list);
1471 l_mg->nr_free_lines++;
1472 spin_unlock(&l_mg->free_lock);
1473
1474 pblk_rl_free_lines_inc(&pblk->rl, line);
1475}
1476
1477int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1478{
1479 struct nvm_rq *rqd;
1480 int err;
1481
Javier González0d880392017-10-13 14:46:08 +02001482 rqd = mempool_alloc(pblk->e_rq_pool, GFP_KERNEL);
Javier González084ec9b2017-06-26 16:27:13 -06001483 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001484
1485 pblk_setup_e_rq(pblk, rqd, ppa);
1486
1487 rqd->end_io = pblk_end_io_erase;
1488 rqd->private = pblk;
1489
1490 /* The write thread schedules erases so that it minimizes disturbances
1491 * with writes. Thus, there is no need to take the LUN semaphore.
1492 */
1493 err = pblk_submit_io(pblk, rqd);
1494 if (err) {
1495 struct nvm_tgt_dev *dev = pblk->dev;
1496 struct nvm_geo *geo = &dev->geo;
1497
1498 pr_err("pblk: could not async erase line:%d,blk:%d\n",
1499 pblk_dev_ppa_to_line(ppa),
1500 pblk_dev_ppa_to_pos(geo, ppa));
1501 }
1502
1503 return err;
1504}
1505
1506struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1507{
1508 return pblk->l_mg.data_line;
1509}
1510
Javier Gonzálezd624f372017-06-26 11:57:15 +02001511/* For now, always erase next line */
1512struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001513{
1514 return pblk->l_mg.data_next;
1515}
1516
1517int pblk_line_is_full(struct pblk_line *line)
1518{
1519 return (line->left_msecs == 0);
1520}
1521
Javier González588726d32017-06-26 11:57:29 +02001522void pblk_line_close_meta_sync(struct pblk *pblk)
1523{
1524 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1525 struct pblk_line_meta *lm = &pblk->lm;
1526 struct pblk_line *line, *tline;
1527 LIST_HEAD(list);
1528
1529 spin_lock(&l_mg->close_lock);
1530 if (list_empty(&l_mg->emeta_list)) {
1531 spin_unlock(&l_mg->close_lock);
1532 return;
1533 }
1534
1535 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1536 spin_unlock(&l_mg->close_lock);
1537
1538 list_for_each_entry_safe(line, tline, &list, list) {
1539 struct pblk_emeta *emeta = line->emeta;
1540
1541 while (emeta->mem < lm->emeta_len[0]) {
1542 int ret;
1543
1544 ret = pblk_submit_meta_io(pblk, line);
1545 if (ret) {
1546 pr_err("pblk: sync meta line %d failed (%d)\n",
1547 line->id, ret);
1548 return;
1549 }
1550 }
1551 }
1552
1553 pblk_wait_for_meta(pblk);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001554 flush_workqueue(pblk->close_wq);
Javier González588726d32017-06-26 11:57:29 +02001555}
1556
1557static void pblk_line_should_sync_meta(struct pblk *pblk)
1558{
1559 if (pblk_rl_is_limit(&pblk->rl))
1560 pblk_line_close_meta_sync(pblk);
1561}
1562
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001563void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1564{
1565 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1566 struct list_head *move_list;
1567
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001568#ifdef CONFIG_NVM_DEBUG
1569 struct pblk_line_meta *lm = &pblk->lm;
1570
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001571 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001572 "pblk: corrupt closed line %d\n", line->id);
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001573#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001574
1575 spin_lock(&l_mg->free_lock);
1576 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1577 spin_unlock(&l_mg->free_lock);
1578
1579 spin_lock(&l_mg->gc_lock);
1580 spin_lock(&line->lock);
1581 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1582 line->state = PBLK_LINESTATE_CLOSED;
1583 move_list = pblk_line_gc_list(pblk, line);
1584
1585 list_add_tail(&line->list, move_list);
1586
1587 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1588 line->map_bitmap = NULL;
1589 line->smeta = NULL;
1590 line->emeta = NULL;
1591
1592 spin_unlock(&line->lock);
1593 spin_unlock(&l_mg->gc_lock);
1594}
1595
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001596void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1597{
1598 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1599 struct pblk_line_meta *lm = &pblk->lm;
1600 struct pblk_emeta *emeta = line->emeta;
1601 struct line_emeta *emeta_buf = emeta->buf;
1602
Javier González588726d32017-06-26 11:57:29 +02001603 /* No need for exact vsc value; avoid a big line lock and take aprox. */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001604 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1605 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1606
1607 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1608 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1609
1610 spin_lock(&l_mg->close_lock);
1611 spin_lock(&line->lock);
1612 list_add_tail(&line->list, &l_mg->emeta_list);
1613 spin_unlock(&line->lock);
1614 spin_unlock(&l_mg->close_lock);
Javier González588726d32017-06-26 11:57:29 +02001615
1616 pblk_line_should_sync_meta(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001617}
1618
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001619void pblk_line_close_ws(struct work_struct *work)
1620{
1621 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1622 ws);
1623 struct pblk *pblk = line_ws->pblk;
1624 struct pblk_line *line = line_ws->line;
1625
1626 pblk_line_close(pblk, line);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001627 mempool_free(line_ws, pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001628}
1629
1630void pblk_line_mark_bb(struct work_struct *work)
1631{
1632 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1633 ws);
1634 struct pblk *pblk = line_ws->pblk;
1635 struct nvm_tgt_dev *dev = pblk->dev;
1636 struct ppa_addr *ppa = line_ws->priv;
1637 int ret;
1638
1639 ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
1640 if (ret) {
1641 struct pblk_line *line;
1642 int pos;
1643
1644 line = &pblk->lines[pblk_dev_ppa_to_line(*ppa)];
1645 pos = pblk_dev_ppa_to_pos(&dev->geo, *ppa);
1646
1647 pr_err("pblk: failed to mark bb, line:%d, pos:%d\n",
1648 line->id, pos);
1649 }
1650
1651 kfree(ppa);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001652 mempool_free(line_ws, pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001653}
1654
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001655void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1656 void (*work)(struct work_struct *), gfp_t gfp_mask,
Javier Gonzálezef576492017-06-26 11:57:28 +02001657 struct workqueue_struct *wq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001658{
1659 struct pblk_line_ws *line_ws;
1660
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001661 line_ws = mempool_alloc(pblk->gen_ws_pool, gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001662 if (!line_ws)
1663 return;
1664
1665 line_ws->pblk = pblk;
1666 line_ws->line = line;
1667 line_ws->priv = priv;
1668
1669 INIT_WORK(&line_ws->ws, work);
Javier Gonzálezef576492017-06-26 11:57:28 +02001670 queue_work(wq, &line_ws->ws);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001671}
1672
Javier González3eaa11e2017-07-07 21:08:52 +02001673static void __pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list,
1674 int nr_ppas, int pos)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001675{
Javier González3eaa11e2017-07-07 21:08:52 +02001676 struct pblk_lun *rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001677 int ret;
1678
1679 /*
1680 * Only send one inflight I/O per LUN. Since we map at a page
1681 * granurality, all ppas in the I/O will map to the same LUN
1682 */
1683#ifdef CONFIG_NVM_DEBUG
1684 int i;
1685
1686 for (i = 1; i < nr_ppas; i++)
1687 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1688 ppa_list[0].g.ch != ppa_list[i].g.ch);
1689#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001690
Javier González3eaa11e2017-07-07 21:08:52 +02001691 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
Rakesh Panditc5493842017-10-13 14:45:58 +02001692 if (ret == -ETIME || ret == -EINTR)
1693 pr_err("pblk: taking lun semaphore timed out: err %d\n", -ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001694}
1695
Javier González3eaa11e2017-07-07 21:08:52 +02001696void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
1697{
1698 struct nvm_tgt_dev *dev = pblk->dev;
1699 struct nvm_geo *geo = &dev->geo;
1700 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1701
1702 __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
1703}
1704
1705void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1706 unsigned long *lun_bitmap)
1707{
1708 struct nvm_tgt_dev *dev = pblk->dev;
1709 struct nvm_geo *geo = &dev->geo;
1710 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1711
1712 /* If the LUN has been locked for this same request, do no attempt to
1713 * lock it again
1714 */
1715 if (test_and_set_bit(pos, lun_bitmap))
1716 return;
1717
1718 __pblk_down_page(pblk, ppa_list, nr_ppas, pos);
1719}
1720
1721void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas)
1722{
1723 struct nvm_tgt_dev *dev = pblk->dev;
1724 struct nvm_geo *geo = &dev->geo;
1725 struct pblk_lun *rlun;
1726 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
1727
1728#ifdef CONFIG_NVM_DEBUG
1729 int i;
1730
1731 for (i = 1; i < nr_ppas; i++)
1732 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1733 ppa_list[0].g.ch != ppa_list[i].g.ch);
1734#endif
1735
1736 rlun = &pblk->luns[pos];
1737 up(&rlun->wr_sem);
1738}
1739
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001740void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1741 unsigned long *lun_bitmap)
1742{
1743 struct nvm_tgt_dev *dev = pblk->dev;
1744 struct nvm_geo *geo = &dev->geo;
1745 struct pblk_lun *rlun;
1746 int nr_luns = geo->nr_luns;
1747 int bit = -1;
1748
1749 while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) {
1750 rlun = &pblk->luns[bit];
1751 up(&rlun->wr_sem);
1752 }
1753
1754 kfree(lun_bitmap);
1755}
1756
1757void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1758{
1759 struct ppa_addr l2p_ppa;
1760
1761 /* logic error: lba out-of-bounds. Ignore update */
1762 if (!(lba < pblk->rl.nr_secs)) {
1763 WARN(1, "pblk: corrupted L2P map request\n");
1764 return;
1765 }
1766
1767 spin_lock(&pblk->trans_lock);
1768 l2p_ppa = pblk_trans_map_get(pblk, lba);
1769
1770 if (!pblk_addr_in_cache(l2p_ppa) && !pblk_ppa_empty(l2p_ppa))
1771 pblk_map_invalidate(pblk, l2p_ppa);
1772
1773 pblk_trans_map_set(pblk, lba, ppa);
1774 spin_unlock(&pblk->trans_lock);
1775}
1776
1777void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1778{
1779#ifdef CONFIG_NVM_DEBUG
1780 /* Callers must ensure that the ppa points to a cache address */
1781 BUG_ON(!pblk_addr_in_cache(ppa));
1782 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1783#endif
1784
1785 pblk_update_map(pblk, lba, ppa);
1786}
1787
1788int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1789 struct pblk_line *gc_line)
1790{
1791 struct ppa_addr l2p_ppa;
1792 int ret = 1;
1793
1794#ifdef CONFIG_NVM_DEBUG
1795 /* Callers must ensure that the ppa points to a cache address */
1796 BUG_ON(!pblk_addr_in_cache(ppa));
1797 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1798#endif
1799
1800 /* logic error: lba out-of-bounds. Ignore update */
1801 if (!(lba < pblk->rl.nr_secs)) {
1802 WARN(1, "pblk: corrupted L2P map request\n");
1803 return 0;
1804 }
1805
1806 spin_lock(&pblk->trans_lock);
1807 l2p_ppa = pblk_trans_map_get(pblk, lba);
1808
1809 /* Prevent updated entries to be overwritten by GC */
1810 if (pblk_addr_in_cache(l2p_ppa) || pblk_ppa_empty(l2p_ppa) ||
1811 pblk_tgt_ppa_to_line(l2p_ppa) != gc_line->id) {
1812 ret = 0;
1813 goto out;
1814 }
1815
1816 pblk_trans_map_set(pblk, lba, ppa);
1817out:
1818 spin_unlock(&pblk->trans_lock);
1819 return ret;
1820}
1821
1822void pblk_update_map_dev(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1823 struct ppa_addr entry_line)
1824{
1825 struct ppa_addr l2p_line;
1826
1827#ifdef CONFIG_NVM_DEBUG
1828 /* Callers must ensure that the ppa points to a device address */
1829 BUG_ON(pblk_addr_in_cache(ppa));
1830#endif
1831 /* Invalidate and discard padded entries */
1832 if (lba == ADDR_EMPTY) {
1833#ifdef CONFIG_NVM_DEBUG
1834 atomic_long_inc(&pblk->padded_wb);
1835#endif
1836 pblk_map_invalidate(pblk, ppa);
1837 return;
1838 }
1839
1840 /* logic error: lba out-of-bounds. Ignore update */
1841 if (!(lba < pblk->rl.nr_secs)) {
1842 WARN(1, "pblk: corrupted L2P map request\n");
1843 return;
1844 }
1845
1846 spin_lock(&pblk->trans_lock);
1847 l2p_line = pblk_trans_map_get(pblk, lba);
1848
1849 /* Do not update L2P if the cacheline has been updated. In this case,
1850 * the mapped ppa must be invalidated
1851 */
1852 if (l2p_line.ppa != entry_line.ppa) {
1853 if (!pblk_ppa_empty(ppa))
1854 pblk_map_invalidate(pblk, ppa);
1855 goto out;
1856 }
1857
1858#ifdef CONFIG_NVM_DEBUG
1859 WARN_ON(!pblk_addr_in_cache(l2p_line) && !pblk_ppa_empty(l2p_line));
1860#endif
1861
1862 pblk_trans_map_set(pblk, lba, ppa);
1863out:
1864 spin_unlock(&pblk->trans_lock);
1865}
1866
1867void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
1868 sector_t blba, int nr_secs)
1869{
1870 int i;
1871
1872 spin_lock(&pblk->trans_lock);
1873 for (i = 0; i < nr_secs; i++)
1874 ppas[i] = pblk_trans_map_get(pblk, blba + i);
1875 spin_unlock(&pblk->trans_lock);
1876}
1877
1878void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
1879 u64 *lba_list, int nr_secs)
1880{
1881 sector_t lba;
1882 int i;
1883
1884 spin_lock(&pblk->trans_lock);
1885 for (i = 0; i < nr_secs; i++) {
1886 lba = lba_list[i];
1887 if (lba == ADDR_EMPTY) {
1888 ppas[i].ppa = ADDR_EMPTY;
1889 } else {
1890 /* logic error: lba out-of-bounds. Ignore update */
1891 if (!(lba < pblk->rl.nr_secs)) {
1892 WARN(1, "pblk: corrupted L2P map request\n");
1893 continue;
1894 }
1895 ppas[i] = pblk_trans_map_get(pblk, lba);
1896 }
1897 }
1898 spin_unlock(&pblk->trans_lock);
1899}