2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * Implementation of a physical block-device target for Open-channel SSDs.
18 * pblk-init.c - pblk's initialization.
23 static struct kmem_cache *pblk_blk_ws_cache, *pblk_rec_cache, *pblk_r_rq_cache,
24 *pblk_w_rq_cache, *pblk_line_meta_cache;
25 static DECLARE_RWSEM(pblk_lock);
26 struct bio_set *pblk_bio_set;
28 static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
33 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
34 * constraint. Writes can be of arbitrary size.
36 if (bio_data_dir(bio) == READ) {
37 blk_queue_split(q, &bio);
38 ret = pblk_submit_read(pblk, bio);
39 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
45 /* Prevent deadlock in the case of a modest LUN configuration and large
46 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
47 * available for user I/O.
49 if (unlikely(pblk_get_secs(bio) >= pblk_rl_sysfs_rate_show(&pblk->rl)))
50 blk_queue_split(q, &bio);
52 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
55 static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
57 struct pblk *pblk = q->queuedata;
59 if (bio_op(bio) == REQ_OP_DISCARD) {
60 pblk_discard(pblk, bio);
61 if (!(bio->bi_opf & REQ_PREFLUSH)) {
67 switch (pblk_rw_io(q, pblk, bio)) {
79 static void pblk_l2p_free(struct pblk *pblk)
81 vfree(pblk->trans_map);
84 static int pblk_l2p_init(struct pblk *pblk)
90 if (pblk->ppaf_bitsize < 32)
93 pblk->trans_map = vmalloc(entry_size * pblk->rl.nr_secs);
97 pblk_ppa_set_empty(&ppa);
99 for (i = 0; i < pblk->rl.nr_secs; i++)
100 pblk_trans_map_set(pblk, i, ppa);
105 static void pblk_rwb_free(struct pblk *pblk)
107 if (pblk_rb_tear_down_check(&pblk->rwb))
108 pr_err("pblk: write buffer error on tear down\n");
110 pblk_rb_data_free(&pblk->rwb);
111 vfree(pblk_rb_entries_ref(&pblk->rwb));
114 static int pblk_rwb_init(struct pblk *pblk)
116 struct nvm_tgt_dev *dev = pblk->dev;
117 struct nvm_geo *geo = &dev->geo;
118 struct pblk_rb_entry *entries;
119 unsigned long nr_entries;
120 unsigned int power_size, power_seg_sz;
122 nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
124 entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
128 power_size = get_count_order(nr_entries);
129 power_seg_sz = get_count_order(geo->sec_size);
131 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
134 /* Minimum pages needed within a lun */
135 #define PAGE_POOL_SIZE 16
136 #define ADDR_POOL_SIZE 64
138 static int pblk_set_ppaf(struct pblk *pblk)
140 struct nvm_tgt_dev *dev = pblk->dev;
141 struct nvm_geo *geo = &dev->geo;
142 struct nvm_addr_format ppaf = geo->ppaf;
145 /* Re-calculate channel and lun format to adapt to configuration */
146 power_len = get_count_order(geo->nr_chnls);
147 if (1 << power_len != geo->nr_chnls) {
148 pr_err("pblk: supports only power-of-two channel config.\n");
151 ppaf.ch_len = power_len;
153 power_len = get_count_order(geo->luns_per_chnl);
154 if (1 << power_len != geo->luns_per_chnl) {
155 pr_err("pblk: supports only power-of-two LUN config.\n");
158 ppaf.lun_len = power_len;
160 pblk->ppaf.sec_offset = 0;
161 pblk->ppaf.pln_offset = ppaf.sect_len;
162 pblk->ppaf.ch_offset = pblk->ppaf.pln_offset + ppaf.pln_len;
163 pblk->ppaf.lun_offset = pblk->ppaf.ch_offset + ppaf.ch_len;
164 pblk->ppaf.pg_offset = pblk->ppaf.lun_offset + ppaf.lun_len;
165 pblk->ppaf.blk_offset = pblk->ppaf.pg_offset + ppaf.pg_len;
166 pblk->ppaf.sec_mask = (1ULL << ppaf.sect_len) - 1;
167 pblk->ppaf.pln_mask = ((1ULL << ppaf.pln_len) - 1) <<
168 pblk->ppaf.pln_offset;
169 pblk->ppaf.ch_mask = ((1ULL << ppaf.ch_len) - 1) <<
170 pblk->ppaf.ch_offset;
171 pblk->ppaf.lun_mask = ((1ULL << ppaf.lun_len) - 1) <<
172 pblk->ppaf.lun_offset;
173 pblk->ppaf.pg_mask = ((1ULL << ppaf.pg_len) - 1) <<
174 pblk->ppaf.pg_offset;
175 pblk->ppaf.blk_mask = ((1ULL << ppaf.blk_len) - 1) <<
176 pblk->ppaf.blk_offset;
178 pblk->ppaf_bitsize = pblk->ppaf.blk_offset + ppaf.blk_len;
183 static int pblk_init_global_caches(struct pblk *pblk)
185 char cache_name[PBLK_CACHE_NAME_LEN];
187 down_write(&pblk_lock);
188 pblk_blk_ws_cache = kmem_cache_create("pblk_blk_ws",
189 sizeof(struct pblk_line_ws), 0, 0, NULL);
190 if (!pblk_blk_ws_cache) {
191 up_write(&pblk_lock);
195 pblk_rec_cache = kmem_cache_create("pblk_rec",
196 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
197 if (!pblk_rec_cache) {
198 kmem_cache_destroy(pblk_blk_ws_cache);
199 up_write(&pblk_lock);
203 pblk_r_rq_cache = kmem_cache_create("pblk_r_rq", pblk_r_rq_size,
205 if (!pblk_r_rq_cache) {
206 kmem_cache_destroy(pblk_blk_ws_cache);
207 kmem_cache_destroy(pblk_rec_cache);
208 up_write(&pblk_lock);
212 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
214 if (!pblk_w_rq_cache) {
215 kmem_cache_destroy(pblk_blk_ws_cache);
216 kmem_cache_destroy(pblk_rec_cache);
217 kmem_cache_destroy(pblk_r_rq_cache);
218 up_write(&pblk_lock);
222 snprintf(cache_name, sizeof(cache_name), "pblk_line_m_%s",
223 pblk->disk->disk_name);
224 pblk_line_meta_cache = kmem_cache_create(cache_name,
225 pblk->lm.sec_bitmap_len, 0, 0, NULL);
226 if (!pblk_line_meta_cache) {
227 kmem_cache_destroy(pblk_blk_ws_cache);
228 kmem_cache_destroy(pblk_rec_cache);
229 kmem_cache_destroy(pblk_r_rq_cache);
230 kmem_cache_destroy(pblk_w_rq_cache);
231 up_write(&pblk_lock);
234 up_write(&pblk_lock);
239 static int pblk_core_init(struct pblk *pblk)
241 struct nvm_tgt_dev *dev = pblk->dev;
242 struct nvm_geo *geo = &dev->geo;
246 pblk->min_write_pgs = geo->sec_per_pl * (geo->sec_size / PAGE_SIZE);
247 max_write_ppas = pblk->min_write_pgs * geo->nr_luns;
248 pblk->max_write_pgs = (max_write_ppas < nvm_max_phys_sects(dev)) ?
249 max_write_ppas : nvm_max_phys_sects(dev);
250 pblk->pgs_in_buffer = NVM_MEM_PAGE_WRITE * geo->sec_per_pg *
251 geo->nr_planes * geo->nr_luns;
253 if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
254 pr_err("pblk: cannot support device max_phys_sect\n");
258 div_u64_rem(geo->sec_per_blk, pblk->min_write_pgs, &mod);
260 pr_err("pblk: bad configuration of sectors/pages\n");
264 if (pblk_init_global_caches(pblk))
267 pblk->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
268 if (!pblk->page_pool)
271 pblk->line_ws_pool = mempool_create_slab_pool(geo->nr_luns,
273 if (!pblk->line_ws_pool)
276 pblk->rec_pool = mempool_create_slab_pool(geo->nr_luns, pblk_rec_cache);
278 goto free_blk_ws_pool;
280 pblk->r_rq_pool = mempool_create_slab_pool(64, pblk_r_rq_cache);
281 if (!pblk->r_rq_pool)
284 pblk->w_rq_pool = mempool_create_slab_pool(64, pblk_w_rq_cache);
285 if (!pblk->w_rq_pool)
288 pblk->line_meta_pool =
289 mempool_create_slab_pool(16, pblk_line_meta_cache);
290 if (!pblk->line_meta_pool)
293 pblk->kw_wq = alloc_workqueue("pblk-aux-wq",
294 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
296 goto free_line_meta_pool;
298 if (pblk_set_ppaf(pblk))
301 if (pblk_rwb_init(pblk))
304 INIT_LIST_HEAD(&pblk->compl_list);
308 destroy_workqueue(pblk->kw_wq);
310 mempool_destroy(pblk->line_meta_pool);
312 mempool_destroy(pblk->w_rq_pool);
314 mempool_destroy(pblk->r_rq_pool);
316 mempool_destroy(pblk->rec_pool);
318 mempool_destroy(pblk->line_ws_pool);
320 mempool_destroy(pblk->page_pool);
324 static void pblk_core_free(struct pblk *pblk)
327 destroy_workqueue(pblk->kw_wq);
329 mempool_destroy(pblk->page_pool);
330 mempool_destroy(pblk->line_ws_pool);
331 mempool_destroy(pblk->rec_pool);
332 mempool_destroy(pblk->r_rq_pool);
333 mempool_destroy(pblk->w_rq_pool);
334 mempool_destroy(pblk->line_meta_pool);
336 kmem_cache_destroy(pblk_blk_ws_cache);
337 kmem_cache_destroy(pblk_rec_cache);
338 kmem_cache_destroy(pblk_r_rq_cache);
339 kmem_cache_destroy(pblk_w_rq_cache);
340 kmem_cache_destroy(pblk_line_meta_cache);
343 static void pblk_luns_free(struct pblk *pblk)
348 static void pblk_lines_free(struct pblk *pblk)
350 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
351 struct pblk_line *line;
354 spin_lock(&l_mg->free_lock);
355 for (i = 0; i < l_mg->nr_lines; i++) {
356 line = &pblk->lines[i];
358 pblk_line_free(pblk, line);
359 kfree(line->blk_bitmap);
360 kfree(line->erase_bitmap);
362 spin_unlock(&l_mg->free_lock);
365 static void pblk_line_meta_free(struct pblk *pblk)
367 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
370 kfree(l_mg->bb_template);
373 for (i = 0; i < PBLK_DATA_LINES; i++) {
374 pblk_mfree(l_mg->sline_meta[i].meta, l_mg->smeta_alloc_type);
375 pblk_mfree(l_mg->eline_meta[i].meta, l_mg->emeta_alloc_type);
381 static int pblk_bb_discovery(struct nvm_tgt_dev *dev, struct pblk_lun *rlun)
383 struct nvm_geo *geo = &dev->geo;
388 nr_blks = geo->blks_per_lun * geo->plane_mode;
389 blks = kmalloc(nr_blks, GFP_KERNEL);
394 ppa.g.ch = rlun->bppa.g.ch;
395 ppa.g.lun = rlun->bppa.g.lun;
397 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
401 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
407 rlun->bb_list = blks;
415 static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line)
417 struct pblk_line_meta *lm = &pblk->lm;
418 struct pblk_lun *rlun;
422 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
423 if (!line->blk_bitmap)
426 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
427 if (!line->erase_bitmap) {
428 kfree(line->blk_bitmap);
432 for (i = 0; i < lm->blk_per_line; i++) {
433 rlun = &pblk->luns[i];
434 if (rlun->bb_list[line->id] == NVM_BLK_T_FREE)
437 set_bit(i, line->blk_bitmap);
444 static int pblk_luns_init(struct pblk *pblk, struct ppa_addr *luns)
446 struct nvm_tgt_dev *dev = pblk->dev;
447 struct nvm_geo *geo = &dev->geo;
448 struct pblk_lun *rlun;
451 /* TODO: Implement unbalanced LUN support */
452 if (geo->luns_per_chnl < 0) {
453 pr_err("pblk: unbalanced LUN config.\n");
457 pblk->luns = kcalloc(geo->nr_luns, sizeof(struct pblk_lun), GFP_KERNEL);
461 for (i = 0; i < geo->nr_luns; i++) {
462 /* Stripe across channels */
463 int ch = i % geo->nr_chnls;
464 int lun_raw = i / geo->nr_chnls;
465 int lunid = lun_raw + ch * geo->luns_per_chnl;
467 rlun = &pblk->luns[i];
468 rlun->bppa = luns[lunid];
470 sema_init(&rlun->wr_sem, 1);
472 ret = pblk_bb_discovery(dev, rlun);
475 kfree(pblk->luns[i].bb_list);
483 static int pblk_lines_configure(struct pblk *pblk, int flags)
485 struct pblk_line *line = NULL;
488 if (!(flags & NVM_TARGET_FACTORY)) {
489 line = pblk_recov_l2p(pblk);
491 pr_err("pblk: could not recover l2p table\n");
497 /* Configure next line for user data */
498 line = pblk_line_get_first_data(pblk);
500 pr_err("pblk: line list corrupted\n");
508 /* See comment over struct line_emeta definition */
509 static unsigned int calc_emeta_len(struct pblk *pblk, struct pblk_line_meta *lm)
511 return (sizeof(struct line_emeta) +
512 ((lm->sec_per_line - lm->emeta_sec) * sizeof(u64)) +
513 (pblk->l_mg.nr_lines * sizeof(u32)) +
517 static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
519 struct nvm_tgt_dev *dev = pblk->dev;
520 struct nvm_geo *geo = &dev->geo;
521 sector_t provisioned;
525 provisioned = nr_free_blks;
526 provisioned *= (100 - pblk->over_pct);
527 sector_div(provisioned, 100);
529 /* Internally pblk manages all free blocks, but all calculations based
530 * on user capacity consider only provisioned blocks
532 pblk->rl.total_blocks = nr_free_blks;
533 pblk->rl.nr_secs = nr_free_blks * geo->sec_per_blk;
534 pblk->capacity = provisioned * geo->sec_per_blk;
535 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
538 static int pblk_lines_init(struct pblk *pblk)
540 struct nvm_tgt_dev *dev = pblk->dev;
541 struct nvm_geo *geo = &dev->geo;
542 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
543 struct pblk_line_meta *lm = &pblk->lm;
544 struct pblk_line *line;
545 unsigned int smeta_len, emeta_len;
546 long nr_bad_blks, nr_meta_blks, nr_free_blks;
551 lm->sec_per_line = geo->sec_per_blk * geo->nr_luns;
552 lm->blk_per_line = geo->nr_luns;
553 lm->blk_bitmap_len = BITS_TO_LONGS(geo->nr_luns) * sizeof(long);
554 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
555 lm->lun_bitmap_len = BITS_TO_LONGS(geo->nr_luns) * sizeof(long);
556 lm->high_thrs = lm->sec_per_line / 2;
557 lm->mid_thrs = lm->sec_per_line / 4;
559 /* Calculate necessary pages for smeta. See comment over struct
560 * line_smeta definition
562 lm->smeta_len = sizeof(struct line_smeta) +
563 PBLK_LINE_NR_LUN_BITMAP * lm->lun_bitmap_len;
567 lm->smeta_sec = i * geo->sec_per_pl;
568 lm->smeta_len = lm->smeta_sec * geo->sec_size;
570 smeta_len = sizeof(struct line_smeta) +
571 PBLK_LINE_NR_LUN_BITMAP * lm->lun_bitmap_len;
572 if (smeta_len > lm->smeta_len) {
577 /* Calculate necessary pages for emeta. See comment over struct
578 * line_emeta definition
582 lm->emeta_sec = i * geo->sec_per_pl;
583 lm->emeta_len = lm->emeta_sec * geo->sec_size;
585 emeta_len = calc_emeta_len(pblk, lm);
586 if (emeta_len > lm->emeta_len) {
590 lm->emeta_bb = geo->nr_luns - i;
592 nr_meta_blks = (lm->smeta_sec + lm->emeta_sec +
593 (geo->sec_per_blk / 2)) / geo->sec_per_blk;
594 lm->min_blk_line = nr_meta_blks + 1;
596 l_mg->nr_lines = geo->blks_per_lun;
597 l_mg->log_line = l_mg->data_line = NULL;
598 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
599 l_mg->nr_free_lines = 0;
600 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
602 /* smeta is always small enough to fit on a kmalloc memory allocation,
603 * emeta depends on the number of LUNs allocated to the pblk instance
605 l_mg->smeta_alloc_type = PBLK_KMALLOC_META;
606 for (i = 0; i < PBLK_DATA_LINES; i++) {
607 l_mg->sline_meta[i].meta = kmalloc(lm->smeta_len, GFP_KERNEL);
608 if (!l_mg->sline_meta[i].meta)
610 kfree(l_mg->sline_meta[i].meta);
616 if (lm->emeta_len > KMALLOC_MAX_CACHE_SIZE) {
617 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
619 for (i = 0; i < PBLK_DATA_LINES; i++) {
620 l_mg->eline_meta[i].meta = vmalloc(lm->emeta_len);
621 if (!l_mg->eline_meta[i].meta)
623 vfree(l_mg->eline_meta[i].meta);
629 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
631 for (i = 0; i < PBLK_DATA_LINES; i++) {
632 l_mg->eline_meta[i].meta =
633 kmalloc(lm->emeta_len, GFP_KERNEL);
634 if (!l_mg->eline_meta[i].meta)
636 kfree(l_mg->eline_meta[i].meta);
643 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
644 if (!l_mg->bb_template) {
649 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
652 goto fail_free_bb_template;
655 bb_distance = (geo->nr_luns) * geo->sec_per_pl;
656 for (i = 0; i < lm->sec_per_line; i += bb_distance)
657 bitmap_set(l_mg->bb_template, i, geo->sec_per_pl);
659 INIT_LIST_HEAD(&l_mg->free_list);
660 INIT_LIST_HEAD(&l_mg->corrupt_list);
661 INIT_LIST_HEAD(&l_mg->bad_list);
662 INIT_LIST_HEAD(&l_mg->gc_full_list);
663 INIT_LIST_HEAD(&l_mg->gc_high_list);
664 INIT_LIST_HEAD(&l_mg->gc_mid_list);
665 INIT_LIST_HEAD(&l_mg->gc_low_list);
666 INIT_LIST_HEAD(&l_mg->gc_empty_list);
668 l_mg->gc_lists[0] = &l_mg->gc_high_list;
669 l_mg->gc_lists[1] = &l_mg->gc_mid_list;
670 l_mg->gc_lists[2] = &l_mg->gc_low_list;
672 spin_lock_init(&l_mg->free_lock);
673 spin_lock_init(&l_mg->gc_lock);
675 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
679 goto fail_free_bb_aux;
683 for (i = 0; i < l_mg->nr_lines; i++) {
686 line = &pblk->lines[i];
690 line->type = PBLK_LINETYPE_FREE;
691 line->state = PBLK_LINESTATE_FREE;
692 line->gc_group = PBLK_LINEGC_NONE;
693 spin_lock_init(&line->lock);
695 nr_bad_blks = pblk_bb_line(pblk, line);
696 if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line) {
698 goto fail_free_lines;
701 blk_in_line = lm->blk_per_line - nr_bad_blks;
702 if (blk_in_line < lm->min_blk_line) {
703 line->state = PBLK_LINESTATE_BAD;
704 list_add_tail(&line->list, &l_mg->bad_list);
708 nr_free_blks += blk_in_line;
709 atomic_set(&line->blk_in_line, blk_in_line);
711 l_mg->nr_free_lines++;
712 list_add_tail(&line->list, &l_mg->free_list);
715 pblk_set_provision(pblk, nr_free_blks);
717 sema_init(&pblk->erase_sem, 1);
719 /* Cleanup per-LUN bad block lists - managed within lines on run-time */
720 for (i = 0; i < geo->nr_luns; i++)
721 kfree(pblk->luns[i].bb_list);
728 fail_free_bb_template:
729 kfree(l_mg->bb_template);
731 for (i = 0; i < PBLK_DATA_LINES; i++) {
732 pblk_mfree(l_mg->sline_meta[i].meta, l_mg->smeta_alloc_type);
733 pblk_mfree(l_mg->eline_meta[i].meta, l_mg->emeta_alloc_type);
736 for (i = 0; i < geo->nr_luns; i++)
737 kfree(pblk->luns[i].bb_list);
742 static int pblk_writer_init(struct pblk *pblk)
744 setup_timer(&pblk->wtimer, pblk_write_timer_fn, (unsigned long)pblk);
745 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
747 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
748 if (IS_ERR(pblk->writer_ts)) {
749 pr_err("pblk: could not allocate writer kthread\n");
750 return PTR_ERR(pblk->writer_ts);
756 static void pblk_writer_stop(struct pblk *pblk)
759 kthread_stop(pblk->writer_ts);
760 del_timer(&pblk->wtimer);
763 static void pblk_free(struct pblk *pblk)
765 pblk_luns_free(pblk);
766 pblk_lines_free(pblk);
767 pblk_line_meta_free(pblk);
768 pblk_core_free(pblk);
774 static void pblk_tear_down(struct pblk *pblk)
776 pblk_flush_writer(pblk);
777 pblk_writer_stop(pblk);
778 pblk_rb_sync_l2p(&pblk->rwb);
779 pblk_recov_pad(pblk);
781 pblk_rl_free(&pblk->rl);
783 pr_debug("pblk: consistent tear down\n");
786 static void pblk_exit(void *private)
788 struct pblk *pblk = private;
790 down_write(&pblk_lock);
792 pblk_tear_down(pblk);
794 up_write(&pblk_lock);
797 static sector_t pblk_capacity(void *private)
799 struct pblk *pblk = private;
801 return pblk->capacity * NR_PHY_IN_LOG;
804 static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
807 struct nvm_geo *geo = &dev->geo;
808 struct request_queue *bqueue = dev->q;
809 struct request_queue *tqueue = tdisk->queue;
813 if (dev->identity.dom & NVM_RSP_L2P) {
814 pr_err("pblk: device-side L2P table not supported. (%x)\n",
816 return ERR_PTR(-EINVAL);
819 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
821 return ERR_PTR(-ENOMEM);
826 spin_lock_init(&pblk->trans_lock);
827 spin_lock_init(&pblk->lock);
829 if (flags & NVM_TARGET_FACTORY)
830 pblk_setup_uuid(pblk);
832 #ifdef CONFIG_NVM_DEBUG
833 atomic_long_set(&pblk->inflight_writes, 0);
834 atomic_long_set(&pblk->padded_writes, 0);
835 atomic_long_set(&pblk->padded_wb, 0);
836 atomic_long_set(&pblk->nr_flush, 0);
837 atomic_long_set(&pblk->req_writes, 0);
838 atomic_long_set(&pblk->sub_writes, 0);
839 atomic_long_set(&pblk->sync_writes, 0);
840 atomic_long_set(&pblk->compl_writes, 0);
841 atomic_long_set(&pblk->inflight_reads, 0);
842 atomic_long_set(&pblk->cache_reads, 0);
843 atomic_long_set(&pblk->sync_reads, 0);
844 atomic_long_set(&pblk->recov_writes, 0);
845 atomic_long_set(&pblk->recov_writes, 0);
846 atomic_long_set(&pblk->recov_gc_writes, 0);
849 atomic_long_set(&pblk->read_failed, 0);
850 atomic_long_set(&pblk->read_empty, 0);
851 atomic_long_set(&pblk->read_high_ecc, 0);
852 atomic_long_set(&pblk->read_failed_gc, 0);
853 atomic_long_set(&pblk->write_failed, 0);
854 atomic_long_set(&pblk->erase_failed, 0);
856 ret = pblk_luns_init(pblk, dev->luns);
858 pr_err("pblk: could not initialize luns\n");
862 ret = pblk_lines_init(pblk);
864 pr_err("pblk: could not initialize lines\n");
868 ret = pblk_core_init(pblk);
870 pr_err("pblk: could not initialize core\n");
871 goto fail_free_line_meta;
874 ret = pblk_l2p_init(pblk);
876 pr_err("pblk: could not initialize maps\n");
880 ret = pblk_lines_configure(pblk, flags);
882 pr_err("pblk: could not configure lines\n");
886 ret = pblk_writer_init(pblk);
888 pr_err("pblk: could not initialize write thread\n");
889 goto fail_free_lines;
892 ret = pblk_gc_init(pblk);
894 pr_err("pblk: could not initialize gc\n");
895 goto fail_stop_writer;
898 /* inherit the size from the underlying device */
899 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
900 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
902 blk_queue_write_cache(tqueue, true, false);
904 tqueue->limits.discard_granularity = geo->pgs_per_blk * geo->pfpg_size;
905 tqueue->limits.discard_alignment = 0;
906 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
907 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, tqueue);
909 pr_info("pblk init: luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
910 geo->nr_luns, pblk->l_mg.nr_lines,
911 (unsigned long long)pblk->rl.nr_secs,
912 pblk->rwb.nr_entries);
914 wake_up_process(pblk->writer_ts);
918 pblk_writer_stop(pblk);
920 pblk_lines_free(pblk);
924 pblk_core_free(pblk);
926 pblk_line_meta_free(pblk);
928 pblk_luns_free(pblk);
934 /* physical block device target */
935 static struct nvm_tgt_type tt_pblk = {
937 .version = {1, 0, 0},
939 .make_rq = pblk_make_rq,
940 .capacity = pblk_capacity,
945 .sysfs_init = pblk_sysfs_init,
946 .sysfs_exit = pblk_sysfs_exit,
949 static int __init pblk_module_init(void)
953 pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
956 ret = nvm_register_tgt_type(&tt_pblk);
958 bioset_free(pblk_bio_set);
962 static void pblk_module_exit(void)
964 bioset_free(pblk_bio_set);
965 nvm_unregister_tgt_type(&tt_pblk);
968 module_init(pblk_module_init);
969 module_exit(pblk_module_exit);
970 MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
971 MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
972 MODULE_LICENSE("GPL v2");
973 MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");