pblk-map.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2016 CNEX Labs
  3. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  4. * Matias Bjorling <matias@cnexlabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * pblk-map.c - pblk's lba-ppa mapping strategy
  16. *
  17. */
  18. #include "pblk.h"
  19. static void pblk_map_page_data(struct pblk *pblk, unsigned int sentry,
  20. struct ppa_addr *ppa_list,
  21. unsigned long *lun_bitmap,
  22. struct pblk_sec_meta *meta_list,
  23. unsigned int valid_secs)
  24. {
  25. struct pblk_line *line = pblk_line_get_data(pblk);
  26. struct pblk_emeta *emeta;
  27. struct pblk_w_ctx *w_ctx;
  28. __le64 *lba_list;
  29. u64 paddr;
  30. int nr_secs = pblk->min_write_pgs;
  31. int i;
  32. if (pblk_line_is_full(line)) {
  33. struct pblk_line *prev_line = line;
  34. line = pblk_line_replace_data(pblk);
  35. pblk_line_close_meta(pblk, prev_line);
  36. }
  37. emeta = line->emeta;
  38. lba_list = emeta_to_lbas(pblk, emeta->buf);
  39. paddr = pblk_alloc_page(pblk, line, nr_secs);
  40. for (i = 0; i < nr_secs; i++, paddr++) {
  41. __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
  42. /* ppa to be sent to the device */
  43. ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
  44. /* Write context for target bio completion on write buffer. Note
  45. * that the write buffer is protected by the sync backpointer,
  46. * and a single writer thread have access to each specific entry
  47. * at a time. Thus, it is safe to modify the context for the
  48. * entry we are setting up for submission without taking any
  49. * lock or memory barrier.
  50. */
  51. if (i < valid_secs) {
  52. kref_get(&line->ref);
  53. w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i);
  54. w_ctx->ppa = ppa_list[i];
  55. meta_list[i].lba = cpu_to_le64(w_ctx->lba);
  56. lba_list[paddr] = cpu_to_le64(w_ctx->lba);
  57. if (lba_list[paddr] != addr_empty)
  58. line->nr_valid_lbas++;
  59. else
  60. atomic64_inc(&pblk->pad_wa);
  61. } else {
  62. lba_list[paddr] = meta_list[i].lba = addr_empty;
  63. __pblk_map_invalidate(pblk, line, paddr);
  64. }
  65. }
  66. pblk_down_rq(pblk, ppa_list, nr_secs, lun_bitmap);
  67. }
  68. void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
  69. unsigned long *lun_bitmap, unsigned int valid_secs,
  70. unsigned int off)
  71. {
  72. struct pblk_sec_meta *meta_list = rqd->meta_list;
  73. unsigned int map_secs;
  74. int min = pblk->min_write_pgs;
  75. int i;
  76. for (i = off; i < rqd->nr_ppas; i += min) {
  77. map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
  78. pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i],
  79. lun_bitmap, &meta_list[i], map_secs);
  80. }
  81. }
  82. /* only if erase_ppa is set, acquire erase semaphore */
  83. void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
  84. unsigned int sentry, unsigned long *lun_bitmap,
  85. unsigned int valid_secs, struct ppa_addr *erase_ppa)
  86. {
  87. struct nvm_tgt_dev *dev = pblk->dev;
  88. struct nvm_geo *geo = &dev->geo;
  89. struct pblk_line_meta *lm = &pblk->lm;
  90. struct pblk_sec_meta *meta_list = rqd->meta_list;
  91. struct pblk_line *e_line, *d_line;
  92. unsigned int map_secs;
  93. int min = pblk->min_write_pgs;
  94. int i, erase_lun;
  95. for (i = 0; i < rqd->nr_ppas; i += min) {
  96. map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
  97. pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i],
  98. lun_bitmap, &meta_list[i], map_secs);
  99. erase_lun = pblk_ppa_to_pos(geo, rqd->ppa_list[i]);
  100. /* line can change after page map. We might also be writing the
  101. * last line.
  102. */
  103. e_line = pblk_line_get_erase(pblk);
  104. if (!e_line)
  105. return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
  106. valid_secs, i + min);
  107. spin_lock(&e_line->lock);
  108. if (!test_bit(erase_lun, e_line->erase_bitmap)) {
  109. set_bit(erase_lun, e_line->erase_bitmap);
  110. atomic_dec(&e_line->left_eblks);
  111. *erase_ppa = rqd->ppa_list[i];
  112. erase_ppa->a.blk = e_line->id;
  113. spin_unlock(&e_line->lock);
  114. /* Avoid evaluating e_line->left_eblks */
  115. return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
  116. valid_secs, i + min);
  117. }
  118. spin_unlock(&e_line->lock);
  119. }
  120. d_line = pblk_line_get_data(pblk);
  121. /* line can change after page map. We might also be writing the
  122. * last line.
  123. */
  124. e_line = pblk_line_get_erase(pblk);
  125. if (!e_line)
  126. return;
  127. /* Erase blocks that are bad in this line but might not be in next */
  128. if (unlikely(pblk_ppa_empty(*erase_ppa)) &&
  129. bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) {
  130. int bit = -1;
  131. retry:
  132. bit = find_next_bit(d_line->blk_bitmap,
  133. lm->blk_per_line, bit + 1);
  134. if (bit >= lm->blk_per_line)
  135. return;
  136. spin_lock(&e_line->lock);
  137. if (test_bit(bit, e_line->erase_bitmap)) {
  138. spin_unlock(&e_line->lock);
  139. goto retry;
  140. }
  141. spin_unlock(&e_line->lock);
  142. set_bit(bit, e_line->erase_bitmap);
  143. atomic_dec(&e_line->left_eblks);
  144. *erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */
  145. erase_ppa->a.blk = e_line->id;
  146. }
  147. }