blob: 97c0dd5f4857347585fcf540ee020dae08727ac0 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Matias Bjorling <matias@cnexlabs.com>
5 * Write buffering: Javier Gonzalez <javier@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * Implementation of a Physical Block-device target for Open-channel SSDs.
17 *
18 */
19
20#ifndef PBLK_H_
21#define PBLK_H_
22
23#include <linux/blkdev.h>
24#include <linux/blk-mq.h>
25#include <linux/bio.h>
26#include <linux/module.h>
27#include <linux/kthread.h>
28#include <linux/vmalloc.h>
29#include <linux/crc32.h>
30#include <linux/uuid.h>
31
32#include <linux/lightnvm.h>
33
34/* Run only GC if less than 1/X blocks are free */
35#define GC_LIMIT_INVERSE 5
36#define GC_TIME_MSECS 1000
37
38#define PBLK_SECTOR (512)
39#define PBLK_EXPOSED_PAGE_SIZE (4096)
40#define PBLK_MAX_REQ_ADDRS (64)
41#define PBLK_MAX_REQ_ADDRS_PW (6)
42
Javier Gonzálezef576492017-06-26 11:57:28 +020043#define PBLK_NR_CLOSE_JOBS (4)
44
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045#define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
46
47#define PBLK_COMMAND_TIMEOUT_MS 30000
48
49/* Max 512 LUNs per device */
50#define PBLK_MAX_LUNS_BITMAP (4)
51
52#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
53
Javier González0d880392017-10-13 14:46:08 +020054/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020055#define PBLK_GEN_WS_POOL_SIZE (2)
56
Javier Gonzáleze5392732018-01-05 14:16:14 +010057#define PBLK_DEFAULT_OP (11)
58
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020060 PBLK_READ = READ,
61 PBLK_WRITE = WRITE,/* Write from write buffer */
62 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010063 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020064 PBLK_ERASE,
65};
66
67enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 /* IO Types */
69 PBLK_IOTYPE_USER = 1 << 0,
70 PBLK_IOTYPE_GC = 1 << 1,
71
72 /* Write buffer flags */
73 PBLK_FLUSH_ENTRY = 1 << 2,
74 PBLK_WRITTEN_DATA = 1 << 3,
75 PBLK_SUBMITTED_ENTRY = 1 << 4,
76 PBLK_WRITABLE_ENTRY = 1 << 5,
77};
78
79enum {
80 PBLK_BLK_ST_OPEN = 0x1,
81 PBLK_BLK_ST_CLOSED = 0x2,
82};
83
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020084struct pblk_sec_meta {
85 u64 reserved;
86 __le64 lba;
87};
88
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020089/* The number of GC lists and the rate-limiter states go together. This way the
90 * rate-limiter can dictate how much GC is needed based on resource utilization.
91 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020092#define PBLK_GC_NR_LISTS 3
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020093
94enum {
95 PBLK_RL_HIGH = 1,
96 PBLK_RL_MID = 2,
97 PBLK_RL_LOW = 3,
98};
99
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200101#define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200102
Javier González084ec9b2017-06-26 16:27:13 -0600103/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104struct pblk_c_ctx {
105 struct list_head list; /* Head for out-of-order completion */
106
107 unsigned long *lun_bitmap; /* Luns used on current request */
108 unsigned int sentry;
109 unsigned int nr_valid;
110 unsigned int nr_padded;
111};
112
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200113/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600114struct pblk_g_ctx {
115 void *private;
Javier González998ba622018-01-05 14:16:20 +0100116 unsigned long start_time;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200117 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200118};
119
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200120/* Pad context */
121struct pblk_pad_rq {
122 struct pblk *pblk;
123 struct completion wait;
124 struct kref ref;
125};
126
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200127/* Recovery context */
128struct pblk_rec_ctx {
129 struct pblk *pblk;
130 struct nvm_rq *rqd;
131 struct list_head failed;
132 struct work_struct ws_rec;
133};
134
135/* Write context */
136struct pblk_w_ctx {
137 struct bio_list bios; /* Original bios - used for completion
138 * in REQ_FUA, REQ_FLUSH case
139 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200140 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200141 struct ppa_addr ppa; /* Physic addr. associated with entry */
142 int flags; /* Write context flags */
143};
144
145struct pblk_rb_entry {
146 struct ppa_addr cacheline; /* Cacheline for this entry */
147 void *data; /* Pointer to data on this entry */
148 struct pblk_w_ctx w_ctx; /* Context for this entry */
149 struct list_head index; /* List head to enable indexes */
150};
151
152#define EMPTY_ENTRY (~0U)
153
154struct pblk_rb_pages {
155 struct page *pages;
156 int order;
157 struct list_head list;
158};
159
160struct pblk_rb {
161 struct pblk_rb_entry *entries; /* Ring buffer entries */
162 unsigned int mem; /* Write offset - points to next
163 * writable entry in memory
164 */
165 unsigned int subm; /* Read offset - points to last entry
166 * that has been submitted to the media
167 * to be persisted
168 */
169 unsigned int sync; /* Synced - backpointer that signals
170 * the last submitted entry that has
171 * been successfully persisted to media
172 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100173 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 * flushed to the media. Used with
175 * REQ_FLUSH and REQ_FUA
176 */
177 unsigned int l2p_update; /* l2p update point - next entry for
178 * which l2p mapping will be updated to
179 * contain a device ppa address (instead
180 * of a cacheline
181 */
182 unsigned int nr_entries; /* Number of entries in write buffer -
183 * must be a power of two
184 */
185 unsigned int seg_size; /* Size of the data segments being
186 * stored on each entry. Typically this
187 * will be 4KB
188 */
189
190 struct list_head pages; /* List of data pages */
191
192 spinlock_t w_lock; /* Write lock */
193 spinlock_t s_lock; /* Sync lock */
194
195#ifdef CONFIG_NVM_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100196 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197#endif
198};
199
200#define PBLK_RECOVERY_SECTORS 16
201
202struct pblk_lun {
203 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200204 struct semaphore wr_sem;
205};
206
207struct pblk_gc_rq {
208 struct pblk_line *line;
209 void *data;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200210 u64 paddr_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200211 u64 lba_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212 int nr_secs;
213 int secs_to_gc;
214 struct list_head list;
215};
216
217struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200218 /* These states are not protected by a lock since (i) they are in the
219 * fast path, and (ii) they are not critical.
220 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221 int gc_active;
222 int gc_enabled;
223 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224
225 struct task_struct *gc_ts;
226 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200227 struct task_struct *gc_reader_ts;
228
229 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200230 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200231
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200232 struct timer_list gc_timer;
233
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200234 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200235 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
236 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
237 * started reads to finished writes
238 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200239 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200240
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200241 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200242 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200243
244 spinlock_t lock;
245 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200246 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200247};
248
249struct pblk_rl {
250 unsigned int high; /* Upper threshold for rate limiter (free run -
251 * user I/O rate limiter
252 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253 unsigned int high_pw; /* High rounded up as a power of 2 */
254
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200255#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
256#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200257
258 int rb_windows_pw; /* Number of rate windows in the write buffer
259 * given as a power-of-2. This guarantees that
260 * when user I/O is being rate limited, there
261 * will be reserved enough space for the GC to
262 * place its payload. A window is of
263 * pblk->max_write_pgs size, which in NVMe is
264 * 64, i.e., 256kb.
265 */
266 int rb_budget; /* Total number of entries available for I/O */
267 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200268 int rb_gc_max; /* Max buffer entries available for GC I/O */
269 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
270 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200271 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200272
273 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200275 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200276
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200277 int rsv_blocks; /* Reserved blocks for GC */
278
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200279 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200280 int rb_gc_active;
281
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282 struct timer_list u_timer;
283
284 unsigned long long nr_secs;
285 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100286
287 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
288 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200289};
290
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200291#define PBLK_LINE_EMPTY (~0U)
292
293enum {
294 /* Line Types */
295 PBLK_LINETYPE_FREE = 0,
296 PBLK_LINETYPE_LOG = 1,
297 PBLK_LINETYPE_DATA = 2,
298
299 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200300 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301 PBLK_LINESTATE_FREE = 10,
302 PBLK_LINESTATE_OPEN = 11,
303 PBLK_LINESTATE_CLOSED = 12,
304 PBLK_LINESTATE_GC = 13,
305 PBLK_LINESTATE_BAD = 14,
306 PBLK_LINESTATE_CORRUPT = 15,
307
308 /* GC group */
309 PBLK_LINEGC_NONE = 20,
310 PBLK_LINEGC_EMPTY = 21,
311 PBLK_LINEGC_LOW = 22,
312 PBLK_LINEGC_MID = 23,
313 PBLK_LINEGC_HIGH = 24,
314 PBLK_LINEGC_FULL = 25,
315};
316
317#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200318
319/* emeta/smeta persistent storage format versions:
320 * Changes in major version requires offline migration.
321 * Changes in minor version are handled automatically during
322 * recovery.
323 */
324
325#define SMETA_VERSION_MAJOR (0)
326#define SMETA_VERSION_MINOR (1)
327
328#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200329#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330
331struct line_header {
332 __le32 crc;
333 __le32 identifier; /* pblk identifier */
334 __u8 uuid[16]; /* instance uuid */
335 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200336 __u8 version_major; /* version major */
337 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200338 __le32 id; /* line id for current line */
339};
340
341struct line_smeta {
342 struct line_header header;
343
344 __le32 crc; /* Full structure including struct crc */
345 /* Previous line metadata */
346 __le32 prev_id; /* Line id for previous line */
347
348 /* Current line metadata */
349 __le64 seq_nr; /* Sequence number for current line */
350
351 /* Active writers */
352 __le32 window_wr_lun; /* Number of parallel LUNs to write */
353
354 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200355
356 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200357};
358
Hans Holmberg76758392018-03-30 00:04:52 +0200359
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200361 * Metadata layout in media:
362 * First sector:
363 * 1. struct line_emeta
364 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200365 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200366 * Mid sectors (start at lbas_sector):
367 * 3. nr_lbas (u64) forming lba list
368 * Last sectors (start at vsc_sector):
369 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370 */
371struct line_emeta {
372 struct line_header header;
373
374 __le32 crc; /* Full structure including struct crc */
375
376 /* Previous line metadata */
377 __le32 prev_id; /* Line id for prev line */
378
379 /* Current line metadata */
380 __le64 seq_nr; /* Sequence number for current line */
381
382 /* Active writers */
383 __le32 window_wr_lun; /* Number of parallel LUNs to write */
384
385 /* Bookkeeping for recovery */
386 __le32 next_id; /* Line id for next line */
387 __le64 nr_lbas; /* Number of lbas mapped in line */
388 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200389 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
390};
391
392
393/* Write amplification counters stored on media */
394struct wa_counters {
395 __le64 user; /* Number of user written sectors */
396 __le64 gc; /* Number of sectors written by GC*/
397 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200398};
399
400struct pblk_emeta {
401 struct line_emeta *buf; /* emeta buffer in media format */
402 int mem; /* Write offset - points to next
403 * writable entry in memory
404 */
405 atomic_t sync; /* Synced - backpointer that signals the
406 * last entry that has been successfully
407 * persisted to media
408 */
409 unsigned int nr_entries; /* Number of emeta entries */
410};
411
412struct pblk_smeta {
413 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414};
415
416struct pblk_line {
417 struct pblk *pblk;
418 unsigned int id; /* Line number corresponds to the
419 * block line
420 */
421 unsigned int seq_nr; /* Unique line sequence number */
422
423 int state; /* PBLK_LINESTATE_X */
424 int type; /* PBLK_LINETYPE_X */
425 int gc_group; /* PBLK_LINEGC_X */
426 struct list_head list; /* Free, GC lists */
427
428 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
429
Javier González32ef9412018-03-30 00:05:20 +0200430 struct nvm_chk_meta *chks; /* Chunks forming line */
431
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200432 struct pblk_smeta *smeta; /* Start metadata */
433 struct pblk_emeta *emeta; /* End medatada */
434
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200436 int meta_distance; /* Distance between data and metadata */
437
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 u64 smeta_ssec; /* Sector where smeta starts */
439 u64 emeta_ssec; /* Sector where emeta starts */
440
441 unsigned int sec_in_line; /* Number of usable secs in line */
442
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200443 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
445 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
446
447 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
448 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
449
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200450 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451 atomic_t left_seblks; /* Blocks left for sync erasing */
452
453 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200455 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
456
457 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200458
459 struct kref ref; /* Write buffer L2P references */
460
461 spinlock_t lock; /* Necessary for invalid_bitmap only */
462};
463
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200464#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200465
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200466enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 PBLK_KMALLOC_META = 1,
468 PBLK_VMALLOC_META = 2,
469};
470
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200471enum {
472 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
473 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
474 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475};
476
477struct pblk_line_mgmt {
478 int nr_lines; /* Total number of full lines */
479 int nr_free_lines; /* Number of full lines in free list */
480
481 /* Free lists - use free_lock */
482 struct list_head free_list; /* Full lines ready to use */
483 struct list_head corrupt_list; /* Full lines corrupted */
484 struct list_head bad_list; /* Full lines bad */
485
486 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200487 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
489 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
490 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
491
492 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
493 struct list_head gc_empty_list; /* Full lines close, all valid */
494
495 struct pblk_line *log_line; /* Current FTL log line */
496 struct pblk_line *data_line; /* Current data line */
497 struct pblk_line *log_next; /* Next FTL log line */
498 struct pblk_line *data_next; /* Next data line */
499
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200500 struct list_head emeta_list; /* Lines queued to schedule emeta */
501
502 __le32 *vsc_list; /* Valid sector counts for all lines */
503
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200504 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505 int emeta_alloc_type;
506
507 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200508 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
509 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200510 unsigned long meta_bitmap;
511
512 /* Helpers for fast bitmap calculations */
513 unsigned long *bb_template;
514 unsigned long *bb_aux;
515
516 unsigned long d_seq_nr; /* Data line unique sequence number */
517 unsigned long l_seq_nr; /* Log line unique sequence number */
518
519 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200520 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200521 spinlock_t gc_lock;
522};
523
524struct pblk_line_meta {
525 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200526 unsigned int smeta_sec; /* Sectors needed for smeta */
527
528 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200529 * [0]: Total
530 * [1]: struct line_emeta +
531 * bb_bitmap + struct wa_counters
532 * [2]: L2P portion
533 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200534 */
535 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
536 * as emeta_len
537 */
538
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200539 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200540
541 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
543 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
544 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
545
546 unsigned int blk_per_line; /* Number of blocks in a full line */
547 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200548 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200549 unsigned int min_blk_line; /* Min. number of good blocks in line */
550
551 unsigned int mid_thrs; /* Threshold for GC mid list */
552 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200553
554 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200555};
556
Javier González588726d32017-06-26 11:57:29 +0200557enum {
558 PBLK_STATE_RUNNING = 0,
559 PBLK_STATE_STOPPING = 1,
560 PBLK_STATE_RECOVERING = 2,
561 PBLK_STATE_STOPPED = 3,
562};
563
Javier González3b2a3ad2018-03-30 00:05:21 +0200564/* Internal format to support not power-of-2 device formats */
565struct pblk_addrf {
566 /* gen to dev */
567 int sec_stripe;
568 int ch_stripe;
569 int lun_stripe;
570
571 /* dev to gen */
572 int sec_lun_stripe;
573 int sec_ws_stripe;
574};
575
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200576struct pblk {
577 struct nvm_tgt_dev *dev;
578 struct gendisk *disk;
579
580 struct kobject kobj;
581
582 struct pblk_lun *luns;
583
584 struct pblk_line *lines; /* Line array */
585 struct pblk_line_mgmt l_mg; /* Line management */
586 struct pblk_line_meta lm; /* Line metadata */
587
Javier González3b2a3ad2018-03-30 00:05:21 +0200588 struct nvm_addrf addrf; /* Aligned address format */
589 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200590 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591
592 struct pblk_rb rwb;
593
Javier González588726d32017-06-26 11:57:29 +0200594 int state; /* pblk line state */
595
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200596 int min_write_pgs; /* Minimum amount of pages required by controller */
597 int max_write_pgs; /* Maximum amount of pages supported by controller */
598 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
599 * guarantee successful reads.
600 */
601
602 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100603
604 int op; /* Percentage of device used for over-provisioning */
605 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200606
607 /* pblk provisioning values. Used by rate limiter */
608 struct pblk_rl rl;
609
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200610 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611
612 unsigned char instance_uuid[16];
Hans Holmberg76758392018-03-30 00:04:52 +0200613
614 /* Persistent write amplification counters, 4kb sector I/Os */
615 atomic64_t user_wa; /* Sectors written by user */
616 atomic64_t gc_wa; /* Sectors written by GC */
617 atomic64_t pad_wa; /* Padded sectors written */
618
619 /* Reset values for delta write amplification measurements */
620 u64 user_rst_wa;
621 u64 gc_rst_wa;
622 u64 pad_rst_wa;
623
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200624 /* Counters used for calculating padding distribution */
625 atomic64_t *pad_dist; /* Padding distribution buckets */
626 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
627 atomic64_t nr_flush; /* Number of flush/fua I/O */
628
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200629#ifdef CONFIG_NVM_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200630 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
632 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
633 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 atomic_long_t req_writes; /* Sectors stored on write buffer */
635 atomic_long_t sub_writes; /* Sectors submitted from buffer */
636 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200637 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200638 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639 atomic_long_t sync_reads; /* Completed sector read requests */
640 atomic_long_t recov_writes; /* Sectors submitted from recovery */
641 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
642 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
643#endif
644
645 spinlock_t lock;
646
647 atomic_long_t read_failed;
648 atomic_long_t read_empty;
649 atomic_long_t read_high_ecc;
650 atomic_long_t read_failed_gc;
651 atomic_long_t write_failed;
652 atomic_long_t erase_failed;
653
Javier González588726d32017-06-26 11:57:29 +0200654 atomic_t inflight_io; /* General inflight I/O counter */
655
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200656 struct task_struct *writer_ts;
657
658 /* Simple translation map of logical addresses to physical addresses.
659 * The logical addresses is known by the host system, while the physical
660 * addresses are used when writing to the disk block device.
661 */
662 unsigned char *trans_map;
663 spinlock_t trans_lock;
664
665 struct list_head compl_list;
666
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400667 mempool_t page_bio_pool;
668 mempool_t gen_ws_pool;
669 mempool_t rec_pool;
670 mempool_t r_rq_pool;
671 mempool_t w_rq_pool;
672 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200673
Javier Gonzálezef576492017-06-26 11:57:28 +0200674 struct workqueue_struct *close_wq;
675 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200676 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200677
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200678 struct timer_list wtimer;
679
680 struct pblk_gc gc;
681};
682
683struct pblk_line_ws {
684 struct pblk *pblk;
685 struct pblk_line *line;
686 void *priv;
687 struct work_struct ws;
688};
689
Javier González084ec9b2017-06-26 16:27:13 -0600690#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200691#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
692
693/*
694 * pblk ring buffer operations
695 */
696int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
697 unsigned int power_size, unsigned int power_seg_sz);
698unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
699void *pblk_rb_entries_ref(struct pblk_rb *rb);
700int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
701 unsigned int nr_entries, unsigned int *pos);
702int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
703 unsigned int *pos);
704void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
705 struct pblk_w_ctx w_ctx, unsigned int pos);
706void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200707 struct pblk_w_ctx w_ctx, struct pblk_line *line,
708 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200709struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200710void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200711
712void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200713unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200714 unsigned int pos, unsigned int nr_entries,
715 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200716unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
717 struct list_head *list,
718 unsigned int max);
719int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200720 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200721unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
722
723unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
724unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
725struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
726 struct ppa_addr *ppa);
727void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100728unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200729
730unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200731unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200732unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
733
734int pblk_rb_tear_down_check(struct pblk_rb *rb);
735int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
736void pblk_rb_data_free(struct pblk_rb *rb);
737ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
738
739/*
740 * pblk core
741 */
Javier González67bf26a2017-10-13 14:46:20 +0200742struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
743void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200744void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200745int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
746 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200747void pblk_discard(struct pblk *pblk, struct bio *bio);
Javier González32ef9412018-03-30 00:05:20 +0200748struct nvm_chk_meta *pblk_chunk_get_info(struct pblk *pblk);
749struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
750 struct nvm_chk_meta *lp,
751 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200752void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
753void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
754int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200755int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200756int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200757struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
758 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200759 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200760struct pblk_line *pblk_line_get(struct pblk *pblk);
761struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200762struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200763int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
764void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
765struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200766struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200767int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
768int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200769void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200770void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200771void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200772void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200773void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200774void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
775 void (*work)(struct work_struct *), gfp_t gfp_mask,
776 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200777u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
778int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200779int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
780 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200781int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
782void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200783void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200784struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200785u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
786void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200787u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200788u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
790 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200791void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200792void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
793 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200794void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200795void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
796 unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200797void pblk_end_io_sync(struct nvm_rq *rqd);
798int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
799 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200800void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
801 int nr_pages);
802void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200803void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
804 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
806void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
807 struct ppa_addr ppa);
808void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
809 struct ppa_addr ppa, struct ppa_addr entry_line);
810int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200811 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200812void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
813 u64 *lba_list, int nr_secs);
814void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
815 sector_t blba, int nr_secs);
816
817/*
818 * pblk user I/O write path
819 */
820int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
821 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200822int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200823
824/*
825 * pblk map
826 */
827void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
828 unsigned int sentry, unsigned long *lun_bitmap,
829 unsigned int valid_secs, struct ppa_addr *erase_ppa);
830void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
831 unsigned long *lun_bitmap, unsigned int valid_secs,
832 unsigned int off);
833
834/*
835 * pblk write thread
836 */
837int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700838void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200839void pblk_write_should_kick(struct pblk *pblk);
840
841/*
842 * pblk read path
843 */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400844extern struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200845int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200846int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847/*
848 * pblk recovery
849 */
850void pblk_submit_rec(struct work_struct *work);
851struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200852int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100853int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
855 struct pblk_rec_ctx *recovery, u64 *comp_bits,
856 unsigned int comp);
857
858/*
859 * pblk gc
860 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200861#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200862#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200863#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
864#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865
866int pblk_gc_init(struct pblk *pblk);
867void pblk_gc_exit(struct pblk *pblk);
868void pblk_gc_should_start(struct pblk *pblk);
869void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200870void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200871void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200872void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
873 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200874int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200875
876/*
877 * pblk rate limiter
878 */
879void pblk_rl_init(struct pblk_rl *rl, int budget);
880void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200881void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200882int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200883unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100884unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200885int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200886void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200887void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
888int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
889void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
890void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200891int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100893void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
894 bool used);
Javier González588726d32017-06-26 11:57:29 +0200895int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200896
897/*
898 * pblk sysfs
899 */
900int pblk_sysfs_init(struct gendisk *tdisk);
901void pblk_sysfs_exit(struct gendisk *tdisk);
902
903static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
904{
905 if (type == PBLK_KMALLOC_META)
906 return kmalloc(size, flags);
907 return vmalloc(size);
908}
909
910static inline void pblk_mfree(void *ptr, int type)
911{
912 if (type == PBLK_KMALLOC_META)
913 kfree(ptr);
914 else
915 vfree(ptr);
916}
917
918static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
919{
920 return c_ctx - sizeof(struct nvm_rq);
921}
922
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200923static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200924{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200925 return emeta->bb_bitmap;
926}
927
Hans Holmberg76758392018-03-30 00:04:52 +0200928static inline void *emeta_to_wa(struct pblk_line_meta *lm,
929 struct line_emeta *emeta)
930{
931 return emeta->bb_bitmap + lm->blk_bitmap_len;
932}
933
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200934static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
935{
936 return ((void *)emeta + pblk->lm.emeta_len[1]);
937}
938
939static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
940{
941 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200942}
943
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200944static inline int pblk_line_vsc(struct pblk_line *line)
945{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200946 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200947}
948
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200949static inline int pblk_pad_distance(struct pblk *pblk)
950{
951 struct nvm_tgt_dev *dev = pblk->dev;
952 struct nvm_geo *geo = &dev->geo;
953
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200954 return geo->mw_cunits * geo->all_luns * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200955}
956
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100957static inline int pblk_ppa_to_line(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200958{
Javier González69471512018-03-30 00:05:15 +0200959 return p.a.blk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200960}
961
962static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
963{
Javier González69471512018-03-30 00:05:15 +0200964 return p.a.lun * geo->num_ch + p.a.ch;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200965}
966
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100967static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
968 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200969{
Javier González3b2a3ad2018-03-30 00:05:21 +0200970 struct nvm_tgt_dev *dev = pblk->dev;
971 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100972 struct ppa_addr ppa;
973
Javier González3b2a3ad2018-03-30 00:05:21 +0200974 if (geo->version == NVM_OCSSD_SPEC_12) {
975 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
976
977 ppa.ppa = 0;
978 ppa.g.blk = line_id;
979 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
980 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
981 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
982 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
983 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
984 } else {
985 struct pblk_addrf *uaddrf = &pblk->uaddrf;
986 int secs, chnls, luns;
987
988 ppa.ppa = 0;
989
990 ppa.m.chk = line_id;
991
992 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
993 ppa.m.sec = secs;
994
995 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
996 ppa.m.grp = chnls;
997
998 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
999 ppa.m.pu = luns;
1000
1001 ppa.m.sec += uaddrf->sec_stripe * paddr;
1002 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001003
1004 return ppa;
1005}
1006
1007static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1008 struct ppa_addr p)
1009{
Javier González3b2a3ad2018-03-30 00:05:21 +02001010 struct nvm_tgt_dev *dev = pblk->dev;
1011 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001012 u64 paddr;
1013
Javier González3b2a3ad2018-03-30 00:05:21 +02001014 if (geo->version == NVM_OCSSD_SPEC_12) {
1015 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1016
1017 paddr = (u64)p.g.ch << ppaf->ch_offset;
1018 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1019 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1020 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1021 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1022 } else {
1023 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1024 u64 secs = p.m.sec;
1025 int sec_stripe;
1026
1027 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1028 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1029
1030 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1031 paddr += secs * uaddrf->sec_ws_stripe;
1032 paddr += sec_stripe;
1033 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001034
1035 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001036}
1037
1038static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1039{
1040 struct ppa_addr ppa64;
1041
1042 ppa64.ppa = 0;
1043
1044 if (ppa32 == -1) {
1045 ppa64.ppa = ADDR_EMPTY;
1046 } else if (ppa32 & (1U << 31)) {
1047 ppa64.c.line = ppa32 & ((~0U) >> 1);
1048 ppa64.c.is_cached = 1;
1049 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001050 struct nvm_tgt_dev *dev = pblk->dev;
1051 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001052
Javier González3b2a3ad2018-03-30 00:05:21 +02001053 if (geo->version == NVM_OCSSD_SPEC_12) {
1054 struct nvm_addrf_12 *ppaf =
1055 (struct nvm_addrf_12 *)&pblk->addrf;
1056
1057 ppa64.g.ch = (ppa32 & ppaf->ch_mask) >>
1058 ppaf->ch_offset;
1059 ppa64.g.lun = (ppa32 & ppaf->lun_mask) >>
1060 ppaf->lun_offset;
1061 ppa64.g.blk = (ppa32 & ppaf->blk_mask) >>
1062 ppaf->blk_offset;
1063 ppa64.g.pg = (ppa32 & ppaf->pg_mask) >>
1064 ppaf->pg_offset;
1065 ppa64.g.pl = (ppa32 & ppaf->pln_mask) >>
1066 ppaf->pln_offset;
1067 ppa64.g.sec = (ppa32 & ppaf->sec_mask) >>
1068 ppaf->sec_offset;
1069 } else {
1070 struct nvm_addrf *lbaf = &pblk->addrf;
1071
1072 ppa64.m.grp = (ppa32 & lbaf->ch_mask) >>
1073 lbaf->ch_offset;
1074 ppa64.m.pu = (ppa32 & lbaf->lun_mask) >>
1075 lbaf->lun_offset;
1076 ppa64.m.chk = (ppa32 & lbaf->chk_mask) >>
1077 lbaf->chk_offset;
1078 ppa64.m.sec = (ppa32 & lbaf->sec_mask) >>
1079 lbaf->sec_offset;
1080 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081 }
1082
1083 return ppa64;
1084}
1085
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001086static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1087{
1088 u32 ppa32 = 0;
1089
1090 if (ppa64.ppa == ADDR_EMPTY) {
1091 ppa32 = ~0U;
1092 } else if (ppa64.c.is_cached) {
1093 ppa32 |= ppa64.c.line;
1094 ppa32 |= 1U << 31;
1095 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001096 struct nvm_tgt_dev *dev = pblk->dev;
1097 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001098
Javier González3b2a3ad2018-03-30 00:05:21 +02001099 if (geo->version == NVM_OCSSD_SPEC_12) {
1100 struct nvm_addrf_12 *ppaf =
1101 (struct nvm_addrf_12 *)&pblk->addrf;
1102
1103 ppa32 |= ppa64.g.ch << ppaf->ch_offset;
1104 ppa32 |= ppa64.g.lun << ppaf->lun_offset;
1105 ppa32 |= ppa64.g.blk << ppaf->blk_offset;
1106 ppa32 |= ppa64.g.pg << ppaf->pg_offset;
1107 ppa32 |= ppa64.g.pl << ppaf->pln_offset;
1108 ppa32 |= ppa64.g.sec << ppaf->sec_offset;
1109 } else {
1110 struct nvm_addrf *lbaf = &pblk->addrf;
1111
1112 ppa32 |= ppa64.m.grp << lbaf->ch_offset;
1113 ppa32 |= ppa64.m.pu << lbaf->lun_offset;
1114 ppa32 |= ppa64.m.chk << lbaf->chk_offset;
1115 ppa32 |= ppa64.m.sec << lbaf->sec_offset;
1116 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117 }
1118
1119 return ppa32;
1120}
1121
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001122static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1123 sector_t lba)
1124{
1125 struct ppa_addr ppa;
1126
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001127 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001128 u32 *map = (u32 *)pblk->trans_map;
1129
1130 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1131 } else {
1132 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1133
1134 ppa = map[lba];
1135 }
1136
1137 return ppa;
1138}
1139
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001140static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1141 struct ppa_addr ppa)
1142{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001143 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001144 u32 *map = (u32 *)pblk->trans_map;
1145
1146 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1147 } else {
1148 u64 *map = (u64 *)pblk->trans_map;
1149
1150 map[lba] = ppa.ppa;
1151 }
1152}
1153
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001154static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1155{
1156 return (ppa_addr.ppa == ADDR_EMPTY);
1157}
1158
1159static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1160{
1161 ppa_addr->ppa = ADDR_EMPTY;
1162}
1163
Javier González076984662017-06-30 17:56:42 +02001164static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1165{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001166 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001167}
1168
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001169static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1170{
1171 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1172}
1173
1174static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1175{
1176 return ppa.c.line;
1177}
1178
1179static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1180{
1181 struct ppa_addr p;
1182
1183 p.c.line = addr;
1184 p.c.is_cached = 1;
1185
1186 return p;
1187}
1188
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001189static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001190 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001191{
1192 u32 crc = ~(u32)0;
1193
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001194 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001195 sizeof(struct line_header) - sizeof(crc));
1196
1197 return crc;
1198}
1199
1200static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1201 struct line_smeta *smeta)
1202{
1203 struct pblk_line_meta *lm = &pblk->lm;
1204 u32 crc = ~(u32)0;
1205
1206 crc = crc32_le(crc, (unsigned char *)smeta +
1207 sizeof(struct line_header) + sizeof(crc),
1208 lm->smeta_len -
1209 sizeof(struct line_header) - sizeof(crc));
1210
1211 return crc;
1212}
1213
1214static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1215 struct line_emeta *emeta)
1216{
1217 struct pblk_line_meta *lm = &pblk->lm;
1218 u32 crc = ~(u32)0;
1219
1220 crc = crc32_le(crc, (unsigned char *)emeta +
1221 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001222 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001223 sizeof(struct line_header) - sizeof(crc));
1224
1225 return crc;
1226}
1227
1228static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1229{
1230 struct nvm_tgt_dev *dev = pblk->dev;
1231 struct nvm_geo *geo = &dev->geo;
1232 int flags;
1233
Javier González3b2a3ad2018-03-30 00:05:21 +02001234 if (geo->version == NVM_OCSSD_SPEC_20)
1235 return 0;
1236
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001237 flags = geo->pln_mode >> 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001238
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001239 if (type == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001240 flags |= NVM_IO_SCRAMBLE_ENABLE;
1241
1242 return flags;
1243}
1244
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001245enum {
1246 PBLK_READ_RANDOM = 0,
1247 PBLK_READ_SEQUENTIAL = 1,
1248};
1249
1250static inline int pblk_set_read_mode(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001251{
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001252 struct nvm_tgt_dev *dev = pblk->dev;
1253 struct nvm_geo *geo = &dev->geo;
1254 int flags;
1255
Javier González3b2a3ad2018-03-30 00:05:21 +02001256 if (geo->version == NVM_OCSSD_SPEC_20)
1257 return 0;
1258
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001259 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1260 if (type == PBLK_READ_SEQUENTIAL)
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001261 flags |= geo->pln_mode >> 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001262
1263 return flags;
1264}
1265
1266static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1267{
1268 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001269}
1270
1271#ifdef CONFIG_NVM_DEBUG
Javier González3b2a3ad2018-03-30 00:05:21 +02001272static inline void print_ppa(struct nvm_geo *geo, struct ppa_addr *p,
1273 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001274{
1275 if (p->c.is_cached) {
1276 pr_err("ppa: (%s: %x) cache line: %llu\n",
1277 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001278 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001279 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1280 msg, error,
1281 p->g.ch, p->g.lun, p->g.blk,
1282 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001283 } else {
1284 pr_err("ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
1285 msg, error,
1286 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287 }
1288}
1289
1290static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1291 int error)
1292{
1293 int bit = -1;
1294
1295 if (rqd->nr_ppas == 1) {
Javier González3b2a3ad2018-03-30 00:05:21 +02001296 print_ppa(&pblk->dev->geo, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001297 return;
1298 }
1299
1300 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1301 bit + 1)) < rqd->nr_ppas) {
Javier González3b2a3ad2018-03-30 00:05:21 +02001302 print_ppa(&pblk->dev->geo, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001303 }
1304
1305 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1306}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001307
1308static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1309 struct ppa_addr *ppas, int nr_ppas)
1310{
1311 struct nvm_geo *geo = &tgt_dev->geo;
1312 struct ppa_addr *ppa;
1313 int i;
1314
1315 for (i = 0; i < nr_ppas; i++) {
1316 ppa = &ppas[i];
1317
Javier González3b2a3ad2018-03-30 00:05:21 +02001318 if (geo->version == NVM_OCSSD_SPEC_12) {
1319 if (!ppa->c.is_cached &&
1320 ppa->g.ch < geo->num_ch &&
1321 ppa->g.lun < geo->num_lun &&
1322 ppa->g.pl < geo->num_pln &&
1323 ppa->g.blk < geo->num_chk &&
1324 ppa->g.pg < geo->num_pg &&
1325 ppa->g.sec < geo->ws_min)
1326 continue;
1327 } else {
1328 if (!ppa->c.is_cached &&
1329 ppa->m.grp < geo->num_ch &&
1330 ppa->m.pu < geo->num_lun &&
1331 ppa->m.chk < geo->num_chk &&
1332 ppa->m.sec < geo->clba)
1333 continue;
1334 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001335
Javier González3b2a3ad2018-03-30 00:05:21 +02001336 print_ppa(geo, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001337
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001338 return 1;
1339 }
1340 return 0;
1341}
1342
Javier González1a94b2d2017-10-13 14:46:47 +02001343static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1344{
1345 struct nvm_tgt_dev *dev = pblk->dev;
1346 struct ppa_addr *ppa_list;
1347
1348 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1349
1350 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1351 WARN_ON(1);
1352 return -EINVAL;
1353 }
1354
1355 if (rqd->opcode == NVM_OP_PWRITE) {
1356 struct pblk_line *line;
1357 struct ppa_addr ppa;
1358 int i;
1359
1360 for (i = 0; i < rqd->nr_ppas; i++) {
1361 ppa = ppa_list[i];
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001362 line = &pblk->lines[pblk_ppa_to_line(ppa)];
Javier González1a94b2d2017-10-13 14:46:47 +02001363
1364 spin_lock(&line->lock);
1365 if (line->state != PBLK_LINESTATE_OPEN) {
1366 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1367 line->id, line->state);
1368 WARN_ON(1);
1369 spin_unlock(&line->lock);
1370 return -EINVAL;
1371 }
1372 spin_unlock(&line->lock);
1373 }
1374 }
1375
1376 return 0;
1377}
1378#endif
1379
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001380static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1381{
1382 struct pblk_line_meta *lm = &pblk->lm;
1383
1384 if (paddr > lm->sec_per_line)
1385 return 1;
1386
1387 return 0;
1388}
1389
1390static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1391{
1392 return bio->bi_iter.bi_idx;
1393}
1394
1395static inline sector_t pblk_get_lba(struct bio *bio)
1396{
1397 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1398}
1399
1400static inline unsigned int pblk_get_secs(struct bio *bio)
1401{
1402 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1403}
1404
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001405static inline void pblk_setup_uuid(struct pblk *pblk)
1406{
1407 uuid_le uuid;
1408
1409 uuid_le_gen(&uuid);
1410 memcpy(pblk->instance_uuid, uuid.b, 16);
1411}
1412#endif /* PBLK_H_ */