|
| 1 | +/* |
| 2 | + * Copyright(c) 2012-2022 Intel Corporation |
| 3 | + * Copyright(c) 2022-2024 Huawei Technologies |
| 4 | + * Copyright(c) 2026 Unvertical |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#include "ocf/ocf.h" |
| 9 | +#include "engine_prefetch.h" |
| 10 | +#include "engine_inv.h" |
| 11 | +#include "engine_bf.h" |
| 12 | +#include "engine_common.h" |
| 13 | +#include "engine_io.h" |
| 14 | +#include "../ocf_cache_priv.h" |
| 15 | +#include "../concurrency/ocf_concurrency.h" |
| 16 | +#include "../ocf_request.h" |
| 17 | +#include "../utils/utils_cache_line.h" |
| 18 | +#include "../utils/utils_user_part.h" |
| 19 | +#include "../metadata/metadata.h" |
| 20 | +#include "../ocf_def_priv.h" |
| 21 | + |
| 22 | +#define OCF_ENGINE_DEBUG_IO_NAME "prefetch" |
| 23 | +#include "engine_debug.h" |
| 24 | + |
| 25 | +static void _ocf_prefetch_read_complete(struct ocf_request *req, int error) |
| 26 | +{ |
| 27 | + OCF_DEBUG_RQ(req, "Prefetch read completion"); |
| 28 | + |
| 29 | + ocf_req_get(req); |
| 30 | + |
| 31 | + if (error) { |
| 32 | + req->complete(req, error); |
| 33 | + |
| 34 | + ctx_data_free(req->cache->owner, req->data); |
| 35 | + |
| 36 | + ocf_core_stats_core_error_update(req->core, OCF_READ); |
| 37 | + |
| 38 | + /* Invalidate metadata */ |
| 39 | + ocf_engine_invalidate(req); |
| 40 | + |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + /* Pretend the data is the copy, so that it's used by the backfill */ |
| 45 | + req->cp_data = req->data; |
| 46 | + req->data = NULL; |
| 47 | + |
| 48 | + /* Complete request */ |
| 49 | + req->complete(req, error); |
| 50 | + |
| 51 | + ocf_engine_backfill(req); |
| 52 | +} |
| 53 | + |
| 54 | +static int _ocf_prefetch_read_do(struct ocf_request *req) |
| 55 | +{ |
| 56 | + struct ocf_alock *c = ocf_cache_line_concurrency(req->cache); |
| 57 | + |
| 58 | + if (unlikely(ocf_engine_is_hit(req))) { |
| 59 | + ocf_req_unlock(c, req); |
| 60 | + req->complete(req, 0); |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + if (req->info.dirty_any) { |
| 65 | + ocf_hb_req_prot_lock_rd(req); |
| 66 | + |
| 67 | + /* Request is dirty need to clean request */ |
| 68 | + ocf_engine_clean(req); |
| 69 | + |
| 70 | + ocf_hb_req_prot_unlock_rd(req); |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + req->data = ctx_data_alloc(ocf_cache_get_ctx(req->cache), |
| 75 | + OCF_DIV_ROUND_UP(req->bytes, PAGE_SIZE)); |
| 76 | + if (!req->data) { |
| 77 | + ocf_req_unlock(c, req); |
| 78 | + req->complete(req, 0); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + ocf_hb_req_prot_lock_wr(req); |
| 83 | + |
| 84 | + /* Set valid status bits map */ |
| 85 | + ocf_set_valid_map_info(req); |
| 86 | + |
| 87 | + if (ocf_engine_needs_repart(req)) { |
| 88 | + OCF_DEBUG_RQ(req, "Re-Part"); |
| 89 | + |
| 90 | + /* Probably some cache lines are assigned into wrong |
| 91 | + * partition. Need to move it to new one |
| 92 | + */ |
| 93 | + ocf_user_part_move(req); |
| 94 | + } |
| 95 | + |
| 96 | + ocf_hb_req_prot_unlock_wr(req); |
| 97 | + |
| 98 | + OCF_DEBUG_RQ(req, "Submit"); |
| 99 | + |
| 100 | + ocf_req_get(req); |
| 101 | + ocf_engine_forward_core_io_req(req, _ocf_prefetch_read_complete); |
| 102 | + |
| 103 | + /* Update statistics */ |
| 104 | + ocf_engine_update_request_stats(req); |
| 105 | + ocf_engine_update_block_stats(req); |
| 106 | + |
| 107 | + /* Put OCF request - decrease reference counter */ |
| 108 | + ocf_req_put(req); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +int ocf_prefetch_read(struct ocf_request *req) |
| 114 | +{ |
| 115 | + struct ocf_user_part *user_part = &req->cache->user_parts[req->part_id]; |
| 116 | + struct ocf_alock *c = ocf_cache_line_concurrency(req->cache); |
| 117 | + ocf_cache_t cache = req->cache; |
| 118 | + int lock; |
| 119 | + |
| 120 | + if (env_atomic_read(&cache->pending_read_misses_list_blocked)) { |
| 121 | + req->complete(req, -OCF_ERR_BUSY); |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + req->engine_handler = _ocf_prefetch_read_do; |
| 126 | + |
| 127 | + if (!ocf_user_part_is_enabled(user_part)) { |
| 128 | + req->complete(req, -OCF_ERR_BUSY); |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + ocf_req_hash(req); |
| 133 | + |
| 134 | + ocf_hb_req_prot_lock_wr(req); |
| 135 | + |
| 136 | + ocf_engine_lookup(req); |
| 137 | + |
| 138 | + if (unlikely(ocf_engine_is_hit(req))) { |
| 139 | + ocf_hb_req_prot_unlock_wr(req); |
| 140 | + req->complete(req, 0); |
| 141 | + return 0; |
| 142 | + } |
| 143 | + |
| 144 | + ocf_prepare_clines_miss(req); |
| 145 | + if (unlikely(ocf_req_test_mapping_error(req))) { |
| 146 | + ocf_hb_req_prot_unlock_wr(req); |
| 147 | + req->complete(req, 0); |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + lock = ocf_req_async_lock_wr(c, req, ocf_engine_on_resume); |
| 152 | + if (unlikely(lock < 0)) { |
| 153 | + ocf_hb_req_prot_unlock_wr(req); |
| 154 | + req->complete(req, 0); |
| 155 | + return 0; |
| 156 | + } |
| 157 | + |
| 158 | + ocf_hb_req_prot_unlock_wr(req); |
| 159 | + |
| 160 | + if (lock == OCF_LOCK_ACQUIRED) |
| 161 | + _ocf_prefetch_read_do(req); |
| 162 | + |
| 163 | + return 0; |
| 164 | +} |
0 commit comments