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