blob: cba7cd78e4a678fc6acd9be213722798be6b81ac [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;
Javier González63e38092017-06-26 11:57:24 +0200558 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200559 struct bio *bio;
560 struct nvm_rq rqd;
Javier González63e38092017-06-26 11:57:24 +0200561 dma_addr_t dma_ppa_list, dma_meta_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
Javier González63e38092017-06-26 11:57:24 +0200580 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
581 &dma_meta_list);
582 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200583 return -ENOMEM;
584
Javier González63e38092017-06-26 11:57:24 +0200585 ppa_list = meta_list + pblk_dma_meta_size;
586 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
587
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200588next_rq:
589 memset(&rqd, 0, sizeof(struct nvm_rq));
590
591 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
592 rq_len = rq_ppas * geo->sec_size;
593
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200594 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200595 if (IS_ERR(bio)) {
596 ret = PTR_ERR(bio);
597 goto free_rqd_dma;
598 }
599
600 bio->bi_iter.bi_sector = 0; /* internal bio */
601 bio_set_op_attrs(bio, bio_op, 0);
602
603 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200604 rqd.meta_list = meta_list;
605 rqd.ppa_list = ppa_list;
606 rqd.dma_meta_list = dma_meta_list;
607 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200608 rqd.opcode = cmd_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610 rqd.end_io = pblk_end_io_sync;
611 rqd.private = &wait;
612
613 if (dir == WRITE) {
Javier González63e38092017-06-26 11:57:24 +0200614 struct pblk_sec_meta *meta_list = rqd.meta_list;
615
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200616 rqd.flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617 for (i = 0; i < rqd.nr_ppas; ) {
618 spin_lock(&line->lock);
619 paddr = __pblk_alloc_page(pblk, line, min);
620 spin_unlock(&line->lock);
Javier González63e38092017-06-26 11:57:24 +0200621 for (j = 0; j < min; j++, i++, paddr++) {
622 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200623 rqd.ppa_list[i] =
624 addr_to_gen_ppa(pblk, paddr, id);
Javier González63e38092017-06-26 11:57:24 +0200625 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626 }
627 } else {
628 for (i = 0; i < rqd.nr_ppas; ) {
629 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
630 int pos = pblk_dev_ppa_to_pos(geo, ppa);
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200631 int read_type = PBLK_READ_RANDOM;
632
633 if (pblk_io_aligned(pblk, rq_ppas))
634 read_type = PBLK_READ_SEQUENTIAL;
635 rqd.flags = pblk_set_read_mode(pblk, read_type);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200636
637 while (test_bit(pos, line->blk_bitmap)) {
638 paddr += min;
639 if (pblk_boundary_paddr_checks(pblk, paddr)) {
640 pr_err("pblk: corrupt emeta line:%d\n",
641 line->id);
642 bio_put(bio);
643 ret = -EINTR;
644 goto free_rqd_dma;
645 }
646
647 ppa = addr_to_gen_ppa(pblk, paddr, id);
648 pos = pblk_dev_ppa_to_pos(geo, ppa);
649 }
650
651 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
652 pr_err("pblk: corrupt emeta line:%d\n",
653 line->id);
654 bio_put(bio);
655 ret = -EINTR;
656 goto free_rqd_dma;
657 }
658
659 for (j = 0; j < min; j++, i++, paddr++)
660 rqd.ppa_list[i] =
661 addr_to_gen_ppa(pblk, paddr, line->id);
662 }
663 }
664
665 ret = pblk_submit_io(pblk, &rqd);
666 if (ret) {
667 pr_err("pblk: emeta I/O submission failed: %d\n", ret);
668 bio_put(bio);
669 goto free_rqd_dma;
670 }
671
672 if (!wait_for_completion_io_timeout(&wait,
673 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
674 pr_err("pblk: emeta I/O timed out\n");
675 }
676 reinit_completion(&wait);
677
Javier Gonzálezf680f192017-06-26 11:57:21 +0200678 if (likely(pblk->l_mg.emeta_alloc_type == PBLK_VMALLOC_META))
679 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200680
681 if (rqd.error) {
682 if (dir == WRITE)
683 pblk_log_write_err(pblk, &rqd);
684 else
685 pblk_log_read_err(pblk, &rqd);
686 }
687
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200688 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200689 left_ppas -= rq_ppas;
690 if (left_ppas)
691 goto next_rq;
692free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200693 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200694 return ret;
695}
696
697u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
698{
699 struct nvm_tgt_dev *dev = pblk->dev;
700 struct nvm_geo *geo = &dev->geo;
701 struct pblk_line_meta *lm = &pblk->lm;
702 int bit;
703
704 /* This usually only happens on bad lines */
705 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
706 if (bit >= lm->blk_per_line)
707 return -1;
708
709 return bit * geo->sec_per_pl;
710}
711
712static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
713 u64 paddr, int dir)
714{
715 struct nvm_tgt_dev *dev = pblk->dev;
716 struct pblk_line_meta *lm = &pblk->lm;
717 struct bio *bio;
718 struct nvm_rq rqd;
719 __le64 *lba_list = NULL;
720 int i, ret;
721 int cmd_op, bio_op;
722 int flags;
723 DECLARE_COMPLETION_ONSTACK(wait);
724
725 if (dir == WRITE) {
726 bio_op = REQ_OP_WRITE;
727 cmd_op = NVM_OP_PWRITE;
728 flags = pblk_set_progr_mode(pblk, WRITE);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200729 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200730 } else if (dir == READ) {
731 bio_op = REQ_OP_READ;
732 cmd_op = NVM_OP_PREAD;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200733 flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200734 } else
735 return -EINVAL;
736
737 memset(&rqd, 0, sizeof(struct nvm_rq));
738
Javier González63e38092017-06-26 11:57:24 +0200739 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
740 &rqd.dma_meta_list);
741 if (!rqd.meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200742 return -ENOMEM;
743
Javier González63e38092017-06-26 11:57:24 +0200744 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
745 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
746
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200747 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
748 if (IS_ERR(bio)) {
749 ret = PTR_ERR(bio);
750 goto free_ppa_list;
751 }
752
753 bio->bi_iter.bi_sector = 0; /* internal bio */
754 bio_set_op_attrs(bio, bio_op, 0);
755
756 rqd.bio = bio;
757 rqd.opcode = cmd_op;
758 rqd.flags = flags;
759 rqd.nr_ppas = lm->smeta_sec;
760 rqd.end_io = pblk_end_io_sync;
761 rqd.private = &wait;
762
763 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
Javier González63e38092017-06-26 11:57:24 +0200764 struct pblk_sec_meta *meta_list = rqd.meta_list;
765
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200766 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
Javier González63e38092017-06-26 11:57:24 +0200767
768 if (dir == WRITE) {
769 u64 addr_empty = cpu_to_le64(ADDR_EMPTY);
770
771 meta_list[i].lba = lba_list[paddr] = addr_empty;
772 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200773 }
774
775 /*
776 * This I/O is sent by the write thread when a line is replace. Since
777 * the write thread is the only one sending write and erase commands,
778 * there is no need to take the LUN semaphore.
779 */
780 ret = pblk_submit_io(pblk, &rqd);
781 if (ret) {
782 pr_err("pblk: smeta I/O submission failed: %d\n", ret);
783 bio_put(bio);
784 goto free_ppa_list;
785 }
786
787 if (!wait_for_completion_io_timeout(&wait,
788 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
789 pr_err("pblk: smeta I/O timed out\n");
790 }
791
792 if (rqd.error) {
793 if (dir == WRITE)
794 pblk_log_write_err(pblk, &rqd);
795 else
796 pblk_log_read_err(pblk, &rqd);
797 }
798
799free_ppa_list:
Javier González63e38092017-06-26 11:57:24 +0200800 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200801
802 return ret;
803}
804
805int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
806{
807 u64 bpaddr = pblk_line_smeta_start(pblk, line);
808
809 return pblk_line_submit_smeta_io(pblk, line, bpaddr, READ);
810}
811
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200812int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
813 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200814{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200815 return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
816 line->emeta_ssec, READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200817}
818
819static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
820 struct ppa_addr ppa)
821{
822 rqd->opcode = NVM_OP_ERASE;
823 rqd->ppa_addr = ppa;
824 rqd->nr_ppas = 1;
825 rqd->flags = pblk_set_progr_mode(pblk, ERASE);
826 rqd->bio = NULL;
827}
828
829static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
830{
831 struct nvm_rq rqd;
832 int ret;
833 DECLARE_COMPLETION_ONSTACK(wait);
834
835 memset(&rqd, 0, sizeof(struct nvm_rq));
836
837 pblk_setup_e_rq(pblk, &rqd, ppa);
838
839 rqd.end_io = pblk_end_io_sync;
840 rqd.private = &wait;
841
842 /* The write thread schedules erases so that it minimizes disturbances
843 * with writes. Thus, there is no need to take the LUN semaphore.
844 */
845 ret = pblk_submit_io(pblk, &rqd);
846 if (ret) {
847 struct nvm_tgt_dev *dev = pblk->dev;
848 struct nvm_geo *geo = &dev->geo;
849
850 pr_err("pblk: could not sync erase line:%d,blk:%d\n",
851 pblk_dev_ppa_to_line(ppa),
852 pblk_dev_ppa_to_pos(geo, ppa));
853
854 rqd.error = ret;
855 goto out;
856 }
857
858 if (!wait_for_completion_io_timeout(&wait,
859 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
860 pr_err("pblk: sync erase timed out\n");
861 }
862
863out:
864 rqd.private = pblk;
865 __pblk_end_io_erase(pblk, &rqd);
866
867 return 0;
868}
869
870int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
871{
872 struct pblk_line_meta *lm = &pblk->lm;
873 struct ppa_addr ppa;
874 int bit = -1;
875
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200876 /* Erase only good blocks, one at a time */
877 do {
878 spin_lock(&line->lock);
879 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
880 bit + 1);
881 if (bit >= lm->blk_per_line) {
882 spin_unlock(&line->lock);
883 break;
884 }
885
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886 ppa = pblk->luns[bit].bppa; /* set ch and lun */
887 ppa.g.blk = line->id;
888
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200889 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200890 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200891 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892
893 if (pblk_blk_erase_sync(pblk, ppa)) {
894 pr_err("pblk: failed to erase line %d\n", line->id);
895 return -ENOMEM;
896 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200897 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898
899 return 0;
900}
901
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200902static void pblk_line_setup_metadata(struct pblk_line *line,
903 struct pblk_line_mgmt *l_mg,
904 struct pblk_line_meta *lm)
905{
906 int meta_line;
907
908retry_meta:
909 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
910 if (meta_line == PBLK_DATA_LINES) {
911 spin_unlock(&l_mg->free_lock);
912 io_schedule();
913 spin_lock(&l_mg->free_lock);
914 goto retry_meta;
915 }
916
917 set_bit(meta_line, &l_mg->meta_bitmap);
918 line->meta_line = meta_line;
919
920 line->smeta = l_mg->sline_meta[meta_line];
921 line->emeta = l_mg->eline_meta[meta_line];
922
923 memset(line->smeta, 0, lm->smeta_len);
924 memset(line->emeta->buf, 0, lm->emeta_len[0]);
925
926 line->emeta->mem = 0;
927 atomic_set(&line->emeta->sync, 0);
928}
929
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200930/* For now lines are always assumed full lines. Thus, smeta former and current
931 * lun bitmaps are omitted.
932 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200933static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200934 struct pblk_line *cur)
935{
936 struct nvm_tgt_dev *dev = pblk->dev;
937 struct nvm_geo *geo = &dev->geo;
938 struct pblk_line_meta *lm = &pblk->lm;
939 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200940 struct pblk_emeta *emeta = line->emeta;
941 struct line_emeta *emeta_buf = emeta->buf;
942 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 int nr_blk_line;
944
945 /* After erasing the line, new bad blocks might appear and we risk
946 * having an invalid line
947 */
948 nr_blk_line = lm->blk_per_line -
949 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
950 if (nr_blk_line < lm->min_blk_line) {
951 spin_lock(&l_mg->free_lock);
952 spin_lock(&line->lock);
953 line->state = PBLK_LINESTATE_BAD;
954 spin_unlock(&line->lock);
955
956 list_add_tail(&line->list, &l_mg->bad_list);
957 spin_unlock(&l_mg->free_lock);
958
959 pr_debug("pblk: line %d is bad\n", line->id);
960
961 return 0;
962 }
963
964 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200965 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200966
967 /* Mark LUNs allocated in this line (all for now) */
968 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
969
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200970 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
971 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
972 smeta_buf->header.id = cpu_to_le32(line->id);
973 smeta_buf->header.type = cpu_to_le16(line->type);
974 smeta_buf->header.version = cpu_to_le16(1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200975
976 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200977 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
978 smeta_buf->window_wr_lun = cpu_to_le32(geo->nr_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200979
980 /* Fill metadata among lines */
981 if (cur) {
982 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200983 smeta_buf->prev_id = cpu_to_le32(cur->id);
984 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200986 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200987 }
988
989 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200990 smeta_buf->header.crc = cpu_to_le32(
991 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
992 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200993
994 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200995 memcpy(&emeta_buf->header, &smeta_buf->header,
996 sizeof(struct line_header));
997 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
998 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
999 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1000 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1001 emeta_buf->crc = cpu_to_le32(0);
1002 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001003
1004 return 1;
1005}
1006
1007/* For now lines are always assumed full lines. Thus, smeta former and current
1008 * lun bitmaps are omitted.
1009 */
1010static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1011 int init)
1012{
1013 struct nvm_tgt_dev *dev = pblk->dev;
1014 struct nvm_geo *geo = &dev->geo;
1015 struct pblk_line_meta *lm = &pblk->lm;
1016 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1017 int nr_bb = 0;
1018 u64 off;
1019 int bit = -1;
1020
1021 line->sec_in_line = lm->sec_per_line;
1022
1023 /* Capture bad block information on line mapping bitmaps */
1024 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1025 bit + 1)) < lm->blk_per_line) {
1026 off = bit * geo->sec_per_pl;
1027 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1028 lm->sec_per_line);
1029 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1030 lm->sec_per_line);
1031 line->sec_in_line -= geo->sec_per_blk;
1032 if (bit >= lm->emeta_bb)
1033 nr_bb++;
1034 }
1035
1036 /* Mark smeta metadata sectors as bad sectors */
1037 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1038 off = bit * geo->sec_per_pl;
1039retry_smeta:
1040 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1041 line->sec_in_line -= lm->smeta_sec;
1042 line->smeta_ssec = off;
1043 line->cur_sec = off + lm->smeta_sec;
1044
1045 if (init && pblk_line_submit_smeta_io(pblk, line, off, WRITE)) {
1046 pr_debug("pblk: line smeta I/O failed. Retry\n");
1047 off += geo->sec_per_pl;
1048 goto retry_smeta;
1049 }
1050
1051 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1052
1053 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1054 * blocks to make sure that there are enough sectors to store emeta
1055 */
1056 bit = lm->sec_per_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001057 off = lm->sec_per_line - lm->emeta_sec[0];
1058 bitmap_set(line->invalid_bitmap, off, lm->emeta_sec[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001059 while (nr_bb) {
1060 off -= geo->sec_per_pl;
1061 if (!test_bit(off, line->invalid_bitmap)) {
1062 bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl);
1063 nr_bb--;
1064 }
1065 }
1066
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001067 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001068 line->emeta_ssec = off;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001069 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001070 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001071 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001072
1073 if (lm->sec_per_line - line->sec_in_line !=
1074 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1075 spin_lock(&line->lock);
1076 line->state = PBLK_LINESTATE_BAD;
1077 spin_unlock(&line->lock);
1078
1079 list_add_tail(&line->list, &l_mg->bad_list);
1080 pr_err("pblk: unexpected line %d is bad\n", line->id);
1081
1082 return 0;
1083 }
1084
1085 return 1;
1086}
1087
1088static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1089{
1090 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001091 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001092
1093 line->map_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1094 if (!line->map_bitmap)
1095 return -ENOMEM;
1096 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1097
1098 /* invalid_bitmap is special since it is used when line is closed. No
1099 * need to zeroized; it will be initialized using bb info form
1100 * map_bitmap
1101 */
1102 line->invalid_bitmap = mempool_alloc(pblk->line_meta_pool, GFP_ATOMIC);
1103 if (!line->invalid_bitmap) {
1104 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1105 return -ENOMEM;
1106 }
1107
1108 spin_lock(&line->lock);
1109 if (line->state != PBLK_LINESTATE_FREE) {
1110 spin_unlock(&line->lock);
1111 WARN(1, "pblk: corrupted line state\n");
1112 return -EINTR;
1113 }
1114 line->state = PBLK_LINESTATE_OPEN;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001115
1116 atomic_set(&line->left_eblks, blk_in_line);
1117 atomic_set(&line->left_seblks, blk_in_line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001118
1119 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001120 spin_unlock(&line->lock);
1121
1122 /* Bad blocks do not need to be erased */
1123 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001124
1125 kref_init(&line->ref);
1126
1127 return 0;
1128}
1129
1130int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1131{
1132 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1133 int ret;
1134
1135 spin_lock(&l_mg->free_lock);
1136 l_mg->data_line = line;
1137 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001138
1139 ret = pblk_line_prepare(pblk, line);
1140 if (ret) {
1141 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001142 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143 return ret;
1144 }
Javier González3dc001f2017-04-22 01:32:45 +02001145 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001146
1147 pblk_rl_free_lines_dec(&pblk->rl, line);
1148
1149 if (!pblk_line_init_bb(pblk, line, 0)) {
1150 list_add(&line->list, &l_mg->free_list);
1151 return -EINTR;
1152 }
1153
1154 return 0;
1155}
1156
1157void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1158{
1159 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1160 line->map_bitmap = NULL;
1161 line->smeta = NULL;
1162 line->emeta = NULL;
1163}
1164
1165struct pblk_line *pblk_line_get(struct pblk *pblk)
1166{
1167 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1168 struct pblk_line_meta *lm = &pblk->lm;
1169 struct pblk_line *line = NULL;
1170 int bit;
1171
1172 lockdep_assert_held(&l_mg->free_lock);
1173
1174retry_get:
1175 if (list_empty(&l_mg->free_list)) {
1176 pr_err("pblk: no free lines\n");
1177 goto out;
1178 }
1179
1180 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1181 list_del(&line->list);
1182 l_mg->nr_free_lines--;
1183
1184 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1185 if (unlikely(bit >= lm->blk_per_line)) {
1186 spin_lock(&line->lock);
1187 line->state = PBLK_LINESTATE_BAD;
1188 spin_unlock(&line->lock);
1189
1190 list_add_tail(&line->list, &l_mg->bad_list);
1191
1192 pr_debug("pblk: line %d is bad\n", line->id);
1193 goto retry_get;
1194 }
1195
1196 if (pblk_line_prepare(pblk, line)) {
1197 pr_err("pblk: failed to prepare line %d\n", line->id);
1198 list_add(&line->list, &l_mg->free_list);
1199 return NULL;
1200 }
1201
1202out:
1203 return line;
1204}
1205
1206static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1207 struct pblk_line *line)
1208{
1209 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1210 struct pblk_line *retry_line;
1211
1212 spin_lock(&l_mg->free_lock);
1213 retry_line = pblk_line_get(pblk);
1214 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001215 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001216 spin_unlock(&l_mg->free_lock);
1217 return NULL;
1218 }
1219
1220 retry_line->smeta = line->smeta;
1221 retry_line->emeta = line->emeta;
1222 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001223
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001224 pblk_line_free(pblk, line);
Javier González3dc001f2017-04-22 01:32:45 +02001225 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001226 spin_unlock(&l_mg->free_lock);
1227
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001228 if (pblk_line_erase(pblk, retry_line)) {
1229 spin_lock(&l_mg->free_lock);
1230 l_mg->data_line = NULL;
1231 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001232 return NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001233 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234
1235 pblk_rl_free_lines_dec(&pblk->rl, retry_line);
1236
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237 return retry_line;
1238}
1239
1240struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1241{
1242 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1243 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244 int is_next = 0;
1245
1246 spin_lock(&l_mg->free_lock);
1247 line = pblk_line_get(pblk);
1248 if (!line) {
1249 spin_unlock(&l_mg->free_lock);
1250 return NULL;
1251 }
1252
1253 line->seq_nr = l_mg->d_seq_nr++;
1254 line->type = PBLK_LINETYPE_DATA;
1255 l_mg->data_line = line;
1256
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001257 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001258
1259 /* Allocate next line for preparation */
1260 l_mg->data_next = pblk_line_get(pblk);
1261 if (l_mg->data_next) {
1262 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1263 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1264 is_next = 1;
1265 }
1266 spin_unlock(&l_mg->free_lock);
1267
1268 pblk_rl_free_lines_dec(&pblk->rl, line);
1269 if (is_next)
1270 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
1271
1272 if (pblk_line_erase(pblk, line))
1273 return NULL;
1274
1275retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001276 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001277 line = pblk_line_retry(pblk, line);
1278 if (!line)
1279 return NULL;
1280
1281 goto retry_setup;
1282 }
1283
1284 if (!pblk_line_init_bb(pblk, line, 1)) {
1285 line = pblk_line_retry(pblk, line);
1286 if (!line)
1287 return NULL;
1288
1289 goto retry_setup;
1290 }
1291
1292 return line;
1293}
1294
1295struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
1296{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001297 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1298 struct pblk_line *cur, *new;
1299 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001300 int is_next = 0;
1301
1302 cur = l_mg->data_line;
1303 new = l_mg->data_next;
1304 if (!new)
1305 return NULL;
1306 l_mg->data_line = new;
1307
1308retry_line:
1309 left_seblks = atomic_read(&new->left_seblks);
1310 if (left_seblks) {
1311 /* If line is not fully erased, erase it */
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001312 if (atomic_read(&new->left_eblks)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001313 if (pblk_line_erase(pblk, new))
1314 return NULL;
1315 } else {
1316 io_schedule();
1317 }
1318 goto retry_line;
1319 }
1320
1321 spin_lock(&l_mg->free_lock);
1322 /* Allocate next line for preparation */
1323 l_mg->data_next = pblk_line_get(pblk);
1324 if (l_mg->data_next) {
1325 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1326 l_mg->data_next->type = PBLK_LINETYPE_DATA;
1327 is_next = 1;
1328 }
1329
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001330 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001331 spin_unlock(&l_mg->free_lock);
1332
1333 if (is_next)
1334 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
1335
1336retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001337 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001338 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001339 if (!new)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001340 return NULL;
1341
1342 goto retry_setup;
1343 }
1344
1345 if (!pblk_line_init_bb(pblk, new, 1)) {
1346 new = pblk_line_retry(pblk, new);
1347 if (!new)
1348 return NULL;
1349
1350 goto retry_setup;
1351 }
1352
1353 return new;
1354}
1355
1356void pblk_line_free(struct pblk *pblk, struct pblk_line *line)
1357{
1358 if (line->map_bitmap)
1359 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1360 if (line->invalid_bitmap)
1361 mempool_free(line->invalid_bitmap, pblk->line_meta_pool);
1362
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001363 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1364
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001365 line->map_bitmap = NULL;
1366 line->invalid_bitmap = NULL;
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001367 line->smeta = NULL;
1368 line->emeta = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001369}
1370
1371void pblk_line_put(struct kref *ref)
1372{
1373 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1374 struct pblk *pblk = line->pblk;
1375 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1376
1377 spin_lock(&line->lock);
1378 WARN_ON(line->state != PBLK_LINESTATE_GC);
1379 line->state = PBLK_LINESTATE_FREE;
1380 line->gc_group = PBLK_LINEGC_NONE;
1381 pblk_line_free(pblk, line);
1382 spin_unlock(&line->lock);
1383
1384 spin_lock(&l_mg->free_lock);
1385 list_add_tail(&line->list, &l_mg->free_list);
1386 l_mg->nr_free_lines++;
1387 spin_unlock(&l_mg->free_lock);
1388
1389 pblk_rl_free_lines_inc(&pblk->rl, line);
1390}
1391
1392int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1393{
1394 struct nvm_rq *rqd;
1395 int err;
1396
Javier González084ec9b2017-06-26 16:27:13 -06001397 rqd = mempool_alloc(pblk->g_rq_pool, GFP_KERNEL);
1398 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001399
1400 pblk_setup_e_rq(pblk, rqd, ppa);
1401
1402 rqd->end_io = pblk_end_io_erase;
1403 rqd->private = pblk;
1404
1405 /* The write thread schedules erases so that it minimizes disturbances
1406 * with writes. Thus, there is no need to take the LUN semaphore.
1407 */
1408 err = pblk_submit_io(pblk, rqd);
1409 if (err) {
1410 struct nvm_tgt_dev *dev = pblk->dev;
1411 struct nvm_geo *geo = &dev->geo;
1412
1413 pr_err("pblk: could not async erase line:%d,blk:%d\n",
1414 pblk_dev_ppa_to_line(ppa),
1415 pblk_dev_ppa_to_pos(geo, ppa));
1416 }
1417
1418 return err;
1419}
1420
1421struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1422{
1423 return pblk->l_mg.data_line;
1424}
1425
Javier Gonzálezd624f372017-06-26 11:57:15 +02001426/* For now, always erase next line */
1427struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001428{
1429 return pblk->l_mg.data_next;
1430}
1431
1432int pblk_line_is_full(struct pblk_line *line)
1433{
1434 return (line->left_msecs == 0);
1435}
1436
1437void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1438{
1439 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001440 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001441 struct list_head *move_list;
1442
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001443 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001444 "pblk: corrupt closed line %d\n", line->id);
1445
1446 spin_lock(&l_mg->free_lock);
1447 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1448 spin_unlock(&l_mg->free_lock);
1449
1450 spin_lock(&l_mg->gc_lock);
1451 spin_lock(&line->lock);
1452 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1453 line->state = PBLK_LINESTATE_CLOSED;
1454 move_list = pblk_line_gc_list(pblk, line);
1455
1456 list_add_tail(&line->list, move_list);
1457
1458 mempool_free(line->map_bitmap, pblk->line_meta_pool);
1459 line->map_bitmap = NULL;
1460 line->smeta = NULL;
1461 line->emeta = NULL;
1462
1463 spin_unlock(&line->lock);
1464 spin_unlock(&l_mg->gc_lock);
1465}
1466
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001467void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1468{
1469 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1470 struct pblk_line_meta *lm = &pblk->lm;
1471 struct pblk_emeta *emeta = line->emeta;
1472 struct line_emeta *emeta_buf = emeta->buf;
1473
1474 /* No need for exact vsc value; avoid a big line lock and tak aprox. */
1475 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1476 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1477
1478 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1479 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1480
1481 spin_lock(&l_mg->close_lock);
1482 spin_lock(&line->lock);
1483 list_add_tail(&line->list, &l_mg->emeta_list);
1484 spin_unlock(&line->lock);
1485 spin_unlock(&l_mg->close_lock);
1486}
1487
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001488void pblk_line_close_ws(struct work_struct *work)
1489{
1490 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1491 ws);
1492 struct pblk *pblk = line_ws->pblk;
1493 struct pblk_line *line = line_ws->line;
1494
1495 pblk_line_close(pblk, line);
1496 mempool_free(line_ws, pblk->line_ws_pool);
1497}
1498
1499void pblk_line_mark_bb(struct work_struct *work)
1500{
1501 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1502 ws);
1503 struct pblk *pblk = line_ws->pblk;
1504 struct nvm_tgt_dev *dev = pblk->dev;
1505 struct ppa_addr *ppa = line_ws->priv;
1506 int ret;
1507
1508 ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
1509 if (ret) {
1510 struct pblk_line *line;
1511 int pos;
1512
1513 line = &pblk->lines[pblk_dev_ppa_to_line(*ppa)];
1514 pos = pblk_dev_ppa_to_pos(&dev->geo, *ppa);
1515
1516 pr_err("pblk: failed to mark bb, line:%d, pos:%d\n",
1517 line->id, pos);
1518 }
1519
1520 kfree(ppa);
1521 mempool_free(line_ws, pblk->line_ws_pool);
1522}
1523
1524void pblk_line_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1525 void (*work)(struct work_struct *))
1526{
1527 struct pblk_line_ws *line_ws;
1528
1529 line_ws = mempool_alloc(pblk->line_ws_pool, GFP_ATOMIC);
1530 if (!line_ws)
1531 return;
1532
1533 line_ws->pblk = pblk;
1534 line_ws->line = line;
1535 line_ws->priv = priv;
1536
1537 INIT_WORK(&line_ws->ws, work);
1538 queue_work(pblk->kw_wq, &line_ws->ws);
1539}
1540
1541void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1542 unsigned long *lun_bitmap)
1543{
1544 struct nvm_tgt_dev *dev = pblk->dev;
1545 struct nvm_geo *geo = &dev->geo;
1546 struct pblk_lun *rlun;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001547 int pos = pblk_ppa_to_pos(geo, ppa_list[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001548 int ret;
1549
1550 /*
1551 * Only send one inflight I/O per LUN. Since we map at a page
1552 * granurality, all ppas in the I/O will map to the same LUN
1553 */
1554#ifdef CONFIG_NVM_DEBUG
1555 int i;
1556
1557 for (i = 1; i < nr_ppas; i++)
1558 WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun ||
1559 ppa_list[0].g.ch != ppa_list[i].g.ch);
1560#endif
1561 /* If the LUN has been locked for this same request, do no attempt to
1562 * lock it again
1563 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001564 if (test_and_set_bit(pos, lun_bitmap))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001565 return;
1566
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001567 rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001568 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(5000));
1569 if (ret) {
1570 switch (ret) {
1571 case -ETIME:
1572 pr_err("pblk: lun semaphore timed out\n");
1573 break;
1574 case -EINTR:
1575 pr_err("pblk: lun semaphore timed out\n");
1576 break;
1577 }
1578 }
1579}
1580
1581void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
1582 unsigned long *lun_bitmap)
1583{
1584 struct nvm_tgt_dev *dev = pblk->dev;
1585 struct nvm_geo *geo = &dev->geo;
1586 struct pblk_lun *rlun;
1587 int nr_luns = geo->nr_luns;
1588 int bit = -1;
1589
1590 while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) {
1591 rlun = &pblk->luns[bit];
1592 up(&rlun->wr_sem);
1593 }
1594
1595 kfree(lun_bitmap);
1596}
1597
1598void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1599{
1600 struct ppa_addr l2p_ppa;
1601
1602 /* logic error: lba out-of-bounds. Ignore update */
1603 if (!(lba < pblk->rl.nr_secs)) {
1604 WARN(1, "pblk: corrupted L2P map request\n");
1605 return;
1606 }
1607
1608 spin_lock(&pblk->trans_lock);
1609 l2p_ppa = pblk_trans_map_get(pblk, lba);
1610
1611 if (!pblk_addr_in_cache(l2p_ppa) && !pblk_ppa_empty(l2p_ppa))
1612 pblk_map_invalidate(pblk, l2p_ppa);
1613
1614 pblk_trans_map_set(pblk, lba, ppa);
1615 spin_unlock(&pblk->trans_lock);
1616}
1617
1618void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
1619{
1620#ifdef CONFIG_NVM_DEBUG
1621 /* Callers must ensure that the ppa points to a cache address */
1622 BUG_ON(!pblk_addr_in_cache(ppa));
1623 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1624#endif
1625
1626 pblk_update_map(pblk, lba, ppa);
1627}
1628
1629int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1630 struct pblk_line *gc_line)
1631{
1632 struct ppa_addr l2p_ppa;
1633 int ret = 1;
1634
1635#ifdef CONFIG_NVM_DEBUG
1636 /* Callers must ensure that the ppa points to a cache address */
1637 BUG_ON(!pblk_addr_in_cache(ppa));
1638 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
1639#endif
1640
1641 /* logic error: lba out-of-bounds. Ignore update */
1642 if (!(lba < pblk->rl.nr_secs)) {
1643 WARN(1, "pblk: corrupted L2P map request\n");
1644 return 0;
1645 }
1646
1647 spin_lock(&pblk->trans_lock);
1648 l2p_ppa = pblk_trans_map_get(pblk, lba);
1649
1650 /* Prevent updated entries to be overwritten by GC */
1651 if (pblk_addr_in_cache(l2p_ppa) || pblk_ppa_empty(l2p_ppa) ||
1652 pblk_tgt_ppa_to_line(l2p_ppa) != gc_line->id) {
1653 ret = 0;
1654 goto out;
1655 }
1656
1657 pblk_trans_map_set(pblk, lba, ppa);
1658out:
1659 spin_unlock(&pblk->trans_lock);
1660 return ret;
1661}
1662
1663void pblk_update_map_dev(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
1664 struct ppa_addr entry_line)
1665{
1666 struct ppa_addr l2p_line;
1667
1668#ifdef CONFIG_NVM_DEBUG
1669 /* Callers must ensure that the ppa points to a device address */
1670 BUG_ON(pblk_addr_in_cache(ppa));
1671#endif
1672 /* Invalidate and discard padded entries */
1673 if (lba == ADDR_EMPTY) {
1674#ifdef CONFIG_NVM_DEBUG
1675 atomic_long_inc(&pblk->padded_wb);
1676#endif
1677 pblk_map_invalidate(pblk, ppa);
1678 return;
1679 }
1680
1681 /* logic error: lba out-of-bounds. Ignore update */
1682 if (!(lba < pblk->rl.nr_secs)) {
1683 WARN(1, "pblk: corrupted L2P map request\n");
1684 return;
1685 }
1686
1687 spin_lock(&pblk->trans_lock);
1688 l2p_line = pblk_trans_map_get(pblk, lba);
1689
1690 /* Do not update L2P if the cacheline has been updated. In this case,
1691 * the mapped ppa must be invalidated
1692 */
1693 if (l2p_line.ppa != entry_line.ppa) {
1694 if (!pblk_ppa_empty(ppa))
1695 pblk_map_invalidate(pblk, ppa);
1696 goto out;
1697 }
1698
1699#ifdef CONFIG_NVM_DEBUG
1700 WARN_ON(!pblk_addr_in_cache(l2p_line) && !pblk_ppa_empty(l2p_line));
1701#endif
1702
1703 pblk_trans_map_set(pblk, lba, ppa);
1704out:
1705 spin_unlock(&pblk->trans_lock);
1706}
1707
1708void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
1709 sector_t blba, int nr_secs)
1710{
1711 int i;
1712
1713 spin_lock(&pblk->trans_lock);
1714 for (i = 0; i < nr_secs; i++)
1715 ppas[i] = pblk_trans_map_get(pblk, blba + i);
1716 spin_unlock(&pblk->trans_lock);
1717}
1718
1719void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
1720 u64 *lba_list, int nr_secs)
1721{
1722 sector_t lba;
1723 int i;
1724
1725 spin_lock(&pblk->trans_lock);
1726 for (i = 0; i < nr_secs; i++) {
1727 lba = lba_list[i];
1728 if (lba == ADDR_EMPTY) {
1729 ppas[i].ppa = ADDR_EMPTY;
1730 } else {
1731 /* logic error: lba out-of-bounds. Ignore update */
1732 if (!(lba < pblk->rl.nr_secs)) {
1733 WARN(1, "pblk: corrupted L2P map request\n");
1734 continue;
1735 }
1736 ppas[i] = pblk_trans_map_get(pblk, lba);
1737 }
1738 }
1739 spin_unlock(&pblk->trans_lock);
1740}