blob: a67855387f53d315d122b9218bbe1223801c51aa [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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046/* Max 512 LUNs per device */
47#define PBLK_MAX_LUNS_BITMAP (4)
48
49#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
50
Javier González0d880392017-10-13 14:46:08 +020051/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020052#define PBLK_GEN_WS_POOL_SIZE (2)
53
Javier Gonzáleze5392732018-01-05 14:16:14 +010054#define PBLK_DEFAULT_OP (11)
55
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020056enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020057 PBLK_READ = READ,
58 PBLK_WRITE = WRITE,/* Write from write buffer */
59 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010060 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020061 PBLK_ERASE,
62};
63
64enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020065 /* IO Types */
66 PBLK_IOTYPE_USER = 1 << 0,
67 PBLK_IOTYPE_GC = 1 << 1,
68
69 /* Write buffer flags */
70 PBLK_FLUSH_ENTRY = 1 << 2,
71 PBLK_WRITTEN_DATA = 1 << 3,
72 PBLK_SUBMITTED_ENTRY = 1 << 4,
73 PBLK_WRITABLE_ENTRY = 1 << 5,
74};
75
76enum {
77 PBLK_BLK_ST_OPEN = 0x1,
78 PBLK_BLK_ST_CLOSED = 0x2,
79};
80
Hans Holmberg4209c312018-10-09 13:11:55 +020081enum {
82 PBLK_CHUNK_RESET_START,
83 PBLK_CHUNK_RESET_DONE,
84 PBLK_CHUNK_RESET_FAILED,
85};
86
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020087struct pblk_sec_meta {
88 u64 reserved;
89 __le64 lba;
90};
91
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020092/* The number of GC lists and the rate-limiter states go together. This way the
93 * rate-limiter can dictate how much GC is needed based on resource utilization.
94 */
Hans Holmberg48b8d202018-06-01 16:41:06 +020095#define PBLK_GC_NR_LISTS 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020096
97enum {
Hans Holmberg48b8d202018-06-01 16:41:06 +020098 PBLK_RL_OFF = 0,
99 PBLK_RL_WERR = 1,
100 PBLK_RL_HIGH = 2,
101 PBLK_RL_MID = 3,
102 PBLK_RL_LOW = 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103};
104
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200105#define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200106
Javier González084ec9b2017-06-26 16:27:13 -0600107/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200108struct pblk_c_ctx {
109 struct list_head list; /* Head for out-of-order completion */
110
111 unsigned long *lun_bitmap; /* Luns used on current request */
112 unsigned int sentry;
113 unsigned int nr_valid;
114 unsigned int nr_padded;
115};
116
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200117/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600118struct pblk_g_ctx {
119 void *private;
Javier González998ba622018-01-05 14:16:20 +0100120 unsigned long start_time;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200121 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200122};
123
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200124/* Pad context */
125struct pblk_pad_rq {
126 struct pblk *pblk;
127 struct completion wait;
128 struct kref ref;
129};
130
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200131/* Recovery context */
132struct pblk_rec_ctx {
133 struct pblk *pblk;
134 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200135 struct work_struct ws_rec;
136};
137
138/* Write context */
139struct pblk_w_ctx {
140 struct bio_list bios; /* Original bios - used for completion
141 * in REQ_FUA, REQ_FLUSH case
142 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200143 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200144 struct ppa_addr ppa; /* Physic addr. associated with entry */
145 int flags; /* Write context flags */
146};
147
148struct pblk_rb_entry {
149 struct ppa_addr cacheline; /* Cacheline for this entry */
150 void *data; /* Pointer to data on this entry */
151 struct pblk_w_ctx w_ctx; /* Context for this entry */
152 struct list_head index; /* List head to enable indexes */
153};
154
155#define EMPTY_ENTRY (~0U)
156
157struct pblk_rb_pages {
158 struct page *pages;
159 int order;
160 struct list_head list;
161};
162
163struct pblk_rb {
164 struct pblk_rb_entry *entries; /* Ring buffer entries */
165 unsigned int mem; /* Write offset - points to next
166 * writable entry in memory
167 */
168 unsigned int subm; /* Read offset - points to last entry
169 * that has been submitted to the media
170 * to be persisted
171 */
172 unsigned int sync; /* Synced - backpointer that signals
173 * the last submitted entry that has
174 * been successfully persisted to media
175 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100176 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200177 * flushed to the media. Used with
178 * REQ_FLUSH and REQ_FUA
179 */
180 unsigned int l2p_update; /* l2p update point - next entry for
181 * which l2p mapping will be updated to
182 * contain a device ppa address (instead
183 * of a cacheline
184 */
185 unsigned int nr_entries; /* Number of entries in write buffer -
186 * must be a power of two
187 */
188 unsigned int seg_size; /* Size of the data segments being
189 * stored on each entry. Typically this
190 * will be 4KB
191 */
192
Javier González766c8ce2018-10-09 13:12:15 +0200193 unsigned int back_thres; /* Threshold that shall be maintained by
194 * the backpointer in order to respect
195 * geo->mw_cunits on a per chunk basis
196 */
197
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200198 struct list_head pages; /* List of data pages */
199
200 spinlock_t w_lock; /* Write lock */
201 spinlock_t s_lock; /* Sync lock */
202
Matias Bjørling880eda52018-07-13 10:48:37 +0200203#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100204 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200205#endif
206};
207
208#define PBLK_RECOVERY_SECTORS 16
209
210struct pblk_lun {
211 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212 struct semaphore wr_sem;
213};
214
215struct pblk_gc_rq {
216 struct pblk_line *line;
217 void *data;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200218 u64 paddr_list[NVM_MAX_VLBA];
219 u64 lba_list[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220 int nr_secs;
221 int secs_to_gc;
222 struct list_head list;
223};
224
225struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200226 /* These states are not protected by a lock since (i) they are in the
227 * fast path, and (ii) they are not critical.
228 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229 int gc_active;
230 int gc_enabled;
231 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200232
233 struct task_struct *gc_ts;
234 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200235 struct task_struct *gc_reader_ts;
236
237 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200240 struct timer_list gc_timer;
241
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200242 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200243 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
244 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
245 * started reads to finished writes
246 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200247 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200248
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200249 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200250 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251
252 spinlock_t lock;
253 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200254 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255};
256
257struct pblk_rl {
258 unsigned int high; /* Upper threshold for rate limiter (free run -
259 * user I/O rate limiter
260 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200261 unsigned int high_pw; /* High rounded up as a power of 2 */
262
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200263#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
264#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265
266 int rb_windows_pw; /* Number of rate windows in the write buffer
267 * given as a power-of-2. This guarantees that
268 * when user I/O is being rate limited, there
269 * will be reserved enough space for the GC to
270 * place its payload. A window is of
271 * pblk->max_write_pgs size, which in NVMe is
272 * 64, i.e., 256kb.
273 */
274 int rb_budget; /* Total number of entries available for I/O */
275 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200276 int rb_gc_max; /* Max buffer entries available for GC I/O */
277 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
278 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200279 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200280
281 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200283 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200284
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200285 int rsv_blocks; /* Reserved blocks for GC */
286
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200287 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200288 int rb_gc_active;
289
Hans Holmberg48b8d202018-06-01 16:41:06 +0200290 atomic_t werr_lines; /* Number of write error lines that needs gc */
291
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200292 struct timer_list u_timer;
293
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200294 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100295
296 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
297 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298};
299
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300#define PBLK_LINE_EMPTY (~0U)
301
302enum {
303 /* Line Types */
304 PBLK_LINETYPE_FREE = 0,
305 PBLK_LINETYPE_LOG = 1,
306 PBLK_LINETYPE_DATA = 2,
307
308 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200309 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200310 PBLK_LINESTATE_FREE = 10,
311 PBLK_LINESTATE_OPEN = 11,
312 PBLK_LINESTATE_CLOSED = 12,
313 PBLK_LINESTATE_GC = 13,
314 PBLK_LINESTATE_BAD = 14,
315 PBLK_LINESTATE_CORRUPT = 15,
316
317 /* GC group */
318 PBLK_LINEGC_NONE = 20,
319 PBLK_LINEGC_EMPTY = 21,
320 PBLK_LINEGC_LOW = 22,
321 PBLK_LINEGC_MID = 23,
322 PBLK_LINEGC_HIGH = 24,
323 PBLK_LINEGC_FULL = 25,
Hans Holmberg48b8d202018-06-01 16:41:06 +0200324 PBLK_LINEGC_WERR = 26
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325};
326
327#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200328
329/* emeta/smeta persistent storage format versions:
330 * Changes in major version requires offline migration.
331 * Changes in minor version are handled automatically during
332 * recovery.
333 */
334
335#define SMETA_VERSION_MAJOR (0)
336#define SMETA_VERSION_MINOR (1)
337
338#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200339#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200340
341struct line_header {
342 __le32 crc;
343 __le32 identifier; /* pblk identifier */
344 __u8 uuid[16]; /* instance uuid */
345 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200346 __u8 version_major; /* version major */
347 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200348 __le32 id; /* line id for current line */
349};
350
351struct line_smeta {
352 struct line_header header;
353
354 __le32 crc; /* Full structure including struct crc */
355 /* Previous line metadata */
356 __le32 prev_id; /* Line id for previous line */
357
358 /* Current line metadata */
359 __le64 seq_nr; /* Sequence number for current line */
360
361 /* Active writers */
362 __le32 window_wr_lun; /* Number of parallel LUNs to write */
363
364 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200365
366 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200367};
368
Hans Holmberg76758392018-03-30 00:04:52 +0200369
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200371 * Metadata layout in media:
372 * First sector:
373 * 1. struct line_emeta
374 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200375 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200376 * Mid sectors (start at lbas_sector):
377 * 3. nr_lbas (u64) forming lba list
378 * Last sectors (start at vsc_sector):
379 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200380 */
381struct line_emeta {
382 struct line_header header;
383
384 __le32 crc; /* Full structure including struct crc */
385
386 /* Previous line metadata */
387 __le32 prev_id; /* Line id for prev line */
388
389 /* Current line metadata */
390 __le64 seq_nr; /* Sequence number for current line */
391
392 /* Active writers */
393 __le32 window_wr_lun; /* Number of parallel LUNs to write */
394
395 /* Bookkeeping for recovery */
396 __le32 next_id; /* Line id for next line */
397 __le64 nr_lbas; /* Number of lbas mapped in line */
398 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200399 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
400};
401
402
403/* Write amplification counters stored on media */
404struct wa_counters {
405 __le64 user; /* Number of user written sectors */
406 __le64 gc; /* Number of sectors written by GC*/
407 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200408};
409
410struct pblk_emeta {
411 struct line_emeta *buf; /* emeta buffer in media format */
412 int mem; /* Write offset - points to next
413 * writable entry in memory
414 */
415 atomic_t sync; /* Synced - backpointer that signals the
416 * last entry that has been successfully
417 * persisted to media
418 */
419 unsigned int nr_entries; /* Number of emeta entries */
420};
421
422struct pblk_smeta {
423 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424};
425
Hans Holmberg48b8d202018-06-01 16:41:06 +0200426struct pblk_w_err_gc {
427 int has_write_err;
Igor Konopkof2e02452019-05-04 20:38:05 +0200428 int has_gc_err;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200429 __le64 *lba_list;
430};
431
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200432struct pblk_line {
433 struct pblk *pblk;
434 unsigned int id; /* Line number corresponds to the
435 * block line
436 */
437 unsigned int seq_nr; /* Unique line sequence number */
438
439 int state; /* PBLK_LINESTATE_X */
440 int type; /* PBLK_LINETYPE_X */
441 int gc_group; /* PBLK_LINEGC_X */
442 struct list_head list; /* Free, GC lists */
443
444 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
445
Javier González32ef9412018-03-30 00:05:20 +0200446 struct nvm_chk_meta *chks; /* Chunks forming line */
447
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200448 struct pblk_smeta *smeta; /* Start metadata */
449 struct pblk_emeta *emeta; /* End medatada */
450
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200452 int meta_distance; /* Distance between data and metadata */
453
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 u64 emeta_ssec; /* Sector where emeta starts */
455
456 unsigned int sec_in_line; /* Number of usable secs in line */
457
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200458 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200459 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
460 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
461
462 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
463 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
464
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200465 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200466 atomic_t left_seblks; /* Blocks left for sync erasing */
467
468 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200469 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200470 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
471
472 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200473
474 struct kref ref; /* Write buffer L2P references */
Heiner Litz05869422019-02-11 13:25:09 +0100475 atomic_t sec_to_update; /* Outstanding L2P updates to ppa */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200476
Hans Holmberg48b8d202018-06-01 16:41:06 +0200477 struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
478
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200479 spinlock_t lock; /* Necessary for invalid_bitmap only */
480};
481
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200482#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200483
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200484enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200485 PBLK_KMALLOC_META = 1,
486 PBLK_VMALLOC_META = 2,
487};
488
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200489enum {
490 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
491 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
492 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200493};
494
495struct pblk_line_mgmt {
496 int nr_lines; /* Total number of full lines */
497 int nr_free_lines; /* Number of full lines in free list */
498
499 /* Free lists - use free_lock */
500 struct list_head free_list; /* Full lines ready to use */
501 struct list_head corrupt_list; /* Full lines corrupted */
502 struct list_head bad_list; /* Full lines bad */
503
504 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200505 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
507 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
508 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
509
Hans Holmberg48b8d202018-06-01 16:41:06 +0200510 struct list_head gc_werr_list; /* Write err recovery list */
511
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200512 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
513 struct list_head gc_empty_list; /* Full lines close, all valid */
514
515 struct pblk_line *log_line; /* Current FTL log line */
516 struct pblk_line *data_line; /* Current data line */
517 struct pblk_line *log_next; /* Next FTL log line */
518 struct pblk_line *data_next; /* Next data line */
519
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200520 struct list_head emeta_list; /* Lines queued to schedule emeta */
521
522 __le32 *vsc_list; /* Valid sector counts for all lines */
523
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200525 int emeta_alloc_type;
526
527 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200528 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
529 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200530 unsigned long meta_bitmap;
531
Hans Holmberg53d82db2018-10-09 13:11:47 +0200532 /* Cache and mempool for map/invalid bitmaps */
533 struct kmem_cache *bitmap_cache;
534 mempool_t *bitmap_pool;
535
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536 /* Helpers for fast bitmap calculations */
537 unsigned long *bb_template;
538 unsigned long *bb_aux;
539
540 unsigned long d_seq_nr; /* Data line unique sequence number */
541 unsigned long l_seq_nr; /* Log line unique sequence number */
542
543 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200544 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545 spinlock_t gc_lock;
546};
547
548struct pblk_line_meta {
549 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200550 unsigned int smeta_sec; /* Sectors needed for smeta */
551
552 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200553 * [0]: Total
554 * [1]: struct line_emeta +
555 * bb_bitmap + struct wa_counters
556 * [2]: L2P portion
557 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200558 */
559 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
560 * as emeta_len
561 */
562
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200564
565 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
567 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
568 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
569
570 unsigned int blk_per_line; /* Number of blocks in a full line */
571 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200572 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200573 unsigned int min_blk_line; /* Min. number of good blocks in line */
574
575 unsigned int mid_thrs; /* Threshold for GC mid list */
576 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200577
578 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579};
580
Javier González588726d32017-06-26 11:57:29 +0200581enum {
582 PBLK_STATE_RUNNING = 0,
583 PBLK_STATE_STOPPING = 1,
584 PBLK_STATE_RECOVERING = 2,
585 PBLK_STATE_STOPPED = 3,
586};
587
Javier González3b2a3ad2018-03-30 00:05:21 +0200588/* Internal format to support not power-of-2 device formats */
589struct pblk_addrf {
590 /* gen to dev */
591 int sec_stripe;
592 int ch_stripe;
593 int lun_stripe;
594
595 /* dev to gen */
596 int sec_lun_stripe;
597 int sec_ws_stripe;
598};
599
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200600struct pblk {
601 struct nvm_tgt_dev *dev;
602 struct gendisk *disk;
603
604 struct kobject kobj;
605
606 struct pblk_lun *luns;
607
608 struct pblk_line *lines; /* Line array */
609 struct pblk_line_mgmt l_mg; /* Line management */
610 struct pblk_line_meta lm; /* Line metadata */
611
Javier González3b2a3ad2018-03-30 00:05:21 +0200612 struct nvm_addrf addrf; /* Aligned address format */
613 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200614 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615
616 struct pblk_rb rwb;
617
Javier González588726d32017-06-26 11:57:29 +0200618 int state; /* pblk line state */
619
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620 int min_write_pgs; /* Minimum amount of pages required by controller */
Igor Konopko55d8ec32018-12-11 20:16:26 +0100621 int min_write_pgs_data; /* Minimum amount of payload pages */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200622 int max_write_pgs; /* Maximum amount of pages supported by controller */
Igor Konopkofaa79f22018-12-11 20:16:23 +0100623 int oob_meta_size; /* Size of OOB sector metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624
625 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100626
627 int op; /* Percentage of device used for over-provisioning */
628 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200629
630 /* pblk provisioning values. Used by rate limiter */
631 struct pblk_rl rl;
632
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200633 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634
Andy Shevchenko7e0a0842019-02-11 13:25:05 +0100635 guid_t instance_uuid;
Hans Holmberg76758392018-03-30 00:04:52 +0200636
637 /* Persistent write amplification counters, 4kb sector I/Os */
638 atomic64_t user_wa; /* Sectors written by user */
639 atomic64_t gc_wa; /* Sectors written by GC */
640 atomic64_t pad_wa; /* Padded sectors written */
641
642 /* Reset values for delta write amplification measurements */
643 u64 user_rst_wa;
644 u64 gc_rst_wa;
645 u64 pad_rst_wa;
646
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200647 /* Counters used for calculating padding distribution */
648 atomic64_t *pad_dist; /* Padding distribution buckets */
649 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
650 atomic64_t nr_flush; /* Number of flush/fua I/O */
651
Matias Bjørling880eda52018-07-13 10:48:37 +0200652#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200653 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200654 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
655 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
656 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657 atomic_long_t req_writes; /* Sectors stored on write buffer */
658 atomic_long_t sub_writes; /* Sectors submitted from buffer */
659 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200661 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662 atomic_long_t sync_reads; /* Completed sector read requests */
663 atomic_long_t recov_writes; /* Sectors submitted from recovery */
664 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
665 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
666#endif
667
668 spinlock_t lock;
669
670 atomic_long_t read_failed;
671 atomic_long_t read_empty;
672 atomic_long_t read_high_ecc;
673 atomic_long_t read_failed_gc;
674 atomic_long_t write_failed;
675 atomic_long_t erase_failed;
676
Javier González588726d32017-06-26 11:57:29 +0200677 atomic_t inflight_io; /* General inflight I/O counter */
678
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 struct task_struct *writer_ts;
680
681 /* Simple translation map of logical addresses to physical addresses.
682 * The logical addresses is known by the host system, while the physical
683 * addresses are used when writing to the disk block device.
684 */
685 unsigned char *trans_map;
686 spinlock_t trans_lock;
687
688 struct list_head compl_list;
689
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200690 spinlock_t resubmit_lock; /* Resubmit list lock */
691 struct list_head resubmit_list; /* Resubmit list for failed writes*/
692
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400693 mempool_t page_bio_pool;
694 mempool_t gen_ws_pool;
695 mempool_t rec_pool;
696 mempool_t r_rq_pool;
697 mempool_t w_rq_pool;
698 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200699
Javier Gonzálezef576492017-06-26 11:57:28 +0200700 struct workqueue_struct *close_wq;
701 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200702 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200703
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200704 struct timer_list wtimer;
705
706 struct pblk_gc gc;
707};
708
709struct pblk_line_ws {
710 struct pblk *pblk;
711 struct pblk_line *line;
712 void *priv;
713 struct work_struct ws;
714};
715
Javier González084ec9b2017-06-26 16:27:13 -0600716#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
718
Matias Bjørling4e495a42018-07-13 10:48:42 +0200719#define pblk_err(pblk, fmt, ...) \
720 pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
721#define pblk_info(pblk, fmt, ...) \
722 pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
723#define pblk_warn(pblk, fmt, ...) \
724 pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
725#define pblk_debug(pblk, fmt, ...) \
726 pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
727
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200728/*
729 * pblk ring buffer operations
730 */
Javier González766c8ce2018-10-09 13:12:15 +0200731int pblk_rb_init(struct pblk_rb *rb, unsigned int size, unsigned int threshold,
732 unsigned int seg_sz);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200733int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
734 unsigned int nr_entries, unsigned int *pos);
735int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
736 unsigned int *pos);
737void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
738 struct pblk_w_ctx w_ctx, unsigned int pos);
739void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200740 struct pblk_w_ctx w_ctx, struct pblk_line *line,
741 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200742struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200743void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200744
745void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200746unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200747 unsigned int pos, unsigned int nr_entries,
748 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200749int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Igor Konopkoa96de642019-05-04 20:38:10 +0200750 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200751unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
752
753unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
754unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
Javier González40b86572018-10-09 13:12:09 +0200755unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p,
756 unsigned int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100758unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200759
760unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200761unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
763
764int pblk_rb_tear_down_check(struct pblk_rb *rb);
765int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
Javier González9bd1f872018-10-09 13:12:10 +0200766void pblk_rb_free(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200767ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
768
769/*
770 * pblk core
771 */
Javier González67bf26a2017-10-13 14:46:20 +0200772struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
773void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier González45dcf292018-10-09 13:12:02 +0200774int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
775void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200776void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200777int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
778 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200779void pblk_discard(struct pblk *pblk, struct bio *bio);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200780struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk);
Javier González32ef9412018-03-30 00:05:20 +0200781struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
782 struct nvm_chk_meta *lp,
783 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200784void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
785void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
786int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200787int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier González253babc2018-10-09 13:12:04 +0200788int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200789int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200790void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200791struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
792 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200793 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200794struct pblk_line *pblk_line_get(struct pblk *pblk);
795struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200796struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Matias Bjørlingae14cc02018-10-09 13:11:40 +0200797void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa);
798void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200799int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
800void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
801struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200802struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200803int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
804int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200805void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200806void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200807void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200808void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200809void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200810void __pblk_pipeline_stop(struct pblk *pblk);
811void __pblk_pipeline_flush(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200812void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
813 void (*work)(struct work_struct *), gfp_t gfp_mask,
814 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200815u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200816int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line);
817int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200818 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200819int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
820void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200821void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200822struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200823u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
824void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200825u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200826u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200827int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
Igor Konopko55d8ec32018-12-11 20:16:26 +0100828 unsigned long secs_to_flush, bool skip_meta);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200829void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830 unsigned long *lun_bitmap);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200831void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa);
832void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa);
Hans Holmberge99e8022018-10-09 13:11:48 +0200833void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200834int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
835 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
837 int nr_pages);
838void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200839void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
840 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200841void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
842void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
843 struct ppa_addr ppa);
844void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
845 struct ppa_addr ppa, struct ppa_addr entry_line);
846int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200847 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200848void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
849 u64 *lba_list, int nr_secs);
Igor Konopkoa96de642019-05-04 20:38:10 +0200850int pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
851 sector_t blba, int nr_secs, bool *from_cache);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100852void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd);
853void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854
855/*
856 * pblk user I/O write path
857 */
Igor Konopko3e03f632019-05-04 20:38:06 +0200858void pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200859 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200860int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200861
862/*
863 * pblk map
864 */
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100865int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866 unsigned int sentry, unsigned long *lun_bitmap,
867 unsigned int valid_secs, struct ppa_addr *erase_ppa);
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100868int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869 unsigned long *lun_bitmap, unsigned int valid_secs,
870 unsigned int off);
871
872/*
873 * pblk write thread
874 */
875int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700876void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200877void pblk_write_should_kick(struct pblk *pblk);
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200878void pblk_write_kick(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200879
880/*
881 * pblk read path
882 */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400883extern struct bio_set pblk_bio_set;
Igor Konopko3e03f632019-05-04 20:38:06 +0200884void pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200885int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886/*
887 * pblk recovery
888 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200889struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200890int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100891int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892
893/*
894 * pblk gc
895 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200896#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200897#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200898#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200899
900int pblk_gc_init(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200901void pblk_gc_exit(struct pblk *pblk, bool graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902void pblk_gc_should_start(struct pblk *pblk);
903void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200904void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200905void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200906void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
907 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200908int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Igor Konopkof2e02452019-05-04 20:38:05 +0200909void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200910
911/*
912 * pblk rate limiter
913 */
Javier Gonzálezb4cdc422019-02-11 13:25:08 +0100914void pblk_rl_init(struct pblk_rl *rl, int budget, int threshold);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200915void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200916void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200917int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200918unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100919unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200920int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200921void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200922void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
923int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
924void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
925void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200926int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100928void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
929 bool used);
Javier González588726d32017-06-26 11:57:29 +0200930int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200931
Hans Holmberg48b8d202018-06-01 16:41:06 +0200932void pblk_rl_werr_line_in(struct pblk_rl *rl);
933void pblk_rl_werr_line_out(struct pblk_rl *rl);
934
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200935/*
936 * pblk sysfs
937 */
938int pblk_sysfs_init(struct gendisk *tdisk);
939void pblk_sysfs_exit(struct gendisk *tdisk);
940
941static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
942{
943 if (type == PBLK_KMALLOC_META)
944 return kmalloc(size, flags);
945 return vmalloc(size);
946}
947
948static inline void pblk_mfree(void *ptr, int type)
949{
950 if (type == PBLK_KMALLOC_META)
951 kfree(ptr);
952 else
953 vfree(ptr);
954}
955
956static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
957{
958 return c_ctx - sizeof(struct nvm_rq);
959}
960
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200961static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200962{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200963 return emeta->bb_bitmap;
964}
965
Hans Holmberg76758392018-03-30 00:04:52 +0200966static inline void *emeta_to_wa(struct pblk_line_meta *lm,
967 struct line_emeta *emeta)
968{
969 return emeta->bb_bitmap + lm->blk_bitmap_len;
970}
971
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200972static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
973{
974 return ((void *)emeta + pblk->lm.emeta_len[1]);
975}
976
977static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
978{
979 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200980}
981
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200982static inline int pblk_line_vsc(struct pblk_line *line)
983{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200984 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +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{
Javier González7f985f92018-10-09 13:11:56 +02001095 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001096
Javier González7f985f92018-10-09 13:11:56 +02001097 return nvm_ppa32_to_ppa64(dev->parent, &pblk->addrf, ppa32);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001098}
1099
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001100static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1101{
Javier González7f985f92018-10-09 13:11:56 +02001102 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001103
Javier González7f985f92018-10-09 13:11:56 +02001104 return nvm_ppa64_to_ppa32(dev->parent, &pblk->addrf, ppa64);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105}
1106
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001107static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1108 sector_t lba)
1109{
1110 struct ppa_addr ppa;
1111
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001112 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001113 u32 *map = (u32 *)pblk->trans_map;
1114
1115 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1116 } else {
1117 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1118
1119 ppa = map[lba];
1120 }
1121
1122 return ppa;
1123}
1124
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001125static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1126 struct ppa_addr ppa)
1127{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001128 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001129 u32 *map = (u32 *)pblk->trans_map;
1130
1131 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1132 } else {
1133 u64 *map = (u64 *)pblk->trans_map;
1134
1135 map[lba] = ppa.ppa;
1136 }
1137}
1138
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001139static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1140{
1141 return (ppa_addr.ppa == ADDR_EMPTY);
1142}
1143
1144static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1145{
1146 ppa_addr->ppa = ADDR_EMPTY;
1147}
1148
Javier González076984662017-06-30 17:56:42 +02001149static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1150{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001151 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001152}
1153
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001154static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1155{
1156 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1157}
1158
1159static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1160{
1161 return ppa.c.line;
1162}
1163
1164static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1165{
1166 struct ppa_addr p;
1167
1168 p.c.line = addr;
1169 p.c.is_cached = 1;
1170
1171 return p;
1172}
1173
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001174static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001175 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001176{
1177 u32 crc = ~(u32)0;
1178
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001179 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001180 sizeof(struct line_header) - sizeof(crc));
1181
1182 return crc;
1183}
1184
1185static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1186 struct line_smeta *smeta)
1187{
1188 struct pblk_line_meta *lm = &pblk->lm;
1189 u32 crc = ~(u32)0;
1190
1191 crc = crc32_le(crc, (unsigned char *)smeta +
1192 sizeof(struct line_header) + sizeof(crc),
1193 lm->smeta_len -
1194 sizeof(struct line_header) - sizeof(crc));
1195
1196 return crc;
1197}
1198
1199static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1200 struct line_emeta *emeta)
1201{
1202 struct pblk_line_meta *lm = &pblk->lm;
1203 u32 crc = ~(u32)0;
1204
1205 crc = crc32_le(crc, (unsigned char *)emeta +
1206 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001207 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001208 sizeof(struct line_header) - sizeof(crc));
1209
1210 return crc;
1211}
1212
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001213static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1214{
1215 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001216}
1217
Matias Bjørling880eda52018-07-13 10:48:37 +02001218#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001219static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p,
Javier González3b2a3ad2018-03-30 00:05:21 +02001220 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001221{
Matias Bjørling4e495a42018-07-13 10:48:42 +02001222 struct nvm_geo *geo = &pblk->dev->geo;
1223
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001224 if (p->c.is_cached) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001225 pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001226 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001227 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001228 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 +02001229 msg, error,
1230 p->g.ch, p->g.lun, p->g.blk,
1231 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001232 } else {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001233 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
Javier González3b2a3ad2018-03-30 00:05:21 +02001234 msg, error,
1235 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001236 }
1237}
1238
1239static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1240 int error)
1241{
1242 int bit = -1;
1243
1244 if (rqd->nr_ppas == 1) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001245 print_ppa(pblk, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001246 return;
1247 }
1248
1249 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1250 bit + 1)) < rqd->nr_ppas) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001251 print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001252 }
1253
Matias Bjørling4e495a42018-07-13 10:48:42 +02001254 pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001255}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001256
1257static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1258 struct ppa_addr *ppas, int nr_ppas)
1259{
1260 struct nvm_geo *geo = &tgt_dev->geo;
1261 struct ppa_addr *ppa;
1262 int i;
1263
1264 for (i = 0; i < nr_ppas; i++) {
1265 ppa = &ppas[i];
1266
Javier González3b2a3ad2018-03-30 00:05:21 +02001267 if (geo->version == NVM_OCSSD_SPEC_12) {
1268 if (!ppa->c.is_cached &&
1269 ppa->g.ch < geo->num_ch &&
1270 ppa->g.lun < geo->num_lun &&
1271 ppa->g.pl < geo->num_pln &&
1272 ppa->g.blk < geo->num_chk &&
1273 ppa->g.pg < geo->num_pg &&
1274 ppa->g.sec < geo->ws_min)
1275 continue;
1276 } else {
1277 if (!ppa->c.is_cached &&
1278 ppa->m.grp < geo->num_ch &&
1279 ppa->m.pu < geo->num_lun &&
1280 ppa->m.chk < geo->num_chk &&
1281 ppa->m.sec < geo->clba)
1282 continue;
1283 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001284
Matias Bjørling4e495a42018-07-13 10:48:42 +02001285 print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001286
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287 return 1;
1288 }
1289 return 0;
1290}
1291
Javier González1a94b2d2017-10-13 14:46:47 +02001292static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1293{
1294 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmbergd68a9342018-10-09 13:11:46 +02001295 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González1a94b2d2017-10-13 14:46:47 +02001296
1297 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1298 WARN_ON(1);
1299 return -EINVAL;
1300 }
1301
1302 if (rqd->opcode == NVM_OP_PWRITE) {
1303 struct pblk_line *line;
Javier González1a94b2d2017-10-13 14:46:47 +02001304 int i;
1305
1306 for (i = 0; i < rqd->nr_ppas; i++) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02001307 line = pblk_ppa_to_line(pblk, ppa_list[i]);
Javier González1a94b2d2017-10-13 14:46:47 +02001308
1309 spin_lock(&line->lock);
1310 if (line->state != PBLK_LINESTATE_OPEN) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001311 pblk_err(pblk, "bad ppa: line:%d,state:%d\n",
Javier González1a94b2d2017-10-13 14:46:47 +02001312 line->id, line->state);
1313 WARN_ON(1);
1314 spin_unlock(&line->lock);
1315 return -EINVAL;
1316 }
1317 spin_unlock(&line->lock);
1318 }
1319 }
1320
1321 return 0;
1322}
1323#endif
1324
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001325static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1326{
1327 struct pblk_line_meta *lm = &pblk->lm;
1328
1329 if (paddr > lm->sec_per_line)
1330 return 1;
1331
1332 return 0;
1333}
1334
1335static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1336{
1337 return bio->bi_iter.bi_idx;
1338}
1339
1340static inline sector_t pblk_get_lba(struct bio *bio)
1341{
1342 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1343}
1344
1345static inline unsigned int pblk_get_secs(struct bio *bio)
1346{
1347 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1348}
1349
Hans Holmberg4c44abf2018-10-09 13:11:52 +02001350static inline char *pblk_disk_name(struct pblk *pblk)
1351{
1352 struct gendisk *disk = pblk->disk;
1353
1354 return disk->disk_name;
1355}
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001356
1357static inline unsigned int pblk_get_min_chks(struct pblk *pblk)
1358{
1359 struct pblk_line_meta *lm = &pblk->lm;
1360 /* In a worst-case scenario every line will have OP invalid sectors.
1361 * We will then need a minimum of 1/OP lines to free up a single line
1362 */
1363
1364 return DIV_ROUND_UP(100, pblk->op) * lm->blk_per_line;
Igor Konopkofaa79f22018-12-11 20:16:23 +01001365}
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001366
Igor Konopkofaa79f22018-12-11 20:16:23 +01001367static inline struct pblk_sec_meta *pblk_get_meta(struct pblk *pblk,
1368 void *meta, int index)
1369{
Igor Konopko2c4d5352018-12-11 20:16:27 +01001370 return meta +
1371 max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1372 * index;
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001373}
Igor Konopko24828d02018-12-11 20:16:24 +01001374
1375static inline int pblk_dma_meta_size(struct pblk *pblk)
1376{
Igor Konopko2c4d5352018-12-11 20:16:27 +01001377 return max_t(int, sizeof(struct pblk_sec_meta), pblk->oob_meta_size)
1378 * NVM_MAX_VLBA;
Igor Konopko24828d02018-12-11 20:16:24 +01001379}
Igor Konopko55d8ec32018-12-11 20:16:26 +01001380
1381static inline int pblk_is_oob_meta_supported(struct pblk *pblk)
1382{
1383 return pblk->oob_meta_size >= sizeof(struct pblk_sec_meta);
1384}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001385#endif /* PBLK_H_ */