blob: 4b7d8618631f6460c158fed62e64e63bcf19dbc7 [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
Javier González0d880392017-10-13 14:46:08 +020054/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020055#define PBLK_GEN_WS_POOL_SIZE (2)
56
Javier Gonzáleze5392732018-01-05 14:16:14 +010057#define PBLK_DEFAULT_OP (11)
58
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020060 PBLK_READ = READ,
61 PBLK_WRITE = WRITE,/* Write from write buffer */
62 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010063 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020064 PBLK_ERASE,
65};
66
67enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 /* IO Types */
69 PBLK_IOTYPE_USER = 1 << 0,
70 PBLK_IOTYPE_GC = 1 << 1,
71
72 /* Write buffer flags */
73 PBLK_FLUSH_ENTRY = 1 << 2,
74 PBLK_WRITTEN_DATA = 1 << 3,
75 PBLK_SUBMITTED_ENTRY = 1 << 4,
76 PBLK_WRITABLE_ENTRY = 1 << 5,
77};
78
79enum {
80 PBLK_BLK_ST_OPEN = 0x1,
81 PBLK_BLK_ST_CLOSED = 0x2,
82};
83
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020084struct pblk_sec_meta {
85 u64 reserved;
86 __le64 lba;
87};
88
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020089/* The number of GC lists and the rate-limiter states go together. This way the
90 * rate-limiter can dictate how much GC is needed based on resource utilization.
91 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020092#define PBLK_GC_NR_LISTS 3
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093
94enum {
95 PBLK_RL_HIGH = 1,
96 PBLK_RL_MID = 2,
97 PBLK_RL_LOW = 3,
98};
99
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200101#define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200102
Javier González084ec9b2017-06-26 16:27:13 -0600103/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104struct pblk_c_ctx {
105 struct list_head list; /* Head for out-of-order completion */
106
107 unsigned long *lun_bitmap; /* Luns used on current request */
108 unsigned int sentry;
109 unsigned int nr_valid;
110 unsigned int nr_padded;
111};
112
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200113/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600114struct pblk_g_ctx {
115 void *private;
Javier González998ba622018-01-05 14:16:20 +0100116 unsigned long start_time;
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 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100173 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 * 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
Hans Holmberg8154d292018-01-05 14:16:09 +0100196 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197#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 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259 unsigned int high_pw; /* High rounded up as a power of 2 */
260
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200261#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
262#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263
264 int rb_windows_pw; /* Number of rate windows in the write buffer
265 * given as a power-of-2. This guarantees that
266 * when user I/O is being rate limited, there
267 * will be reserved enough space for the GC to
268 * place its payload. A window is of
269 * pblk->max_write_pgs size, which in NVMe is
270 * 64, i.e., 256kb.
271 */
272 int rb_budget; /* Total number of entries available for I/O */
273 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274 int rb_gc_max; /* Max buffer entries available for GC I/O */
275 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
276 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200277 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200278
279 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200280 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200281 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200283 int rsv_blocks; /* Reserved blocks for GC */
284
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200285 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200286 int rb_gc_active;
287
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288 struct timer_list u_timer;
289
290 unsigned long long nr_secs;
291 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100292
293 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
294 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200295};
296
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297#define PBLK_LINE_EMPTY (~0U)
298
299enum {
300 /* Line Types */
301 PBLK_LINETYPE_FREE = 0,
302 PBLK_LINETYPE_LOG = 1,
303 PBLK_LINETYPE_DATA = 2,
304
305 /* Line state */
306 PBLK_LINESTATE_FREE = 10,
307 PBLK_LINESTATE_OPEN = 11,
308 PBLK_LINESTATE_CLOSED = 12,
309 PBLK_LINESTATE_GC = 13,
310 PBLK_LINESTATE_BAD = 14,
311 PBLK_LINESTATE_CORRUPT = 15,
312
313 /* GC group */
314 PBLK_LINEGC_NONE = 20,
315 PBLK_LINEGC_EMPTY = 21,
316 PBLK_LINEGC_LOW = 22,
317 PBLK_LINEGC_MID = 23,
318 PBLK_LINEGC_HIGH = 24,
319 PBLK_LINEGC_FULL = 25,
320};
321
322#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200323
324/* emeta/smeta persistent storage format versions:
325 * Changes in major version requires offline migration.
326 * Changes in minor version are handled automatically during
327 * recovery.
328 */
329
330#define SMETA_VERSION_MAJOR (0)
331#define SMETA_VERSION_MINOR (1)
332
333#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200334#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200335
336struct line_header {
337 __le32 crc;
338 __le32 identifier; /* pblk identifier */
339 __u8 uuid[16]; /* instance uuid */
340 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200341 __u8 version_major; /* version major */
342 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200343 __le32 id; /* line id for current line */
344};
345
346struct line_smeta {
347 struct line_header header;
348
349 __le32 crc; /* Full structure including struct crc */
350 /* Previous line metadata */
351 __le32 prev_id; /* Line id for previous line */
352
353 /* Current line metadata */
354 __le64 seq_nr; /* Sequence number for current line */
355
356 /* Active writers */
357 __le32 window_wr_lun; /* Number of parallel LUNs to write */
358
359 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200360
361 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362};
363
Hans Holmberg76758392018-03-30 00:04:52 +0200364
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200365/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200366 * Metadata layout in media:
367 * First sector:
368 * 1. struct line_emeta
369 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200370 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200371 * Mid sectors (start at lbas_sector):
372 * 3. nr_lbas (u64) forming lba list
373 * Last sectors (start at vsc_sector):
374 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375 */
376struct line_emeta {
377 struct line_header header;
378
379 __le32 crc; /* Full structure including struct crc */
380
381 /* Previous line metadata */
382 __le32 prev_id; /* Line id for prev line */
383
384 /* Current line metadata */
385 __le64 seq_nr; /* Sequence number for current line */
386
387 /* Active writers */
388 __le32 window_wr_lun; /* Number of parallel LUNs to write */
389
390 /* Bookkeeping for recovery */
391 __le32 next_id; /* Line id for next line */
392 __le64 nr_lbas; /* Number of lbas mapped in line */
393 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200394 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
395};
396
397
398/* Write amplification counters stored on media */
399struct wa_counters {
400 __le64 user; /* Number of user written sectors */
401 __le64 gc; /* Number of sectors written by GC*/
402 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200403};
404
405struct pblk_emeta {
406 struct line_emeta *buf; /* emeta buffer in media format */
407 int mem; /* Write offset - points to next
408 * writable entry in memory
409 */
410 atomic_t sync; /* Synced - backpointer that signals the
411 * last entry that has been successfully
412 * persisted to media
413 */
414 unsigned int nr_entries; /* Number of emeta entries */
415};
416
417struct pblk_smeta {
418 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200419};
420
421struct pblk_line {
422 struct pblk *pblk;
423 unsigned int id; /* Line number corresponds to the
424 * block line
425 */
426 unsigned int seq_nr; /* Unique line sequence number */
427
428 int state; /* PBLK_LINESTATE_X */
429 int type; /* PBLK_LINETYPE_X */
430 int gc_group; /* PBLK_LINEGC_X */
431 struct list_head list; /* Free, GC lists */
432
433 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
434
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200435 struct pblk_smeta *smeta; /* Start metadata */
436 struct pblk_emeta *emeta; /* End medatada */
437
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200439 int meta_distance; /* Distance between data and metadata */
440
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200441 u64 smeta_ssec; /* Sector where smeta starts */
442 u64 emeta_ssec; /* Sector where emeta starts */
443
444 unsigned int sec_in_line; /* Number of usable secs in line */
445
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200446 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
448 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
449
450 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
451 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
452
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200453 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 atomic_t left_seblks; /* Blocks left for sync erasing */
455
456 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200457 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200458 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
459
460 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200461
462 struct kref ref; /* Write buffer L2P references */
463
464 spinlock_t lock; /* Necessary for invalid_bitmap only */
465};
466
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200467#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200469enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470 PBLK_KMALLOC_META = 1,
471 PBLK_VMALLOC_META = 2,
472};
473
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200474enum {
475 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
476 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
477 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478};
479
480struct pblk_line_mgmt {
481 int nr_lines; /* Total number of full lines */
482 int nr_free_lines; /* Number of full lines in free list */
483
484 /* Free lists - use free_lock */
485 struct list_head free_list; /* Full lines ready to use */
486 struct list_head corrupt_list; /* Full lines corrupted */
487 struct list_head bad_list; /* Full lines bad */
488
489 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200490 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200491 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
492 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
493 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
494
495 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
496 struct list_head gc_empty_list; /* Full lines close, all valid */
497
498 struct pblk_line *log_line; /* Current FTL log line */
499 struct pblk_line *data_line; /* Current data line */
500 struct pblk_line *log_next; /* Next FTL log line */
501 struct pblk_line *data_next; /* Next data line */
502
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200503 struct list_head emeta_list; /* Lines queued to schedule emeta */
504
505 __le32 *vsc_list; /* Valid sector counts for all lines */
506
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508 int emeta_alloc_type;
509
510 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200511 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
512 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513 unsigned long meta_bitmap;
514
515 /* Helpers for fast bitmap calculations */
516 unsigned long *bb_template;
517 unsigned long *bb_aux;
518
519 unsigned long d_seq_nr; /* Data line unique sequence number */
520 unsigned long l_seq_nr; /* Log line unique sequence number */
521
522 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200523 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 spinlock_t gc_lock;
525};
526
527struct pblk_line_meta {
528 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200529 unsigned int smeta_sec; /* Sectors needed for smeta */
530
531 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200532 * [0]: Total
533 * [1]: struct line_emeta +
534 * bb_bitmap + struct wa_counters
535 * [2]: L2P portion
536 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200537 */
538 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
539 * as emeta_len
540 */
541
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200543
544 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
546 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
547 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
548
549 unsigned int blk_per_line; /* Number of blocks in a full line */
550 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200551 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552 unsigned int min_blk_line; /* Min. number of good blocks in line */
553
554 unsigned int mid_thrs; /* Threshold for GC mid list */
555 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200556
557 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558};
559
560struct pblk_addr_format {
561 u64 ch_mask;
562 u64 lun_mask;
563 u64 pln_mask;
564 u64 blk_mask;
565 u64 pg_mask;
566 u64 sec_mask;
567 u8 ch_offset;
568 u8 lun_offset;
569 u8 pln_offset;
570 u8 blk_offset;
571 u8 pg_offset;
572 u8 sec_offset;
573};
574
Javier González588726d32017-06-26 11:57:29 +0200575enum {
576 PBLK_STATE_RUNNING = 0,
577 PBLK_STATE_STOPPING = 1,
578 PBLK_STATE_RECOVERING = 2,
579 PBLK_STATE_STOPPED = 3,
580};
581
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582struct pblk {
583 struct nvm_tgt_dev *dev;
584 struct gendisk *disk;
585
586 struct kobject kobj;
587
588 struct pblk_lun *luns;
589
590 struct pblk_line *lines; /* Line array */
591 struct pblk_line_mgmt l_mg; /* Line management */
592 struct pblk_line_meta lm; /* Line metadata */
593
594 int ppaf_bitsize;
595 struct pblk_addr_format ppaf;
596
597 struct pblk_rb rwb;
598
Javier González588726d32017-06-26 11:57:29 +0200599 int state; /* pblk line state */
600
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601 int min_write_pgs; /* Minimum amount of pages required by controller */
602 int max_write_pgs; /* Maximum amount of pages supported by controller */
603 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
604 * guarantee successful reads.
605 */
606
607 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100608
609 int op; /* Percentage of device used for over-provisioning */
610 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611
612 /* pblk provisioning values. Used by rate limiter */
613 struct pblk_rl rl;
614
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200615 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616
617 unsigned char instance_uuid[16];
Hans Holmberg76758392018-03-30 00:04:52 +0200618
619 /* Persistent write amplification counters, 4kb sector I/Os */
620 atomic64_t user_wa; /* Sectors written by user */
621 atomic64_t gc_wa; /* Sectors written by GC */
622 atomic64_t pad_wa; /* Padded sectors written */
623
624 /* Reset values for delta write amplification measurements */
625 u64 user_rst_wa;
626 u64 gc_rst_wa;
627 u64 pad_rst_wa;
628
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200629#ifdef CONFIG_NVM_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200630 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
632 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
633 atomic_long_t padded_wb; /* Sectors padded in write buffer */
634 atomic_long_t nr_flush; /* Number of flush/fua I/O */
635 atomic_long_t req_writes; /* Sectors stored on write buffer */
636 atomic_long_t sub_writes; /* Sectors submitted from buffer */
637 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200638 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200639 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200640 atomic_long_t sync_reads; /* Completed sector read requests */
641 atomic_long_t recov_writes; /* Sectors submitted from recovery */
642 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
643 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
644#endif
645
646 spinlock_t lock;
647
648 atomic_long_t read_failed;
649 atomic_long_t read_empty;
650 atomic_long_t read_high_ecc;
651 atomic_long_t read_failed_gc;
652 atomic_long_t write_failed;
653 atomic_long_t erase_failed;
654
Javier González588726d32017-06-26 11:57:29 +0200655 atomic_t inflight_io; /* General inflight I/O counter */
656
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657 struct task_struct *writer_ts;
658
659 /* Simple translation map of logical addresses to physical addresses.
660 * The logical addresses is known by the host system, while the physical
661 * addresses are used when writing to the disk block device.
662 */
663 unsigned char *trans_map;
664 spinlock_t trans_lock;
665
666 struct list_head compl_list;
667
Javier Gonzálezbd432412017-10-13 14:46:06 +0200668 mempool_t *page_bio_pool;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200669 mempool_t *gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200670 mempool_t *rec_pool;
Javier González0d880392017-10-13 14:46:08 +0200671 mempool_t *r_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 mempool_t *w_rq_pool;
Javier González0d880392017-10-13 14:46:08 +0200673 mempool_t *e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200674
Javier Gonzálezef576492017-06-26 11:57:28 +0200675 struct workqueue_struct *close_wq;
676 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200677 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200678
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 struct timer_list wtimer;
680
681 struct pblk_gc gc;
682};
683
684struct pblk_line_ws {
685 struct pblk *pblk;
686 struct pblk_line *line;
687 void *priv;
688 struct work_struct ws;
689};
690
Javier González084ec9b2017-06-26 16:27:13 -0600691#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
693
694/*
695 * pblk ring buffer operations
696 */
697int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
698 unsigned int power_size, unsigned int power_seg_sz);
699unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
700void *pblk_rb_entries_ref(struct pblk_rb *rb);
701int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
702 unsigned int nr_entries, unsigned int *pos);
703int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
704 unsigned int *pos);
705void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
706 struct pblk_w_ctx w_ctx, unsigned int pos);
707void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200708 struct pblk_w_ctx w_ctx, struct pblk_line *line,
709 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200710struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200711void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200712
713void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200714unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200715 unsigned int pos, unsigned int nr_entries,
716 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
718 struct list_head *list,
719 unsigned int max);
720int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200721 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200722unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
723
724unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
725unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
726struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
727 struct ppa_addr *ppa);
728void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100729unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200730
731unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200732unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200733unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
734
735int pblk_rb_tear_down_check(struct pblk_rb *rb);
736int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
737void pblk_rb_data_free(struct pblk_rb *rb);
738ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
739
740/*
741 * pblk core
742 */
Javier González67bf26a2017-10-13 14:46:20 +0200743struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
744void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200745void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
747 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200748void pblk_discard(struct pblk *pblk, struct bio *bio);
749void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
750void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
751int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200752int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200753int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200754struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
755 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200756 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757struct pblk_line *pblk_line_get(struct pblk *pblk);
758struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200759struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200760int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
761void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
762struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200763struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200764int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
765int pblk_line_is_full(struct pblk_line *line);
766void pblk_line_free(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200767void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200768void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200769void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200770void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200771void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
772 void (*work)(struct work_struct *), gfp_t gfp_mask,
773 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200774u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
775int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200776int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
777 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200778int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
779void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200780void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200781struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200782u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
783void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200784u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200785u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200786int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
787 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200788void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
790 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200791void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200792void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
793 unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794void pblk_end_io_sync(struct nvm_rq *rqd);
795int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
796 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200797void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
798 int nr_pages);
799void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200800void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
801 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200802void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
803void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
804 struct ppa_addr ppa);
805void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
806 struct ppa_addr ppa, struct ppa_addr entry_line);
807int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200808 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200809void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
810 u64 *lba_list, int nr_secs);
811void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
812 sector_t blba, int nr_secs);
813
814/*
815 * pblk user I/O write path
816 */
817int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
818 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200819int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200820
821/*
822 * pblk map
823 */
824void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
825 unsigned int sentry, unsigned long *lun_bitmap,
826 unsigned int valid_secs, struct ppa_addr *erase_ppa);
827void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
828 unsigned long *lun_bitmap, unsigned int valid_secs,
829 unsigned int off);
830
831/*
832 * pblk write thread
833 */
834int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700835void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836void pblk_write_should_kick(struct pblk *pblk);
837
838/*
839 * pblk read path
840 */
NeilBrownb25d5232017-06-18 14:38:58 +1000841extern struct bio_set *pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200843int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200844/*
845 * pblk recovery
846 */
847void pblk_submit_rec(struct work_struct *work);
848struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200849int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100850int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200851int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
852 struct pblk_rec_ctx *recovery, u64 *comp_bits,
853 unsigned int comp);
854
855/*
856 * pblk gc
857 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200858#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200859#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200860#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
861#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200862
863int pblk_gc_init(struct pblk *pblk);
864void pblk_gc_exit(struct pblk *pblk);
865void pblk_gc_should_start(struct pblk *pblk);
866void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200867void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200868void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
870 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200871int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200872
873/*
874 * pblk rate limiter
875 */
876void pblk_rl_init(struct pblk_rl *rl, int budget);
877void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200878void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200879int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200880unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100881unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200882int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200883void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200884void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
885int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
886void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
887void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200888int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200889void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100890void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
891 bool used);
Javier González588726d32017-06-26 11:57:29 +0200892int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893
894/*
895 * pblk sysfs
896 */
897int pblk_sysfs_init(struct gendisk *tdisk);
898void pblk_sysfs_exit(struct gendisk *tdisk);
899
900static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
901{
902 if (type == PBLK_KMALLOC_META)
903 return kmalloc(size, flags);
904 return vmalloc(size);
905}
906
907static inline void pblk_mfree(void *ptr, int type)
908{
909 if (type == PBLK_KMALLOC_META)
910 kfree(ptr);
911 else
912 vfree(ptr);
913}
914
915static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
916{
917 return c_ctx - sizeof(struct nvm_rq);
918}
919
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200920static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200922 return emeta->bb_bitmap;
923}
924
Hans Holmberg76758392018-03-30 00:04:52 +0200925static inline void *emeta_to_wa(struct pblk_line_meta *lm,
926 struct line_emeta *emeta)
927{
928 return emeta->bb_bitmap + lm->blk_bitmap_len;
929}
930
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200931static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
932{
933 return ((void *)emeta + pblk->lm.emeta_len[1]);
934}
935
936static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
937{
938 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200939}
940
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200941static inline int pblk_line_vsc(struct pblk_line *line)
942{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200943 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200944}
945
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200946#define NVM_MEM_PAGE_WRITE (8)
947
948static inline int pblk_pad_distance(struct pblk *pblk)
949{
950 struct nvm_tgt_dev *dev = pblk->dev;
951 struct nvm_geo *geo = &dev->geo;
952
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100953 return NVM_MEM_PAGE_WRITE * geo->all_luns * geo->sec_per_pl;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954}
955
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100956static inline int pblk_ppa_to_line(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200957{
958 return p.g.blk;
959}
960
961static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
962{
963 return p.g.lun * geo->nr_chnls + p.g.ch;
964}
965
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100966static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
967 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200968{
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100969 struct ppa_addr ppa;
970
971 ppa.ppa = 0;
972 ppa.g.blk = line_id;
973 ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset;
974 ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset;
975 ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset;
976 ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset;
977 ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset;
978
979 return ppa;
980}
981
982static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
983 struct ppa_addr p)
984{
985 u64 paddr;
986
987 paddr = (u64)p.g.pg << pblk->ppaf.pg_offset;
988 paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset;
989 paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset;
990 paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset;
991 paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset;
992
993 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994}
995
996static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
997{
998 struct ppa_addr ppa64;
999
1000 ppa64.ppa = 0;
1001
1002 if (ppa32 == -1) {
1003 ppa64.ppa = ADDR_EMPTY;
1004 } else if (ppa32 & (1U << 31)) {
1005 ppa64.c.line = ppa32 & ((~0U) >> 1);
1006 ppa64.c.is_cached = 1;
1007 } else {
1008 ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >>
1009 pblk->ppaf.blk_offset;
1010 ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >>
1011 pblk->ppaf.pg_offset;
1012 ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >>
1013 pblk->ppaf.lun_offset;
1014 ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >>
1015 pblk->ppaf.ch_offset;
1016 ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >>
1017 pblk->ppaf.pln_offset;
1018 ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >>
1019 pblk->ppaf.sec_offset;
1020 }
1021
1022 return ppa64;
1023}
1024
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001025static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1026{
1027 u32 ppa32 = 0;
1028
1029 if (ppa64.ppa == ADDR_EMPTY) {
1030 ppa32 = ~0U;
1031 } else if (ppa64.c.is_cached) {
1032 ppa32 |= ppa64.c.line;
1033 ppa32 |= 1U << 31;
1034 } else {
1035 ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset;
1036 ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset;
1037 ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset;
1038 ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset;
1039 ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset;
1040 ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset;
1041 }
1042
1043 return ppa32;
1044}
1045
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001046static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1047 sector_t lba)
1048{
1049 struct ppa_addr ppa;
1050
1051 if (pblk->ppaf_bitsize < 32) {
1052 u32 *map = (u32 *)pblk->trans_map;
1053
1054 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1055 } else {
1056 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1057
1058 ppa = map[lba];
1059 }
1060
1061 return ppa;
1062}
1063
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001064static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1065 struct ppa_addr ppa)
1066{
1067 if (pblk->ppaf_bitsize < 32) {
1068 u32 *map = (u32 *)pblk->trans_map;
1069
1070 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1071 } else {
1072 u64 *map = (u64 *)pblk->trans_map;
1073
1074 map[lba] = ppa.ppa;
1075 }
1076}
1077
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001078static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1079{
1080 return (ppa_addr.ppa == ADDR_EMPTY);
1081}
1082
1083static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1084{
1085 ppa_addr->ppa = ADDR_EMPTY;
1086}
1087
Javier González076984662017-06-30 17:56:42 +02001088static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1089{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001090 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001091}
1092
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001093static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1094{
1095 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1096}
1097
1098static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1099{
1100 return ppa.c.line;
1101}
1102
1103static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1104{
1105 struct ppa_addr p;
1106
1107 p.c.line = addr;
1108 p.c.is_cached = 1;
1109
1110 return p;
1111}
1112
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001114 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001115{
1116 u32 crc = ~(u32)0;
1117
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001118 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001119 sizeof(struct line_header) - sizeof(crc));
1120
1121 return crc;
1122}
1123
1124static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1125 struct line_smeta *smeta)
1126{
1127 struct pblk_line_meta *lm = &pblk->lm;
1128 u32 crc = ~(u32)0;
1129
1130 crc = crc32_le(crc, (unsigned char *)smeta +
1131 sizeof(struct line_header) + sizeof(crc),
1132 lm->smeta_len -
1133 sizeof(struct line_header) - sizeof(crc));
1134
1135 return crc;
1136}
1137
1138static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1139 struct line_emeta *emeta)
1140{
1141 struct pblk_line_meta *lm = &pblk->lm;
1142 u32 crc = ~(u32)0;
1143
1144 crc = crc32_le(crc, (unsigned char *)emeta +
1145 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001146 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001147 sizeof(struct line_header) - sizeof(crc));
1148
1149 return crc;
1150}
1151
1152static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1153{
1154 struct nvm_tgt_dev *dev = pblk->dev;
1155 struct nvm_geo *geo = &dev->geo;
1156 int flags;
1157
1158 flags = geo->plane_mode >> 1;
1159
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001160 if (type == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001161 flags |= NVM_IO_SCRAMBLE_ENABLE;
1162
1163 return flags;
1164}
1165
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001166enum {
1167 PBLK_READ_RANDOM = 0,
1168 PBLK_READ_SEQUENTIAL = 1,
1169};
1170
1171static inline int pblk_set_read_mode(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001172{
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001173 struct nvm_tgt_dev *dev = pblk->dev;
1174 struct nvm_geo *geo = &dev->geo;
1175 int flags;
1176
1177 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1178 if (type == PBLK_READ_SEQUENTIAL)
1179 flags |= geo->plane_mode >> 1;
1180
1181 return flags;
1182}
1183
1184static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1185{
1186 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001187}
1188
1189#ifdef CONFIG_NVM_DEBUG
1190static inline void print_ppa(struct ppa_addr *p, char *msg, int error)
1191{
1192 if (p->c.is_cached) {
1193 pr_err("ppa: (%s: %x) cache line: %llu\n",
1194 msg, error, (u64)p->c.line);
1195 } else {
1196 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1197 msg, error,
1198 p->g.ch, p->g.lun, p->g.blk,
1199 p->g.pg, p->g.pl, p->g.sec);
1200 }
1201}
1202
1203static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1204 int error)
1205{
1206 int bit = -1;
1207
1208 if (rqd->nr_ppas == 1) {
1209 print_ppa(&rqd->ppa_addr, "rqd", error);
1210 return;
1211 }
1212
1213 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1214 bit + 1)) < rqd->nr_ppas) {
1215 print_ppa(&rqd->ppa_list[bit], "rqd", error);
1216 }
1217
1218 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1219}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001220
1221static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1222 struct ppa_addr *ppas, int nr_ppas)
1223{
1224 struct nvm_geo *geo = &tgt_dev->geo;
1225 struct ppa_addr *ppa;
1226 int i;
1227
1228 for (i = 0; i < nr_ppas; i++) {
1229 ppa = &ppas[i];
1230
1231 if (!ppa->c.is_cached &&
1232 ppa->g.ch < geo->nr_chnls &&
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001233 ppa->g.lun < geo->nr_luns &&
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234 ppa->g.pl < geo->nr_planes &&
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001235 ppa->g.blk < geo->nr_chks &&
1236 ppa->g.pg < geo->ws_per_chk &&
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237 ppa->g.sec < geo->sec_per_pg)
1238 continue;
1239
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001240 print_ppa(ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001241
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001242 return 1;
1243 }
1244 return 0;
1245}
1246
Javier González1a94b2d2017-10-13 14:46:47 +02001247static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1248{
1249 struct nvm_tgt_dev *dev = pblk->dev;
1250 struct ppa_addr *ppa_list;
1251
1252 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1253
1254 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1255 WARN_ON(1);
1256 return -EINVAL;
1257 }
1258
1259 if (rqd->opcode == NVM_OP_PWRITE) {
1260 struct pblk_line *line;
1261 struct ppa_addr ppa;
1262 int i;
1263
1264 for (i = 0; i < rqd->nr_ppas; i++) {
1265 ppa = ppa_list[i];
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001266 line = &pblk->lines[pblk_ppa_to_line(ppa)];
Javier González1a94b2d2017-10-13 14:46:47 +02001267
1268 spin_lock(&line->lock);
1269 if (line->state != PBLK_LINESTATE_OPEN) {
1270 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1271 line->id, line->state);
1272 WARN_ON(1);
1273 spin_unlock(&line->lock);
1274 return -EINVAL;
1275 }
1276 spin_unlock(&line->lock);
1277 }
1278 }
1279
1280 return 0;
1281}
1282#endif
1283
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001284static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1285{
1286 struct pblk_line_meta *lm = &pblk->lm;
1287
1288 if (paddr > lm->sec_per_line)
1289 return 1;
1290
1291 return 0;
1292}
1293
1294static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1295{
1296 return bio->bi_iter.bi_idx;
1297}
1298
1299static inline sector_t pblk_get_lba(struct bio *bio)
1300{
1301 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1302}
1303
1304static inline unsigned int pblk_get_secs(struct bio *bio)
1305{
1306 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1307}
1308
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001309static inline void pblk_setup_uuid(struct pblk *pblk)
1310{
1311 uuid_le uuid;
1312
1313 uuid_le_gen(&uuid);
1314 memcpy(pblk->instance_uuid, uuid.b, 16);
1315}
1316#endif /* PBLK_H_ */