blob: 29565f89a85e0a48e06d4b9dcd9c95d2821d2200 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-core.c - pblk's core functionality
16 *
17 */
18
19#include "pblk.h"
20#include <linux/time.h>
21
22static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
23 struct ppa_addr *ppa)
24{
25 struct nvm_tgt_dev *dev = pblk->dev;
26 struct nvm_geo *geo = &dev->geo;
27 int pos = pblk_dev_ppa_to_pos(geo, *ppa);
28
29 pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos);
30 atomic_long_inc(&pblk->erase_failed);
31
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020032 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020033 if (test_and_set_bit(pos, line->blk_bitmap))
34 pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n",
35 line->id, pos);
36
37 pblk_line_run_ws(pblk, NULL, ppa, pblk_line_mark_bb);
38}
39
40static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
41{
42 struct pblk_line *line;
43
44 line = &pblk->lines[pblk_dev_ppa_to_line(rqd->ppa_addr)];
45 atomic_dec(&line->left_seblks);
46
47 if (rqd->error) {
48 struct ppa_addr *ppa;
49
50 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
51 if (!ppa)
52 return;
53
54 *ppa = rqd->ppa_addr;
55 pblk_mark_bb(pblk, line, ppa);
56 }
57}
58
59/* Erase completion assumes that only one block is erased at the time */
60static void pblk_end_io_erase(struct nvm_rq *rqd)
61{
62 struct pblk *pblk = rqd->private;
63
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020064 __pblk_end_io_erase(pblk, rqd);
Javier González084ec9b2017-06-26 16:27:13 -060065 mempool_free(rqd, pblk->g_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020066}
67
Javier González0880a9a2017-06-26 11:57:19 +020068void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
69 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020070{
71 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
72 struct list_head *move_list = NULL;
73
74 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
75 * table is modified with reclaimed sectors, a check is done to endure
76 * that newer updates are not overwritten.
77 */
78 spin_lock(&line->lock);
79 if (line->state == PBLK_LINESTATE_GC ||
80 line->state == PBLK_LINESTATE_FREE) {
81 spin_unlock(&line->lock);
82 return;
83 }
84
85 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
86 WARN_ONCE(1, "pblk: double invalidate\n");
87 spin_unlock(&line->lock);
88 return;
89 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020090 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091
92 if (line->state == PBLK_LINESTATE_CLOSED)
93 move_list = pblk_line_gc_list(pblk, line);
94 spin_unlock(&line->lock);
95
96 if (move_list) {
97 spin_lock(&l_mg->gc_lock);
98 spin_lock(&line->lock);
99 /* Prevent moving a line that has just been chosen for GC */
100 if (line->state == PBLK_LINESTATE_GC ||
101 line->state == PBLK_LINESTATE_FREE) {
102 spin_unlock(&line->lock);
103 spin_unlock(&l_mg->gc_lock);
104 return;
105 }
106 spin_unlock(&line->lock);
107
108 list_move_tail(&line->list, move_list);
109 spin_unlock(&l_mg->gc_lock);
110 }
111}
112
113void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
114{
115 struct pblk_line *line;
116 u64 paddr;
117 int line_id;
118
119#ifdef CONFIG_NVM_DEBUG
120 /* Callers must ensure that the ppa points to a device address */
121 BUG_ON(pblk_addr_in_cache(ppa));
122 BUG_ON(pblk_ppa_empty(ppa));
123#endif
124
125 line_id = pblk_tgt_ppa_to_line(ppa);
126 line = &pblk->lines[line_id];
127 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
128
129 __pblk_map_invalidate(pblk, line, paddr);
130}
131
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200132static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
133 unsigned int nr_secs)
134{
135 sector_t lba;
136
137 spin_lock(&pblk->trans_lock);
138 for (lba = slba; lba < slba + nr_secs; lba++) {
139 struct ppa_addr ppa;
140
141 ppa = pblk_trans_map_get(pblk, lba);
142
143 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
144 pblk_map_invalidate(pblk, ppa);
145
146 pblk_ppa_set_empty(&ppa);
147 pblk_trans_map_set(pblk, lba, ppa);
148 }
149 spin_unlock(&pblk->trans_lock);
150}
151
152struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int rw)
153{
154 mempool_t *pool;
155 struct nvm_rq *rqd;
156 int rq_size;
157
158 if (rw == WRITE) {
159 pool = pblk->w_rq_pool;
160 rq_size = pblk_w_rq_size;
161 } else {
Javier González084ec9b2017-06-26 16:27:13 -0600162 pool = pblk->g_rq_pool;
163 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200164 }
165
166 rqd = mempool_alloc(pool, GFP_KERNEL);
167 memset(rqd, 0, rq_size);
168
169 return rqd;
170}
171
172void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int rw)
173{
174 mempool_t *pool;
175
176 if (rw == WRITE)
177 pool = pblk->w_rq_pool;
178 else
Javier González084ec9b2017-06-26 16:27:13 -0600179 pool = pblk->g_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200180
181 mempool_free(rqd, pool);
182}
183
184void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
185 int nr_pages)
186{
187 struct bio_vec bv;
188 int i;
189
190 WARN_ON(off + nr_pages != bio->bi_vcnt);
191
192 bio_advance(bio, off * PBLK_EXPOSED_PAGE_SIZE);
193 for (i = off; i < nr_pages + off; i++) {
194 bv = bio->bi_io_vec[i];
195 mempool_free(bv.bv_page, pblk->page_pool);
196 }
197}
198
199int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
200 int nr_pages)
201{
202 struct request_queue *q = pblk->dev->q;
203 struct page *page;
204 int i, ret;
205
206 for (i = 0; i < nr_pages; i++) {
207 page = mempool_alloc(pblk->page_pool, flags);
208 if (!page)
209 goto err;
210
211 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
212 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
213 pr_err("pblk: could not add page to bio\n");
214 mempool_free(page, pblk->page_pool);
215 goto err;
216 }
217 }
218
219 return 0;
220err:
221 pblk_bio_free_pages(pblk, bio, 0, i - 1);
222 return -1;
223}
224
225static void pblk_write_kick(struct pblk *pblk)
226{
227 wake_up_process(pblk->writer_ts);
228 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
229}
230
231void pblk_write_timer_fn(unsigned long data)
232{
233 struct pblk *pblk = (struct pblk *)data;
234
235 /* kick the write thread every tick to flush outstanding data */
236 pblk_write_kick(pblk);
237}
238
239void pblk_write_should_kick(struct pblk *pblk)
240{
241 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
242
243 if (secs_avail >= pblk->min_write_pgs)
244 pblk_write_kick(pblk);
245}
246
247void pblk_end_bio_sync(struct bio *bio)
248{
249 struct completion *waiting = bio->bi_private;
250
251 complete(waiting);
252}
253
254void pblk_end_io_sync(struct nvm_rq *rqd)
255{
256 struct completion *waiting = rqd->private;
257
258 complete(waiting);
259}
260
261void pblk_flush_writer(struct pblk *pblk)
262{
263 struct bio *bio;
264 int ret;
265 DECLARE_COMPLETION_ONSTACK(wait);
266
267 bio = bio_alloc(GFP_KERNEL, 1);
268 if (!bio)
269 return;
270
271 bio->bi_iter.bi_sector = 0; /* internal bio */
272 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_OP_FLUSH);
273 bio->bi_private = &wait;
274 bio->bi_end_io = pblk_end_bio_sync;
275
276 ret = pblk_write_to_cache(pblk, bio, 0);
277 if (ret == NVM_IO_OK) {
278 if (!wait_for_completion_io_timeout(&wait,
279 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
280 pr_err("pblk: flush cache timed out\n");
281 }
282 } else if (ret != NVM_IO_DONE) {
283 pr_err("pblk: tear down bio failed\n");
284 }
285
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200286 if (bio->bi_status)
287 pr_err("pblk: flush sync write failed (%u)\n", bio->bi_status);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288
289 bio_put(bio);
290}
291
292struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
293{
294 struct pblk_line_meta *lm = &pblk->lm;
295 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
296 struct list_head *move_list = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200297 int vsc = le32_to_cpu(*line->vsc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200299 if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 if (line->gc_group != PBLK_LINEGC_FULL) {
301 line->gc_group = PBLK_LINEGC_FULL;
302 move_list = &l_mg->gc_full_list;
303 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200304 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305 if (line->gc_group != PBLK_LINEGC_HIGH) {
306 line->gc_group = PBLK_LINEGC_HIGH;
307 move_list = &l_mg->gc_high_list;
308 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200309 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200310 if (line->gc_group != PBLK_LINEGC_MID) {
311 line->gc_group = PBLK_LINEGC_MID;
312 move_list = &l_mg->gc_mid_list;
313 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200314 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200315 if (line->gc_group != PBLK_LINEGC_LOW) {
316 line->gc_group = PBLK_LINEGC_LOW;
317 move_list = &l_mg->gc_low_list;
318 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200319 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320 if (line->gc_group != PBLK_LINEGC_EMPTY) {
321 line->gc_group = PBLK_LINEGC_EMPTY;
322 move_list = &l_mg->gc_empty_list;
323 }
324 } else {
325 line->state = PBLK_LINESTATE_CORRUPT;
326 line->gc_group = PBLK_LINEGC_NONE;
327 move_list = &l_mg->corrupt_list;
328 pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200329 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 line->sec_in_line,
331 lm->high_thrs, lm->mid_thrs);
332 }
333
334 return move_list;
335}
336
337void pblk_discard(struct pblk *pblk, struct bio *bio)
338{
339 sector_t slba = pblk_get_lba(bio);
340 sector_t nr_secs = pblk_get_secs(bio);
341
342 pblk_invalidate_range(pblk, slba, nr_secs);
343}
344
345struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba)
346{
347 struct ppa_addr ppa;
348
349 spin_lock(&pblk->trans_lock);
350 ppa = pblk_trans_map_get(pblk, lba);
351 spin_unlock(&pblk->trans_lock);
352
353 return ppa;
354}
355
356void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
357{
358 atomic_long_inc(&pblk->write_failed);
359#ifdef CONFIG_NVM_DEBUG
360 pblk_print_failed_rqd(pblk, rqd, rqd->error);
361#endif
362}
363
364void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
365{
366 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
367 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
368 atomic_long_inc(&pblk->read_empty);
369 return;
370 }
371
372 switch (rqd->error) {
373 case NVM_RSP_WARN_HIGHECC:
374 atomic_long_inc(&pblk->read_high_ecc);
375 break;
376 case NVM_RSP_ERR_FAILECC:
377 case NVM_RSP_ERR_FAILCRC:
378 atomic_long_inc(&pblk->read_failed);
379 break;
380 default:
381 pr_err("pblk: unknown read error:%d\n", rqd->error);
382 }
383#ifdef CONFIG_NVM_DEBUG
384 pblk_print_failed_rqd(pblk, rqd, rqd->error);
385#endif
386}
387
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200388void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
389{
390 pblk->sec_per_write = sec_per_write;
391}
392
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200393int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
394{
395 struct nvm_tgt_dev *dev = pblk->dev;
396
397#ifdef CONFIG_NVM_DEBUG
398 struct ppa_addr *ppa_list;
399
400 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
401 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
402 WARN_ON(1);
403 return -EINVAL;
404 }
405
406 if (rqd->opcode == NVM_OP_PWRITE) {
407 struct pblk_line *line;
408 struct ppa_addr ppa;
409 int i;
410
411 for (i = 0; i < rqd->nr_ppas; i++) {
412 ppa = ppa_list[i];
413 line = &pblk->lines[pblk_dev_ppa_to_line(ppa)];
414
415 spin_lock(&line->lock);
416 if (line->state != PBLK_LINESTATE_OPEN) {
417 pr_err("pblk: bad ppa: line:%d,state:%d\n",
418 line->id, line->state);
419 WARN_ON(1);
420 spin_unlock(&line->lock);
421 return -EINVAL;
422 }
423 spin_unlock(&line->lock);
424 }
425 }
426#endif
427 return nvm_submit_io(dev, rqd);
428}
429
430struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
431 unsigned int nr_secs, unsigned int len,
432 gfp_t gfp_mask)
433{
434 struct nvm_tgt_dev *dev = pblk->dev;
435 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
436 void *kaddr = data;
437 struct page *page;
438 struct bio *bio;
439 int i, ret;
440
441 if (l_mg->emeta_alloc_type == PBLK_KMALLOC_META)
442 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
443
444 bio = bio_kmalloc(gfp_mask, nr_secs);
445 if (!bio)
446 return ERR_PTR(-ENOMEM);
447
448 for (i = 0; i < nr_secs; i++) {
449 page = vmalloc_to_page(kaddr);
450 if (!page) {
451 pr_err("pblk: could not map vmalloc bio\n");
452 bio_put(bio);
453 bio = ERR_PTR(-ENOMEM);
454 goto out;
455 }
456
457 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
458 if (ret != PAGE_SIZE) {
459 pr_err("pblk: could not add page to bio\n");
460 bio_put(bio);
461 bio = ERR_PTR(-ENOMEM);
462 goto out;
463 }
464
465 kaddr += PAGE_SIZE;
466 }
467out:
468 return bio;
469}
470
471int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
472 unsigned long secs_to_flush)
473{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200474 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475 int min = pblk->min_write_pgs;
476 int secs_to_sync = 0;
477
478 if (secs_avail >= max)
479 secs_to_sync = max;
480 else if (secs_avail >= min)
481 secs_to_sync = min * (secs_avail / min);
482 else if (secs_to_flush)
483 secs_to_sync = min;
484
485 return secs_to_sync;
486}
487
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200488void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
489{
490 u64 addr;
491 int i;
492
493 addr = find_next_zero_bit(line->map_bitmap,
494 pblk->lm.sec_per_line, line->cur_sec);
495 line->cur_sec = addr - nr_secs;
496
497 for (i = 0; i < nr_secs; i++, line->cur_sec--)
498 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
499}
500
501u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502{
503 u64 addr;
504 int i;
505
506 /* logic error: ppa out-of-bounds. Prevent generating bad address */
507 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
508 WARN(1, "pblk: page allocation out of bounds\n");
509 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
510 }
511
512 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
513 pblk->lm.sec_per_line, line->cur_sec);
514 for (i = 0; i < nr_secs; i++, line->cur_sec++)
515 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
516
517 return addr;
518}
519
520u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
521{
522 u64 addr;
523
524 /* Lock needed in case a write fails and a recovery needs to remap
525 * failed write buffer entries
526 */
527 spin_lock(&line->lock);
528 addr = __pblk_alloc_page(pblk, line, nr_secs);
529 line->left_msecs -= nr_secs;
530 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
531 spin_unlock(&line->lock);
532
533 return addr;
534}
535
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200536u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
537{
538 u64 paddr;
539
540 spin_lock(&line->lock);
541 paddr = find_next_zero_bit(line->map_bitmap,
542 pblk->lm.sec_per_line, line->cur_sec);
543 spin_unlock(&line->lock);
544
545 return paddr;
546}
547
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548/*
549 * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when
550 * taking the per LUN semaphore.
551 */
552static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200553 void *emeta_buf, u64 paddr, int dir)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200554{
555 struct nvm_tgt_dev *dev = pblk->dev;
556 struct nvm_geo *geo = &dev->geo;
557 struct pblk_line_meta *lm = &pblk->lm;
558 struct bio *bio;
559 struct nvm_rq rqd;
560 struct ppa_addr *ppa_list;
561 dma_addr_t dma_ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200562 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200563 int left_ppas = lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564 int id = line->id;
565 int rq_ppas, rq_len;
566 int cmd_op, bio_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200567 int i, j;
568 int ret;
569 DECLARE_COMPLETION_ONSTACK(wait);
570
571 if (dir == WRITE) {
572 bio_op = REQ_OP_WRITE;
573 cmd_op = NVM_OP_PWRITE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200574 } else if (dir == READ) {
575 bio_op = REQ_OP_READ;
576 cmd_op = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577 } else
578 return -EINVAL;
579
580 ppa_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_ppa_list);
581 if (!ppa_list)
582 return -ENOMEM;
583
584next_rq:
585 memset(&rqd, 0, sizeof(struct nvm_rq));
586
587 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
588 rq_len = rq_ppas * geo->sec_size;
589
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200590 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591 if (IS_ERR(bio)) {
592 ret = PTR_ERR(bio);
593 goto free_rqd_dma;
594 }
595
596 bio->bi_iter.bi_sector = 0; /* internal bio */
597 bio_set_op_attrs(bio, bio_op, 0);
598
599 rqd.bio = bio;
600 rqd.opcode = cmd_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601 rqd.nr_ppas = rq_ppas;
602 rqd.ppa_list = ppa_list;
603 rqd.dma_ppa_list = dma_ppa_list;
604 rqd.end_io = pblk_end_io_sync;
605 rqd.private = &wait;
606
607 if (dir == WRITE) {
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200608 rqd.flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609 for (i = 0; i < rqd.nr_ppas; ) {
610 spin_lock(&line->lock);
611 paddr = __pblk_alloc_page(pblk, line, min);
612 spin_unlock(&line->lock);
613 for (j = 0; j < min; j++, i++, paddr++)
614 rqd.ppa_list[i] =
615 addr_to_gen_ppa(pblk, paddr, id);
616 }
617 } else {
618 for (i = 0; i < rqd.nr_ppas; ) {
619 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
620 int pos = pblk_dev_ppa_to_pos(geo, ppa);
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200621 int read_type = PBLK_READ_RANDOM;
622
623 if (pblk_io_aligned(pblk, rq_ppas))
624 read_type = PBLK_READ_SEQUENTIAL;
625 rqd.flags = pblk_set_read_mode(pblk, read_type);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626
627 while (test_bit(pos, line->blk_bitmap)) {
628 paddr += min;
629 if (pblk_boundary_paddr_checks(pblk, paddr)) {
630 pr_err("pblk: corrupt emeta line:%d\n",
631 line->id);
632 bio_put(bio);
633 ret = -EINTR;
634 goto free_rqd_dma;
635 }
636
637 ppa = addr_to_gen_ppa(pblk, paddr, id);
638 pos = pblk_dev_ppa_to_pos(geo, ppa);
639 }
640
641 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
642 pr_err("pblk: corrupt emeta line:%d\n",
643 line->id);
644 bio_put(bio);
645 ret = -EINTR;
646 goto free_rqd_dma;
647 }
648
649 for (j = 0; j < min; j++, i++, paddr++)
650 rqd.ppa_list[i] =
651 addr_to_gen_ppa(pblk, paddr, line->id);
652 }
653 }
654
655 ret = pblk_submit_io(pblk, &rqd);
656 if (ret) {
657 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
658 bio_put(bio);
659 goto free_rqd_dma;
660 }
661
662 if (!wait_for_completion_io_timeout(&wait,
663 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
664 pr_err("pblk: emeta I/O timed out\n");
665 }
666 reinit_completion(&wait);
667
668 bio_put(bio);
669
670 if (rqd.error) {
671 if (dir == WRITE)
672 pblk_log_write_err(pblk, &rqd);
673 else
674 pblk_log_read_err(pblk, &rqd);
675 }
676
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200677 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200678 left_ppas -= rq_ppas;
679 if (left_ppas)
680 goto next_rq;
681free_rqd_dma:
682 nvm_dev_dma_free(dev->parent, ppa_list, dma_ppa_list);
683 return ret;
684}
685
686u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
687{
688 struct nvm_tgt_dev *dev = pblk->dev;
689 struct nvm_geo *geo = &dev->geo;
690 struct pblk_line_meta *lm = &pblk->lm;
691 int bit;
692
693 /* This usually only happens on bad lines */
694 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
695 if (bit >= lm->blk_per_line)
696 return -1;
697
698 return bit * geo->sec_per_pl;
699}
700
701static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
702 u64 paddr, int dir)
703{
704 struct nvm_tgt_dev *dev = pblk->dev;
705 struct pblk_line_meta *lm = &pblk->lm;
706 struct bio *bio;
707 struct nvm_rq rqd;
708 __le64 *lba_list = NULL;
709 int i, ret;
710 int cmd_op, bio_op;
711 int flags;
712 DECLARE_COMPLETION_ONSTACK(wait);
713
714 if (dir == WRITE) {
715 bio_op = REQ_OP_WRITE;
716 cmd_op = NVM_OP_PWRITE;
717 flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200718 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200719 } else if (dir == READ) {
720 bio_op = REQ_OP_READ;
721 cmd_op = NVM_OP_PREAD;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200722 flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200723 } else
724 return -EINVAL;
725
726 memset(&rqd, 0, sizeof(struct nvm_rq));
727
728 rqd.ppa_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
729 &rqd.dma_ppa_list);
730 if (!rqd.ppa_list)
731 return -ENOMEM;
732
733 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
734 if (IS_ERR(bio)) {
735 ret = PTR_ERR(bio);
736 goto free_ppa_list;
737 }
738
739 bio->bi_iter.bi_sector = 0; /* internal bio */
740 bio_set_op_attrs(bio, bio_op, 0);
741
742 rqd.bio = bio;
743 rqd.opcode = cmd_op;
744 rqd.flags = flags;
745 rqd.nr_ppas = lm->smeta_sec;
746 rqd.end_io = pblk_end_io_sync;
747 rqd.private = &wait;
748
749 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
750 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
751 if (dir == WRITE)
752 lba_list[paddr] = cpu_to_le64(ADDR_EMPTY);
753 }
754
755 /*
756 * This I/O is sent by the write thread when a line is replace. Since
757 * the write thread is the only one sending write and erase commands,
758 * there is no need to take the LUN semaphore.
759 */
760 ret = pblk_submit_io(pblk, &rqd);
761 if (ret) {
762 pr_err("pblk: smeta I/O submission failed: %d\n", ret);
763 bio_put(bio);
764 goto free_ppa_list;
765 }
766
767 if (!wait_for_completion_io_timeout(&wait,
768 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
769 pr_err("pblk: smeta I/O timed out\n");
770 }
771
772 if (rqd.error) {
773 if (dir == WRITE)
774 pblk_log_write_err(pblk, &rqd);
775 else
776 pblk_log_read_err(pblk, &rqd);
777 }
778
779free_ppa_list:
780 nvm_dev_dma_free(dev->parent, rqd.ppa_list, rqd.dma_ppa_list);
781
782 return ret;
783}
784
785int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
786{
787 u64 bpaddr = pblk_line_smeta_start(pblk, line);
788
789 return pblk_line_submit_smeta_io(pblk, line, bpaddr, READ);
790}
791
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200792int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
793 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200795 return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
796 line->emeta_ssec, READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200797}
798
799static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
800 struct ppa_addr ppa)
801{
802 rqd->opcode = NVM_OP_ERASE;
803 rqd->ppa_addr = ppa;
804 rqd->nr_ppas = 1;
805 rqd->flags = pblk_set_progr_mode(pblk, ERASE);
806 rqd->bio = NULL;
807}
808
809static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
810{
811 struct nvm_rq rqd;
812 int ret;
813 DECLARE_COMPLETION_ONSTACK(wait);
814
815 memset(&rqd, 0, sizeof(struct nvm_rq));
816
817 pblk_setup_e_rq(pblk, &rqd, ppa);
818
819 rqd.end_io = pblk_end_io_sync;
820 rqd.private = &wait;
821
822 /* The write thread schedules erases so that it minimizes disturbances
823 * with writes. Thus, there is no need to take the LUN semaphore.
824 */
825 ret = pblk_submit_io(pblk, &rqd);
826 if (ret) {
827 struct nvm_tgt_dev *dev = pblk->dev;
828 struct nvm_geo *geo = &dev->geo;
829
830 pr_err("pblk: could not sync erase line:%d,blk:%d\n",
831 pblk_dev_ppa_to_line(ppa),
832 pblk_dev_ppa_to_pos(geo, ppa));
833
834 rqd.error = ret;
835 goto out;
836 }
837
838 if (!wait_for_completion_io_timeout(&wait,
839 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
840 pr_err("pblk: sync erase timed out\n");
841 }
842
843out:
844 rqd.private = pblk;
845 __pblk_end_io_erase(pblk, &rqd);
846
847 return 0;
848}
849
850int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
851{
852 struct pblk_line_meta *lm = &pblk->lm;
853 struct ppa_addr ppa;
854 int bit = -1;
855
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200856 /* Erase only good blocks, one at a time */
857 do {
858 spin_lock(&line->lock);
859 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
860 bit + 1);
861 if (bit >= lm->blk_per_line) {
862 spin_unlock(&line->lock);
863 break;
864 }
865
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866 ppa = pblk->luns[bit].bppa; /* set ch and lun */
867 ppa.g.blk = line->id;
868
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200869 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200870 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200871 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200872
873 if (pblk_blk_erase_sync(pblk, ppa)) {
874 pr_err("pblk: failed to erase line %d\n", line->id);
875 return -ENOMEM;
876 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200877 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200878
879 return 0;
880}
881
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200882static void pblk_line_setup_metadata(struct pblk_line *line,
883 struct pblk_line_mgmt *l_mg,
884 struct pblk_line_meta *lm)
885{
886 int meta_line;
887
888retry_meta:
889 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
890 if (meta_line == PBLK_DATA_LINES) {
891 spin_unlock(&l_mg->free_lock);
892 io_schedule();
893 spin_lock(&l_mg->free_lock);
894 goto retry_meta;
895 }
896
897 set_bit(meta_line, &l_mg->meta_bitmap);
898 line->meta_line = meta_line;
899
900 line->smeta = l_mg->sline_meta[meta_line];
901 line->emeta = l_mg->eline_meta[meta_line];
902
903 memset(line->smeta, 0, lm->smeta_len);
904 memset(line->emeta->buf, 0, lm->emeta_len[0]);
905
906 line->emeta->mem = 0;
907 atomic_set(&line->emeta->sync, 0);
908}
909
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200910/* For now lines are always assumed full lines. Thus, smeta former and current
911 * lun bitmaps are omitted.
912 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200913static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200914 struct pblk_line *cur)
915{
916 struct nvm_tgt_dev *dev = pblk->dev;
917 struct nvm_geo *geo = &dev->geo;
918 struct pblk_line_meta *lm = &pblk->lm;
919 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200920 struct pblk_emeta *emeta = line->emeta;
921 struct line_emeta *emeta_buf = emeta->buf;
922 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200923 int nr_blk_line;
924
925 /* After erasing the line, new bad blocks might appear and we risk
926 * having an invalid line
927 */
928 nr_blk_line = lm->blk_per_line -
929 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
930 if (nr_blk_line < lm->min_blk_line) {
931 spin_lock(&l_mg->free_lock);
932 spin_lock(&line->lock);
933 line->state = PBLK_LINESTATE_BAD;
934 spin_unlock(&line->lock);
935
936 list_add_tail(&line->list, &l_mg->bad_list);
937 spin_unlock(&l_mg->free_lock);
938
939 pr_debug("pblk: line %d is bad\n", line->id);
940
941 return 0;
942 }
943
944 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200945 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200946
947 /* Mark LUNs allocated in this line (all for now) */
948 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
949
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200950 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
951 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
952 smeta_buf->header.id = cpu_to_le32(line->id);
953 smeta_buf->header.type = cpu_to_le16(line->type);
954 smeta_buf->header.version = cpu_to_le16(1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200955
956 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200957 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
958 smeta_buf->window_wr_lun = cpu_to_le32(geo->nr_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200959
960 /* Fill metadata among lines */
961 if (cur) {
962 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200963 smeta_buf->prev_id = cpu_to_le32(cur->id);
964 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200965 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200966 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200967 }
968
969 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200970 smeta_buf->header.crc = cpu_to_le32(
971 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
972 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200973
974 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200975 memcpy(&emeta_buf->header, &smeta_buf->header,
976 sizeof(struct line_header));
977 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
978 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
979 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
980 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
981 emeta_buf->crc = cpu_to_le32(0);
982 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200983
984 return 1;
985}
986
987/* For now lines are always assumed full lines. Thus, smeta former and current
988 * lun bitmaps are omitted.
989 */
990static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
991 int init)
992{
993 struct nvm_tgt_dev *dev = pblk->dev;
994 struct nvm_geo *geo = &dev->geo;
995 struct pblk_line_meta *lm = &pblk->lm;
996 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
997 int nr_bb = 0;
998 u64 off;
999 int bit = -1;
1000
1001 line->sec_in_line = lm->sec_per_line;
1002
1003 /* Capture bad block information on line mapping bitmaps */
1004 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1005 bit + 1)) < lm->blk_per_line) {
1006 off = bit * geo->sec_per_pl;
1007 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1008 lm->sec_per_line);
1009 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1010 lm->sec_per_line);
1011 line->sec_in_line -= geo->sec_per_blk;
1012 if (bit >= lm->emeta_bb)
1013 nr_bb++;
1014 }
1015
1016 /* Mark smeta metadata sectors as bad sectors */
1017 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1018 off = bit * geo->sec_per_pl;
1019retry_smeta:
1020 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1021 line->sec_in_line -= lm->smeta_sec;
1022 line->smeta_ssec = off;
1023 line->cur_sec = off + lm->smeta_sec;
1024
1025 if (init && pblk_line_submit_smeta_io(pblk, line, off, WRITE)) {
1026 pr_debug("pblk: line smeta I/O failed. Retry\n");
1027 off += geo->sec_per_pl;
1028 goto retry_smeta;
1029 }
1030
1031 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1032
1033 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1034 * blocks to make sure that there are enough sectors to store emeta
1035 */
1036 bit = lm->sec_per_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001037 off = lm->sec_per_line - lm->emeta_sec[0];
1038 bitmap_set(line->invalid_bitmap, off, lm->emeta_sec[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001039 while (nr_bb) {
1040 off -= geo->sec_per_pl;
1041 if (!test_bit(off, line->invalid_bitmap)) {
1042 bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl);
1043 nr_bb--;
1044 }
1045 }
1046
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001047 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001048 line->emeta_ssec = off;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001049 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001050 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001051 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001052
1053 if (lm->sec_per_line - line->sec_in_line !=
1054 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1055 spin_lock(&line->lock);
1056 line->state = PBLK_LINESTATE_BAD;
1057 spin_unlock(&line->lock);
1058
1059 list_add_tail(&line->list, &l_mg->bad_list);
1060 pr_err("pblk: unexpected line %d is bad\n", line->id);
1061
1062 return 0;
1063 }
1064
1065 return 1;
1066}
1067
1068static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1069{
1070 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001071 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001072
1073 line->map_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1074 if (!line->map_bitmap)
1075 return -ENOMEM;
1076 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1077
1078 /* invalid_bitmap is special since it is used when line is closed. No
1079 * need to zeroized; it will be initialized using bb info form
1080 * map_bitmap
1081 */
1082 line->invalid_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1083 if (!line->invalid_bitmap) {
1084 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1085 return -ENOMEM;
1086 }
1087
1088 spin_lock(&line->lock);
1089 if (line->state != PBLK_LINESTATE_FREE) {
1090 spin_unlock(&line->lock);
1091 WARN(1, "pblk: corrupted line state\n");
1092 return -EINTR;
1093 }
1094 line->state = PBLK_LINESTATE_OPEN;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001095
1096 atomic_set(&line->left_eblks, blk_in_line);
1097 atomic_set(&line->left_seblks, blk_in_line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001098
1099 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001100 spin_unlock(&line->lock);
1101
1102 /* Bad blocks do not need to be erased */
1103 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001104
1105 kref_init(&line->ref);
1106
1107 return 0;
1108}
1109
1110int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1111{
1112 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1113 int ret;
1114
1115 spin_lock(&l_mg->free_lock);
1116 l_mg->data_line = line;
1117 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001118
1119 ret = pblk_line_prepare(pblk, line);
1120 if (ret) {
1121 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001122 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123 return ret;
1124 }
Javier González3dc001f2017-04-22 01:32:45 +02001125 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001126
1127 pblk_rl_free_lines_dec(&pblk->rl, line);
1128
1129 if (!pblk_line_init_bb(pblk, line, 0)) {
1130 list_add(&line->list, &l_mg->free_list);
1131 return -EINTR;
1132 }
1133
1134 return 0;
1135}
1136
1137void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1138{
1139 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1140 line->map_bitmap = NULL;
1141 line->smeta = NULL;
1142 line->emeta = NULL;
1143}
1144
1145struct pblk_line *pblk_line_get(struct pblk *pblk)
1146{
1147 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1148 struct pblk_line_meta *lm = &pblk->lm;
1149 struct pblk_line *line = NULL;
1150 int bit;
1151
1152 lockdep_assert_held(&l_mg->free_lock);
1153
1154retry_get:
1155 if (list_empty(&l_mg->free_list)) {
1156 pr_err("pblk: no free lines\n");
1157 goto out;
1158 }
1159
1160 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1161 list_del(&line->list);
1162 l_mg->nr_free_lines--;
1163
1164 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1165 if (unlikely(bit >= lm->blk_per_line)) {
1166 spin_lock(&line->lock);
1167 line->state = PBLK_LINESTATE_BAD;
1168 spin_unlock(&line->lock);
1169
1170 list_add_tail(&line->list, &l_mg->bad_list);
1171
1172 pr_debug("pblk: line %d is bad\n", line->id);
1173 goto retry_get;
1174 }
1175
1176 if (pblk_line_prepare(pblk, line)) {
1177 pr_err("pblk: failed to prepare line %d\n", line->id);
1178 list_add(&line->list, &l_mg->free_list);
1179 return NULL;
1180 }
1181
1182out:
1183 return line;
1184}
1185
1186static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1187 struct pblk_line *line)
1188{
1189 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1190 struct pblk_line *retry_line;
1191
1192 spin_lock(&l_mg->free_lock);
1193 retry_line = pblk_line_get(pblk);
1194 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001195 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001196 spin_unlock(&l_mg->free_lock);
1197 return NULL;
1198 }
1199
1200 retry_line->smeta = line->smeta;
1201 retry_line->emeta = line->emeta;
1202 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001203
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001204 pblk_line_free(pblk, line);
Javier González3dc001f2017-04-22 01:32:45 +02001205 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001206 spin_unlock(&l_mg->free_lock);
1207
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001208 if (pblk_line_erase(pblk, retry_line)) {
1209 spin_lock(&l_mg->free_lock);
1210 l_mg->data_line = NULL;
1211 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001212 return NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001213 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001214
1215 pblk_rl_free_lines_dec(&pblk->rl, retry_line);
1216
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001217 return retry_line;
1218}
1219
1220struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1221{
1222 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1223 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001224 int is_next = 0;
1225
1226 spin_lock(&l_mg->free_lock);
1227 line = pblk_line_get(pblk);
1228 if (!line) {
1229 spin_unlock(&l_mg->free_lock);
1230 return NULL;
1231 }
1232
1233 line->seq_nr = l_mg->d_seq_nr++;
1234 line->type = PBLK_LINETYPE_DATA;
1235 l_mg->data_line = line;
1236
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001237 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001238
1239 /* Allocate next line for preparation */
1240 l_mg->data_next = pblk_line_get(pblk);
1241 if (l_mg->data_next) {
1242 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1243 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1244 is_next = 1;
1245 }
1246 spin_unlock(&l_mg->free_lock);
1247
1248 pblk_rl_free_lines_dec(&pblk->rl, line);
1249 if (is_next)
1250 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
1251
1252 if (pblk_line_erase(pblk, line))
1253 return NULL;
1254
1255retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001256 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001257 line = pblk_line_retry(pblk, line);
1258 if (!line)
1259 return NULL;
1260
1261 goto retry_setup;
1262 }
1263
1264 if (!pblk_line_init_bb(pblk, line, 1)) {
1265 line = pblk_line_retry(pblk, line);
1266 if (!line)
1267 return NULL;
1268
1269 goto retry_setup;
1270 }
1271
1272 return line;
1273}
1274
1275struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
1276{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001277 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1278 struct pblk_line *cur, *new;
1279 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001280 int is_next = 0;
1281
1282 cur = l_mg->data_line;
1283 new = l_mg->data_next;
1284 if (!new)
1285 return NULL;
1286 l_mg->data_line = new;
1287
1288retry_line:
1289 left_seblks = atomic_read(&new->left_seblks);
1290 if (left_seblks) {
1291 /* If line is not fully erased, erase it */
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001292 if (atomic_read(&new->left_eblks)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001293 if (pblk_line_erase(pblk, new))
1294 return NULL;
1295 } else {
1296 io_schedule();
1297 }
1298 goto retry_line;
1299 }
1300
1301 spin_lock(&l_mg->free_lock);
1302 /* Allocate next line for preparation */
1303 l_mg->data_next = pblk_line_get(pblk);
1304 if (l_mg->data_next) {
1305 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1306 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1307 is_next = 1;
1308 }
1309
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001310 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001311 spin_unlock(&l_mg->free_lock);
1312
1313 if (is_next)
1314 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
1315
1316retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001317 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001318 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001319 if (!new)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001320 return NULL;
1321
1322 goto retry_setup;
1323 }
1324
1325 if (!pblk_line_init_bb(pblk, new, 1)) {
1326 new = pblk_line_retry(pblk, new);
1327 if (!new)
1328 return NULL;
1329
1330 goto retry_setup;
1331 }
1332
1333 return new;
1334}
1335
1336void pblk_line_free(struct pblk *pblk, struct pblk_line *line)
1337{
1338 if (line->map_bitmap)
1339 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1340 if (line->invalid_bitmap)
1341 mempool_free(line->invalid_bitmap, pblk->line_meta_pool);
1342
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001343 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1344
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001345 line->map_bitmap = NULL;
1346 line->invalid_bitmap = NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001347 line->smeta = NULL;
1348 line->emeta = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001349}
1350
1351void pblk_line_put(struct kref *ref)
1352{
1353 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1354 struct pblk *pblk = line->pblk;
1355 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1356
1357 spin_lock(&line->lock);
1358 WARN_ON(line->state != PBLK_LINESTATE_GC);
1359 line->state = PBLK_LINESTATE_FREE;
1360 line->gc_group = PBLK_LINEGC_NONE;
1361 pblk_line_free(pblk, line);
1362 spin_unlock(&line->lock);
1363
1364 spin_lock(&l_mg->free_lock);
1365 list_add_tail(&line->list, &l_mg->free_list);
1366 l_mg->nr_free_lines++;
1367 spin_unlock(&l_mg->free_lock);
1368
1369 pblk_rl_free_lines_inc(&pblk->rl, line);
1370}
1371
1372int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1373{
1374 struct nvm_rq *rqd;
1375 int err;
1376
Javier González084ec9b2017-06-26 16:27:13 -06001377 rqd = mempool_alloc(pblk->g_rq_pool, GFP_KERNEL);
1378 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001379
1380 pblk_setup_e_rq(pblk, rqd, ppa);
1381
1382 rqd->end_io = pblk_end_io_erase;
1383 rqd->private = pblk;
1384
1385 /* The write thread schedules erases so that it minimizes disturbances
1386 * with writes. Thus, there is no need to take the LUN semaphore.
1387 */
1388 err = pblk_submit_io(pblk, rqd);
1389 if (err) {
1390 struct nvm_tgt_dev *dev = pblk->dev;
1391 struct nvm_geo *geo = &dev->geo;
1392
1393 pr_err("pblk: could not async erase line:%d,blk:%d\n",
1394 pblk_dev_ppa_to_line(ppa),
1395 pblk_dev_ppa_to_pos(geo, ppa));
1396 }
1397
1398 return err;
1399}
1400
1401struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1402{
1403 return pblk->l_mg.data_line;
1404}
1405
Javier Gonzálezd624f372017-06-26 11:57:15 +02001406/* For now, always erase next line */
1407struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001408{
1409 return pblk->l_mg.data_next;
1410}
1411
1412int pblk_line_is_full(struct pblk_line *line)
1413{
1414 return (line->left_msecs == 0);
1415}
1416
1417void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1418{
1419 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001420 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001421 struct list_head *move_list;
1422
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001423 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001424 "pblk: corrupt closed line %d\n", line->id);
1425
1426 spin_lock(&l_mg->free_lock);
1427 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1428 spin_unlock(&l_mg->free_lock);
1429
1430 spin_lock(&l_mg->gc_lock);
1431 spin_lock(&line->lock);
1432 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1433 line->state = PBLK_LINESTATE_CLOSED;
1434 move_list = pblk_line_gc_list(pblk, line);
1435
1436 list_add_tail(&line->list, move_list);
1437
1438 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1439 line->map_bitmap = NULL;
1440 line->smeta = NULL;
1441 line->emeta = NULL;
1442
1443 spin_unlock(&line->lock);
1444 spin_unlock(&l_mg->gc_lock);
1445}
1446
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001447void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1448{
1449 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1450 struct pblk_line_meta *lm = &pblk->lm;
1451 struct pblk_emeta *emeta = line->emeta;
1452 struct line_emeta *emeta_buf = emeta->buf;
1453
1454 /* No need for exact vsc value; avoid a big line lock and tak aprox. */
1455 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1456 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1457
1458 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1459 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1460
1461 spin_lock(&l_mg->close_lock);
1462 spin_lock(&line->lock);
1463 list_add_tail(&line->list, &l_mg->emeta_list);
1464 spin_unlock(&line->lock);
1465 spin_unlock(&l_mg->close_lock);
1466}
1467
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001468void pblk_line_close_ws(struct work_struct *work)
1469{
1470 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1471 ws);
1472 struct pblk *pblk = line_ws->pblk;
1473 struct pblk_line *line = line_ws->line;
1474
1475 pblk_line_close(pblk, line);
1476 mempool_free(line_ws, pblk->line_ws_pool);
1477}
1478
1479void pblk_line_mark_bb(struct work_struct *work)
1480{
1481 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1482 ws);
1483 struct pblk *pblk = line_ws->pblk;
1484 struct nvm_tgt_dev *dev = pblk->dev;
1485 struct ppa_addr *ppa = line_ws->priv;
1486 int ret;
1487
1488 ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
1489 if (ret) {
1490 struct pblk_line *line;
1491 int pos;
1492
1493 line = &pblk->lines[pblk_dev_ppa_to_line(*ppa)];
1494 pos = pblk_dev_ppa_to_pos(&dev->geo, *ppa);
1495
1496 pr_err("pblk: failed to mark bb, line:%d, pos:%d\n",
1497 line->id, pos);
1498 }
1499
1500 kfree(ppa);
1501 mempool_free(line_ws, pblk->line_ws_pool);
1502}
1503
1504void pblk_line_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1505 void (*work)(struct work_struct *))
1506{
1507 struct pblk_line_ws *line_ws;
1508
1509 line_ws = mempool_alloc(pblk->line_ws_pool, GFP_ATOMIC);
1510 if (!line_ws)
1511 return;
1512
1513 line_ws->pblk = pblk;
1514 line_ws->line = line;
1515 line_ws->priv = priv;
1516
1517 INIT_WORK(&line_ws->ws, work);
1518 queue_work(pblk->kw_wq, &line_ws->ws);
1519}
1520
1521void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1522 unsigned long *lun_bitmap)
1523{
1524 struct nvm_tgt_dev *dev = pblk->dev;
1525 struct nvm_geo *geo = &dev->geo;
1526 struct pblk_lun *rlun;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001527 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001528 int ret;
1529
1530 /*
1531 * Only send one inflight I/O per LUN. Since we map at a page
1532 * granurality, all ppas in the I/O will map to the same LUN
1533 */
1534#ifdef CONFIG_NVM_DEBUG
1535 int i;
1536
1537 for (i = 1; i < nr_ppas; i++)
1538 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1539 ppa_list[0].g.ch != ppa_list[i].g.ch);
1540#endif
1541 /* If the LUN has been locked for this same request, do no attempt to
1542 * lock it again
1543 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001544 if (test_and_set_bit(pos, lun_bitmap))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001545 return;
1546
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001547 rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001548 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(5000));
1549 if (ret) {
1550 switch (ret) {
1551 case -ETIME:
1552 pr_err("pblk: lun semaphore timed out\n");
1553 break;
1554 case -EINTR:
1555 pr_err("pblk: lun semaphore timed out\n");
1556 break;
1557 }
1558 }
1559}
1560
1561void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1562 unsigned long *lun_bitmap)
1563{
1564 struct nvm_tgt_dev *dev = pblk->dev;
1565 struct nvm_geo *geo = &dev->geo;
1566 struct pblk_lun *rlun;
1567 int nr_luns = geo->nr_luns;
1568 int bit = -1;
1569
1570 while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) {
1571 rlun = &pblk->luns[bit];
1572 up(&rlun->wr_sem);
1573 }
1574
1575 kfree(lun_bitmap);
1576}
1577
1578void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1579{
1580 struct ppa_addr l2p_ppa;
1581
1582 /* logic error: lba out-of-bounds. Ignore update */
1583 if (!(lba < pblk->rl.nr_secs)) {
1584 WARN(1, "pblk: corrupted L2P map request\n");
1585 return;
1586 }
1587
1588 spin_lock(&pblk->trans_lock);
1589 l2p_ppa = pblk_trans_map_get(pblk, lba);
1590
1591 if (!pblk_addr_in_cache(l2p_ppa) && !pblk_ppa_empty(l2p_ppa))
1592 pblk_map_invalidate(pblk, l2p_ppa);
1593
1594 pblk_trans_map_set(pblk, lba, ppa);
1595 spin_unlock(&pblk->trans_lock);
1596}
1597
1598void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1599{
1600#ifdef CONFIG_NVM_DEBUG
1601 /* Callers must ensure that the ppa points to a cache address */
1602 BUG_ON(!pblk_addr_in_cache(ppa));
1603 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1604#endif
1605
1606 pblk_update_map(pblk, lba, ppa);
1607}
1608
1609int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1610 struct pblk_line *gc_line)
1611{
1612 struct ppa_addr l2p_ppa;
1613 int ret = 1;
1614
1615#ifdef CONFIG_NVM_DEBUG
1616 /* Callers must ensure that the ppa points to a cache address */
1617 BUG_ON(!pblk_addr_in_cache(ppa));
1618 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1619#endif
1620
1621 /* logic error: lba out-of-bounds. Ignore update */
1622 if (!(lba < pblk->rl.nr_secs)) {
1623 WARN(1, "pblk: corrupted L2P map request\n");
1624 return 0;
1625 }
1626
1627 spin_lock(&pblk->trans_lock);
1628 l2p_ppa = pblk_trans_map_get(pblk, lba);
1629
1630 /* Prevent updated entries to be overwritten by GC */
1631 if (pblk_addr_in_cache(l2p_ppa) || pblk_ppa_empty(l2p_ppa) ||
1632 pblk_tgt_ppa_to_line(l2p_ppa) != gc_line->id) {
1633 ret = 0;
1634 goto out;
1635 }
1636
1637 pblk_trans_map_set(pblk, lba, ppa);
1638out:
1639 spin_unlock(&pblk->trans_lock);
1640 return ret;
1641}
1642
1643void pblk_update_map_dev(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1644 struct ppa_addr entry_line)
1645{
1646 struct ppa_addr l2p_line;
1647
1648#ifdef CONFIG_NVM_DEBUG
1649 /* Callers must ensure that the ppa points to a device address */
1650 BUG_ON(pblk_addr_in_cache(ppa));
1651#endif
1652 /* Invalidate and discard padded entries */
1653 if (lba == ADDR_EMPTY) {
1654#ifdef CONFIG_NVM_DEBUG
1655 atomic_long_inc(&pblk->padded_wb);
1656#endif
1657 pblk_map_invalidate(pblk, ppa);
1658 return;
1659 }
1660
1661 /* logic error: lba out-of-bounds. Ignore update */
1662 if (!(lba < pblk->rl.nr_secs)) {
1663 WARN(1, "pblk: corrupted L2P map request\n");
1664 return;
1665 }
1666
1667 spin_lock(&pblk->trans_lock);
1668 l2p_line = pblk_trans_map_get(pblk, lba);
1669
1670 /* Do not update L2P if the cacheline has been updated. In this case,
1671 * the mapped ppa must be invalidated
1672 */
1673 if (l2p_line.ppa != entry_line.ppa) {
1674 if (!pblk_ppa_empty(ppa))
1675 pblk_map_invalidate(pblk, ppa);
1676 goto out;
1677 }
1678
1679#ifdef CONFIG_NVM_DEBUG
1680 WARN_ON(!pblk_addr_in_cache(l2p_line) && !pblk_ppa_empty(l2p_line));
1681#endif
1682
1683 pblk_trans_map_set(pblk, lba, ppa);
1684out:
1685 spin_unlock(&pblk->trans_lock);
1686}
1687
1688void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
1689 sector_t blba, int nr_secs)
1690{
1691 int i;
1692
1693 spin_lock(&pblk->trans_lock);
1694 for (i = 0; i < nr_secs; i++)
1695 ppas[i] = pblk_trans_map_get(pblk, blba + i);
1696 spin_unlock(&pblk->trans_lock);
1697}
1698
1699void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
1700 u64 *lba_list, int nr_secs)
1701{
1702 sector_t lba;
1703 int i;
1704
1705 spin_lock(&pblk->trans_lock);
1706 for (i = 0; i < nr_secs; i++) {
1707 lba = lba_list[i];
1708 if (lba == ADDR_EMPTY) {
1709 ppas[i].ppa = ADDR_EMPTY;
1710 } else {
1711 /* logic error: lba out-of-bounds. Ignore update */
1712 if (!(lba < pblk->rl.nr_secs)) {
1713 WARN(1, "pblk: corrupted L2P map request\n");
1714 continue;
1715 }
1716 ppas[i] = pblk_trans_map_get(pblk, lba);
1717 }
1718 }
1719 spin_unlock(&pblk->trans_lock);
1720}