blob: c6f8841973a0cd15b1f5d81c3dfd17ae7e074b55 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Matias Bjorling <matias@cnexlabs.com>
5 * Write buffering: Javier Gonzalez <javier@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 */
19
20#ifndef PBLK_H_
21#define PBLK_H_
22
23#include <linux/blkdev.h>
24#include <linux/blk-mq.h>
25#include <linux/bio.h>
26#include <linux/module.h>
27#include <linux/kthread.h>
28#include <linux/vmalloc.h>
29#include <linux/crc32.h>
30#include <linux/uuid.h>
31
32#include <linux/lightnvm.h>
33
34/* Run only GC if less than 1/X blocks are free */
35#define GC_LIMIT_INVERSE 5
36#define GC_TIME_MSECS 1000
37
38#define PBLK_SECTOR (512)
39#define PBLK_EXPOSED_PAGE_SIZE (4096)
40#define PBLK_MAX_REQ_ADDRS (64)
41#define PBLK_MAX_REQ_ADDRS_PW (6)
42
Javier Gonzálezef576492017-06-26 11:57:28 +020043#define PBLK_NR_CLOSE_JOBS (4)
44
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045#define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
46
47#define PBLK_COMMAND_TIMEOUT_MS 30000
48
49/* Max 512 LUNs per device */
50#define PBLK_MAX_LUNS_BITMAP (4)
51
52#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
53
54#define pblk_for_each_lun(pblk, rlun, i) \
55 for ((i) = 0, rlun = &(pblk)->luns[0]; \
56 (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)])
57
Javier González0d880392017-10-13 14:46:08 +020058/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020059#define PBLK_GEN_WS_POOL_SIZE (2)
60
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020061enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020062 PBLK_READ = READ,
63 PBLK_WRITE = WRITE,/* Write from write buffer */
64 PBLK_WRITE_INT, /* Internal write - no write buffer */
65 PBLK_ERASE,
66};
67
68enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020069 /* IO Types */
70 PBLK_IOTYPE_USER = 1 << 0,
71 PBLK_IOTYPE_GC = 1 << 1,
72
73 /* Write buffer flags */
74 PBLK_FLUSH_ENTRY = 1 << 2,
75 PBLK_WRITTEN_DATA = 1 << 3,
76 PBLK_SUBMITTED_ENTRY = 1 << 4,
77 PBLK_WRITABLE_ENTRY = 1 << 5,
78};
79
80enum {
81 PBLK_BLK_ST_OPEN = 0x1,
82 PBLK_BLK_ST_CLOSED = 0x2,
83};
84
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020085struct pblk_sec_meta {
86 u64 reserved;
87 __le64 lba;
88};
89
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020090/* The number of GC lists and the rate-limiter states go together. This way the
91 * rate-limiter can dictate how much GC is needed based on resource utilization.
92 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020093#define PBLK_GC_NR_LISTS 3
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020094
95enum {
96 PBLK_RL_HIGH = 1,
97 PBLK_RL_MID = 2,
98 PBLK_RL_LOW = 3,
99};
100
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200101#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200102#define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103
Javier González084ec9b2017-06-26 16:27:13 -0600104/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200105struct pblk_c_ctx {
106 struct list_head list; /* Head for out-of-order completion */
107
108 unsigned long *lun_bitmap; /* Luns used on current request */
109 unsigned int sentry;
110 unsigned int nr_valid;
111 unsigned int nr_padded;
112};
113
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200114/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600115struct pblk_g_ctx {
116 void *private;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200117 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200118};
119
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200120/* Pad context */
121struct pblk_pad_rq {
122 struct pblk *pblk;
123 struct completion wait;
124 struct kref ref;
125};
126
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200127/* Recovery context */
128struct pblk_rec_ctx {
129 struct pblk *pblk;
130 struct nvm_rq *rqd;
131 struct list_head failed;
132 struct work_struct ws_rec;
133};
134
135/* Write context */
136struct pblk_w_ctx {
137 struct bio_list bios; /* Original bios - used for completion
138 * in REQ_FUA, REQ_FLUSH case
139 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200140 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200141 struct ppa_addr ppa; /* Physic addr. associated with entry */
142 int flags; /* Write context flags */
143};
144
145struct pblk_rb_entry {
146 struct ppa_addr cacheline; /* Cacheline for this entry */
147 void *data; /* Pointer to data on this entry */
148 struct pblk_w_ctx w_ctx; /* Context for this entry */
149 struct list_head index; /* List head to enable indexes */
150};
151
152#define EMPTY_ENTRY (~0U)
153
154struct pblk_rb_pages {
155 struct page *pages;
156 int order;
157 struct list_head list;
158};
159
160struct pblk_rb {
161 struct pblk_rb_entry *entries; /* Ring buffer entries */
162 unsigned int mem; /* Write offset - points to next
163 * writable entry in memory
164 */
165 unsigned int subm; /* Read offset - points to last entry
166 * that has been submitted to the media
167 * to be persisted
168 */
169 unsigned int sync; /* Synced - backpointer that signals
170 * the last submitted entry that has
171 * been successfully persisted to media
172 */
173 unsigned int sync_point; /* Sync point - last entry that must be
174 * flushed to the media. Used with
175 * REQ_FLUSH and REQ_FUA
176 */
177 unsigned int l2p_update; /* l2p update point - next entry for
178 * which l2p mapping will be updated to
179 * contain a device ppa address (instead
180 * of a cacheline
181 */
182 unsigned int nr_entries; /* Number of entries in write buffer -
183 * must be a power of two
184 */
185 unsigned int seg_size; /* Size of the data segments being
186 * stored on each entry. Typically this
187 * will be 4KB
188 */
189
190 struct list_head pages; /* List of data pages */
191
192 spinlock_t w_lock; /* Write lock */
193 spinlock_t s_lock; /* Sync lock */
194
195#ifdef CONFIG_NVM_DEBUG
196 atomic_t inflight_sync_point; /* Not served REQ_FLUSH | REQ_FUA */
197#endif
198};
199
200#define PBLK_RECOVERY_SECTORS 16
201
202struct pblk_lun {
203 struct ppa_addr bppa;
204
205 u8 *bb_list; /* Bad block list for LUN. Only used on
206 * bring up. Bad blocks are managed
207 * within lines on run-time.
208 */
209
210 struct semaphore wr_sem;
211};
212
213struct pblk_gc_rq {
214 struct pblk_line *line;
215 void *data;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200216 u64 paddr_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200217 u64 lba_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200218 int nr_secs;
219 int secs_to_gc;
220 struct list_head list;
221};
222
223struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200224 /* These states are not protected by a lock since (i) they are in the
225 * fast path, and (ii) they are not critical.
226 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200227 int gc_active;
228 int gc_enabled;
229 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200230
231 struct task_struct *gc_ts;
232 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200233 struct task_struct *gc_reader_ts;
234
235 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200236 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200237
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 struct timer_list gc_timer;
239
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200240 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200241 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
242 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
243 * started reads to finished writes
244 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200246
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200247 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200248 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200249
250 spinlock_t lock;
251 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200252 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253};
254
255struct pblk_rl {
256 unsigned int high; /* Upper threshold for rate limiter (free run -
257 * user I/O rate limiter
258 */
259 unsigned int low; /* Lower threshold for rate limiter (user I/O
260 * rate limiter - stall)
261 */
262 unsigned int high_pw; /* High rounded up as a power of 2 */
263
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200264#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
265#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200266
267 int rb_windows_pw; /* Number of rate windows in the write buffer
268 * given as a power-of-2. This guarantees that
269 * when user I/O is being rate limited, there
270 * will be reserved enough space for the GC to
271 * place its payload. A window is of
272 * pblk->max_write_pgs size, which in NVMe is
273 * 64, i.e., 256kb.
274 */
275 int rb_budget; /* Total number of entries available for I/O */
276 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200277 int rb_gc_max; /* Max buffer entries available for GC I/O */
278 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
279 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200280 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200281
282 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200283 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200284 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200285
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200286 int rsv_blocks; /* Reserved blocks for GC */
287
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200289 int rb_gc_active;
290
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200291 struct timer_list u_timer;
292
293 unsigned long long nr_secs;
294 unsigned long total_blocks;
295 atomic_t free_blocks;
296};
297
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298#define PBLK_LINE_EMPTY (~0U)
299
300enum {
301 /* Line Types */
302 PBLK_LINETYPE_FREE = 0,
303 PBLK_LINETYPE_LOG = 1,
304 PBLK_LINETYPE_DATA = 2,
305
306 /* Line state */
307 PBLK_LINESTATE_FREE = 10,
308 PBLK_LINESTATE_OPEN = 11,
309 PBLK_LINESTATE_CLOSED = 12,
310 PBLK_LINESTATE_GC = 13,
311 PBLK_LINESTATE_BAD = 14,
312 PBLK_LINESTATE_CORRUPT = 15,
313
314 /* GC group */
315 PBLK_LINEGC_NONE = 20,
316 PBLK_LINEGC_EMPTY = 21,
317 PBLK_LINEGC_LOW = 22,
318 PBLK_LINEGC_MID = 23,
319 PBLK_LINEGC_HIGH = 24,
320 PBLK_LINEGC_FULL = 25,
321};
322
323#define PBLK_MAGIC 0x70626c6b /*pblk*/
Rakesh Panditc79819b2017-10-13 14:45:59 +0200324#define SMETA_VERSION cpu_to_le16(1)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325
326struct line_header {
327 __le32 crc;
328 __le32 identifier; /* pblk identifier */
329 __u8 uuid[16]; /* instance uuid */
330 __le16 type; /* line type */
331 __le16 version; /* type version */
332 __le32 id; /* line id for current line */
333};
334
335struct line_smeta {
336 struct line_header header;
337
338 __le32 crc; /* Full structure including struct crc */
339 /* Previous line metadata */
340 __le32 prev_id; /* Line id for previous line */
341
342 /* Current line metadata */
343 __le64 seq_nr; /* Sequence number for current line */
344
345 /* Active writers */
346 __le32 window_wr_lun; /* Number of parallel LUNs to write */
347
348 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200349
350 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200351};
352
353/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200354 * Metadata layout in media:
355 * First sector:
356 * 1. struct line_emeta
357 * 2. bad block bitmap (u64 * window_wr_lun)
358 * Mid sectors (start at lbas_sector):
359 * 3. nr_lbas (u64) forming lba list
360 * Last sectors (start at vsc_sector):
361 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362 */
363struct line_emeta {
364 struct line_header header;
365
366 __le32 crc; /* Full structure including struct crc */
367
368 /* Previous line metadata */
369 __le32 prev_id; /* Line id for prev line */
370
371 /* Current line metadata */
372 __le64 seq_nr; /* Sequence number for current line */
373
374 /* Active writers */
375 __le32 window_wr_lun; /* Number of parallel LUNs to write */
376
377 /* Bookkeeping for recovery */
378 __le32 next_id; /* Line id for next line */
379 __le64 nr_lbas; /* Number of lbas mapped in line */
380 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200381 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
382};
383
384struct pblk_emeta {
385 struct line_emeta *buf; /* emeta buffer in media format */
386 int mem; /* Write offset - points to next
387 * writable entry in memory
388 */
389 atomic_t sync; /* Synced - backpointer that signals the
390 * last entry that has been successfully
391 * persisted to media
392 */
393 unsigned int nr_entries; /* Number of emeta entries */
394};
395
396struct pblk_smeta {
397 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398};
399
400struct pblk_line {
401 struct pblk *pblk;
402 unsigned int id; /* Line number corresponds to the
403 * block line
404 */
405 unsigned int seq_nr; /* Unique line sequence number */
406
407 int state; /* PBLK_LINESTATE_X */
408 int type; /* PBLK_LINETYPE_X */
409 int gc_group; /* PBLK_LINEGC_X */
410 struct list_head list; /* Free, GC lists */
411
412 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
413
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200414 struct pblk_smeta *smeta; /* Start metadata */
415 struct pblk_emeta *emeta; /* End medatada */
416
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200417 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200418 int meta_distance; /* Distance between data and metadata */
419
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200420 u64 smeta_ssec; /* Sector where smeta starts */
421 u64 emeta_ssec; /* Sector where emeta starts */
422
423 unsigned int sec_in_line; /* Number of usable secs in line */
424
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200425 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
427 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
428
429 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
430 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
431
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200432 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433 atomic_t left_seblks; /* Blocks left for sync erasing */
434
435 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200437 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
438
439 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200440
441 struct kref ref; /* Write buffer L2P references */
442
443 spinlock_t lock; /* Necessary for invalid_bitmap only */
444};
445
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200446#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200448enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449 PBLK_KMALLOC_META = 1,
450 PBLK_VMALLOC_META = 2,
451};
452
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200453enum {
454 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
455 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
456 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200457};
458
459struct pblk_line_mgmt {
460 int nr_lines; /* Total number of full lines */
461 int nr_free_lines; /* Number of full lines in free list */
462
463 /* Free lists - use free_lock */
464 struct list_head free_list; /* Full lines ready to use */
465 struct list_head corrupt_list; /* Full lines corrupted */
466 struct list_head bad_list; /* Full lines bad */
467
468 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200469 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
471 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
472 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
473
474 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
475 struct list_head gc_empty_list; /* Full lines close, all valid */
476
477 struct pblk_line *log_line; /* Current FTL log line */
478 struct pblk_line *data_line; /* Current data line */
479 struct pblk_line *log_next; /* Next FTL log line */
480 struct pblk_line *data_next; /* Next data line */
481
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200482 struct list_head emeta_list; /* Lines queued to schedule emeta */
483
484 __le32 *vsc_list; /* Valid sector counts for all lines */
485
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200486 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487 int emeta_alloc_type;
488
489 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200490 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
491 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492 unsigned long meta_bitmap;
493
494 /* Helpers for fast bitmap calculations */
495 unsigned long *bb_template;
496 unsigned long *bb_aux;
497
498 unsigned long d_seq_nr; /* Data line unique sequence number */
499 unsigned long l_seq_nr; /* Log line unique sequence number */
500
501 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200502 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200503 spinlock_t gc_lock;
504};
505
506struct pblk_line_meta {
507 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200508 unsigned int smeta_sec; /* Sectors needed for smeta */
509
510 unsigned int emeta_len[4]; /* Lengths for emeta:
511 * [0]: Total length
512 * [1]: struct line_emeta length
513 * [2]: L2P portion length
514 * [3]: vsc list length
515 */
516 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
517 * as emeta_len
518 */
519
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200520 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200521
522 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
524 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
525 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
526
527 unsigned int blk_per_line; /* Number of blocks in a full line */
528 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200529 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200530 unsigned int min_blk_line; /* Min. number of good blocks in line */
531
532 unsigned int mid_thrs; /* Threshold for GC mid list */
533 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200534
535 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536};
537
538struct pblk_addr_format {
539 u64 ch_mask;
540 u64 lun_mask;
541 u64 pln_mask;
542 u64 blk_mask;
543 u64 pg_mask;
544 u64 sec_mask;
545 u8 ch_offset;
546 u8 lun_offset;
547 u8 pln_offset;
548 u8 blk_offset;
549 u8 pg_offset;
550 u8 sec_offset;
551};
552
Javier González588726d32017-06-26 11:57:29 +0200553enum {
554 PBLK_STATE_RUNNING = 0,
555 PBLK_STATE_STOPPING = 1,
556 PBLK_STATE_RECOVERING = 2,
557 PBLK_STATE_STOPPED = 3,
558};
559
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200560struct pblk {
561 struct nvm_tgt_dev *dev;
562 struct gendisk *disk;
563
564 struct kobject kobj;
565
566 struct pblk_lun *luns;
567
568 struct pblk_line *lines; /* Line array */
569 struct pblk_line_mgmt l_mg; /* Line management */
570 struct pblk_line_meta lm; /* Line metadata */
571
572 int ppaf_bitsize;
573 struct pblk_addr_format ppaf;
574
575 struct pblk_rb rwb;
576
Javier González588726d32017-06-26 11:57:29 +0200577 int state; /* pblk line state */
578
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579 int min_write_pgs; /* Minimum amount of pages required by controller */
580 int max_write_pgs; /* Maximum amount of pages supported by controller */
581 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
582 * guarantee successful reads.
583 */
584
585 sector_t capacity; /* Device capacity when bad blocks are subtracted */
586 int over_pct; /* Percentage of device used for over-provisioning */
587
588 /* pblk provisioning values. Used by rate limiter */
589 struct pblk_rl rl;
590
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200591 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592
593 unsigned char instance_uuid[16];
594#ifdef CONFIG_NVM_DEBUG
595 /* All debug counters apply to 4kb sector I/Os */
596 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
597 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
598 atomic_long_t padded_wb; /* Sectors padded in write buffer */
599 atomic_long_t nr_flush; /* Number of flush/fua I/O */
600 atomic_long_t req_writes; /* Sectors stored on write buffer */
601 atomic_long_t sub_writes; /* Sectors submitted from buffer */
602 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200604 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605 atomic_long_t sync_reads; /* Completed sector read requests */
606 atomic_long_t recov_writes; /* Sectors submitted from recovery */
607 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
608 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
609#endif
610
611 spinlock_t lock;
612
613 atomic_long_t read_failed;
614 atomic_long_t read_empty;
615 atomic_long_t read_high_ecc;
616 atomic_long_t read_failed_gc;
617 atomic_long_t write_failed;
618 atomic_long_t erase_failed;
619
Javier González588726d32017-06-26 11:57:29 +0200620 atomic_t inflight_io; /* General inflight I/O counter */
621
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200622 struct task_struct *writer_ts;
623
624 /* Simple translation map of logical addresses to physical addresses.
625 * The logical addresses is known by the host system, while the physical
626 * addresses are used when writing to the disk block device.
627 */
628 unsigned char *trans_map;
629 spinlock_t trans_lock;
630
631 struct list_head compl_list;
632
Javier Gonzálezbd432412017-10-13 14:46:06 +0200633 mempool_t *page_bio_pool;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200634 mempool_t *gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 mempool_t *rec_pool;
Javier González0d880392017-10-13 14:46:08 +0200636 mempool_t *r_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200637 mempool_t *w_rq_pool;
Javier González0d880392017-10-13 14:46:08 +0200638 mempool_t *e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639
Javier Gonzálezef576492017-06-26 11:57:28 +0200640 struct workqueue_struct *close_wq;
641 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200642 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200643
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200644 struct timer_list wtimer;
645
646 struct pblk_gc gc;
647};
648
649struct pblk_line_ws {
650 struct pblk *pblk;
651 struct pblk_line *line;
652 void *priv;
653 struct work_struct ws;
654};
655
Javier González084ec9b2017-06-26 16:27:13 -0600656#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
658
659/*
660 * pblk ring buffer operations
661 */
662int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
663 unsigned int power_size, unsigned int power_seg_sz);
664unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
665void *pblk_rb_entries_ref(struct pblk_rb *rb);
666int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
667 unsigned int nr_entries, unsigned int *pos);
668int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
669 unsigned int *pos);
670void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
671 struct pblk_w_ctx w_ctx, unsigned int pos);
672void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200673 struct pblk_w_ctx w_ctx, struct pblk_line *line,
674 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200675struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200676void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200677
678void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200679unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200680 unsigned int pos, unsigned int nr_entries,
681 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200682unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
683 struct list_head *list,
684 unsigned int max);
685int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200686 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200687unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
688
689unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
690unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
691struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
692 struct ppa_addr *ppa);
693void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
694unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb);
695
696unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200697unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200698unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
699
700int pblk_rb_tear_down_check(struct pblk_rb *rb);
701int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
702void pblk_rb_data_free(struct pblk_rb *rb);
703ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
704
705/*
706 * pblk core
707 */
Javier González67bf26a2017-10-13 14:46:20 +0200708struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
709void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200710void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200711int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
712 struct pblk_c_ctx *c_ctx);
Javier González588726d32017-06-26 11:57:29 +0200713void pblk_wait_for_meta(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200714struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba);
715void pblk_discard(struct pblk *pblk, struct bio *bio);
716void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
717void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
718int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200719int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200720struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
721 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200722 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200723struct pblk_line *pblk_line_get(struct pblk *pblk);
724struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200725struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200726int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
727void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
728struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200729struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200730int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
731int pblk_line_is_full(struct pblk_line *line);
732void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200733void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200734void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier González588726d32017-06-26 11:57:29 +0200735void pblk_line_close_meta_sync(struct pblk *pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200736void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200737void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200738void pblk_line_mark_bb(struct work_struct *work);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200739void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
740 void (*work)(struct work_struct *), gfp_t gfp_mask,
741 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200742u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
743int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200744int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
745 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
747void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200748void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200749struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200750u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
751void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200752u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200753u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200754int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
755 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200756void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
758 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200759void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200760void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
761 unsigned long *lun_bitmap);
762void pblk_end_bio_sync(struct bio *bio);
763void pblk_end_io_sync(struct nvm_rq *rqd);
764int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
765 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200766void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
767 int nr_pages);
768void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200769void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
770 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200771void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
772void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
773 struct ppa_addr ppa);
774void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
775 struct ppa_addr ppa, struct ppa_addr entry_line);
776int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200777 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200778void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
779 u64 *lba_list, int nr_secs);
780void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
781 sector_t blba, int nr_secs);
782
783/*
784 * pblk user I/O write path
785 */
786int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
787 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200788int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789
790/*
791 * pblk map
792 */
793void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
794 unsigned int sentry, unsigned long *lun_bitmap,
795 unsigned int valid_secs, struct ppa_addr *erase_ppa);
796void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
797 unsigned long *lun_bitmap, unsigned int valid_secs,
798 unsigned int off);
799
800/*
801 * pblk write thread
802 */
803int pblk_write_ts(void *data);
804void pblk_write_timer_fn(unsigned long data);
805void pblk_write_should_kick(struct pblk *pblk);
806
807/*
808 * pblk read path
809 */
NeilBrownb25d5232017-06-18 14:38:58 +1000810extern struct bio_set *pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200811int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200812int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200813/*
814 * pblk recovery
815 */
816void pblk_submit_rec(struct work_struct *work);
817struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200818int pblk_recov_pad(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200819__le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta);
820int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
821 struct pblk_rec_ctx *recovery, u64 *comp_bits,
822 unsigned int comp);
823
824/*
825 * pblk gc
826 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200827#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200828#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200829#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
830#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200831
832int pblk_gc_init(struct pblk *pblk);
833void pblk_gc_exit(struct pblk *pblk);
834void pblk_gc_should_start(struct pblk *pblk);
835void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200836void pblk_gc_should_kick(struct pblk *pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200837void pblk_gc_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200838void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200839void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
840 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200841int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842
843/*
844 * pblk rate limiter
845 */
846void pblk_rl_init(struct pblk_rl *rl, int budget);
847void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200848void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200849int pblk_rl_high_thrs(struct pblk_rl *rl);
850int pblk_rl_low_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200851unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
852int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200853void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
855int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
856void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
857void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200858int pblk_rl_sysfs_rate_show(struct pblk_rl *rl);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200859int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200860void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
861void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line);
Javier González588726d32017-06-26 11:57:29 +0200862void pblk_rl_set_space_limit(struct pblk_rl *rl, int entries_left);
863int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200864
865/*
866 * pblk sysfs
867 */
868int pblk_sysfs_init(struct gendisk *tdisk);
869void pblk_sysfs_exit(struct gendisk *tdisk);
870
871static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
872{
873 if (type == PBLK_KMALLOC_META)
874 return kmalloc(size, flags);
875 return vmalloc(size);
876}
877
878static inline void pblk_mfree(void *ptr, int type)
879{
880 if (type == PBLK_KMALLOC_META)
881 kfree(ptr);
882 else
883 vfree(ptr);
884}
885
886static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
887{
888 return c_ctx - sizeof(struct nvm_rq);
889}
890
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200891static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200893 return emeta->bb_bitmap;
894}
895
896static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
897{
898 return ((void *)emeta + pblk->lm.emeta_len[1]);
899}
900
901static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
902{
903 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200904}
905
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200906static inline int pblk_line_vsc(struct pblk_line *line)
907{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200908 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200909}
910
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200911#define NVM_MEM_PAGE_WRITE (8)
912
913static inline int pblk_pad_distance(struct pblk *pblk)
914{
915 struct nvm_tgt_dev *dev = pblk->dev;
916 struct nvm_geo *geo = &dev->geo;
917
918 return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl;
919}
920
921static inline int pblk_dev_ppa_to_line(struct ppa_addr p)
922{
923 return p.g.blk;
924}
925
926static inline int pblk_tgt_ppa_to_line(struct ppa_addr p)
927{
928 return p.g.blk;
929}
930
931static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
932{
933 return p.g.lun * geo->nr_chnls + p.g.ch;
934}
935
936/* A block within a line corresponds to the lun */
937static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
938{
939 return p.g.lun * geo->nr_chnls + p.g.ch;
940}
941
942static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
943{
944 struct ppa_addr ppa64;
945
946 ppa64.ppa = 0;
947
948 if (ppa32 == -1) {
949 ppa64.ppa = ADDR_EMPTY;
950 } else if (ppa32 & (1U << 31)) {
951 ppa64.c.line = ppa32 & ((~0U) >> 1);
952 ppa64.c.is_cached = 1;
953 } else {
954 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
955 pblk->ppaf.blk_offset;
956 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
957 pblk->ppaf.pg_offset;
958 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
959 pblk->ppaf.lun_offset;
960 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
961 pblk->ppaf.ch_offset;
962 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
963 pblk->ppaf.pln_offset;
964 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
965 pblk->ppaf.sec_offset;
966 }
967
968 return ppa64;
969}
970
971static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
972 sector_t lba)
973{
974 struct ppa_addr ppa;
975
976 if (pblk->ppaf_bitsize < 32) {
977 u32 *map = (u32 *)pblk->trans_map;
978
979 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
980 } else {
981 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
982
983 ppa = map[lba];
984 }
985
986 return ppa;
987}
988
989static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
990{
991 u32 ppa32 = 0;
992
993 if (ppa64.ppa == ADDR_EMPTY) {
994 ppa32 = ~0U;
995 } else if (ppa64.c.is_cached) {
996 ppa32 |= ppa64.c.line;
997 ppa32 |= 1U << 31;
998 } else {
999 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
1000 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
1001 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
1002 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
1003 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
1004 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
1005 }
1006
1007 return ppa32;
1008}
1009
1010static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1011 struct ppa_addr ppa)
1012{
1013 if (pblk->ppaf_bitsize < 32) {
1014 u32 *map = (u32 *)pblk->trans_map;
1015
1016 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1017 } else {
1018 u64 *map = (u64 *)pblk->trans_map;
1019
1020 map[lba] = ppa.ppa;
1021 }
1022}
1023
1024static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1025 struct ppa_addr p)
1026{
1027 u64 paddr;
1028
1029 paddr = 0;
1030 paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset;
1031 paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
1032 paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
1033 paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
1034 paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
1035
1036 return paddr;
1037}
1038
1039static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1040{
1041 return (ppa_addr.ppa == ADDR_EMPTY);
1042}
1043
1044static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1045{
1046 ppa_addr->ppa = ADDR_EMPTY;
1047}
1048
Javier González076984662017-06-30 17:56:42 +02001049static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1050{
1051 if (lppa.ppa == rppa.ppa)
1052 return true;
1053
1054 return false;
1055}
1056
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001057static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1058{
1059 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1060}
1061
1062static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1063{
1064 return ppa.c.line;
1065}
1066
1067static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1068{
1069 struct ppa_addr p;
1070
1071 p.c.line = addr;
1072 p.c.is_cached = 1;
1073
1074 return p;
1075}
1076
1077static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1078 u64 line_id)
1079{
1080 struct ppa_addr ppa;
1081
1082 ppa.ppa = 0;
1083 ppa.g.blk = line_id;
1084 ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
1085 ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
1086 ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
1087 ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
1088 ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
1089
1090 return ppa;
1091}
1092
1093static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr,
1094 u64 line_id)
1095{
1096 struct ppa_addr ppa;
1097
1098 ppa = addr_to_gen_ppa(pblk, paddr, line_id);
1099
1100 return ppa;
1101}
1102
1103static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001104 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105{
1106 u32 crc = ~(u32)0;
1107
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001108 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001109 sizeof(struct line_header) - sizeof(crc));
1110
1111 return crc;
1112}
1113
1114static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1115 struct line_smeta *smeta)
1116{
1117 struct pblk_line_meta *lm = &pblk->lm;
1118 u32 crc = ~(u32)0;
1119
1120 crc = crc32_le(crc, (unsigned char *)smeta +
1121 sizeof(struct line_header) + sizeof(crc),
1122 lm->smeta_len -
1123 sizeof(struct line_header) - sizeof(crc));
1124
1125 return crc;
1126}
1127
1128static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1129 struct line_emeta *emeta)
1130{
1131 struct pblk_line_meta *lm = &pblk->lm;
1132 u32 crc = ~(u32)0;
1133
1134 crc = crc32_le(crc, (unsigned char *)emeta +
1135 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001136 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137 sizeof(struct line_header) - sizeof(crc));
1138
1139 return crc;
1140}
1141
1142static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1143{
1144 struct nvm_tgt_dev *dev = pblk->dev;
1145 struct nvm_geo *geo = &dev->geo;
1146 int flags;
1147
1148 flags = geo->plane_mode >> 1;
1149
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001150 if (type == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001151 flags |= NVM_IO_SCRAMBLE_ENABLE;
1152
1153 return flags;
1154}
1155
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001156enum {
1157 PBLK_READ_RANDOM = 0,
1158 PBLK_READ_SEQUENTIAL = 1,
1159};
1160
1161static inline int pblk_set_read_mode(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001162{
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001163 struct nvm_tgt_dev *dev = pblk->dev;
1164 struct nvm_geo *geo = &dev->geo;
1165 int flags;
1166
1167 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1168 if (type == PBLK_READ_SEQUENTIAL)
1169 flags |= geo->plane_mode >> 1;
1170
1171 return flags;
1172}
1173
1174static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1175{
1176 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001177}
1178
1179#ifdef CONFIG_NVM_DEBUG
1180static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1181{
1182 if (p->c.is_cached) {
1183 pr_err("ppa: (%s: %x) cache line: %llu\n",
1184 msg, error, (u64)p->c.line);
1185 } else {
1186 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1187 msg, error,
1188 p->g.ch, p->g.lun, p->g.blk,
1189 p->g.pg, p->g.pl, p->g.sec);
1190 }
1191}
1192
1193static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1194 int error)
1195{
1196 int bit = -1;
1197
1198 if (rqd->nr_ppas == 1) {
1199 print_ppa(&rqd->ppa_addr, "rqd", error);
1200 return;
1201 }
1202
1203 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1204 bit + 1)) < rqd->nr_ppas) {
1205 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1206 }
1207
1208 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1209}
1210#endif
1211
1212static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1213 struct ppa_addr *ppas, int nr_ppas)
1214{
1215 struct nvm_geo *geo = &tgt_dev->geo;
1216 struct ppa_addr *ppa;
1217 int i;
1218
1219 for (i = 0; i < nr_ppas; i++) {
1220 ppa = &ppas[i];
1221
1222 if (!ppa->c.is_cached &&
1223 ppa->g.ch < geo->nr_chnls &&
1224 ppa->g.lun < geo->luns_per_chnl &&
1225 ppa->g.pl < geo->nr_planes &&
1226 ppa->g.blk < geo->blks_per_lun &&
1227 ppa->g.pg < geo->pgs_per_blk &&
1228 ppa->g.sec < geo->sec_per_pg)
1229 continue;
1230
1231#ifdef CONFIG_NVM_DEBUG
1232 print_ppa(ppa, "boundary", i);
1233#endif
1234 return 1;
1235 }
1236 return 0;
1237}
1238
1239static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1240{
1241 struct pblk_line_meta *lm = &pblk->lm;
1242
1243 if (paddr > lm->sec_per_line)
1244 return 1;
1245
1246 return 0;
1247}
1248
1249static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1250{
1251 return bio->bi_iter.bi_idx;
1252}
1253
1254static inline sector_t pblk_get_lba(struct bio *bio)
1255{
1256 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1257}
1258
1259static inline unsigned int pblk_get_secs(struct bio *bio)
1260{
1261 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1262}
1263
1264static inline sector_t pblk_get_sector(sector_t lba)
1265{
1266 return lba * NR_PHY_IN_LOG;
1267}
1268
1269static inline void pblk_setup_uuid(struct pblk *pblk)
1270{
1271 uuid_le uuid;
1272
1273 uuid_le_gen(&uuid);
1274 memcpy(pblk->instance_uuid, uuid.b, 16);
1275}
1276#endif /* PBLK_H_ */