Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2018 Intel Corporation.
3 : * All rights reserved.
4 : */
5 :
6 : #include "spdk/stdinc.h"
7 : #include "spdk/nvme.h"
8 : #include "spdk/thread.h"
9 : #include "spdk/string.h"
10 : #include "spdk/likely.h"
11 : #include "spdk/ftl.h"
12 : #include "spdk/likely.h"
13 : #include "spdk/string.h"
14 : #include "spdk/bdev_module.h"
15 : #include "spdk/config.h"
16 :
17 : #include "ftl_core.h"
18 : #include "ftl_io.h"
19 : #include "ftl_band.h"
20 : #include "ftl_debug.h"
21 : #include "ftl_nv_cache.h"
22 : #include "ftl_writer.h"
23 : #include "ftl_utils.h"
24 : #include "mngt/ftl_mngt.h"
25 :
26 : struct ftl_dev_init_ctx {
27 : spdk_ftl_init_fn cb_fn;
28 : /* Callback's argument */
29 : void *cb_arg;
30 : };
31 :
32 : struct ftl_dev_free_ctx {
33 : spdk_ftl_fn cb_fn;
34 : /* Callback's argument */
35 : void *cb_arg;
36 : };
37 :
38 : static int
39 0 : init_core_thread(struct spdk_ftl_dev *dev)
40 : {
41 0 : struct spdk_cpuset cpumask = {};
42 :
43 : /*
44 : * If core mask is provided create core thread on first cpu that match with the mask,
45 : * otherwise use current user thread
46 : */
47 0 : if (dev->conf.core_mask) {
48 0 : if (spdk_cpuset_parse(&cpumask, dev->conf.core_mask)) {
49 0 : return -EINVAL;
50 : }
51 0 : dev->core_thread = spdk_thread_create("ftl_core_thread", &cpumask);
52 : } else {
53 0 : dev->core_thread = spdk_get_thread();
54 : }
55 :
56 0 : if (dev->core_thread == NULL) {
57 0 : FTL_ERRLOG(dev, "Cannot create thread for mask %s\n", dev->conf.core_mask);
58 0 : return -ENOMEM;
59 : }
60 :
61 0 : return 0;
62 : }
63 :
64 : static void
65 0 : exit_thread(void *ctx)
66 : {
67 0 : struct spdk_thread *thread = ctx;
68 :
69 0 : spdk_thread_exit(thread);
70 0 : }
71 :
72 : static void
73 0 : deinit_core_thread(struct spdk_ftl_dev *dev)
74 : {
75 0 : if (dev->core_thread && dev->conf.core_mask) {
76 0 : spdk_thread_send_msg(dev->core_thread, exit_thread,
77 0 : dev->core_thread);
78 0 : dev->core_thread = NULL;
79 : }
80 0 : }
81 :
82 : static void
83 0 : free_dev(struct spdk_ftl_dev *dev)
84 : {
85 0 : if (!dev) {
86 0 : return;
87 : }
88 :
89 0 : deinit_core_thread(dev);
90 0 : spdk_ftl_conf_deinit(&dev->conf);
91 0 : ftl_properties_deinit(dev);
92 0 : free(dev);
93 : }
94 :
95 : static struct spdk_ftl_dev *
96 0 : allocate_dev(const struct spdk_ftl_conf *conf, int *error)
97 : {
98 : int rc;
99 0 : struct spdk_ftl_dev *dev = calloc(1, sizeof(*dev));
100 :
101 0 : if (!dev) {
102 0 : FTL_ERRLOG(dev, "Cannot allocate FTL device\n");
103 0 : *error = -ENOMEM;
104 0 : return NULL;
105 : }
106 :
107 0 : rc = ftl_properties_init(dev);
108 0 : if (rc) {
109 0 : *error = rc;
110 0 : goto error;
111 : }
112 :
113 0 : rc = ftl_conf_init_dev(dev, conf);
114 0 : if (rc) {
115 0 : *error = rc;
116 0 : goto error;
117 : }
118 :
119 0 : rc = init_core_thread(dev);
120 0 : if (rc) {
121 0 : *error = rc;
122 0 : goto error;
123 : }
124 :
125 0 : TAILQ_INIT(&dev->rd_sq);
126 0 : TAILQ_INIT(&dev->wr_sq);
127 0 : TAILQ_INIT(&dev->unmap_sq);
128 0 : TAILQ_INIT(&dev->ioch_queue);
129 :
130 0 : ftl_writer_init(dev, &dev->writer_user, SPDK_FTL_LIMIT_HIGH, FTL_BAND_TYPE_COMPACTION);
131 0 : ftl_writer_init(dev, &dev->writer_gc, SPDK_FTL_LIMIT_CRIT, FTL_BAND_TYPE_GC);
132 :
133 0 : return dev;
134 0 : error:
135 0 : free_dev(dev);
136 0 : return NULL;
137 : }
138 :
139 : static void
140 0 : dev_init_cb(struct spdk_ftl_dev *dev, void *_ctx, int status)
141 : {
142 0 : struct ftl_dev_init_ctx *ctx = _ctx;
143 : int rc;
144 :
145 0 : if (status) {
146 0 : if (dev->init_retry) {
147 0 : FTL_NOTICELOG(dev, "Startup retry\n");
148 0 : rc = spdk_ftl_dev_init(&dev->conf, ctx->cb_fn, ctx->cb_arg);
149 0 : if (!rc) {
150 0 : free_dev(dev);
151 0 : free(ctx);
152 0 : return;
153 : }
154 0 : FTL_NOTICELOG(dev, "Startup retry failed: %d\n", rc);
155 : }
156 :
157 0 : free_dev(dev);
158 0 : dev = NULL;
159 : }
160 0 : ctx->cb_fn(dev, ctx->cb_arg, status);
161 0 : free(ctx);
162 : }
163 :
164 : int
165 0 : spdk_ftl_dev_init(const struct spdk_ftl_conf *conf, spdk_ftl_init_fn cb_fn, void *cb_arg)
166 : {
167 0 : int rc = -1;
168 : struct ftl_dev_init_ctx *ctx;
169 0 : struct spdk_ftl_dev *dev = NULL;
170 :
171 0 : ctx = calloc(1, sizeof(*ctx));
172 0 : if (!ctx) {
173 0 : rc = -ENOMEM;
174 0 : goto error;
175 : }
176 0 : ctx->cb_fn = cb_fn;
177 0 : ctx->cb_arg = cb_arg;
178 :
179 0 : dev = allocate_dev(conf, &rc);
180 0 : if (!dev) {
181 0 : goto error;
182 : }
183 :
184 0 : rc = ftl_mngt_call_dev_startup(dev, dev_init_cb, ctx);
185 0 : if (rc) {
186 0 : goto error;
187 : }
188 :
189 0 : return 0;
190 :
191 0 : error:
192 0 : free(ctx);
193 0 : free_dev(dev);
194 0 : return rc;
195 : }
196 :
197 : static void
198 0 : dev_free_cb(struct spdk_ftl_dev *dev, void *_ctx, int status)
199 : {
200 0 : struct ftl_dev_free_ctx *ctx = _ctx;
201 :
202 0 : if (!status) {
203 0 : free_dev(dev);
204 : }
205 0 : ctx->cb_fn(ctx->cb_arg, status);
206 0 : free(ctx);
207 0 : }
208 :
209 : int
210 0 : spdk_ftl_dev_free(struct spdk_ftl_dev *dev, spdk_ftl_fn cb_fn, void *cb_arg)
211 : {
212 0 : int rc = -1;
213 : struct ftl_dev_free_ctx *ctx;
214 :
215 0 : ctx = calloc(1, sizeof(*ctx));
216 0 : if (!ctx) {
217 0 : rc = -ENOMEM;
218 0 : goto error;
219 : }
220 0 : ctx->cb_fn = cb_fn;
221 0 : ctx->cb_arg = cb_arg;
222 :
223 0 : rc = ftl_mngt_call_dev_shutdown(dev, dev_free_cb, ctx);
224 0 : if (rc) {
225 0 : goto error;
226 : }
227 :
228 0 : return 0;
229 :
230 0 : error:
231 0 : free(ctx);
232 0 : return rc;
233 : }
234 :
235 0 : SPDK_LOG_REGISTER_COMPONENT(ftl_init)
|