Commit | Line | Data |
---|---|---|
a4bd217b JG |
1 | /* |
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 | ||
ef576494 JG |
43 | #define PBLK_NR_CLOSE_JOBS (4) |
44 | ||
a4bd217b JG |
45 | #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 | ||
54 | #define pblk_for_each_lun(pblk, rlun, i) \ | |
55 | for ((i) = 0, rlun = &(pblk)->luns[0]; \ | |
56 | (i) < (pblk)->nr_luns; (i)++, rlun = &(pblk)->luns[(i)]) | |
57 | ||
0d880398 | 58 | /* Static pool sizes */ |
b84ae4a8 JG |
59 | #define PBLK_GEN_WS_POOL_SIZE (2) |
60 | ||
e2cddf20 JG |
61 | enum { |
62 | PBLK_READ = READ, | |
63 | PBLK_WRITE = WRITE,/* Write from write buffer */ | |
64 | PBLK_WRITE_INT, /* Internal write - no write buffer */ | |
65 | PBLK_ERASE, | |
66 | }; | |
67 | ||
a4bd217b JG |
68 | enum { |
69 | /* IO Types */ | |
70 | PBLK_IOTYPE_USER = 1 << 0, | |
71 | PBLK_IOTYPE_GC = 1 << 1, | |
72 | ||
73 | /* Write buffer flags */ | |
74 | PBLK_FLUSH_ENTRY = 1 << 2, | |
75 | PBLK_WRITTEN_DATA = 1 << 3, | |
76 | PBLK_SUBMITTED_ENTRY = 1 << 4, | |
77 | PBLK_WRITABLE_ENTRY = 1 << 5, | |
78 | }; | |
79 | ||
80 | enum { | |
81 | PBLK_BLK_ST_OPEN = 0x1, | |
82 | PBLK_BLK_ST_CLOSED = 0x2, | |
83 | }; | |
84 | ||
b20ba1bc JG |
85 | struct pblk_sec_meta { |
86 | u64 reserved; | |
87 | __le64 lba; | |
88 | }; | |
89 | ||
a4bd217b JG |
90 | /* The number of GC lists and the rate-limiter states go together. This way the |
91 | * rate-limiter can dictate how much GC is needed based on resource utilization. | |
92 | */ | |
b20ba1bc | 93 | #define PBLK_GC_NR_LISTS 3 |
a4bd217b JG |
94 | |
95 | enum { | |
96 | PBLK_RL_HIGH = 1, | |
97 | PBLK_RL_MID = 2, | |
98 | PBLK_RL_LOW = 3, | |
99 | }; | |
100 | ||
a4bd217b | 101 | #define pblk_dma_meta_size (sizeof(struct pblk_sec_meta) * PBLK_MAX_REQ_ADDRS) |
a4809fee | 102 | #define pblk_dma_ppa_size (sizeof(u64) * PBLK_MAX_REQ_ADDRS) |
a4bd217b | 103 | |
084ec9ba | 104 | /* write buffer completion context */ |
a4bd217b JG |
105 | struct pblk_c_ctx { |
106 | struct list_head list; /* Head for out-of-order completion */ | |
107 | ||
108 | unsigned long *lun_bitmap; /* Luns used on current request */ | |
109 | unsigned int sentry; | |
110 | unsigned int nr_valid; | |
111 | unsigned int nr_padded; | |
112 | }; | |
113 | ||
a4809fee | 114 | /* read context */ |
084ec9ba JG |
115 | struct pblk_g_ctx { |
116 | void *private; | |
a4809fee | 117 | u64 lba; |
a4bd217b JG |
118 | }; |
119 | ||
ee8d5c1a JG |
120 | /* Pad context */ |
121 | struct pblk_pad_rq { | |
122 | struct pblk *pblk; | |
123 | struct completion wait; | |
124 | struct kref ref; | |
125 | }; | |
126 | ||
a4bd217b JG |
127 | /* Recovery context */ |
128 | struct pblk_rec_ctx { | |
129 | struct pblk *pblk; | |
130 | struct nvm_rq *rqd; | |
131 | struct list_head failed; | |
132 | struct work_struct ws_rec; | |
133 | }; | |
134 | ||
135 | /* Write context */ | |
136 | struct pblk_w_ctx { | |
137 | struct bio_list bios; /* Original bios - used for completion | |
138 | * in REQ_FUA, REQ_FLUSH case | |
139 | */ | |
ef697902 | 140 | u64 lba; /* Logic addr. associated with entry */ |
a4bd217b JG |
141 | struct ppa_addr ppa; /* Physic addr. associated with entry */ |
142 | int flags; /* Write context flags */ | |
143 | }; | |
144 | ||
145 | struct pblk_rb_entry { | |
146 | struct ppa_addr cacheline; /* Cacheline for this entry */ | |
147 | void *data; /* Pointer to data on this entry */ | |
148 | struct pblk_w_ctx w_ctx; /* Context for this entry */ | |
149 | struct list_head index; /* List head to enable indexes */ | |
150 | }; | |
151 | ||
152 | #define EMPTY_ENTRY (~0U) | |
153 | ||
154 | struct pblk_rb_pages { | |
155 | struct page *pages; | |
156 | int order; | |
157 | struct list_head list; | |
158 | }; | |
159 | ||
160 | struct pblk_rb { | |
161 | struct pblk_rb_entry *entries; /* Ring buffer entries */ | |
162 | unsigned int mem; /* Write offset - points to next | |
163 | * writable entry in memory | |
164 | */ | |
165 | unsigned int subm; /* Read offset - points to last entry | |
166 | * that has been submitted to the media | |
167 | * to be persisted | |
168 | */ | |
169 | unsigned int sync; /* Synced - backpointer that signals | |
170 | * the last submitted entry that has | |
171 | * been successfully persisted to media | |
172 | */ | |
173 | unsigned int sync_point; /* Sync point - last entry that must be | |
174 | * flushed to the media. Used with | |
175 | * REQ_FLUSH and REQ_FUA | |
176 | */ | |
177 | unsigned int l2p_update; /* l2p update point - next entry for | |
178 | * which l2p mapping will be updated to | |
179 | * contain a device ppa address (instead | |
180 | * of a cacheline | |
181 | */ | |
182 | unsigned int nr_entries; /* Number of entries in write buffer - | |
183 | * must be a power of two | |
184 | */ | |
185 | unsigned int seg_size; /* Size of the data segments being | |
186 | * stored on each entry. Typically this | |
187 | * will be 4KB | |
188 | */ | |
189 | ||
190 | struct list_head pages; /* List of data pages */ | |
191 | ||
192 | spinlock_t w_lock; /* Write lock */ | |
193 | spinlock_t s_lock; /* Sync lock */ | |
194 | ||
195 | #ifdef CONFIG_NVM_DEBUG | |
196 | atomic_t inflight_sync_point; /* Not served REQ_FLUSH | REQ_FUA */ | |
197 | #endif | |
198 | }; | |
199 | ||
200 | #define PBLK_RECOVERY_SECTORS 16 | |
201 | ||
202 | struct pblk_lun { | |
203 | struct ppa_addr bppa; | |
204 | ||
205 | u8 *bb_list; /* Bad block list for LUN. Only used on | |
206 | * bring up. Bad blocks are managed | |
207 | * within lines on run-time. | |
208 | */ | |
209 | ||
210 | struct semaphore wr_sem; | |
211 | }; | |
212 | ||
213 | struct pblk_gc_rq { | |
214 | struct pblk_line *line; | |
215 | void *data; | |
d340121e | 216 | u64 paddr_list[PBLK_MAX_REQ_ADDRS]; |
b20ba1bc | 217 | u64 lba_list[PBLK_MAX_REQ_ADDRS]; |
a4bd217b JG |
218 | int nr_secs; |
219 | int secs_to_gc; | |
220 | struct list_head list; | |
221 | }; | |
222 | ||
223 | struct pblk_gc { | |
b20ba1bc JG |
224 | /* These states are not protected by a lock since (i) they are in the |
225 | * fast path, and (ii) they are not critical. | |
226 | */ | |
a4bd217b JG |
227 | int gc_active; |
228 | int gc_enabled; | |
229 | int gc_forced; | |
a4bd217b JG |
230 | |
231 | struct task_struct *gc_ts; | |
232 | struct task_struct *gc_writer_ts; | |
b20ba1bc JG |
233 | struct task_struct *gc_reader_ts; |
234 | ||
235 | struct workqueue_struct *gc_line_reader_wq; | |
a4bd217b | 236 | struct workqueue_struct *gc_reader_wq; |
b20ba1bc | 237 | |
a4bd217b JG |
238 | struct timer_list gc_timer; |
239 | ||
b20ba1bc JG |
240 | struct semaphore gc_sem; |
241 | atomic_t inflight_gc; | |
a4bd217b | 242 | int w_entries; |
b20ba1bc | 243 | |
a4bd217b | 244 | struct list_head w_list; |
b20ba1bc | 245 | struct list_head r_list; |
a4bd217b JG |
246 | |
247 | spinlock_t lock; | |
248 | spinlock_t w_lock; | |
b20ba1bc | 249 | spinlock_t r_lock; |
a4bd217b JG |
250 | }; |
251 | ||
252 | struct pblk_rl { | |
253 | unsigned int high; /* Upper threshold for rate limiter (free run - | |
254 | * user I/O rate limiter | |
255 | */ | |
256 | unsigned int low; /* Lower threshold for rate limiter (user I/O | |
257 | * rate limiter - stall) | |
258 | */ | |
259 | unsigned int high_pw; /* High rounded up as a power of 2 */ | |
260 | ||
b20ba1bc JG |
261 | #define PBLK_USER_HIGH_THRS 8 /* Begin write limit at 12% available blks */ |
262 | #define PBLK_USER_LOW_THRS 10 /* Aggressive GC at 10% available blocks */ | |
a4bd217b JG |
263 | |
264 | int rb_windows_pw; /* Number of rate windows in the write buffer | |
265 | * given as a power-of-2. This guarantees that | |
266 | * when user I/O is being rate limited, there | |
267 | * will be reserved enough space for the GC to | |
268 | * place its payload. A window is of | |
269 | * pblk->max_write_pgs size, which in NVMe is | |
270 | * 64, i.e., 256kb. | |
271 | */ | |
272 | int rb_budget; /* Total number of entries available for I/O */ | |
273 | int rb_user_max; /* Max buffer entries available for user I/O */ | |
a4bd217b JG |
274 | int rb_gc_max; /* Max buffer entries available for GC I/O */ |
275 | int rb_gc_rsv; /* Reserved buffer entries for GC I/O */ | |
276 | int rb_state; /* Rate-limiter current state */ | |
da67e68f | 277 | int rb_max_io; /* Maximum size for an I/O giving the config */ |
588726d3 JG |
278 | |
279 | atomic_t rb_user_cnt; /* User I/O buffer counter */ | |
a4bd217b | 280 | atomic_t rb_gc_cnt; /* GC I/O buffer counter */ |
588726d3 | 281 | atomic_t rb_space; /* Space limit in case of reaching capacity */ |
a4bd217b | 282 | |
b20ba1bc JG |
283 | int rsv_blocks; /* Reserved blocks for GC */ |
284 | ||
a4bd217b | 285 | int rb_user_active; |
b20ba1bc JG |
286 | int rb_gc_active; |
287 | ||
a4bd217b JG |
288 | struct timer_list u_timer; |
289 | ||
290 | unsigned long long nr_secs; | |
291 | unsigned long total_blocks; | |
292 | atomic_t free_blocks; | |
293 | }; | |
294 | ||
a4bd217b JG |
295 | #define PBLK_LINE_EMPTY (~0U) |
296 | ||
297 | enum { | |
298 | /* Line Types */ | |
299 | PBLK_LINETYPE_FREE = 0, | |
300 | PBLK_LINETYPE_LOG = 1, | |
301 | PBLK_LINETYPE_DATA = 2, | |
302 | ||
303 | /* Line state */ | |
304 | PBLK_LINESTATE_FREE = 10, | |
305 | PBLK_LINESTATE_OPEN = 11, | |
306 | PBLK_LINESTATE_CLOSED = 12, | |
307 | PBLK_LINESTATE_GC = 13, | |
308 | PBLK_LINESTATE_BAD = 14, | |
309 | PBLK_LINESTATE_CORRUPT = 15, | |
310 | ||
311 | /* GC group */ | |
312 | PBLK_LINEGC_NONE = 20, | |
313 | PBLK_LINEGC_EMPTY = 21, | |
314 | PBLK_LINEGC_LOW = 22, | |
315 | PBLK_LINEGC_MID = 23, | |
316 | PBLK_LINEGC_HIGH = 24, | |
317 | PBLK_LINEGC_FULL = 25, | |
318 | }; | |
319 | ||
320 | #define PBLK_MAGIC 0x70626c6b /*pblk*/ | |
c79819bc | 321 | #define SMETA_VERSION cpu_to_le16(1) |
a4bd217b JG |
322 | |
323 | struct line_header { | |
324 | __le32 crc; | |
325 | __le32 identifier; /* pblk identifier */ | |
326 | __u8 uuid[16]; /* instance uuid */ | |
327 | __le16 type; /* line type */ | |
328 | __le16 version; /* type version */ | |
329 | __le32 id; /* line id for current line */ | |
330 | }; | |
331 | ||
332 | struct line_smeta { | |
333 | struct line_header header; | |
334 | ||
335 | __le32 crc; /* Full structure including struct crc */ | |
336 | /* Previous line metadata */ | |
337 | __le32 prev_id; /* Line id for previous line */ | |
338 | ||
339 | /* Current line metadata */ | |
340 | __le64 seq_nr; /* Sequence number for current line */ | |
341 | ||
342 | /* Active writers */ | |
343 | __le32 window_wr_lun; /* Number of parallel LUNs to write */ | |
344 | ||
345 | __le32 rsvd[2]; | |
dd2a4343 JG |
346 | |
347 | __le64 lun_bitmap[]; | |
a4bd217b JG |
348 | }; |
349 | ||
350 | /* | |
dd2a4343 JG |
351 | * Metadata layout in media: |
352 | * First sector: | |
353 | * 1. struct line_emeta | |
354 | * 2. bad block bitmap (u64 * window_wr_lun) | |
355 | * Mid sectors (start at lbas_sector): | |
356 | * 3. nr_lbas (u64) forming lba list | |
357 | * Last sectors (start at vsc_sector): | |
358 | * 4. u32 valid sector count (vsc) for all lines (~0U: free line) | |
a4bd217b JG |
359 | */ |
360 | struct line_emeta { | |
361 | struct line_header header; | |
362 | ||
363 | __le32 crc; /* Full structure including struct crc */ | |
364 | ||
365 | /* Previous line metadata */ | |
366 | __le32 prev_id; /* Line id for prev line */ | |
367 | ||
368 | /* Current line metadata */ | |
369 | __le64 seq_nr; /* Sequence number for current line */ | |
370 | ||
371 | /* Active writers */ | |
372 | __le32 window_wr_lun; /* Number of parallel LUNs to write */ | |
373 | ||
374 | /* Bookkeeping for recovery */ | |
375 | __le32 next_id; /* Line id for next line */ | |
376 | __le64 nr_lbas; /* Number of lbas mapped in line */ | |
377 | __le64 nr_valid_lbas; /* Number of valid lbas mapped in line */ | |
dd2a4343 JG |
378 | __le64 bb_bitmap[]; /* Updated bad block bitmap for line */ |
379 | }; | |
380 | ||
381 | struct pblk_emeta { | |
382 | struct line_emeta *buf; /* emeta buffer in media format */ | |
383 | int mem; /* Write offset - points to next | |
384 | * writable entry in memory | |
385 | */ | |
386 | atomic_t sync; /* Synced - backpointer that signals the | |
387 | * last entry that has been successfully | |
388 | * persisted to media | |
389 | */ | |
390 | unsigned int nr_entries; /* Number of emeta entries */ | |
391 | }; | |
392 | ||
393 | struct pblk_smeta { | |
394 | struct line_smeta *buf; /* smeta buffer in persistent format */ | |
a4bd217b JG |
395 | }; |
396 | ||
397 | struct pblk_line { | |
398 | struct pblk *pblk; | |
399 | unsigned int id; /* Line number corresponds to the | |
400 | * block line | |
401 | */ | |
402 | unsigned int seq_nr; /* Unique line sequence number */ | |
403 | ||
404 | int state; /* PBLK_LINESTATE_X */ | |
405 | int type; /* PBLK_LINETYPE_X */ | |
406 | int gc_group; /* PBLK_LINEGC_X */ | |
407 | struct list_head list; /* Free, GC lists */ | |
408 | ||
409 | unsigned long *lun_bitmap; /* Bitmap for LUNs mapped in line */ | |
410 | ||
dd2a4343 JG |
411 | struct pblk_smeta *smeta; /* Start metadata */ |
412 | struct pblk_emeta *emeta; /* End medatada */ | |
413 | ||
a4bd217b | 414 | int meta_line; /* Metadata line id */ |
dd2a4343 JG |
415 | int meta_distance; /* Distance between data and metadata */ |
416 | ||
a4bd217b JG |
417 | u64 smeta_ssec; /* Sector where smeta starts */ |
418 | u64 emeta_ssec; /* Sector where emeta starts */ | |
419 | ||
420 | unsigned int sec_in_line; /* Number of usable secs in line */ | |
421 | ||
a44f53fa | 422 | atomic_t blk_in_line; /* Number of good blocks in line */ |
a4bd217b JG |
423 | unsigned long *blk_bitmap; /* Bitmap for valid/invalid blocks */ |
424 | unsigned long *erase_bitmap; /* Bitmap for erased blocks */ | |
425 | ||
426 | unsigned long *map_bitmap; /* Bitmap for mapped sectors in line */ | |
427 | unsigned long *invalid_bitmap; /* Bitmap for invalid sectors in line */ | |
428 | ||
a44f53fa | 429 | atomic_t left_eblks; /* Blocks left for erasing */ |
a4bd217b JG |
430 | atomic_t left_seblks; /* Blocks left for sync erasing */ |
431 | ||
432 | int left_msecs; /* Sectors left for mapping */ | |
a4bd217b | 433 | unsigned int cur_sec; /* Sector map pointer */ |
dd2a4343 JG |
434 | unsigned int nr_valid_lbas; /* Number of valid lbas in line */ |
435 | ||
436 | __le32 *vsc; /* Valid sector count in line */ | |
a4bd217b JG |
437 | |
438 | struct kref ref; /* Write buffer L2P references */ | |
439 | ||
440 | spinlock_t lock; /* Necessary for invalid_bitmap only */ | |
441 | }; | |
442 | ||
a44f53fa | 443 | #define PBLK_DATA_LINES 4 |
a4bd217b | 444 | |
dd2a4343 | 445 | enum { |
a4bd217b JG |
446 | PBLK_KMALLOC_META = 1, |
447 | PBLK_VMALLOC_META = 2, | |
448 | }; | |
449 | ||
dd2a4343 JG |
450 | enum { |
451 | PBLK_EMETA_TYPE_HEADER = 1, /* struct line_emeta first sector */ | |
452 | PBLK_EMETA_TYPE_LLBA = 2, /* lba list - type: __le64 */ | |
453 | PBLK_EMETA_TYPE_VSC = 3, /* vsc list - type: __le32 */ | |
a4bd217b JG |
454 | }; |
455 | ||
456 | struct pblk_line_mgmt { | |
457 | int nr_lines; /* Total number of full lines */ | |
458 | int nr_free_lines; /* Number of full lines in free list */ | |
459 | ||
460 | /* Free lists - use free_lock */ | |
461 | struct list_head free_list; /* Full lines ready to use */ | |
462 | struct list_head corrupt_list; /* Full lines corrupted */ | |
463 | struct list_head bad_list; /* Full lines bad */ | |
464 | ||
465 | /* GC lists - use gc_lock */ | |
b20ba1bc | 466 | struct list_head *gc_lists[PBLK_GC_NR_LISTS]; |
a4bd217b JG |
467 | struct list_head gc_high_list; /* Full lines ready to GC, high isc */ |
468 | struct list_head gc_mid_list; /* Full lines ready to GC, mid isc */ | |
469 | struct list_head gc_low_list; /* Full lines ready to GC, low isc */ | |
470 | ||
471 | struct list_head gc_full_list; /* Full lines ready to GC, no valid */ | |
472 | struct list_head gc_empty_list; /* Full lines close, all valid */ | |
473 | ||
474 | struct pblk_line *log_line; /* Current FTL log line */ | |
475 | struct pblk_line *data_line; /* Current data line */ | |
476 | struct pblk_line *log_next; /* Next FTL log line */ | |
477 | struct pblk_line *data_next; /* Next data line */ | |
478 | ||
dd2a4343 JG |
479 | struct list_head emeta_list; /* Lines queued to schedule emeta */ |
480 | ||
481 | __le32 *vsc_list; /* Valid sector counts for all lines */ | |
482 | ||
a4bd217b | 483 | /* Metadata allocation type: VMALLOC | KMALLOC */ |
a4bd217b JG |
484 | int emeta_alloc_type; |
485 | ||
486 | /* Pre-allocated metadata for data lines */ | |
dd2a4343 JG |
487 | struct pblk_smeta *sline_meta[PBLK_DATA_LINES]; |
488 | struct pblk_emeta *eline_meta[PBLK_DATA_LINES]; | |
a4bd217b JG |
489 | unsigned long meta_bitmap; |
490 | ||
491 | /* Helpers for fast bitmap calculations */ | |
492 | unsigned long *bb_template; | |
493 | unsigned long *bb_aux; | |
494 | ||
495 | unsigned long d_seq_nr; /* Data line unique sequence number */ | |
496 | unsigned long l_seq_nr; /* Log line unique sequence number */ | |
497 | ||
498 | spinlock_t free_lock; | |
dd2a4343 | 499 | spinlock_t close_lock; |
a4bd217b JG |
500 | spinlock_t gc_lock; |
501 | }; | |
502 | ||
503 | struct pblk_line_meta { | |
504 | unsigned int smeta_len; /* Total length for smeta */ | |
dd2a4343 JG |
505 | unsigned int smeta_sec; /* Sectors needed for smeta */ |
506 | ||
507 | unsigned int emeta_len[4]; /* Lengths for emeta: | |
508 | * [0]: Total length | |
509 | * [1]: struct line_emeta length | |
510 | * [2]: L2P portion length | |
511 | * [3]: vsc list length | |
512 | */ | |
513 | unsigned int emeta_sec[4]; /* Sectors needed for emeta. Same layout | |
514 | * as emeta_len | |
515 | */ | |
516 | ||
a4bd217b | 517 | unsigned int emeta_bb; /* Boundary for bb that affects emeta */ |
dd2a4343 JG |
518 | |
519 | unsigned int vsc_list_len; /* Length for vsc list */ | |
a4bd217b JG |
520 | unsigned int sec_bitmap_len; /* Length for sector bitmap in line */ |
521 | unsigned int blk_bitmap_len; /* Length for block bitmap in line */ | |
522 | unsigned int lun_bitmap_len; /* Length for lun bitmap in line */ | |
523 | ||
524 | unsigned int blk_per_line; /* Number of blocks in a full line */ | |
525 | unsigned int sec_per_line; /* Number of sectors in a line */ | |
dd2a4343 | 526 | unsigned int dsec_per_line; /* Number of data sectors in a line */ |
a4bd217b JG |
527 | unsigned int min_blk_line; /* Min. number of good blocks in line */ |
528 | ||
529 | unsigned int mid_thrs; /* Threshold for GC mid list */ | |
530 | unsigned int high_thrs; /* Threshold for GC high list */ | |
dd2a4343 JG |
531 | |
532 | unsigned int meta_distance; /* Distance between data and metadata */ | |
a4bd217b JG |
533 | }; |
534 | ||
535 | struct pblk_addr_format { | |
536 | u64 ch_mask; | |
537 | u64 lun_mask; | |
538 | u64 pln_mask; | |
539 | u64 blk_mask; | |
540 | u64 pg_mask; | |
541 | u64 sec_mask; | |
542 | u8 ch_offset; | |
543 | u8 lun_offset; | |
544 | u8 pln_offset; | |
545 | u8 blk_offset; | |
546 | u8 pg_offset; | |
547 | u8 sec_offset; | |
548 | }; | |
549 | ||
588726d3 JG |
550 | enum { |
551 | PBLK_STATE_RUNNING = 0, | |
552 | PBLK_STATE_STOPPING = 1, | |
553 | PBLK_STATE_RECOVERING = 2, | |
554 | PBLK_STATE_STOPPED = 3, | |
555 | }; | |
556 | ||
a4bd217b JG |
557 | struct pblk { |
558 | struct nvm_tgt_dev *dev; | |
559 | struct gendisk *disk; | |
560 | ||
561 | struct kobject kobj; | |
562 | ||
563 | struct pblk_lun *luns; | |
564 | ||
565 | struct pblk_line *lines; /* Line array */ | |
566 | struct pblk_line_mgmt l_mg; /* Line management */ | |
567 | struct pblk_line_meta lm; /* Line metadata */ | |
568 | ||
569 | int ppaf_bitsize; | |
570 | struct pblk_addr_format ppaf; | |
571 | ||
572 | struct pblk_rb rwb; | |
573 | ||
588726d3 JG |
574 | int state; /* pblk line state */ |
575 | ||
a4bd217b JG |
576 | int min_write_pgs; /* Minimum amount of pages required by controller */ |
577 | int max_write_pgs; /* Maximum amount of pages supported by controller */ | |
578 | int pgs_in_buffer; /* Number of pages that need to be held in buffer to | |
579 | * guarantee successful reads. | |
580 | */ | |
581 | ||
582 | sector_t capacity; /* Device capacity when bad blocks are subtracted */ | |
583 | int over_pct; /* Percentage of device used for over-provisioning */ | |
584 | ||
585 | /* pblk provisioning values. Used by rate limiter */ | |
586 | struct pblk_rl rl; | |
587 | ||
c2e9f5d4 | 588 | int sec_per_write; |
a4bd217b JG |
589 | |
590 | unsigned char instance_uuid[16]; | |
591 | #ifdef CONFIG_NVM_DEBUG | |
592 | /* All debug counters apply to 4kb sector I/Os */ | |
593 | atomic_long_t inflight_writes; /* Inflight writes (user and gc) */ | |
594 | atomic_long_t padded_writes; /* Sectors padded due to flush/fua */ | |
595 | atomic_long_t padded_wb; /* Sectors padded in write buffer */ | |
596 | atomic_long_t nr_flush; /* Number of flush/fua I/O */ | |
597 | atomic_long_t req_writes; /* Sectors stored on write buffer */ | |
598 | atomic_long_t sub_writes; /* Sectors submitted from buffer */ | |
599 | atomic_long_t sync_writes; /* Sectors synced to media */ | |
a4bd217b | 600 | atomic_long_t inflight_reads; /* Inflight sector read requests */ |
db7ada33 | 601 | atomic_long_t cache_reads; /* Read requests that hit the cache */ |
a4bd217b JG |
602 | atomic_long_t sync_reads; /* Completed sector read requests */ |
603 | atomic_long_t recov_writes; /* Sectors submitted from recovery */ | |
604 | atomic_long_t recov_gc_writes; /* Sectors submitted from write GC */ | |
605 | atomic_long_t recov_gc_reads; /* Sectors submitted from read GC */ | |
606 | #endif | |
607 | ||
608 | spinlock_t lock; | |
609 | ||
610 | atomic_long_t read_failed; | |
611 | atomic_long_t read_empty; | |
612 | atomic_long_t read_high_ecc; | |
613 | atomic_long_t read_failed_gc; | |
614 | atomic_long_t write_failed; | |
615 | atomic_long_t erase_failed; | |
616 | ||
588726d3 JG |
617 | atomic_t inflight_io; /* General inflight I/O counter */ |
618 | ||
a4bd217b JG |
619 | struct task_struct *writer_ts; |
620 | ||
621 | /* Simple translation map of logical addresses to physical addresses. | |
622 | * The logical addresses is known by the host system, while the physical | |
623 | * addresses are used when writing to the disk block device. | |
624 | */ | |
625 | unsigned char *trans_map; | |
626 | spinlock_t trans_lock; | |
627 | ||
628 | struct list_head compl_list; | |
629 | ||
bd432417 | 630 | mempool_t *page_bio_pool; |
b84ae4a8 | 631 | mempool_t *gen_ws_pool; |
a4bd217b | 632 | mempool_t *rec_pool; |
0d880398 | 633 | mempool_t *r_rq_pool; |
a4bd217b | 634 | mempool_t *w_rq_pool; |
0d880398 | 635 | mempool_t *e_rq_pool; |
a4bd217b | 636 | |
ef576494 JG |
637 | struct workqueue_struct *close_wq; |
638 | struct workqueue_struct *bb_wq; | |
7bd4d370 | 639 | struct workqueue_struct *r_end_wq; |
ef576494 | 640 | |
a4bd217b JG |
641 | struct timer_list wtimer; |
642 | ||
643 | struct pblk_gc gc; | |
644 | }; | |
645 | ||
646 | struct pblk_line_ws { | |
647 | struct pblk *pblk; | |
648 | struct pblk_line *line; | |
649 | void *priv; | |
650 | struct work_struct ws; | |
651 | }; | |
652 | ||
084ec9ba | 653 | #define pblk_g_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_g_ctx)) |
a4bd217b JG |
654 | #define pblk_w_rq_size (sizeof(struct nvm_rq) + sizeof(struct pblk_c_ctx)) |
655 | ||
656 | /* | |
657 | * pblk ring buffer operations | |
658 | */ | |
659 | int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base, | |
660 | unsigned int power_size, unsigned int power_seg_sz); | |
661 | unsigned int pblk_rb_calculate_size(unsigned int nr_entries); | |
662 | void *pblk_rb_entries_ref(struct pblk_rb *rb); | |
663 | int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio, | |
664 | unsigned int nr_entries, unsigned int *pos); | |
665 | int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries, | |
666 | unsigned int *pos); | |
667 | void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data, | |
668 | struct pblk_w_ctx w_ctx, unsigned int pos); | |
669 | void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data, | |
d340121e JG |
670 | struct pblk_w_ctx w_ctx, struct pblk_line *line, |
671 | u64 paddr, unsigned int pos); | |
a4bd217b | 672 | struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos); |
588726d3 | 673 | void pblk_rb_flush(struct pblk_rb *rb); |
a4bd217b JG |
674 | |
675 | void pblk_rb_sync_l2p(struct pblk_rb *rb); | |
d624f371 | 676 | unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd, |
875d94f3 JG |
677 | unsigned int pos, unsigned int nr_entries, |
678 | unsigned int count); | |
a4bd217b JG |
679 | unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio, |
680 | struct list_head *list, | |
681 | unsigned int max); | |
682 | int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba, | |
75cb8e93 | 683 | struct ppa_addr ppa, int bio_iter, bool advanced_bio); |
a4bd217b JG |
684 | unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int entries); |
685 | ||
686 | unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags); | |
687 | unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries); | |
688 | struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb, | |
689 | struct ppa_addr *ppa); | |
690 | void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags); | |
691 | unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb); | |
692 | ||
693 | unsigned int pblk_rb_read_count(struct pblk_rb *rb); | |
ee8d5c1a | 694 | unsigned int pblk_rb_sync_count(struct pblk_rb *rb); |
a4bd217b JG |
695 | unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos); |
696 | ||
697 | int pblk_rb_tear_down_check(struct pblk_rb *rb); | |
698 | int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos); | |
699 | void pblk_rb_data_free(struct pblk_rb *rb); | |
700 | ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf); | |
701 | ||
702 | /* | |
703 | * pblk core | |
704 | */ | |
67bf26a3 JG |
705 | struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type); |
706 | void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type); | |
c2e9f5d4 | 707 | void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write); |
a4bd217b JG |
708 | int pblk_setup_w_rec_rq(struct pblk *pblk, struct nvm_rq *rqd, |
709 | struct pblk_c_ctx *c_ctx); | |
588726d3 | 710 | void pblk_wait_for_meta(struct pblk *pblk); |
a4bd217b JG |
711 | struct ppa_addr pblk_get_lba_map(struct pblk *pblk, sector_t lba); |
712 | void pblk_discard(struct pblk *pblk, struct bio *bio); | |
713 | void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd); | |
714 | void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd); | |
715 | int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd); | |
dd2a4343 | 716 | int pblk_submit_meta_io(struct pblk *pblk, struct pblk_line *meta_line); |
a4bd217b JG |
717 | struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data, |
718 | unsigned int nr_secs, unsigned int len, | |
de54e703 | 719 | int alloc_type, gfp_t gfp_mask); |
a4bd217b JG |
720 | struct pblk_line *pblk_line_get(struct pblk *pblk); |
721 | struct pblk_line *pblk_line_get_first_data(struct pblk *pblk); | |
21d22871 | 722 | struct pblk_line *pblk_line_replace_data(struct pblk *pblk); |
a4bd217b JG |
723 | int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line); |
724 | void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line); | |
725 | struct pblk_line *pblk_line_get_data(struct pblk *pblk); | |
d624f371 | 726 | struct pblk_line *pblk_line_get_erase(struct pblk *pblk); |
a4bd217b JG |
727 | int pblk_line_erase(struct pblk *pblk, struct pblk_line *line); |
728 | int pblk_line_is_full(struct pblk_line *line); | |
729 | void pblk_line_free(struct pblk *pblk, struct pblk_line *line); | |
dd2a4343 | 730 | void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line); |
a4bd217b | 731 | void pblk_line_close(struct pblk *pblk, struct pblk_line *line); |
588726d3 | 732 | void pblk_line_close_meta_sync(struct pblk *pblk); |
dd2a4343 | 733 | void pblk_line_close_ws(struct work_struct *work); |
588726d3 | 734 | void pblk_pipeline_stop(struct pblk *pblk); |
a4bd217b | 735 | void pblk_line_mark_bb(struct work_struct *work); |
b84ae4a8 JG |
736 | void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv, |
737 | void (*work)(struct work_struct *), gfp_t gfp_mask, | |
738 | struct workqueue_struct *wq); | |
a4bd217b JG |
739 | u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line); |
740 | int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line); | |
dd2a4343 JG |
741 | int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line, |
742 | void *emeta_buf); | |
a4bd217b JG |
743 | int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr erase_ppa); |
744 | void pblk_line_put(struct kref *ref); | |
7bd4d370 | 745 | void pblk_line_put_wq(struct kref *ref); |
a4bd217b | 746 | struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line); |
dd2a4343 JG |
747 | u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line); |
748 | void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); | |
a4bd217b | 749 | u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); |
dd2a4343 | 750 | u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs); |
a4bd217b JG |
751 | int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail, |
752 | unsigned long secs_to_flush); | |
3eaa11e2 | 753 | void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas); |
a4bd217b JG |
754 | void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
755 | unsigned long *lun_bitmap); | |
3eaa11e2 | 756 | void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas); |
a4bd217b JG |
757 | void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
758 | unsigned long *lun_bitmap); | |
759 | void pblk_end_bio_sync(struct bio *bio); | |
760 | void pblk_end_io_sync(struct nvm_rq *rqd); | |
761 | int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags, | |
762 | int nr_pages); | |
a4bd217b JG |
763 | void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off, |
764 | int nr_pages); | |
765 | void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa); | |
0880a9aa JG |
766 | void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line, |
767 | u64 paddr); | |
a4bd217b JG |
768 | void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa); |
769 | void pblk_update_map_cache(struct pblk *pblk, sector_t lba, | |
770 | struct ppa_addr ppa); | |
771 | void pblk_update_map_dev(struct pblk *pblk, sector_t lba, | |
772 | struct ppa_addr ppa, struct ppa_addr entry_line); | |
773 | int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa, | |
d340121e | 774 | struct pblk_line *gc_line, u64 paddr); |
a4bd217b JG |
775 | void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas, |
776 | u64 *lba_list, int nr_secs); | |
777 | void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas, | |
778 | sector_t blba, int nr_secs); | |
779 | ||
780 | /* | |
781 | * pblk user I/O write path | |
782 | */ | |
783 | int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, | |
784 | unsigned long flags); | |
d340121e | 785 | int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq); |
a4bd217b JG |
786 | |
787 | /* | |
788 | * pblk map | |
789 | */ | |
790 | void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd, | |
791 | unsigned int sentry, unsigned long *lun_bitmap, | |
792 | unsigned int valid_secs, struct ppa_addr *erase_ppa); | |
793 | void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry, | |
794 | unsigned long *lun_bitmap, unsigned int valid_secs, | |
795 | unsigned int off); | |
796 | ||
797 | /* | |
798 | * pblk write thread | |
799 | */ | |
800 | int pblk_write_ts(void *data); | |
801 | void pblk_write_timer_fn(unsigned long data); | |
802 | void pblk_write_should_kick(struct pblk *pblk); | |
803 | ||
804 | /* | |
805 | * pblk read path | |
806 | */ | |
b25d5237 | 807 | extern struct bio_set *pblk_bio_set; |
a4bd217b | 808 | int pblk_submit_read(struct pblk *pblk, struct bio *bio); |
d340121e | 809 | int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq); |
a4bd217b JG |
810 | /* |
811 | * pblk recovery | |
812 | */ | |
813 | void pblk_submit_rec(struct work_struct *work); | |
814 | struct pblk_line *pblk_recov_l2p(struct pblk *pblk); | |
588726d3 | 815 | int pblk_recov_pad(struct pblk *pblk); |
a4bd217b JG |
816 | __le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta); |
817 | int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx, | |
818 | struct pblk_rec_ctx *recovery, u64 *comp_bits, | |
819 | unsigned int comp); | |
820 | ||
821 | /* | |
822 | * pblk gc | |
823 | */ | |
b20ba1bc | 824 | #define PBLK_GC_MAX_READERS 8 /* Max number of outstanding GC reader jobs */ |
3627896a | 825 | #define PBLK_GC_RQ_QD 128 /* Queue depth for inflight GC requests */ |
b20ba1bc JG |
826 | #define PBLK_GC_L_QD 4 /* Queue depth for inflight GC lines */ |
827 | #define PBLK_GC_RSV_LINE 1 /* Reserved lines for GC */ | |
a4bd217b JG |
828 | |
829 | int pblk_gc_init(struct pblk *pblk); | |
830 | void pblk_gc_exit(struct pblk *pblk); | |
831 | void pblk_gc_should_start(struct pblk *pblk); | |
832 | void pblk_gc_should_stop(struct pblk *pblk); | |
03661b5f | 833 | void pblk_gc_should_kick(struct pblk *pblk); |
b20ba1bc | 834 | void pblk_gc_kick(struct pblk *pblk); |
37ce33d5 | 835 | void pblk_gc_free_full_lines(struct pblk *pblk); |
a4bd217b JG |
836 | void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled, |
837 | int *gc_active); | |
b20ba1bc | 838 | int pblk_gc_sysfs_force(struct pblk *pblk, int force); |
a4bd217b JG |
839 | |
840 | /* | |
841 | * pblk rate limiter | |
842 | */ | |
843 | void pblk_rl_init(struct pblk_rl *rl, int budget); | |
844 | void pblk_rl_free(struct pblk_rl *rl); | |
03661b5f | 845 | void pblk_rl_update_rates(struct pblk_rl *rl); |
b20ba1bc JG |
846 | int pblk_rl_high_thrs(struct pblk_rl *rl); |
847 | int pblk_rl_low_thrs(struct pblk_rl *rl); | |
a4bd217b JG |
848 | unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl); |
849 | int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries); | |
588726d3 | 850 | void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries); |
a4bd217b JG |
851 | void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries); |
852 | int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries); | |
853 | void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries); | |
854 | void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc); | |
a4bd217b | 855 | int pblk_rl_sysfs_rate_show(struct pblk_rl *rl); |
da67e68f | 856 | int pblk_rl_max_io(struct pblk_rl *rl); |
a4bd217b JG |
857 | void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line); |
858 | void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line); | |
588726d3 JG |
859 | void pblk_rl_set_space_limit(struct pblk_rl *rl, int entries_left); |
860 | int pblk_rl_is_limit(struct pblk_rl *rl); | |
a4bd217b JG |
861 | |
862 | /* | |
863 | * pblk sysfs | |
864 | */ | |
865 | int pblk_sysfs_init(struct gendisk *tdisk); | |
866 | void pblk_sysfs_exit(struct gendisk *tdisk); | |
867 | ||
868 | static inline void *pblk_malloc(size_t size, int type, gfp_t flags) | |
869 | { | |
870 | if (type == PBLK_KMALLOC_META) | |
871 | return kmalloc(size, flags); | |
872 | return vmalloc(size); | |
873 | } | |
874 | ||
875 | static inline void pblk_mfree(void *ptr, int type) | |
876 | { | |
877 | if (type == PBLK_KMALLOC_META) | |
878 | kfree(ptr); | |
879 | else | |
880 | vfree(ptr); | |
881 | } | |
882 | ||
883 | static inline struct nvm_rq *nvm_rq_from_c_ctx(void *c_ctx) | |
884 | { | |
885 | return c_ctx - sizeof(struct nvm_rq); | |
886 | } | |
887 | ||
dd2a4343 JG |
888 | static inline void *emeta_to_bb(struct line_emeta *emeta) |
889 | { | |
890 | return emeta->bb_bitmap; | |
891 | } | |
892 | ||
893 | static inline void *emeta_to_lbas(struct pblk *pblk, struct line_emeta *emeta) | |
894 | { | |
895 | return ((void *)emeta + pblk->lm.emeta_len[1]); | |
896 | } | |
897 | ||
898 | static inline void *emeta_to_vsc(struct pblk *pblk, struct line_emeta *emeta) | |
a4bd217b | 899 | { |
dd2a4343 | 900 | return (emeta_to_lbas(pblk, emeta) + pblk->lm.emeta_len[2]); |
a4bd217b JG |
901 | } |
902 | ||
b20ba1bc JG |
903 | static inline int pblk_line_vsc(struct pblk_line *line) |
904 | { | |
d340121e | 905 | return le32_to_cpu(*line->vsc); |
b20ba1bc JG |
906 | } |
907 | ||
a4bd217b JG |
908 | #define NVM_MEM_PAGE_WRITE (8) |
909 | ||
910 | static inline int pblk_pad_distance(struct pblk *pblk) | |
911 | { | |
912 | struct nvm_tgt_dev *dev = pblk->dev; | |
913 | struct nvm_geo *geo = &dev->geo; | |
914 | ||
915 | return NVM_MEM_PAGE_WRITE * geo->nr_luns * geo->sec_per_pl; | |
916 | } | |
917 | ||
918 | static inline int pblk_dev_ppa_to_line(struct ppa_addr p) | |
919 | { | |
920 | return p.g.blk; | |
921 | } | |
922 | ||
923 | static inline int pblk_tgt_ppa_to_line(struct ppa_addr p) | |
924 | { | |
925 | return p.g.blk; | |
926 | } | |
927 | ||
928 | static inline int pblk_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p) | |
929 | { | |
930 | return p.g.lun * geo->nr_chnls + p.g.ch; | |
931 | } | |
932 | ||
933 | /* A block within a line corresponds to the lun */ | |
934 | static inline int pblk_dev_ppa_to_pos(struct nvm_geo *geo, struct ppa_addr p) | |
935 | { | |
936 | return p.g.lun * geo->nr_chnls + p.g.ch; | |
937 | } | |
938 | ||
939 | static inline struct ppa_addr pblk_ppa32_to_ppa64(struct pblk *pblk, u32 ppa32) | |
940 | { | |
941 | struct ppa_addr ppa64; | |
942 | ||
943 | ppa64.ppa = 0; | |
944 | ||
945 | if (ppa32 == -1) { | |
946 | ppa64.ppa = ADDR_EMPTY; | |
947 | } else if (ppa32 & (1U << 31)) { | |
948 | ppa64.c.line = ppa32 & ((~0U) >> 1); | |
949 | ppa64.c.is_cached = 1; | |
950 | } else { | |
951 | ppa64.g.blk = (ppa32 & pblk->ppaf.blk_mask) >> | |
952 | pblk->ppaf.blk_offset; | |
953 | ppa64.g.pg = (ppa32 & pblk->ppaf.pg_mask) >> | |
954 | pblk->ppaf.pg_offset; | |
955 | ppa64.g.lun = (ppa32 & pblk->ppaf.lun_mask) >> | |
956 | pblk->ppaf.lun_offset; | |
957 | ppa64.g.ch = (ppa32 & pblk->ppaf.ch_mask) >> | |
958 | pblk->ppaf.ch_offset; | |
959 | ppa64.g.pl = (ppa32 & pblk->ppaf.pln_mask) >> | |
960 | pblk->ppaf.pln_offset; | |
961 | ppa64.g.sec = (ppa32 & pblk->ppaf.sec_mask) >> | |
962 | pblk->ppaf.sec_offset; | |
963 | } | |
964 | ||
965 | return ppa64; | |
966 | } | |
967 | ||
968 | static inline struct ppa_addr pblk_trans_map_get(struct pblk *pblk, | |
969 | sector_t lba) | |
970 | { | |
971 | struct ppa_addr ppa; | |
972 | ||
973 | if (pblk->ppaf_bitsize < 32) { | |
974 | u32 *map = (u32 *)pblk->trans_map; | |
975 | ||
976 | ppa = pblk_ppa32_to_ppa64(pblk, map[lba]); | |
977 | } else { | |
978 | struct ppa_addr *map = (struct ppa_addr *)pblk->trans_map; | |
979 | ||
980 | ppa = map[lba]; | |
981 | } | |
982 | ||
983 | return ppa; | |
984 | } | |
985 | ||
986 | static inline u32 pblk_ppa64_to_ppa32(struct pblk *pblk, struct ppa_addr ppa64) | |
987 | { | |
988 | u32 ppa32 = 0; | |
989 | ||
990 | if (ppa64.ppa == ADDR_EMPTY) { | |
991 | ppa32 = ~0U; | |
992 | } else if (ppa64.c.is_cached) { | |
993 | ppa32 |= ppa64.c.line; | |
994 | ppa32 |= 1U << 31; | |
995 | } else { | |
996 | ppa32 |= ppa64.g.blk << pblk->ppaf.blk_offset; | |
997 | ppa32 |= ppa64.g.pg << pblk->ppaf.pg_offset; | |
998 | ppa32 |= ppa64.g.lun << pblk->ppaf.lun_offset; | |
999 | ppa32 |= ppa64.g.ch << pblk->ppaf.ch_offset; | |
1000 | ppa32 |= ppa64.g.pl << pblk->ppaf.pln_offset; | |
1001 | ppa32 |= ppa64.g.sec << pblk->ppaf.sec_offset; | |
1002 | } | |
1003 | ||
1004 | return ppa32; | |
1005 | } | |
1006 | ||
1007 | static inline void pblk_trans_map_set(struct pblk *pblk, sector_t lba, | |
1008 | struct ppa_addr ppa) | |
1009 | { | |
1010 | if (pblk->ppaf_bitsize < 32) { | |
1011 | u32 *map = (u32 *)pblk->trans_map; | |
1012 | ||
1013 | map[lba] = pblk_ppa64_to_ppa32(pblk, ppa); | |
1014 | } else { | |
1015 | u64 *map = (u64 *)pblk->trans_map; | |
1016 | ||
1017 | map[lba] = ppa.ppa; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | static inline u64 pblk_dev_ppa_to_line_addr(struct pblk *pblk, | |
1022 | struct ppa_addr p) | |
1023 | { | |
1024 | u64 paddr; | |
1025 | ||
1026 | paddr = 0; | |
1027 | paddr |= (u64)p.g.pg << pblk->ppaf.pg_offset; | |
1028 | paddr |= (u64)p.g.lun << pblk->ppaf.lun_offset; | |
1029 | paddr |= (u64)p.g.ch << pblk->ppaf.ch_offset; | |
1030 | paddr |= (u64)p.g.pl << pblk->ppaf.pln_offset; | |
1031 | paddr |= (u64)p.g.sec << pblk->ppaf.sec_offset; | |
1032 | ||
1033 | return paddr; | |
1034 | } | |
1035 | ||
1036 | static inline int pblk_ppa_empty(struct ppa_addr ppa_addr) | |
1037 | { | |
1038 | return (ppa_addr.ppa == ADDR_EMPTY); | |
1039 | } | |
1040 | ||
1041 | static inline void pblk_ppa_set_empty(struct ppa_addr *ppa_addr) | |
1042 | { | |
1043 | ppa_addr->ppa = ADDR_EMPTY; | |
1044 | } | |
1045 | ||
07698466 JG |
1046 | static inline bool pblk_ppa_comp(struct ppa_addr lppa, struct ppa_addr rppa) |
1047 | { | |
1048 | if (lppa.ppa == rppa.ppa) | |
1049 | return true; | |
1050 | ||
1051 | return false; | |
1052 | } | |
1053 | ||
a4bd217b JG |
1054 | static inline int pblk_addr_in_cache(struct ppa_addr ppa) |
1055 | { | |
1056 | return (ppa.ppa != ADDR_EMPTY && ppa.c.is_cached); | |
1057 | } | |
1058 | ||
1059 | static inline int pblk_addr_to_cacheline(struct ppa_addr ppa) | |
1060 | { | |
1061 | return ppa.c.line; | |
1062 | } | |
1063 | ||
1064 | static inline struct ppa_addr pblk_cacheline_to_addr(int addr) | |
1065 | { | |
1066 | struct ppa_addr p; | |
1067 | ||
1068 | p.c.line = addr; | |
1069 | p.c.is_cached = 1; | |
1070 | ||
1071 | return p; | |
1072 | } | |
1073 | ||
1074 | static inline struct ppa_addr addr_to_gen_ppa(struct pblk *pblk, u64 paddr, | |
1075 | u64 line_id) | |
1076 | { | |
1077 | struct ppa_addr ppa; | |
1078 | ||
1079 | ppa.ppa = 0; | |
1080 | ppa.g.blk = line_id; | |
1081 | ppa.g.pg = (paddr & pblk->ppaf.pg_mask) >> pblk->ppaf.pg_offset; | |
1082 | ppa.g.lun = (paddr & pblk->ppaf.lun_mask) >> pblk->ppaf.lun_offset; | |
1083 | ppa.g.ch = (paddr & pblk->ppaf.ch_mask) >> pblk->ppaf.ch_offset; | |
1084 | ppa.g.pl = (paddr & pblk->ppaf.pln_mask) >> pblk->ppaf.pln_offset; | |
1085 | ppa.g.sec = (paddr & pblk->ppaf.sec_mask) >> pblk->ppaf.sec_offset; | |
1086 | ||
1087 | return ppa; | |
1088 | } | |
1089 | ||
1090 | static inline struct ppa_addr addr_to_pblk_ppa(struct pblk *pblk, u64 paddr, | |
1091 | u64 line_id) | |
1092 | { | |
1093 | struct ppa_addr ppa; | |
1094 | ||
1095 | ppa = addr_to_gen_ppa(pblk, paddr, line_id); | |
1096 | ||
1097 | return ppa; | |
1098 | } | |
1099 | ||
1100 | static inline u32 pblk_calc_meta_header_crc(struct pblk *pblk, | |
dd2a4343 | 1101 | struct line_header *header) |
a4bd217b JG |
1102 | { |
1103 | u32 crc = ~(u32)0; | |
1104 | ||
dd2a4343 | 1105 | crc = crc32_le(crc, (unsigned char *)header + sizeof(crc), |
a4bd217b JG |
1106 | sizeof(struct line_header) - sizeof(crc)); |
1107 | ||
1108 | return crc; | |
1109 | } | |
1110 | ||
1111 | static inline u32 pblk_calc_smeta_crc(struct pblk *pblk, | |
1112 | struct line_smeta *smeta) | |
1113 | { | |
1114 | struct pblk_line_meta *lm = &pblk->lm; | |
1115 | u32 crc = ~(u32)0; | |
1116 | ||
1117 | crc = crc32_le(crc, (unsigned char *)smeta + | |
1118 | sizeof(struct line_header) + sizeof(crc), | |
1119 | lm->smeta_len - | |
1120 | sizeof(struct line_header) - sizeof(crc)); | |
1121 | ||
1122 | return crc; | |
1123 | } | |
1124 | ||
1125 | static inline u32 pblk_calc_emeta_crc(struct pblk *pblk, | |
1126 | struct line_emeta *emeta) | |
1127 | { | |
1128 | struct pblk_line_meta *lm = &pblk->lm; | |
1129 | u32 crc = ~(u32)0; | |
1130 | ||
1131 | crc = crc32_le(crc, (unsigned char *)emeta + | |
1132 | sizeof(struct line_header) + sizeof(crc), | |
dd2a4343 | 1133 | lm->emeta_len[0] - |
a4bd217b JG |
1134 | sizeof(struct line_header) - sizeof(crc)); |
1135 | ||
1136 | return crc; | |
1137 | } | |
1138 | ||
1139 | static inline int pblk_set_progr_mode(struct pblk *pblk, int type) | |
1140 | { | |
1141 | struct nvm_tgt_dev *dev = pblk->dev; | |
1142 | struct nvm_geo *geo = &dev->geo; | |
1143 | int flags; | |
1144 | ||
1145 | flags = geo->plane_mode >> 1; | |
1146 | ||
e2cddf20 | 1147 | if (type == PBLK_WRITE) |
a4bd217b JG |
1148 | flags |= NVM_IO_SCRAMBLE_ENABLE; |
1149 | ||
1150 | return flags; | |
1151 | } | |
1152 | ||
f9c10152 JG |
1153 | enum { |
1154 | PBLK_READ_RANDOM = 0, | |
1155 | PBLK_READ_SEQUENTIAL = 1, | |
1156 | }; | |
1157 | ||
1158 | static inline int pblk_set_read_mode(struct pblk *pblk, int type) | |
1159 | { | |
1160 | struct nvm_tgt_dev *dev = pblk->dev; | |
1161 | struct nvm_geo *geo = &dev->geo; | |
1162 | int flags; | |
1163 | ||
1164 | flags = NVM_IO_SUSPEND | NVM_IO_SCRAMBLE_ENABLE; | |
1165 | if (type == PBLK_READ_SEQUENTIAL) | |
1166 | flags |= geo->plane_mode >> 1; | |
1167 | ||
1168 | return flags; | |
1169 | } | |
1170 | ||
1171 | static inline int pblk_io_aligned(struct pblk *pblk, int nr_secs) | |
a4bd217b | 1172 | { |
f9c10152 | 1173 | return !(nr_secs % pblk->min_write_pgs); |
a4bd217b JG |
1174 | } |
1175 | ||
1176 | #ifdef CONFIG_NVM_DEBUG | |
1177 | static inline void print_ppa(struct ppa_addr *p, char *msg, int error) | |
1178 | { | |
1179 | if (p->c.is_cached) { | |
1180 | pr_err("ppa: (%s: %x) cache line: %llu\n", | |
1181 | msg, error, (u64)p->c.line); | |
1182 | } else { | |
1183 | pr_err("ppa: (%s: %x):ch:%d,lun:%d,blk:%d,pg:%d,pl:%d,sec:%d\n", | |
1184 | msg, error, | |
1185 | p->g.ch, p->g.lun, p->g.blk, | |
1186 | p->g.pg, p->g.pl, p->g.sec); | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | static inline void pblk_print_failed_rqd(struct pblk *pblk, struct nvm_rq *rqd, | |
1191 | int error) | |
1192 | { | |
1193 | int bit = -1; | |
1194 | ||
1195 | if (rqd->nr_ppas == 1) { | |
1196 | print_ppa(&rqd->ppa_addr, "rqd", error); | |
1197 | return; | |
1198 | } | |
1199 | ||
1200 | while ((bit = find_next_bit((void *)&rqd->ppa_status, rqd->nr_ppas, | |
1201 | bit + 1)) < rqd->nr_ppas) { | |
1202 | print_ppa(&rqd->ppa_list[bit], "rqd", error); | |
1203 | } | |
1204 | ||
1205 | pr_err("error:%d, ppa_status:%llx\n", error, rqd->ppa_status); | |
1206 | } | |
1207 | #endif | |
1208 | ||
1209 | static inline int pblk_boundary_ppa_checks(struct nvm_tgt_dev *tgt_dev, | |
1210 | struct ppa_addr *ppas, int nr_ppas) | |
1211 | { | |
1212 | struct nvm_geo *geo = &tgt_dev->geo; | |
1213 | struct ppa_addr *ppa; | |
1214 | int i; | |
1215 | ||
1216 | for (i = 0; i < nr_ppas; i++) { | |
1217 | ppa = &ppas[i]; | |
1218 | ||
1219 | if (!ppa->c.is_cached && | |
1220 | ppa->g.ch < geo->nr_chnls && | |
1221 | ppa->g.lun < geo->luns_per_chnl && | |
1222 | ppa->g.pl < geo->nr_planes && | |
1223 | ppa->g.blk < geo->blks_per_lun && | |
1224 | ppa->g.pg < geo->pgs_per_blk && | |
1225 | ppa->g.sec < geo->sec_per_pg) | |
1226 | continue; | |
1227 | ||
1228 | #ifdef CONFIG_NVM_DEBUG | |
1229 | print_ppa(ppa, "boundary", i); | |
1230 | #endif | |
1231 | return 1; | |
1232 | } | |
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | static inline int pblk_boundary_paddr_checks(struct pblk *pblk, u64 paddr) | |
1237 | { | |
1238 | struct pblk_line_meta *lm = &pblk->lm; | |
1239 | ||
1240 | if (paddr > lm->sec_per_line) | |
1241 | return 1; | |
1242 | ||
1243 | return 0; | |
1244 | } | |
1245 | ||
1246 | static inline unsigned int pblk_get_bi_idx(struct bio *bio) | |
1247 | { | |
1248 | return bio->bi_iter.bi_idx; | |
1249 | } | |
1250 | ||
1251 | static inline sector_t pblk_get_lba(struct bio *bio) | |
1252 | { | |
1253 | return bio->bi_iter.bi_sector / NR_PHY_IN_LOG; | |
1254 | } | |
1255 | ||
1256 | static inline unsigned int pblk_get_secs(struct bio *bio) | |
1257 | { | |
1258 | return bio->bi_iter.bi_size / PBLK_EXPOSED_PAGE_SIZE; | |
1259 | } | |
1260 | ||
1261 | static inline sector_t pblk_get_sector(sector_t lba) | |
1262 | { | |
1263 | return lba * NR_PHY_IN_LOG; | |
1264 | } | |
1265 | ||
1266 | static inline void pblk_setup_uuid(struct pblk *pblk) | |
1267 | { | |
1268 | uuid_le uuid; | |
1269 | ||
1270 | uuid_le_gen(&uuid); | |
1271 | memcpy(pblk->instance_uuid, uuid.b, 16); | |
1272 | } | |
1273 | #endif /* PBLK_H_ */ |