blob: e5b88a25d4d6e2a1ab85a1c92fb37d0519929d60 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001/* SPDX-License-Identifier: GPL-2.0 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2015 IT University of Copenhagen (rrpc.h)
4 * Copyright (C) 2016 CNEX Labs
5 * Initial release: Matias Bjorling <matias@cnexlabs.com>
6 * Write buffering: Javier Gonzalez <javier@cnexlabs.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * Implementation of a Physical Block-device target for Open-channel SSDs.
18 *
19 */
20
21#ifndef PBLK_H_
22#define PBLK_H_
23
24#include <linux/blkdev.h>
25#include <linux/blk-mq.h>
26#include <linux/bio.h>
27#include <linux/module.h>
28#include <linux/kthread.h>
29#include <linux/vmalloc.h>
30#include <linux/crc32.h>
31#include <linux/uuid.h>
32
33#include <linux/lightnvm.h>
34
35/* Run only GC if less than 1/X blocks are free */
36#define GC_LIMIT_INVERSE 5
37#define GC_TIME_MSECS 1000
38
39#define PBLK_SECTOR (512)
40#define PBLK_EXPOSED_PAGE_SIZE (4096)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020041
Javier Gonzálezef576492017-06-26 11:57:28 +020042#define PBLK_NR_CLOSE_JOBS (4)
43
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044#define PBLK_CACHE_NAME_LEN (DISK_NAME_LEN + 16)
45
46#define PBLK_COMMAND_TIMEOUT_MS 30000
47
48/* Max 512 LUNs per device */
49#define PBLK_MAX_LUNS_BITMAP (4)
50
51#define NR_PHY_IN_LOG (PBLK_EXPOSED_PAGE_SIZE / PBLK_SECTOR)
52
Javier González0d880392017-10-13 14:46:08 +020053/* Static pool sizes */
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020054#define PBLK_GEN_WS_POOL_SIZE (2)
55
Javier Gonzáleze5392732018-01-05 14:16:14 +010056#define PBLK_DEFAULT_OP (11)
57
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058enum {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020059 PBLK_READ = READ,
60 PBLK_WRITE = WRITE,/* Write from write buffer */
61 PBLK_WRITE_INT, /* Internal write - no write buffer */
Javier González8f554592018-01-05 14:16:16 +010062 PBLK_READ_RECOV, /* Recovery read - errors allowed */
Javier Gonzáleze2cddf22017-10-13 14:46:19 +020063 PBLK_ERASE,
64};
65
66enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020067 /* IO Types */
68 PBLK_IOTYPE_USER = 1 << 0,
69 PBLK_IOTYPE_GC = 1 << 1,
70
71 /* Write buffer flags */
72 PBLK_FLUSH_ENTRY = 1 << 2,
73 PBLK_WRITTEN_DATA = 1 << 3,
74 PBLK_SUBMITTED_ENTRY = 1 << 4,
75 PBLK_WRITABLE_ENTRY = 1 << 5,
76};
77
78enum {
79 PBLK_BLK_ST_OPEN = 0x1,
80 PBLK_BLK_ST_CLOSED = 0x2,
81};
82
Hans Holmberg4209c312018-10-09 13:11:55 +020083enum {
84 PBLK_CHUNK_RESET_START,
85 PBLK_CHUNK_RESET_DONE,
86 PBLK_CHUNK_RESET_FAILED,
87};
88
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020089struct pblk_sec_meta {
90 u64 reserved;
91 __le64 lba;
92};
93
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020094/* The number of GC lists and the rate-limiter states go together. This way the
95 * rate-limiter can dictate how much GC is needed based on resource utilization.
96 */
Hans Holmberg48b8d202018-06-01 16:41:06 +020097#define PBLK_GC_NR_LISTS 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020098
99enum {
Hans Holmberg48b8d202018-06-01 16:41:06 +0200100 PBLK_RL_OFF = 0,
101 PBLK_RL_WERR = 1,
102 PBLK_RL_HIGH = 2,
103 PBLK_RL_MID = 3,
104 PBLK_RL_LOW = 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200105};
106
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200107#define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * NVM_MAX_VLBA)
108#define pblk_dma_ppa_size (sizeof(u64) * NVM_MAX_VLBA)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200109
Javier González084ec9b2017-06-26 16:27:13 -0600110/* write buffer completion context */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200111struct pblk_c_ctx {
112 struct list_head list; /* Head for out-of-order completion */
113
114 unsigned long *lun_bitmap; /* Luns used on current request */
115 unsigned int sentry;
116 unsigned int nr_valid;
117 unsigned int nr_padded;
118};
119
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200120/* read context */
Javier González084ec9b2017-06-26 16:27:13 -0600121struct pblk_g_ctx {
122 void *private;
Javier González998ba622018-01-05 14:16:20 +0100123 unsigned long start_time;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200124 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200125};
126
Heiner Litz11f6ad62018-07-13 10:48:44 +0200127/* partial read context */
128struct pblk_pr_ctx {
129 struct bio *orig_bio;
130 DECLARE_BITMAP(bitmap, NVM_MAX_VLBA);
131 unsigned int orig_nr_secs;
132 unsigned int bio_init_idx;
133 void *ppa_ptr;
134 dma_addr_t dma_ppa_list;
135};
136
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200137/* Pad context */
138struct pblk_pad_rq {
139 struct pblk *pblk;
140 struct completion wait;
141 struct kref ref;
142};
143
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200144/* Recovery context */
145struct pblk_rec_ctx {
146 struct pblk *pblk;
147 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200148 struct work_struct ws_rec;
149};
150
151/* Write context */
152struct pblk_w_ctx {
153 struct bio_list bios; /* Original bios - used for completion
154 * in REQ_FUA, REQ_FLUSH case
155 */
Arnd Bergmannef697902017-04-19 19:39:13 +0200156 u64 lba; /* Logic addr. associated with entry */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200157 struct ppa_addr ppa; /* Physic addr. associated with entry */
158 int flags; /* Write context flags */
159};
160
161struct pblk_rb_entry {
162 struct ppa_addr cacheline; /* Cacheline for this entry */
163 void *data; /* Pointer to data on this entry */
164 struct pblk_w_ctx w_ctx; /* Context for this entry */
165 struct list_head index; /* List head to enable indexes */
166};
167
168#define EMPTY_ENTRY (~0U)
169
170struct pblk_rb_pages {
171 struct page *pages;
172 int order;
173 struct list_head list;
174};
175
176struct pblk_rb {
177 struct pblk_rb_entry *entries; /* Ring buffer entries */
178 unsigned int mem; /* Write offset - points to next
179 * writable entry in memory
180 */
181 unsigned int subm; /* Read offset - points to last entry
182 * that has been submitted to the media
183 * to be persisted
184 */
185 unsigned int sync; /* Synced - backpointer that signals
186 * the last submitted entry that has
187 * been successfully persisted to media
188 */
Hans Holmberg8154d292018-01-05 14:16:09 +0100189 unsigned int flush_point; /* Sync point - last entry that must be
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200190 * flushed to the media. Used with
191 * REQ_FLUSH and REQ_FUA
192 */
193 unsigned int l2p_update; /* l2p update point - next entry for
194 * which l2p mapping will be updated to
195 * contain a device ppa address (instead
196 * of a cacheline
197 */
198 unsigned int nr_entries; /* Number of entries in write buffer -
199 * must be a power of two
200 */
201 unsigned int seg_size; /* Size of the data segments being
202 * stored on each entry. Typically this
203 * will be 4KB
204 */
205
Javier González766c8ce2018-10-09 13:12:15 +0200206 unsigned int back_thres; /* Threshold that shall be maintained by
207 * the backpointer in order to respect
208 * geo->mw_cunits on a per chunk basis
209 */
210
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200211 struct list_head pages; /* List of data pages */
212
213 spinlock_t w_lock; /* Write lock */
214 spinlock_t s_lock; /* Sync lock */
215
Matias Bjørling880eda52018-07-13 10:48:37 +0200216#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg8154d292018-01-05 14:16:09 +0100217 atomic_t inflight_flush_point; /* Not served REQ_FLUSH | REQ_FUA */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200218#endif
219};
220
221#define PBLK_RECOVERY_SECTORS 16
222
223struct pblk_lun {
224 struct ppa_addr bppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200225 struct semaphore wr_sem;
226};
227
228struct pblk_gc_rq {
229 struct pblk_line *line;
230 void *data;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200231 u64 paddr_list[NVM_MAX_VLBA];
232 u64 lba_list[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200233 int nr_secs;
234 int secs_to_gc;
235 struct list_head list;
236};
237
238struct pblk_gc {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239 /* These states are not protected by a lock since (i) they are in the
240 * fast path, and (ii) they are not critical.
241 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200242 int gc_active;
243 int gc_enabled;
244 int gc_forced;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245
246 struct task_struct *gc_ts;
247 struct task_struct *gc_writer_ts;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200248 struct task_struct *gc_reader_ts;
249
250 struct workqueue_struct *gc_line_reader_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251 struct workqueue_struct *gc_reader_wq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200252
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253 struct timer_list gc_timer;
254
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200255 struct semaphore gc_sem;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200256 atomic_t read_inflight_gc; /* Number of lines with inflight GC reads */
257 atomic_t pipeline_gc; /* Number of lines in the GC pipeline -
258 * started reads to finished writes
259 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200260 int w_entries;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200261
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200262 struct list_head w_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200263 struct list_head r_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200264
265 spinlock_t lock;
266 spinlock_t w_lock;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200267 spinlock_t r_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200268};
269
270struct pblk_rl {
271 unsigned int high; /* Upper threshold for rate limiter (free run -
272 * user I/O rate limiter
273 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274 unsigned int high_pw; /* High rounded up as a power of 2 */
275
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200276#define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */
277#define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200278
279 int rb_windows_pw; /* Number of rate windows in the write buffer
280 * given as a power-of-2. This guarantees that
281 * when user I/O is being rate limited, there
282 * will be reserved enough space for the GC to
283 * place its payload. A window is of
284 * pblk->max_write_pgs size, which in NVMe is
285 * 64, i.e., 256kb.
286 */
287 int rb_budget; /* Total number of entries available for I/O */
288 int rb_user_max; /* Max buffer entries available for user I/O */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200289 int rb_gc_max; /* Max buffer entries available for GC I/O */
290 int rb_gc_rsv; /* Reserved buffer entries for GC I/O */
291 int rb_state; /* Rate-limiter current state */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200292 int rb_max_io; /* Maximum size for an I/O giving the config */
Javier González588726d32017-06-26 11:57:29 +0200293
294 atomic_t rb_user_cnt; /* User I/O buffer counter */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200295 atomic_t rb_gc_cnt; /* GC I/O buffer counter */
Javier González588726d32017-06-26 11:57:29 +0200296 atomic_t rb_space; /* Space limit in case of reaching capacity */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200298 int rsv_blocks; /* Reserved blocks for GC */
299
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 int rb_user_active;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200301 int rb_gc_active;
302
Hans Holmberg48b8d202018-06-01 16:41:06 +0200303 atomic_t werr_lines; /* Number of write error lines that needs gc */
304
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305 struct timer_list u_timer;
306
307 unsigned long long nr_secs;
308 unsigned long total_blocks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100309
310 atomic_t free_blocks; /* Total number of free blocks (+ OP) */
311 atomic_t free_user_blocks; /* Number of user free blocks (no OP) */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200312};
313
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200314#define PBLK_LINE_EMPTY (~0U)
315
316enum {
317 /* Line Types */
318 PBLK_LINETYPE_FREE = 0,
319 PBLK_LINETYPE_LOG = 1,
320 PBLK_LINETYPE_DATA = 2,
321
322 /* Line state */
Javier González32ef9412018-03-30 00:05:20 +0200323 PBLK_LINESTATE_NEW = 9,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200324 PBLK_LINESTATE_FREE = 10,
325 PBLK_LINESTATE_OPEN = 11,
326 PBLK_LINESTATE_CLOSED = 12,
327 PBLK_LINESTATE_GC = 13,
328 PBLK_LINESTATE_BAD = 14,
329 PBLK_LINESTATE_CORRUPT = 15,
330
331 /* GC group */
332 PBLK_LINEGC_NONE = 20,
333 PBLK_LINEGC_EMPTY = 21,
334 PBLK_LINEGC_LOW = 22,
335 PBLK_LINEGC_MID = 23,
336 PBLK_LINEGC_HIGH = 24,
337 PBLK_LINEGC_FULL = 25,
Hans Holmberg48b8d202018-06-01 16:41:06 +0200338 PBLK_LINEGC_WERR = 26
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200339};
340
341#define PBLK_MAGIC 0x70626c6b /*pblk*/
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200342
343/* emeta/smeta persistent storage format versions:
344 * Changes in major version requires offline migration.
345 * Changes in minor version are handled automatically during
346 * recovery.
347 */
348
349#define SMETA_VERSION_MAJOR (0)
350#define SMETA_VERSION_MINOR (1)
351
352#define EMETA_VERSION_MAJOR (0)
Hans Holmberg76758392018-03-30 00:04:52 +0200353#define EMETA_VERSION_MINOR (2)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200354
355struct line_header {
356 __le32 crc;
357 __le32 identifier; /* pblk identifier */
358 __u8 uuid[16]; /* instance uuid */
359 __le16 type; /* line type */
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200360 __u8 version_major; /* version major */
361 __u8 version_minor; /* version minor */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362 __le32 id; /* line id for current line */
363};
364
365struct line_smeta {
366 struct line_header header;
367
368 __le32 crc; /* Full structure including struct crc */
369 /* Previous line metadata */
370 __le32 prev_id; /* Line id for previous line */
371
372 /* Current line metadata */
373 __le64 seq_nr; /* Sequence number for current line */
374
375 /* Active writers */
376 __le32 window_wr_lun; /* Number of parallel LUNs to write */
377
378 __le32 rsvd[2];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200379
380 __le64 lun_bitmap[];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200381};
382
Hans Holmberg76758392018-03-30 00:04:52 +0200383
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384/*
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200385 * Metadata layout in media:
386 * First sector:
387 * 1. struct line_emeta
388 * 2. bad block bitmap (u64 * window_wr_lun)
Hans Holmberg76758392018-03-30 00:04:52 +0200389 * 3. write amplification counters
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200390 * Mid sectors (start at lbas_sector):
391 * 3. nr_lbas (u64) forming lba list
392 * Last sectors (start at vsc_sector):
393 * 4. u32 valid sector count (vsc) for all lines (~0U: free line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200394 */
395struct line_emeta {
396 struct line_header header;
397
398 __le32 crc; /* Full structure including struct crc */
399
400 /* Previous line metadata */
401 __le32 prev_id; /* Line id for prev line */
402
403 /* Current line metadata */
404 __le64 seq_nr; /* Sequence number for current line */
405
406 /* Active writers */
407 __le32 window_wr_lun; /* Number of parallel LUNs to write */
408
409 /* Bookkeeping for recovery */
410 __le32 next_id; /* Line id for next line */
411 __le64 nr_lbas; /* Number of lbas mapped in line */
412 __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */
Hans Holmberg76758392018-03-30 00:04:52 +0200413 __le64 bb_bitmap[]; /* Updated bad block bitmap for line */
414};
415
416
417/* Write amplification counters stored on media */
418struct wa_counters {
419 __le64 user; /* Number of user written sectors */
420 __le64 gc; /* Number of sectors written by GC*/
421 __le64 pad; /* Number of padded sectors */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200422};
423
424struct pblk_emeta {
425 struct line_emeta *buf; /* emeta buffer in media format */
426 int mem; /* Write offset - points to next
427 * writable entry in memory
428 */
429 atomic_t sync; /* Synced - backpointer that signals the
430 * last entry that has been successfully
431 * persisted to media
432 */
433 unsigned int nr_entries; /* Number of emeta entries */
434};
435
436struct pblk_smeta {
437 struct line_smeta *buf; /* smeta buffer in persistent format */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438};
439
Hans Holmberg48b8d202018-06-01 16:41:06 +0200440struct pblk_w_err_gc {
441 int has_write_err;
442 __le64 *lba_list;
443};
444
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200445struct pblk_line {
446 struct pblk *pblk;
447 unsigned int id; /* Line number corresponds to the
448 * block line
449 */
450 unsigned int seq_nr; /* Unique line sequence number */
451
452 int state; /* PBLK_LINESTATE_X */
453 int type; /* PBLK_LINETYPE_X */
454 int gc_group; /* PBLK_LINEGC_X */
455 struct list_head list; /* Free, GC lists */
456
457 unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */
458
Javier González32ef9412018-03-30 00:05:20 +0200459 struct nvm_chk_meta *chks; /* Chunks forming line */
460
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200461 struct pblk_smeta *smeta; /* Start metadata */
462 struct pblk_emeta *emeta; /* End medatada */
463
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464 int meta_line; /* Metadata line id */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200465 int meta_distance; /* Distance between data and metadata */
466
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 u64 smeta_ssec; /* Sector where smeta starts */
468 u64 emeta_ssec; /* Sector where emeta starts */
469
470 unsigned int sec_in_line; /* Number of usable secs in line */
471
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200472 atomic_t blk_in_line; /* Number of good blocks in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200473 unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */
474 unsigned long *erase_bitmap; /* Bitmap for erased blocks */
475
476 unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */
477 unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */
478
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200479 atomic_t left_eblks; /* Blocks left for erasing */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480 atomic_t left_seblks; /* Blocks left for sync erasing */
481
482 int left_msecs; /* Sectors left for mapping */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200483 unsigned int cur_sec; /* Sector map pointer */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200484 unsigned int nr_valid_lbas; /* Number of valid lbas in line */
485
486 __le32 *vsc; /* Valid sector count in line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487
488 struct kref ref; /* Write buffer L2P references */
489
Hans Holmberg48b8d202018-06-01 16:41:06 +0200490 struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
491
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492 spinlock_t lock; /* Necessary for invalid_bitmap only */
493};
494
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200495#define PBLK_DATA_LINES 4
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200496
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200497enum {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200498 PBLK_KMALLOC_META = 1,
499 PBLK_VMALLOC_META = 2,
500};
501
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200502enum {
503 PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */
504 PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */
505 PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506};
507
508struct pblk_line_mgmt {
509 int nr_lines; /* Total number of full lines */
510 int nr_free_lines; /* Number of full lines in free list */
511
512 /* Free lists - use free_lock */
513 struct list_head free_list; /* Full lines ready to use */
514 struct list_head corrupt_list; /* Full lines corrupted */
515 struct list_head bad_list; /* Full lines bad */
516
517 /* GC lists - use gc_lock */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200518 struct list_head *gc_lists[PBLK_GC_NR_LISTS];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200519 struct list_head gc_high_list; /* Full lines ready to GC, high isc */
520 struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */
521 struct list_head gc_low_list; /* Full lines ready to GC, low isc */
522
Hans Holmberg48b8d202018-06-01 16:41:06 +0200523 struct list_head gc_werr_list; /* Write err recovery list */
524
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200525 struct list_head gc_full_list; /* Full lines ready to GC, no valid */
526 struct list_head gc_empty_list; /* Full lines close, all valid */
527
528 struct pblk_line *log_line; /* Current FTL log line */
529 struct pblk_line *data_line; /* Current data line */
530 struct pblk_line *log_next; /* Next FTL log line */
531 struct pblk_line *data_next; /* Next data line */
532
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200533 struct list_head emeta_list; /* Lines queued to schedule emeta */
534
535 __le32 *vsc_list; /* Valid sector counts for all lines */
536
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200537 /* Metadata allocation type: VMALLOC | KMALLOC */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538 int emeta_alloc_type;
539
540 /* Pre-allocated metadata for data lines */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200541 struct pblk_smeta *sline_meta[PBLK_DATA_LINES];
542 struct pblk_emeta *eline_meta[PBLK_DATA_LINES];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200543 unsigned long meta_bitmap;
544
Hans Holmberg53d82db2018-10-09 13:11:47 +0200545 /* Cache and mempool for map/invalid bitmaps */
546 struct kmem_cache *bitmap_cache;
547 mempool_t *bitmap_pool;
548
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200549 /* Helpers for fast bitmap calculations */
550 unsigned long *bb_template;
551 unsigned long *bb_aux;
552
553 unsigned long d_seq_nr; /* Data line unique sequence number */
554 unsigned long l_seq_nr; /* Log line unique sequence number */
555
556 spinlock_t free_lock;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200557 spinlock_t close_lock;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558 spinlock_t gc_lock;
559};
560
561struct pblk_line_meta {
562 unsigned int smeta_len; /* Total length for smeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200563 unsigned int smeta_sec; /* Sectors needed for smeta */
564
565 unsigned int emeta_len[4]; /* Lengths for emeta:
Hans Holmberg76758392018-03-30 00:04:52 +0200566 * [0]: Total
567 * [1]: struct line_emeta +
568 * bb_bitmap + struct wa_counters
569 * [2]: L2P portion
570 * [3]: vsc
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200571 */
572 unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout
573 * as emeta_len
574 */
575
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200576 unsigned int emeta_bb; /* Boundary for bb that affects emeta */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200577
578 unsigned int vsc_list_len; /* Length for vsc list */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579 unsigned int sec_bitmap_len; /* Length for sector bitmap in line */
580 unsigned int blk_bitmap_len; /* Length for block bitmap in line */
581 unsigned int lun_bitmap_len; /* Length for lun bitmap in line */
582
583 unsigned int blk_per_line; /* Number of blocks in a full line */
584 unsigned int sec_per_line; /* Number of sectors in a line */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200585 unsigned int dsec_per_line; /* Number of data sectors in a line */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200586 unsigned int min_blk_line; /* Min. number of good blocks in line */
587
588 unsigned int mid_thrs; /* Threshold for GC mid list */
589 unsigned int high_thrs; /* Threshold for GC high list */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200590
591 unsigned int meta_distance; /* Distance between data and metadata */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200592};
593
Javier González588726d32017-06-26 11:57:29 +0200594enum {
595 PBLK_STATE_RUNNING = 0,
596 PBLK_STATE_STOPPING = 1,
597 PBLK_STATE_RECOVERING = 2,
598 PBLK_STATE_STOPPED = 3,
599};
600
Javier González3b2a3ad2018-03-30 00:05:21 +0200601/* Internal format to support not power-of-2 device formats */
602struct pblk_addrf {
603 /* gen to dev */
604 int sec_stripe;
605 int ch_stripe;
606 int lun_stripe;
607
608 /* dev to gen */
609 int sec_lun_stripe;
610 int sec_ws_stripe;
611};
612
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200613struct pblk {
614 struct nvm_tgt_dev *dev;
615 struct gendisk *disk;
616
617 struct kobject kobj;
618
619 struct pblk_lun *luns;
620
621 struct pblk_line *lines; /* Line array */
622 struct pblk_line_mgmt l_mg; /* Line management */
623 struct pblk_line_meta lm; /* Line metadata */
624
Javier González3b2a3ad2018-03-30 00:05:21 +0200625 struct nvm_addrf addrf; /* Aligned address format */
626 struct pblk_addrf uaddrf; /* Unaligned address format */
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200627 int addrf_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628
629 struct pblk_rb rwb;
630
Javier González588726d32017-06-26 11:57:29 +0200631 int state; /* pblk line state */
632
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633 int min_write_pgs; /* Minimum amount of pages required by controller */
634 int max_write_pgs; /* Maximum amount of pages supported by controller */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635
636 sector_t capacity; /* Device capacity when bad blocks are subtracted */
Javier Gonzáleza7689932018-01-05 14:16:13 +0100637
638 int op; /* Percentage of device used for over-provisioning */
639 int op_blks; /* Number of blocks used for over-provisioning */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200640
641 /* pblk provisioning values. Used by rate limiter */
642 struct pblk_rl rl;
643
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200644 int sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645
646 unsigned char instance_uuid[16];
Hans Holmberg76758392018-03-30 00:04:52 +0200647
648 /* Persistent write amplification counters, 4kb sector I/Os */
649 atomic64_t user_wa; /* Sectors written by user */
650 atomic64_t gc_wa; /* Sectors written by GC */
651 atomic64_t pad_wa; /* Padded sectors written */
652
653 /* Reset values for delta write amplification measurements */
654 u64 user_rst_wa;
655 u64 gc_rst_wa;
656 u64 pad_rst_wa;
657
Hans Holmberg5d149bf2018-03-30 00:04:55 +0200658 /* Counters used for calculating padding distribution */
659 atomic64_t *pad_dist; /* Padding distribution buckets */
660 u64 nr_flush_rst; /* Flushes reset value for pad dist.*/
661 atomic64_t nr_flush; /* Number of flush/fua I/O */
662
Matias Bjørling880eda52018-07-13 10:48:37 +0200663#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmberg76758392018-03-30 00:04:52 +0200664 /* Non-persistent debug counters, 4kb sector I/Os */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200665 atomic_long_t inflight_writes; /* Inflight writes (user and gc) */
666 atomic_long_t padded_writes; /* Sectors padded due to flush/fua */
667 atomic_long_t padded_wb; /* Sectors padded in write buffer */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200668 atomic_long_t req_writes; /* Sectors stored on write buffer */
669 atomic_long_t sub_writes; /* Sectors submitted from buffer */
670 atomic_long_t sync_writes; /* Sectors synced to media */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200671 atomic_long_t inflight_reads; /* Inflight sector read requests */
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200672 atomic_long_t cache_reads; /* Read requests that hit the cache */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200673 atomic_long_t sync_reads; /* Completed sector read requests */
674 atomic_long_t recov_writes; /* Sectors submitted from recovery */
675 atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */
676 atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */
677#endif
678
679 spinlock_t lock;
680
681 atomic_long_t read_failed;
682 atomic_long_t read_empty;
683 atomic_long_t read_high_ecc;
684 atomic_long_t read_failed_gc;
685 atomic_long_t write_failed;
686 atomic_long_t erase_failed;
687
Javier González588726d32017-06-26 11:57:29 +0200688 atomic_t inflight_io; /* General inflight I/O counter */
689
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690 struct task_struct *writer_ts;
691
692 /* Simple translation map of logical addresses to physical addresses.
693 * The logical addresses is known by the host system, while the physical
694 * addresses are used when writing to the disk block device.
695 */
696 unsigned char *trans_map;
697 spinlock_t trans_lock;
698
699 struct list_head compl_list;
700
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200701 spinlock_t resubmit_lock; /* Resubmit list lock */
702 struct list_head resubmit_list; /* Resubmit list for failed writes*/
703
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400704 mempool_t page_bio_pool;
705 mempool_t gen_ws_pool;
706 mempool_t rec_pool;
707 mempool_t r_rq_pool;
708 mempool_t w_rq_pool;
709 mempool_t e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200710
Javier Gonzálezef576492017-06-26 11:57:28 +0200711 struct workqueue_struct *close_wq;
712 struct workqueue_struct *bb_wq;
Javier González7bd4d372017-10-13 14:46:23 +0200713 struct workqueue_struct *r_end_wq;
Javier Gonzálezef576492017-06-26 11:57:28 +0200714
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200715 struct timer_list wtimer;
716
717 struct pblk_gc gc;
718};
719
720struct pblk_line_ws {
721 struct pblk *pblk;
722 struct pblk_line *line;
723 void *priv;
724 struct work_struct ws;
725};
726
Javier González084ec9b2017-06-26 16:27:13 -0600727#define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200728#define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx))
729
Matias Bjørling4e495a42018-07-13 10:48:42 +0200730#define pblk_err(pblk, fmt, ...) \
731 pr_err("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
732#define pblk_info(pblk, fmt, ...) \
733 pr_info("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
734#define pblk_warn(pblk, fmt, ...) \
735 pr_warn("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
736#define pblk_debug(pblk, fmt, ...) \
737 pr_debug("pblk %s: " fmt, pblk->disk->disk_name, ##__VA_ARGS__)
738
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200739/*
740 * pblk ring buffer operations
741 */
Javier González766c8ce2018-10-09 13:12:15 +0200742int pblk_rb_init(struct pblk_rb *rb, unsigned int size, unsigned int threshold,
743 unsigned int seg_sz);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200744int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
745 unsigned int nr_entries, unsigned int *pos);
746int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
747 unsigned int *pos);
748void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
749 struct pblk_w_ctx w_ctx, unsigned int pos);
750void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200751 struct pblk_w_ctx w_ctx, struct pblk_line *line,
752 u64 paddr, unsigned int pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200753struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos);
Javier González588726d32017-06-26 11:57:29 +0200754void pblk_rb_flush(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200755
756void pblk_rb_sync_l2p(struct pblk_rb *rb);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200757unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
Javier González875d94f2017-10-13 14:46:18 +0200758 unsigned int pos, unsigned int nr_entries,
759 unsigned int count);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200760int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
Javier González75cb8e92017-07-28 15:13:16 +0200761 struct ppa_addr ppa, int bio_iter, bool advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries);
763
764unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags);
765unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries);
Javier González40b86572018-10-09 13:12:09 +0200766unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p,
767 unsigned int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200768void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags);
Hans Holmberg8154d292018-01-05 14:16:09 +0100769unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200770
771unsigned int pblk_rb_read_count(struct pblk_rb *rb);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200772unsigned int pblk_rb_sync_count(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200773unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos);
774
775int pblk_rb_tear_down_check(struct pblk_rb *rb);
776int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos);
Javier González9bd1f872018-10-09 13:12:10 +0200777void pblk_rb_free(struct pblk_rb *rb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200778ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf);
779
780/*
781 * pblk core
782 */
Javier González67bf26a2017-10-13 14:46:20 +0200783struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type);
784void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type);
Javier González45dcf292018-10-09 13:12:02 +0200785int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
786void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200787void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200788int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd,
789 struct pblk_c_ctx *c_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790void pblk_discard(struct pblk *pblk, struct bio *bio);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200791struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk);
Javier González32ef9412018-03-30 00:05:20 +0200792struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
793 struct nvm_chk_meta *lp,
794 struct ppa_addr ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200795void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd);
796void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd);
797int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200798int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd);
Javier González253babc2018-10-09 13:12:04 +0200799int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200800int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line);
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200801void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200802struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
803 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200804 int alloc_type, gfp_t gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805struct pblk_line *pblk_line_get(struct pblk *pblk);
806struct pblk_line *pblk_line_get_first_data(struct pblk *pblk);
Javier González21d22872017-10-13 14:46:26 +0200807struct pblk_line *pblk_line_replace_data(struct pblk *pblk);
Matias Bjørlingae14cc02018-10-09 13:11:40 +0200808void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa);
809void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200810int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line);
811void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line);
812struct pblk_line *pblk_line_get_data(struct pblk *pblk);
Javier Gonzálezd624f372017-06-26 11:57:15 +0200813struct pblk_line *pblk_line_get_erase(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200814int pblk_line_erase(struct pblk *pblk, struct pblk_line *line);
815int pblk_line_is_full(struct pblk_line *line);
Javier González8e55c072018-06-01 15:04:22 +0200816void pblk_line_free(struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200817void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200818void pblk_line_close(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200819void pblk_line_close_ws(struct work_struct *work);
Javier González588726d32017-06-26 11:57:29 +0200820void pblk_pipeline_stop(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200821void __pblk_pipeline_stop(struct pblk *pblk);
822void __pblk_pipeline_flush(struct pblk *pblk);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200823void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
824 void (*work)(struct work_struct *), gfp_t gfp_mask,
825 struct workqueue_struct *wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200826u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200827int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line);
828int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200829 void *emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa);
831void pblk_line_put(struct kref *ref);
Javier González7bd4d372017-10-13 14:46:23 +0200832void pblk_line_put_wq(struct kref *ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200833struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200834u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line);
835void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200836u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200837u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200838int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
839 unsigned long secs_to_flush);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200840void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200841 unsigned long *lun_bitmap);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200842void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa);
843void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa);
Hans Holmberge99e8022018-10-09 13:11:48 +0200844void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200845int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
846 int nr_pages);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200847void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
848 int nr_pages);
849void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa);
Javier González0880a9a2017-06-26 11:57:19 +0200850void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
851 u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa);
853void pblk_update_map_cache(struct pblk *pblk, sector_t lba,
854 struct ppa_addr ppa);
855void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
856 struct ppa_addr ppa, struct ppa_addr entry_line);
857int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200858 struct pblk_line *gc_line, u64 paddr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200859void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
860 u64 *lba_list, int nr_secs);
861void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
862 sector_t blba, int nr_secs);
863
864/*
865 * pblk user I/O write path
866 */
867int pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
868 unsigned long flags);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200869int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200870
871/*
872 * pblk map
873 */
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100874int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200875 unsigned int sentry, unsigned long *lun_bitmap,
876 unsigned int valid_secs, struct ppa_addr *erase_ppa);
Hans Holmberg525f7bb2018-12-11 20:16:12 +0100877int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200878 unsigned long *lun_bitmap, unsigned int valid_secs,
879 unsigned int off);
880
881/*
882 * pblk write thread
883 */
884int pblk_write_ts(void *data);
Kees Cook87c1d2d2017-10-17 21:10:19 -0700885void pblk_write_timer_fn(struct timer_list *t);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886void pblk_write_should_kick(struct pblk *pblk);
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200887void pblk_write_kick(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200888
889/*
890 * pblk read path
891 */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400892extern struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893int pblk_submit_read(struct pblk *pblk, struct bio *bio);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200894int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200895/*
896 * pblk recovery
897 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898struct pblk_line *pblk_recov_l2p(struct pblk *pblk);
Javier González588726d32017-06-26 11:57:29 +0200899int pblk_recov_pad(struct pblk *pblk);
Hans Holmberg06bc0722018-01-05 14:16:08 +0100900int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200901
902/*
903 * pblk gc
904 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200905#define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */
Javier González36278962017-10-13 14:46:11 +0200906#define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200907#define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200908
909int pblk_gc_init(struct pblk *pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200910void pblk_gc_exit(struct pblk *pblk, bool graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200911void pblk_gc_should_start(struct pblk *pblk);
912void pblk_gc_should_stop(struct pblk *pblk);
Hans Holmberg03661b52017-10-13 14:46:37 +0200913void pblk_gc_should_kick(struct pblk *pblk);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200914void pblk_gc_free_full_lines(struct pblk *pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200915void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
916 int *gc_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200917int pblk_gc_sysfs_force(struct pblk *pblk, int force);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200918
919/*
920 * pblk rate limiter
921 */
922void pblk_rl_init(struct pblk_rl *rl, int budget);
923void pblk_rl_free(struct pblk_rl *rl);
Hans Holmberg03661b52017-10-13 14:46:37 +0200924void pblk_rl_update_rates(struct pblk_rl *rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200925int pblk_rl_high_thrs(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200926unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100927unsigned long pblk_rl_nr_user_free_blks(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200928int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries);
Javier González588726d32017-06-26 11:57:29 +0200929void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200930void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries);
931int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries);
932void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries);
933void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc);
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200934int pblk_rl_max_io(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200935void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100936void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line,
937 bool used);
Javier González588726d32017-06-26 11:57:29 +0200938int pblk_rl_is_limit(struct pblk_rl *rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200939
Hans Holmberg48b8d202018-06-01 16:41:06 +0200940void pblk_rl_werr_line_in(struct pblk_rl *rl);
941void pblk_rl_werr_line_out(struct pblk_rl *rl);
942
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943/*
944 * pblk sysfs
945 */
946int pblk_sysfs_init(struct gendisk *tdisk);
947void pblk_sysfs_exit(struct gendisk *tdisk);
948
949static inline void *pblk_malloc(size_t size, int type, gfp_t flags)
950{
951 if (type == PBLK_KMALLOC_META)
952 return kmalloc(size, flags);
953 return vmalloc(size);
954}
955
956static inline void pblk_mfree(void *ptr, int type)
957{
958 if (type == PBLK_KMALLOC_META)
959 kfree(ptr);
960 else
961 vfree(ptr);
962}
963
964static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx)
965{
966 return c_ctx - sizeof(struct nvm_rq);
967}
968
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200969static inline void *emeta_to_bb(struct line_emeta *emeta)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200970{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200971 return emeta->bb_bitmap;
972}
973
Hans Holmberg76758392018-03-30 00:04:52 +0200974static inline void *emeta_to_wa(struct pblk_line_meta *lm,
975 struct line_emeta *emeta)
976{
977 return emeta->bb_bitmap + lm->blk_bitmap_len;
978}
979
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200980static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta)
981{
982 return ((void *)emeta + pblk->lm.emeta_len[1]);
983}
984
985static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta)
986{
987 return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988}
989
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200990static inline int pblk_line_vsc(struct pblk_line *line)
991{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200992 return le32_to_cpu(*line->vsc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200993}
994
Javier Gonzálezcb216652018-10-09 13:11:42 +0200995static inline int pblk_ppa_to_line_id(struct ppa_addr p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200996{
Javier González69471512018-03-30 00:05:15 +0200997 return p.a.blk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200998}
999
Javier Gonzálezcb216652018-10-09 13:11:42 +02001000static inline struct pblk_line *pblk_ppa_to_line(struct pblk *pblk,
1001 struct ppa_addr p)
1002{
1003 return &pblk->lines[pblk_ppa_to_line_id(p)];
1004}
1005
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001006static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p)
1007{
Javier González69471512018-03-30 00:05:15 +02001008 return p.a.lun * geo->num_ch + p.a.ch;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001009}
1010
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001011static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr,
1012 u64 line_id)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001013{
Javier González3b2a3ad2018-03-30 00:05:21 +02001014 struct nvm_tgt_dev *dev = pblk->dev;
1015 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001016 struct ppa_addr ppa;
1017
Javier González3b2a3ad2018-03-30 00:05:21 +02001018 if (geo->version == NVM_OCSSD_SPEC_12) {
1019 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1020
1021 ppa.ppa = 0;
1022 ppa.g.blk = line_id;
1023 ppa.g.pg = (paddr & ppaf->pg_mask) >> ppaf->pg_offset;
1024 ppa.g.lun = (paddr & ppaf->lun_mask) >> ppaf->lun_offset;
1025 ppa.g.ch = (paddr & ppaf->ch_mask) >> ppaf->ch_offset;
1026 ppa.g.pl = (paddr & ppaf->pln_mask) >> ppaf->pln_offset;
1027 ppa.g.sec = (paddr & ppaf->sec_mask) >> ppaf->sec_offset;
1028 } else {
1029 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1030 int secs, chnls, luns;
1031
1032 ppa.ppa = 0;
1033
1034 ppa.m.chk = line_id;
1035
1036 paddr = div_u64_rem(paddr, uaddrf->sec_stripe, &secs);
1037 ppa.m.sec = secs;
1038
1039 paddr = div_u64_rem(paddr, uaddrf->ch_stripe, &chnls);
1040 ppa.m.grp = chnls;
1041
1042 paddr = div_u64_rem(paddr, uaddrf->lun_stripe, &luns);
1043 ppa.m.pu = luns;
1044
1045 ppa.m.sec += uaddrf->sec_stripe * paddr;
1046 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001047
1048 return ppa;
1049}
1050
Javier González2cf99bb2018-10-09 13:11:41 +02001051static inline struct nvm_chk_meta *pblk_dev_ppa_to_chunk(struct pblk *pblk,
1052 struct ppa_addr p)
1053{
1054 struct nvm_tgt_dev *dev = pblk->dev;
1055 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezcb216652018-10-09 13:11:42 +02001056 struct pblk_line *line = pblk_ppa_to_line(pblk, p);
Javier González2cf99bb2018-10-09 13:11:41 +02001057 int pos = pblk_ppa_to_pos(geo, p);
1058
1059 return &line->chks[pos];
1060}
1061
1062static inline u64 pblk_dev_ppa_to_chunk_addr(struct pblk *pblk,
1063 struct ppa_addr p)
1064{
1065 struct nvm_tgt_dev *dev = pblk->dev;
1066
1067 return dev_to_chunk_addr(dev->parent, &pblk->addrf, p);
1068}
1069
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001070static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk,
1071 struct ppa_addr p)
1072{
Javier González3b2a3ad2018-03-30 00:05:21 +02001073 struct nvm_tgt_dev *dev = pblk->dev;
1074 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001075 u64 paddr;
1076
Javier González3b2a3ad2018-03-30 00:05:21 +02001077 if (geo->version == NVM_OCSSD_SPEC_12) {
1078 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
1079
1080 paddr = (u64)p.g.ch << ppaf->ch_offset;
1081 paddr |= (u64)p.g.lun << ppaf->lun_offset;
1082 paddr |= (u64)p.g.pg << ppaf->pg_offset;
1083 paddr |= (u64)p.g.pl << ppaf->pln_offset;
1084 paddr |= (u64)p.g.sec << ppaf->sec_offset;
1085 } else {
1086 struct pblk_addrf *uaddrf = &pblk->uaddrf;
1087 u64 secs = p.m.sec;
1088 int sec_stripe;
1089
1090 paddr = (u64)p.m.grp * uaddrf->sec_stripe;
1091 paddr += (u64)p.m.pu * uaddrf->sec_lun_stripe;
1092
1093 secs = div_u64_rem(secs, uaddrf->sec_stripe, &sec_stripe);
1094 paddr += secs * uaddrf->sec_ws_stripe;
1095 paddr += sec_stripe;
1096 }
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001097
1098 return paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001099}
1100
1101static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32)
1102{
Javier González7f985f92018-10-09 13:11:56 +02001103 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001104
Javier González7f985f92018-10-09 13:11:56 +02001105 return nvm_ppa32_to_ppa64(dev->parent, &pblk->addrf, ppa32);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001106}
1107
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001108static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64)
1109{
Javier González7f985f92018-10-09 13:11:56 +02001110 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001111
Javier González7f985f92018-10-09 13:11:56 +02001112 return nvm_ppa64_to_ppa32(dev->parent, &pblk->addrf, ppa64);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113}
1114
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001115static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk,
1116 sector_t lba)
1117{
1118 struct ppa_addr ppa;
1119
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001120 if (pblk->addrf_len < 32) {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001121 u32 *map = (u32 *)pblk->trans_map;
1122
1123 ppa = pblk_ppa32_to_ppa64(pblk, map[lba]);
1124 } else {
1125 struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map;
1126
1127 ppa = map[lba];
1128 }
1129
1130 return ppa;
1131}
1132
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001133static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba,
1134 struct ppa_addr ppa)
1135{
Javier Gonzálezbb845ae42018-03-30 00:05:19 +02001136 if (pblk->addrf_len < 32) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137 u32 *map = (u32 *)pblk->trans_map;
1138
1139 map[lba] = pblk_ppa64_to_ppa32(pblk, ppa);
1140 } else {
1141 u64 *map = (u64 *)pblk->trans_map;
1142
1143 map[lba] = ppa.ppa;
1144 }
1145}
1146
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001147static inline int pblk_ppa_empty(struct ppa_addr ppa_addr)
1148{
1149 return (ppa_addr.ppa == ADDR_EMPTY);
1150}
1151
1152static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr)
1153{
1154 ppa_addr->ppa = ADDR_EMPTY;
1155}
1156
Javier González076984662017-06-30 17:56:42 +02001157static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa)
1158{
Matias Bjørling8b7bc842018-01-05 14:16:21 +01001159 return (lppa.ppa == rppa.ppa);
Javier González076984662017-06-30 17:56:42 +02001160}
1161
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001162static inline int pblk_addr_in_cache(struct ppa_addr ppa)
1163{
1164 return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached);
1165}
1166
1167static inline int pblk_addr_to_cacheline(struct ppa_addr ppa)
1168{
1169 return ppa.c.line;
1170}
1171
1172static inline struct ppa_addr pblk_cacheline_to_addr(int addr)
1173{
1174 struct ppa_addr p;
1175
1176 p.c.line = addr;
1177 p.c.is_cached = 1;
1178
1179 return p;
1180}
1181
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001183 struct line_header *header)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001184{
1185 u32 crc = ~(u32)0;
1186
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001187 crc = crc32_le(crc, (unsigned char *)header + sizeof(crc),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001188 sizeof(struct line_header) - sizeof(crc));
1189
1190 return crc;
1191}
1192
1193static inline u32 pblk_calc_smeta_crc(struct pblk *pblk,
1194 struct line_smeta *smeta)
1195{
1196 struct pblk_line_meta *lm = &pblk->lm;
1197 u32 crc = ~(u32)0;
1198
1199 crc = crc32_le(crc, (unsigned char *)smeta +
1200 sizeof(struct line_header) + sizeof(crc),
1201 lm->smeta_len -
1202 sizeof(struct line_header) - sizeof(crc));
1203
1204 return crc;
1205}
1206
1207static inline u32 pblk_calc_emeta_crc(struct pblk *pblk,
1208 struct line_emeta *emeta)
1209{
1210 struct pblk_line_meta *lm = &pblk->lm;
1211 u32 crc = ~(u32)0;
1212
1213 crc = crc32_le(crc, (unsigned char *)emeta +
1214 sizeof(struct line_header) + sizeof(crc),
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001215 lm->emeta_len[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001216 sizeof(struct line_header) - sizeof(crc));
1217
1218 return crc;
1219}
1220
Javier Gonzálezf9c10152017-06-26 11:57:20 +02001221static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs)
1222{
1223 return !(nr_secs % pblk->min_write_pgs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001224}
1225
Matias Bjørling880eda52018-07-13 10:48:37 +02001226#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001227static inline void print_ppa(struct pblk *pblk, struct ppa_addr *p,
Javier González3b2a3ad2018-03-30 00:05:21 +02001228 char *msg, int error)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001229{
Matias Bjørling4e495a42018-07-13 10:48:42 +02001230 struct nvm_geo *geo = &pblk->dev->geo;
1231
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001232 if (p->c.is_cached) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001233 pblk_err(pblk, "ppa: (%s: %x) cache line: %llu\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234 msg, error, (u64)p->c.line);
Javier González3b2a3ad2018-03-30 00:05:21 +02001235 } else if (geo->version == NVM_OCSSD_SPEC_12) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001236 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237 msg, error,
1238 p->g.ch, p->g.lun, p->g.blk,
1239 p->g.pg, p->g.pl, p->g.sec);
Javier González3b2a3ad2018-03-30 00:05:21 +02001240 } else {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001241 pblk_err(pblk, "ppa: (%s: %x):ch:%d,lun:%d,chk:%d,sec:%d\n",
Javier González3b2a3ad2018-03-30 00:05:21 +02001242 msg, error,
1243 p->m.grp, p->m.pu, p->m.chk, p->m.sec);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244 }
1245}
1246
1247static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd,
1248 int error)
1249{
1250 int bit = -1;
1251
1252 if (rqd->nr_ppas == 1) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001253 print_ppa(pblk, &rqd->ppa_addr, "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001254 return;
1255 }
1256
1257 while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas,
1258 bit + 1)) < rqd->nr_ppas) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001259 print_ppa(pblk, &rqd->ppa_list[bit], "rqd", error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001260 }
1261
Matias Bjørling4e495a42018-07-13 10:48:42 +02001262 pblk_err(pblk, "error:%d, ppa_status:%llx\n", error, rqd->ppa_status);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001263}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001264
1265static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev,
1266 struct ppa_addr *ppas, int nr_ppas)
1267{
1268 struct nvm_geo *geo = &tgt_dev->geo;
1269 struct ppa_addr *ppa;
1270 int i;
1271
1272 for (i = 0; i < nr_ppas; i++) {
1273 ppa = &ppas[i];
1274
Javier González3b2a3ad2018-03-30 00:05:21 +02001275 if (geo->version == NVM_OCSSD_SPEC_12) {
1276 if (!ppa->c.is_cached &&
1277 ppa->g.ch < geo->num_ch &&
1278 ppa->g.lun < geo->num_lun &&
1279 ppa->g.pl < geo->num_pln &&
1280 ppa->g.blk < geo->num_chk &&
1281 ppa->g.pg < geo->num_pg &&
1282 ppa->g.sec < geo->ws_min)
1283 continue;
1284 } else {
1285 if (!ppa->c.is_cached &&
1286 ppa->m.grp < geo->num_ch &&
1287 ppa->m.pu < geo->num_lun &&
1288 ppa->m.chk < geo->num_chk &&
1289 ppa->m.sec < geo->clba)
1290 continue;
1291 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001292
Matias Bjørling4e495a42018-07-13 10:48:42 +02001293 print_ppa(tgt_dev->q->queuedata, ppa, "boundary", i);
Javier González1a94b2d2017-10-13 14:46:47 +02001294
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001295 return 1;
1296 }
1297 return 0;
1298}
1299
Javier González1a94b2d2017-10-13 14:46:47 +02001300static inline int pblk_check_io(struct pblk *pblk, struct nvm_rq *rqd)
1301{
1302 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmbergd68a9342018-10-09 13:11:46 +02001303 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier González1a94b2d2017-10-13 14:46:47 +02001304
1305 if (pblk_boundary_ppa_checks(dev, ppa_list, rqd->nr_ppas)) {
1306 WARN_ON(1);
1307 return -EINVAL;
1308 }
1309
1310 if (rqd->opcode == NVM_OP_PWRITE) {
1311 struct pblk_line *line;
Javier González1a94b2d2017-10-13 14:46:47 +02001312 int i;
1313
1314 for (i = 0; i < rqd->nr_ppas; i++) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02001315 line = pblk_ppa_to_line(pblk, ppa_list[i]);
Javier González1a94b2d2017-10-13 14:46:47 +02001316
1317 spin_lock(&line->lock);
1318 if (line->state != PBLK_LINESTATE_OPEN) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001319 pblk_err(pblk, "bad ppa: line:%d,state:%d\n",
Javier González1a94b2d2017-10-13 14:46:47 +02001320 line->id, line->state);
1321 WARN_ON(1);
1322 spin_unlock(&line->lock);
1323 return -EINVAL;
1324 }
1325 spin_unlock(&line->lock);
1326 }
1327 }
1328
1329 return 0;
1330}
1331#endif
1332
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001333static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr)
1334{
1335 struct pblk_line_meta *lm = &pblk->lm;
1336
1337 if (paddr > lm->sec_per_line)
1338 return 1;
1339
1340 return 0;
1341}
1342
1343static inline unsigned int pblk_get_bi_idx(struct bio *bio)
1344{
1345 return bio->bi_iter.bi_idx;
1346}
1347
1348static inline sector_t pblk_get_lba(struct bio *bio)
1349{
1350 return bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
1351}
1352
1353static inline unsigned int pblk_get_secs(struct bio *bio)
1354{
1355 return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE;
1356}
1357
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001358static inline void pblk_setup_uuid(struct pblk *pblk)
1359{
1360 uuid_le uuid;
1361
1362 uuid_le_gen(&uuid);
1363 memcpy(pblk->instance_uuid, uuid.b, 16);
1364}
Hans Holmberg4c44abf2018-10-09 13:11:52 +02001365
1366static inline char *pblk_disk_name(struct pblk *pblk)
1367{
1368 struct gendisk *disk = pblk->disk;
1369
1370 return disk->disk_name;
1371}
Hans Holmberg3bcebc52018-12-11 20:16:13 +01001372
1373static inline unsigned int pblk_get_min_chks(struct pblk *pblk)
1374{
1375 struct pblk_line_meta *lm = &pblk->lm;
1376 /* In a worst-case scenario every line will have OP invalid sectors.
1377 * We will then need a minimum of 1/OP lines to free up a single line
1378 */
1379
1380 return DIV_ROUND_UP(100, pblk->op) * lm->blk_per_line;
1381
1382}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001383#endif /* PBLK_H_ */