xref: /spdk/module/bdev/ocf/utils.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/log.h"
36 
37 #include "utils.h"
38 #include "vbdev_ocf.h"
39 
40 static char *cache_modes[ocf_cache_mode_max] = {
41 	[ocf_cache_mode_wt] = "wt",
42 	[ocf_cache_mode_wb] = "wb",
43 	[ocf_cache_mode_wa] = "wa",
44 	[ocf_cache_mode_pt] = "pt",
45 	[ocf_cache_mode_wi] = "wi",
46 	[ocf_cache_mode_wo] = "wo",
47 };
48 
49 static char *seqcutoff_policies[ocf_seq_cutoff_policy_max] = {
50 	[ocf_seq_cutoff_policy_always] = "always",
51 	[ocf_seq_cutoff_policy_full] = "full",
52 	[ocf_seq_cutoff_policy_never] = "never",
53 };
54 
55 ocf_cache_mode_t
56 ocf_get_cache_mode(const char *cache_mode)
57 {
58 	int i;
59 
60 	for (i = 0; i < ocf_cache_mode_max; i++) {
61 		if (strcmp(cache_mode, cache_modes[i]) == 0) {
62 			return i;
63 		}
64 	}
65 
66 	return ocf_cache_mode_none;
67 }
68 
69 const char *
70 ocf_get_cache_modename(ocf_cache_mode_t mode)
71 {
72 	if (mode > ocf_cache_mode_none && mode < ocf_cache_mode_max) {
73 		return cache_modes[mode];
74 	} else {
75 		return NULL;
76 	}
77 }
78 
79 int
80 ocf_get_cache_line_size(ocf_cache_t cache)
81 {
82 	return ocf_cache_get_line_size(cache) / KiB;
83 }
84 
85 ocf_seq_cutoff_policy
86 ocf_get_seqcutoff_policy(const char *policy_name)
87 {
88 	int policy;
89 
90 	for (policy = 0; policy < ocf_seq_cutoff_policy_max; policy++)
91 		if (!strcmp(policy_name, seqcutoff_policies[policy])) {
92 			return policy;
93 		}
94 
95 	return ocf_seq_cutoff_policy_max;
96 }
97 
98 int
99 vbdev_ocf_mngt_start(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *path,
100 		     vbdev_ocf_mngt_callback cb, void *cb_arg)
101 {
102 	if (vbdev->mngt_ctx.current_step) {
103 		return -EBUSY;
104 	}
105 
106 	memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx));
107 
108 	vbdev->mngt_ctx.current_step = path;
109 	vbdev->mngt_ctx.cb = cb;
110 	vbdev->mngt_ctx.cb_arg = cb_arg;
111 
112 	(*vbdev->mngt_ctx.current_step)(vbdev);
113 
114 	return 0;
115 }
116 
117 void
118 vbdev_ocf_mngt_stop(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *rollback_path, int status)
119 {
120 	if (status) {
121 		vbdev->mngt_ctx.status = status;
122 	}
123 
124 	if (vbdev->mngt_ctx.status && rollback_path) {
125 		vbdev->mngt_ctx.poller_fn = NULL;
126 		vbdev->mngt_ctx.current_step = rollback_path;
127 		(*vbdev->mngt_ctx.current_step)(vbdev);
128 		return;
129 	}
130 
131 	if (vbdev->mngt_ctx.cb) {
132 		vbdev->mngt_ctx.cb(vbdev->mngt_ctx.status, vbdev, vbdev->mngt_ctx.cb_arg);
133 	}
134 
135 	memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx));
136 }
137 
138 void
139 vbdev_ocf_mngt_continue(struct vbdev_ocf *vbdev, int status)
140 {
141 	if (vbdev->mngt_ctx.current_step == NULL) {
142 		return;
143 	}
144 
145 	assert((*vbdev->mngt_ctx.current_step) != NULL);
146 
147 	vbdev->mngt_ctx.status = status;
148 
149 	vbdev->mngt_ctx.current_step++;
150 	if (*vbdev->mngt_ctx.current_step) {
151 		(*vbdev->mngt_ctx.current_step)(vbdev);
152 		return;
153 	}
154 
155 	vbdev_ocf_mngt_stop(vbdev, NULL, 0);
156 }
157 
158 int
159 vbdev_ocf_mngt_get_status(struct vbdev_ocf *vbdev)
160 {
161 	return vbdev->mngt_ctx.status;
162 }
163