Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / drivers / lightnvm / rrpc.h
CommitLineData
ae1519ec
MB
1/*
2 * Copyright (C) 2015 IT University of Copenhagen
3 * Initial release: Matias Bjorling <m@bjorling.me>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version
7 * 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * Implementation of a Round-robin page-based Hybrid FTL for Open-channel SSDs.
15 */
16
17#ifndef RRPC_H_
18#define RRPC_H_
19
20#include <linux/blkdev.h>
21#include <linux/blk-mq.h>
22#include <linux/bio.h>
23#include <linux/module.h>
24#include <linux/kthread.h>
25#include <linux/vmalloc.h>
26
27#include <linux/lightnvm.h>
28
29/* Run only GC if less than 1/X blocks are free */
30#define GC_LIMIT_INVERSE 10
31#define GC_TIME_SECS 100
32
33#define RRPC_SECTOR (512)
34#define RRPC_EXPOSED_PAGE_SIZE (4096)
35
36#define NR_PHY_IN_LOG (RRPC_EXPOSED_PAGE_SIZE / RRPC_SECTOR)
37
38struct rrpc_inflight {
39 struct list_head reqs;
40 spinlock_t lock;
41};
42
43struct rrpc_inflight_rq {
44 struct list_head list;
45 sector_t l_start;
46 sector_t l_end;
47};
48
49struct rrpc_rq {
50 struct rrpc_inflight_rq inflight_rq;
51 struct rrpc_addr *addr;
52 unsigned long flags;
53};
54
55struct rrpc_block {
56 struct nvm_block *parent;
d7a64d27 57 struct rrpc_lun *rlun;
ae1519ec 58 struct list_head prio;
ff0e498b 59 struct list_head list;
ae1519ec
MB
60
61#define MAX_INVALID_PAGES_STORAGE 8
62 /* Bitmap for invalid page intries */
63 unsigned long invalid_pages[MAX_INVALID_PAGES_STORAGE];
64 /* points to the next writable page within a block */
65 unsigned int next_page;
66 /* number of pages that are invalid, wrt host page size */
67 unsigned int nr_invalid_pages;
68
69 spinlock_t lock;
70 atomic_t data_cmnt_size; /* data pages committed to stable storage */
71};
72
73struct rrpc_lun {
74 struct rrpc *rrpc;
75 struct nvm_lun *parent;
76 struct rrpc_block *cur, *gc_cur;
77 struct rrpc_block *blocks; /* Reference to block allocation */
ff0e498b
JG
78
79 struct list_head prio_list; /* Blocks that may be GC'ed */
80 struct list_head open_list; /* In-use open blocks. These are blocks
81 * that can be both written to and read
82 * from
83 */
84 struct list_head closed_list; /* In-use closed blocks. These are
85 * blocks that can _only_ be read from
86 */
87
ae1519ec
MB
88 struct work_struct ws_gc;
89
90 spinlock_t lock;
91};
92
93struct rrpc {
94 /* instance must be kept in top to resolve rrpc in unprep */
95 struct nvm_tgt_instance instance;
96
97 struct nvm_dev *dev;
98 struct gendisk *disk;
99
b7ceb7d5 100 u64 poffset; /* physical page offset */
ae1519ec
MB
101 int lun_offset;
102
103 int nr_luns;
104 struct rrpc_lun *luns;
105
106 /* calculated values */
107 unsigned long long nr_pages;
108 unsigned long total_blocks;
109
110 /* Write strategy variables. Move these into each for structure for each
111 * strategy
112 */
113 atomic_t next_lun; /* Whenever a page is written, this is updated
114 * to point to the next write lun
115 */
116
117 spinlock_t bio_lock;
118 struct bio_list requeue_bios;
119 struct work_struct ws_requeue;
120
121 /* Simple translation map of logical addresses to physical addresses.
122 * The logical addresses is known by the host system, while the physical
123 * addresses are used when writing to the disk block device.
124 */
125 struct rrpc_addr *trans_map;
126 /* also store a reverse map for garbage collection */
127 struct rrpc_rev_addr *rev_trans_map;
128 spinlock_t rev_lock;
129
130 struct rrpc_inflight inflights;
131
132 mempool_t *addr_pool;
133 mempool_t *page_pool;
134 mempool_t *gcb_pool;
135 mempool_t *rq_pool;
136
137 struct timer_list gc_timer;
138 struct workqueue_struct *krqd_wq;
139 struct workqueue_struct *kgc_wq;
140};
141
142struct rrpc_block_gc {
143 struct rrpc *rrpc;
144 struct rrpc_block *rblk;
145 struct work_struct ws_gc;
146};
147
148/* Logical to physical mapping */
149struct rrpc_addr {
b7ceb7d5 150 u64 addr;
ae1519ec
MB
151 struct rrpc_block *rblk;
152};
153
154/* Physical to logical mapping */
155struct rrpc_rev_addr {
b7ceb7d5 156 u64 addr;
ae1519ec
MB
157};
158
159static inline sector_t rrpc_get_laddr(struct bio *bio)
160{
161 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
162}
163
164static inline unsigned int rrpc_get_pages(struct bio *bio)
165{
166 return bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
167}
168
169static inline sector_t rrpc_get_sector(sector_t laddr)
170{
171 return laddr * NR_PHY_IN_LOG;
172}
173
174static inline int request_intersects(struct rrpc_inflight_rq *r,
175 sector_t laddr_start, sector_t laddr_end)
176{
3704e098 177 return (laddr_end >= r->l_start) && (laddr_start <= r->l_end);
ae1519ec
MB
178}
179
180static int __rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
181 unsigned pages, struct rrpc_inflight_rq *r)
182{
183 sector_t laddr_end = laddr + pages - 1;
184 struct rrpc_inflight_rq *rtmp;
185
bba7f40a
JG
186 WARN_ON(irqs_disabled());
187
ae1519ec
MB
188 spin_lock_irq(&rrpc->inflights.lock);
189 list_for_each_entry(rtmp, &rrpc->inflights.reqs, list) {
190 if (unlikely(request_intersects(rtmp, laddr, laddr_end))) {
191 /* existing, overlapping request, come back later */
192 spin_unlock_irq(&rrpc->inflights.lock);
193 return 1;
194 }
195 }
196
197 r->l_start = laddr;
198 r->l_end = laddr_end;
199
200 list_add_tail(&r->list, &rrpc->inflights.reqs);
201 spin_unlock_irq(&rrpc->inflights.lock);
202 return 0;
203}
204
205static inline int rrpc_lock_laddr(struct rrpc *rrpc, sector_t laddr,
206 unsigned pages,
207 struct rrpc_inflight_rq *r)
208{
209 BUG_ON((laddr + pages) > rrpc->nr_pages);
210
211 return __rrpc_lock_laddr(rrpc, laddr, pages, r);
212}
213
214static inline struct rrpc_inflight_rq *rrpc_get_inflight_rq(struct nvm_rq *rqd)
215{
216 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
217
218 return &rrqd->inflight_rq;
219}
220
221static inline int rrpc_lock_rq(struct rrpc *rrpc, struct bio *bio,
222 struct nvm_rq *rqd)
223{
224 sector_t laddr = rrpc_get_laddr(bio);
225 unsigned int pages = rrpc_get_pages(bio);
226 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
227
228 return rrpc_lock_laddr(rrpc, laddr, pages, r);
229}
230
231static inline void rrpc_unlock_laddr(struct rrpc *rrpc,
232 struct rrpc_inflight_rq *r)
233{
234 unsigned long flags;
235
236 spin_lock_irqsave(&rrpc->inflights.lock, flags);
237 list_del_init(&r->list);
238 spin_unlock_irqrestore(&rrpc->inflights.lock, flags);
239}
240
241static inline void rrpc_unlock_rq(struct rrpc *rrpc, struct nvm_rq *rqd)
242{
243 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
244 uint8_t pages = rqd->nr_pages;
245
246 BUG_ON((r->l_start + pages) > rrpc->nr_pages);
247
248 rrpc_unlock_laddr(rrpc, r);
249}
250
251#endif /* RRPC_H_ */