]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/lightnvm/pblk-init.c
c8a718249e267dfbe4bfd0cdfb339bfad54d9289
[linux.git] / drivers / lightnvm / pblk-init.c
1 /*
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>
6  *
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.
10  *
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.
15  *
16  * Implementation of a physical block-device target for Open-channel SSDs.
17  *
18  * pblk-init.c - pblk's initialization.
19  */
20
21 #include "pblk.h"
22
23 static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
24                                 *pblk_w_rq_cache;
25 static DECLARE_RWSEM(pblk_lock);
26 struct bio_set *pblk_bio_set;
27
28 static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
29                           struct bio *bio)
30 {
31         int ret;
32
33         /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
34          * constraint. Writes can be of arbitrary size.
35          */
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))
40                         bio_put(bio);
41
42                 return ret;
43         }
44
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.
48          */
49         if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
50                 blk_queue_split(q, &bio);
51
52         return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
53 }
54
55 static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
56 {
57         struct pblk *pblk = q->queuedata;
58
59         if (bio_op(bio) == REQ_OP_DISCARD) {
60                 pblk_discard(pblk, bio);
61                 if (!(bio->bi_opf & REQ_PREFLUSH)) {
62                         bio_endio(bio);
63                         return BLK_QC_T_NONE;
64                 }
65         }
66
67         switch (pblk_rw_io(q, pblk, bio)) {
68         case NVM_IO_ERR:
69                 bio_io_error(bio);
70                 break;
71         case NVM_IO_DONE:
72                 bio_endio(bio);
73                 break;
74         }
75
76         return BLK_QC_T_NONE;
77 }
78
79 static size_t pblk_trans_map_size(struct pblk *pblk)
80 {
81         int entry_size = 8;
82
83         if (pblk->ppaf_bitsize < 32)
84                 entry_size = 4;
85
86         return entry_size * pblk->rl.nr_secs;
87 }
88
89 #ifdef CONFIG_NVM_DEBUG
90 static u32 pblk_l2p_crc(struct pblk *pblk)
91 {
92         size_t map_size;
93         u32 crc = ~(u32)0;
94
95         map_size = pblk_trans_map_size(pblk);
96         crc = crc32_le(crc, pblk->trans_map, map_size);
97         return crc;
98 }
99 #endif
100
101 static void pblk_l2p_free(struct pblk *pblk)
102 {
103         vfree(pblk->trans_map);
104 }
105
106 static int pblk_l2p_init(struct pblk *pblk)
107 {
108         sector_t i;
109         struct ppa_addr ppa;
110         size_t map_size;
111
112         map_size = pblk_trans_map_size(pblk);
113         pblk->trans_map = vmalloc(map_size);
114         if (!pblk->trans_map)
115                 return -ENOMEM;
116
117         pblk_ppa_set_empty(&ppa);
118
119         for (i = 0; i < pblk->rl.nr_secs; i++)
120                 pblk_trans_map_set(pblk, i, ppa);
121
122         return 0;
123 }
124
125 static void pblk_rwb_free(struct pblk *pblk)
126 {
127         if (pblk_rb_tear_down_check(&pblk->rwb))
128                 pr_err("pblk: write buffer error on tear down\n");
129
130         pblk_rb_data_free(&pblk->rwb);
131         vfree(pblk_rb_entries_ref(&pblk->rwb));
132 }
133
134 static int pblk_rwb_init(struct pblk *pblk)
135 {
136         struct nvm_tgt_dev *dev = pblk->dev;
137         struct nvm_geo *geo = &dev->geo;
138         struct pblk_rb_entry *entries;
139         unsigned long nr_entries;
140         unsigned int power_size, power_seg_sz;
141
142         nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
143
144         entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
145         if (!entries)
146                 return -ENOMEM;
147
148         power_size = get_count_order(nr_entries);
149         power_seg_sz = get_count_order(geo->sec_size);
150
151         return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
152 }
153
154 /* Minimum pages needed within a lun */
155 #define ADDR_POOL_SIZE 64
156
157 static int pblk_set_ppaf(struct pblk *pblk)
158 {
159         struct nvm_tgt_dev *dev = pblk->dev;
160         struct nvm_geo *geo = &dev->geo;
161         struct nvm_addr_format ppaf = geo->ppaf;
162         int power_len;
163
164         /* Re-calculate channel and lun format to adapt to configuration */
165         power_len = get_count_order(geo->nr_chnls);
166         if (1 << power_len != geo->nr_chnls) {
167                 pr_err("pblk: supports only power-of-two channel config.\n");
168                 return -EINVAL;
169         }
170         ppaf.ch_len = power_len;
171
172         power_len = get_count_order(geo->nr_luns);
173         if (1 << power_len != geo->nr_luns) {
174                 pr_err("pblk: supports only power-of-two LUN config.\n");
175                 return -EINVAL;
176         }
177         ppaf.lun_len = power_len;
178
179         pblk->ppaf.sec_offset = 0;
180         pblk->ppaf.pln_offset = ppaf.sect_len;
181         pblk->ppaf.ch_offset = pblk->ppaf.pln_offset + ppaf.pln_len;
182         pblk->ppaf.lun_offset = pblk->ppaf.ch_offset + ppaf.ch_len;
183         pblk->ppaf.pg_offset = pblk->ppaf.lun_offset + ppaf.lun_len;
184         pblk->ppaf.blk_offset = pblk->ppaf.pg_offset + ppaf.pg_len;
185         pblk->ppaf.sec_mask = (1ULL << ppaf.sect_len) - 1;
186         pblk->ppaf.pln_mask = ((1ULL << ppaf.pln_len) - 1) <<
187                                                         pblk->ppaf.pln_offset;
188         pblk->ppaf.ch_mask = ((1ULL << ppaf.ch_len) - 1) <<
189                                                         pblk->ppaf.ch_offset;
190         pblk->ppaf.lun_mask = ((1ULL << ppaf.lun_len) - 1) <<
191                                                         pblk->ppaf.lun_offset;
192         pblk->ppaf.pg_mask = ((1ULL << ppaf.pg_len) - 1) <<
193                                                         pblk->ppaf.pg_offset;
194         pblk->ppaf.blk_mask = ((1ULL << ppaf.blk_len) - 1) <<
195                                                         pblk->ppaf.blk_offset;
196
197         pblk->ppaf_bitsize = pblk->ppaf.blk_offset + ppaf.blk_len;
198
199         return 0;
200 }
201
202 static int pblk_init_global_caches(struct pblk *pblk)
203 {
204         down_write(&pblk_lock);
205         pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
206                                 sizeof(struct pblk_line_ws), 0, 0, NULL);
207         if (!pblk_ws_cache) {
208                 up_write(&pblk_lock);
209                 return -ENOMEM;
210         }
211
212         pblk_rec_cache = kmem_cache_create("pblk_rec",
213                                 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
214         if (!pblk_rec_cache) {
215                 kmem_cache_destroy(pblk_ws_cache);
216                 up_write(&pblk_lock);
217                 return -ENOMEM;
218         }
219
220         pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
221                                 0, 0, NULL);
222         if (!pblk_g_rq_cache) {
223                 kmem_cache_destroy(pblk_ws_cache);
224                 kmem_cache_destroy(pblk_rec_cache);
225                 up_write(&pblk_lock);
226                 return -ENOMEM;
227         }
228
229         pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
230                                 0, 0, NULL);
231         if (!pblk_w_rq_cache) {
232                 kmem_cache_destroy(pblk_ws_cache);
233                 kmem_cache_destroy(pblk_rec_cache);
234                 kmem_cache_destroy(pblk_g_rq_cache);
235                 up_write(&pblk_lock);
236                 return -ENOMEM;
237         }
238         up_write(&pblk_lock);
239
240         return 0;
241 }
242
243 static void pblk_free_global_caches(struct pblk *pblk)
244 {
245         kmem_cache_destroy(pblk_ws_cache);
246         kmem_cache_destroy(pblk_rec_cache);
247         kmem_cache_destroy(pblk_g_rq_cache);
248         kmem_cache_destroy(pblk_w_rq_cache);
249 }
250
251 static int pblk_core_init(struct pblk *pblk)
252 {
253         struct nvm_tgt_dev *dev = pblk->dev;
254         struct nvm_geo *geo = &dev->geo;
255
256         pblk->pgs_in_buffer = NVM_MEM_PAGE_WRITE * geo->sec_per_pg *
257                                                 geo->nr_planes * geo->all_luns;
258
259         if (pblk_init_global_caches(pblk))
260                 return -ENOMEM;
261
262         /* Internal bios can be at most the sectors signaled by the device. */
263         pblk->page_bio_pool = mempool_create_page_pool(nvm_max_phys_sects(dev),
264                                                                         0);
265         if (!pblk->page_bio_pool)
266                 goto free_global_caches;
267
268         pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
269                                                         pblk_ws_cache);
270         if (!pblk->gen_ws_pool)
271                 goto free_page_bio_pool;
272
273         pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
274                                                         pblk_rec_cache);
275         if (!pblk->rec_pool)
276                 goto free_gen_ws_pool;
277
278         pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
279                                                         pblk_g_rq_cache);
280         if (!pblk->r_rq_pool)
281                 goto free_rec_pool;
282
283         pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
284                                                         pblk_g_rq_cache);
285         if (!pblk->e_rq_pool)
286                 goto free_r_rq_pool;
287
288         pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
289                                                         pblk_w_rq_cache);
290         if (!pblk->w_rq_pool)
291                 goto free_e_rq_pool;
292
293         pblk->close_wq = alloc_workqueue("pblk-close-wq",
294                         WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
295         if (!pblk->close_wq)
296                 goto free_w_rq_pool;
297
298         pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
299                         WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
300         if (!pblk->bb_wq)
301                 goto free_close_wq;
302
303         pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
304                         WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
305         if (!pblk->r_end_wq)
306                 goto free_bb_wq;
307
308         if (pblk_set_ppaf(pblk))
309                 goto free_r_end_wq;
310
311         if (pblk_rwb_init(pblk))
312                 goto free_r_end_wq;
313
314         INIT_LIST_HEAD(&pblk->compl_list);
315         return 0;
316
317 free_r_end_wq:
318         destroy_workqueue(pblk->r_end_wq);
319 free_bb_wq:
320         destroy_workqueue(pblk->bb_wq);
321 free_close_wq:
322         destroy_workqueue(pblk->close_wq);
323 free_w_rq_pool:
324         mempool_destroy(pblk->w_rq_pool);
325 free_e_rq_pool:
326         mempool_destroy(pblk->e_rq_pool);
327 free_r_rq_pool:
328         mempool_destroy(pblk->r_rq_pool);
329 free_rec_pool:
330         mempool_destroy(pblk->rec_pool);
331 free_gen_ws_pool:
332         mempool_destroy(pblk->gen_ws_pool);
333 free_page_bio_pool:
334         mempool_destroy(pblk->page_bio_pool);
335 free_global_caches:
336         pblk_free_global_caches(pblk);
337         return -ENOMEM;
338 }
339
340 static void pblk_core_free(struct pblk *pblk)
341 {
342         if (pblk->close_wq)
343                 destroy_workqueue(pblk->close_wq);
344
345         if (pblk->r_end_wq)
346                 destroy_workqueue(pblk->r_end_wq);
347
348         if (pblk->bb_wq)
349                 destroy_workqueue(pblk->bb_wq);
350
351         mempool_destroy(pblk->page_bio_pool);
352         mempool_destroy(pblk->gen_ws_pool);
353         mempool_destroy(pblk->rec_pool);
354         mempool_destroy(pblk->r_rq_pool);
355         mempool_destroy(pblk->e_rq_pool);
356         mempool_destroy(pblk->w_rq_pool);
357
358         pblk_free_global_caches(pblk);
359 }
360
361 static void pblk_luns_free(struct pblk *pblk)
362 {
363         kfree(pblk->luns);
364 }
365
366 static void pblk_free_line_bitmaps(struct pblk_line *line)
367 {
368         kfree(line->blk_bitmap);
369         kfree(line->erase_bitmap);
370 }
371
372 static void pblk_lines_free(struct pblk *pblk)
373 {
374         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
375         struct pblk_line *line;
376         int i;
377
378         spin_lock(&l_mg->free_lock);
379         for (i = 0; i < l_mg->nr_lines; i++) {
380                 line = &pblk->lines[i];
381
382                 pblk_line_free(pblk, line);
383                 pblk_free_line_bitmaps(line);
384         }
385         spin_unlock(&l_mg->free_lock);
386 }
387
388 static void pblk_line_meta_free(struct pblk *pblk)
389 {
390         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
391         int i;
392
393         kfree(l_mg->bb_template);
394         kfree(l_mg->bb_aux);
395         kfree(l_mg->vsc_list);
396
397         for (i = 0; i < PBLK_DATA_LINES; i++) {
398                 kfree(l_mg->sline_meta[i]);
399                 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
400                 kfree(l_mg->eline_meta[i]);
401         }
402
403         kfree(pblk->lines);
404 }
405
406 static int pblk_bb_discovery(struct nvm_tgt_dev *dev, struct pblk_lun *rlun)
407 {
408         struct nvm_geo *geo = &dev->geo;
409         struct ppa_addr ppa;
410         u8 *blks;
411         int nr_blks, ret;
412
413         nr_blks = geo->nr_chks * geo->plane_mode;
414         blks = kmalloc(nr_blks, GFP_KERNEL);
415         if (!blks)
416                 return -ENOMEM;
417
418         ppa.ppa = 0;
419         ppa.g.ch = rlun->bppa.g.ch;
420         ppa.g.lun = rlun->bppa.g.lun;
421
422         ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
423         if (ret)
424                 goto out;
425
426         nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
427         if (nr_blks < 0) {
428                 ret = nr_blks;
429                 goto out;
430         }
431
432         rlun->bb_list = blks;
433
434         return 0;
435 out:
436         kfree(blks);
437         return ret;
438 }
439
440 static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line,
441                         int blk_per_line)
442 {
443         struct nvm_tgt_dev *dev = pblk->dev;
444         struct nvm_geo *geo = &dev->geo;
445         struct pblk_lun *rlun;
446         int bb_cnt = 0;
447         int i;
448
449         for (i = 0; i < blk_per_line; i++) {
450                 rlun = &pblk->luns[i];
451                 if (rlun->bb_list[line->id] == NVM_BLK_T_FREE)
452                         continue;
453
454                 set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap);
455                 bb_cnt++;
456         }
457
458         return bb_cnt;
459 }
460
461 static int pblk_alloc_line_bitmaps(struct pblk *pblk, struct pblk_line *line)
462 {
463         struct pblk_line_meta *lm = &pblk->lm;
464
465         line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
466         if (!line->blk_bitmap)
467                 return -ENOMEM;
468
469         line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
470         if (!line->erase_bitmap) {
471                 kfree(line->blk_bitmap);
472                 return -ENOMEM;
473         }
474
475         return 0;
476 }
477
478 static int pblk_luns_init(struct pblk *pblk, struct ppa_addr *luns)
479 {
480         struct nvm_tgt_dev *dev = pblk->dev;
481         struct nvm_geo *geo = &dev->geo;
482         struct pblk_lun *rlun;
483         int i, ret;
484
485         /* TODO: Implement unbalanced LUN support */
486         if (geo->nr_luns < 0) {
487                 pr_err("pblk: unbalanced LUN config.\n");
488                 return -EINVAL;
489         }
490
491         pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
492                                                                 GFP_KERNEL);
493         if (!pblk->luns)
494                 return -ENOMEM;
495
496         for (i = 0; i < geo->all_luns; i++) {
497                 /* Stripe across channels */
498                 int ch = i % geo->nr_chnls;
499                 int lun_raw = i / geo->nr_chnls;
500                 int lunid = lun_raw + ch * geo->nr_luns;
501
502                 rlun = &pblk->luns[i];
503                 rlun->bppa = luns[lunid];
504
505                 sema_init(&rlun->wr_sem, 1);
506
507                 ret = pblk_bb_discovery(dev, rlun);
508                 if (ret) {
509                         while (--i >= 0)
510                                 kfree(pblk->luns[i].bb_list);
511                         return ret;
512                 }
513         }
514
515         return 0;
516 }
517
518 static int pblk_lines_configure(struct pblk *pblk, int flags)
519 {
520         struct pblk_line *line = NULL;
521         int ret = 0;
522
523         if (!(flags & NVM_TARGET_FACTORY)) {
524                 line = pblk_recov_l2p(pblk);
525                 if (IS_ERR(line)) {
526                         pr_err("pblk: could not recover l2p table\n");
527                         ret = -EFAULT;
528                 }
529         }
530
531 #ifdef CONFIG_NVM_DEBUG
532         pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
533 #endif
534
535         /* Free full lines directly as GC has not been started yet */
536         pblk_gc_free_full_lines(pblk);
537
538         if (!line) {
539                 /* Configure next line for user data */
540                 line = pblk_line_get_first_data(pblk);
541                 if (!line) {
542                         pr_err("pblk: line list corrupted\n");
543                         ret = -EFAULT;
544                 }
545         }
546
547         return ret;
548 }
549
550 /* See comment over struct line_emeta definition */
551 static unsigned int calc_emeta_len(struct pblk *pblk)
552 {
553         struct pblk_line_meta *lm = &pblk->lm;
554         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
555         struct nvm_tgt_dev *dev = pblk->dev;
556         struct nvm_geo *geo = &dev->geo;
557
558         /* Round to sector size so that lba_list starts on its own sector */
559         lm->emeta_sec[1] = DIV_ROUND_UP(
560                         sizeof(struct line_emeta) + lm->blk_bitmap_len,
561                         geo->sec_size);
562         lm->emeta_len[1] = lm->emeta_sec[1] * geo->sec_size;
563
564         /* Round to sector size so that vsc_list starts on its own sector */
565         lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
566         lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
567                         geo->sec_size);
568         lm->emeta_len[2] = lm->emeta_sec[2] * geo->sec_size;
569
570         lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
571                         geo->sec_size);
572         lm->emeta_len[3] = lm->emeta_sec[3] * geo->sec_size;
573
574         lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
575
576         return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
577 }
578
579 static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
580 {
581         struct nvm_tgt_dev *dev = pblk->dev;
582         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
583         struct pblk_line_meta *lm = &pblk->lm;
584         struct nvm_geo *geo = &dev->geo;
585         sector_t provisioned;
586         int sec_meta, blk_meta;
587
588         pblk->op = 20;
589
590         provisioned = nr_free_blks;
591         provisioned *= (100 - pblk->op);
592         sector_div(provisioned, 100);
593
594         pblk->op_blks = nr_free_blks - provisioned;
595
596         /* Internally pblk manages all free blocks, but all calculations based
597          * on user capacity consider only provisioned blocks
598          */
599         pblk->rl.total_blocks = nr_free_blks;
600         pblk->rl.nr_secs = nr_free_blks * geo->sec_per_chk;
601
602         /* Consider sectors used for metadata */
603         sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
604         blk_meta = DIV_ROUND_UP(sec_meta, geo->sec_per_chk);
605
606         pblk->capacity = (provisioned - blk_meta) * geo->sec_per_chk;
607
608         atomic_set(&pblk->rl.free_blocks, nr_free_blks);
609         atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
610 }
611
612 static int pblk_lines_alloc_metadata(struct pblk *pblk)
613 {
614         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
615         struct pblk_line_meta *lm = &pblk->lm;
616         int i;
617
618         /* smeta is always small enough to fit on a kmalloc memory allocation,
619          * emeta depends on the number of LUNs allocated to the pblk instance
620          */
621         for (i = 0; i < PBLK_DATA_LINES; i++) {
622                 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
623                 if (!l_mg->sline_meta[i])
624                         goto fail_free_smeta;
625         }
626
627         /* emeta allocates three different buffers for managing metadata with
628          * in-memory and in-media layouts
629          */
630         for (i = 0; i < PBLK_DATA_LINES; i++) {
631                 struct pblk_emeta *emeta;
632
633                 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
634                 if (!emeta)
635                         goto fail_free_emeta;
636
637                 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
638                         l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
639
640                         emeta->buf = vmalloc(lm->emeta_len[0]);
641                         if (!emeta->buf) {
642                                 kfree(emeta);
643                                 goto fail_free_emeta;
644                         }
645
646                         emeta->nr_entries = lm->emeta_sec[0];
647                         l_mg->eline_meta[i] = emeta;
648                 } else {
649                         l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
650
651                         emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
652                         if (!emeta->buf) {
653                                 kfree(emeta);
654                                 goto fail_free_emeta;
655                         }
656
657                         emeta->nr_entries = lm->emeta_sec[0];
658                         l_mg->eline_meta[i] = emeta;
659                 }
660         }
661
662         l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
663         if (!l_mg->vsc_list)
664                 goto fail_free_emeta;
665
666         for (i = 0; i < l_mg->nr_lines; i++)
667                 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
668
669         return 0;
670
671 fail_free_emeta:
672         while (--i >= 0) {
673                 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
674                         vfree(l_mg->eline_meta[i]->buf);
675                 else
676                         kfree(l_mg->eline_meta[i]->buf);
677                 kfree(l_mg->eline_meta[i]);
678         }
679
680 fail_free_smeta:
681         for (i = 0; i < PBLK_DATA_LINES; i++)
682                 kfree(l_mg->sline_meta[i]);
683
684         return -ENOMEM;
685 }
686
687 static int pblk_lines_init(struct pblk *pblk)
688 {
689         struct nvm_tgt_dev *dev = pblk->dev;
690         struct nvm_geo *geo = &dev->geo;
691         struct pblk_line_mgmt *l_mg = &pblk->l_mg;
692         struct pblk_line_meta *lm = &pblk->lm;
693         struct pblk_line *line;
694         unsigned int smeta_len, emeta_len;
695         long nr_bad_blks, nr_free_blks;
696         int bb_distance, max_write_ppas, mod;
697         int i, ret;
698
699         pblk->min_write_pgs = geo->sec_per_pl * (geo->sec_size / PAGE_SIZE);
700         max_write_ppas = pblk->min_write_pgs * geo->all_luns;
701         pblk->max_write_pgs = (max_write_ppas < nvm_max_phys_sects(dev)) ?
702                                 max_write_ppas : nvm_max_phys_sects(dev);
703         pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
704
705         if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
706                 pr_err("pblk: cannot support device max_phys_sect\n");
707                 return -EINVAL;
708         }
709
710         div_u64_rem(geo->sec_per_chk, pblk->min_write_pgs, &mod);
711         if (mod) {
712                 pr_err("pblk: bad configuration of sectors/pages\n");
713                 return -EINVAL;
714         }
715
716         l_mg->nr_lines = geo->nr_chks;
717         l_mg->log_line = l_mg->data_line = NULL;
718         l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
719         l_mg->nr_free_lines = 0;
720         bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
721
722         lm->sec_per_line = geo->sec_per_chk * geo->all_luns;
723         lm->blk_per_line = geo->all_luns;
724         lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
725         lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
726         lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
727         lm->mid_thrs = lm->sec_per_line / 2;
728         lm->high_thrs = lm->sec_per_line / 4;
729         lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
730
731         /* Calculate necessary pages for smeta. See comment over struct
732          * line_smeta definition
733          */
734         i = 1;
735 add_smeta_page:
736         lm->smeta_sec = i * geo->sec_per_pl;
737         lm->smeta_len = lm->smeta_sec * geo->sec_size;
738
739         smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
740         if (smeta_len > lm->smeta_len) {
741                 i++;
742                 goto add_smeta_page;
743         }
744
745         /* Calculate necessary pages for emeta. See comment over struct
746          * line_emeta definition
747          */
748         i = 1;
749 add_emeta_page:
750         lm->emeta_sec[0] = i * geo->sec_per_pl;
751         lm->emeta_len[0] = lm->emeta_sec[0] * geo->sec_size;
752
753         emeta_len = calc_emeta_len(pblk);
754         if (emeta_len > lm->emeta_len[0]) {
755                 i++;
756                 goto add_emeta_page;
757         }
758
759         lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
760
761         lm->min_blk_line = 1;
762         if (geo->all_luns > 1)
763                 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
764                                         lm->emeta_sec[0], geo->sec_per_chk);
765
766         if (lm->min_blk_line > lm->blk_per_line) {
767                 pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
768                                                         lm->blk_per_line);
769                 ret = -EINVAL;
770                 goto fail;
771         }
772
773         ret = pblk_lines_alloc_metadata(pblk);
774         if (ret)
775                 goto fail;
776
777         l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
778         if (!l_mg->bb_template) {
779                 ret = -ENOMEM;
780                 goto fail_free_meta;
781         }
782
783         l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
784         if (!l_mg->bb_aux) {
785                 ret = -ENOMEM;
786                 goto fail_free_bb_template;
787         }
788
789         bb_distance = (geo->all_luns) * geo->sec_per_pl;
790         for (i = 0; i < lm->sec_per_line; i += bb_distance)
791                 bitmap_set(l_mg->bb_template, i, geo->sec_per_pl);
792
793         INIT_LIST_HEAD(&l_mg->free_list);
794         INIT_LIST_HEAD(&l_mg->corrupt_list);
795         INIT_LIST_HEAD(&l_mg->bad_list);
796         INIT_LIST_HEAD(&l_mg->gc_full_list);
797         INIT_LIST_HEAD(&l_mg->gc_high_list);
798         INIT_LIST_HEAD(&l_mg->gc_mid_list);
799         INIT_LIST_HEAD(&l_mg->gc_low_list);
800         INIT_LIST_HEAD(&l_mg->gc_empty_list);
801
802         INIT_LIST_HEAD(&l_mg->emeta_list);
803
804         l_mg->gc_lists[0] = &l_mg->gc_high_list;
805         l_mg->gc_lists[1] = &l_mg->gc_mid_list;
806         l_mg->gc_lists[2] = &l_mg->gc_low_list;
807
808         spin_lock_init(&l_mg->free_lock);
809         spin_lock_init(&l_mg->close_lock);
810         spin_lock_init(&l_mg->gc_lock);
811
812         pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
813                                                                 GFP_KERNEL);
814         if (!pblk->lines) {
815                 ret = -ENOMEM;
816                 goto fail_free_bb_aux;
817         }
818
819         nr_free_blks = 0;
820         for (i = 0; i < l_mg->nr_lines; i++) {
821                 int blk_in_line;
822
823                 line = &pblk->lines[i];
824
825                 line->pblk = pblk;
826                 line->id = i;
827                 line->type = PBLK_LINETYPE_FREE;
828                 line->state = PBLK_LINESTATE_FREE;
829                 line->gc_group = PBLK_LINEGC_NONE;
830                 line->vsc = &l_mg->vsc_list[i];
831                 spin_lock_init(&line->lock);
832
833                 ret = pblk_alloc_line_bitmaps(pblk, line);
834                 if (ret)
835                         goto fail_free_lines;
836
837                 nr_bad_blks = pblk_bb_line(pblk, line, lm->blk_per_line);
838                 if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line) {
839                         pblk_free_line_bitmaps(line);
840                         ret = -EINVAL;
841                         goto fail_free_lines;
842                 }
843
844                 blk_in_line = lm->blk_per_line - nr_bad_blks;
845                 if (blk_in_line < lm->min_blk_line) {
846                         line->state = PBLK_LINESTATE_BAD;
847                         list_add_tail(&line->list, &l_mg->bad_list);
848                         continue;
849                 }
850
851                 nr_free_blks += blk_in_line;
852                 atomic_set(&line->blk_in_line, blk_in_line);
853
854                 l_mg->nr_free_lines++;
855                 list_add_tail(&line->list, &l_mg->free_list);
856         }
857
858         pblk_set_provision(pblk, nr_free_blks);
859
860         /* Cleanup per-LUN bad block lists - managed within lines on run-time */
861         for (i = 0; i < geo->all_luns; i++)
862                 kfree(pblk->luns[i].bb_list);
863
864         return 0;
865 fail_free_lines:
866         while (--i >= 0)
867                 pblk_free_line_bitmaps(&pblk->lines[i]);
868 fail_free_bb_aux:
869         kfree(l_mg->bb_aux);
870 fail_free_bb_template:
871         kfree(l_mg->bb_template);
872 fail_free_meta:
873         pblk_line_meta_free(pblk);
874 fail:
875         for (i = 0; i < geo->all_luns; i++)
876                 kfree(pblk->luns[i].bb_list);
877
878         return ret;
879 }
880
881 static int pblk_writer_init(struct pblk *pblk)
882 {
883         timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
884         mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
885
886         pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
887         if (IS_ERR(pblk->writer_ts)) {
888                 pr_err("pblk: could not allocate writer kthread\n");
889                 return PTR_ERR(pblk->writer_ts);
890         }
891
892         return 0;
893 }
894
895 static void pblk_writer_stop(struct pblk *pblk)
896 {
897         /* The pipeline must be stopped and the write buffer emptied before the
898          * write thread is stopped
899          */
900         WARN(pblk_rb_read_count(&pblk->rwb),
901                         "Stopping not fully persisted write buffer\n");
902
903         WARN(pblk_rb_sync_count(&pblk->rwb),
904                         "Stopping not fully synced write buffer\n");
905
906         if (pblk->writer_ts)
907                 kthread_stop(pblk->writer_ts);
908         del_timer(&pblk->wtimer);
909 }
910
911 static void pblk_free(struct pblk *pblk)
912 {
913         pblk_luns_free(pblk);
914         pblk_lines_free(pblk);
915         pblk_line_meta_free(pblk);
916         pblk_core_free(pblk);
917         pblk_l2p_free(pblk);
918
919         kfree(pblk);
920 }
921
922 static void pblk_tear_down(struct pblk *pblk)
923 {
924         pblk_pipeline_stop(pblk);
925         pblk_writer_stop(pblk);
926         pblk_rb_sync_l2p(&pblk->rwb);
927         pblk_rwb_free(pblk);
928         pblk_rl_free(&pblk->rl);
929
930         pr_debug("pblk: consistent tear down\n");
931 }
932
933 static void pblk_exit(void *private)
934 {
935         struct pblk *pblk = private;
936
937         down_write(&pblk_lock);
938         pblk_gc_exit(pblk);
939         pblk_tear_down(pblk);
940
941 #ifdef CONFIG_NVM_DEBUG
942         pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
943 #endif
944
945         pblk_free(pblk);
946         up_write(&pblk_lock);
947 }
948
949 static sector_t pblk_capacity(void *private)
950 {
951         struct pblk *pblk = private;
952
953         return pblk->capacity * NR_PHY_IN_LOG;
954 }
955
956 static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
957                        int flags)
958 {
959         struct nvm_geo *geo = &dev->geo;
960         struct request_queue *bqueue = dev->q;
961         struct request_queue *tqueue = tdisk->queue;
962         struct pblk *pblk;
963         int ret;
964
965         if (dev->identity.dom & NVM_RSP_L2P) {
966                 pr_err("pblk: host-side L2P table not supported. (%x)\n",
967                                                         dev->identity.dom);
968                 return ERR_PTR(-EINVAL);
969         }
970
971         pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
972         if (!pblk)
973                 return ERR_PTR(-ENOMEM);
974
975         pblk->dev = dev;
976         pblk->disk = tdisk;
977         pblk->state = PBLK_STATE_RUNNING;
978         pblk->gc.gc_enabled = 0;
979
980         spin_lock_init(&pblk->trans_lock);
981         spin_lock_init(&pblk->lock);
982
983         if (flags & NVM_TARGET_FACTORY)
984                 pblk_setup_uuid(pblk);
985
986 #ifdef CONFIG_NVM_DEBUG
987         atomic_long_set(&pblk->inflight_writes, 0);
988         atomic_long_set(&pblk->padded_writes, 0);
989         atomic_long_set(&pblk->padded_wb, 0);
990         atomic_long_set(&pblk->nr_flush, 0);
991         atomic_long_set(&pblk->req_writes, 0);
992         atomic_long_set(&pblk->sub_writes, 0);
993         atomic_long_set(&pblk->sync_writes, 0);
994         atomic_long_set(&pblk->inflight_reads, 0);
995         atomic_long_set(&pblk->cache_reads, 0);
996         atomic_long_set(&pblk->sync_reads, 0);
997         atomic_long_set(&pblk->recov_writes, 0);
998         atomic_long_set(&pblk->recov_writes, 0);
999         atomic_long_set(&pblk->recov_gc_writes, 0);
1000         atomic_long_set(&pblk->recov_gc_reads, 0);
1001 #endif
1002
1003         atomic_long_set(&pblk->read_failed, 0);
1004         atomic_long_set(&pblk->read_empty, 0);
1005         atomic_long_set(&pblk->read_high_ecc, 0);
1006         atomic_long_set(&pblk->read_failed_gc, 0);
1007         atomic_long_set(&pblk->write_failed, 0);
1008         atomic_long_set(&pblk->erase_failed, 0);
1009
1010         ret = pblk_luns_init(pblk, dev->luns);
1011         if (ret) {
1012                 pr_err("pblk: could not initialize luns\n");
1013                 goto fail;
1014         }
1015
1016         ret = pblk_lines_init(pblk);
1017         if (ret) {
1018                 pr_err("pblk: could not initialize lines\n");
1019                 goto fail_free_luns;
1020         }
1021
1022         ret = pblk_core_init(pblk);
1023         if (ret) {
1024                 pr_err("pblk: could not initialize core\n");
1025                 goto fail_free_line_meta;
1026         }
1027
1028         ret = pblk_l2p_init(pblk);
1029         if (ret) {
1030                 pr_err("pblk: could not initialize maps\n");
1031                 goto fail_free_core;
1032         }
1033
1034         ret = pblk_lines_configure(pblk, flags);
1035         if (ret) {
1036                 pr_err("pblk: could not configure lines\n");
1037                 goto fail_free_l2p;
1038         }
1039
1040         ret = pblk_writer_init(pblk);
1041         if (ret) {
1042                 pr_err("pblk: could not initialize write thread\n");
1043                 goto fail_free_lines;
1044         }
1045
1046         ret = pblk_gc_init(pblk);
1047         if (ret) {
1048                 pr_err("pblk: could not initialize gc\n");
1049                 goto fail_stop_writer;
1050         }
1051
1052         /* inherit the size from the underlying device */
1053         blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1054         blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1055
1056         blk_queue_write_cache(tqueue, true, false);
1057
1058         tqueue->limits.discard_granularity = geo->sec_per_chk * geo->sec_size;
1059         tqueue->limits.discard_alignment = 0;
1060         blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
1061         queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, tqueue);
1062
1063         pr_info("pblk init: luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1064                         geo->all_luns, pblk->l_mg.nr_lines,
1065                         (unsigned long long)pblk->rl.nr_secs,
1066                         pblk->rwb.nr_entries);
1067
1068         wake_up_process(pblk->writer_ts);
1069
1070         /* Check if we need to start GC */
1071         pblk_gc_should_kick(pblk);
1072
1073         return pblk;
1074
1075 fail_stop_writer:
1076         pblk_writer_stop(pblk);
1077 fail_free_lines:
1078         pblk_lines_free(pblk);
1079 fail_free_l2p:
1080         pblk_l2p_free(pblk);
1081 fail_free_core:
1082         pblk_core_free(pblk);
1083 fail_free_line_meta:
1084         pblk_line_meta_free(pblk);
1085 fail_free_luns:
1086         pblk_luns_free(pblk);
1087 fail:
1088         kfree(pblk);
1089         return ERR_PTR(ret);
1090 }
1091
1092 /* physical block device target */
1093 static struct nvm_tgt_type tt_pblk = {
1094         .name           = "pblk",
1095         .version        = {1, 0, 0},
1096
1097         .make_rq        = pblk_make_rq,
1098         .capacity       = pblk_capacity,
1099
1100         .init           = pblk_init,
1101         .exit           = pblk_exit,
1102
1103         .sysfs_init     = pblk_sysfs_init,
1104         .sysfs_exit     = pblk_sysfs_exit,
1105         .owner          = THIS_MODULE,
1106 };
1107
1108 static int __init pblk_module_init(void)
1109 {
1110         int ret;
1111
1112         pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
1113         if (!pblk_bio_set)
1114                 return -ENOMEM;
1115         ret = nvm_register_tgt_type(&tt_pblk);
1116         if (ret)
1117                 bioset_free(pblk_bio_set);
1118         return ret;
1119 }
1120
1121 static void pblk_module_exit(void)
1122 {
1123         bioset_free(pblk_bio_set);
1124         nvm_unregister_tgt_type(&tt_pblk);
1125 }
1126
1127 module_init(pblk_module_init);
1128 module_exit(pblk_module_exit);
1129 MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1130 MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1131 MODULE_LICENSE("GPL v2");
1132 MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");