blob: af9477b7e8032e899f6dfbf221ae2322d678870d [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)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020040
Javier Gonzálezef576492017-06-26 11:57:28 +020041#define PBLK_NR_CLOSE_JOBS (4)
42
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020043#define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
44
45#define PBLK_COMMAND_TIMEOUT_MS 30000
46
47/* Max 512 LUNs per device */
48#define PBLK_MAX_LUNS_BITMAP (4)
49
50#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
51
Javier González0d880392017-10-13 14:46:08 +020052/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020053#define PBLK_GEN_WS_POOL_SIZE (2)
54
Javier Gonzáleze5392732018-01-05 14:16:14 +010055#define PBLK_DEFAULT_OP (11)
56
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020058 PBLK_READ = READ,
59 PBLK_WRITE = WRITE,/* Write from write buffer */
60 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010061 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020062 PBLK_ERASE,
63};
64
65enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020066 /* IO Types */
67 PBLK_IOTYPE_USER = 1 << 0,
68 PBLK_IOTYPE_GC = 1 << 1,
69
70 /* Write buffer flags */
71 PBLK_FLUSH_ENTRY = 1 << 2,
72 PBLK_WRITTEN_DATA = 1 << 3,
73 PBLK_SUBMITTED_ENTRY = 1 << 4,
74 PBLK_WRITABLE_ENTRY = 1 << 5,
75};
76
77enum {
78 PBLK_BLK_ST_OPEN = 0x1,
79 PBLK_BLK_ST_CLOSED = 0x2,
80};
81
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020082struct pblk_sec_meta {
83 u64 reserved;
84 __le64 lba;
85};
86
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020087/* The number of GC lists and the rate-limiter states go together. This way the
88 * rate-limiter can dictate how much GC is needed based on resource utilization.
89 */
Hans Holmberg48b8d202018-06-01 16:41:06 +020090#define PBLK_GC_NR_LISTS 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091
92enum {
Hans Holmberg48b8d202018-06-01 16:41:06 +020093 PBLK_RL_OFF = 0,
94 PBLK_RL_WERR = 1,
95 PBLK_RL_HIGH = 2,
96 PBLK_RL_MID = 3,
97 PBLK_RL_LOW = 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020098};
99
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200100#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * NVM_MAX_VLBA)
101#define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA)
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
Heiner Litz11f6ad62018-07-13 10:48:44 +0200120/* partial read context */
121struct pblk_pr_ctx {
122 struct bio *orig_bio;
123 DECLARE_BITMAP(bitmap, NVM_MAX_VLBA);
124 unsigned int orig_nr_secs;
125 unsigned int bio_init_idx;
126 void *ppa_ptr;
127 dma_addr_t dma_ppa_list;
128};
129
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200130/* Pad context */
131struct pblk_pad_rq {
132 struct pblk *pblk;
133 struct completion wait;
134 struct kref ref;
135};
136
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200137/* Recovery context */
138struct pblk_rec_ctx {
139 struct pblk *pblk;
140 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200141 struct work_struct ws_rec;
142};
143
144/* Write context */
145struct pblk_w_ctx {
146 struct bio_list bios; /* Original bios - used for completion
147 * in REQ_FUA, REQ_FLUSH case
148 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200149 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200150 struct ppa_addr ppa; /* Physic addr. associated with entry */
151 int flags; /* Write context flags */
152};
153
154struct pblk_rb_entry {
155 struct ppa_addr cacheline; /* Cacheline for this entry */
156 void *data; /* Pointer to data on this entry */
157 struct pblk_w_ctx w_ctx; /* Context for this entry */
158 struct list_head index; /* List head to enable indexes */
159};
160
161#define EMPTY_ENTRY (~0U)
162
163struct pblk_rb_pages {
164 struct page *pages;
165 int order;
166 struct list_head list;
167};
168
169struct pblk_rb {
170 struct pblk_rb_entry *entries; /* Ring buffer entries */
171 unsigned int mem; /* Write offset - points to next
172 * writable entry in memory
173 */
174 unsigned int subm; /* Read offset - points to last entry
175 * that has been submitted to the media
176 * to be persisted
177 */
178 unsigned int sync; /* Synced - backpointer that signals
179 * the last submitted entry that has
180 * been successfully persisted to media
181 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100182 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183 * flushed to the media. Used with
184 * REQ_FLUSH and REQ_FUA
185 */
186 unsigned int l2p_update; /* l2p update point - next entry for
187 * which l2p mapping will be updated to
188 * contain a device ppa address (instead
189 * of a cacheline
190 */
191 unsigned int nr_entries; /* Number of entries in write buffer -
192 * must be a power of two
193 */
194 unsigned int seg_size; /* Size of the data segments being
195 * stored on each entry. Typically this
196 * will be 4KB
197 */
198
199 struct list_head pages; /* List of data pages */
200
201 spinlock_t w_lock; /* Write lock */
202 spinlock_t s_lock; /* Sync lock */
203
Matias Bjørling880eda52018-07-13 10:48:37 +0200204#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100205 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206#endif
207};
208
209#define PBLK_RECOVERY_SECTORS 16
210
211struct pblk_lun {
212 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200213 struct semaphore wr_sem;
214};
215
216struct pblk_gc_rq {
217 struct pblk_line *line;
218 void *data;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200219 u64 paddr_list[NVM_MAX_VLBA];
220 u64 lba_list[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221 int nr_secs;
222 int secs_to_gc;
223 struct list_head list;
224};
225
226struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200227 /* These states are not protected by a lock since (i) they are in the
228 * fast path, and (ii) they are not critical.
229 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200230 int gc_active;
231 int gc_enabled;
232 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200233
234 struct task_struct *gc_ts;
235 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200236 struct task_struct *gc_reader_ts;
237
238 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200239 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200240
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200241 struct timer_list gc_timer;
242
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200243 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200244 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
245 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
246 * started reads to finished writes
247 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200248 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200249
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252
253 spinlock_t lock;
254 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200255 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256};
257
258struct pblk_rl {
259 unsigned int high; /* Upper threshold for rate limiter (free run -
260 * user I/O rate limiter
261 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200262 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
Hans Holmberg48b8d202018-06-01 16:41:06 +0200291 atomic_t werr_lines; /* Number of write error lines that needs gc */
292
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200293 struct timer_list u_timer;
294
295 unsigned long long nr_secs;
296 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100297
298 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
299 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300};
301
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302#define PBLK_LINE_EMPTY (~0U)
303
304enum {
305 /* Line Types */
306 PBLK_LINETYPE_FREE = 0,
307 PBLK_LINETYPE_LOG = 1,
308 PBLK_LINETYPE_DATA = 2,
309
310 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200311 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200312 PBLK_LINESTATE_FREE = 10,
313 PBLK_LINESTATE_OPEN = 11,
314 PBLK_LINESTATE_CLOSED = 12,
315 PBLK_LINESTATE_GC = 13,
316 PBLK_LINESTATE_BAD = 14,
317 PBLK_LINESTATE_CORRUPT = 15,
318
319 /* GC group */
320 PBLK_LINEGC_NONE = 20,
321 PBLK_LINEGC_EMPTY = 21,
322 PBLK_LINEGC_LOW = 22,
323 PBLK_LINEGC_MID = 23,
324 PBLK_LINEGC_HIGH = 24,
325 PBLK_LINEGC_FULL = 25,
Hans Holmberg48b8d202018-06-01 16:41:06 +0200326 PBLK_LINEGC_WERR = 26
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200327};
328
329#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200330
331/* emeta/smeta persistent storage format versions:
332 * Changes in major version requires offline migration.
333 * Changes in minor version are handled automatically during
334 * recovery.
335 */
336
337#define SMETA_VERSION_MAJOR (0)
338#define SMETA_VERSION_MINOR (1)
339
340#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200341#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200342
343struct line_header {
344 __le32 crc;
345 __le32 identifier; /* pblk identifier */
346 __u8 uuid[16]; /* instance uuid */
347 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200348 __u8 version_major; /* version major */
349 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200350 __le32 id; /* line id for current line */
351};
352
353struct line_smeta {
354 struct line_header header;
355
356 __le32 crc; /* Full structure including struct crc */
357 /* Previous line metadata */
358 __le32 prev_id; /* Line id for previous line */
359
360 /* Current line metadata */
361 __le64 seq_nr; /* Sequence number for current line */
362
363 /* Active writers */
364 __le32 window_wr_lun; /* Number of parallel LUNs to write */
365
366 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200367
368 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369};
370
Hans Holmberg76758392018-03-30 00:04:52 +0200371
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200372/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200373 * Metadata layout in media:
374 * First sector:
375 * 1. struct line_emeta
376 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200377 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200378 * Mid sectors (start at lbas_sector):
379 * 3. nr_lbas (u64) forming lba list
380 * Last sectors (start at vsc_sector):
381 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200382 */
383struct line_emeta {
384 struct line_header header;
385
386 __le32 crc; /* Full structure including struct crc */
387
388 /* Previous line metadata */
389 __le32 prev_id; /* Line id for prev line */
390
391 /* Current line metadata */
392 __le64 seq_nr; /* Sequence number for current line */
393
394 /* Active writers */
395 __le32 window_wr_lun; /* Number of parallel LUNs to write */
396
397 /* Bookkeeping for recovery */
398 __le32 next_id; /* Line id for next line */
399 __le64 nr_lbas; /* Number of lbas mapped in line */
400 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200401 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
402};
403
404
405/* Write amplification counters stored on media */
406struct wa_counters {
407 __le64 user; /* Number of user written sectors */
408 __le64 gc; /* Number of sectors written by GC*/
409 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200410};
411
412struct pblk_emeta {
413 struct line_emeta *buf; /* emeta buffer in media format */
414 int mem; /* Write offset - points to next
415 * writable entry in memory
416 */
417 atomic_t sync; /* Synced - backpointer that signals the
418 * last entry that has been successfully
419 * persisted to media
420 */
421 unsigned int nr_entries; /* Number of emeta entries */
422};
423
424struct pblk_smeta {
425 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426};
427
Hans Holmberg48b8d202018-06-01 16:41:06 +0200428struct pblk_w_err_gc {
429 int has_write_err;
430 __le64 *lba_list;
431};
432
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433struct pblk_line {
434 struct pblk *pblk;
435 unsigned int id; /* Line number corresponds to the
436 * block line
437 */
438 unsigned int seq_nr; /* Unique line sequence number */
439
440 int state; /* PBLK_LINESTATE_X */
441 int type; /* PBLK_LINETYPE_X */
442 int gc_group; /* PBLK_LINEGC_X */
443 struct list_head list; /* Free, GC lists */
444
445 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
446
Javier González32ef9412018-03-30 00:05:20 +0200447 struct nvm_chk_meta *chks; /* Chunks forming line */
448
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200449 struct pblk_smeta *smeta; /* Start metadata */
450 struct pblk_emeta *emeta; /* End medatada */
451
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200453 int meta_distance; /* Distance between data and metadata */
454
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455 u64 smeta_ssec; /* Sector where smeta starts */
456 u64 emeta_ssec; /* Sector where emeta starts */
457
458 unsigned int sec_in_line; /* Number of usable secs in line */
459
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200460 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200461 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
462 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
463
464 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
465 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
466
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200467 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468 atomic_t left_seblks; /* Blocks left for sync erasing */
469
470 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200471 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200472 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
473
474 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475
476 struct kref ref; /* Write buffer L2P references */
477
Hans Holmberg48b8d202018-06-01 16:41:06 +0200478 struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
479
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480 spinlock_t lock; /* Necessary for invalid_bitmap only */
481};
482
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200483#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200485enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200486 PBLK_KMALLOC_META = 1,
487 PBLK_VMALLOC_META = 2,
488};
489
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200490enum {
491 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
492 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
493 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494};
495
496struct pblk_line_mgmt {
497 int nr_lines; /* Total number of full lines */
498 int nr_free_lines; /* Number of full lines in free list */
499
500 /* Free lists - use free_lock */
501 struct list_head free_list; /* Full lines ready to use */
502 struct list_head corrupt_list; /* Full lines corrupted */
503 struct list_head bad_list; /* Full lines bad */
504
505 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200506 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
508 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
509 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
510
Hans Holmberg48b8d202018-06-01 16:41:06 +0200511 struct list_head gc_werr_list; /* Write err recovery list */
512
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
514 struct list_head gc_empty_list; /* Full lines close, all valid */
515
516 struct pblk_line *log_line; /* Current FTL log line */
517 struct pblk_line *data_line; /* Current data line */
518 struct pblk_line *log_next; /* Next FTL log line */
519 struct pblk_line *data_next; /* Next data line */
520
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200521 struct list_head emeta_list; /* Lines queued to schedule emeta */
522
523 __le32 *vsc_list; /* Valid sector counts for all lines */
524
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200525 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200526 int emeta_alloc_type;
527
528 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200529 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
530 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200531 unsigned long meta_bitmap;
532
533 /* Helpers for fast bitmap calculations */
534 unsigned long *bb_template;
535 unsigned long *bb_aux;
536
537 unsigned long d_seq_nr; /* Data line unique sequence number */
538 unsigned long l_seq_nr; /* Log line unique sequence number */
539
540 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200541 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542 spinlock_t gc_lock;
543};
544
545struct pblk_line_meta {
546 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200547 unsigned int smeta_sec; /* Sectors needed for smeta */
548
549 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200550 * [0]: Total
551 * [1]: struct line_emeta +
552 * bb_bitmap + struct wa_counters
553 * [2]: L2P portion
554 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200555 */
556 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
557 * as emeta_len
558 */
559
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200560 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200561
562 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
564 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
565 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
566
567 unsigned int blk_per_line; /* Number of blocks in a full line */
568 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200569 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570 unsigned int min_blk_line; /* Min. number of good blocks in line */
571
572 unsigned int mid_thrs; /* Threshold for GC mid list */
573 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200574
575 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200576};
577
Javier González588726d32017-06-26 11:57:29 +0200578enum {
579 PBLK_STATE_RUNNING = 0,
580 PBLK_STATE_STOPPING = 1,
581 PBLK_STATE_RECOVERING = 2,
582 PBLK_STATE_STOPPED = 3,
583};
584
Javier González3b2a3ad2018-03-30 00:05:21 +0200585/* Internal format to support not power-of-2 device formats */
586struct pblk_addrf {
587 /* gen to dev */
588 int sec_stripe;
589 int ch_stripe;
590 int lun_stripe;
591
592 /* dev to gen */
593 int sec_lun_stripe;
594 int sec_ws_stripe;
595};
596
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200597struct pblk {
598 struct nvm_tgt_dev *dev;
599 struct gendisk *disk;
600
601 struct kobject kobj;
602
603 struct pblk_lun *luns;
604
605 struct pblk_line *lines; /* Line array */
606 struct pblk_line_mgmt l_mg; /* Line management */
607 struct pblk_line_meta lm; /* Line metadata */
608
Javier González3b2a3ad2018-03-30 00:05:21 +0200609 struct nvm_addrf addrf; /* Aligned address format */
610 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200611 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612
613 struct pblk_rb rwb;
614
Javier González588726d32017-06-26 11:57:29 +0200615 int state; /* pblk line state */
616
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617 int min_write_pgs; /* Minimum amount of pages required by controller */
618 int max_write_pgs; /* Maximum amount of pages supported by controller */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619
620 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100621
622 int op; /* Percentage of device used for over-provisioning */
623 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624
625 /* pblk provisioning values. Used by rate limiter */
626 struct pblk_rl rl;
627
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200628 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200629
630 unsigned char instance_uuid[16];
Hans Holmberg76758392018-03-30 00:04:52 +0200631
632 /* Persistent write amplification counters, 4kb sector I/Os */
633 atomic64_t user_wa; /* Sectors written by user */
634 atomic64_t gc_wa; /* Sectors written by GC */
635 atomic64_t pad_wa; /* Padded sectors written */
636
637 /* Reset values for delta write amplification measurements */
638 u64 user_rst_wa;
639 u64 gc_rst_wa;
640 u64 pad_rst_wa;
641
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200642 /* Counters used for calculating padding distribution */
643 atomic64_t *pad_dist; /* Padding distribution buckets */
644 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
645 atomic64_t nr_flush; /* Number of flush/fua I/O */
646
Matias Bjørling880eda52018-07-13 10:48:37 +0200647#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200648 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
650 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
651 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200652 atomic_long_t req_writes; /* Sectors stored on write buffer */
653 atomic_long_t sub_writes; /* Sectors submitted from buffer */
654 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200656 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657 atomic_long_t sync_reads; /* Completed sector read requests */
658 atomic_long_t recov_writes; /* Sectors submitted from recovery */
659 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
660 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
661#endif
662
663 spinlock_t lock;
664
665 atomic_long_t read_failed;
666 atomic_long_t read_empty;
667 atomic_long_t read_high_ecc;
668 atomic_long_t read_failed_gc;
669 atomic_long_t write_failed;
670 atomic_long_t erase_failed;
671
Javier González588726d32017-06-26 11:57:29 +0200672 atomic_t inflight_io; /* General inflight I/O counter */
673
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200674 struct task_struct *writer_ts;
675
676 /* Simple translation map of logical addresses to physical addresses.
677 * The logical addresses is known by the host system, while the physical
678 * addresses are used when writing to the disk block device.
679 */
680 unsigned char *trans_map;
681 spinlock_t trans_lock;
682
683 struct list_head compl_list;
684
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200685 spinlock_t resubmit_lock; /* Resubmit list lock */
686 struct list_head resubmit_list; /* Resubmit list for failed writes*/
687
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400688 mempool_t page_bio_pool;
689 mempool_t gen_ws_pool;
690 mempool_t rec_pool;
691 mempool_t r_rq_pool;
692 mempool_t w_rq_pool;
693 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200694
Javier Gonzálezef576492017-06-26 11:57:28 +0200695 struct workqueue_struct *close_wq;
696 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200697 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200698
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200699 struct timer_list wtimer;
700
701 struct pblk_gc gc;
702};
703
704struct pblk_line_ws {
705 struct pblk *pblk;
706 struct pblk_line *line;
707 void *priv;
708 struct work_struct ws;
709};
710
Javier González084ec9b2017-06-26 16:27:13 -0600711#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200712#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
713
Matias Bjørling4e495a42018-07-13 10:48:42 +0200714#define pblk_err(pblk, fmt, ...) \
715 pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
716#define pblk_info(pblk, fmt, ...) \
717 pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
718#define pblk_warn(pblk, fmt, ...) \
719 pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
720#define pblk_debug(pblk, fmt, ...) \
721 pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
722
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200723/*
724 * pblk ring buffer operations
725 */
726int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
727 unsigned int power_size, unsigned int power_seg_sz);
728unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
729void *pblk_rb_entries_ref(struct pblk_rb *rb);
730int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
731 unsigned int nr_entries, unsigned int *pos);
732int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
733 unsigned int *pos);
734void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
735 struct pblk_w_ctx w_ctx, unsigned int pos);
736void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200737 struct pblk_w_ctx w_ctx, struct pblk_line *line,
738 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200739struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200740void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200741
742void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200743unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200744 unsigned int pos, unsigned int nr_entries,
745 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200747 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200748unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
749
750unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
751unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
752struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
753 struct ppa_addr *ppa);
754void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100755unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200756
757unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200758unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200759unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
760
761int pblk_rb_tear_down_check(struct pblk_rb *rb);
762int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
763void pblk_rb_data_free(struct pblk_rb *rb);
764ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
765
766/*
767 * pblk core
768 */
Javier González67bf26a2017-10-13 14:46:20 +0200769struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
770void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200771void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200772int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
773 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200774void pblk_discard(struct pblk *pblk, struct bio *bio);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200775struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk);
Javier González32ef9412018-03-30 00:05:20 +0200776struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
777 struct nvm_chk_meta *lp,
778 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200779void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
780void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
781int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200782int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200783int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200784struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
785 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200786 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200787struct pblk_line *pblk_line_get(struct pblk *pblk);
788struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200789struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Matias Bjørlingae14cc02018-10-09 13:11:40 +0200790void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa);
791void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200792int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
793void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
794struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200795struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200796int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
797int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200798void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200799void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200800void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200801void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200802void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200803void __pblk_pipeline_stop(struct pblk *pblk);
804void __pblk_pipeline_flush(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200805void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
806 void (*work)(struct work_struct *), gfp_t gfp_mask,
807 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200808u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
809int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200810int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
811 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200812int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
813void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200814void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200815struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200816u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
817void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200818u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200819u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200820int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
821 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200822void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200823void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
824 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200825void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200826void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
827 unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200828int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
829 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
831 int nr_pages);
832void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200833void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
834 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200835void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
836void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
837 struct ppa_addr ppa);
838void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
839 struct ppa_addr ppa, struct ppa_addr entry_line);
840int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200841 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
843 u64 *lba_list, int nr_secs);
844void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
845 sector_t blba, int nr_secs);
846
847/*
848 * pblk user I/O write path
849 */
850int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
851 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200852int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200853
854/*
855 * pblk map
856 */
857void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
858 unsigned int sentry, unsigned long *lun_bitmap,
859 unsigned int valid_secs, struct ppa_addr *erase_ppa);
860void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
861 unsigned long *lun_bitmap, unsigned int valid_secs,
862 unsigned int off);
863
864/*
865 * pblk write thread
866 */
867int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700868void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869void pblk_write_should_kick(struct pblk *pblk);
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200870void pblk_write_kick(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200871
872/*
873 * pblk read path
874 */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400875extern struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200876int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200877int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200878/*
879 * pblk recovery
880 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200881struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200882int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100883int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200884
885/*
886 * pblk gc
887 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200888#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200889#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200890#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
891#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892
893int pblk_gc_init(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200894void pblk_gc_exit(struct pblk *pblk, bool graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200895void pblk_gc_should_start(struct pblk *pblk);
896void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200897void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200898void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200899void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
900 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200901int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902
903/*
904 * pblk rate limiter
905 */
906void pblk_rl_init(struct pblk_rl *rl, int budget);
907void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200908void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200909int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200910unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100911unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200912int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200913void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200914void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
915int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
916void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
917void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200918int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200919void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100920void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
921 bool used);
Javier González588726d32017-06-26 11:57:29 +0200922int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200923
Hans Holmberg48b8d202018-06-01 16:41:06 +0200924void pblk_rl_werr_line_in(struct pblk_rl *rl);
925void pblk_rl_werr_line_out(struct pblk_rl *rl);
926
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927/*
928 * pblk sysfs
929 */
930int pblk_sysfs_init(struct gendisk *tdisk);
931void pblk_sysfs_exit(struct gendisk *tdisk);
932
933static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
934{
935 if (type == PBLK_KMALLOC_META)
936 return kmalloc(size, flags);
937 return vmalloc(size);
938}
939
940static inline void pblk_mfree(void *ptr, int type)
941{
942 if (type == PBLK_KMALLOC_META)
943 kfree(ptr);
944 else
945 vfree(ptr);
946}
947
948static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
949{
950 return c_ctx - sizeof(struct nvm_rq);
951}
952
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200953static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200955 return emeta->bb_bitmap;
956}
957
Hans Holmberg76758392018-03-30 00:04:52 +0200958static inline void *emeta_to_wa(struct pblk_line_meta *lm,
959 struct line_emeta *emeta)
960{
961 return emeta->bb_bitmap + lm->blk_bitmap_len;
962}
963
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200964static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
965{
966 return ((void *)emeta + pblk->lm.emeta_len[1]);
967}
968
969static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
970{
971 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200972}
973
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200974static inline int pblk_line_vsc(struct pblk_line *line)
975{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200976 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200977}
978
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200979static inline int pblk_pad_distance(struct pblk *pblk)
980{
981 struct nvm_tgt_dev *dev = pblk->dev;
982 struct nvm_geo *geo = &dev->geo;
983
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200984 return geo->mw_cunits * geo->all_luns * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985}
986
Javier Gonzálezcb216652018-10-09 13:11:42 +0200987static inline int pblk_ppa_to_line_id(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988{
Javier González69471512018-03-30 00:05:15 +0200989 return p.a.blk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200990}
991
Javier Gonzálezcb216652018-10-09 13:11:42 +0200992static inline struct pblk_line *pblk_ppa_to_line(struct pblk *pblk,
993 struct ppa_addr p)
994{
995 return &pblk->lines[pblk_ppa_to_line_id(p)];
996}
997
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200998static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
999{
Javier González69471512018-03-30 00:05:15 +02001000 return p.a.lun * geo->num_ch + p.a.ch;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001001}
1002
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001003static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1004 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001005{
Javier González3b2a3ad2018-03-30 00:05:21 +02001006 struct nvm_tgt_dev *dev = pblk->dev;
1007 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001008 struct ppa_addr ppa;
1009
Javier González3b2a3ad2018-03-30 00:05:21 +02001010 if (geo->version == NVM_OCSSD_SPEC_12) {
1011 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1012
1013 ppa.ppa = 0;
1014 ppa.g.blk = line_id;
1015 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
1016 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
1017 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
1018 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
1019 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
1020 } else {
1021 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1022 int secs, chnls, luns;
1023
1024 ppa.ppa = 0;
1025
1026 ppa.m.chk = line_id;
1027
1028 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
1029 ppa.m.sec = secs;
1030
1031 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
1032 ppa.m.grp = chnls;
1033
1034 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
1035 ppa.m.pu = luns;
1036
1037 ppa.m.sec += uaddrf->sec_stripe * paddr;
1038 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001039
1040 return ppa;
1041}
1042
Javier González2cf99bb2018-10-09 13:11:41 +02001043static inline struct nvm_chk_meta *pblk_dev_ppa_to_chunk(struct pblk *pblk,
1044 struct ppa_addr p)
1045{
1046 struct nvm_tgt_dev *dev = pblk->dev;
1047 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezcb216652018-10-09 13:11:42 +02001048 struct pblk_line *line = pblk_ppa_to_line(pblk, p);
Javier González2cf99bb2018-10-09 13:11:41 +02001049 int pos = pblk_ppa_to_pos(geo, p);
1050
1051 return &line->chks[pos];
1052}
1053
1054static inline u64 pblk_dev_ppa_to_chunk_addr(struct pblk *pblk,
1055 struct ppa_addr p)
1056{
1057 struct nvm_tgt_dev *dev = pblk->dev;
1058
1059 return dev_to_chunk_addr(dev->parent, &pblk->addrf, p);
1060}
1061
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001062static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1063 struct ppa_addr p)
1064{
Javier González3b2a3ad2018-03-30 00:05:21 +02001065 struct nvm_tgt_dev *dev = pblk->dev;
1066 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001067 u64 paddr;
1068
Javier González3b2a3ad2018-03-30 00:05:21 +02001069 if (geo->version == NVM_OCSSD_SPEC_12) {
1070 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1071
1072 paddr = (u64)p.g.ch << ppaf->ch_offset;
1073 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1074 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1075 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1076 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1077 } else {
1078 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1079 u64 secs = p.m.sec;
1080 int sec_stripe;
1081
1082 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1083 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1084
1085 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1086 paddr += secs * uaddrf->sec_ws_stripe;
1087 paddr += sec_stripe;
1088 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001089
1090 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001091}
1092
1093static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1094{
1095 struct ppa_addr ppa64;
1096
1097 ppa64.ppa = 0;
1098
1099 if (ppa32 == -1) {
1100 ppa64.ppa = ADDR_EMPTY;
1101 } else if (ppa32 & (1U << 31)) {
1102 ppa64.c.line = ppa32 & ((~0U) >> 1);
1103 ppa64.c.is_cached = 1;
1104 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001105 struct nvm_tgt_dev *dev = pblk->dev;
1106 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001107
Javier González3b2a3ad2018-03-30 00:05:21 +02001108 if (geo->version == NVM_OCSSD_SPEC_12) {
1109 struct nvm_addrf_12 *ppaf =
1110 (struct nvm_addrf_12 *)&pblk->addrf;
1111
1112 ppa64.g.ch = (ppa32 & ppaf->ch_mask) >>
1113 ppaf->ch_offset;
1114 ppa64.g.lun = (ppa32 & ppaf->lun_mask) >>
1115 ppaf->lun_offset;
1116 ppa64.g.blk = (ppa32 & ppaf->blk_mask) >>
1117 ppaf->blk_offset;
1118 ppa64.g.pg = (ppa32 & ppaf->pg_mask) >>
1119 ppaf->pg_offset;
1120 ppa64.g.pl = (ppa32 & ppaf->pln_mask) >>
1121 ppaf->pln_offset;
1122 ppa64.g.sec = (ppa32 & ppaf->sec_mask) >>
1123 ppaf->sec_offset;
1124 } else {
1125 struct nvm_addrf *lbaf = &pblk->addrf;
1126
1127 ppa64.m.grp = (ppa32 & lbaf->ch_mask) >>
1128 lbaf->ch_offset;
1129 ppa64.m.pu = (ppa32 & lbaf->lun_mask) >>
1130 lbaf->lun_offset;
1131 ppa64.m.chk = (ppa32 & lbaf->chk_mask) >>
1132 lbaf->chk_offset;
1133 ppa64.m.sec = (ppa32 & lbaf->sec_mask) >>
1134 lbaf->sec_offset;
1135 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001136 }
1137
1138 return ppa64;
1139}
1140
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001141static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1142{
1143 u32 ppa32 = 0;
1144
1145 if (ppa64.ppa == ADDR_EMPTY) {
1146 ppa32 = ~0U;
1147 } else if (ppa64.c.is_cached) {
1148 ppa32 |= ppa64.c.line;
1149 ppa32 |= 1U << 31;
1150 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001151 struct nvm_tgt_dev *dev = pblk->dev;
1152 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001153
Javier González3b2a3ad2018-03-30 00:05:21 +02001154 if (geo->version == NVM_OCSSD_SPEC_12) {
1155 struct nvm_addrf_12 *ppaf =
1156 (struct nvm_addrf_12 *)&pblk->addrf;
1157
1158 ppa32 |= ppa64.g.ch << ppaf->ch_offset;
1159 ppa32 |= ppa64.g.lun << ppaf->lun_offset;
1160 ppa32 |= ppa64.g.blk << ppaf->blk_offset;
1161 ppa32 |= ppa64.g.pg << ppaf->pg_offset;
1162 ppa32 |= ppa64.g.pl << ppaf->pln_offset;
1163 ppa32 |= ppa64.g.sec << ppaf->sec_offset;
1164 } else {
1165 struct nvm_addrf *lbaf = &pblk->addrf;
1166
1167 ppa32 |= ppa64.m.grp << lbaf->ch_offset;
1168 ppa32 |= ppa64.m.pu << lbaf->lun_offset;
1169 ppa32 |= ppa64.m.chk << lbaf->chk_offset;
1170 ppa32 |= ppa64.m.sec << lbaf->sec_offset;
1171 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001172 }
1173
1174 return ppa32;
1175}
1176
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001177static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1178 sector_t lba)
1179{
1180 struct ppa_addr ppa;
1181
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001182 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001183 u32 *map = (u32 *)pblk->trans_map;
1184
1185 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1186 } else {
1187 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1188
1189 ppa = map[lba];
1190 }
1191
1192 return ppa;
1193}
1194
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001195static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1196 struct ppa_addr ppa)
1197{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001198 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001199 u32 *map = (u32 *)pblk->trans_map;
1200
1201 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1202 } else {
1203 u64 *map = (u64 *)pblk->trans_map;
1204
1205 map[lba] = ppa.ppa;
1206 }
1207}
1208
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001209static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1210{
1211 return (ppa_addr.ppa == ADDR_EMPTY);
1212}
1213
1214static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1215{
1216 ppa_addr->ppa = ADDR_EMPTY;
1217}
1218
Javier González076984662017-06-30 17:56:42 +02001219static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1220{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001221 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001222}
1223
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001224static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1225{
1226 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1227}
1228
1229static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1230{
1231 return ppa.c.line;
1232}
1233
1234static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1235{
1236 struct ppa_addr p;
1237
1238 p.c.line = addr;
1239 p.c.is_cached = 1;
1240
1241 return p;
1242}
1243
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001245 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001246{
1247 u32 crc = ~(u32)0;
1248
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001249 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001250 sizeof(struct line_header) - sizeof(crc));
1251
1252 return crc;
1253}
1254
1255static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1256 struct line_smeta *smeta)
1257{
1258 struct pblk_line_meta *lm = &pblk->lm;
1259 u32 crc = ~(u32)0;
1260
1261 crc = crc32_le(crc, (unsigned char *)smeta +
1262 sizeof(struct line_header) + sizeof(crc),
1263 lm->smeta_len -
1264 sizeof(struct line_header) - sizeof(crc));
1265
1266 return crc;
1267}
1268
1269static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1270 struct line_emeta *emeta)
1271{
1272 struct pblk_line_meta *lm = &pblk->lm;
1273 u32 crc = ~(u32)0;
1274
1275 crc = crc32_le(crc, (unsigned char *)emeta +
1276 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001277 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001278 sizeof(struct line_header) - sizeof(crc));
1279
1280 return crc;
1281}
1282
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001283static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1284{
1285 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001286}
1287
Matias Bjørling880eda52018-07-13 10:48:37 +02001288#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001289static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p,
Javier González3b2a3ad2018-03-30 00:05:21 +02001290 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001291{
Matias Bjørling4e495a42018-07-13 10:48:42 +02001292 struct nvm_geo *geo = &pblk->dev->geo;
1293
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001294 if (p->c.is_cached) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001295 pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001296 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001297 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001298 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001299 msg, error,
1300 p->g.ch, p->g.lun, p->g.blk,
1301 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001302 } else {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001303 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
Javier González3b2a3ad2018-03-30 00:05:21 +02001304 msg, error,
1305 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001306 }
1307}
1308
1309static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1310 int error)
1311{
1312 int bit = -1;
1313
1314 if (rqd->nr_ppas == 1) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001315 print_ppa(pblk, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001316 return;
1317 }
1318
1319 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1320 bit + 1)) < rqd->nr_ppas) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001321 print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001322 }
1323
Matias Bjørling4e495a42018-07-13 10:48:42 +02001324 pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001325}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001326
1327static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1328 struct ppa_addr *ppas, int nr_ppas)
1329{
1330 struct nvm_geo *geo = &tgt_dev->geo;
1331 struct ppa_addr *ppa;
1332 int i;
1333
1334 for (i = 0; i < nr_ppas; i++) {
1335 ppa = &ppas[i];
1336
Javier González3b2a3ad2018-03-30 00:05:21 +02001337 if (geo->version == NVM_OCSSD_SPEC_12) {
1338 if (!ppa->c.is_cached &&
1339 ppa->g.ch < geo->num_ch &&
1340 ppa->g.lun < geo->num_lun &&
1341 ppa->g.pl < geo->num_pln &&
1342 ppa->g.blk < geo->num_chk &&
1343 ppa->g.pg < geo->num_pg &&
1344 ppa->g.sec < geo->ws_min)
1345 continue;
1346 } else {
1347 if (!ppa->c.is_cached &&
1348 ppa->m.grp < geo->num_ch &&
1349 ppa->m.pu < geo->num_lun &&
1350 ppa->m.chk < geo->num_chk &&
1351 ppa->m.sec < geo->clba)
1352 continue;
1353 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001354
Matias Bjørling4e495a42018-07-13 10:48:42 +02001355 print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001356
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001357 return 1;
1358 }
1359 return 0;
1360}
1361
Javier González1a94b2d2017-10-13 14:46:47 +02001362static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1363{
1364 struct nvm_tgt_dev *dev = pblk->dev;
1365 struct ppa_addr *ppa_list;
1366
1367 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1368
1369 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1370 WARN_ON(1);
1371 return -EINVAL;
1372 }
1373
1374 if (rqd->opcode == NVM_OP_PWRITE) {
1375 struct pblk_line *line;
1376 struct ppa_addr ppa;
1377 int i;
1378
1379 for (i = 0; i < rqd->nr_ppas; i++) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02001380 line = pblk_ppa_to_line(pblk, ppa_list[i]);
Javier González1a94b2d2017-10-13 14:46:47 +02001381
1382 spin_lock(&line->lock);
1383 if (line->state != PBLK_LINESTATE_OPEN) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001384 pblk_err(pblk, "bad ppa: line:%d,state:%d\n",
Javier González1a94b2d2017-10-13 14:46:47 +02001385 line->id, line->state);
1386 WARN_ON(1);
1387 spin_unlock(&line->lock);
1388 return -EINVAL;
1389 }
1390 spin_unlock(&line->lock);
1391 }
1392 }
1393
1394 return 0;
1395}
1396#endif
1397
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001398static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1399{
1400 struct pblk_line_meta *lm = &pblk->lm;
1401
1402 if (paddr > lm->sec_per_line)
1403 return 1;
1404
1405 return 0;
1406}
1407
1408static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1409{
1410 return bio->bi_iter.bi_idx;
1411}
1412
1413static inline sector_t pblk_get_lba(struct bio *bio)
1414{
1415 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1416}
1417
1418static inline unsigned int pblk_get_secs(struct bio *bio)
1419{
1420 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1421}
1422
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001423static inline void pblk_setup_uuid(struct pblk *pblk)
1424{
1425 uuid_le uuid;
1426
1427 uuid_le_gen(&uuid);
1428 memcpy(pblk->instance_uuid, uuid.b, 16);
1429}
1430#endif /* PBLK_H_ */