1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * pblk-core.c - pblk's core functionality
17 *
18 */
19
20#define CREATE_TRACE_POINTS
21
22#include "pblk.h"
23#include "pblk-trace.h"
24
25static void pblk_line_mark_bb(struct work_struct *work)
26{
27 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
28 ws);
29 struct pblk *pblk = line_ws->pblk;
30 struct nvm_tgt_dev *dev = pblk->dev;
31 struct ppa_addr *ppa = line_ws->priv;
32 int ret;
33
34 ret = nvm_set_chunk_meta(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
35 if (ret) {
36 struct pblk_line *line;
37 int pos;
38
39 line = pblk_ppa_to_line(pblk, *ppa);
40 pos = pblk_ppa_to_pos(&dev->geo, *ppa);
41
42 pblk_err(pblk, "failed to mark bb, line:%d, pos:%d\n",
43 line->id, pos);
44 }
45
46 kfree(ppa);
47 mempool_free(line_ws, &pblk->gen_ws_pool);
48}
49
50static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
51 struct ppa_addr ppa_addr)
52{
53 struct nvm_tgt_dev *dev = pblk->dev;
54 struct nvm_geo *geo = &dev->geo;
55 struct ppa_addr *ppa;
56 int pos = pblk_ppa_to_pos(geo, ppa_addr);
57
58 pblk_debug(pblk, "erase failed: line:%d, pos:%d\n", line->id, pos);
59 atomic_long_inc(&pblk->erase_failed);
60
61 atomic_dec(&line->blk_in_line);
62 if (test_and_set_bit(pos, line->blk_bitmap))
63 pblk_err(pblk, "attempted to erase bb: line:%d, pos:%d\n",
64 line->id, pos);
65
66 /* Not necessary to mark bad blocks on 2.0 spec. */
67 if (geo->version == NVM_OCSSD_SPEC_20)
68 return;
69
70 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
71 if (!ppa)
72 return;
73
74 *ppa = ppa_addr;
75 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
76 GFP_ATOMIC, pblk->bb_wq);
77}
78
79static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
80{
81 struct nvm_tgt_dev *dev = pblk->dev;
82 struct nvm_geo *geo = &dev->geo;
83 struct nvm_chk_meta *chunk;
84 struct pblk_line *line;
85 int pos;
86
87 line = pblk_ppa_to_line(pblk, rqd->ppa_addr);
88 pos = pblk_ppa_to_pos(geo, rqd->ppa_addr);
89 chunk = &line->chks[pos];
90
91 atomic_dec(&line->left_seblks);
92
93 if (rqd->error) {
94 trace_pblk_chunk_reset(pblk_disk_name(pblk),
95 &rqd->ppa_addr, PBLK_CHUNK_RESET_FAILED);
96
97 chunk->state = NVM_CHK_ST_OFFLINE;
98 pblk_mark_bb(pblk, line, rqd->ppa_addr);
99 } else {
100 trace_pblk_chunk_reset(pblk_disk_name(pblk),
101 &rqd->ppa_addr, PBLK_CHUNK_RESET_DONE);
102
103 chunk->state = NVM_CHK_ST_FREE;
104 }
105
106 trace_pblk_chunk_state(pblk_disk_name(pblk), &rqd->ppa_addr,
107 chunk->state);
108
109 atomic_dec(&pblk->inflight_io);
110}
111
112/* Erase completion assumes that only one block is erased at the time */
113static void pblk_end_io_erase(struct nvm_rq *rqd)
114{
115 struct pblk *pblk = rqd->private;
116
117 __pblk_end_io_erase(pblk, rqd);
118 mempool_free(rqd, &pblk->e_rq_pool);
119}
120
121/*
122 * Get information for all chunks from the device.
123 *
124 * The caller is responsible for freeing (vmalloc) the returned structure
125 */
126struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk)
127{
128 struct nvm_tgt_dev *dev = pblk->dev;
129 struct nvm_geo *geo = &dev->geo;
130 struct nvm_chk_meta *meta;
131 struct ppa_addr ppa;
132 unsigned long len;
133 int ret;
134
135 ppa.ppa = 0;
136
137 len = geo->all_chunks * sizeof(*meta);
138 meta = vzalloc(len);
139 if (!meta)
140 return ERR_PTR(-ENOMEM);
141
142 ret = nvm_get_chunk_meta(dev, ppa, geo->all_chunks, meta);
143 if (ret) {
144 vfree(meta);
145 return ERR_PTR(-EIO);
146 }
147
148 return meta;
149}
150
151struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
152 struct nvm_chk_meta *meta,
153 struct ppa_addr ppa)
154{
155 struct nvm_tgt_dev *dev = pblk->dev;
156 struct nvm_geo *geo = &dev->geo;
157 int ch_off = ppa.m.grp * geo->num_chk * geo->num_lun;
158 int lun_off = ppa.m.pu * geo->num_chk;
159 int chk_off = ppa.m.chk;
160
161 return meta + ch_off + lun_off + chk_off;
162}
163
164void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
165 u64 paddr)
166{
167 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
168 struct list_head *move_list = NULL;
169
170 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
171 * table is modified with reclaimed sectors, a check is done to endure
172 * that newer updates are not overwritten.
173 */
174 spin_lock(&line->lock);
175 WARN_ON(line->state == PBLK_LINESTATE_FREE);
176
177 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
178 WARN_ONCE(1, "pblk: double invalidate\n");
179 spin_unlock(&line->lock);
180 return;
181 }
182 le32_add_cpu(line->vsc, -1);
183
184 if (line->state == PBLK_LINESTATE_CLOSED)
185 move_list = pblk_line_gc_list(pblk, line);
186 spin_unlock(&line->lock);
187
188 if (move_list) {
189 spin_lock(&l_mg->gc_lock);
190 spin_lock(&line->lock);
191 /* Prevent moving a line that has just been chosen for GC */
192 if (line->state == PBLK_LINESTATE_GC) {
193 spin_unlock(&line->lock);
194 spin_unlock(&l_mg->gc_lock);
195 return;
196 }
197 spin_unlock(&line->lock);
198
199 list_move_tail(&line->list, move_list);
200 spin_unlock(&l_mg->gc_lock);
201 }
202}
203
204void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
205{
206 struct pblk_line *line;
207 u64 paddr;
208
209#ifdef CONFIG_NVM_PBLK_DEBUG
210 /* Callers must ensure that the ppa points to a device address */
211 BUG_ON(pblk_addr_in_cache(ppa));
212 BUG_ON(pblk_ppa_empty(ppa));
213#endif
214
215 line = pblk_ppa_to_line(pblk, ppa);
216 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
217
218 __pblk_map_invalidate(pblk, line, paddr);
219}
220
221static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
222 unsigned int nr_secs)
223{
224 sector_t lba;
225
226 spin_lock(&pblk->trans_lock);
227 for (lba = slba; lba < slba + nr_secs; lba++) {
228 struct ppa_addr ppa;
229
230 ppa = pblk_trans_map_get(pblk, lba);
231
232 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
233 pblk_map_invalidate(pblk, ppa);
234
235 pblk_ppa_set_empty(&ppa);
236 pblk_trans_map_set(pblk, lba, ppa);
237 }
238 spin_unlock(&pblk->trans_lock);
239}
240
241int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
242{
243 struct nvm_tgt_dev *dev = pblk->dev;
244
245 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
246 &rqd->dma_meta_list);
247 if (!rqd->meta_list)
248 return -ENOMEM;
249
250 if (rqd->nr_ppas == 1)
251 return 0;
252
253 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size(pblk);
254 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size(pblk);
255
256 return 0;
257}
258
259void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
260{
261 struct nvm_tgt_dev *dev = pblk->dev;
262
263 if (rqd->meta_list)
264 nvm_dev_dma_free(dev->parent, rqd->meta_list,
265 rqd->dma_meta_list);
266}
267
268/* Caller must guarantee that the request is a valid type */
269struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
270{
271 mempool_t *pool;
272 struct nvm_rq *rqd;
273 int rq_size;
274
275 switch (type) {
276 case PBLK_WRITE:
277 case PBLK_WRITE_INT:
278 pool = &pblk->w_rq_pool;
279 rq_size = pblk_w_rq_size;
280 break;
281 case PBLK_READ:
282 pool = &pblk->r_rq_pool;
283 rq_size = pblk_g_rq_size;
284 break;
285 default:
286 pool = &pblk->e_rq_pool;
287 rq_size = pblk_g_rq_size;
288 }
289
290 rqd = mempool_alloc(pool, GFP_KERNEL);
291 memset(rqd, 0, rq_size);
292
293 return rqd;
294}
295
296/* Typically used on completion path. Cannot guarantee request consistency */
297void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
298{
299 mempool_t *pool;
300
301 switch (type) {
302 case PBLK_WRITE:
303 kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
304 /* fall through */
305 case PBLK_WRITE_INT:
306 pool = &pblk->w_rq_pool;
307 break;
308 case PBLK_READ:
309 pool = &pblk->r_rq_pool;
310 break;
311 case PBLK_ERASE:
312 pool = &pblk->e_rq_pool;
313 break;
314 default:
315 pblk_err(pblk, "trying to free unknown rqd type\n");
316 return;
317 }
318
319 pblk_free_rqd_meta(pblk, rqd);
320 mempool_free(rqd, pool);
321}
322
323void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
324 int nr_pages)
325{
326 struct bio_vec bv;
327 int i;
328
329 WARN_ON(off + nr_pages != bio->bi_vcnt);
330
331 for (i = off; i < nr_pages + off; i++) {
332 bv = bio->bi_io_vec[i];
333 mempool_free(bv.bv_page, &pblk->page_bio_pool);
334 }
335}
336
337int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
338 int nr_pages)
339{
340 struct request_queue *q = pblk->dev->q;
341 struct page *page;
342 int i, ret;
343
344 for (i = 0; i < nr_pages; i++) {
345 page = mempool_alloc(&pblk->page_bio_pool, flags);
346
347 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
348 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
349 pblk_err(pblk, "could not add page to bio\n");
350 mempool_free(page, &pblk->page_bio_pool);
351 goto err;
352 }
353 }
354
355 return 0;
356err:
357 pblk_bio_free_pages(pblk, bio, (bio->bi_vcnt - i), i);
358 return -1;
359}
360
361void pblk_write_kick(struct pblk *pblk)
362{
363 wake_up_process(pblk->writer_ts);
364 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
365}
366
367void pblk_write_timer_fn(struct timer_list *t)
368{
369 struct pblk *pblk = from_timer(pblk, t, wtimer);
370
371 /* kick the write thread every tick to flush outstanding data */
372 pblk_write_kick(pblk);
373}
374
375void pblk_write_should_kick(struct pblk *pblk)
376{
377 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
378
379 if (secs_avail >= pblk->min_write_pgs_data)
380 pblk_write_kick(pblk);
381}
382
383static void pblk_wait_for_meta(struct pblk *pblk)
384{
385 do {
386 if (!atomic_read(&pblk->inflight_io))
387 break;
388
389 schedule();
390 } while (1);
391}
392
393static void pblk_flush_writer(struct pblk *pblk)
394{
395 pblk_rb_flush(&pblk->rwb);
396 do {
397 if (!pblk_rb_sync_count(&pblk->rwb))
398 break;
399
400 pblk_write_kick(pblk);
401 schedule();
402 } while (1);
403}
404
405struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
406{
407 struct pblk_line_meta *lm = &pblk->lm;
408 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
409 struct list_head *move_list = NULL;
410 int packed_meta = (le32_to_cpu(*line->vsc) / pblk->min_write_pgs_data)
411 * (pblk->min_write_pgs - pblk->min_write_pgs_data);
412 int vsc = le32_to_cpu(*line->vsc) + packed_meta;
413
414 lockdep_assert_held(&line->lock);
415
416 if (line->w_err_gc->has_write_err) {
417 if (line->gc_group != PBLK_LINEGC_WERR) {
418 line->gc_group = PBLK_LINEGC_WERR;
419 move_list = &l_mg->gc_werr_list;
420 pblk_rl_werr_line_in(&pblk->rl);
421 }
422 } else if (!vsc) {
423 if (line->gc_group != PBLK_LINEGC_FULL) {
424 line->gc_group = PBLK_LINEGC_FULL;
425 move_list = &l_mg->gc_full_list;
426 }
427 } else if (vsc < lm->high_thrs) {
428 if (line->gc_group != PBLK_LINEGC_HIGH) {
429 line->gc_group = PBLK_LINEGC_HIGH;
430 move_list = &l_mg->gc_high_list;
431 }
432 } else if (vsc < lm->mid_thrs) {
433 if (line->gc_group != PBLK_LINEGC_MID) {
434 line->gc_group = PBLK_LINEGC_MID;
435 move_list = &l_mg->gc_mid_list;
436 }
437 } else if (vsc < line->sec_in_line) {
438 if (line->gc_group != PBLK_LINEGC_LOW) {
439 line->gc_group = PBLK_LINEGC_LOW;
440 move_list = &l_mg->gc_low_list;
441 }
442 } else if (vsc == line->sec_in_line) {
443 if (line->gc_group != PBLK_LINEGC_EMPTY) {
444 line->gc_group = PBLK_LINEGC_EMPTY;
445 move_list = &l_mg->gc_empty_list;
446 }
447 } else {
448 line->state = PBLK_LINESTATE_CORRUPT;
449 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
450 line->state);
451
452 line->gc_group = PBLK_LINEGC_NONE;
453 move_list = &l_mg->corrupt_list;
454 pblk_err(pblk, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
455 line->id, vsc,
456 line->sec_in_line,
457 lm->high_thrs, lm->mid_thrs);
458 }
459
460 return move_list;
461}
462
463void pblk_discard(struct pblk *pblk, struct bio *bio)
464{
465 sector_t slba = pblk_get_lba(bio);
466 sector_t nr_secs = pblk_get_secs(bio);
467
468 pblk_invalidate_range(pblk, slba, nr_secs);
469}
470
471void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
472{
473 atomic_long_inc(&pblk->write_failed);
474#ifdef CONFIG_NVM_PBLK_DEBUG
475 pblk_print_failed_rqd(pblk, rqd, rqd->error);
476#endif
477}
478
479void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
480{
481 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
482 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
483 atomic_long_inc(&pblk->read_empty);
484 return;
485 }
486
487 switch (rqd->error) {
488 case NVM_RSP_WARN_HIGHECC:
489 atomic_long_inc(&pblk->read_high_ecc);
490 break;
491 case NVM_RSP_ERR_FAILECC:
492 case NVM_RSP_ERR_FAILCRC:
493 atomic_long_inc(&pblk->read_failed);
494 break;
495 default:
496 pblk_err(pblk, "unknown read error:%d\n", rqd->error);
497 }
498#ifdef CONFIG_NVM_PBLK_DEBUG
499 pblk_print_failed_rqd(pblk, rqd, rqd->error);
500#endif
501}
502
503void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
504{
505 pblk->sec_per_write = sec_per_write;
506}
507
508int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
509{
510 struct nvm_tgt_dev *dev = pblk->dev;
511
512 atomic_inc(&pblk->inflight_io);
513
514#ifdef CONFIG_NVM_PBLK_DEBUG
515 if (pblk_check_io(pblk, rqd))
516 return NVM_IO_ERR;
517#endif
518
519 return nvm_submit_io(dev, rqd);
520}
521
522void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd)
523{
524 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
525
526 int i;
527
528 for (i = 0; i < rqd->nr_ppas; i++) {
529 struct ppa_addr *ppa = &ppa_list[i];
530 struct nvm_chk_meta *chunk = pblk_dev_ppa_to_chunk(pblk, *ppa);
531 u64 caddr = pblk_dev_ppa_to_chunk_addr(pblk, *ppa);
532
533 if (caddr == 0)
534 trace_pblk_chunk_state(pblk_disk_name(pblk),
535 ppa, NVM_CHK_ST_OPEN);
536 else if (caddr == (chunk->cnlb - 1))
537 trace_pblk_chunk_state(pblk_disk_name(pblk),
538 ppa, NVM_CHK_ST_CLOSED);
539 }
540}
541
542int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
543{
544 struct nvm_tgt_dev *dev = pblk->dev;
545 int ret;
546
547 atomic_inc(&pblk->inflight_io);
548
549#ifdef CONFIG_NVM_PBLK_DEBUG
550 if (pblk_check_io(pblk, rqd))
551 return NVM_IO_ERR;
552#endif
553
554 ret = nvm_submit_io_sync(dev, rqd);
555
556 if (trace_pblk_chunk_state_enabled() && !ret &&
557 rqd->opcode == NVM_OP_PWRITE)
558 pblk_check_chunk_state_update(pblk, rqd);
559
560 return ret;
561}
562
563int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd)
564{
565 struct ppa_addr *ppa_list;
566 int ret;
567
568 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
569
570 pblk_down_chunk(pblk, ppa_list[0]);
571 ret = pblk_submit_io_sync(pblk, rqd);
572 pblk_up_chunk(pblk, ppa_list[0]);
573
574 return ret;
575}
576
577static void pblk_bio_map_addr_endio(struct bio *bio)
578{
579 bio_put(bio);
580}
581
582struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
583 unsigned int nr_secs, unsigned int len,
584 int alloc_type, gfp_t gfp_mask)
585{
586 struct nvm_tgt_dev *dev = pblk->dev;
587 void *kaddr = data;
588 struct page *page;
589 struct bio *bio;
590 int i, ret;
591
592 if (alloc_type == PBLK_KMALLOC_META)
593 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
594
595 bio = bio_kmalloc(gfp_mask, nr_secs);
596 if (!bio)
597 return ERR_PTR(-ENOMEM);
598
599 for (i = 0; i < nr_secs; i++) {
600 page = vmalloc_to_page(kaddr);
601 if (!page) {
602 pblk_err(pblk, "could not map vmalloc bio\n");
603 bio_put(bio);
604 bio = ERR_PTR(-ENOMEM);
605 goto out;
606 }
607
608 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
609 if (ret != PAGE_SIZE) {
610 pblk_err(pblk, "could not add page to bio\n");
611 bio_put(bio);
612 bio = ERR_PTR(-ENOMEM);
613 goto out;
614 }
615
616 kaddr += PAGE_SIZE;
617 }
618
619 bio->bi_end_io = pblk_bio_map_addr_endio;
620out:
621 return bio;
622}
623
624int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
625 unsigned long secs_to_flush, bool skip_meta)
626{
627 int max = pblk->sec_per_write;
628 int min = pblk->min_write_pgs;
629 int secs_to_sync = 0;
630
631 if (skip_meta && pblk->min_write_pgs_data != pblk->min_write_pgs)
632 min = max = pblk->min_write_pgs_data;
633
634 if (secs_avail >= max)
635 secs_to_sync = max;
636 else if (secs_avail >= min)
637 secs_to_sync = min * (secs_avail / min);
638 else if (secs_to_flush)
639 secs_to_sync = min;
640
641 return secs_to_sync;
642}
643
644void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
645{
646 u64 addr;
647 int i;
648
649 spin_lock(&line->lock);
650 addr = find_next_zero_bit(line->map_bitmap,
651 pblk->lm.sec_per_line, line->cur_sec);
652 line->cur_sec = addr - nr_secs;
653
654 for (i = 0; i < nr_secs; i++, line->cur_sec--)
655 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
656 spin_unlock(&line->lock);
657}
658
659u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
660{
661 u64 addr;
662 int i;
663
664 lockdep_assert_held(&line->lock);
665
666 /* logic error: ppa out-of-bounds. Prevent generating bad address */
667 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
668 WARN(1, "pblk: page allocation out of bounds\n");
669 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
670 }
671
672 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
673 pblk->lm.sec_per_line, line->cur_sec);
674 for (i = 0; i < nr_secs; i++, line->cur_sec++)
675 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
676
677 return addr;
678}
679
680u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
681{
682 u64 addr;
683
684 /* Lock needed in case a write fails and a recovery needs to remap
685 * failed write buffer entries
686 */
687 spin_lock(&line->lock);
688 addr = __pblk_alloc_page(pblk, line, nr_secs);
689 line->left_msecs -= nr_secs;
690 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
691 spin_unlock(&line->lock);
692
693 return addr;
694}
695
696u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
697{
698 u64 paddr;
699
700 spin_lock(&line->lock);
701 paddr = find_next_zero_bit(line->map_bitmap,
702 pblk->lm.sec_per_line, line->cur_sec);
703 spin_unlock(&line->lock);
704
705 return paddr;
706}
707
708u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
709{
710 struct nvm_tgt_dev *dev = pblk->dev;
711 struct nvm_geo *geo = &dev->geo;
712 struct pblk_line_meta *lm = &pblk->lm;
713 int bit;
714
715 /* This usually only happens on bad lines */
716 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
717 if (bit >= lm->blk_per_line)
718 return -1;
719
720 return bit * geo->ws_opt;
721}
722
723int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line)
724{
725 struct nvm_tgt_dev *dev = pblk->dev;
726 struct pblk_line_meta *lm = &pblk->lm;
727 struct bio *bio;
728 struct nvm_rq rqd;
729 u64 paddr = pblk_line_smeta_start(pblk, line);
730 int i, ret;
731
732 memset(&rqd, 0, sizeof(struct nvm_rq));
733
734 ret = pblk_alloc_rqd_meta(pblk, &rqd);
735 if (ret)
736 return ret;
737
738 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
739 if (IS_ERR(bio)) {
740 ret = PTR_ERR(bio);
741 goto clear_rqd;
742 }
743
744 bio->bi_iter.bi_sector = 0; /* internal bio */
745 bio_set_op_attrs(bio, REQ_OP_READ, 0);
746
747 rqd.bio = bio;
748 rqd.opcode = NVM_OP_PREAD;
749 rqd.nr_ppas = lm->smeta_sec;
750 rqd.is_seq = 1;
751
752 for (i = 0; i < lm->smeta_sec; i++, paddr++)
753 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
754
755 ret = pblk_submit_io_sync(pblk, &rqd);
756 if (ret) {
757 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
758 bio_put(bio);
759 goto clear_rqd;
760 }
761
762 atomic_dec(&pblk->inflight_io);
763
764 if (rqd.error)
765 pblk_log_read_err(pblk, &rqd);
766
767clear_rqd:
768 pblk_free_rqd_meta(pblk, &rqd);
769 return ret;
770}
771
772static int pblk_line_smeta_write(struct pblk *pblk, struct pblk_line *line,
773 u64 paddr)
774{
775 struct nvm_tgt_dev *dev = pblk->dev;
776 struct pblk_line_meta *lm = &pblk->lm;
777 struct bio *bio;
778 struct nvm_rq rqd;
779 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
780 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
781 int i, ret;
782
783 memset(&rqd, 0, sizeof(struct nvm_rq));
784
785 ret = pblk_alloc_rqd_meta(pblk, &rqd);
786 if (ret)
787 return ret;
788
789 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
790 if (IS_ERR(bio)) {
791 ret = PTR_ERR(bio);
792 goto clear_rqd;
793 }
794
795 bio->bi_iter.bi_sector = 0; /* internal bio */
796 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
797
798 rqd.bio = bio;
799 rqd.opcode = NVM_OP_PWRITE;
800 rqd.nr_ppas = lm->smeta_sec;
801 rqd.is_seq = 1;
802
803 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
804 struct pblk_sec_meta *meta = pblk_get_meta(pblk,
805 rqd.meta_list, i);
806
807 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
808 meta->lba = lba_list[paddr] = addr_empty;
809 }
810
811 ret = pblk_submit_io_sync_sem(pblk, &rqd);
812 if (ret) {
813 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
814 bio_put(bio);
815 goto clear_rqd;
816 }
817
818 atomic_dec(&pblk->inflight_io);
819
820 if (rqd.error) {
821 pblk_log_write_err(pblk, &rqd);
822 ret = -EIO;
823 }
824
825clear_rqd:
826 pblk_free_rqd_meta(pblk, &rqd);
827 return ret;
828}
829
830int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
831 void *emeta_buf)
832{
833 struct nvm_tgt_dev *dev = pblk->dev;
834 struct nvm_geo *geo = &dev->geo;
835 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
836 struct pblk_line_meta *lm = &pblk->lm;
837 void *ppa_list, *meta_list;
838 struct bio *bio;
839 struct nvm_rq rqd;
840 u64 paddr = line->emeta_ssec;
841 dma_addr_t dma_ppa_list, dma_meta_list;
842 int min = pblk->min_write_pgs;
843 int left_ppas = lm->emeta_sec[0];
844 int line_id = line->id;
845 int rq_ppas, rq_len;
846 int i, j;
847 int ret;
848
849 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
850 &dma_meta_list);
851 if (!meta_list)
852 return -ENOMEM;
853
854 ppa_list = meta_list + pblk_dma_meta_size(pblk);
855 dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
856
857next_rq:
858 memset(&rqd, 0, sizeof(struct nvm_rq));
859
860 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
861 rq_len = rq_ppas * geo->csecs;
862
863 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
864 l_mg->emeta_alloc_type, GFP_KERNEL);
865 if (IS_ERR(bio)) {
866 ret = PTR_ERR(bio);
867 goto free_rqd_dma;
868 }
869
870 bio->bi_iter.bi_sector = 0; /* internal bio */
871 bio_set_op_attrs(bio, REQ_OP_READ, 0);
872
873 rqd.bio = bio;
874 rqd.meta_list = meta_list;
875 rqd.ppa_list = ppa_list;
876 rqd.dma_meta_list = dma_meta_list;
877 rqd.dma_ppa_list = dma_ppa_list;
878 rqd.opcode = NVM_OP_PREAD;
879 rqd.nr_ppas = rq_ppas;
880
881 for (i = 0; i < rqd.nr_ppas; ) {
882 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, line_id);
883 int pos = pblk_ppa_to_pos(geo, ppa);
884
885 if (pblk_io_aligned(pblk, rq_ppas))
886 rqd.is_seq = 1;
887
888 while (test_bit(pos, line->blk_bitmap)) {
889 paddr += min;
890 if (pblk_boundary_paddr_checks(pblk, paddr)) {
891 bio_put(bio);
892 ret = -EINTR;
893 goto free_rqd_dma;
894 }
895
896 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
897 pos = pblk_ppa_to_pos(geo, ppa);
898 }
899
900 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
901 bio_put(bio);
902 ret = -EINTR;
903 goto free_rqd_dma;
904 }
905
906 for (j = 0; j < min; j++, i++, paddr++)
907 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line_id);
908 }
909
910 ret = pblk_submit_io_sync(pblk, &rqd);
911 if (ret) {
912 pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
913 bio_put(bio);
914 goto free_rqd_dma;
915 }
916
917 atomic_dec(&pblk->inflight_io);
918
919 if (rqd.error)
920 pblk_log_read_err(pblk, &rqd);
921
922 emeta_buf += rq_len;
923 left_ppas -= rq_ppas;
924 if (left_ppas)
925 goto next_rq;
926
927free_rqd_dma:
928 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
929 return ret;
930}
931
932static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
933 struct ppa_addr ppa)
934{
935 rqd->opcode = NVM_OP_ERASE;
936 rqd->ppa_addr = ppa;
937 rqd->nr_ppas = 1;
938 rqd->is_seq = 1;
939 rqd->bio = NULL;
940}
941
942static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
943{
944 struct nvm_rq rqd = {NULL};
945 int ret;
946
947 trace_pblk_chunk_reset(pblk_disk_name(pblk), &ppa,
948 PBLK_CHUNK_RESET_START);
949
950 pblk_setup_e_rq(pblk, &rqd, ppa);
951
952 /* The write thread schedules erases so that it minimizes disturbances
953 * with writes. Thus, there is no need to take the LUN semaphore.
954 */
955 ret = pblk_submit_io_sync(pblk, &rqd);
956 rqd.private = pblk;
957 __pblk_end_io_erase(pblk, &rqd);
958
959 return ret;
960}
961
962int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
963{
964 struct pblk_line_meta *lm = &pblk->lm;
965 struct ppa_addr ppa;
966 int ret, bit = -1;
967
968 /* Erase only good blocks, one at a time */
969 do {
970 spin_lock(&line->lock);
971 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
972 bit + 1);
973 if (bit >= lm->blk_per_line) {
974 spin_unlock(&line->lock);
975 break;
976 }
977
978 ppa = pblk->luns[bit].bppa; /* set ch and lun */
979 ppa.a.blk = line->id;
980
981 atomic_dec(&line->left_eblks);
982 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
983 spin_unlock(&line->lock);
984
985 ret = pblk_blk_erase_sync(pblk, ppa);
986 if (ret) {
987 pblk_err(pblk, "failed to erase line %d\n", line->id);
988 return ret;
989 }
990 } while (1);
991
992 return 0;
993}
994
995static void pblk_line_setup_metadata(struct pblk_line *line,
996 struct pblk_line_mgmt *l_mg,
997 struct pblk_line_meta *lm)
998{
999 int meta_line;
1000
1001 lockdep_assert_held(&l_mg->free_lock);
1002
1003retry_meta:
1004 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
1005 if (meta_line == PBLK_DATA_LINES) {
1006 spin_unlock(&l_mg->free_lock);
1007 io_schedule();
1008 spin_lock(&l_mg->free_lock);
1009 goto retry_meta;
1010 }
1011
1012 set_bit(meta_line, &l_mg->meta_bitmap);
1013 line->meta_line = meta_line;
1014
1015 line->smeta = l_mg->sline_meta[meta_line];
1016 line->emeta = l_mg->eline_meta[meta_line];
1017
1018 memset(line->smeta, 0, lm->smeta_len);
1019 memset(line->emeta->buf, 0, lm->emeta_len[0]);
1020
1021 line->emeta->mem = 0;
1022 atomic_set(&line->emeta->sync, 0);
1023}
1024
1025/* For now lines are always assumed full lines. Thus, smeta former and current
1026 * lun bitmaps are omitted.
1027 */
1028static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
1029 struct pblk_line *cur)
1030{
1031 struct nvm_tgt_dev *dev = pblk->dev;
1032 struct nvm_geo *geo = &dev->geo;
1033 struct pblk_line_meta *lm = &pblk->lm;
1034 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1035 struct pblk_emeta *emeta = line->emeta;
1036 struct line_emeta *emeta_buf = emeta->buf;
1037 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
1038 int nr_blk_line;
1039
1040 /* After erasing the line, new bad blocks might appear and we risk
1041 * having an invalid line
1042 */
1043 nr_blk_line = lm->blk_per_line -
1044 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
1045 if (nr_blk_line < lm->min_blk_line) {
1046 spin_lock(&l_mg->free_lock);
1047 spin_lock(&line->lock);
1048 line->state = PBLK_LINESTATE_BAD;
1049 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1050 line->state);
1051 spin_unlock(&line->lock);
1052
1053 list_add_tail(&line->list, &l_mg->bad_list);
1054 spin_unlock(&l_mg->free_lock);
1055
1056 pblk_debug(pblk, "line %d is bad\n", line->id);
1057
1058 return 0;
1059 }
1060
1061 /* Run-time metadata */
1062 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
1063
1064 /* Mark LUNs allocated in this line (all for now) */
1065 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
1066
1067 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1068 guid_copy((guid_t *)&smeta_buf->header.uuid, &pblk->instance_uuid);
1069 smeta_buf->header.id = cpu_to_le32(line->id);
1070 smeta_buf->header.type = cpu_to_le16(line->type);
1071 smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
1072 smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
1073
1074 /* Start metadata */
1075 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1076 smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
1077
1078 /* Fill metadata among lines */
1079 if (cur) {
1080 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
1081 smeta_buf->prev_id = cpu_to_le32(cur->id);
1082 cur->emeta->buf->next_id = cpu_to_le32(line->id);
1083 } else {
1084 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
1085 }
1086
1087 /* All smeta must be set at this point */
1088 smeta_buf->header.crc = cpu_to_le32(
1089 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
1090 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
1091
1092 /* End metadata */
1093 memcpy(&emeta_buf->header, &smeta_buf->header,
1094 sizeof(struct line_header));
1095
1096 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1097 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1098 emeta_buf->header.crc = cpu_to_le32(
1099 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1100
1101 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1102 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1103 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1104 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1105 emeta_buf->crc = cpu_to_le32(0);
1106 emeta_buf->prev_id = smeta_buf->prev_id;
1107
1108 return 1;
1109}
1110
1111static int pblk_line_alloc_bitmaps(struct pblk *pblk, struct pblk_line *line)
1112{
1113 struct pblk_line_meta *lm = &pblk->lm;
1114 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1115
1116 line->map_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
1117 if (!line->map_bitmap)
1118 return -ENOMEM;
1119
1120 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1121
1122 /* will be initialized using bb info from map_bitmap */
1123 line->invalid_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
1124 if (!line->invalid_bitmap) {
1125 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1126 line->map_bitmap = NULL;
1127 return -ENOMEM;
1128 }
1129
1130 return 0;
1131}
1132
1133/* For now lines are always assumed full lines. Thus, smeta former and current
1134 * lun bitmaps are omitted.
1135 */
1136static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1137 int init)
1138{
1139 struct nvm_tgt_dev *dev = pblk->dev;
1140 struct nvm_geo *geo = &dev->geo;
1141 struct pblk_line_meta *lm = &pblk->lm;
1142 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1143 u64 off;
1144 int bit = -1;
1145 int emeta_secs;
1146
1147 line->sec_in_line = lm->sec_per_line;
1148
1149 /* Capture bad block information on line mapping bitmaps */
1150 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1151 bit + 1)) < lm->blk_per_line) {
1152 off = bit * geo->ws_opt;
1153 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1154 lm->sec_per_line);
1155 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1156 lm->sec_per_line);
1157 line->sec_in_line -= geo->clba;
1158 }
1159
1160 /* Mark smeta metadata sectors as bad sectors */
1161 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1162 off = bit * geo->ws_opt;
1163 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1164 line->sec_in_line -= lm->smeta_sec;
1165 line->smeta_ssec = off;
1166 line->cur_sec = off + lm->smeta_sec;
1167
1168 if (init && pblk_line_smeta_write(pblk, line, off)) {
1169 pblk_debug(pblk, "line smeta I/O failed. Retry\n");
1170 return 0;
1171 }
1172
1173 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1174
1175 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1176 * blocks to make sure that there are enough sectors to store emeta
1177 */
1178 emeta_secs = lm->emeta_sec[0];
1179 off = lm->sec_per_line;
1180 while (emeta_secs) {
1181 off -= geo->ws_opt;
1182 if (!test_bit(off, line->invalid_bitmap)) {
1183 bitmap_set(line->invalid_bitmap, off, geo->ws_opt);
1184 emeta_secs -= geo->ws_opt;
1185 }
1186 }
1187
1188 line->emeta_ssec = off;
1189 line->sec_in_line -= lm->emeta_sec[0];
1190 line->nr_valid_lbas = 0;
1191 line->left_msecs = line->sec_in_line;
1192 *line->vsc = cpu_to_le32(line->sec_in_line);
1193
1194 if (lm->sec_per_line - line->sec_in_line !=
1195 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1196 spin_lock(&line->lock);
1197 line->state = PBLK_LINESTATE_BAD;
1198 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1199 line->state);
1200 spin_unlock(&line->lock);
1201
1202 list_add_tail(&line->list, &l_mg->bad_list);
1203 pblk_err(pblk, "unexpected line %d is bad\n", line->id);
1204
1205 return 0;
1206 }
1207
1208 return 1;
1209}
1210
1211static int pblk_prepare_new_line(struct pblk *pblk, struct pblk_line *line)
1212{
1213 struct pblk_line_meta *lm = &pblk->lm;
1214 struct nvm_tgt_dev *dev = pblk->dev;
1215 struct nvm_geo *geo = &dev->geo;
1216 int blk_to_erase = atomic_read(&line->blk_in_line);
1217 int i;
1218
1219 for (i = 0; i < lm->blk_per_line; i++) {
1220 struct pblk_lun *rlun = &pblk->luns[i];
1221 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1222 int state = line->chks[pos].state;
1223
1224 /* Free chunks should not be erased */
1225 if (state & NVM_CHK_ST_FREE) {
1226 set_bit(pblk_ppa_to_pos(geo, rlun->bppa),
1227 line->erase_bitmap);
1228 blk_to_erase--;
1229 }
1230 }
1231
1232 return blk_to_erase;
1233}
1234
1235static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1236{
1237 struct pblk_line_meta *lm = &pblk->lm;
1238 int blk_in_line = atomic_read(&line->blk_in_line);
1239 int blk_to_erase;
1240
1241 /* Bad blocks do not need to be erased */
1242 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
1243
1244 spin_lock(&line->lock);
1245
1246 /* If we have not written to this line, we need to mark up free chunks
1247 * as already erased
1248 */
1249 if (line->state == PBLK_LINESTATE_NEW) {
1250 blk_to_erase = pblk_prepare_new_line(pblk, line);
1251 line->state = PBLK_LINESTATE_FREE;
1252 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1253 line->state);
1254 } else {
1255 blk_to_erase = blk_in_line;
1256 }
1257
1258 if (blk_in_line < lm->min_blk_line) {
1259 spin_unlock(&line->lock);
1260 return -EAGAIN;
1261 }
1262
1263 if (line->state != PBLK_LINESTATE_FREE) {
1264 WARN(1, "pblk: corrupted line %d, state %d\n",
1265 line->id, line->state);
1266 spin_unlock(&line->lock);
1267 return -EINTR;
1268 }
1269
1270 line->state = PBLK_LINESTATE_OPEN;
1271 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1272 line->state);
1273
1274 atomic_set(&line->left_eblks, blk_to_erase);
1275 atomic_set(&line->left_seblks, blk_to_erase);
1276
1277 line->meta_distance = lm->meta_distance;
1278 spin_unlock(&line->lock);
1279
1280 kref_init(&line->ref);
1281 atomic_set(&line->sec_to_update, 0);
1282
1283 return 0;
1284}
1285
1286/* Line allocations in the recovery path are always single threaded */
1287int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1288{
1289 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1290 int ret;
1291
1292 spin_lock(&l_mg->free_lock);
1293 l_mg->data_line = line;
1294 list_del(&line->list);
1295
1296 ret = pblk_line_prepare(pblk, line);
1297 if (ret) {
1298 list_add(&line->list, &l_mg->free_list);
1299 spin_unlock(&l_mg->free_lock);
1300 return ret;
1301 }
1302 spin_unlock(&l_mg->free_lock);
1303
1304 ret = pblk_line_alloc_bitmaps(pblk, line);
1305 if (ret)
1306 goto fail;
1307
1308 if (!pblk_line_init_bb(pblk, line, 0)) {
1309 ret = -EINTR;
1310 goto fail;
1311 }
1312
1313 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1314 return 0;
1315
1316fail:
1317 spin_lock(&l_mg->free_lock);
1318 list_add(&line->list, &l_mg->free_list);
1319 spin_unlock(&l_mg->free_lock);
1320
1321 return ret;
1322}
1323
1324void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1325{
1326 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1327
1328 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1329 line->map_bitmap = NULL;
1330 line->smeta = NULL;
1331 line->emeta = NULL;
1332}
1333
1334static void pblk_line_reinit(struct pblk_line *line)
1335{
1336 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1337
1338 line->map_bitmap = NULL;
1339 line->invalid_bitmap = NULL;
1340 line->smeta = NULL;
1341 line->emeta = NULL;
1342}
1343
1344void pblk_line_free(struct pblk_line *line)
1345{
1346 struct pblk *pblk = line->pblk;
1347 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1348
1349 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1350 mempool_free(line->invalid_bitmap, l_mg->bitmap_pool);
1351
1352 pblk_line_reinit(line);
1353}
1354
1355struct pblk_line *pblk_line_get(struct pblk *pblk)
1356{
1357 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1358 struct pblk_line_meta *lm = &pblk->lm;
1359 struct pblk_line *line;
1360 int ret, bit;
1361
1362 lockdep_assert_held(&l_mg->free_lock);
1363
1364retry:
1365 if (list_empty(&l_mg->free_list)) {
1366 pblk_err(pblk, "no free lines\n");
1367 return NULL;
1368 }
1369
1370 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1371 list_del(&line->list);
1372 l_mg->nr_free_lines--;
1373
1374 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1375 if (unlikely(bit >= lm->blk_per_line)) {
1376 spin_lock(&line->lock);
1377 line->state = PBLK_LINESTATE_BAD;
1378 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1379 line->state);
1380 spin_unlock(&line->lock);
1381
1382 list_add_tail(&line->list, &l_mg->bad_list);
1383
1384 pblk_debug(pblk, "line %d is bad\n", line->id);
1385 goto retry;
1386 }
1387
1388 ret = pblk_line_prepare(pblk, line);
1389 if (ret) {
1390 switch (ret) {
1391 case -EAGAIN:
1392 list_add(&line->list, &l_mg->bad_list);
1393 goto retry;
1394 case -EINTR:
1395 list_add(&line->list, &l_mg->corrupt_list);
1396 goto retry;
1397 default:
1398 pblk_err(pblk, "failed to prepare line %d\n", line->id);
1399 list_add(&line->list, &l_mg->free_list);
1400 l_mg->nr_free_lines++;
1401 return NULL;
1402 }
1403 }
1404
1405 return line;
1406}
1407
1408static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1409 struct pblk_line *line)
1410{
1411 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1412 struct pblk_line *retry_line;
1413
1414retry:
1415 spin_lock(&l_mg->free_lock);
1416 retry_line = pblk_line_get(pblk);
1417 if (!retry_line) {
1418 l_mg->data_line = NULL;
1419 spin_unlock(&l_mg->free_lock);
1420 return NULL;
1421 }
1422
1423 retry_line->map_bitmap = line->map_bitmap;
1424 retry_line->invalid_bitmap = line->invalid_bitmap;
1425 retry_line->smeta = line->smeta;
1426 retry_line->emeta = line->emeta;
1427 retry_line->meta_line = line->meta_line;
1428
1429 pblk_line_reinit(line);
1430
1431 l_mg->data_line = retry_line;
1432 spin_unlock(&l_mg->free_lock);
1433
1434 pblk_rl_free_lines_dec(&pblk->rl, line, false);
1435
1436 if (pblk_line_erase(pblk, retry_line))
1437 goto retry;
1438
1439 return retry_line;
1440}
1441
1442static void pblk_set_space_limit(struct pblk *pblk)
1443{
1444 struct pblk_rl *rl = &pblk->rl;
1445
1446 atomic_set(&rl->rb_space, 0);
1447}
1448
1449struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1450{
1451 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1452 struct pblk_line *line;
1453
1454 spin_lock(&l_mg->free_lock);
1455 line = pblk_line_get(pblk);
1456 if (!line) {
1457 spin_unlock(&l_mg->free_lock);
1458 return NULL;
1459 }
1460
1461 line->seq_nr = l_mg->d_seq_nr++;
1462 line->type = PBLK_LINETYPE_DATA;
1463 l_mg->data_line = line;
1464
1465 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
1466
1467 /* Allocate next line for preparation */
1468 l_mg->data_next = pblk_line_get(pblk);
1469 if (!l_mg->data_next) {
1470 /* If we cannot get a new line, we need to stop the pipeline.
1471 * Only allow as many writes in as we can store safely and then
1472 * fail gracefully
1473 */
1474 pblk_set_space_limit(pblk);
1475
1476 l_mg->data_next = NULL;
1477 } else {
1478 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1479 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1480 }
1481 spin_unlock(&l_mg->free_lock);
1482
1483 if (pblk_line_alloc_bitmaps(pblk, line))
1484 return NULL;
1485
1486 if (pblk_line_erase(pblk, line)) {
1487 line = pblk_line_retry(pblk, line);
1488 if (!line)
1489 return NULL;
1490 }
1491
1492retry_setup:
1493 if (!pblk_line_init_metadata(pblk, line, NULL)) {
1494 line = pblk_line_retry(pblk, line);
1495 if (!line)
1496 return NULL;
1497
1498 goto retry_setup;
1499 }
1500
1501 if (!pblk_line_init_bb(pblk, line, 1)) {
1502 line = pblk_line_retry(pblk, line);
1503 if (!line)
1504 return NULL;
1505
1506 goto retry_setup;
1507 }
1508
1509 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1510
1511 return line;
1512}
1513
1514void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa)
1515{
1516 struct pblk_line *line;
1517
1518 line = pblk_ppa_to_line(pblk, ppa);
1519 kref_put(&line->ref, pblk_line_put_wq);
1520}
1521
1522void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd)
1523{
1524 struct ppa_addr *ppa_list;
1525 int i;
1526
1527 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1528
1529 for (i = 0; i < rqd->nr_ppas; i++)
1530 pblk_ppa_to_line_put(pblk, ppa_list[i]);
1531}
1532
1533static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1534{
1535 lockdep_assert_held(&pblk->l_mg.free_lock);
1536
1537 pblk_set_space_limit(pblk);
1538 pblk->state = PBLK_STATE_STOPPING;
1539 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
1540}
1541
1542static void pblk_line_close_meta_sync(struct pblk *pblk)
1543{
1544 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1545 struct pblk_line_meta *lm = &pblk->lm;
1546 struct pblk_line *line, *tline;
1547 LIST_HEAD(list);
1548
1549 spin_lock(&l_mg->close_lock);
1550 if (list_empty(&l_mg->emeta_list)) {
1551 spin_unlock(&l_mg->close_lock);
1552 return;
1553 }
1554
1555 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1556 spin_unlock(&l_mg->close_lock);
1557
1558 list_for_each_entry_safe(line, tline, &list, list) {
1559 struct pblk_emeta *emeta = line->emeta;
1560
1561 while (emeta->mem < lm->emeta_len[0]) {
1562 int ret;
1563
1564 ret = pblk_submit_meta_io(pblk, line);
1565 if (ret) {
1566 pblk_err(pblk, "sync meta line %d failed (%d)\n",
1567 line->id, ret);
1568 return;
1569 }
1570 }
1571 }
1572
1573 pblk_wait_for_meta(pblk);
1574 flush_workqueue(pblk->close_wq);
1575}
1576
1577void __pblk_pipeline_flush(struct pblk *pblk)
1578{
1579 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1580 int ret;
1581
1582 spin_lock(&l_mg->free_lock);
1583 if (pblk->state == PBLK_STATE_RECOVERING ||
1584 pblk->state == PBLK_STATE_STOPPED) {
1585 spin_unlock(&l_mg->free_lock);
1586 return;
1587 }
1588 pblk->state = PBLK_STATE_RECOVERING;
1589 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
1590 spin_unlock(&l_mg->free_lock);
1591
1592 pblk_flush_writer(pblk);
1593 pblk_wait_for_meta(pblk);
1594
1595 ret = pblk_recov_pad(pblk);
1596 if (ret) {
1597 pblk_err(pblk, "could not close data on teardown(%d)\n", ret);
1598 return;
1599 }
1600
1601 flush_workqueue(pblk->bb_wq);
1602 pblk_line_close_meta_sync(pblk);
1603}
1604
1605void __pblk_pipeline_stop(struct pblk *pblk)
1606{
1607 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1608
1609 spin_lock(&l_mg->free_lock);
1610 pblk->state = PBLK_STATE_STOPPED;
1611 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
1612 l_mg->data_line = NULL;
1613 l_mg->data_next = NULL;
1614 spin_unlock(&l_mg->free_lock);
1615}
1616
1617void pblk_pipeline_stop(struct pblk *pblk)
1618{
1619 __pblk_pipeline_flush(pblk);
1620 __pblk_pipeline_stop(pblk);
1621}
1622
1623struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
1624{
1625 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1626 struct pblk_line *cur, *new = NULL;
1627 unsigned int left_seblks;
1628
1629 new = l_mg->data_next;
1630 if (!new)
1631 goto out;
1632
1633 spin_lock(&l_mg->free_lock);
1634 cur = l_mg->data_line;
1635 l_mg->data_line = new;
1636
1637 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
1638 spin_unlock(&l_mg->free_lock);
1639
1640retry_erase:
1641 left_seblks = atomic_read(&new->left_seblks);
1642 if (left_seblks) {
1643 /* If line is not fully erased, erase it */
1644 if (atomic_read(&new->left_eblks)) {
1645 if (pblk_line_erase(pblk, new))
1646 goto out;
1647 } else {
1648 io_schedule();
1649 }
1650 goto retry_erase;
1651 }
1652
1653 if (pblk_line_alloc_bitmaps(pblk, new))
1654 return NULL;
1655
1656retry_setup:
1657 if (!pblk_line_init_metadata(pblk, new, cur)) {
1658 new = pblk_line_retry(pblk, new);
1659 if (!new)
1660 goto out;
1661
1662 goto retry_setup;
1663 }
1664
1665 if (!pblk_line_init_bb(pblk, new, 1)) {
1666 new = pblk_line_retry(pblk, new);
1667 if (!new)
1668 goto out;
1669
1670 goto retry_setup;
1671 }
1672
1673 pblk_rl_free_lines_dec(&pblk->rl, new, true);
1674
1675 /* Allocate next line for preparation */
1676 spin_lock(&l_mg->free_lock);
1677 l_mg->data_next = pblk_line_get(pblk);
1678 if (!l_mg->data_next) {
1679 /* If we cannot get a new line, we need to stop the pipeline.
1680 * Only allow as many writes in as we can store safely and then
1681 * fail gracefully
1682 */
1683 pblk_stop_writes(pblk, new);
1684 l_mg->data_next = NULL;
1685 } else {
1686 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1687 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1688 }
1689 spin_unlock(&l_mg->free_lock);
1690
1691out:
1692 return new;
1693}
1694
1695static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
1696{
1697 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1698 struct pblk_gc *gc = &pblk->gc;
1699
1700 spin_lock(&line->lock);
1701 WARN_ON(line->state != PBLK_LINESTATE_GC);
1702 line->state = PBLK_LINESTATE_FREE;
1703 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1704 line->state);
1705 line->gc_group = PBLK_LINEGC_NONE;
1706 pblk_line_free(line);
1707
1708 if (line->w_err_gc->has_write_err) {
1709 pblk_rl_werr_line_out(&pblk->rl);
1710 line->w_err_gc->has_write_err = 0;
1711 }
1712
1713 spin_unlock(&line->lock);
1714 atomic_dec(&gc->pipeline_gc);
1715
1716 spin_lock(&l_mg->free_lock);
1717 list_add_tail(&line->list, &l_mg->free_list);
1718 l_mg->nr_free_lines++;
1719 spin_unlock(&l_mg->free_lock);
1720
1721 pblk_rl_free_lines_inc(&pblk->rl, line);
1722}
1723
1724static void pblk_line_put_ws(struct work_struct *work)
1725{
1726 struct pblk_line_ws *line_put_ws = container_of(work,
1727 struct pblk_line_ws, ws);
1728 struct pblk *pblk = line_put_ws->pblk;
1729 struct pblk_line *line = line_put_ws->line;
1730
1731 __pblk_line_put(pblk, line);
1732 mempool_free(line_put_ws, &pblk->gen_ws_pool);
1733}
1734
1735void pblk_line_put(struct kref *ref)
1736{
1737 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1738 struct pblk *pblk = line->pblk;
1739
1740 __pblk_line_put(pblk, line);
1741}
1742
1743void pblk_line_put_wq(struct kref *ref)
1744{
1745 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1746 struct pblk *pblk = line->pblk;
1747 struct pblk_line_ws *line_put_ws;
1748
1749 line_put_ws = mempool_alloc(&pblk->gen_ws_pool, GFP_ATOMIC);
1750 if (!line_put_ws)
1751 return;
1752
1753 line_put_ws->pblk = pblk;
1754 line_put_ws->line = line;
1755 line_put_ws->priv = NULL;
1756
1757 INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
1758 queue_work(pblk->r_end_wq, &line_put_ws->ws);
1759}
1760
1761int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1762{
1763 struct nvm_rq *rqd;
1764 int err;
1765
1766 rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
1767
1768 pblk_setup_e_rq(pblk, rqd, ppa);
1769
1770 rqd->end_io = pblk_end_io_erase;
1771 rqd->private = pblk;
1772
1773 trace_pblk_chunk_reset(pblk_disk_name(pblk),
1774 &ppa, PBLK_CHUNK_RESET_START);
1775
1776 /* The write thread schedules erases so that it minimizes disturbances
1777 * with writes. Thus, there is no need to take the LUN semaphore.
1778 */
1779 err = pblk_submit_io(pblk, rqd);
1780 if (err) {
1781 struct nvm_tgt_dev *dev = pblk->dev;
1782 struct nvm_geo *geo = &dev->geo;
1783
1784 pblk_err(pblk, "could not async erase line:%d,blk:%d\n",
1785 pblk_ppa_to_line_id(ppa),
1786 pblk_ppa_to_pos(geo, ppa));
1787 }
1788
1789 return err;
1790}
1791
1792struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1793{
1794 return pblk->l_mg.data_line;
1795}
1796
1797/* For now, always erase next line */
1798struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
1799{
1800 return pblk->l_mg.data_next;
1801}
1802
1803int pblk_line_is_full(struct pblk_line *line)
1804{
1805 return (line->left_msecs == 0);
1806}
1807
1808static void pblk_line_should_sync_meta(struct pblk *pblk)
1809{
1810 if (pblk_rl_is_limit(&pblk->rl))
1811 pblk_line_close_meta_sync(pblk);
1812}
1813
1814void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1815{
1816 struct nvm_tgt_dev *dev = pblk->dev;
1817 struct nvm_geo *geo = &dev->geo;
1818 struct pblk_line_meta *lm = &pblk->lm;
1819 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1820 struct list_head *move_list;
1821 int i;
1822
1823#ifdef CONFIG_NVM_PBLK_DEBUG
1824 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
1825 "pblk: corrupt closed line %d\n", line->id);
1826#endif
1827
1828 spin_lock(&l_mg->free_lock);
1829 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1830 spin_unlock(&l_mg->free_lock);
1831
1832 spin_lock(&l_mg->gc_lock);
1833 spin_lock(&line->lock);
1834 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1835 line->state = PBLK_LINESTATE_CLOSED;
1836 move_list = pblk_line_gc_list(pblk, line);
1837 list_add_tail(&line->list, move_list);
1838
1839 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1840 line->map_bitmap = NULL;
1841 line->smeta = NULL;
1842 line->emeta = NULL;
1843
1844 for (i = 0; i < lm->blk_per_line; i++) {
1845 struct pblk_lun *rlun = &pblk->luns[i];
1846 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1847 int state = line->chks[pos].state;
1848
1849 if (!(state & NVM_CHK_ST_OFFLINE))
1850 state = NVM_CHK_ST_CLOSED;
1851 }
1852
1853 spin_unlock(&line->lock);
1854 spin_unlock(&l_mg->gc_lock);
1855
1856 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1857 line->state);
1858}
1859
1860void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1861{
1862 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1863 struct pblk_line_meta *lm = &pblk->lm;
1864 struct pblk_emeta *emeta = line->emeta;
1865 struct line_emeta *emeta_buf = emeta->buf;
1866 struct wa_counters *wa = emeta_to_wa(lm, emeta_buf);
1867
1868 /* No need for exact vsc value; avoid a big line lock and take aprox. */
1869 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1870 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1871
1872 wa->user = cpu_to_le64(atomic64_read(&pblk->user_wa));
1873 wa->pad = cpu_to_le64(atomic64_read(&pblk->pad_wa));
1874 wa->gc = cpu_to_le64(atomic64_read(&pblk->gc_wa));
1875
1876 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) {
1877 emeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1878 guid_copy((guid_t *)&emeta_buf->header.uuid,
1879 &pblk->instance_uuid);
1880 emeta_buf->header.id = cpu_to_le32(line->id);
1881 emeta_buf->header.type = cpu_to_le16(line->type);
1882 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1883 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1884 emeta_buf->header.crc = cpu_to_le32(
1885 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1886 }
1887
1888 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1889 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1890
1891 spin_lock(&l_mg->close_lock);
1892 spin_lock(&line->lock);
1893
1894 /* Update the in-memory start address for emeta, in case it has
1895 * shifted due to write errors
1896 */
1897 if (line->emeta_ssec != line->cur_sec)
1898 line->emeta_ssec = line->cur_sec;
1899
1900 list_add_tail(&line->list, &l_mg->emeta_list);
1901 spin_unlock(&line->lock);
1902 spin_unlock(&l_mg->close_lock);
1903
1904 pblk_line_should_sync_meta(pblk);
1905}
1906
1907static void pblk_save_lba_list(struct pblk *pblk, struct pblk_line *line)
1908{
1909 struct pblk_line_meta *lm = &pblk->lm;
1910 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1911 unsigned int lba_list_size = lm->emeta_len[2];
1912 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1913 struct pblk_emeta *emeta = line->emeta;
1914
1915 w_err_gc->lba_list = pblk_malloc(lba_list_size,
1916 l_mg->emeta_alloc_type, GFP_KERNEL);
1917 memcpy(w_err_gc->lba_list, emeta_to_lbas(pblk, emeta->buf),
1918 lba_list_size);
1919}
1920
1921void pblk_line_close_ws(struct work_struct *work)
1922{
1923 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1924 ws);
1925 struct pblk *pblk = line_ws->pblk;
1926 struct pblk_line *line = line_ws->line;
1927 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1928
1929 /* Write errors makes the emeta start address stored in smeta invalid,
1930 * so keep a copy of the lba list until we've gc'd the line
1931 */
1932 if (w_err_gc->has_write_err)
1933 pblk_save_lba_list(pblk, line);
1934
1935 pblk_line_close(pblk, line);
1936 mempool_free(line_ws, &pblk->gen_ws_pool);
1937}
1938
1939void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1940 void (*work)(struct work_struct *), gfp_t gfp_mask,
1941 struct workqueue_struct *wq)
1942{
1943 struct pblk_line_ws *line_ws;
1944
1945 line_ws = mempool_alloc(&pblk->gen_ws_pool, gfp_mask);
1946
1947 line_ws->pblk = pblk;
1948 line_ws->line = line;
1949 line_ws->priv = priv;
1950
1951 INIT_WORK(&line_ws->ws, work);
1952 queue_work(wq, &line_ws->ws);
1953}
1954
1955static void __pblk_down_chunk(struct pblk *pblk, int pos)
1956{
1957 struct pblk_lun *rlun = &pblk->luns[pos];
1958 int ret;
1959
1960 /*
1961 * Only send one inflight I/O per LUN. Since we map at a page
1962 * granurality, all ppas in the I/O will map to the same LUN
1963 */
1964
1965 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
1966 if (ret == -ETIME || ret == -EINTR)
1967 pblk_err(pblk, "taking lun semaphore timed out: err %d\n",
1968 -ret);
1969}
1970
1971void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa)
1972{
1973 struct nvm_tgt_dev *dev = pblk->dev;
1974 struct nvm_geo *geo = &dev->geo;
1975 int pos = pblk_ppa_to_pos(geo, ppa);
1976
1977 __pblk_down_chunk(pblk, pos);
1978}
1979
1980void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
1981 unsigned long *lun_bitmap)
1982{
1983 struct nvm_tgt_dev *dev = pblk->dev;
1984 struct nvm_geo *geo = &dev->geo;
1985 int pos = pblk_ppa_to_pos(geo, ppa);
1986
1987 /* If the LUN has been locked for this same request, do no attempt to
1988 * lock it again
1989 */
1990 if (test_and_set_bit(pos, lun_bitmap))
1991 return;
1992
1993 __pblk_down_chunk(pblk, pos);
1994}
1995
1996void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa)
1997{
1998 struct nvm_tgt_dev *dev = pblk->dev;
1999 struct nvm_geo *geo = &dev->geo;
2000 struct pblk_lun *rlun;
2001 int pos = pblk_ppa_to_pos(geo, ppa);
2002
2003 rlun = &pblk->luns[pos];
2004 up(&rlun->wr_sem);
2005}
2006
2007void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap)
2008{
2009 struct nvm_tgt_dev *dev = pblk->dev;
2010 struct nvm_geo *geo = &dev->geo;
2011 struct pblk_lun *rlun;
2012 int num_lun = geo->all_luns;
2013 int bit = -1;
2014
2015 while ((bit = find_next_bit(lun_bitmap, num_lun, bit + 1)) < num_lun) {
2016 rlun = &pblk->luns[bit];
2017 up(&rlun->wr_sem);
2018 }
2019}
2020
2021void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2022{
2023 struct ppa_addr ppa_l2p;
2024
2025 /* logic error: lba out-of-bounds. Ignore update */
2026 if (!(lba < pblk->rl.nr_secs)) {
2027 WARN(1, "pblk: corrupted L2P map request\n");
2028 return;
2029 }
2030
2031 spin_lock(&pblk->trans_lock);
2032 ppa_l2p = pblk_trans_map_get(pblk, lba);
2033
2034 if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
2035 pblk_map_invalidate(pblk, ppa_l2p);
2036
2037 pblk_trans_map_set(pblk, lba, ppa);
2038 spin_unlock(&pblk->trans_lock);
2039}
2040
2041void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2042{
2043
2044#ifdef CONFIG_NVM_PBLK_DEBUG
2045 /* Callers must ensure that the ppa points to a cache address */
2046 BUG_ON(!pblk_addr_in_cache(ppa));
2047 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
2048#endif
2049
2050 pblk_update_map(pblk, lba, ppa);
2051}
2052
2053int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
2054 struct pblk_line *gc_line, u64 paddr_gc)
2055{
2056 struct ppa_addr ppa_l2p, ppa_gc;
2057 int ret = 1;
2058
2059#ifdef CONFIG_NVM_PBLK_DEBUG
2060 /* Callers must ensure that the ppa points to a cache address */
2061 BUG_ON(!pblk_addr_in_cache(ppa_new));
2062 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
2063#endif
2064
2065 /* logic error: lba out-of-bounds. Ignore update */
2066 if (!(lba < pblk->rl.nr_secs)) {
2067 WARN(1, "pblk: corrupted L2P map request\n");
2068 return 0;
2069 }
2070
2071 spin_lock(&pblk->trans_lock);
2072 ppa_l2p = pblk_trans_map_get(pblk, lba);
2073 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
2074
2075 if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
2076 spin_lock(&gc_line->lock);
2077 WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
2078 "pblk: corrupted GC update");
2079 spin_unlock(&gc_line->lock);
2080
2081 ret = 0;
2082 goto out;
2083 }
2084
2085 pblk_trans_map_set(pblk, lba, ppa_new);
2086out:
2087 spin_unlock(&pblk->trans_lock);
2088 return ret;
2089}
2090
2091void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
2092 struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
2093{
2094 struct ppa_addr ppa_l2p;
2095
2096#ifdef CONFIG_NVM_PBLK_DEBUG
2097 /* Callers must ensure that the ppa points to a device address */
2098 BUG_ON(pblk_addr_in_cache(ppa_mapped));
2099#endif
2100 /* Invalidate and discard padded entries */
2101 if (lba == ADDR_EMPTY) {
2102 atomic64_inc(&pblk->pad_wa);
2103#ifdef CONFIG_NVM_PBLK_DEBUG
2104 atomic_long_inc(&pblk->padded_wb);
2105#endif
2106 if (!pblk_ppa_empty(ppa_mapped))
2107 pblk_map_invalidate(pblk, ppa_mapped);
2108 return;
2109 }
2110
2111 /* logic error: lba out-of-bounds. Ignore update */
2112 if (!(lba < pblk->rl.nr_secs)) {
2113 WARN(1, "pblk: corrupted L2P map request\n");
2114 return;
2115 }
2116
2117 spin_lock(&pblk->trans_lock);
2118 ppa_l2p = pblk_trans_map_get(pblk, lba);
2119
2120 /* Do not update L2P if the cacheline has been updated. In this case,
2121 * the mapped ppa must be invalidated
2122 */
2123 if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
2124 if (!pblk_ppa_empty(ppa_mapped))
2125 pblk_map_invalidate(pblk, ppa_mapped);
2126 goto out;
2127 }
2128
2129#ifdef CONFIG_NVM_PBLK_DEBUG
2130 WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
2131#endif
2132
2133 pblk_trans_map_set(pblk, lba, ppa_mapped);
2134out:
2135 spin_unlock(&pblk->trans_lock);
2136}
2137
2138void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
2139 sector_t blba, int nr_secs)
2140{
2141 int i;
2142
2143 spin_lock(&pblk->trans_lock);
2144 for (i = 0; i < nr_secs; i++) {
2145 struct ppa_addr ppa;
2146
2147 ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
2148
2149 /* If the L2P entry maps to a line, the reference is valid */
2150 if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
2151 struct pblk_line *line = pblk_ppa_to_line(pblk, ppa);
2152
2153 kref_get(&line->ref);
2154 }
2155 }
2156 spin_unlock(&pblk->trans_lock);
2157}
2158
2159void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
2160 u64 *lba_list, int nr_secs)
2161{
2162 u64 lba;
2163 int i;
2164
2165 spin_lock(&pblk->trans_lock);
2166 for (i = 0; i < nr_secs; i++) {
2167 lba = lba_list[i];
2168 if (lba != ADDR_EMPTY) {
2169 /* logic error: lba out-of-bounds. Ignore update */
2170 if (!(lba < pblk->rl.nr_secs)) {
2171 WARN(1, "pblk: corrupted L2P map request\n");
2172 continue;
2173 }
2174 ppas[i] = pblk_trans_map_get(pblk, lba);
2175 }
2176 }
2177 spin_unlock(&pblk->trans_lock);
2178}
2179
2180void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd)
2181{
2182 void *buffer;
2183
2184 if (pblk_is_oob_meta_supported(pblk)) {
2185 /* Just use OOB metadata buffer as always */
2186 buffer = rqd->meta_list;
2187 } else {
2188 /* We need to reuse last page of request (packed metadata)
2189 * in similar way as traditional oob metadata
2190 */
2191 buffer = page_to_virt(
2192 rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
2193 }
2194
2195 return buffer;
2196}
2197
2198void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd)
2199{
2200 void *meta_list = rqd->meta_list;
2201 void *page;
2202 int i = 0;
2203
2204 if (pblk_is_oob_meta_supported(pblk))
2205 return;
2206
2207 page = page_to_virt(rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
2208 /* We need to fill oob meta buffer with data from packed metadata */
2209 for (; i < rqd->nr_ppas; i++)
2210 memcpy(pblk_get_meta(pblk, meta_list, i),
2211 page + (i * sizeof(struct pblk_sec_meta)),
2212 sizeof(struct pblk_sec_meta));
2213}
2214