]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/lightnvm/pblk-rb.c
lightnvm: pblk: encapsulate rb pointer operations
[mirror_ubuntu-hirsute-kernel.git] / drivers / lightnvm / pblk-rb.c
CommitLineData
02a1520d 1// SPDX-License-Identifier: GPL-2.0
a4bd217b
JG
2/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 *
6 * Based upon the circular ringbuffer.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * pblk-rb.c - pblk's write buffer
18 */
19
20#include <linux/circ_buf.h>
21
22#include "pblk.h"
23
24static DECLARE_RWSEM(pblk_rb_lock);
25
26void pblk_rb_data_free(struct pblk_rb *rb)
27{
28 struct pblk_rb_pages *p, *t;
29
30 down_write(&pblk_rb_lock);
31 list_for_each_entry_safe(p, t, &rb->pages, list) {
32 free_pages((unsigned long)page_address(p->pages), p->order);
33 list_del(&p->list);
34 kfree(p);
35 }
36 up_write(&pblk_rb_lock);
37}
38
39/*
40 * Initialize ring buffer. The data and metadata buffers must be previously
41 * allocated and their size must be a power of two
5fb94e9c 42 * (Documentation/core-api/circular-buffers.rst)
a4bd217b
JG
43 */
44int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
45 unsigned int power_size, unsigned int power_seg_sz)
46{
47 struct pblk *pblk = container_of(rb, struct pblk, rwb);
48 unsigned int init_entry = 0;
49 unsigned int alloc_order = power_size;
50 unsigned int max_order = MAX_ORDER - 1;
51 unsigned int order, iter;
52
53 down_write(&pblk_rb_lock);
54 rb->entries = rb_entry_base;
55 rb->seg_size = (1 << power_seg_sz);
56 rb->nr_entries = (1 << power_size);
57 rb->mem = rb->subm = rb->sync = rb->l2p_update = 0;
8154d296 58 rb->flush_point = EMPTY_ENTRY;
a4bd217b
JG
59
60 spin_lock_init(&rb->w_lock);
61 spin_lock_init(&rb->s_lock);
62
63 INIT_LIST_HEAD(&rb->pages);
64
65 if (alloc_order >= max_order) {
66 order = max_order;
67 iter = (1 << (alloc_order - max_order));
68 } else {
69 order = alloc_order;
70 iter = 1;
71 }
72
73 do {
74 struct pblk_rb_entry *entry;
75 struct pblk_rb_pages *page_set;
76 void *kaddr;
77 unsigned long set_size;
78 int i;
79
80 page_set = kmalloc(sizeof(struct pblk_rb_pages), GFP_KERNEL);
81 if (!page_set) {
82 up_write(&pblk_rb_lock);
83 return -ENOMEM;
84 }
85
86 page_set->order = order;
87 page_set->pages = alloc_pages(GFP_KERNEL, order);
88 if (!page_set->pages) {
89 kfree(page_set);
90 pblk_rb_data_free(rb);
91 up_write(&pblk_rb_lock);
92 return -ENOMEM;
93 }
94 kaddr = page_address(page_set->pages);
95
96 entry = &rb->entries[init_entry];
97 entry->data = kaddr;
98 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
99 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
100
101 set_size = (1 << order);
102 for (i = 1; i < set_size; i++) {
103 entry = &rb->entries[init_entry];
104 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
105 entry->data = kaddr + (i * rb->seg_size);
106 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
107 bio_list_init(&entry->w_ctx.bios);
108 }
109
110 list_add_tail(&page_set->list, &rb->pages);
111 iter--;
112 } while (iter > 0);
113 up_write(&pblk_rb_lock);
114
880eda54 115#ifdef CONFIG_NVM_PBLK_DEBUG
8154d296 116 atomic_set(&rb->inflight_flush_point, 0);
a4bd217b
JG
117#endif
118
119 /*
120 * Initialize rate-limiter, which controls access to the write buffer
121 * but user and GC I/O
122 */
123 pblk_rl_init(&pblk->rl, rb->nr_entries);
124
125 return 0;
126}
127
128/*
129 * pblk_rb_calculate_size -- calculate the size of the write buffer
130 */
131unsigned int pblk_rb_calculate_size(unsigned int nr_entries)
132{
133 /* Alloc a write buffer that can at least fit 128 entries */
134 return (1 << max(get_count_order(nr_entries), 7));
135}
136
137void *pblk_rb_entries_ref(struct pblk_rb *rb)
138{
139 return rb->entries;
140}
141
142static void clean_wctx(struct pblk_w_ctx *w_ctx)
143{
144 int flags;
145
a4bd217b 146 flags = READ_ONCE(w_ctx->flags);
e37d0798
JG
147 WARN_ONCE(!(flags & PBLK_SUBMITTED_ENTRY),
148 "pblk: overwriting unsubmitted data\n");
a4bd217b
JG
149
150 /* Release flags on context. Protect from writes and reads */
151 smp_store_release(&w_ctx->flags, PBLK_WRITABLE_ENTRY);
152 pblk_ppa_set_empty(&w_ctx->ppa);
07698466 153 w_ctx->lba = ADDR_EMPTY;
a4bd217b
JG
154}
155
156#define pblk_rb_ring_count(head, tail, size) CIRC_CNT(head, tail, size)
157#define pblk_rb_ring_space(rb, head, tail, size) \
158 (CIRC_SPACE(head, tail, size))
159
160/*
161 * Buffer space is calculated with respect to the back pointer signaling
162 * synchronized entries to the media.
163 */
164static unsigned int pblk_rb_space(struct pblk_rb *rb)
165{
166 unsigned int mem = READ_ONCE(rb->mem);
167 unsigned int sync = READ_ONCE(rb->sync);
168
169 return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries);
170}
171
40b8657d
JG
172unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p,
173 unsigned int nr_entries)
174{
175 return (p + nr_entries) & (rb->nr_entries - 1);
176}
177
a4bd217b
JG
178/*
179 * Buffer count is calculated with respect to the submission entry signaling the
180 * entries that are available to send to the media
181 */
182unsigned int pblk_rb_read_count(struct pblk_rb *rb)
183{
184 unsigned int mem = READ_ONCE(rb->mem);
185 unsigned int subm = READ_ONCE(rb->subm);
186
187 return pblk_rb_ring_count(mem, subm, rb->nr_entries);
188}
189
ee8d5c1a
JG
190unsigned int pblk_rb_sync_count(struct pblk_rb *rb)
191{
192 unsigned int mem = READ_ONCE(rb->mem);
193 unsigned int sync = READ_ONCE(rb->sync);
194
195 return pblk_rb_ring_count(mem, sync, rb->nr_entries);
196}
197
a4bd217b
JG
198unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
199{
200 unsigned int subm;
201
202 subm = READ_ONCE(rb->subm);
203 /* Commit read means updating submission pointer */
40b8657d 204 smp_store_release(&rb->subm, pblk_rb_ptr_wrap(rb, subm, nr_entries));
a4bd217b
JG
205
206 return subm;
207}
208
05ed3447 209static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
a4bd217b
JG
210{
211 struct pblk *pblk = container_of(rb, struct pblk, rwb);
212 struct pblk_line *line;
213 struct pblk_rb_entry *entry;
214 struct pblk_w_ctx *w_ctx;
b20ba1bc 215 unsigned int user_io = 0, gc_io = 0;
a4bd217b 216 unsigned int i;
b20ba1bc 217 int flags;
a4bd217b
JG
218
219 for (i = 0; i < to_update; i++) {
05ed3447 220 entry = &rb->entries[rb->l2p_update];
a4bd217b
JG
221 w_ctx = &entry->w_ctx;
222
b20ba1bc
JG
223 flags = READ_ONCE(entry->w_ctx.flags);
224 if (flags & PBLK_IOTYPE_USER)
225 user_io++;
226 else if (flags & PBLK_IOTYPE_GC)
227 gc_io++;
228 else
229 WARN(1, "pblk: unknown IO type\n");
230
a4bd217b
JG
231 pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
232 entry->cacheline);
233
cb21665c 234 line = pblk_ppa_to_line(pblk, w_ctx->ppa);
a4bd217b
JG
235 kref_put(&line->ref, pblk_line_put);
236 clean_wctx(w_ctx);
40b8657d 237 rb->l2p_update = pblk_rb_ptr_wrap(rb, rb->l2p_update, 1);
a4bd217b
JG
238 }
239
b20ba1bc
JG
240 pblk_rl_out(&pblk->rl, user_io, gc_io);
241
a4bd217b
JG
242 return 0;
243}
244
245/*
246 * When we move the l2p_update pointer, we update the l2p table - lookups will
247 * point to the physical address instead of to the cacheline in the write buffer
248 * from this moment on.
249 */
250static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
251 unsigned int mem, unsigned int sync)
252{
253 unsigned int space, count;
254 int ret = 0;
255
256 lockdep_assert_held(&rb->w_lock);
257
258 /* Update l2p only as buffer entries are being overwritten */
259 space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries);
260 if (space > nr_entries)
261 goto out;
262
263 count = nr_entries - space;
264 /* l2p_update used exclusively under rb->w_lock */
05ed3447 265 ret = __pblk_rb_update_l2p(rb, count);
a4bd217b
JG
266
267out:
268 return ret;
269}
270
271/*
272 * Update the l2p entry for all sectors stored on the write buffer. This means
273 * that all future lookups to the l2p table will point to a device address, not
274 * to the cacheline in the write buffer.
275 */
276void pblk_rb_sync_l2p(struct pblk_rb *rb)
277{
278 unsigned int sync;
279 unsigned int to_update;
280
281 spin_lock(&rb->w_lock);
282
283 /* Protect from reads and writes */
284 sync = smp_load_acquire(&rb->sync);
285
286 to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
05ed3447 287 __pblk_rb_update_l2p(rb, to_update);
a4bd217b
JG
288
289 spin_unlock(&rb->w_lock);
290}
291
292/*
293 * Write @nr_entries to ring buffer from @data buffer if there is enough space.
294 * Typically, 4KB data chunks coming from a bio will be copied to the ring
295 * buffer, thus the write will fail if not all incoming data can be copied.
296 *
297 */
298static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data,
299 struct pblk_w_ctx w_ctx,
300 struct pblk_rb_entry *entry)
301{
302 memcpy(entry->data, data, rb->seg_size);
303
304 entry->w_ctx.lba = w_ctx.lba;
305 entry->w_ctx.ppa = w_ctx.ppa;
306}
307
308void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
309 struct pblk_w_ctx w_ctx, unsigned int ring_pos)
310{
311 struct pblk *pblk = container_of(rb, struct pblk, rwb);
312 struct pblk_rb_entry *entry;
313 int flags;
314
315 entry = &rb->entries[ring_pos];
316 flags = READ_ONCE(entry->w_ctx.flags);
880eda54 317#ifdef CONFIG_NVM_PBLK_DEBUG
a4bd217b
JG
318 /* Caller must guarantee that the entry is free */
319 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
320#endif
321
322 __pblk_rb_write_entry(rb, data, w_ctx, entry);
323
324 pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
325 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
326
327 /* Release flags on write context. Protect from writes */
328 smp_store_release(&entry->w_ctx.flags, flags);
329}
330
331void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
d340121e
JG
332 struct pblk_w_ctx w_ctx, struct pblk_line *line,
333 u64 paddr, unsigned int ring_pos)
a4bd217b
JG
334{
335 struct pblk *pblk = container_of(rb, struct pblk, rwb);
336 struct pblk_rb_entry *entry;
337 int flags;
338
339 entry = &rb->entries[ring_pos];
340 flags = READ_ONCE(entry->w_ctx.flags);
880eda54 341#ifdef CONFIG_NVM_PBLK_DEBUG
a4bd217b
JG
342 /* Caller must guarantee that the entry is free */
343 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
344#endif
345
346 __pblk_rb_write_entry(rb, data, w_ctx, entry);
347
d340121e 348 if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, line, paddr))
a4bd217b
JG
349 entry->w_ctx.lba = ADDR_EMPTY;
350
351 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
352
353 /* Release flags on write context. Protect from writes */
354 smp_store_release(&entry->w_ctx.flags, flags);
355}
356
8154d296 357static int pblk_rb_flush_point_set(struct pblk_rb *rb, struct bio *bio,
cc9c9a00 358 unsigned int pos)
a4bd217b
JG
359{
360 struct pblk_rb_entry *entry;
533657c1 361 unsigned int sync, flush_point;
a4bd217b 362
3c05ef11 363 pblk_rb_sync_init(rb, NULL);
533657c1
HH
364 sync = READ_ONCE(rb->sync);
365
3c05ef11
HH
366 if (pos == sync) {
367 pblk_rb_sync_end(rb, NULL);
533657c1 368 return 0;
3c05ef11 369 }
a4bd217b 370
880eda54 371#ifdef CONFIG_NVM_PBLK_DEBUG
8154d296 372 atomic_inc(&rb->inflight_flush_point);
a4bd217b
JG
373#endif
374
8154d296
HH
375 flush_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
376 entry = &rb->entries[flush_point];
a4bd217b 377
8154d296
HH
378 /* Protect flush points */
379 smp_store_release(&rb->flush_point, flush_point);
a4bd217b 380
b36bbf9d
HH
381 if (bio)
382 bio_list_add(&entry->w_ctx.bios, bio);
588726d3 383
b36bbf9d 384 pblk_rb_sync_end(rb, NULL);
a4bd217b 385
b36bbf9d 386 return bio ? 1 : 0;
a4bd217b
JG
387}
388
389static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
390 unsigned int *pos)
391{
392 unsigned int mem;
393 unsigned int sync;
394
395 sync = READ_ONCE(rb->sync);
396 mem = READ_ONCE(rb->mem);
397
398 if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
399 return 0;
400
401 if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
402 return 0;
403
404 *pos = mem;
405
406 return 1;
407}
408
409static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
410 unsigned int *pos)
411{
412 if (!__pblk_rb_may_write(rb, nr_entries, pos))
413 return 0;
414
415 /* Protect from read count */
40b8657d 416 smp_store_release(&rb->mem, pblk_rb_ptr_wrap(rb, *pos, nr_entries));
a4bd217b
JG
417 return 1;
418}
419
588726d3
JG
420void pblk_rb_flush(struct pblk_rb *rb)
421{
422 struct pblk *pblk = container_of(rb, struct pblk, rwb);
423 unsigned int mem = READ_ONCE(rb->mem);
424
8154d296 425 if (pblk_rb_flush_point_set(rb, NULL, mem))
588726d3
JG
426 return;
427
cc9c9a00 428 pblk_write_kick(pblk);
588726d3
JG
429}
430
a4bd217b
JG
431static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
432 unsigned int *pos, struct bio *bio,
433 int *io_ret)
434{
435 unsigned int mem;
436
437 if (!__pblk_rb_may_write(rb, nr_entries, pos))
438 return 0;
439
40b8657d 440 mem = pblk_rb_ptr_wrap(rb, *pos, nr_entries);
a4bd217b
JG
441 *io_ret = NVM_IO_DONE;
442
443 if (bio->bi_opf & REQ_PREFLUSH) {
444 struct pblk *pblk = container_of(rb, struct pblk, rwb);
445
5d149bfa 446 atomic64_inc(&pblk->nr_flush);
8154d296 447 if (pblk_rb_flush_point_set(&pblk->rwb, bio, mem))
a4bd217b
JG
448 *io_ret = NVM_IO_OK;
449 }
450
451 /* Protect from read count */
452 smp_store_release(&rb->mem, mem);
6ca2f71f 453
a4bd217b
JG
454 return 1;
455}
456
457/*
458 * Atomically check that (i) there is space on the write buffer for the
459 * incoming I/O, and (ii) the current I/O type has enough budget in the write
460 * buffer (rate-limiter).
461 */
462int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
463 unsigned int nr_entries, unsigned int *pos)
464{
465 struct pblk *pblk = container_of(rb, struct pblk, rwb);
588726d3 466 int io_ret;
a4bd217b
JG
467
468 spin_lock(&rb->w_lock);
588726d3
JG
469 io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries);
470 if (io_ret) {
a4bd217b 471 spin_unlock(&rb->w_lock);
588726d3 472 return io_ret;
a4bd217b
JG
473 }
474
588726d3 475 if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) {
a4bd217b
JG
476 spin_unlock(&rb->w_lock);
477 return NVM_IO_REQUEUE;
478 }
479
480 pblk_rl_user_in(&pblk->rl, nr_entries);
481 spin_unlock(&rb->w_lock);
482
588726d3 483 return io_ret;
a4bd217b
JG
484}
485
486/*
487 * Look at pblk_rb_may_write_user comment
488 */
489int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
490 unsigned int *pos)
491{
492 struct pblk *pblk = container_of(rb, struct pblk, rwb);
493
494 spin_lock(&rb->w_lock);
495 if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
496 spin_unlock(&rb->w_lock);
497 return 0;
498 }
499
500 if (!pblk_rb_may_write(rb, nr_entries, pos)) {
501 spin_unlock(&rb->w_lock);
502 return 0;
503 }
504
505 pblk_rl_gc_in(&pblk->rl, nr_entries);
506 spin_unlock(&rb->w_lock);
507
508 return 1;
509}
510
a4bd217b
JG
511/*
512 * Read available entries on rb and add them to the given bio. To avoid a memory
513 * copy, a page reference to the write buffer is used to be added to the bio.
514 *
515 * This function is used by the write thread to form the write bio that will
516 * persist data on the write buffer to the media.
517 */
d624f371 518unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
875d94f3
JG
519 unsigned int pos, unsigned int nr_entries,
520 unsigned int count)
a4bd217b
JG
521{
522 struct pblk *pblk = container_of(rb, struct pblk, rwb);
d624f371
JG
523 struct request_queue *q = pblk->dev->q;
524 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
875d94f3 525 struct bio *bio = rqd->bio;
a4bd217b
JG
526 struct pblk_rb_entry *entry;
527 struct page *page;
d624f371 528 unsigned int pad = 0, to_read = nr_entries;
a4bd217b
JG
529 unsigned int i;
530 int flags;
a4bd217b
JG
531
532 if (count < nr_entries) {
533 pad = nr_entries - count;
534 to_read = count;
535 }
536
537 c_ctx->sentry = pos;
538 c_ctx->nr_valid = to_read;
539 c_ctx->nr_padded = pad;
540
541 for (i = 0; i < to_read; i++) {
542 entry = &rb->entries[pos];
543
544 /* A write has been allowed into the buffer, but data is still
545 * being copied to it. It is ok to busy wait.
546 */
547try:
548 flags = READ_ONCE(entry->w_ctx.flags);
10888129
JG
549 if (!(flags & PBLK_WRITTEN_DATA)) {
550 io_schedule();
a4bd217b 551 goto try;
10888129 552 }
a4bd217b 553
a4bd217b
JG
554 page = virt_to_page(entry->data);
555 if (!page) {
4e495a46 556 pblk_err(pblk, "could not allocate write bio page\n");
a4bd217b
JG
557 flags &= ~PBLK_WRITTEN_DATA;
558 flags |= PBLK_SUBMITTED_ENTRY;
559 /* Release flags on context. Protect from writes */
560 smp_store_release(&entry->w_ctx.flags, flags);
d624f371 561 return NVM_IO_ERR;
a4bd217b
JG
562 }
563
d624f371
JG
564 if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) !=
565 rb->seg_size) {
4e495a46 566 pblk_err(pblk, "could not add page to write bio\n");
a4bd217b
JG
567 flags &= ~PBLK_WRITTEN_DATA;
568 flags |= PBLK_SUBMITTED_ENTRY;
569 /* Release flags on context. Protect from writes */
570 smp_store_release(&entry->w_ctx.flags, flags);
d624f371 571 return NVM_IO_ERR;
a4bd217b
JG
572 }
573
a4bd217b
JG
574 flags &= ~PBLK_WRITTEN_DATA;
575 flags |= PBLK_SUBMITTED_ENTRY;
576
577 /* Release flags on context. Protect from writes */
578 smp_store_release(&entry->w_ctx.flags, flags);
579
40b8657d 580 pos = pblk_rb_ptr_wrap(rb, pos, 1);
a4bd217b
JG
581 }
582
d624f371
JG
583 if (pad) {
584 if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) {
4e495a46 585 pblk_err(pblk, "could not pad page in write bio\n");
d624f371
JG
586 return NVM_IO_ERR;
587 }
d624f371 588
5d149bfa
HH
589 if (pad < pblk->min_write_pgs)
590 atomic64_inc(&pblk->pad_dist[pad - 1]);
591 else
4e495a46 592 pblk_warn(pblk, "padding more than min. sectors\n");
5d149bfa
HH
593
594 atomic64_add(pad, &pblk->pad_wa);
595 }
76758390 596
880eda54 597#ifdef CONFIG_NVM_PBLK_DEBUG
5d149bfa 598 atomic_long_add(pad, &pblk->padded_writes);
a4bd217b 599#endif
d624f371
JG
600
601 return NVM_IO_OK;
a4bd217b
JG
602}
603
604/*
605 * Copy to bio only if the lba matches the one on the given cache entry.
606 * Otherwise, it means that the entry has been overwritten, and the bio should
607 * be directed to disk.
608 */
609int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
75cb8e93 610 struct ppa_addr ppa, int bio_iter, bool advanced_bio)
a4bd217b 611{
07698466 612 struct pblk *pblk = container_of(rb, struct pblk, rwb);
a4bd217b
JG
613 struct pblk_rb_entry *entry;
614 struct pblk_w_ctx *w_ctx;
07698466
JG
615 struct ppa_addr l2p_ppa;
616 u64 pos = pblk_addr_to_cacheline(ppa);
a4bd217b
JG
617 void *data;
618 int flags;
619 int ret = 1;
620
a4bd217b 621
880eda54 622#ifdef CONFIG_NVM_PBLK_DEBUG
a4bd217b
JG
623 /* Caller must ensure that the access will not cause an overflow */
624 BUG_ON(pos >= rb->nr_entries);
625#endif
626 entry = &rb->entries[pos];
627 w_ctx = &entry->w_ctx;
628 flags = READ_ONCE(w_ctx->flags);
629
07698466
JG
630 spin_lock(&rb->w_lock);
631 spin_lock(&pblk->trans_lock);
632 l2p_ppa = pblk_trans_map_get(pblk, lba);
633 spin_unlock(&pblk->trans_lock);
634
a4bd217b 635 /* Check if the entry has been overwritten or is scheduled to be */
07698466
JG
636 if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba ||
637 flags & PBLK_WRITABLE_ENTRY) {
a4bd217b
JG
638 ret = 0;
639 goto out;
640 }
641
642 /* Only advance the bio if it hasn't been advanced already. If advanced,
643 * this bio is at least a partial bio (i.e., it has partially been
644 * filled with data from the cache). If part of the data resides on the
645 * media, we will read later on
646 */
75cb8e93 647 if (unlikely(!advanced_bio))
a4bd217b
JG
648 bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
649
650 data = bio_data(bio);
651 memcpy(data, entry->data, rb->seg_size);
652
653out:
654 spin_unlock(&rb->w_lock);
655 return ret;
656}
657
658struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
659{
40b8657d 660 unsigned int entry = pblk_rb_ptr_wrap(rb, pos, 0);
a4bd217b
JG
661
662 return &rb->entries[entry].w_ctx;
663}
664
665unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
666 __acquires(&rb->s_lock)
667{
668 if (flags)
669 spin_lock_irqsave(&rb->s_lock, *flags);
670 else
671 spin_lock_irq(&rb->s_lock);
672
673 return rb->sync;
674}
675
676void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
677 __releases(&rb->s_lock)
678{
679 lockdep_assert_held(&rb->s_lock);
680
681 if (flags)
682 spin_unlock_irqrestore(&rb->s_lock, *flags);
683 else
684 spin_unlock_irq(&rb->s_lock);
685}
686
687unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
688{
533657c1 689 unsigned int sync, flush_point;
a4bd217b
JG
690 lockdep_assert_held(&rb->s_lock);
691
692 sync = READ_ONCE(rb->sync);
533657c1
HH
693 flush_point = READ_ONCE(rb->flush_point);
694
695 if (flush_point != EMPTY_ENTRY) {
696 unsigned int secs_to_flush;
a4bd217b 697
533657c1
HH
698 secs_to_flush = pblk_rb_ring_count(flush_point, sync,
699 rb->nr_entries);
700 if (secs_to_flush < nr_entries) {
701 /* Protect flush points */
702 smp_store_release(&rb->flush_point, EMPTY_ENTRY);
703 }
704 }
705
40b8657d 706 sync = pblk_rb_ptr_wrap(rb, sync, nr_entries);
a4bd217b
JG
707
708 /* Protect from counts */
709 smp_store_release(&rb->sync, sync);
710
711 return sync;
712}
713
533657c1 714/* Calculate how many sectors to submit up to the current flush point. */
8154d296 715unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb)
a4bd217b 716{
533657c1
HH
717 unsigned int subm, sync, flush_point;
718 unsigned int submitted, to_flush;
a4bd217b 719
8154d296
HH
720 /* Protect flush points */
721 flush_point = smp_load_acquire(&rb->flush_point);
722 if (flush_point == EMPTY_ENTRY)
a4bd217b
JG
723 return 0;
724
533657c1
HH
725 /* Protect syncs */
726 sync = smp_load_acquire(&rb->sync);
727
a4bd217b 728 subm = READ_ONCE(rb->subm);
533657c1 729 submitted = pblk_rb_ring_count(subm, sync, rb->nr_entries);
a4bd217b
JG
730
731 /* The sync point itself counts as a sector to sync */
533657c1 732 to_flush = pblk_rb_ring_count(flush_point, sync, rb->nr_entries) + 1;
a4bd217b 733
533657c1 734 return (submitted < to_flush) ? (to_flush - submitted) : 0;
a4bd217b
JG
735}
736
a4bd217b
JG
737int pblk_rb_tear_down_check(struct pblk_rb *rb)
738{
739 struct pblk_rb_entry *entry;
740 int i;
741 int ret = 0;
742
743 spin_lock(&rb->w_lock);
744 spin_lock_irq(&rb->s_lock);
745
746 if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
747 (rb->sync == rb->l2p_update) &&
8154d296 748 (rb->flush_point == EMPTY_ENTRY)) {
a4bd217b
JG
749 goto out;
750 }
751
752 if (!rb->entries) {
753 ret = 1;
754 goto out;
755 }
756
757 for (i = 0; i < rb->nr_entries; i++) {
758 entry = &rb->entries[i];
759
760 if (!entry->data) {
761 ret = 1;
762 goto out;
763 }
764 }
765
766out:
767 spin_unlock(&rb->w_lock);
768 spin_unlock_irq(&rb->s_lock);
769
770 return ret;
771}
772
773unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
774{
775 return (pos & (rb->nr_entries - 1));
776}
777
778int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
779{
780 return (pos >= rb->nr_entries);
781}
782
783ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
784{
785 struct pblk *pblk = container_of(rb, struct pblk, rwb);
786 struct pblk_c_ctx *c;
787 ssize_t offset;
788 int queued_entries = 0;
789
790 spin_lock_irq(&rb->s_lock);
791 list_for_each_entry(c, &pblk->compl_list, list)
792 queued_entries++;
793 spin_unlock_irq(&rb->s_lock);
794
8154d296 795 if (rb->flush_point != EMPTY_ENTRY)
a4bd217b
JG
796 offset = scnprintf(buf, PAGE_SIZE,
797 "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
798 rb->nr_entries,
799 rb->mem,
800 rb->subm,
801 rb->sync,
802 rb->l2p_update,
880eda54 803#ifdef CONFIG_NVM_PBLK_DEBUG
8154d296 804 atomic_read(&rb->inflight_flush_point),
a4bd217b
JG
805#else
806 0,
807#endif
8154d296 808 rb->flush_point,
a4bd217b
JG
809 pblk_rb_read_count(rb),
810 pblk_rb_space(rb),
8154d296 811 pblk_rb_flush_point_count(rb),
a4bd217b
JG
812 queued_entries);
813 else
814 offset = scnprintf(buf, PAGE_SIZE,
815 "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
816 rb->nr_entries,
817 rb->mem,
818 rb->subm,
819 rb->sync,
820 rb->l2p_update,
880eda54 821#ifdef CONFIG_NVM_PBLK_DEBUG
8154d296 822 atomic_read(&rb->inflight_flush_point),
a4bd217b
JG
823#else
824 0,
825#endif
826 pblk_rb_read_count(rb),
827 pblk_rb_space(rb),
8154d296 828 pblk_rb_flush_point_count(rb),
a4bd217b
JG
829 queued_entries);
830
831 return offset;
832}