Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2015 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2020 Mellanox Technologies LTD. All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/config.h"
7 : : #include "spdk/nvmf_spec.h"
8 : : #include "spdk/string.h"
9 : : #include "spdk/env.h"
10 : : #include "nvme_internal.h"
11 : : #include "nvme_io_msg.h"
12 : :
13 : : #define SPDK_NVME_DRIVER_NAME "spdk_nvme_driver"
14 : :
15 : : struct nvme_driver *g_spdk_nvme_driver;
16 : : pid_t g_spdk_nvme_pid;
17 : :
18 : : /* gross timeout of 180 seconds in milliseconds */
19 : : static int g_nvme_driver_timeout_ms = 3 * 60 * 1000;
20 : :
21 : : /* Per-process attached controller list */
22 : : static TAILQ_HEAD(, spdk_nvme_ctrlr) g_nvme_attached_ctrlrs =
23 : : TAILQ_HEAD_INITIALIZER(g_nvme_attached_ctrlrs);
24 : :
25 : : /* Returns true if ctrlr should be stored on the multi-process shared_attached_ctrlrs list */
26 : : static bool
27 : 6166 : nvme_ctrlr_shared(const struct spdk_nvme_ctrlr *ctrlr)
28 : : {
29 [ + - + - : 6166 : return ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_PCIE;
+ - ]
30 : : }
31 : :
32 : : void
33 : 41 : nvme_ctrlr_connected(struct spdk_nvme_probe_ctx *probe_ctx,
34 : : struct spdk_nvme_ctrlr *ctrlr)
35 : : {
36 [ # # # # : 41 : TAILQ_INSERT_TAIL(&probe_ctx->init_ctrlrs, ctrlr, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
37 : 41 : }
38 : :
39 : : static void
40 : 3093 : nvme_ctrlr_detach_async_finish(struct spdk_nvme_ctrlr *ctrlr)
41 : : {
42 [ + - ]: 3093 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
43 [ + + ]: 3093 : if (nvme_ctrlr_shared(ctrlr)) {
44 [ + + + - : 726 : TAILQ_REMOVE(&g_spdk_nvme_driver->shared_attached_ctrlrs, ctrlr, tailq);
+ - + - #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
45 : 2 : } else {
46 [ + + + - : 2367 : TAILQ_REMOVE(&g_nvme_attached_ctrlrs, ctrlr, tailq);
+ - + - #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
47 : : }
48 [ + - ]: 3093 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
49 : 3093 : }
50 : :
51 : : static int
52 : 3321 : nvme_ctrlr_detach_async(struct spdk_nvme_ctrlr *ctrlr,
53 : : struct nvme_ctrlr_detach_ctx **_ctx)
54 : : {
55 : : struct nvme_ctrlr_detach_ctx *ctx;
56 : : int ref_count;
57 : :
58 [ + - ]: 3321 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
59 : :
60 : 3321 : ref_count = nvme_ctrlr_get_ref_count(ctrlr);
61 [ + + # # ]: 3321 : assert(ref_count > 0);
62 : :
63 [ + + ]: 3321 : if (ref_count == 1) {
64 : : /* This is the last reference to the controller, so we need to
65 : : * allocate a context to destruct it.
66 : : */
67 : 3093 : ctx = calloc(1, sizeof(*ctx));
68 [ + + ]: 3093 : if (ctx == NULL) {
69 [ # # ]: 0 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
70 : :
71 : 0 : return -ENOMEM;
72 : : }
73 [ + - + - ]: 3093 : ctx->ctrlr = ctrlr;
74 [ + - + - ]: 3093 : ctx->cb_fn = nvme_ctrlr_detach_async_finish;
75 : :
76 : 3093 : nvme_ctrlr_proc_put_ref(ctrlr);
77 : :
78 : 3093 : nvme_io_msg_ctrlr_detach(ctrlr);
79 : :
80 : 3093 : nvme_ctrlr_destruct_async(ctrlr, ctx);
81 : :
82 [ + - ]: 3093 : *_ctx = ctx;
83 : 797 : } else {
84 : 228 : nvme_ctrlr_proc_put_ref(ctrlr);
85 : : }
86 : :
87 [ + - ]: 3321 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
88 : :
89 : 3321 : return 0;
90 : 797 : }
91 : :
92 : : static int
93 : 18402367 : nvme_ctrlr_detach_poll_async(struct nvme_ctrlr_detach_ctx *ctx)
94 : : {
95 : : int rc;
96 : :
97 [ + - + - ]: 18402367 : rc = nvme_ctrlr_destruct_poll_async(ctx->ctrlr, ctx);
98 [ + + ]: 18402367 : if (rc == -EAGAIN) {
99 : 18399274 : return -EAGAIN;
100 : : }
101 : :
102 : 3093 : free(ctx);
103 : :
104 : 3093 : return rc;
105 : 1187439 : }
106 : :
107 : : int
108 : 400 : spdk_nvme_detach(struct spdk_nvme_ctrlr *ctrlr)
109 : : {
110 : 400 : struct nvme_ctrlr_detach_ctx *ctx = NULL;
111 : : int rc;
112 : :
113 : 400 : rc = nvme_ctrlr_detach_async(ctrlr, &ctx);
114 [ - + ]: 400 : if (rc != 0) {
115 : 0 : return rc;
116 [ + + ]: 400 : } else if (ctx == NULL) {
117 : : /* ctrlr was detached from the caller process but any other process
118 : : * still attaches it.
119 : : */
120 : 3 : return 0;
121 : : }
122 : :
123 : 35 : while (1) {
124 : 2882 : rc = nvme_ctrlr_detach_poll_async(ctx);
125 [ + + ]: 2882 : if (rc != -EAGAIN) {
126 : 397 : break;
127 : : }
128 : 2485 : nvme_delay(1000);
129 : : }
130 : :
131 : 397 : return 0;
132 : 7 : }
133 : :
134 : : int
135 : 2921 : spdk_nvme_detach_async(struct spdk_nvme_ctrlr *ctrlr,
136 : : struct spdk_nvme_detach_ctx **_detach_ctx)
137 : : {
138 : : struct spdk_nvme_detach_ctx *detach_ctx;
139 : 2921 : struct nvme_ctrlr_detach_ctx *ctx = NULL;
140 : : int rc;
141 : :
142 [ + - - + ]: 2921 : if (ctrlr == NULL || _detach_ctx == NULL) {
143 : 0 : return -EINVAL;
144 : : }
145 : :
146 : : /* Use a context header to poll detachment for multiple controllers.
147 : : * Allocate an new one if not allocated yet, or use the passed one otherwise.
148 : : */
149 [ + - ]: 2921 : detach_ctx = *_detach_ctx;
150 [ + + ]: 2921 : if (detach_ctx == NULL) {
151 : 2877 : detach_ctx = calloc(1, sizeof(*detach_ctx));
152 [ + + ]: 2877 : if (detach_ctx == NULL) {
153 : 0 : return -ENOMEM;
154 : : }
155 [ + - + - : 2877 : TAILQ_INIT(&detach_ctx->head);
+ - + - +
- + - + -
+ - ]
156 : 790 : }
157 : :
158 : 2921 : rc = nvme_ctrlr_detach_async(ctrlr, &ctx);
159 [ + - + + ]: 2921 : if (rc != 0 || ctx == NULL) {
160 : : /* If this detach failed and the context header is empty, it means we just
161 : : * allocated the header and need to free it before returning.
162 : : */
163 [ + - # # : 225 : if (TAILQ_EMPTY(&detach_ctx->head)) {
# # # # ]
164 : 225 : free(detach_ctx);
165 : 0 : }
166 : 225 : return rc;
167 : : }
168 : :
169 : : /* Append a context for this detachment to the context header. */
170 [ + - + - : 2696 : TAILQ_INSERT_TAIL(&detach_ctx->head, ctx, link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
171 : :
172 [ + - ]: 2696 : *_detach_ctx = detach_ctx;
173 : :
174 : 2696 : return 0;
175 : 790 : }
176 : :
177 : : int
178 : 18399373 : spdk_nvme_detach_poll_async(struct spdk_nvme_detach_ctx *detach_ctx)
179 : : {
180 : : struct nvme_ctrlr_detach_ctx *ctx, *tmp_ctx;
181 : : int rc;
182 : :
183 [ + + ]: 18399373 : if (detach_ctx == NULL) {
184 : 0 : return -EINVAL;
185 : : }
186 : :
187 [ + + + - : 36798858 : TAILQ_FOREACH_SAFE(ctx, &detach_ctx->head, link, tmp_ctx) {
+ - + + +
- + - + -
+ + ]
188 [ + + + - : 18399485 : TAILQ_REMOVE(&detach_ctx->head, ctx, link);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
189 : :
190 : 18399485 : rc = nvme_ctrlr_detach_poll_async(ctx);
191 [ + + ]: 18399485 : if (rc == -EAGAIN) {
192 : : /* If not -EAGAIN, ctx was freed by nvme_ctrlr_detach_poll_async(). */
193 [ + + + - : 18396789 : TAILQ_INSERT_HEAD(&detach_ctx->head, ctx, link);
+ - + - +
- + - - +
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
194 : 1186614 : }
195 : 1187404 : }
196 : :
197 [ + + + - : 18399373 : if (!TAILQ_EMPTY(&detach_ctx->head)) {
+ - + + ]
198 : 18396721 : return -EAGAIN;
199 : : }
200 : :
201 : 2652 : free(detach_ctx);
202 : 2652 : return 0;
203 : 1187404 : }
204 : :
205 : : void
206 : 1074 : spdk_nvme_detach_poll(struct spdk_nvme_detach_ctx *detach_ctx)
207 : : {
208 [ + + + + ]: 18210133 : while (detach_ctx && spdk_nvme_detach_poll_async(detach_ctx) == -EAGAIN) {
209 : : ;
210 : : }
211 : 1074 : }
212 : :
213 : : void
214 : 17631 : nvme_completion_poll_cb(void *arg, const struct spdk_nvme_cpl *cpl)
215 : : {
216 : 17631 : struct nvme_completion_poll_status *status = arg;
217 : :
218 [ + + + + : 17631 : if (status->timed_out) {
+ - - + ]
219 : : /* There is no routine waiting for the completion of this request, free allocated memory */
220 [ # # # # ]: 0 : spdk_free(status->dma_data);
221 : 0 : free(status);
222 : 0 : return;
223 : : }
224 : :
225 : : /*
226 : : * Copy status into the argument passed by the caller, so that
227 : : * the caller can check the status to determine if the
228 : : * the request passed or failed.
229 : : */
230 [ + - + - : 17631 : memcpy(&status->cpl, cpl, sizeof(*cpl));
+ - ]
231 [ + - + - ]: 17631 : status->done = true;
232 : 1594 : }
233 : :
234 : : static void
235 : 0 : dummy_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
236 : : {
237 : 0 : }
238 : :
239 : : int
240 : 3581132 : nvme_wait_for_completion_robust_lock_timeout_poll(struct spdk_nvme_qpair *qpair,
241 : : struct nvme_completion_poll_status *status,
242 : : pthread_mutex_t *robust_mutex)
243 : : {
244 : : int rc;
245 : :
246 [ + + ]: 3581132 : if (robust_mutex) {
247 : 745939 : nvme_robust_mutex_lock(robust_mutex);
248 : 0 : }
249 : :
250 [ + + + - : 3581132 : if (qpair->poll_group) {
+ - ]
251 [ # # # # : 1260268 : rc = (int)spdk_nvme_poll_group_process_completions(qpair->poll_group->group, 0,
# # # # ]
252 : : dummy_disconnected_qpair_cb);
253 : 0 : } else {
254 : 2320864 : rc = spdk_nvme_qpair_process_completions(qpair, 0);
255 : : }
256 : :
257 [ + + ]: 3581132 : if (robust_mutex) {
258 : 745939 : nvme_robust_mutex_unlock(robust_mutex);
259 : 0 : }
260 : :
261 [ + + ]: 3581132 : if (rc < 0) {
262 [ # # # # : 12 : status->cpl.status.sct = SPDK_NVME_SCT_GENERIC;
# # # # ]
263 [ # # # # : 12 : status->cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
# # # # ]
264 : 12 : goto error;
265 : : }
266 : :
267 [ + + + + : 3581120 : if (!status->done && status->timeout_tsc && spdk_get_ticks() > status->timeout_tsc) {
+ + + + +
- + - - +
# # # # #
# ]
268 : 6 : goto error;
269 : : }
270 : :
271 [ + + + - : 3581114 : if (qpair->ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
+ - + - +
- + + ]
272 [ + - + - ]: 28262 : union spdk_nvme_csts_register csts = spdk_nvme_ctrlr_get_regs_csts(qpair->ctrlr);
273 [ + + ]: 28262 : if (csts.raw == SPDK_NVME_INVALID_REGISTER_VALUE) {
274 [ # # # # : 0 : status->cpl.status.sct = SPDK_NVME_SCT_GENERIC;
# # # # ]
275 [ # # # # : 0 : status->cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
# # # # ]
276 : 0 : goto error;
277 : : }
278 : 33 : }
279 : :
280 [ + + + + : 3581114 : if (!status->done) {
+ - + + ]
281 : 3563474 : return -EAGAIN;
282 [ + + + + : 17640 : } else if (spdk_nvme_cpl_is_error(&status->cpl)) {
+ - + - +
- - + - +
- + - + -
+ ]
283 : 264 : return -EIO;
284 : : } else {
285 : 17376 : return 0;
286 : : }
287 : 18 : error:
288 : : /* Either transport error occurred or we've timed out. Either way, if the response hasn't
289 : : * been received yet, mark the command as timed out, so the status gets freed when the
290 : : * command is completed or aborted.
291 : : */
292 [ + + + - : 18 : if (!status->done) {
# # # # ]
293 [ # # # # ]: 18 : status->timed_out = true;
294 : 0 : }
295 : :
296 : 18 : return -ECANCELED;
297 : 46906 : }
298 : :
299 : : /**
300 : : * Poll qpair for completions until a command completes.
301 : : *
302 : : * \param qpair queue to poll
303 : : * \param status completion status. The user must fill this structure with zeroes before calling
304 : : * this function
305 : : * \param robust_mutex optional robust mutex to lock while polling qpair
306 : : * \param timeout_in_usecs optional timeout
307 : : *
308 : : * \return 0 if command completed without error,
309 : : * -EIO if command completed with error,
310 : : * -ECANCELED if command is not completed due to transport/device error or time expired
311 : : *
312 : : * The command to wait upon must be submitted with nvme_completion_poll_cb as the callback
313 : : * and status as the callback argument.
314 : : */
315 : : int
316 : 6103 : nvme_wait_for_completion_robust_lock_timeout(
317 : : struct spdk_nvme_qpair *qpair,
318 : : struct nvme_completion_poll_status *status,
319 : : pthread_mutex_t *robust_mutex,
320 : : uint64_t timeout_in_usecs)
321 : : {
322 : : int rc;
323 : :
324 [ + + ]: 6103 : if (timeout_in_usecs) {
325 [ # # # # ]: 370 : status->timeout_tsc = spdk_get_ticks() + timeout_in_usecs *
326 [ # # # # ]: 188 : spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
327 : 0 : } else {
328 [ + - + - ]: 5915 : status->timeout_tsc = 0;
329 : : }
330 : :
331 [ + - + - : 6103 : status->cpl.status_raw = 0;
+ - + - ]
332 : 18 : do {
333 : 1853274 : rc = nvme_wait_for_completion_robust_lock_timeout_poll(qpair, status, robust_mutex);
334 [ + + ]: 1853274 : } while (rc == -EAGAIN);
335 : :
336 : 6103 : return rc;
337 : : }
338 : :
339 : : /**
340 : : * Poll qpair for completions until a command completes.
341 : : *
342 : : * \param qpair queue to poll
343 : : * \param status completion status. The user must fill this structure with zeroes before calling
344 : : * this function
345 : : * \param robust_mutex optional robust mutex to lock while polling qpair
346 : : *
347 : : * \return 0 if command completed without error,
348 : : * -EIO if command completed with error,
349 : : * -ECANCELED if command is not completed due to transport/device error
350 : : *
351 : : * The command to wait upon must be submitted with nvme_completion_poll_cb as the callback
352 : : * and status as the callback argument.
353 : : */
354 : : int
355 : 2231 : nvme_wait_for_completion_robust_lock(
356 : : struct spdk_nvme_qpair *qpair,
357 : : struct nvme_completion_poll_status *status,
358 : : pthread_mutex_t *robust_mutex)
359 : : {
360 : 2231 : return nvme_wait_for_completion_robust_lock_timeout(qpair, status, robust_mutex, 0);
361 : : }
362 : :
363 : : int
364 : 3684 : nvme_wait_for_completion(struct spdk_nvme_qpair *qpair,
365 : : struct nvme_completion_poll_status *status)
366 : : {
367 : 3684 : return nvme_wait_for_completion_robust_lock_timeout(qpair, status, NULL, 0);
368 : : }
369 : :
370 : : /**
371 : : * Poll qpair for completions until a command completes.
372 : : *
373 : : * \param qpair queue to poll
374 : : * \param status completion status. The user must fill this structure with zeroes before calling
375 : : * this function
376 : : * \param timeout_in_usecs optional timeout
377 : : *
378 : : * \return 0 if command completed without error,
379 : : * -EIO if command completed with error,
380 : : * -ECANCELED if command is not completed due to transport/device error or time expired
381 : : *
382 : : * The command to wait upon must be submitted with nvme_completion_poll_cb as the callback
383 : : * and status as the callback argument.
384 : : */
385 : : int
386 : 166 : nvme_wait_for_completion_timeout(struct spdk_nvme_qpair *qpair,
387 : : struct nvme_completion_poll_status *status,
388 : : uint64_t timeout_in_usecs)
389 : : {
390 : 166 : return nvme_wait_for_completion_robust_lock_timeout(qpair, status, NULL, timeout_in_usecs);
391 : : }
392 : :
393 : : static void
394 : 277541 : nvme_user_copy_cmd_complete(void *arg, const struct spdk_nvme_cpl *cpl)
395 : : {
396 : 277541 : struct nvme_request *req = arg;
397 : : spdk_nvme_cmd_cb user_cb_fn;
398 : : void *user_cb_arg;
399 : : enum spdk_nvme_data_transfer xfer;
400 : :
401 [ + + + - : 277541 : if (req->user_buffer && req->payload_size) {
+ + + - +
- - + ]
402 : : /* Copy back to the user buffer */
403 [ + + + - : 268560 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
# # ]
404 [ + - + - ]: 268560 : xfer = spdk_nvme_opc_get_data_transfer(req->cmd.opc);
405 [ + + - + ]: 268560 : if (xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST ||
406 : 0 : xfer == SPDK_NVME_DATA_BIDIRECTIONAL) {
407 [ + + + - : 17222 : assert(req->pid == getpid());
+ - # # ]
408 [ + + + + : 17222 : memcpy(req->user_buffer, req->payload.contig_or_cb_arg, req->payload_size);
+ - + - +
- + - + -
+ - + - ]
409 : 3188 : }
410 : 3188 : }
411 : :
412 [ + - + - ]: 277541 : user_cb_fn = req->user_cb_fn;
413 [ + - + - ]: 277541 : user_cb_arg = req->user_cb_arg;
414 : 277541 : nvme_cleanup_user_req(req);
415 : :
416 : : /* Call the user's original callback now that the buffer has been copied */
417 [ - + + - ]: 277541 : user_cb_fn(user_cb_arg, cpl);
418 : :
419 : 277541 : }
420 : :
421 : : /**
422 : : * Allocate a request as well as a DMA-capable buffer to copy to/from the user's buffer.
423 : : *
424 : : * This is intended for use in non-fast-path functions (admin commands, reservations, etc.)
425 : : * where the overhead of a copy is not a problem.
426 : : */
427 : : struct nvme_request *
428 : 280320 : nvme_allocate_request_user_copy(struct spdk_nvme_qpair *qpair,
429 : : void *buffer, uint32_t payload_size, spdk_nvme_cmd_cb cb_fn,
430 : : void *cb_arg, bool host_to_controller)
431 : : {
432 : : struct nvme_request *req;
433 : 280320 : void *dma_buffer = NULL;
434 : :
435 [ + + + + ]: 280320 : if (buffer && payload_size) {
436 : 271339 : dma_buffer = spdk_zmalloc(payload_size, 4096, NULL,
437 : : SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
438 [ + + ]: 271339 : if (!dma_buffer) {
439 : 3 : return NULL;
440 : : }
441 : :
442 [ + + + - ]: 271336 : if (host_to_controller) {
443 [ - + - + ]: 254104 : memcpy(dma_buffer, buffer, payload_size);
444 : 0 : }
445 : 3188 : }
446 : :
447 : 280317 : req = nvme_allocate_request_contig(qpair, dma_buffer, payload_size, nvme_user_copy_cmd_complete,
448 : : NULL);
449 [ + + ]: 280317 : if (!req) {
450 : 2749 : spdk_free(dma_buffer);
451 : 2749 : return NULL;
452 : : }
453 : :
454 [ + - + - ]: 277568 : req->user_cb_fn = cb_fn;
455 [ + - + - ]: 277568 : req->user_cb_arg = cb_arg;
456 [ + - + - ]: 277568 : req->user_buffer = buffer;
457 [ + - + - ]: 277568 : req->cb_arg = req;
458 : :
459 : 277568 : return req;
460 : 5577 : }
461 : :
462 : : /**
463 : : * Check if a request has exceeded the controller timeout.
464 : : *
465 : : * \param req request to check for timeout.
466 : : * \param cid command ID for command submitted by req (will be passed to timeout_cb_fn)
467 : : * \param active_proc per-process data for the controller associated with req
468 : : * \param now_tick current time from spdk_get_ticks()
469 : : * \return 0 if requests submitted more recently than req should still be checked for timeouts, or
470 : : * 1 if requests newer than req need not be checked.
471 : : *
472 : : * The request's timeout callback will be called if needed; the caller is only responsible for
473 : : * calling this function on each outstanding request.
474 : : */
475 : : int
476 : 172603 : nvme_request_check_timeout(struct nvme_request *req, uint16_t cid,
477 : : struct spdk_nvme_ctrlr_process *active_proc,
478 : : uint64_t now_tick)
479 : : {
480 [ # # # # ]: 172603 : struct spdk_nvme_qpair *qpair = req->qpair;
481 [ # # # # ]: 172603 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
482 [ # # ]: 172603 : uint64_t timeout_ticks = nvme_qpair_is_admin_queue(qpair) ?
483 [ + + # # : 172603 : active_proc->timeout_admin_ticks : active_proc->timeout_io_ticks;
# # # # ]
484 : :
485 [ - + # # : 172603 : assert(active_proc->timeout_cb_fn != NULL);
# # # # ]
486 : :
487 [ + + + + : 172603 : if (req->timed_out || req->submit_tick == 0) {
# # # # #
# ]
488 : 2334 : return 0;
489 : : }
490 : :
491 [ + + # # : 170269 : if (req->pid != g_spdk_nvme_pid) {
# # ]
492 : 3 : return 0;
493 : : }
494 : :
495 [ + + # # ]: 170266 : if (nvme_qpair_is_admin_queue(qpair) &&
496 [ + + # # ]: 529 : req->cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
497 : 3 : return 0;
498 : : }
499 : :
500 [ + + # # : 170263 : if (req->submit_tick + timeout_ticks > now_tick) {
# # ]
501 : 170260 : return 1;
502 : : }
503 : :
504 [ # # ]: 3 : req->timed_out = true;
505 : :
506 : : /*
507 : : * We don't want to expose the admin queue to the user,
508 : : * so when we're timing out admin commands set the
509 : : * qpair to NULL.
510 : : */
511 [ + - # # : 5 : active_proc->timeout_cb_fn(active_proc->timeout_cb_arg, ctrlr,
# # # # #
# # # ]
512 [ # # ]: 3 : nvme_qpair_is_admin_queue(qpair) ? NULL : qpair,
513 : 0 : cid);
514 : 3 : return 0;
515 : 0 : }
516 : :
517 : : int
518 : 972 : nvme_robust_mutex_init_shared(pthread_mutex_t *mtx)
519 : : {
520 : 972 : int rc = 0;
521 : :
522 : : #ifdef __FreeBSD__
523 : : pthread_mutex_init(mtx, NULL);
524 : : #else
525 : 288 : pthread_mutexattr_t attr;
526 : :
527 [ + + + + ]: 972 : if (pthread_mutexattr_init(&attr)) {
528 : 6 : return -1;
529 : : }
530 [ + + + - : 1932 : if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) ||
+ + ]
531 [ + + + + ]: 1898 : pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST) ||
532 [ + + ]: 966 : pthread_mutex_init(mtx, &attr)) {
533 : 3 : rc = -1;
534 : 0 : }
535 [ + + ]: 966 : pthread_mutexattr_destroy(&attr);
536 : : #endif
537 : :
538 : 966 : return rc;
539 : 34 : }
540 : :
541 : : int
542 : 196856 : nvme_driver_init(void)
543 : : {
544 : : static pthread_mutex_t g_init_mutex = PTHREAD_MUTEX_INITIALIZER;
545 : 196856 : int ret = 0;
546 : :
547 : : /* Use a special process-private mutex to ensure the global
548 : : * nvme driver object (g_spdk_nvme_driver) gets initialized by
549 : : * only one thread. Once that object is established and its
550 : : * mutex is initialized, we can unlock this mutex and use that
551 : : * one instead.
552 : : */
553 [ + + ]: 196856 : pthread_mutex_lock(&g_init_mutex);
554 : :
555 : : /* Each process needs its own pid. */
556 : 196856 : g_spdk_nvme_pid = getpid();
557 : :
558 : : /*
559 : : * Only one thread from one process will do this driver init work.
560 : : * The primary process will reserve the shared memory and do the
561 : : * initialization.
562 : : * The secondary process will lookup the existing reserved memory.
563 : : */
564 [ + + ]: 196856 : if (spdk_process_is_primary()) {
565 : : /* The unique named memzone already reserved. */
566 [ + + ]: 196612 : if (g_spdk_nvme_driver != NULL) {
567 [ + + ]: 195646 : pthread_mutex_unlock(&g_init_mutex);
568 : 195646 : return 0;
569 : : } else {
570 : 966 : g_spdk_nvme_driver = spdk_memzone_reserve(SPDK_NVME_DRIVER_NAME,
571 : : sizeof(struct nvme_driver), SPDK_ENV_NUMA_ID_ANY,
572 : : SPDK_MEMZONE_NO_IOVA_CONTIG);
573 : : }
574 : :
575 [ + + ]: 966 : if (g_spdk_nvme_driver == NULL) {
576 : 3 : SPDK_ERRLOG("primary process failed to reserve memory\n");
577 [ - + ]: 3 : pthread_mutex_unlock(&g_init_mutex);
578 : 3 : return -1;
579 : : }
580 : 34 : } else {
581 : 244 : g_spdk_nvme_driver = spdk_memzone_lookup(SPDK_NVME_DRIVER_NAME);
582 : :
583 : : /* The unique named memzone already reserved by the primary process. */
584 [ + + ]: 244 : if (g_spdk_nvme_driver != NULL) {
585 : 235 : int ms_waited = 0;
586 : :
587 : : /* Wait the nvme driver to get initialized. */
588 [ + + + + : 535 : while ((g_spdk_nvme_driver->initialized == false) &&
# # # # #
# ]
589 [ + + ]: 303 : (ms_waited < g_nvme_driver_timeout_ms)) {
590 [ # # ]: 300 : ms_waited++;
591 : 300 : nvme_delay(1000); /* delay 1ms */
592 : : }
593 [ + + + + : 235 : if (g_spdk_nvme_driver->initialized == false) {
# # # # ]
594 : 3 : SPDK_ERRLOG("timeout waiting for primary process to init\n");
595 [ - + ]: 3 : pthread_mutex_unlock(&g_init_mutex);
596 : 3 : return -1;
597 : : }
598 : 0 : } else {
599 : 9 : SPDK_ERRLOG("primary process is not started yet\n");
600 [ - + ]: 9 : pthread_mutex_unlock(&g_init_mutex);
601 : 9 : return -1;
602 : : }
603 : :
604 [ - + ]: 232 : pthread_mutex_unlock(&g_init_mutex);
605 : 232 : return 0;
606 : : }
607 : :
608 : : /*
609 : : * At this moment, only one thread from the primary process will do
610 : : * the g_spdk_nvme_driver initialization
611 : : */
612 [ + + # # ]: 963 : assert(spdk_process_is_primary());
613 : :
614 [ + - ]: 963 : ret = nvme_robust_mutex_init_shared(&g_spdk_nvme_driver->lock);
615 [ + + ]: 963 : if (ret != 0) {
616 : 3 : SPDK_ERRLOG("failed to initialize mutex\n");
617 : 3 : spdk_memzone_free(SPDK_NVME_DRIVER_NAME);
618 [ - + ]: 3 : pthread_mutex_unlock(&g_init_mutex);
619 : 3 : return ret;
620 : : }
621 : :
622 : : /* The lock in the shared g_spdk_nvme_driver object is now ready to
623 : : * be used - so we can unlock the g_init_mutex here.
624 : : */
625 [ + + ]: 960 : pthread_mutex_unlock(&g_init_mutex);
626 [ + - ]: 960 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
627 : :
628 [ + - + - ]: 960 : g_spdk_nvme_driver->initialized = false;
629 [ + - + - ]: 960 : g_spdk_nvme_driver->hotplug_fd = spdk_pci_event_listen();
630 [ + + + - : 960 : if (g_spdk_nvme_driver->hotplug_fd < 0) {
+ - ]
631 [ # # # # : 0 : SPDK_DEBUGLOG(nvme, "Failed to open uevent netlink socket\n");
# # ]
632 : 0 : }
633 : :
634 [ + - + - : 960 : TAILQ_INIT(&g_spdk_nvme_driver->shared_attached_ctrlrs);
+ - + - +
- + - + -
+ - ]
635 : :
636 [ + - ]: 960 : spdk_uuid_generate(&g_spdk_nvme_driver->default_extended_host_id);
637 : :
638 [ + - ]: 960 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
639 : :
640 : 960 : return ret;
641 : 2393 : }
642 : :
643 : : /* This function must only be called while holding g_spdk_nvme_driver->lock */
644 : : int
645 : 3124 : nvme_ctrlr_probe(const struct spdk_nvme_transport_id *trid,
646 : : struct spdk_nvme_probe_ctx *probe_ctx, void *devhandle)
647 : : {
648 : : struct spdk_nvme_ctrlr *ctrlr;
649 : 422 : struct spdk_nvme_ctrlr_opts opts;
650 : :
651 [ + + # # ]: 3124 : assert(trid != NULL);
652 : :
653 : 3124 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&opts, sizeof(opts));
654 : :
655 [ + + + + : 3124 : if (!probe_ctx->probe_cb || probe_ctx->probe_cb(probe_ctx->cb_ctx, trid, &opts)) {
+ + + - +
- - + + -
+ - + - +
- ]
656 : 3099 : ctrlr = nvme_get_ctrlr_by_trid_unsafe(trid, opts.hostnqn);
657 [ + + ]: 3099 : if (ctrlr) {
658 : : /* This ctrlr already exists. */
659 : :
660 [ + + + - : 3 : if (ctrlr->is_destructed) {
# # # # ]
661 : : /* This ctrlr is being destructed asynchronously. */
662 [ # # ]: 3 : SPDK_ERRLOG("NVMe controller for SSD: %s is being destructed\n",
663 : : trid->traddr);
664 [ # # # # : 3 : probe_ctx->attach_fail_cb(probe_ctx->cb_ctx, trid, -EBUSY);
# # # # #
# # # ]
665 : 3 : return -EBUSY;
666 : : }
667 : :
668 : : /* Increase the ref count before calling attach_cb() as the user may
669 : : * call nvme_detach() immediately. */
670 : 0 : nvme_ctrlr_proc_get_ref(ctrlr);
671 : :
672 [ # # # # : 0 : if (probe_ctx->attach_cb) {
# # ]
673 [ # # ]: 0 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
674 [ # # # # : 0 : probe_ctx->attach_cb(probe_ctx->cb_ctx, &ctrlr->trid, ctrlr, &ctrlr->opts);
# # # # #
# # # # #
# # ]
675 [ # # ]: 0 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
676 : 0 : }
677 : 0 : return 0;
678 : : }
679 : :
680 : 3096 : ctrlr = nvme_transport_ctrlr_construct(trid, &opts, devhandle);
681 [ + + ]: 3096 : if (ctrlr == NULL) {
682 [ # # ]: 9 : SPDK_ERRLOG("Failed to construct NVMe controller for SSD: %s\n", trid->traddr);
683 [ # # # # : 9 : probe_ctx->attach_fail_cb(probe_ctx->cb_ctx, trid, -ENODEV);
# # # # #
# # # ]
684 : 9 : return -1;
685 : : }
686 [ + - + - : 3087 : ctrlr->remove_cb = probe_ctx->remove_cb;
+ - + - ]
687 [ + - + - : 3087 : ctrlr->cb_ctx = probe_ctx->cb_ctx;
+ - + - ]
688 : :
689 [ + - + - ]: 3087 : nvme_qpair_set_state(ctrlr->adminq, NVME_QPAIR_ENABLED);
690 [ + - + - : 3087 : TAILQ_INSERT_TAIL(&probe_ctx->init_ctrlrs, ctrlr, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
691 : 3087 : return 0;
692 : : }
693 : :
694 : 25 : return 1;
695 : 797 : }
696 : :
697 : : static void
698 : 146216312 : nvme_ctrlr_poll_internal(struct spdk_nvme_ctrlr *ctrlr,
699 : : struct spdk_nvme_probe_ctx *probe_ctx)
700 : : {
701 : 146216312 : int rc = 0;
702 : :
703 : 146216312 : rc = nvme_ctrlr_process_init(ctrlr);
704 : :
705 [ + + ]: 146216312 : if (rc) {
706 : : /* Controller failed to initialize. */
707 [ - + # # : 58 : TAILQ_REMOVE(&probe_ctx->init_ctrlrs, ctrlr, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
708 [ # # # # ]: 58 : SPDK_ERRLOG("Failed to initialize SSD: %s\n", ctrlr->trid.traddr);
709 [ # # # # : 58 : probe_ctx->attach_fail_cb(probe_ctx->cb_ctx, &ctrlr->trid, rc);
# # # # #
# # # #
# ]
710 : 58 : nvme_ctrlr_lock(ctrlr);
711 : 58 : nvme_ctrlr_fail(ctrlr, false);
712 : 58 : nvme_ctrlr_unlock(ctrlr);
713 : 58 : nvme_ctrlr_destruct(ctrlr);
714 : 58 : return;
715 : : }
716 : :
717 [ + + + - : 146216254 : if (ctrlr->state != NVME_CTRLR_STATE_READY) {
+ + ]
718 : 146213181 : return;
719 : : }
720 : :
721 [ + - + - : 3073 : STAILQ_INIT(&ctrlr->io_producers);
+ - + - +
- + - + -
+ - ]
722 : :
723 : : /*
724 : : * Controller has been initialized.
725 : : * Move it to the attached_ctrlrs list.
726 : : */
727 [ + + + - : 3073 : TAILQ_REMOVE(&probe_ctx->init_ctrlrs, ctrlr, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
728 : :
729 [ + - ]: 3073 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
730 [ + + ]: 3073 : if (nvme_ctrlr_shared(ctrlr)) {
731 [ + - + - : 709 : TAILQ_INSERT_TAIL(&g_spdk_nvme_driver->shared_attached_ctrlrs, ctrlr, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
732 : 2 : } else {
733 [ + - + - : 2364 : TAILQ_INSERT_TAIL(&g_nvme_attached_ctrlrs, ctrlr, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
734 : : }
735 : :
736 : : /*
737 : : * Increase the ref count before calling attach_cb() as the user may
738 : : * call nvme_detach() immediately.
739 : : */
740 : 3073 : nvme_ctrlr_proc_get_ref(ctrlr);
741 [ + - ]: 3073 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
742 : :
743 [ + + + - : 3073 : if (probe_ctx->attach_cb) {
+ + ]
744 [ + - + - : 1855 : probe_ctx->attach_cb(probe_ctx->cb_ctx, &ctrlr->trid, ctrlr, &ctrlr->opts);
- + + - +
- + - + -
+ - ]
745 : 2 : }
746 : 31931824 : }
747 : :
748 : : static int
749 : 119897 : nvme_init_controllers(struct spdk_nvme_probe_ctx *probe_ctx)
750 : : {
751 : 119897 : int rc = 0;
752 : :
753 : 31929779 : while (true) {
754 : 146029073 : rc = spdk_nvme_probe_poll_async(probe_ctx);
755 [ + + ]: 146029073 : if (rc != -EAGAIN) {
756 : 119897 : return rc;
757 : : }
758 : : }
759 : :
760 : : return rc;
761 : : }
762 : :
763 : : /* This function must not be called while holding g_spdk_nvme_driver->lock */
764 : : static struct spdk_nvme_ctrlr *
765 : 1271 : nvme_get_ctrlr_by_trid(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
766 : : {
767 : : struct spdk_nvme_ctrlr *ctrlr;
768 : :
769 [ + - ]: 1271 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
770 : 1271 : ctrlr = nvme_get_ctrlr_by_trid_unsafe(trid, hostnqn);
771 [ + - ]: 1271 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
772 : :
773 : 1271 : return ctrlr;
774 : : }
775 : :
776 : : /* This function must be called while holding g_spdk_nvme_driver->lock */
777 : : struct spdk_nvme_ctrlr *
778 : 5395 : nvme_get_ctrlr_by_trid_unsafe(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
779 : : {
780 : : struct spdk_nvme_ctrlr *ctrlr;
781 : :
782 : : /* Search per-process list */
783 [ + + # # : 5722 : TAILQ_FOREACH(ctrlr, &g_nvme_attached_ctrlrs, tailq) {
# # # # ]
784 [ + + - + ]: 1512 : if (spdk_nvme_transport_id_compare(&ctrlr->trid, trid) != 0) {
785 : 324 : continue;
786 : : }
787 [ + - + + : 1188 : if (hostnqn && strcmp(ctrlr->opts.hostnqn, hostnqn) != 0) {
+ + + + +
- + - ]
788 : 3 : continue;
789 : : }
790 : 1185 : return ctrlr;
791 : : }
792 : :
793 : : /* Search multi-process shared list */
794 [ + + + - : 4744 : TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->shared_attached_ctrlrs, tailq) {
+ - - + #
# # # #
# ]
795 [ + + # # ]: 917 : if (spdk_nvme_transport_id_compare(&ctrlr->trid, trid) != 0) {
796 : 534 : continue;
797 : : }
798 [ + + - + : 383 : if (hostnqn && strcmp(ctrlr->opts.hostnqn, hostnqn) != 0) {
- + - + #
# # # ]
799 : 0 : continue;
800 : : }
801 : 383 : return ctrlr;
802 : : }
803 : :
804 : 3827 : return NULL;
805 : 1594 : }
806 : :
807 : : /* This function must only be called while holding g_spdk_nvme_driver->lock */
808 : : static int
809 : 125350 : nvme_probe_internal(struct spdk_nvme_probe_ctx *probe_ctx,
810 : : bool direct_connect)
811 : : {
812 : : int rc;
813 : : struct spdk_nvme_ctrlr *ctrlr, *ctrlr_tmp;
814 [ + - + - ]: 125350 : const struct spdk_nvme_ctrlr_opts *opts = probe_ctx->opts;
815 : :
816 [ + + + - : 125350 : if (strlen(probe_ctx->trid.trstring) == 0) {
+ - + + ]
817 : : /* If user didn't provide trstring, derive it from trtype */
818 [ + - + - : 84 : spdk_nvme_trid_populate_transport(&probe_ctx->trid, probe_ctx->trid.trtype);
+ - + - ]
819 : 7 : }
820 : :
821 [ + + + - : 125350 : if (!spdk_nvme_transport_available_by_name(probe_ctx->trid.trstring)) {
+ - ]
822 [ # # # # : 3 : SPDK_ERRLOG("NVMe trtype %u (%s) not available\n",
# # # # #
# ]
823 : : probe_ctx->trid.trtype, probe_ctx->trid.trstring);
824 : 3 : return -1;
825 : : }
826 : :
827 [ + - ]: 125347 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
828 : :
829 [ + - ]: 125347 : rc = nvme_transport_ctrlr_scan(probe_ctx, direct_connect);
830 [ + + ]: 125347 : if (rc != 0) {
831 : 21 : SPDK_ERRLOG("NVMe ctrlr scan failed\n");
832 [ + + # # : 24 : TAILQ_FOREACH_SAFE(ctrlr, &probe_ctx->init_ctrlrs, tailq, ctrlr_tmp) {
# # # # #
# # # # #
# # ]
833 [ - + # # : 3 : TAILQ_REMOVE(&probe_ctx->init_ctrlrs, ctrlr, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
834 [ # # # # : 3 : probe_ctx->attach_fail_cb(probe_ctx->cb_ctx, &ctrlr->trid, -EFAULT);
# # # # #
# # # #
# ]
835 : 3 : nvme_transport_ctrlr_destruct(ctrlr);
836 : 0 : }
837 [ # # ]: 21 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
838 : 21 : return -1;
839 : : }
840 : :
841 : : /*
842 : : * Probe controllers on the shared_attached_ctrlrs list
843 : : */
844 [ + + + - : 125326 : if (!spdk_process_is_primary() && (probe_ctx->trid.trtype == SPDK_NVME_TRANSPORT_PCIE)) {
# # # # #
# ]
845 [ + + # # : 453 : TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->shared_attached_ctrlrs, tailq) {
# # # # #
# # # #
# ]
846 : : /* Do not attach other ctrlrs if user specify a valid trid */
847 [ + + + + : 351 : if ((strlen(probe_ctx->trid.traddr) != 0) &&
# # # # #
# ]
848 [ # # # # ]: 69 : (spdk_nvme_transport_id_compare(&probe_ctx->trid, &ctrlr->trid))) {
849 : 30 : continue;
850 : : }
851 : :
852 [ + + - + : 252 : if (opts && strcmp(opts->hostnqn, ctrlr->opts.hostnqn) != 0) {
- + - + #
# # # #
# ]
853 : 0 : continue;
854 : : }
855 : :
856 : : /* Do not attach if we failed to initialize it in this process */
857 [ - + ]: 252 : if (nvme_ctrlr_get_current_process(ctrlr) == NULL) {
858 : 0 : continue;
859 : : }
860 : :
861 : 252 : nvme_ctrlr_proc_get_ref(ctrlr);
862 : :
863 : : /*
864 : : * Unlock while calling attach_cb() so the user can call other functions
865 : : * that may take the driver lock, like nvme_detach().
866 : : */
867 [ + + # # : 252 : if (probe_ctx->attach_cb) {
# # ]
868 [ # # ]: 217 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
869 [ # # # # : 217 : probe_ctx->attach_cb(probe_ctx->cb_ctx, &ctrlr->trid, ctrlr, &ctrlr->opts);
# # # # #
# # # # #
# # ]
870 [ # # ]: 217 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
871 : 0 : }
872 : 0 : }
873 : 0 : }
874 : :
875 [ + - ]: 125326 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
876 : :
877 : 125326 : return 0;
878 : 797 : }
879 : :
880 : : static void
881 : 58 : nvme_dummy_attach_fail_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
882 : : int rc)
883 : : {
884 [ # # # # : 58 : SPDK_ERRLOG("Failed to attach nvme ctrlr: trtype=%s adrfam=%s traddr=%s trsvcid=%s "
# # # # #
# # # # #
# # ]
885 : : "subnqn=%s, %s\n", spdk_nvme_transport_id_trtype_str(trid->trtype),
886 : : spdk_nvme_transport_id_adrfam_str(trid->adrfam), trid->traddr, trid->trsvcid,
887 : : trid->subnqn, spdk_strerror(-rc));
888 : 58 : }
889 : :
890 : : static void
891 : 190497 : nvme_probe_ctx_init(struct spdk_nvme_probe_ctx *probe_ctx,
892 : : const struct spdk_nvme_transport_id *trid,
893 : : const struct spdk_nvme_ctrlr_opts *opts,
894 : : void *cb_ctx,
895 : : spdk_nvme_probe_cb probe_cb,
896 : : spdk_nvme_attach_cb attach_cb,
897 : : spdk_nvme_attach_fail_cb attach_fail_cb,
898 : : spdk_nvme_remove_cb remove_cb)
899 : : {
900 [ + - ]: 190497 : probe_ctx->trid = *trid;
901 [ + - + - ]: 190497 : probe_ctx->opts = opts;
902 [ + - + - ]: 190497 : probe_ctx->cb_ctx = cb_ctx;
903 [ + - + - ]: 190497 : probe_ctx->probe_cb = probe_cb;
904 [ + - + - ]: 190497 : probe_ctx->attach_cb = attach_cb;
905 [ + + ]: 190497 : if (attach_fail_cb != NULL) {
906 [ # # # # ]: 18 : probe_ctx->attach_fail_cb = attach_fail_cb;
907 : 0 : } else {
908 [ + - + - ]: 190479 : probe_ctx->attach_fail_cb = nvme_dummy_attach_fail_cb;
909 : : }
910 [ + - + - ]: 190497 : probe_ctx->remove_cb = remove_cb;
911 [ + - + - : 190497 : TAILQ_INIT(&probe_ctx->init_ctrlrs);
+ - + - +
- + - + -
+ - ]
912 : 190497 : }
913 : :
914 : : int
915 : 118625 : spdk_nvme_probe(const struct spdk_nvme_transport_id *trid, void *cb_ctx,
916 : : spdk_nvme_probe_cb probe_cb, spdk_nvme_attach_cb attach_cb,
917 : : spdk_nvme_remove_cb remove_cb)
918 : : {
919 : 118625 : return spdk_nvme_probe_ext(trid, cb_ctx, probe_cb, attach_cb, NULL, remove_cb);
920 : : }
921 : :
922 : : int
923 : 118637 : spdk_nvme_probe_ext(const struct spdk_nvme_transport_id *trid, void *cb_ctx,
924 : : spdk_nvme_probe_cb probe_cb, spdk_nvme_attach_cb attach_cb,
925 : : spdk_nvme_attach_fail_cb attach_fail_cb, spdk_nvme_remove_cb remove_cb)
926 : : {
927 : 57506 : struct spdk_nvme_transport_id trid_pcie;
928 : : struct spdk_nvme_probe_ctx *probe_ctx;
929 : :
930 [ + + ]: 118637 : if (trid == NULL) {
931 [ + + ]: 118360 : memset(&trid_pcie, 0, sizeof(trid_pcie));
932 : 118360 : spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
933 : 118360 : trid = &trid_pcie;
934 : 1 : }
935 : :
936 : 118638 : probe_ctx = spdk_nvme_probe_async_ext(trid, cb_ctx, probe_cb,
937 : 1 : attach_cb, attach_fail_cb, remove_cb);
938 [ + + ]: 118637 : if (!probe_ctx) {
939 : 8 : SPDK_ERRLOG("Create probe context failed\n");
940 : 8 : return -1;
941 : : }
942 : :
943 : : /*
944 : : * Keep going even if one or more nvme_attach() calls failed,
945 : : * but maintain the value of rc to signal errors when we return.
946 : : */
947 : 118629 : return nvme_init_controllers(probe_ctx);
948 : 1 : }
949 : :
950 : : static bool
951 : 1643 : nvme_connect_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
952 : : struct spdk_nvme_ctrlr_opts *opts)
953 : : {
954 : 1643 : struct spdk_nvme_ctrlr_opts *requested_opts = cb_ctx;
955 : :
956 [ + + # # ]: 1643 : assert(requested_opts);
957 [ + + + + ]: 1643 : memcpy(opts, requested_opts, sizeof(*opts));
958 : :
959 : 1643 : return true;
960 : : }
961 : :
962 : : static void
963 : 77 : nvme_ctrlr_opts_init(struct spdk_nvme_ctrlr_opts *opts,
964 : : const struct spdk_nvme_ctrlr_opts *opts_user,
965 : : size_t opts_size_user)
966 : : {
967 [ - + # # ]: 77 : assert(opts);
968 [ - + # # ]: 77 : assert(opts_user);
969 : :
970 : 77 : spdk_nvme_ctrlr_get_default_ctrlr_opts(opts, opts_size_user);
971 : :
972 : : #define FIELD_OK(field) \
973 : : offsetof(struct spdk_nvme_ctrlr_opts, field) + sizeof(opts->field) <= (opts->opts_size)
974 : :
975 : : #define SET_FIELD(field) \
976 : : if (FIELD_OK(field)) { \
977 : : opts->field = opts_user->field; \
978 : : }
979 : :
980 : : #define SET_FIELD_ARRAY(field) \
981 : : if (FIELD_OK(field)) { \
982 : : memcpy(opts->field, opts_user->field, sizeof(opts_user->field)); \
983 : : }
984 : :
985 [ + + # # : 77 : SET_FIELD(num_io_queues);
# # # # #
# # # #
# ]
986 [ + + - + : 77 : SET_FIELD(use_cmb_sqs);
# # # # #
# # # # #
# # ]
987 [ + + - + : 77 : SET_FIELD(no_shn_notification);
# # # # #
# # # # #
# # ]
988 [ + + # # : 77 : SET_FIELD(arb_mechanism);
# # # # #
# # # #
# ]
989 [ + + # # : 77 : SET_FIELD(arbitration_burst);
# # # # #
# # # #
# ]
990 [ + + # # : 77 : SET_FIELD(low_priority_weight);
# # # # #
# # # #
# ]
991 [ + + # # : 77 : SET_FIELD(medium_priority_weight);
# # # # #
# # # #
# ]
992 [ + + # # : 77 : SET_FIELD(high_priority_weight);
# # # # #
# # # #
# ]
993 [ + + # # : 77 : SET_FIELD(keep_alive_timeout_ms);
# # # # #
# # # #
# ]
994 [ + + # # : 77 : SET_FIELD(transport_retry_count);
# # # # #
# # # #
# ]
995 [ + + # # : 77 : SET_FIELD(io_queue_size);
# # # # #
# # # #
# ]
996 [ + + - + : 77 : SET_FIELD_ARRAY(hostnqn);
- + # # #
# # # #
# ]
997 [ + + # # : 77 : SET_FIELD(io_queue_requests);
# # # # #
# # # #
# ]
998 [ + + - + : 77 : SET_FIELD_ARRAY(src_addr);
- + # # #
# # # #
# ]
999 [ + + - + : 77 : SET_FIELD_ARRAY(src_svcid);
- + # # #
# # # #
# ]
1000 [ + + # # : 77 : SET_FIELD_ARRAY(host_id);
# # # # #
# # # #
# ]
1001 [ + + # # : 77 : SET_FIELD_ARRAY(extended_host_id);
# # # # #
# # # #
# ]
1002 [ + + # # : 77 : SET_FIELD(command_set);
# # # # #
# # # #
# ]
1003 [ + + # # : 77 : SET_FIELD(admin_timeout_ms);
# # # # #
# # # #
# ]
1004 [ + + - + : 77 : SET_FIELD(header_digest);
# # # # #
# # # # #
# # ]
1005 [ + + - + : 77 : SET_FIELD(data_digest);
# # # # #
# # # # #
# # ]
1006 [ + + - + : 77 : SET_FIELD(disable_error_logging);
# # # # #
# # # # #
# # ]
1007 [ + + # # : 77 : SET_FIELD(transport_ack_timeout);
# # # # #
# # # #
# ]
1008 [ + + # # : 77 : SET_FIELD(admin_queue_size);
# # # # #
# # # #
# ]
1009 [ + + # # : 77 : SET_FIELD(fabrics_connect_timeout_us);
# # # # #
# # # #
# ]
1010 [ + + - + : 77 : SET_FIELD(disable_read_ana_log_page);
# # # # #
# # # # #
# # ]
1011 [ + + # # : 77 : SET_FIELD(disable_read_changed_ns_list_log_page);
# # # # #
# # # #
# ]
1012 [ + + # # : 77 : SET_FIELD(tls_psk);
# # # # #
# # # #
# ]
1013 [ + + # # : 77 : SET_FIELD(dhchap_key);
# # # # #
# # # #
# ]
1014 [ + + # # : 77 : SET_FIELD(dhchap_ctrlr_key);
# # # # #
# # # #
# ]
1015 [ + + # # : 77 : SET_FIELD(dhchap_digests);
# # # # #
# # # #
# ]
1016 [ + + # # : 77 : SET_FIELD(dhchap_dhgroups);
# # # # #
# # # #
# ]
1017 : :
1018 : : #undef FIELD_OK
1019 : : #undef SET_FIELD
1020 : : #undef SET_FIELD_ARRAY
1021 : 77 : }
1022 : :
1023 : : struct spdk_nvme_ctrlr *
1024 : 1265 : spdk_nvme_connect(const struct spdk_nvme_transport_id *trid,
1025 : : const struct spdk_nvme_ctrlr_opts *opts, size_t opts_size)
1026 : : {
1027 : : int rc;
1028 : 1265 : struct spdk_nvme_ctrlr *ctrlr = NULL;
1029 : : struct spdk_nvme_probe_ctx *probe_ctx;
1030 : 1265 : struct spdk_nvme_ctrlr_opts *opts_local_p = NULL;
1031 : 61 : struct spdk_nvme_ctrlr_opts opts_local;
1032 : 61 : char hostnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
1033 : :
1034 [ + + ]: 1265 : if (trid == NULL) {
1035 : 3 : SPDK_ERRLOG("No transport ID specified\n");
1036 : 3 : return NULL;
1037 : : }
1038 : :
1039 : 1262 : rc = nvme_driver_init();
1040 [ + + ]: 1262 : if (rc != 0) {
1041 : 3 : return NULL;
1042 : : }
1043 : :
1044 : 1259 : nvme_get_default_hostnqn(hostnqn, sizeof(hostnqn));
1045 [ + + ]: 1259 : if (opts) {
1046 : 77 : opts_local_p = &opts_local;
1047 : 77 : nvme_ctrlr_opts_init(opts_local_p, opts, opts_size);
1048 [ # # # # ]: 77 : memcpy(hostnqn, opts_local.hostnqn, sizeof(hostnqn));
1049 : 0 : }
1050 : :
1051 : 1259 : probe_ctx = spdk_nvme_connect_async(trid, opts_local_p, NULL);
1052 [ + + ]: 1259 : if (!probe_ctx) {
1053 : 0 : SPDK_ERRLOG("Create probe context failed\n");
1054 : 0 : return NULL;
1055 : : }
1056 : :
1057 : 1259 : rc = nvme_init_controllers(probe_ctx);
1058 [ - + ]: 1259 : if (rc != 0) {
1059 : 0 : return NULL;
1060 : : }
1061 : :
1062 : 1259 : ctrlr = nvme_get_ctrlr_by_trid(trid, hostnqn);
1063 : :
1064 : 1259 : return ctrlr;
1065 : 795 : }
1066 : :
1067 : : void
1068 : 205194 : spdk_nvme_trid_populate_transport(struct spdk_nvme_transport_id *trid,
1069 : : enum spdk_nvme_transport_type trtype)
1070 : : {
1071 : : const char *trstring;
1072 : :
1073 [ + - + - ]: 205194 : trid->trtype = trtype;
1074 [ - + + + : 205194 : switch (trtype) {
+ - - ]
1075 : 0 : case SPDK_NVME_TRANSPORT_FC:
1076 : 0 : trstring = SPDK_NVME_TRANSPORT_NAME_FC;
1077 : 0 : break;
1078 : 188548 : case SPDK_NVME_TRANSPORT_PCIE:
1079 : 188554 : trstring = SPDK_NVME_TRANSPORT_NAME_PCIE;
1080 : 188554 : break;
1081 : 3144 : case SPDK_NVME_TRANSPORT_RDMA:
1082 : 3144 : trstring = SPDK_NVME_TRANSPORT_NAME_RDMA;
1083 : 3144 : break;
1084 : 11100 : case SPDK_NVME_TRANSPORT_TCP:
1085 : 13489 : trstring = SPDK_NVME_TRANSPORT_NAME_TCP;
1086 : 13489 : break;
1087 : 0 : case SPDK_NVME_TRANSPORT_VFIOUSER:
1088 : 7 : trstring = SPDK_NVME_TRANSPORT_NAME_VFIOUSER;
1089 : 7 : break;
1090 : 0 : case SPDK_NVME_TRANSPORT_CUSTOM:
1091 : 0 : trstring = SPDK_NVME_TRANSPORT_NAME_CUSTOM;
1092 : 0 : break;
1093 : 0 : default:
1094 : 0 : SPDK_ERRLOG("no available transports\n");
1095 [ # # ]: 0 : assert(0);
1096 : : return;
1097 : : }
1098 [ + + ]: 205194 : snprintf(trid->trstring, SPDK_NVMF_TRSTRING_MAX_LEN, "%s", trstring);
1099 : 2402 : }
1100 : :
1101 : : int
1102 : 3001 : spdk_nvme_transport_id_populate_trstring(struct spdk_nvme_transport_id *trid, const char *trstring)
1103 : : {
1104 : 3001 : int i = 0;
1105 : :
1106 [ + - - + ]: 3001 : if (trid == NULL || trstring == NULL) {
1107 : 0 : return -EINVAL;
1108 : : }
1109 : :
1110 : : /* Note: gcc-11 has some false positive -Wstringop-overread warnings with LTO builds if we
1111 : : * use strnlen here. So do the trstring copy manually instead. See GitHub issue #2391.
1112 : : */
1113 : :
1114 : : /* cast official trstring to uppercase version of input. */
1115 [ + + + + : 13487 : while (i < SPDK_NVMF_TRSTRING_MAX_LEN && trstring[i] != 0) {
+ - + + ]
1116 [ + - + - : 10486 : trid->trstring[i] = toupper(trstring[i]);
+ - + - +
- + - ]
1117 [ + - ]: 10486 : i++;
1118 : : }
1119 : :
1120 [ + + + - : 3001 : if (trstring[i] != 0) {
+ - ]
1121 : 0 : return -EINVAL;
1122 : : } else {
1123 [ + - + - : 3001 : trid->trstring[i] = 0;
+ - + - ]
1124 : 3001 : return 0;
1125 : : }
1126 : 65 : }
1127 : :
1128 : : int
1129 : 2852 : spdk_nvme_transport_id_parse_trtype(enum spdk_nvme_transport_type *trtype, const char *str)
1130 : : {
1131 [ + + + + ]: 2852 : if (trtype == NULL || str == NULL) {
1132 : 6 : return -EINVAL;
1133 : : }
1134 : :
1135 [ + + + + : 2846 : if (strcasecmp(str, "PCIe") == 0) {
+ + ]
1136 [ + - ]: 653 : *trtype = SPDK_NVME_TRANSPORT_PCIE;
1137 [ + + + + : 2194 : } else if (strcasecmp(str, "RDMA") == 0) {
+ - ]
1138 [ # # ]: 435 : *trtype = SPDK_NVME_TRANSPORT_RDMA;
1139 [ + + + + : 1758 : } else if (strcasecmp(str, "FC") == 0) {
+ - ]
1140 [ # # ]: 6 : *trtype = SPDK_NVME_TRANSPORT_FC;
1141 [ + + + + : 1752 : } else if (strcasecmp(str, "TCP") == 0) {
+ + ]
1142 [ + - ]: 1679 : *trtype = SPDK_NVME_TRANSPORT_TCP;
1143 [ + + + + : 123 : } else if (strcasecmp(str, "VFIOUSER") == 0) {
- + ]
1144 [ - + ]: 70 : *trtype = SPDK_NVME_TRANSPORT_VFIOUSER;
1145 : 14 : } else {
1146 [ # # ]: 3 : *trtype = SPDK_NVME_TRANSPORT_CUSTOM;
1147 : : }
1148 : 2846 : return 0;
1149 : 65 : }
1150 : :
1151 : : const char *
1152 : 2723 : spdk_nvme_transport_id_trtype_str(enum spdk_nvme_transport_type trtype)
1153 : : {
1154 [ + + + + : 2723 : switch (trtype) {
- - + ]
1155 : 1041 : case SPDK_NVME_TRANSPORT_PCIE:
1156 : 1041 : return "PCIe";
1157 : 495 : case SPDK_NVME_TRANSPORT_RDMA:
1158 : 495 : return "RDMA";
1159 : 3 : case SPDK_NVME_TRANSPORT_FC:
1160 : 3 : return "FC";
1161 : 1181 : case SPDK_NVME_TRANSPORT_TCP:
1162 : 1181 : return "TCP";
1163 : 0 : case SPDK_NVME_TRANSPORT_VFIOUSER:
1164 : 0 : return "VFIOUSER";
1165 : 0 : case SPDK_NVME_TRANSPORT_CUSTOM:
1166 : 0 : return "CUSTOM";
1167 : 3 : default:
1168 : 3 : return NULL;
1169 : : }
1170 : 0 : }
1171 : :
1172 : : int
1173 : 1375 : spdk_nvme_transport_id_parse_adrfam(enum spdk_nvmf_adrfam *adrfam, const char *str)
1174 : : {
1175 [ + + + + ]: 1375 : if (adrfam == NULL || str == NULL) {
1176 : 6 : return -EINVAL;
1177 : : }
1178 : :
1179 [ + + + + : 1369 : if (strcasecmp(str, "IPv4") == 0) {
- + ]
1180 [ + - ]: 1348 : *adrfam = SPDK_NVMF_ADRFAM_IPV4;
1181 [ + + + + : 71 : } else if (strcasecmp(str, "IPv6") == 0) {
# # ]
1182 [ # # ]: 6 : *adrfam = SPDK_NVMF_ADRFAM_IPV6;
1183 [ + + + + : 15 : } else if (strcasecmp(str, "IB") == 0) {
# # ]
1184 [ # # ]: 6 : *adrfam = SPDK_NVMF_ADRFAM_IB;
1185 [ + + + + : 9 : } else if (strcasecmp(str, "FC") == 0) {
# # ]
1186 [ # # ]: 6 : *adrfam = SPDK_NVMF_ADRFAM_FC;
1187 : 0 : } else {
1188 : 3 : return -ENOENT;
1189 : : }
1190 : 1366 : return 0;
1191 : 50 : }
1192 : :
1193 : : const char *
1194 : 4537 : spdk_nvme_transport_id_adrfam_str(enum spdk_nvmf_adrfam adrfam)
1195 : : {
1196 [ + + + + : 4537 : switch (adrfam) {
+ ]
1197 : 3509 : case SPDK_NVMF_ADRFAM_IPV4:
1198 : 3509 : return "IPv4";
1199 : 3 : case SPDK_NVMF_ADRFAM_IPV6:
1200 : 3 : return "IPv6";
1201 : 3 : case SPDK_NVMF_ADRFAM_IB:
1202 : 3 : return "IB";
1203 : 3 : case SPDK_NVMF_ADRFAM_FC:
1204 : 3 : return "FC";
1205 : 1019 : default:
1206 : 1019 : return NULL;
1207 : : }
1208 : 0 : }
1209 : :
1210 : : static size_t
1211 : 1322 : parse_next_key(const char **str, char *key, char *val, size_t key_buf_size, size_t val_buf_size)
1212 : : {
1213 : :
1214 : : const char *sep, *sep1;
1215 : 1322 : const char *whitespace = " \t\n";
1216 : : size_t key_len, val_len;
1217 : :
1218 [ + + + + : 1322 : *str += strspn(*str, whitespace);
+ - + - +
- ]
1219 : :
1220 [ + + + - ]: 1322 : sep = strchr(*str, ':');
1221 [ + + ]: 1322 : if (!sep) {
1222 [ - + # # ]: 89 : sep = strchr(*str, '=');
1223 [ + + ]: 89 : if (!sep) {
1224 : 3 : SPDK_ERRLOG("Key without ':' or '=' separator\n");
1225 : 3 : return 0;
1226 : : }
1227 : 0 : } else {
1228 [ + + + - ]: 1233 : sep1 = strchr(*str, '=');
1229 [ + + + - ]: 1233 : if ((sep1 != NULL) && (sep1 < sep)) {
1230 : 12 : sep = sep1;
1231 : 0 : }
1232 : : }
1233 : :
1234 [ + - ]: 1319 : key_len = sep - *str;
1235 [ + + ]: 1319 : if (key_len >= key_buf_size) {
1236 : 3 : SPDK_ERRLOG("Key length %zu greater than maximum allowed %zu\n",
1237 : : key_len, key_buf_size - 1);
1238 : 3 : return 0;
1239 : : }
1240 : :
1241 [ + + + + : 1316 : memcpy(key, *str, key_len);
+ - ]
1242 [ + - + - ]: 1316 : key[key_len] = '\0';
1243 : :
1244 [ + - + - ]: 1316 : *str += key_len + 1; /* Skip key: */
1245 [ + + + + : 1316 : val_len = strcspn(*str, whitespace);
+ - ]
1246 [ + + ]: 1316 : if (val_len == 0) {
1247 : 3 : SPDK_ERRLOG("Key without value\n");
1248 : 3 : return 0;
1249 : : }
1250 : :
1251 [ - + ]: 1313 : if (val_len >= val_buf_size) {
1252 : 0 : SPDK_ERRLOG("Value length %zu greater than maximum allowed %zu\n",
1253 : : val_len, val_buf_size - 1);
1254 : 0 : return 0;
1255 : : }
1256 : :
1257 [ - + - + : 1313 : memcpy(val, *str, val_len);
- + ]
1258 [ - + - + ]: 1313 : val[val_len] = '\0';
1259 : :
1260 [ - + - + ]: 1313 : *str += val_len;
1261 : :
1262 : 1313 : return val_len;
1263 : 125 : }
1264 : :
1265 : : int
1266 : 409 : spdk_nvme_transport_id_parse(struct spdk_nvme_transport_id *trid, const char *str)
1267 : : {
1268 : : size_t val_len;
1269 : 115 : char key[32];
1270 : 115 : char val[1024];
1271 : :
1272 [ + + + + ]: 409 : if (trid == NULL || str == NULL) {
1273 : 9 : return -EINVAL;
1274 : : }
1275 : :
1276 [ + + + + ]: 1695 : while (*str != '\0') {
1277 : :
1278 : 1304 : val_len = parse_next_key(&str, key, val, sizeof(key), sizeof(val));
1279 : :
1280 [ + + ]: 1304 : if (val_len == 0) {
1281 : 9 : SPDK_ERRLOG("Failed to parse transport ID\n");
1282 : 9 : return -EINVAL;
1283 : : }
1284 : :
1285 [ + + + - : 1295 : if (strcasecmp(key, "trtype") == 0) {
+ + ]
1286 [ + + ]: 391 : if (spdk_nvme_transport_id_populate_trstring(trid, val) != 0) {
1287 : 0 : SPDK_ERRLOG("invalid transport '%s'\n", val);
1288 : 0 : return -EINVAL;
1289 : : }
1290 [ + + + - ]: 391 : if (spdk_nvme_transport_id_parse_trtype(&trid->trtype, val) != 0) {
1291 : 0 : SPDK_ERRLOG("Unknown trtype '%s'\n", val);
1292 : 0 : return -EINVAL;
1293 : : }
1294 [ + + + - : 929 : } else if (strcasecmp(key, "adrfam") == 0) {
+ + ]
1295 [ + + + - ]: 209 : if (spdk_nvme_transport_id_parse_adrfam(&trid->adrfam, val) != 0) {
1296 : 0 : SPDK_ERRLOG("Unknown adrfam '%s'\n", val);
1297 : 0 : return -EINVAL;
1298 : : }
1299 [ + + + - : 720 : } else if (strcasecmp(key, "traddr") == 0) {
+ + ]
1300 [ + + ]: 343 : if (val_len > SPDK_NVMF_TRADDR_MAX_LEN) {
1301 : 0 : SPDK_ERRLOG("traddr length %zu greater than maximum allowed %u\n",
1302 : : val_len, SPDK_NVMF_TRADDR_MAX_LEN);
1303 : 0 : return -EINVAL;
1304 : : }
1305 [ + + + - : 343 : memcpy(trid->traddr, val, val_len + 1);
+ - ]
1306 [ + + + - : 377 : } else if (strcasecmp(key, "trsvcid") == 0) {
+ + ]
1307 [ + + ]: 209 : if (val_len > SPDK_NVMF_TRSVCID_MAX_LEN) {
1308 : 0 : SPDK_ERRLOG("trsvcid length %zu greater than maximum allowed %u\n",
1309 : : val_len, SPDK_NVMF_TRSVCID_MAX_LEN);
1310 : 0 : return -EINVAL;
1311 : : }
1312 [ + + + - : 209 : memcpy(trid->trsvcid, val, val_len + 1);
+ - ]
1313 [ + + + - : 168 : } else if (strcasecmp(key, "priority") == 0) {
+ - ]
1314 [ - + ]: 3 : if (val_len > SPDK_NVMF_PRIORITY_MAX_LEN) {
1315 : 0 : SPDK_ERRLOG("priority length %zu greater than maximum allowed %u\n",
1316 : : val_len, SPDK_NVMF_PRIORITY_MAX_LEN);
1317 : 0 : return -EINVAL;
1318 : : }
1319 [ # # # # ]: 3 : trid->priority = spdk_strtol(val, 10);
1320 [ + + + - : 140 : } else if (strcasecmp(key, "subnqn") == 0) {
- + ]
1321 [ + + ]: 131 : if (val_len > SPDK_NVMF_NQN_MAX_LEN) {
1322 : 0 : SPDK_ERRLOG("subnqn length %zu greater than maximum allowed %u\n",
1323 : : val_len, SPDK_NVMF_NQN_MAX_LEN);
1324 : 0 : return -EINVAL;
1325 : : }
1326 [ + + + - : 131 : memcpy(trid->subnqn, val, val_len + 1);
+ - ]
1327 [ - + # # : 34 : } else if (strcasecmp(key, "hostaddr") == 0) {
# # ]
1328 : 0 : continue;
1329 [ - + # # : 9 : } else if (strcasecmp(key, "hostsvcid") == 0) {
# # ]
1330 : 0 : continue;
1331 [ + + # # : 9 : } else if (strcasecmp(key, "hostnqn") == 0) {
# # ]
1332 : 3 : continue;
1333 [ + + # # : 6 : } else if (strcasecmp(key, "ns") == 0) {
# # ]
1334 : : /*
1335 : : * Special case. The namespace id parameter may
1336 : : * optionally be passed in the transport id string
1337 : : * for an SPDK application (e.g. spdk_nvme_perf)
1338 : : * and additionally parsed therein to limit
1339 : : * targeting a specific namespace. For this
1340 : : * scenario, just silently ignore this key
1341 : : * rather than letting it default to logging
1342 : : * it as an invalid key.
1343 : : */
1344 : 5 : continue;
1345 [ + - # # : 1 : } else if (strcasecmp(key, "alt_traddr") == 0) {
# # ]
1346 : : /*
1347 : : * Used by applications for enabling transport ID failover.
1348 : : * Please see the case above for more information on custom parameters.
1349 : : */
1350 : 1 : continue;
1351 : : } else {
1352 : 0 : SPDK_ERRLOG("Unknown transport ID key '%s'\n", key);
1353 : : }
1354 : : }
1355 : :
1356 : 391 : return 0;
1357 : 25 : }
1358 : :
1359 : : int
1360 : 9 : spdk_nvme_host_id_parse(struct spdk_nvme_host_id *hostid, const char *str)
1361 : 9 : {
1362 : :
1363 : 9 : size_t key_size = 32;
1364 : 9 : size_t val_size = 1024;
1365 : : size_t val_len;
1366 [ - + ]: 9 : char key[key_size];
1367 [ - + ]: 9 : char val[val_size];
1368 : :
1369 [ + - - + ]: 9 : if (hostid == NULL || str == NULL) {
1370 : 0 : return -EINVAL;
1371 : : }
1372 : :
1373 [ + + # # ]: 18 : while (*str != '\0') {
1374 : :
1375 : 9 : val_len = parse_next_key(&str, key, val, key_size, val_size);
1376 : :
1377 [ - + ]: 9 : if (val_len == 0) {
1378 : 0 : SPDK_ERRLOG("Failed to parse host ID\n");
1379 : 0 : return val_len;
1380 : : }
1381 : :
1382 : : /* Ignore the rest of the options from the transport ID. */
1383 [ + + + + : 9 : if (strcasecmp(key, "trtype") == 0) {
# # ]
1384 : 3 : continue;
1385 [ - + - + : 6 : } else if (strcasecmp(key, "adrfam") == 0) {
# # ]
1386 : 0 : continue;
1387 [ - + - + : 6 : } else if (strcasecmp(key, "traddr") == 0) {
# # ]
1388 : 0 : continue;
1389 [ - + - + : 6 : } else if (strcasecmp(key, "trsvcid") == 0) {
# # ]
1390 : 0 : continue;
1391 [ - + - + : 6 : } else if (strcasecmp(key, "subnqn") == 0) {
# # ]
1392 : 0 : continue;
1393 [ - + - + : 6 : } else if (strcasecmp(key, "priority") == 0) {
# # ]
1394 : 0 : continue;
1395 [ - + - + : 6 : } else if (strcasecmp(key, "ns") == 0) {
# # ]
1396 : 0 : continue;
1397 [ + + + + : 6 : } else if (strcasecmp(key, "hostaddr") == 0) {
# # ]
1398 [ - + ]: 3 : if (val_len > SPDK_NVMF_TRADDR_MAX_LEN) {
1399 : 0 : SPDK_ERRLOG("hostaddr length %zu greater than maximum allowed %u\n",
1400 : : val_len, SPDK_NVMF_TRADDR_MAX_LEN);
1401 : 0 : return -EINVAL;
1402 : : }
1403 [ - + - + : 3 : memcpy(hostid->hostaddr, val, val_len + 1);
# # ]
1404 : :
1405 [ + + + - : 3 : } else if (strcasecmp(key, "hostsvcid") == 0) {
# # ]
1406 [ - + ]: 3 : if (val_len > SPDK_NVMF_TRSVCID_MAX_LEN) {
1407 : 0 : SPDK_ERRLOG("trsvcid length %zu greater than maximum allowed %u\n",
1408 : : val_len, SPDK_NVMF_TRSVCID_MAX_LEN);
1409 : 0 : return -EINVAL;
1410 : : }
1411 [ - + - + : 3 : memcpy(hostid->hostsvcid, val, val_len + 1);
# # ]
1412 : 0 : } else {
1413 : 0 : SPDK_ERRLOG("Unknown transport ID key '%s'\n", key);
1414 : : }
1415 : : }
1416 : :
1417 : 9 : return 0;
1418 : 0 : }
1419 : :
1420 : : static int
1421 : 43010 : cmp_int(int a, int b)
1422 : : {
1423 [ + - ]: 43010 : return a - b;
1424 : : }
1425 : :
1426 : : int
1427 : 22284 : spdk_nvme_transport_id_compare(const struct spdk_nvme_transport_id *trid1,
1428 : : const struct spdk_nvme_transport_id *trid2)
1429 : : {
1430 : : int cmp;
1431 : :
1432 [ + + + - : 22284 : if (trid1->trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
- + ]
1433 [ # # # # : 0 : cmp = strcasecmp(trid1->trstring, trid2->trstring);
# # # # ]
1434 : 0 : } else {
1435 [ + - + - : 22284 : cmp = cmp_int(trid1->trtype, trid2->trtype);
+ - + - ]
1436 : : }
1437 : :
1438 [ + + ]: 22284 : if (cmp) {
1439 : 3 : return cmp;
1440 : : }
1441 : :
1442 [ + + + - : 22281 : if (trid1->trtype == SPDK_NVME_TRANSPORT_PCIE) {
- + ]
1443 : 1214 : struct spdk_pci_addr pci_addr1 = {};
1444 : 1214 : struct spdk_pci_addr pci_addr2 = {};
1445 : :
1446 : : /* Normalize PCI addresses before comparing */
1447 [ + - - + : 2428 : if (spdk_pci_addr_parse(&pci_addr1, trid1->traddr) < 0 ||
# # ]
1448 [ # # ]: 1214 : spdk_pci_addr_parse(&pci_addr2, trid2->traddr) < 0) {
1449 : 0 : return -1;
1450 : : }
1451 : :
1452 : : /* PCIe transport ID only uses trtype and traddr */
1453 : 1214 : return spdk_pci_addr_compare(&pci_addr1, &pci_addr2);
1454 : : }
1455 : :
1456 [ + + + + : 21067 : cmp = strcasecmp(trid1->traddr, trid2->traddr);
+ - + - ]
1457 [ + + ]: 21067 : if (cmp) {
1458 : 341 : return cmp;
1459 : : }
1460 : :
1461 [ + - + - : 20726 : cmp = cmp_int(trid1->adrfam, trid2->adrfam);
+ - + - ]
1462 [ + + ]: 20726 : if (cmp) {
1463 : 3 : return cmp;
1464 : : }
1465 : :
1466 [ + + + + : 20723 : cmp = strcasecmp(trid1->trsvcid, trid2->trsvcid);
+ - + - ]
1467 [ + + ]: 20723 : if (cmp) {
1468 : 1245 : return cmp;
1469 : : }
1470 : :
1471 [ + + + + : 19478 : cmp = strcmp(trid1->subnqn, trid2->subnqn);
+ - + - ]
1472 [ + + ]: 19478 : if (cmp) {
1473 : 496 : return cmp;
1474 : : }
1475 : :
1476 : 18982 : return 0;
1477 : 3441 : }
1478 : :
1479 : : int
1480 : 12 : spdk_nvme_prchk_flags_parse(uint32_t *prchk_flags, const char *str)
1481 : : {
1482 : : size_t val_len;
1483 : 12 : char key[32];
1484 : 12 : char val[1024];
1485 : :
1486 [ + + - + ]: 12 : if (prchk_flags == NULL || str == NULL) {
1487 : 3 : return -EINVAL;
1488 : : }
1489 : :
1490 [ + + # # ]: 21 : while (*str != '\0') {
1491 : 9 : val_len = parse_next_key(&str, key, val, sizeof(key), sizeof(val));
1492 : :
1493 [ - + ]: 9 : if (val_len == 0) {
1494 : 0 : SPDK_ERRLOG("Failed to parse prchk\n");
1495 : 0 : return -EINVAL;
1496 : : }
1497 : :
1498 [ + - # # : 9 : if (strcasecmp(key, "prchk") == 0) {
# # ]
1499 [ + + # # : 9 : if (strcasestr(val, "reftag") != NULL) {
# # ]
1500 [ # # # # ]: 6 : *prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
1501 : 0 : }
1502 [ + + # # : 9 : if (strcasestr(val, "guard") != NULL) {
# # ]
1503 [ # # # # ]: 6 : *prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
1504 : 0 : }
1505 : 0 : } else {
1506 : 0 : SPDK_ERRLOG("Unknown key '%s'\n", key);
1507 : 0 : return -EINVAL;
1508 : : }
1509 : : }
1510 : :
1511 : 9 : return 0;
1512 : 0 : }
1513 : :
1514 : : const char *
1515 : 9 : spdk_nvme_prchk_flags_str(uint32_t prchk_flags)
1516 : : {
1517 [ + + # # ]: 9 : if (prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) {
1518 [ + + # # ]: 6 : if (prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) {
1519 : 3 : return "prchk:reftag|guard";
1520 : : } else {
1521 : 3 : return "prchk:reftag";
1522 : : }
1523 : : } else {
1524 [ + - # # ]: 3 : if (prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) {
1525 : 3 : return "prchk:guard";
1526 : : } else {
1527 : 0 : return NULL;
1528 : : }
1529 : : }
1530 : 0 : }
1531 : :
1532 : : int
1533 : 65135 : spdk_nvme_scan_attached(const struct spdk_nvme_transport_id *trid)
1534 : : {
1535 : : int rc;
1536 : : struct spdk_nvme_probe_ctx *probe_ctx;
1537 : :
1538 : 65135 : rc = nvme_driver_init();
1539 [ - + ]: 65135 : if (rc != 0) {
1540 : 0 : return rc;
1541 : : }
1542 : :
1543 : 65135 : probe_ctx = calloc(1, sizeof(*probe_ctx));
1544 [ + + ]: 65135 : if (!probe_ctx) {
1545 : 0 : return -ENOMEM;
1546 : : }
1547 : :
1548 : 65135 : nvme_probe_ctx_init(probe_ctx, trid, NULL, NULL, NULL, NULL, NULL, NULL);
1549 : :
1550 [ - + ]: 65135 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
1551 : 65135 : rc = nvme_transport_ctrlr_scan_attached(probe_ctx);
1552 [ - + ]: 65135 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
1553 : 65135 : free(probe_ctx);
1554 : :
1555 [ + - ]: 65135 : return rc < 0 ? rc : 0;
1556 : 3 : }
1557 : :
1558 : : struct spdk_nvme_probe_ctx *
1559 : 3878 : spdk_nvme_probe_async(const struct spdk_nvme_transport_id *trid,
1560 : : void *cb_ctx,
1561 : : spdk_nvme_probe_cb probe_cb,
1562 : : spdk_nvme_attach_cb attach_cb,
1563 : : spdk_nvme_remove_cb remove_cb)
1564 : : {
1565 : 3878 : return spdk_nvme_probe_async_ext(trid, cb_ctx, probe_cb, attach_cb, NULL, remove_cb);
1566 : : }
1567 : :
1568 : : struct spdk_nvme_probe_ctx *
1569 : 122515 : spdk_nvme_probe_async_ext(const struct spdk_nvme_transport_id *trid,
1570 : : void *cb_ctx,
1571 : : spdk_nvme_probe_cb probe_cb,
1572 : : spdk_nvme_attach_cb attach_cb,
1573 : : spdk_nvme_attach_fail_cb attach_fail_cb,
1574 : : spdk_nvme_remove_cb remove_cb)
1575 : : {
1576 : : int rc;
1577 : : struct spdk_nvme_probe_ctx *probe_ctx;
1578 : :
1579 : 122515 : rc = nvme_driver_init();
1580 [ + + ]: 122515 : if (rc != 0) {
1581 : 3 : return NULL;
1582 : : }
1583 : :
1584 : 122512 : probe_ctx = calloc(1, sizeof(*probe_ctx));
1585 [ + + ]: 122512 : if (!probe_ctx) {
1586 : 0 : return NULL;
1587 : : }
1588 : :
1589 : 122513 : nvme_probe_ctx_init(probe_ctx, trid, NULL, cb_ctx, probe_cb, attach_cb, attach_fail_cb,
1590 : 1 : remove_cb);
1591 : 122512 : rc = nvme_probe_internal(probe_ctx, false);
1592 [ + + ]: 122512 : if (rc != 0) {
1593 : 5 : free(probe_ctx);
1594 : 5 : return NULL;
1595 : : }
1596 : :
1597 : 122507 : return probe_ctx;
1598 : 1 : }
1599 : :
1600 : : int
1601 : 146303398 : spdk_nvme_probe_poll_async(struct spdk_nvme_probe_ctx *probe_ctx)
1602 : : {
1603 : : struct spdk_nvme_ctrlr *ctrlr, *ctrlr_tmp;
1604 : :
1605 [ + + + - : 146303398 : if (!spdk_process_is_primary() && probe_ctx->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
# # # # #
# ]
1606 : 171 : free(probe_ctx);
1607 : 171 : return 0;
1608 : : }
1609 : :
1610 [ + + + - : 292519539 : TAILQ_FOREACH_SAFE(ctrlr, &probe_ctx->init_ctrlrs, tailq, ctrlr_tmp) {
+ - + + +
- + - + -
+ + ]
1611 : 146216312 : nvme_ctrlr_poll_internal(ctrlr, probe_ctx);
1612 : 31931824 : }
1613 : :
1614 [ + + + - : 146303227 : if (TAILQ_EMPTY(&probe_ctx->init_ctrlrs)) {
+ - + + ]
1615 [ - + ]: 125161 : nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock);
1616 [ - + - + ]: 125161 : g_spdk_nvme_driver->initialized = true;
1617 [ - + ]: 125161 : nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock);
1618 : 125161 : free(probe_ctx);
1619 : 125161 : return 0;
1620 : : }
1621 : :
1622 : 146178066 : return -EAGAIN;
1623 : 31931824 : }
1624 : :
1625 : : struct spdk_nvme_probe_ctx *
1626 : 2835 : spdk_nvme_connect_async(const struct spdk_nvme_transport_id *trid,
1627 : : const struct spdk_nvme_ctrlr_opts *opts,
1628 : : spdk_nvme_attach_cb attach_cb)
1629 : : {
1630 : : int rc;
1631 : 2835 : spdk_nvme_probe_cb probe_cb = NULL;
1632 : : struct spdk_nvme_probe_ctx *probe_ctx;
1633 : :
1634 : 2835 : rc = nvme_driver_init();
1635 [ - + ]: 2835 : if (rc != 0) {
1636 : 0 : return NULL;
1637 : : }
1638 : :
1639 : 2835 : probe_ctx = calloc(1, sizeof(*probe_ctx));
1640 [ + + ]: 2835 : if (!probe_ctx) {
1641 : 0 : return NULL;
1642 : : }
1643 : :
1644 [ + + ]: 2835 : if (opts) {
1645 : 1653 : probe_cb = nvme_connect_probe_cb;
1646 : 1 : }
1647 : :
1648 : 2835 : nvme_probe_ctx_init(probe_ctx, trid, opts, (void *)opts, probe_cb, attach_cb, NULL, NULL);
1649 : 2835 : rc = nvme_probe_internal(probe_ctx, true);
1650 [ + + ]: 2835 : if (rc != 0) {
1651 : 16 : free(probe_ctx);
1652 : 16 : return NULL;
1653 : : }
1654 : :
1655 : 2819 : return probe_ctx;
1656 : 796 : }
1657 : :
1658 : : int
1659 : 22634 : nvme_parse_addr(struct sockaddr_storage *sa, int family, const char *addr, const char *service,
1660 : : long int *port)
1661 : : {
1662 : 109 : struct addrinfo *res;
1663 : 109 : struct addrinfo hints;
1664 : : int ret;
1665 : :
1666 [ + + ]: 22634 : memset(&hints, 0, sizeof(hints));
1667 [ + - ]: 22634 : hints.ai_family = family;
1668 [ + - ]: 22634 : hints.ai_socktype = SOCK_STREAM;
1669 [ + - ]: 22634 : hints.ai_protocol = 0;
1670 : :
1671 [ + + ]: 22634 : if (service != NULL) {
1672 [ + - ]: 22619 : *port = spdk_strtol(service, 10);
1673 [ + - + + : 22619 : if (*port <= 0 || *port >= 65536) {
+ - - + ]
1674 : 0 : SPDK_ERRLOG("Invalid port: %s\n", service);
1675 : 0 : return -EINVAL;
1676 : : }
1677 : 1576 : }
1678 : :
1679 : 22634 : ret = getaddrinfo(addr, service, &hints, &res);
1680 [ + + ]: 22634 : if (ret) {
1681 : 3 : SPDK_ERRLOG("getaddrinfo failed: %s (%d)\n", gai_strerror(ret), ret);
1682 [ # # ]: 3 : return -(abs(ret));
1683 : : }
1684 : :
1685 [ + + + - : 22631 : if (res->ai_addrlen > sizeof(*sa)) {
- + ]
1686 [ # # # # ]: 0 : SPDK_ERRLOG("getaddrinfo() ai_addrlen %zu too large\n", (size_t)res->ai_addrlen);
1687 : 0 : ret = -EINVAL;
1688 : 0 : } else {
1689 [ + + + + : 22631 : memcpy(sa, res->ai_addr, res->ai_addrlen);
+ - + - +
- + - ]
1690 : : }
1691 : :
1692 : 22631 : freeaddrinfo(res);
1693 : 22631 : return ret;
1694 : 1576 : }
1695 : :
1696 : : int
1697 : 6383 : nvme_get_default_hostnqn(char *buf, int len)
1698 : : {
1699 : 935 : char uuid[SPDK_UUID_STRING_LEN];
1700 : : int rc;
1701 : :
1702 [ + - ]: 6383 : spdk_uuid_fmt_lower(uuid, sizeof(uuid), &g_spdk_nvme_driver->default_extended_host_id);
1703 [ - + ]: 6383 : rc = snprintf(buf, len, "nqn.2014-08.org.nvmexpress:uuid:%s", uuid);
1704 [ + - - + ]: 6383 : if (rc < 0 || rc >= len) {
1705 : 0 : return -EINVAL;
1706 : : }
1707 : :
1708 : 6383 : return 0;
1709 : 1593 : }
1710 : :
1711 : 2568 : SPDK_LOG_REGISTER_COMPONENT(nvme)
|