blob: 043a851a8e6cd7d54c4840df6a7da05d6f5a5bce [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;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200131 struct work_struct ws_rec;
132};
133
134/* Write context */
135struct pblk_w_ctx {
136 struct bio_list bios; /* Original bios - used for completion
137 * in REQ_FUA, REQ_FLUSH case
138 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200139 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200140 struct ppa_addr ppa; /* Physic addr. associated with entry */
141 int flags; /* Write context flags */
142};
143
144struct pblk_rb_entry {
145 struct ppa_addr cacheline; /* Cacheline for this entry */
146 void *data; /* Pointer to data on this entry */
147 struct pblk_w_ctx w_ctx; /* Context for this entry */
148 struct list_head index; /* List head to enable indexes */
149};
150
151#define EMPTY_ENTRY (~0U)
152
153struct pblk_rb_pages {
154 struct page *pages;
155 int order;
156 struct list_head list;
157};
158
159struct pblk_rb {
160 struct pblk_rb_entry *entries; /* Ring buffer entries */
161 unsigned int mem; /* Write offset - points to next
162 * writable entry in memory
163 */
164 unsigned int subm; /* Read offset - points to last entry
165 * that has been submitted to the media
166 * to be persisted
167 */
168 unsigned int sync; /* Synced - backpointer that signals
169 * the last submitted entry that has
170 * been successfully persisted to media
171 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100172 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200173 * flushed to the media. Used with
174 * REQ_FLUSH and REQ_FUA
175 */
176 unsigned int l2p_update; /* l2p update point - next entry for
177 * which l2p mapping will be updated to
178 * contain a device ppa address (instead
179 * of a cacheline
180 */
181 unsigned int nr_entries; /* Number of entries in write buffer -
182 * must be a power of two
183 */
184 unsigned int seg_size; /* Size of the data segments being
185 * stored on each entry. Typically this
186 * will be 4KB
187 */
188
189 struct list_head pages; /* List of data pages */
190
191 spinlock_t w_lock; /* Write lock */
192 spinlock_t s_lock; /* Sync lock */
193
194#ifdef CONFIG_NVM_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100195 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200196#endif
197};
198
199#define PBLK_RECOVERY_SECTORS 16
200
201struct pblk_lun {
202 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200203 struct semaphore wr_sem;
204};
205
206struct pblk_gc_rq {
207 struct pblk_line *line;
208 void *data;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200209 u64 paddr_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200210 u64 lba_list[PBLK_MAX_REQ_ADDRS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200211 int nr_secs;
212 int secs_to_gc;
213 struct list_head list;
214};
215
216struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200217 /* These states are not protected by a lock since (i) they are in the
218 * fast path, and (ii) they are not critical.
219 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220 int gc_active;
221 int gc_enabled;
222 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223
224 struct task_struct *gc_ts;
225 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200226 struct task_struct *gc_reader_ts;
227
228 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200230
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200231 struct timer_list gc_timer;
232
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200233 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200234 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
235 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
236 * started reads to finished writes
237 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200240 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200241 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200242
243 spinlock_t lock;
244 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200245 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200246};
247
248struct pblk_rl {
249 unsigned int high; /* Upper threshold for rate limiter (free run -
250 * user I/O rate limiter
251 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252 unsigned int high_pw; /* High rounded up as a power of 2 */
253
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200254#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
255#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256
257 int rb_windows_pw; /* Number of rate windows in the write buffer
258 * given as a power-of-2. This guarantees that
259 * when user I/O is being rate limited, there
260 * will be reserved enough space for the GC to
261 * place its payload. A window is of
262 * pblk->max_write_pgs size, which in NVMe is
263 * 64, i.e., 256kb.
264 */
265 int rb_budget; /* Total number of entries available for I/O */
266 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200267 int rb_gc_max; /* Max buffer entries available for GC I/O */
268 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
269 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200270 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200271
272 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200274 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200275
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200276 int rsv_blocks; /* Reserved blocks for GC */
277
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200278 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200279 int rb_gc_active;
280
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200281 struct timer_list u_timer;
282
283 unsigned long long nr_secs;
284 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100285
286 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
287 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200288};
289
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200290#define PBLK_LINE_EMPTY (~0U)
291
292enum {
293 /* Line Types */
294 PBLK_LINETYPE_FREE = 0,
295 PBLK_LINETYPE_LOG = 1,
296 PBLK_LINETYPE_DATA = 2,
297
298 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200299 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 PBLK_LINESTATE_FREE = 10,
301 PBLK_LINESTATE_OPEN = 11,
302 PBLK_LINESTATE_CLOSED = 12,
303 PBLK_LINESTATE_GC = 13,
304 PBLK_LINESTATE_BAD = 14,
305 PBLK_LINESTATE_CORRUPT = 15,
306
307 /* GC group */
308 PBLK_LINEGC_NONE = 20,
309 PBLK_LINEGC_EMPTY = 21,
310 PBLK_LINEGC_LOW = 22,
311 PBLK_LINEGC_MID = 23,
312 PBLK_LINEGC_HIGH = 24,
313 PBLK_LINEGC_FULL = 25,
314};
315
316#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200317
318/* emeta/smeta persistent storage format versions:
319 * Changes in major version requires offline migration.
320 * Changes in minor version are handled automatically during
321 * recovery.
322 */
323
324#define SMETA_VERSION_MAJOR (0)
325#define SMETA_VERSION_MINOR (1)
326
327#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200328#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200329
330struct line_header {
331 __le32 crc;
332 __le32 identifier; /* pblk identifier */
333 __u8 uuid[16]; /* instance uuid */
334 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200335 __u8 version_major; /* version major */
336 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337 __le32 id; /* line id for current line */
338};
339
340struct line_smeta {
341 struct line_header header;
342
343 __le32 crc; /* Full structure including struct crc */
344 /* Previous line metadata */
345 __le32 prev_id; /* Line id for previous line */
346
347 /* Current line metadata */
348 __le64 seq_nr; /* Sequence number for current line */
349
350 /* Active writers */
351 __le32 window_wr_lun; /* Number of parallel LUNs to write */
352
353 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200354
355 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200356};
357
Hans Holmberg76758392018-03-30 00:04:52 +0200358
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200359/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200360 * Metadata layout in media:
361 * First sector:
362 * 1. struct line_emeta
363 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200364 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200365 * Mid sectors (start at lbas_sector):
366 * 3. nr_lbas (u64) forming lba list
367 * Last sectors (start at vsc_sector):
368 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369 */
370struct line_emeta {
371 struct line_header header;
372
373 __le32 crc; /* Full structure including struct crc */
374
375 /* Previous line metadata */
376 __le32 prev_id; /* Line id for prev line */
377
378 /* Current line metadata */
379 __le64 seq_nr; /* Sequence number for current line */
380
381 /* Active writers */
382 __le32 window_wr_lun; /* Number of parallel LUNs to write */
383
384 /* Bookkeeping for recovery */
385 __le32 next_id; /* Line id for next line */
386 __le64 nr_lbas; /* Number of lbas mapped in line */
387 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200388 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
389};
390
391
392/* Write amplification counters stored on media */
393struct wa_counters {
394 __le64 user; /* Number of user written sectors */
395 __le64 gc; /* Number of sectors written by GC*/
396 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200397};
398
399struct pblk_emeta {
400 struct line_emeta *buf; /* emeta buffer in media format */
401 int mem; /* Write offset - points to next
402 * writable entry in memory
403 */
404 atomic_t sync; /* Synced - backpointer that signals the
405 * last entry that has been successfully
406 * persisted to media
407 */
408 unsigned int nr_entries; /* Number of emeta entries */
409};
410
411struct pblk_smeta {
412 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200413};
414
415struct pblk_line {
416 struct pblk *pblk;
417 unsigned int id; /* Line number corresponds to the
418 * block line
419 */
420 unsigned int seq_nr; /* Unique line sequence number */
421
422 int state; /* PBLK_LINESTATE_X */
423 int type; /* PBLK_LINETYPE_X */
424 int gc_group; /* PBLK_LINEGC_X */
425 struct list_head list; /* Free, GC lists */
426
427 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
428
Javier González32ef9412018-03-30 00:05:20 +0200429 struct nvm_chk_meta *chks; /* Chunks forming line */
430
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200431 struct pblk_smeta *smeta; /* Start metadata */
432 struct pblk_emeta *emeta; /* End medatada */
433
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200434 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200435 int meta_distance; /* Distance between data and metadata */
436
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200437 u64 smeta_ssec; /* Sector where smeta starts */
438 u64 emeta_ssec; /* Sector where emeta starts */
439
440 unsigned int sec_in_line; /* Number of usable secs in line */
441
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200442 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200443 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
444 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
445
446 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
447 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
448
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200449 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200450 atomic_t left_seblks; /* Blocks left for sync erasing */
451
452 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200454 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
455
456 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200457
458 struct kref ref; /* Write buffer L2P references */
459
460 spinlock_t lock; /* Necessary for invalid_bitmap only */
461};
462
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200463#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200465enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200466 PBLK_KMALLOC_META = 1,
467 PBLK_VMALLOC_META = 2,
468};
469
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200470enum {
471 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
472 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
473 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474};
475
476struct pblk_line_mgmt {
477 int nr_lines; /* Total number of full lines */
478 int nr_free_lines; /* Number of full lines in free list */
479
480 /* Free lists - use free_lock */
481 struct list_head free_list; /* Full lines ready to use */
482 struct list_head corrupt_list; /* Full lines corrupted */
483 struct list_head bad_list; /* Full lines bad */
484
485 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200486 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
488 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
489 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
490
491 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
492 struct list_head gc_empty_list; /* Full lines close, all valid */
493
494 struct pblk_line *log_line; /* Current FTL log line */
495 struct pblk_line *data_line; /* Current data line */
496 struct pblk_line *log_next; /* Next FTL log line */
497 struct pblk_line *data_next; /* Next data line */
498
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200499 struct list_head emeta_list; /* Lines queued to schedule emeta */
500
501 __le32 *vsc_list; /* Valid sector counts for all lines */
502
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200503 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200504 int emeta_alloc_type;
505
506 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200507 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
508 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200509 unsigned long meta_bitmap;
510
511 /* Helpers for fast bitmap calculations */
512 unsigned long *bb_template;
513 unsigned long *bb_aux;
514
515 unsigned long d_seq_nr; /* Data line unique sequence number */
516 unsigned long l_seq_nr; /* Log line unique sequence number */
517
518 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200519 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200520 spinlock_t gc_lock;
521};
522
523struct pblk_line_meta {
524 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200525 unsigned int smeta_sec; /* Sectors needed for smeta */
526
527 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200528 * [0]: Total
529 * [1]: struct line_emeta +
530 * bb_bitmap + struct wa_counters
531 * [2]: L2P portion
532 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200533 */
534 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
535 * as emeta_len
536 */
537
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200539
540 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200541 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
542 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
543 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
544
545 unsigned int blk_per_line; /* Number of blocks in a full line */
546 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200547 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548 unsigned int min_blk_line; /* Min. number of good blocks in line */
549
550 unsigned int mid_thrs; /* Threshold for GC mid list */
551 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200552
553 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200554};
555
Javier González588726d32017-06-26 11:57:29 +0200556enum {
557 PBLK_STATE_RUNNING = 0,
558 PBLK_STATE_STOPPING = 1,
559 PBLK_STATE_RECOVERING = 2,
560 PBLK_STATE_STOPPED = 3,
561};
562
Javier González3b2a3ad2018-03-30 00:05:21 +0200563/* Internal format to support not power-of-2 device formats */
564struct pblk_addrf {
565 /* gen to dev */
566 int sec_stripe;
567 int ch_stripe;
568 int lun_stripe;
569
570 /* dev to gen */
571 int sec_lun_stripe;
572 int sec_ws_stripe;
573};
574
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200575struct pblk {
576 struct nvm_tgt_dev *dev;
577 struct gendisk *disk;
578
579 struct kobject kobj;
580
581 struct pblk_lun *luns;
582
583 struct pblk_line *lines; /* Line array */
584 struct pblk_line_mgmt l_mg; /* Line management */
585 struct pblk_line_meta lm; /* Line metadata */
586
Javier González3b2a3ad2018-03-30 00:05:21 +0200587 struct nvm_addrf addrf; /* Aligned address format */
588 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200589 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200590
591 struct pblk_rb rwb;
592
Javier González588726d32017-06-26 11:57:29 +0200593 int state; /* pblk line state */
594
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200595 int min_write_pgs; /* Minimum amount of pages required by controller */
596 int max_write_pgs; /* Maximum amount of pages supported by controller */
597 int pgs_in_buffer; /* Number of pages that need to be held in buffer to
598 * guarantee successful reads.
599 */
600
601 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100602
603 int op; /* Percentage of device used for over-provisioning */
604 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605
606 /* pblk provisioning values. Used by rate limiter */
607 struct pblk_rl rl;
608
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200609 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610
611 unsigned char instance_uuid[16];
Hans Holmberg76758392018-03-30 00:04:52 +0200612
613 /* Persistent write amplification counters, 4kb sector I/Os */
614 atomic64_t user_wa; /* Sectors written by user */
615 atomic64_t gc_wa; /* Sectors written by GC */
616 atomic64_t pad_wa; /* Padded sectors written */
617
618 /* Reset values for delta write amplification measurements */
619 u64 user_rst_wa;
620 u64 gc_rst_wa;
621 u64 pad_rst_wa;
622
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200623 /* Counters used for calculating padding distribution */
624 atomic64_t *pad_dist; /* Padding distribution buckets */
625 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
626 atomic64_t nr_flush; /* Number of flush/fua I/O */
627
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628#ifdef CONFIG_NVM_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200629 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200630 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
631 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
632 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633 atomic_long_t req_writes; /* Sectors stored on write buffer */
634 atomic_long_t sub_writes; /* Sectors submitted from buffer */
635 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200636 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200637 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200638 atomic_long_t sync_reads; /* Completed sector read requests */
639 atomic_long_t recov_writes; /* Sectors submitted from recovery */
640 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
641 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
642#endif
643
644 spinlock_t lock;
645
646 atomic_long_t read_failed;
647 atomic_long_t read_empty;
648 atomic_long_t read_high_ecc;
649 atomic_long_t read_failed_gc;
650 atomic_long_t write_failed;
651 atomic_long_t erase_failed;
652
Javier González588726d32017-06-26 11:57:29 +0200653 atomic_t inflight_io; /* General inflight I/O counter */
654
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655 struct task_struct *writer_ts;
656
657 /* Simple translation map of logical addresses to physical addresses.
658 * The logical addresses is known by the host system, while the physical
659 * addresses are used when writing to the disk block device.
660 */
661 unsigned char *trans_map;
662 spinlock_t trans_lock;
663
664 struct list_head compl_list;
665
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200666 spinlock_t resubmit_lock; /* Resubmit list lock */
667 struct list_head resubmit_list; /* Resubmit list for failed writes*/
668
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400669 mempool_t page_bio_pool;
670 mempool_t gen_ws_pool;
671 mempool_t rec_pool;
672 mempool_t r_rq_pool;
673 mempool_t w_rq_pool;
674 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200675
Javier Gonzálezef576492017-06-26 11:57:28 +0200676 struct workqueue_struct *close_wq;
677 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200678 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200679
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200680 struct timer_list wtimer;
681
682 struct pblk_gc gc;
683};
684
685struct pblk_line_ws {
686 struct pblk *pblk;
687 struct pblk_line *line;
688 void *priv;
689 struct work_struct ws;
690};
691
Javier González084ec9b2017-06-26 16:27:13 -0600692#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200693#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
694
695/*
696 * pblk ring buffer operations
697 */
698int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
699 unsigned int power_size, unsigned int power_seg_sz);
700unsigned int pblk_rb_calculate_size(unsigned int nr_entries);
701void *pblk_rb_entries_ref(struct pblk_rb *rb);
702int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
703 unsigned int nr_entries, unsigned int *pos);
704int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
705 unsigned int *pos);
706void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
707 struct pblk_w_ctx w_ctx, unsigned int pos);
708void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200709 struct pblk_w_ctx w_ctx, struct pblk_line *line,
710 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200711struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200712void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200713
714void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200715unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200716 unsigned int pos, unsigned int nr_entries,
717 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200718int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200719 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200720unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
721
722unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
723unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
724struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
725 struct ppa_addr *ppa);
726void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100727unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200728
729unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200730unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200731unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
732
733int pblk_rb_tear_down_check(struct pblk_rb *rb);
734int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
735void pblk_rb_data_free(struct pblk_rb *rb);
736ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
737
738/*
739 * pblk core
740 */
Javier González67bf26a2017-10-13 14:46:20 +0200741struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
742void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200743void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200744int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
745 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200746void pblk_discard(struct pblk *pblk, struct bio *bio);
Javier González32ef9412018-03-30 00:05:20 +0200747struct nvm_chk_meta *pblk_chunk_get_info(struct pblk *pblk);
748struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
749 struct nvm_chk_meta *lp,
750 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200751void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
752void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
753int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200754int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200755int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200756struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
757 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200758 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200759struct pblk_line *pblk_line_get(struct pblk *pblk);
760struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200761struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
763void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
764struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200765struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200766int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
767int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200768void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200769void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200770void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200771void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200772void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200773void __pblk_pipeline_stop(struct pblk *pblk);
774void __pblk_pipeline_flush(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200775void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
776 void (*work)(struct work_struct *), gfp_t gfp_mask,
777 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200778u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
779int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200780int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
781 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200782int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
783void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200784void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200785struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200786u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
787void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200788u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200789u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
791 unsigned long secs_to_flush);
Javier González3eaa11e2017-07-07 21:08:52 +0200792void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200793void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
794 unsigned long *lun_bitmap);
Javier González3eaa11e2017-07-07 21:08:52 +0200795void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200796void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas,
797 unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200798int 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 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200850struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200851int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100852int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200853
854/*
855 * pblk gc
856 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200857#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200858#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200859#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
860#define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200861
862int pblk_gc_init(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200863void pblk_gc_exit(struct pblk *pblk, bool graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200864void pblk_gc_should_start(struct pblk *pblk);
865void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200866void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200867void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200868void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
869 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200870int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200871
872/*
873 * pblk rate limiter
874 */
875void pblk_rl_init(struct pblk_rl *rl, int budget);
876void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200877void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200878int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200879unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100880unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200881int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200882void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200883void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
884int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
885void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
886void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200887int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200888void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100889void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
890 bool used);
Javier González588726d32017-06-26 11:57:29 +0200891int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892
893/*
894 * pblk sysfs
895 */
896int pblk_sysfs_init(struct gendisk *tdisk);
897void pblk_sysfs_exit(struct gendisk *tdisk);
898
899static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
900{
901 if (type == PBLK_KMALLOC_META)
902 return kmalloc(size, flags);
903 return vmalloc(size);
904}
905
906static inline void pblk_mfree(void *ptr, int type)
907{
908 if (type == PBLK_KMALLOC_META)
909 kfree(ptr);
910 else
911 vfree(ptr);
912}
913
914static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
915{
916 return c_ctx - sizeof(struct nvm_rq);
917}
918
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200919static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200920{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200921 return emeta->bb_bitmap;
922}
923
Hans Holmberg76758392018-03-30 00:04:52 +0200924static inline void *emeta_to_wa(struct pblk_line_meta *lm,
925 struct line_emeta *emeta)
926{
927 return emeta->bb_bitmap + lm->blk_bitmap_len;
928}
929
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200930static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
931{
932 return ((void *)emeta + pblk->lm.emeta_len[1]);
933}
934
935static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
936{
937 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200938}
939
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200940static inline int pblk_line_vsc(struct pblk_line *line)
941{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200942 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200943}
944
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200945static inline int pblk_pad_distance(struct pblk *pblk)
946{
947 struct nvm_tgt_dev *dev = pblk->dev;
948 struct nvm_geo *geo = &dev->geo;
949
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200950 return geo->mw_cunits * geo->all_luns * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200951}
952
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100953static inline int pblk_ppa_to_line(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954{
Javier González69471512018-03-30 00:05:15 +0200955 return p.a.blk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200956}
957
958static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
959{
Javier González69471512018-03-30 00:05:15 +0200960 return p.a.lun * geo->num_ch + p.a.ch;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961}
962
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100963static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
964 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200965{
Javier González3b2a3ad2018-03-30 00:05:21 +0200966 struct nvm_tgt_dev *dev = pblk->dev;
967 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100968 struct ppa_addr ppa;
969
Javier González3b2a3ad2018-03-30 00:05:21 +0200970 if (geo->version == NVM_OCSSD_SPEC_12) {
971 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
972
973 ppa.ppa = 0;
974 ppa.g.blk = line_id;
975 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
976 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
977 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
978 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
979 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
980 } else {
981 struct pblk_addrf *uaddrf = &pblk->uaddrf;
982 int secs, chnls, luns;
983
984 ppa.ppa = 0;
985
986 ppa.m.chk = line_id;
987
988 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
989 ppa.m.sec = secs;
990
991 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
992 ppa.m.grp = chnls;
993
994 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
995 ppa.m.pu = luns;
996
997 ppa.m.sec += uaddrf->sec_stripe * paddr;
998 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100999
1000 return ppa;
1001}
1002
1003static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1004 struct ppa_addr p)
1005{
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 u64 paddr;
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 paddr = (u64)p.g.ch << ppaf->ch_offset;
1014 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1015 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1016 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1017 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1018 } else {
1019 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1020 u64 secs = p.m.sec;
1021 int sec_stripe;
1022
1023 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1024 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1025
1026 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1027 paddr += secs * uaddrf->sec_ws_stripe;
1028 paddr += sec_stripe;
1029 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001030
1031 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001032}
1033
1034static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1035{
1036 struct ppa_addr ppa64;
1037
1038 ppa64.ppa = 0;
1039
1040 if (ppa32 == -1) {
1041 ppa64.ppa = ADDR_EMPTY;
1042 } else if (ppa32 & (1U << 31)) {
1043 ppa64.c.line = ppa32 & ((~0U) >> 1);
1044 ppa64.c.is_cached = 1;
1045 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001046 struct nvm_tgt_dev *dev = pblk->dev;
1047 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001048
Javier González3b2a3ad2018-03-30 00:05:21 +02001049 if (geo->version == NVM_OCSSD_SPEC_12) {
1050 struct nvm_addrf_12 *ppaf =
1051 (struct nvm_addrf_12 *)&pblk->addrf;
1052
1053 ppa64.g.ch = (ppa32 & ppaf->ch_mask) >>
1054 ppaf->ch_offset;
1055 ppa64.g.lun = (ppa32 & ppaf->lun_mask) >>
1056 ppaf->lun_offset;
1057 ppa64.g.blk = (ppa32 & ppaf->blk_mask) >>
1058 ppaf->blk_offset;
1059 ppa64.g.pg = (ppa32 & ppaf->pg_mask) >>
1060 ppaf->pg_offset;
1061 ppa64.g.pl = (ppa32 & ppaf->pln_mask) >>
1062 ppaf->pln_offset;
1063 ppa64.g.sec = (ppa32 & ppaf->sec_mask) >>
1064 ppaf->sec_offset;
1065 } else {
1066 struct nvm_addrf *lbaf = &pblk->addrf;
1067
1068 ppa64.m.grp = (ppa32 & lbaf->ch_mask) >>
1069 lbaf->ch_offset;
1070 ppa64.m.pu = (ppa32 & lbaf->lun_mask) >>
1071 lbaf->lun_offset;
1072 ppa64.m.chk = (ppa32 & lbaf->chk_mask) >>
1073 lbaf->chk_offset;
1074 ppa64.m.sec = (ppa32 & lbaf->sec_mask) >>
1075 lbaf->sec_offset;
1076 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077 }
1078
1079 return ppa64;
1080}
1081
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001082static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1083{
1084 u32 ppa32 = 0;
1085
1086 if (ppa64.ppa == ADDR_EMPTY) {
1087 ppa32 = ~0U;
1088 } else if (ppa64.c.is_cached) {
1089 ppa32 |= ppa64.c.line;
1090 ppa32 |= 1U << 31;
1091 } else {
Javier González3b2a3ad2018-03-30 00:05:21 +02001092 struct nvm_tgt_dev *dev = pblk->dev;
1093 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001094
Javier González3b2a3ad2018-03-30 00:05:21 +02001095 if (geo->version == NVM_OCSSD_SPEC_12) {
1096 struct nvm_addrf_12 *ppaf =
1097 (struct nvm_addrf_12 *)&pblk->addrf;
1098
1099 ppa32 |= ppa64.g.ch << ppaf->ch_offset;
1100 ppa32 |= ppa64.g.lun << ppaf->lun_offset;
1101 ppa32 |= ppa64.g.blk << ppaf->blk_offset;
1102 ppa32 |= ppa64.g.pg << ppaf->pg_offset;
1103 ppa32 |= ppa64.g.pl << ppaf->pln_offset;
1104 ppa32 |= ppa64.g.sec << ppaf->sec_offset;
1105 } else {
1106 struct nvm_addrf *lbaf = &pblk->addrf;
1107
1108 ppa32 |= ppa64.m.grp << lbaf->ch_offset;
1109 ppa32 |= ppa64.m.pu << lbaf->lun_offset;
1110 ppa32 |= ppa64.m.chk << lbaf->chk_offset;
1111 ppa32 |= ppa64.m.sec << lbaf->sec_offset;
1112 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113 }
1114
1115 return ppa32;
1116}
1117
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001118static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1119 sector_t lba)
1120{
1121 struct ppa_addr ppa;
1122
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001123 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001124 u32 *map = (u32 *)pblk->trans_map;
1125
1126 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1127 } else {
1128 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1129
1130 ppa = map[lba];
1131 }
1132
1133 return ppa;
1134}
1135
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001136static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1137 struct ppa_addr ppa)
1138{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001139 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001140 u32 *map = (u32 *)pblk->trans_map;
1141
1142 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1143 } else {
1144 u64 *map = (u64 *)pblk->trans_map;
1145
1146 map[lba] = ppa.ppa;
1147 }
1148}
1149
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001150static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1151{
1152 return (ppa_addr.ppa == ADDR_EMPTY);
1153}
1154
1155static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1156{
1157 ppa_addr->ppa = ADDR_EMPTY;
1158}
1159
Javier González076984662017-06-30 17:56:42 +02001160static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1161{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001162 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001163}
1164
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001165static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1166{
1167 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1168}
1169
1170static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1171{
1172 return ppa.c.line;
1173}
1174
1175static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1176{
1177 struct ppa_addr p;
1178
1179 p.c.line = addr;
1180 p.c.is_cached = 1;
1181
1182 return p;
1183}
1184
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001185static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001186 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001187{
1188 u32 crc = ~(u32)0;
1189
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001190 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001191 sizeof(struct line_header) - sizeof(crc));
1192
1193 return crc;
1194}
1195
1196static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1197 struct line_smeta *smeta)
1198{
1199 struct pblk_line_meta *lm = &pblk->lm;
1200 u32 crc = ~(u32)0;
1201
1202 crc = crc32_le(crc, (unsigned char *)smeta +
1203 sizeof(struct line_header) + sizeof(crc),
1204 lm->smeta_len -
1205 sizeof(struct line_header) - sizeof(crc));
1206
1207 return crc;
1208}
1209
1210static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1211 struct line_emeta *emeta)
1212{
1213 struct pblk_line_meta *lm = &pblk->lm;
1214 u32 crc = ~(u32)0;
1215
1216 crc = crc32_le(crc, (unsigned char *)emeta +
1217 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001218 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001219 sizeof(struct line_header) - sizeof(crc));
1220
1221 return crc;
1222}
1223
1224static inline int pblk_set_progr_mode(struct pblk *pblk, int type)
1225{
1226 struct nvm_tgt_dev *dev = pblk->dev;
1227 struct nvm_geo *geo = &dev->geo;
1228 int flags;
1229
Javier González3b2a3ad2018-03-30 00:05:21 +02001230 if (geo->version == NVM_OCSSD_SPEC_20)
1231 return 0;
1232
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001233 flags = geo->pln_mode >> 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001235 if (type == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001236 flags |= NVM_IO_SCRAMBLE_ENABLE;
1237
1238 return flags;
1239}
1240
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001241enum {
1242 PBLK_READ_RANDOM = 0,
1243 PBLK_READ_SEQUENTIAL = 1,
1244};
1245
1246static inline int pblk_set_read_mode(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001247{
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001248 struct nvm_tgt_dev *dev = pblk->dev;
1249 struct nvm_geo *geo = &dev->geo;
1250 int flags;
1251
Javier González3b2a3ad2018-03-30 00:05:21 +02001252 if (geo->version == NVM_OCSSD_SPEC_20)
1253 return 0;
1254
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001255 flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE;
1256 if (type == PBLK_READ_SEQUENTIAL)
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001257 flags |= geo->pln_mode >> 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001258
1259 return flags;
1260}
1261
1262static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1263{
1264 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001265}
1266
1267#ifdef CONFIG_NVM_DEBUG
Javier González3b2a3ad2018-03-30 00:05:21 +02001268static inline void print_ppa(struct nvm_geo *geo, struct ppa_addr *p,
1269 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001270{
1271 if (p->c.is_cached) {
1272 pr_err("ppa: (%s: %x) cache line: %llu\n",
1273 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001274 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001275 pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
1276 msg, error,
1277 p->g.ch, p->g.lun, p->g.blk,
1278 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001279 } else {
1280 pr_err("ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
1281 msg, error,
1282 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001283 }
1284}
1285
1286static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1287 int error)
1288{
1289 int bit = -1;
1290
1291 if (rqd->nr_ppas == 1) {
Javier González3b2a3ad2018-03-30 00:05:21 +02001292 print_ppa(&pblk->dev->geo, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001293 return;
1294 }
1295
1296 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1297 bit + 1)) < rqd->nr_ppas) {
Javier González3b2a3ad2018-03-30 00:05:21 +02001298 print_ppa(&pblk->dev->geo, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001299 }
1300
1301 pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
1302}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001303
1304static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1305 struct ppa_addr *ppas, int nr_ppas)
1306{
1307 struct nvm_geo *geo = &tgt_dev->geo;
1308 struct ppa_addr *ppa;
1309 int i;
1310
1311 for (i = 0; i < nr_ppas; i++) {
1312 ppa = &ppas[i];
1313
Javier González3b2a3ad2018-03-30 00:05:21 +02001314 if (geo->version == NVM_OCSSD_SPEC_12) {
1315 if (!ppa->c.is_cached &&
1316 ppa->g.ch < geo->num_ch &&
1317 ppa->g.lun < geo->num_lun &&
1318 ppa->g.pl < geo->num_pln &&
1319 ppa->g.blk < geo->num_chk &&
1320 ppa->g.pg < geo->num_pg &&
1321 ppa->g.sec < geo->ws_min)
1322 continue;
1323 } else {
1324 if (!ppa->c.is_cached &&
1325 ppa->m.grp < geo->num_ch &&
1326 ppa->m.pu < geo->num_lun &&
1327 ppa->m.chk < geo->num_chk &&
1328 ppa->m.sec < geo->clba)
1329 continue;
1330 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001331
Javier González3b2a3ad2018-03-30 00:05:21 +02001332 print_ppa(geo, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001333
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001334 return 1;
1335 }
1336 return 0;
1337}
1338
Javier González1a94b2d2017-10-13 14:46:47 +02001339static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1340{
1341 struct nvm_tgt_dev *dev = pblk->dev;
1342 struct ppa_addr *ppa_list;
1343
1344 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1345
1346 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1347 WARN_ON(1);
1348 return -EINVAL;
1349 }
1350
1351 if (rqd->opcode == NVM_OP_PWRITE) {
1352 struct pblk_line *line;
1353 struct ppa_addr ppa;
1354 int i;
1355
1356 for (i = 0; i < rqd->nr_ppas; i++) {
1357 ppa = ppa_list[i];
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001358 line = &pblk->lines[pblk_ppa_to_line(ppa)];
Javier González1a94b2d2017-10-13 14:46:47 +02001359
1360 spin_lock(&line->lock);
1361 if (line->state != PBLK_LINESTATE_OPEN) {
1362 pr_err("pblk: bad ppa: line:%d,state:%d\n",
1363 line->id, line->state);
1364 WARN_ON(1);
1365 spin_unlock(&line->lock);
1366 return -EINVAL;
1367 }
1368 spin_unlock(&line->lock);
1369 }
1370 }
1371
1372 return 0;
1373}
1374#endif
1375
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001376static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1377{
1378 struct pblk_line_meta *lm = &pblk->lm;
1379
1380 if (paddr > lm->sec_per_line)
1381 return 1;
1382
1383 return 0;
1384}
1385
1386static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1387{
1388 return bio->bi_iter.bi_idx;
1389}
1390
1391static inline sector_t pblk_get_lba(struct bio *bio)
1392{
1393 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1394}
1395
1396static inline unsigned int pblk_get_secs(struct bio *bio)
1397{
1398 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1399}
1400
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001401static inline void pblk_setup_uuid(struct pblk *pblk)
1402{
1403 uuid_le uuid;
1404
1405 uuid_le_gen(&uuid);
1406 memcpy(pblk->instance_uuid, uuid.b, 16);
1407}
1408#endif /* PBLK_H_ */