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