2 * Copyright(c) 2019 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
6 #include "../metadata/metadata.h"
10 #include "nhit/nhit.h"
12 struct promotion_policy_ops ocf_promotion_policies
[ocf_promotion_max
] = {
13 [ocf_promotion_always
] = {
16 [ocf_promotion_nhit
] = {
20 .deinit
= nhit_deinit
,
21 .set_param
= nhit_set_param
,
22 .get_param
= nhit_get_param
,
23 .req_purge
= nhit_req_purge
,
24 .req_should_promote
= nhit_req_should_promote
,
28 ocf_error_t
ocf_promotion_init(ocf_cache_t cache
, ocf_promotion_t type
)
30 ocf_promotion_policy_t policy
;
31 ocf_error_t result
= 0;
33 ENV_BUG_ON(type
>= ocf_promotion_max
);
35 policy
= env_vmalloc(sizeof(*policy
));
37 return -OCF_ERR_NO_MEM
;
40 policy
->owner
= cache
;
42 (void *)&cache
->conf_meta
->promotion
[type
].data
;
43 cache
->promotion_policy
= policy
;
45 if (ocf_promotion_policies
[type
].init
)
46 result
= ocf_promotion_policies
[type
].init(cache
);
49 env_vfree(cache
->promotion_policy
);
50 cache
->promotion_policy
= NULL
;
51 ocf_cache_log(cache
, log_info
,
52 "Policy '%s' failed to initialize\n",
53 ocf_promotion_policies
[type
].name
);
55 ocf_cache_log(cache
, log_info
,
56 "Policy '%s' initialized successfully\n",
57 ocf_promotion_policies
[type
].name
);
63 void ocf_promotion_deinit(ocf_promotion_policy_t policy
)
65 ocf_promotion_t type
= policy
->type
;
67 ENV_BUG_ON(type
>= ocf_promotion_max
);
69 if (ocf_promotion_policies
[type
].deinit
)
70 ocf_promotion_policies
[type
].deinit(policy
);
75 ocf_error_t
ocf_promotion_set_policy(ocf_promotion_policy_t policy
,
78 ocf_error_t result
= 0;
79 ocf_cache_t cache
= policy
->owner
;
80 ocf_promotion_t prev_policy
;
82 if (type
>= ocf_promotion_max
)
83 return -OCF_ERR_INVAL
;
85 prev_policy
= cache
->conf_meta
->promotion_policy_type
;
87 if (type
== prev_policy
) {
88 ocf_cache_log(cache
, log_info
, "Promotion policy '%s' is already set\n",
89 ocf_promotion_policies
[type
].name
);
93 if (ocf_promotion_policies
[prev_policy
].deinit
)
94 ocf_promotion_policies
[prev_policy
].deinit(policy
);
96 cache
->conf_meta
->promotion_policy_type
= type
;
99 if (ocf_promotion_policies
[type
].init
)
100 result
= ocf_promotion_policies
[type
].init(cache
);
103 ocf_cache_log(cache
, log_err
,
104 "Error switching to new promotion policy\n");
105 ocf_cache_log(cache
, log_err
,
106 "Falling back to 'always' promotion policy\n");
107 cache
->conf_meta
->promotion_policy_type
= ocf_promotion_always
;
108 policy
->type
= ocf_promotion_always
;
110 ocf_cache_log(cache
, log_info
,
111 "Switched to '%s' promotion policy\n",
112 ocf_promotion_policies
[type
].name
);
118 ocf_error_t
ocf_promotion_set_param(ocf_cache_t cache
, ocf_promotion_t type
,
119 uint8_t param_id
, uint32_t param_value
)
121 ocf_error_t result
= -OCF_ERR_INVAL
;
123 ENV_BUG_ON(type
>= ocf_promotion_max
);
125 if (ocf_promotion_policies
[type
].set_param
) {
126 result
= ocf_promotion_policies
[type
].set_param(cache
, param_id
,
133 ocf_error_t
ocf_promotion_get_param(ocf_cache_t cache
, ocf_promotion_t type
,
134 uint8_t param_id
, uint32_t *param_value
)
136 ocf_error_t result
= -OCF_ERR_INVAL
;
138 ENV_BUG_ON(type
>= ocf_promotion_max
);
140 if (ocf_promotion_policies
[type
].get_param
) {
141 result
= ocf_promotion_policies
[type
].get_param(cache
, param_id
,
148 void ocf_promotion_req_purge(ocf_promotion_policy_t policy
,
149 struct ocf_request
*req
)
151 ocf_promotion_t type
= policy
->type
;
153 ENV_BUG_ON(type
>= ocf_promotion_max
);
155 if (ocf_promotion_policies
[type
].req_purge
)
156 ocf_promotion_policies
[type
].req_purge(policy
, req
);
159 bool ocf_promotion_req_should_promote(ocf_promotion_policy_t policy
,
160 struct ocf_request
*req
)
162 ocf_promotion_t type
= policy
->type
;
165 ENV_BUG_ON(type
>= ocf_promotion_max
);
167 if (ocf_promotion_policies
[type
].req_should_promote
) {
168 result
= ocf_promotion_policies
[type
].req_should_promote(policy
,