blob: ac3ab778e9767f677420e6f626f6c89184e76c18 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001/* SPDX-License-Identifier: GPL-2.0 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
4 * Copyright (C) 2016 CNEX Labs
5 * Initial release: Matias Bjorling <matias@cnexlabs.com>
6 * Write buffering: Javier Gonzalez <javier@cnexlabs.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * Implementation of a Physical Block-device target for Open-channel SSDs.
18 *
19 */
20
21#ifndef PBLK_H_
22#define PBLK_H_
23
24#include <linux/blkdev.h>
25#include <linux/blk-mq.h>
26#include <linux/bio.h>
27#include <linux/module.h>
28#include <linux/kthread.h>
29#include <linux/vmalloc.h>
30#include <linux/crc32.h>
31#include <linux/uuid.h>
32
33#include <linux/lightnvm.h>
34
35/* Run only GC if less than 1/X blocks are free */
36#define GC_LIMIT_INVERSE 5
37#define GC_TIME_MSECS 1000
38
39#define PBLK_SECTOR (512)
40#define PBLK_EXPOSED_PAGE_SIZE (4096)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020041
Javier Gonzálezef576492017-06-26 11:57:28 +020042#define PBLK_NR_CLOSE_JOBS (4)
43
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044#define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
45
46#define PBLK_COMMAND_TIMEOUT_MS 30000
47
48/* Max 512 LUNs per device */
49#define PBLK_MAX_LUNS_BITMAP (4)
50
51#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
52
Javier González0d880392017-10-13 14:46:08 +020053/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020054#define PBLK_GEN_WS_POOL_SIZE (2)
55
Javier Gonzáleze5392732018-01-05 14:16:14 +010056#define PBLK_DEFAULT_OP (11)
57
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020059 PBLK_READ = READ,
60 PBLK_WRITE = WRITE,/* Write from write buffer */
61 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010062 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020063 PBLK_ERASE,
64};
65
66enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020067 /* IO Types */
68 PBLK_IOTYPE_USER = 1 << 0,
69 PBLK_IOTYPE_GC = 1 << 1,
70
71 /* Write buffer flags */
72 PBLK_FLUSH_ENTRY = 1 << 2,
73 PBLK_WRITTEN_DATA = 1 << 3,
74 PBLK_SUBMITTED_ENTRY = 1 << 4,
75 PBLK_WRITABLE_ENTRY = 1 << 5,
76};
77
78enum {
79 PBLK_BLK_ST_OPEN = 0x1,
80 PBLK_BLK_ST_CLOSED = 0x2,
81};
82
Hans Holmberg4209c312018-10-09 13:11:55 +020083enum {
84 PBLK_CHUNK_RESET_START,
85 PBLK_CHUNK_RESET_DONE,
86 PBLK_CHUNK_RESET_FAILED,
87};
88
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020089struct pblk_sec_meta {
90 u64 reserved;
91 __le64 lba;
92};
93
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020094/* The number of GC lists and the rate-limiter states go together. This way the
95 * rate-limiter can dictate how much GC is needed based on resource utilization.
96 */
Hans Holmberg48b8d202018-06-01 16:41:06 +020097#define PBLK_GC_NR_LISTS 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020098
99enum {
Hans Holmberg48b8d202018-06-01 16:41:06 +0200100 PBLK_RL_OFF = 0,
101 PBLK_RL_WERR = 1,
102 PBLK_RL_HIGH = 2,
103 PBLK_RL_MID = 3,
104 PBLK_RL_LOW = 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200105};
106
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200107#define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200108
Javier González084ec9b2017-06-26 16:27:13 -0600109/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200110struct pblk_c_ctx {
111 struct list_head list; /* Head for out-of-order completion */
112
113 unsigned long *lun_bitmap; /* Luns used on current request */
114 unsigned int sentry;
115 unsigned int nr_valid;
116 unsigned int nr_padded;
117};
118
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200119/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600120struct pblk_g_ctx {
121 void *private;
Javier González998ba622018-01-05 14:16:20 +0100122 unsigned long start_time;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200123 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200124};
125
Heiner Litz11f6ad62018-07-13 10:48:44 +0200126/* partial read context */
127struct pblk_pr_ctx {
128 struct bio *orig_bio;
129 DECLARE_BITMAP(bitmap, NVM_MAX_VLBA);
130 unsigned int orig_nr_secs;
131 unsigned int bio_init_idx;
132 void *ppa_ptr;
133 dma_addr_t dma_ppa_list;
Andy Shevchenkoe74ecf62019-02-11 13:25:04 +0100134 u64 lba_list_mem[NVM_MAX_VLBA];
135 u64 lba_list_media[NVM_MAX_VLBA];
Heiner Litz11f6ad62018-07-13 10:48:44 +0200136};
137
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200138/* Pad context */
139struct pblk_pad_rq {
140 struct pblk *pblk;
141 struct completion wait;
142 struct kref ref;
143};
144
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200145/* Recovery context */
146struct pblk_rec_ctx {
147 struct pblk *pblk;
148 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200149 struct work_struct ws_rec;
150};
151
152/* Write context */
153struct pblk_w_ctx {
154 struct bio_list bios; /* Original bios - used for completion
155 * in REQ_FUA, REQ_FLUSH case
156 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200157 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200158 struct ppa_addr ppa; /* Physic addr. associated with entry */
159 int flags; /* Write context flags */
160};
161
162struct pblk_rb_entry {
163 struct ppa_addr cacheline; /* Cacheline for this entry */
164 void *data; /* Pointer to data on this entry */
165 struct pblk_w_ctx w_ctx; /* Context for this entry */
166 struct list_head index; /* List head to enable indexes */
167};
168
169#define EMPTY_ENTRY (~0U)
170
171struct pblk_rb_pages {
172 struct page *pages;
173 int order;
174 struct list_head list;
175};
176
177struct pblk_rb {
178 struct pblk_rb_entry *entries; /* Ring buffer entries */
179 unsigned int mem; /* Write offset - points to next
180 * writable entry in memory
181 */
182 unsigned int subm; /* Read offset - points to last entry
183 * that has been submitted to the media
184 * to be persisted
185 */
186 unsigned int sync; /* Synced - backpointer that signals
187 * the last submitted entry that has
188 * been successfully persisted to media
189 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100190 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200191 * flushed to the media. Used with
192 * REQ_FLUSH and REQ_FUA
193 */
194 unsigned int l2p_update; /* l2p update point - next entry for
195 * which l2p mapping will be updated to
196 * contain a device ppa address (instead
197 * of a cacheline
198 */
199 unsigned int nr_entries; /* Number of entries in write buffer -
200 * must be a power of two
201 */
202 unsigned int seg_size; /* Size of the data segments being
203 * stored on each entry. Typically this
204 * will be 4KB
205 */
206
Javier González766c8ce2018-10-09 13:12:15 +0200207 unsigned int back_thres; /* Threshold that shall be maintained by
208 * the backpointer in order to respect
209 * geo->mw_cunits on a per chunk basis
210 */
211
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212 struct list_head pages; /* List of data pages */
213
214 spinlock_t w_lock; /* Write lock */
215 spinlock_t s_lock; /* Sync lock */
216
Matias Bjørling880eda52018-07-13 10:48:37 +0200217#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100218 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200219#endif
220};
221
222#define PBLK_RECOVERY_SECTORS 16
223
224struct pblk_lun {
225 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200226 struct semaphore wr_sem;
227};
228
229struct pblk_gc_rq {
230 struct pblk_line *line;
231 void *data;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200232 u64 paddr_list[NVM_MAX_VLBA];
233 u64 lba_list[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200234 int nr_secs;
235 int secs_to_gc;
236 struct list_head list;
237};
238
239struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200240 /* These states are not protected by a lock since (i) they are in the
241 * fast path, and (ii) they are not critical.
242 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200243 int gc_active;
244 int gc_enabled;
245 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200246
247 struct task_struct *gc_ts;
248 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200249 struct task_struct *gc_reader_ts;
250
251 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200253
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200254 struct timer_list gc_timer;
255
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200256 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200257 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
258 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
259 * started reads to finished writes
260 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200261 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200262
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200264 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265
266 spinlock_t lock;
267 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200268 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200269};
270
271struct pblk_rl {
272 unsigned int high; /* Upper threshold for rate limiter (free run -
273 * user I/O rate limiter
274 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200275 unsigned int high_pw; /* High rounded up as a power of 2 */
276
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200277#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
278#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279
280 int rb_windows_pw; /* Number of rate windows in the write buffer
281 * given as a power-of-2. This guarantees that
282 * when user I/O is being rate limited, there
283 * will be reserved enough space for the GC to
284 * place its payload. A window is of
285 * pblk->max_write_pgs size, which in NVMe is
286 * 64, i.e., 256kb.
287 */
288 int rb_budget; /* Total number of entries available for I/O */
289 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200290 int rb_gc_max; /* Max buffer entries available for GC I/O */
291 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
292 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200293 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200294
295 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200297 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200299 int rsv_blocks; /* Reserved blocks for GC */
300
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200302 int rb_gc_active;
303
Hans Holmberg48b8d202018-06-01 16:41:06 +0200304 atomic_t werr_lines; /* Number of write error lines that needs gc */
305
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200306 struct timer_list u_timer;
307
308 unsigned long long nr_secs;
309 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100310
311 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
312 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313};
314
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200315#define PBLK_LINE_EMPTY (~0U)
316
317enum {
318 /* Line Types */
319 PBLK_LINETYPE_FREE = 0,
320 PBLK_LINETYPE_LOG = 1,
321 PBLK_LINETYPE_DATA = 2,
322
323 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200324 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325 PBLK_LINESTATE_FREE = 10,
326 PBLK_LINESTATE_OPEN = 11,
327 PBLK_LINESTATE_CLOSED = 12,
328 PBLK_LINESTATE_GC = 13,
329 PBLK_LINESTATE_BAD = 14,
330 PBLK_LINESTATE_CORRUPT = 15,
331
332 /* GC group */
333 PBLK_LINEGC_NONE = 20,
334 PBLK_LINEGC_EMPTY = 21,
335 PBLK_LINEGC_LOW = 22,
336 PBLK_LINEGC_MID = 23,
337 PBLK_LINEGC_HIGH = 24,
338 PBLK_LINEGC_FULL = 25,
Hans Holmberg48b8d202018-06-01 16:41:06 +0200339 PBLK_LINEGC_WERR = 26
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200340};
341
342#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200343
344/* emeta/smeta persistent storage format versions:
345 * Changes in major version requires offline migration.
346 * Changes in minor version are handled automatically during
347 * recovery.
348 */
349
350#define SMETA_VERSION_MAJOR (0)
351#define SMETA_VERSION_MINOR (1)
352
353#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200354#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200355
356struct line_header {
357 __le32 crc;
358 __le32 identifier; /* pblk identifier */
359 __u8 uuid[16]; /* instance uuid */
360 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200361 __u8 version_major; /* version major */
362 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200363 __le32 id; /* line id for current line */
364};
365
366struct line_smeta {
367 struct line_header header;
368
369 __le32 crc; /* Full structure including struct crc */
370 /* Previous line metadata */
371 __le32 prev_id; /* Line id for previous line */
372
373 /* Current line metadata */
374 __le64 seq_nr; /* Sequence number for current line */
375
376 /* Active writers */
377 __le32 window_wr_lun; /* Number of parallel LUNs to write */
378
379 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200380
381 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200382};
383
Hans Holmberg76758392018-03-30 00:04:52 +0200384
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200385/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200386 * Metadata layout in media:
387 * First sector:
388 * 1. struct line_emeta
389 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200390 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200391 * Mid sectors (start at lbas_sector):
392 * 3. nr_lbas (u64) forming lba list
393 * Last sectors (start at vsc_sector):
394 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200395 */
396struct line_emeta {
397 struct line_header header;
398
399 __le32 crc; /* Full structure including struct crc */
400
401 /* Previous line metadata */
402 __le32 prev_id; /* Line id for prev line */
403
404 /* Current line metadata */
405 __le64 seq_nr; /* Sequence number for current line */
406
407 /* Active writers */
408 __le32 window_wr_lun; /* Number of parallel LUNs to write */
409
410 /* Bookkeeping for recovery */
411 __le32 next_id; /* Line id for next line */
412 __le64 nr_lbas; /* Number of lbas mapped in line */
413 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200414 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
415};
416
417
418/* Write amplification counters stored on media */
419struct wa_counters {
420 __le64 user; /* Number of user written sectors */
421 __le64 gc; /* Number of sectors written by GC*/
422 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200423};
424
425struct pblk_emeta {
426 struct line_emeta *buf; /* emeta buffer in media format */
427 int mem; /* Write offset - points to next
428 * writable entry in memory
429 */
430 atomic_t sync; /* Synced - backpointer that signals the
431 * last entry that has been successfully
432 * persisted to media
433 */
434 unsigned int nr_entries; /* Number of emeta entries */
435};
436
437struct pblk_smeta {
438 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439};
440
Hans Holmberg48b8d202018-06-01 16:41:06 +0200441struct pblk_w_err_gc {
442 int has_write_err;
443 __le64 *lba_list;
444};
445
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200446struct pblk_line {
447 struct pblk *pblk;
448 unsigned int id; /* Line number corresponds to the
449 * block line
450 */
451 unsigned int seq_nr; /* Unique line sequence number */
452
453 int state; /* PBLK_LINESTATE_X */
454 int type; /* PBLK_LINETYPE_X */
455 int gc_group; /* PBLK_LINEGC_X */
456 struct list_head list; /* Free, GC lists */
457
458 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
459
Javier González32ef9412018-03-30 00:05:20 +0200460 struct nvm_chk_meta *chks; /* Chunks forming line */
461
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200462 struct pblk_smeta *smeta; /* Start metadata */
463 struct pblk_emeta *emeta; /* End medatada */
464
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200465 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200466 int meta_distance; /* Distance between data and metadata */
467
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468 u64 smeta_ssec; /* Sector where smeta starts */
469 u64 emeta_ssec; /* Sector where emeta starts */
470
471 unsigned int sec_in_line; /* Number of usable secs in line */
472
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200473 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
475 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
476
477 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
478 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
479
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200480 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200481 atomic_t left_seblks; /* Blocks left for sync erasing */
482
483 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200485 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
486
487 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488
489 struct kref ref; /* Write buffer L2P references */
Heiner Litz05869422019-02-11 13:25:09 +0100490 atomic_t sec_to_update; /* Outstanding L2P updates to ppa */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200491
Hans Holmberg48b8d202018-06-01 16:41:06 +0200492 struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
493
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 spinlock_t lock; /* Necessary for invalid_bitmap only */
495};
496
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200497#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200498
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200499enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500 PBLK_KMALLOC_META = 1,
501 PBLK_VMALLOC_META = 2,
502};
503
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200504enum {
505 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
506 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
507 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508};
509
510struct pblk_line_mgmt {
511 int nr_lines; /* Total number of full lines */
512 int nr_free_lines; /* Number of full lines in free list */
513
514 /* Free lists - use free_lock */
515 struct list_head free_list; /* Full lines ready to use */
516 struct list_head corrupt_list; /* Full lines corrupted */
517 struct list_head bad_list; /* Full lines bad */
518
519 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200520 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200521 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
522 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
523 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
524
Hans Holmberg48b8d202018-06-01 16:41:06 +0200525 struct list_head gc_werr_list; /* Write err recovery list */
526
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200527 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
528 struct list_head gc_empty_list; /* Full lines close, all valid */
529
530 struct pblk_line *log_line; /* Current FTL log line */
531 struct pblk_line *data_line; /* Current data line */
532 struct pblk_line *log_next; /* Next FTL log line */
533 struct pblk_line *data_next; /* Next data line */
534
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200535 struct list_head emeta_list; /* Lines queued to schedule emeta */
536
537 __le32 *vsc_list; /* Valid sector counts for all lines */
538
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200539 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540 int emeta_alloc_type;
541
542 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200543 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
544 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545 unsigned long meta_bitmap;
546
Hans Holmberg53d82db2018-10-09 13:11:47 +0200547 /* Cache and mempool for map/invalid bitmaps */
548 struct kmem_cache *bitmap_cache;
549 mempool_t *bitmap_pool;
550
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200551 /* Helpers for fast bitmap calculations */
552 unsigned long *bb_template;
553 unsigned long *bb_aux;
554
555 unsigned long d_seq_nr; /* Data line unique sequence number */
556 unsigned long l_seq_nr; /* Log line unique sequence number */
557
558 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200559 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200560 spinlock_t gc_lock;
561};
562
563struct pblk_line_meta {
564 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200565 unsigned int smeta_sec; /* Sectors needed for smeta */
566
567 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200568 * [0]: Total
569 * [1]: struct line_emeta +
570 * bb_bitmap + struct wa_counters
571 * [2]: L2P portion
572 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200573 */
574 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
575 * as emeta_len
576 */
577
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200579
580 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200581 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
582 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
583 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
584
585 unsigned int blk_per_line; /* Number of blocks in a full line */
586 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200587 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200588 unsigned int min_blk_line; /* Min. number of good blocks in line */
589
590 unsigned int mid_thrs; /* Threshold for GC mid list */
591 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200592
593 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594};
595
Javier González588726d32017-06-26 11:57:29 +0200596enum {
597 PBLK_STATE_RUNNING = 0,
598 PBLK_STATE_STOPPING = 1,
599 PBLK_STATE_RECOVERING = 2,
600 PBLK_STATE_STOPPED = 3,
601};
602
Javier González3b2a3ad2018-03-30 00:05:21 +0200603/* Internal format to support not power-of-2 device formats */
604struct pblk_addrf {
605 /* gen to dev */
606 int sec_stripe;
607 int ch_stripe;
608 int lun_stripe;
609
610 /* dev to gen */
611 int sec_lun_stripe;
612 int sec_ws_stripe;
613};
614
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615struct pblk {
616 struct nvm_tgt_dev *dev;
617 struct gendisk *disk;
618
619 struct kobject kobj;
620
621 struct pblk_lun *luns;
622
623 struct pblk_line *lines; /* Line array */
624 struct pblk_line_mgmt l_mg; /* Line management */
625 struct pblk_line_meta lm; /* Line metadata */
626
Javier González3b2a3ad2018-03-30 00:05:21 +0200627 struct nvm_addrf addrf; /* Aligned address format */
628 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200629 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200630
631 struct pblk_rb rwb;
632
Javier González588726d32017-06-26 11:57:29 +0200633 int state; /* pblk line state */
634
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 int min_write_pgs; /* Minimum amount of pages required by controller */
Igor Konopko55d8ec32018-12-11 20:16:26 +0100636 int min_write_pgs_data; /* Minimum amount of payload pages */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200637 int max_write_pgs; /* Maximum amount of pages supported by controller */
Igor Konopkofaa79f22018-12-11 20:16:23 +0100638 int oob_meta_size; /* Size of OOB sector metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639
640 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100641
642 int op; /* Percentage of device used for over-provisioning */
643 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200644
645 /* pblk provisioning values. Used by rate limiter */
646 struct pblk_rl rl;
647
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200648 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100650 guid_t instance_uuid;
Hans Holmberg76758392018-03-30 00:04:52 +0200651
652 /* Persistent write amplification counters, 4kb sector I/Os */
653 atomic64_t user_wa; /* Sectors written by user */
654 atomic64_t gc_wa; /* Sectors written by GC */
655 atomic64_t pad_wa; /* Padded sectors written */
656
657 /* Reset values for delta write amplification measurements */
658 u64 user_rst_wa;
659 u64 gc_rst_wa;
660 u64 pad_rst_wa;
661
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200662 /* Counters used for calculating padding distribution */
663 atomic64_t *pad_dist; /* Padding distribution buckets */
664 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
665 atomic64_t nr_flush; /* Number of flush/fua I/O */
666
Matias Bjørling880eda52018-07-13 10:48:37 +0200667#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200668 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200669 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
670 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
671 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 atomic_long_t req_writes; /* Sectors stored on write buffer */
673 atomic_long_t sub_writes; /* Sectors submitted from buffer */
674 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200675 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200676 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200677 atomic_long_t sync_reads; /* Completed sector read requests */
678 atomic_long_t recov_writes; /* Sectors submitted from recovery */
679 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
680 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
681#endif
682
683 spinlock_t lock;
684
685 atomic_long_t read_failed;
686 atomic_long_t read_empty;
687 atomic_long_t read_high_ecc;
688 atomic_long_t read_failed_gc;
689 atomic_long_t write_failed;
690 atomic_long_t erase_failed;
691
Javier González588726d32017-06-26 11:57:29 +0200692 atomic_t inflight_io; /* General inflight I/O counter */
693
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200694 struct task_struct *writer_ts;
695
696 /* Simple translation map of logical addresses to physical addresses.
697 * The logical addresses is known by the host system, while the physical
698 * addresses are used when writing to the disk block device.
699 */
700 unsigned char *trans_map;
701 spinlock_t trans_lock;
702
703 struct list_head compl_list;
704
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200705 spinlock_t resubmit_lock; /* Resubmit list lock */
706 struct list_head resubmit_list; /* Resubmit list for failed writes*/
707
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400708 mempool_t page_bio_pool;
709 mempool_t gen_ws_pool;
710 mempool_t rec_pool;
711 mempool_t r_rq_pool;
712 mempool_t w_rq_pool;
713 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200714
Javier Gonzálezef576492017-06-26 11:57:28 +0200715 struct workqueue_struct *close_wq;
716 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200717 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200718
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200719 struct timer_list wtimer;
720
721 struct pblk_gc gc;
722};
723
724struct pblk_line_ws {
725 struct pblk *pblk;
726 struct pblk_line *line;
727 void *priv;
728 struct work_struct ws;
729};
730
Javier González084ec9b2017-06-26 16:27:13 -0600731#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200732#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
733
Matias Bjørling4e495a42018-07-13 10:48:42 +0200734#define pblk_err(pblk, fmt, ...) \
735 pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
736#define pblk_info(pblk, fmt, ...) \
737 pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
738#define pblk_warn(pblk, fmt, ...) \
739 pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
740#define pblk_debug(pblk, fmt, ...) \
741 pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
742
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200743/*
744 * pblk ring buffer operations
745 */
Javier González766c8ce2018-10-09 13:12:15 +0200746int pblk_rb_init(struct pblk_rb *rb, unsigned int size, unsigned int threshold,
747 unsigned int seg_sz);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200748int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
749 unsigned int nr_entries, unsigned int *pos);
750int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
751 unsigned int *pos);
752void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
753 struct pblk_w_ctx w_ctx, unsigned int pos);
754void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200755 struct pblk_w_ctx w_ctx, struct pblk_line *line,
756 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200758void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200759
760void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200761unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200762 unsigned int pos, unsigned int nr_entries,
763 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200764int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200765 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200766unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
767
768unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
769unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
Javier González40b86572018-10-09 13:12:09 +0200770unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p,
771 unsigned int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200772void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100773unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200774
775unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200776unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200777unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
778
779int pblk_rb_tear_down_check(struct pblk_rb *rb);
780int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
Javier González9bd1f872018-10-09 13:12:10 +0200781void pblk_rb_free(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200782ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
783
784/*
785 * pblk core
786 */
Javier González67bf26a2017-10-13 14:46:20 +0200787struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
788void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier González45dcf292018-10-09 13:12:02 +0200789int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
790void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200791void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200792int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
793 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794void pblk_discard(struct pblk *pblk, struct bio *bio);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200795struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk);
Javier González32ef9412018-03-30 00:05:20 +0200796struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
797 struct nvm_chk_meta *lp,
798 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200799void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
800void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
801int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200802int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier González253babc2018-10-09 13:12:04 +0200803int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200804int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200805void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200806struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
807 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200808 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200809struct pblk_line *pblk_line_get(struct pblk *pblk);
810struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200811struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Matias Bjørlingae14cc02018-10-09 13:11:40 +0200812void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa);
813void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200814int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
815void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
816struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200817struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200818int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
819int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200820void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200821void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200822void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200823void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200824void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200825void __pblk_pipeline_stop(struct pblk *pblk);
826void __pblk_pipeline_flush(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200827void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
828 void (*work)(struct work_struct *), gfp_t gfp_mask,
829 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200831int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line);
832int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200833 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200834int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
835void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200836void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200837struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200838u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
839void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200840u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200841u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
Igor Konopko55d8ec32018-12-11 20:16:26 +0100843 unsigned long secs_to_flush, bool skip_meta);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200844void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200845 unsigned long *lun_bitmap);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200846void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa);
847void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa);
Hans Holmberge99e8022018-10-09 13:11:48 +0200848void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200849int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
850 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200851void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
852 int nr_pages);
853void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200854void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
855 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200856void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
857void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
858 struct ppa_addr ppa);
859void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
860 struct ppa_addr ppa, struct ppa_addr entry_line);
861int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200862 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200863void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
864 u64 *lba_list, int nr_secs);
865void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
866 sector_t blba, int nr_secs);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100867void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd);
868void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869
870/*
871 * pblk user I/O write path
872 */
873int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
874 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200875int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200876
877/*
878 * pblk map
879 */
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100880int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200881 unsigned int sentry, unsigned long *lun_bitmap,
882 unsigned int valid_secs, struct ppa_addr *erase_ppa);
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100883int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200884 unsigned long *lun_bitmap, unsigned int valid_secs,
885 unsigned int off);
886
887/*
888 * pblk write thread
889 */
890int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700891void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892void pblk_write_should_kick(struct pblk *pblk);
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200893void pblk_write_kick(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200894
895/*
896 * pblk read path
897 */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400898extern struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200899int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200900int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200901/*
902 * pblk recovery
903 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200904struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200905int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100906int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200907
908/*
909 * pblk gc
910 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200911#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200912#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200913#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200914
915int pblk_gc_init(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200916void pblk_gc_exit(struct pblk *pblk, bool graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200917void pblk_gc_should_start(struct pblk *pblk);
918void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200919void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200920void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
922 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200923int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200924
925/*
926 * pblk rate limiter
927 */
Javier Gonzálezb4cdc422019-02-11 13:25:08 +0100928void pblk_rl_init(struct pblk_rl *rl, int budget, int threshold);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200929void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200930void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200931int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200932unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100933unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200934int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200935void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200936void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
937int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
938void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
939void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200940int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200941void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100942void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
943 bool used);
Javier González588726d32017-06-26 11:57:29 +0200944int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200945
Hans Holmberg48b8d202018-06-01 16:41:06 +0200946void pblk_rl_werr_line_in(struct pblk_rl *rl);
947void pblk_rl_werr_line_out(struct pblk_rl *rl);
948
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200949/*
950 * pblk sysfs
951 */
952int pblk_sysfs_init(struct gendisk *tdisk);
953void pblk_sysfs_exit(struct gendisk *tdisk);
954
955static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
956{
957 if (type == PBLK_KMALLOC_META)
958 return kmalloc(size, flags);
959 return vmalloc(size);
960}
961
962static inline void pblk_mfree(void *ptr, int type)
963{
964 if (type == PBLK_KMALLOC_META)
965 kfree(ptr);
966 else
967 vfree(ptr);
968}
969
970static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
971{
972 return c_ctx - sizeof(struct nvm_rq);
973}
974
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200975static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200977 return emeta->bb_bitmap;
978}
979
Hans Holmberg76758392018-03-30 00:04:52 +0200980static inline void *emeta_to_wa(struct pblk_line_meta *lm,
981 struct line_emeta *emeta)
982{
983 return emeta->bb_bitmap + lm->blk_bitmap_len;
984}
985
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200986static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
987{
988 return ((void *)emeta + pblk->lm.emeta_len[1]);
989}
990
991static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
992{
993 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994}
995
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200996static inline int pblk_line_vsc(struct pblk_line *line)
997{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200998 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200999}
1000
Javier Gonzálezcb216652018-10-09 13:11:42 +02001001static inline int pblk_ppa_to_line_id(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001002{
Javier González69471512018-03-30 00:05:15 +02001003 return p.a.blk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001004}
1005
Javier Gonzálezcb216652018-10-09 13:11:42 +02001006static inline struct pblk_line *pblk_ppa_to_line(struct pblk *pblk,
1007 struct ppa_addr p)
1008{
1009 return &pblk->lines[pblk_ppa_to_line_id(p)];
1010}
1011
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001012static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
1013{
Javier González69471512018-03-30 00:05:15 +02001014 return p.a.lun * geo->num_ch + p.a.ch;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001015}
1016
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001017static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1018 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001019{
Javier González3b2a3ad2018-03-30 00:05:21 +02001020 struct nvm_tgt_dev *dev = pblk->dev;
1021 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001022 struct ppa_addr ppa;
1023
Javier González3b2a3ad2018-03-30 00:05:21 +02001024 if (geo->version == NVM_OCSSD_SPEC_12) {
1025 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1026
1027 ppa.ppa = 0;
1028 ppa.g.blk = line_id;
1029 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
1030 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
1031 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
1032 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
1033 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
1034 } else {
1035 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1036 int secs, chnls, luns;
1037
1038 ppa.ppa = 0;
1039
1040 ppa.m.chk = line_id;
1041
1042 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
1043 ppa.m.sec = secs;
1044
1045 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
1046 ppa.m.grp = chnls;
1047
1048 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
1049 ppa.m.pu = luns;
1050
1051 ppa.m.sec += uaddrf->sec_stripe * paddr;
1052 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001053
1054 return ppa;
1055}
1056
Javier González2cf99bb2018-10-09 13:11:41 +02001057static inline struct nvm_chk_meta *pblk_dev_ppa_to_chunk(struct pblk *pblk,
1058 struct ppa_addr p)
1059{
1060 struct nvm_tgt_dev *dev = pblk->dev;
1061 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezcb216652018-10-09 13:11:42 +02001062 struct pblk_line *line = pblk_ppa_to_line(pblk, p);
Javier González2cf99bb2018-10-09 13:11:41 +02001063 int pos = pblk_ppa_to_pos(geo, p);
1064
1065 return &line->chks[pos];
1066}
1067
1068static inline u64 pblk_dev_ppa_to_chunk_addr(struct pblk *pblk,
1069 struct ppa_addr p)
1070{
1071 struct nvm_tgt_dev *dev = pblk->dev;
1072
1073 return dev_to_chunk_addr(dev->parent, &pblk->addrf, p);
1074}
1075
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001076static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1077 struct ppa_addr p)
1078{
Javier González3b2a3ad2018-03-30 00:05:21 +02001079 struct nvm_tgt_dev *dev = pblk->dev;
1080 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001081 u64 paddr;
1082
Javier González3b2a3ad2018-03-30 00:05:21 +02001083 if (geo->version == NVM_OCSSD_SPEC_12) {
1084 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1085
1086 paddr = (u64)p.g.ch << ppaf->ch_offset;
1087 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1088 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1089 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1090 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1091 } else {
1092 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1093 u64 secs = p.m.sec;
1094 int sec_stripe;
1095
1096 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1097 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1098
1099 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1100 paddr += secs * uaddrf->sec_ws_stripe;
1101 paddr += sec_stripe;
1102 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001103
1104 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105}
1106
1107static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1108{
Javier González7f985f92018-10-09 13:11:56 +02001109 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110
Javier González7f985f92018-10-09 13:11:56 +02001111 return nvm_ppa32_to_ppa64(dev->parent, &pblk->addrf, ppa32);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001112}
1113
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001114static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1115{
Javier González7f985f92018-10-09 13:11:56 +02001116 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117
Javier González7f985f92018-10-09 13:11:56 +02001118 return nvm_ppa64_to_ppa32(dev->parent, &pblk->addrf, ppa64);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001119}
1120
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001121static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1122 sector_t lba)
1123{
1124 struct ppa_addr ppa;
1125
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001126 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001127 u32 *map = (u32 *)pblk->trans_map;
1128
1129 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1130 } else {
1131 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1132
1133 ppa = map[lba];
1134 }
1135
1136 return ppa;
1137}
1138
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001139static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1140 struct ppa_addr ppa)
1141{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001142 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143 u32 *map = (u32 *)pblk->trans_map;
1144
1145 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1146 } else {
1147 u64 *map = (u64 *)pblk->trans_map;
1148
1149 map[lba] = ppa.ppa;
1150 }
1151}
1152
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1154{
1155 return (ppa_addr.ppa == ADDR_EMPTY);
1156}
1157
1158static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1159{
1160 ppa_addr->ppa = ADDR_EMPTY;
1161}
1162
Javier González076984662017-06-30 17:56:42 +02001163static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1164{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001165 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001166}
1167
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001168static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1169{
1170 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1171}
1172
1173static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1174{
1175 return ppa.c.line;
1176}
1177
1178static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1179{
1180 struct ppa_addr p;
1181
1182 p.c.line = addr;
1183 p.c.is_cached = 1;
1184
1185 return p;
1186}
1187
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001188static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001189 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001190{
1191 u32 crc = ~(u32)0;
1192
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001193 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001194 sizeof(struct line_header) - sizeof(crc));
1195
1196 return crc;
1197}
1198
1199static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1200 struct line_smeta *smeta)
1201{
1202 struct pblk_line_meta *lm = &pblk->lm;
1203 u32 crc = ~(u32)0;
1204
1205 crc = crc32_le(crc, (unsigned char *)smeta +
1206 sizeof(struct line_header) + sizeof(crc),
1207 lm->smeta_len -
1208 sizeof(struct line_header) - sizeof(crc));
1209
1210 return crc;
1211}
1212
1213static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1214 struct line_emeta *emeta)
1215{
1216 struct pblk_line_meta *lm = &pblk->lm;
1217 u32 crc = ~(u32)0;
1218
1219 crc = crc32_le(crc, (unsigned char *)emeta +
1220 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001221 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001222 sizeof(struct line_header) - sizeof(crc));
1223
1224 return crc;
1225}
1226
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001227static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1228{
1229 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001230}
1231
Matias Bjørling880eda52018-07-13 10:48:37 +02001232#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001233static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p,
Javier González3b2a3ad2018-03-30 00:05:21 +02001234 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001235{
Matias Bjørling4e495a42018-07-13 10:48:42 +02001236 struct nvm_geo *geo = &pblk->dev->geo;
1237
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001238 if (p->c.is_cached) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001239 pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001240 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001241 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001242 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 +02001243 msg, error,
1244 p->g.ch, p->g.lun, p->g.blk,
1245 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001246 } else {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001247 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
Javier González3b2a3ad2018-03-30 00:05:21 +02001248 msg, error,
1249 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001250 }
1251}
1252
1253static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1254 int error)
1255{
1256 int bit = -1;
1257
1258 if (rqd->nr_ppas == 1) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001259 print_ppa(pblk, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001260 return;
1261 }
1262
1263 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1264 bit + 1)) < rqd->nr_ppas) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001265 print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001266 }
1267
Matias Bjørling4e495a42018-07-13 10:48:42 +02001268 pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001269}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001270
1271static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1272 struct ppa_addr *ppas, int nr_ppas)
1273{
1274 struct nvm_geo *geo = &tgt_dev->geo;
1275 struct ppa_addr *ppa;
1276 int i;
1277
1278 for (i = 0; i < nr_ppas; i++) {
1279 ppa = &ppas[i];
1280
Javier González3b2a3ad2018-03-30 00:05:21 +02001281 if (geo->version == NVM_OCSSD_SPEC_12) {
1282 if (!ppa->c.is_cached &&
1283 ppa->g.ch < geo->num_ch &&
1284 ppa->g.lun < geo->num_lun &&
1285 ppa->g.pl < geo->num_pln &&
1286 ppa->g.blk < geo->num_chk &&
1287 ppa->g.pg < geo->num_pg &&
1288 ppa->g.sec < geo->ws_min)
1289 continue;
1290 } else {
1291 if (!ppa->c.is_cached &&
1292 ppa->m.grp < geo->num_ch &&
1293 ppa->m.pu < geo->num_lun &&
1294 ppa->m.chk < geo->num_chk &&
1295 ppa->m.sec < geo->clba)
1296 continue;
1297 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001298
Matias Bjørling4e495a42018-07-13 10:48:42 +02001299 print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001300
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001301 return 1;
1302 }
1303 return 0;
1304}
1305
Javier González1a94b2d2017-10-13 14:46:47 +02001306static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1307{
1308 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmbergd68a9342018-10-09 13:11:46 +02001309 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González1a94b2d2017-10-13 14:46:47 +02001310
1311 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1312 WARN_ON(1);
1313 return -EINVAL;
1314 }
1315
1316 if (rqd->opcode == NVM_OP_PWRITE) {
1317 struct pblk_line *line;
Javier González1a94b2d2017-10-13 14:46:47 +02001318 int i;
1319
1320 for (i = 0; i < rqd->nr_ppas; i++) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02001321 line = pblk_ppa_to_line(pblk, ppa_list[i]);
Javier González1a94b2d2017-10-13 14:46:47 +02001322
1323 spin_lock(&line->lock);
1324 if (line->state != PBLK_LINESTATE_OPEN) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001325 pblk_err(pblk, "bad ppa: line:%d,state:%d\n",
Javier González1a94b2d2017-10-13 14:46:47 +02001326 line->id, line->state);
1327 WARN_ON(1);
1328 spin_unlock(&line->lock);
1329 return -EINVAL;
1330 }
1331 spin_unlock(&line->lock);
1332 }
1333 }
1334
1335 return 0;
1336}
1337#endif
1338
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001339static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1340{
1341 struct pblk_line_meta *lm = &pblk->lm;
1342
1343 if (paddr > lm->sec_per_line)
1344 return 1;
1345
1346 return 0;
1347}
1348
1349static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1350{
1351 return bio->bi_iter.bi_idx;
1352}
1353
1354static inline sector_t pblk_get_lba(struct bio *bio)
1355{
1356 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1357}
1358
1359static inline unsigned int pblk_get_secs(struct bio *bio)
1360{
1361 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1362}
1363
Hans Holmberg4c44abf2018-10-09 13:11:52 +02001364static inline char *pblk_disk_name(struct pblk *pblk)
1365{
1366 struct gendisk *disk = pblk->disk;
1367
1368 return disk->disk_name;
1369}
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001370
1371static inline unsigned int pblk_get_min_chks(struct pblk *pblk)
1372{
1373 struct pblk_line_meta *lm = &pblk->lm;
1374 /* In a worst-case scenario every line will have OP invalid sectors.
1375 * We will then need a minimum of 1/OP lines to free up a single line
1376 */
1377
1378 return DIV_ROUND_UP(100, pblk->op) * lm->blk_per_line;
Igor Konopkofaa79f22018-12-11 20:16:23 +01001379}
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001380
Igor Konopkofaa79f22018-12-11 20:16:23 +01001381static inline struct pblk_sec_meta *pblk_get_meta(struct pblk *pblk,
1382 void *meta, int index)
1383{
Igor Konopko2c4d5352018-12-11 20:16:27 +01001384 return meta +
1385 max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1386 * index;
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001387}
Igor Konopko24828d02018-12-11 20:16:24 +01001388
1389static inline int pblk_dma_meta_size(struct pblk *pblk)
1390{
Igor Konopko2c4d5352018-12-11 20:16:27 +01001391 return max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1392 * NVM_MAX_VLBA;
Igor Konopko24828d02018-12-11 20:16:24 +01001393}
Igor Konopko55d8ec32018-12-11 20:16:26 +01001394
1395static inline int pblk_is_oob_meta_supported(struct pblk *pblk)
1396{
1397 return pblk->oob_meta_size >= sizeof(struct pblk_sec_meta);
1398}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001399#endif /* PBLK_H_ */