Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2020 Intel Corporation.
3 : : * Copyright (c) 2019-2022, Nutanix Inc. All rights reserved.
4 : : * Copyright (c) 2022, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : /*
8 : : * NVMe over vfio-user transport
9 : : */
10 : :
11 : : #include <sys/param.h>
12 : :
13 : : #include <vfio-user/libvfio-user.h>
14 : : #include <vfio-user/pci_defs.h>
15 : :
16 : : #include "spdk/barrier.h"
17 : : #include "spdk/stdinc.h"
18 : : #include "spdk/assert.h"
19 : : #include "spdk/thread.h"
20 : : #include "spdk/nvmf_transport.h"
21 : : #include "spdk/sock.h"
22 : : #include "spdk/string.h"
23 : : #include "spdk/util.h"
24 : : #include "spdk/log.h"
25 : :
26 : : #include "transport.h"
27 : :
28 : : #include "nvmf_internal.h"
29 : :
30 : : #define SWAP(x, y) \
31 : : do \
32 : : { \
33 : : typeof(x) _tmp = x; \
34 : : x = y; \
35 : : y = _tmp; \
36 : : } while (0)
37 : :
38 : : #define NVMF_VFIO_USER_DEFAULT_MAX_QUEUE_DEPTH 256
39 : : #define NVMF_VFIO_USER_DEFAULT_AQ_DEPTH 32
40 : : #define NVMF_VFIO_USER_DEFAULT_MAX_IO_SIZE ((NVMF_REQ_MAX_BUFFERS - 1) << SHIFT_4KB)
41 : : #define NVMF_VFIO_USER_DEFAULT_IO_UNIT_SIZE NVMF_VFIO_USER_DEFAULT_MAX_IO_SIZE
42 : :
43 : : #define NVME_DOORBELLS_OFFSET 0x1000
44 : : #define NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT 2
45 : : #define NVMF_VFIO_USER_SET_EVENTIDX_MAX_ATTEMPTS 3
46 : : #define NVMF_VFIO_USER_EVENTIDX_POLL UINT32_MAX
47 : :
48 : : #define NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR 512
49 : : #define NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR (NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR / 4)
50 : :
51 : : /* NVMe spec 1.4, section 5.21.1.7 */
52 : : SPDK_STATIC_ASSERT(NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR >= 2 &&
53 : : NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR <= SPDK_NVME_MAX_IO_QUEUES,
54 : : "bad number of queues");
55 : :
56 : : /*
57 : : * NVMe driver reads 4096 bytes, which is the extended PCI configuration space
58 : : * available on PCI-X 2.0 and PCI Express buses
59 : : */
60 : : #define NVME_REG_CFG_SIZE 0x1000
61 : :
62 : : /*
63 : : * Doorbells must be page aligned so that they can memory mapped.
64 : : *
65 : : * TODO does the NVMe spec also require this? Document it.
66 : : */
67 : : #define NVMF_VFIO_USER_DOORBELLS_SIZE \
68 : : SPDK_ALIGN_CEIL( \
69 : : (NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR * 2 * SPDK_NVME_DOORBELL_REGISTER_SIZE), \
70 : : 0x1000)
71 : : #define NVME_REG_BAR0_SIZE (NVME_DOORBELLS_OFFSET + NVMF_VFIO_USER_DOORBELLS_SIZE)
72 : :
73 : : /*
74 : : * TODO check the PCI spec whether BAR4 and BAR5 really have to be at least one
75 : : * page and a multiple of page size (maybe QEMU also needs this?). Document all
76 : : * this.
77 : : */
78 : :
79 : : #define NVMF_VFIO_USER_MSIX_NUM MAX(CHAR_BIT, NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR)
80 : :
81 : : #define NVMF_VFIO_USER_MSIX_TABLE_BIR (4)
82 : : #define NVMF_VFIO_USER_BAR4_SIZE SPDK_ALIGN_CEIL((NVMF_VFIO_USER_MSIX_NUM * 16), 0x1000)
83 : : SPDK_STATIC_ASSERT(NVMF_VFIO_USER_BAR4_SIZE > 0, "Incorrect size");
84 : :
85 : : /*
86 : : * TODO according to the PCI spec we need one bit per vector, document the
87 : : * relevant section.
88 : : */
89 : : #define NVMF_VFIO_USER_MSIX_PBA_BIR (5)
90 : : #define NVMF_VFIO_USER_BAR5_SIZE SPDK_ALIGN_CEIL((NVMF_VFIO_USER_MSIX_NUM / CHAR_BIT), 0x1000)
91 : : SPDK_STATIC_ASSERT(NVMF_VFIO_USER_BAR5_SIZE > 0, "Incorrect size");
92 : : struct nvmf_vfio_user_req;
93 : :
94 : : typedef int (*nvmf_vfio_user_req_cb_fn)(struct nvmf_vfio_user_req *req, void *cb_arg);
95 : :
96 : : /* 1 more for PRP2 list itself */
97 : : #define NVMF_VFIO_USER_MAX_IOVECS (NVMF_REQ_MAX_BUFFERS + 1)
98 : :
99 : : enum nvmf_vfio_user_req_state {
100 : : VFIO_USER_REQUEST_STATE_FREE = 0,
101 : : VFIO_USER_REQUEST_STATE_EXECUTING,
102 : : };
103 : :
104 : : /*
105 : : * Support for live migration in NVMf/vfio-user: live migration is implemented
106 : : * by stopping the NVMf subsystem when the device is instructed to enter the
107 : : * stop-and-copy state and then trivially, and most importantly safely,
108 : : * collecting migration state and providing it to the vfio-user client. We
109 : : * don't provide any migration state at the pre-copy state as that's too
110 : : * complicated to do, we might support this in the future.
111 : : */
112 : :
113 : :
114 : : /* NVMe device state representation */
115 : : struct nvme_migr_sq_state {
116 : : uint16_t sqid;
117 : : uint16_t cqid;
118 : : uint32_t head;
119 : : uint32_t size;
120 : : uint32_t reserved;
121 : : uint64_t dma_addr;
122 : : };
123 : : SPDK_STATIC_ASSERT(sizeof(struct nvme_migr_sq_state) == 0x18, "Incorrect size");
124 : :
125 : : struct nvme_migr_cq_state {
126 : : uint16_t cqid;
127 : : uint16_t phase;
128 : : uint32_t tail;
129 : : uint32_t size;
130 : : uint32_t iv;
131 : : uint32_t ien;
132 : : uint32_t reserved;
133 : : uint64_t dma_addr;
134 : : };
135 : : SPDK_STATIC_ASSERT(sizeof(struct nvme_migr_cq_state) == 0x20, "Incorrect size");
136 : :
137 : : #define VFIO_USER_MIGR_CALLBACK_VERS 1
138 : : #define VFIO_USER_NVME_MIGR_MAGIC 0xAFEDBC23
139 : :
140 : : /* The device state is in VFIO MIGRATION BAR(9) region, keep the device state page aligned.
141 : : *
142 : : * NVMe device migration region is defined as below:
143 : : * -------------------------------------------------------------------------
144 : : * | vfio_user_nvme_migr_header | nvmf controller data | queue pairs | BARs |
145 : : * -------------------------------------------------------------------------
146 : : *
147 : : * Keep vfio_user_nvme_migr_header as a fixed 0x1000 length, all new added fields
148 : : * can use the reserved space at the end of the data structure.
149 : : */
150 : : struct vfio_user_nvme_migr_header {
151 : : /* Magic value to validate migration data */
152 : : uint32_t magic;
153 : : /* Version to check the data is same from source to destination */
154 : : uint32_t version;
155 : :
156 : : /* The library uses this field to know how many fields in this
157 : : * structure are valid, starting at the beginning of this data
158 : : * structure. New added fields in future use `unused` memory
159 : : * spaces.
160 : : */
161 : : uint32_t opts_size;
162 : : uint32_t reserved0;
163 : :
164 : : /* BARs information */
165 : : uint64_t bar_offset[VFU_PCI_DEV_NUM_REGIONS];
166 : : uint64_t bar_len[VFU_PCI_DEV_NUM_REGIONS];
167 : :
168 : : /* Queue pair start offset, starting at the beginning of this
169 : : * data structure.
170 : : */
171 : : uint64_t qp_offset;
172 : : uint64_t qp_len;
173 : :
174 : : /* Controller data structure */
175 : : uint32_t num_io_queues;
176 : : uint32_t reserved1;
177 : :
178 : : /* NVMf controller data offset and length if exist, starting at
179 : : * the beginning of this data structure.
180 : : */
181 : : uint64_t nvmf_data_offset;
182 : : uint64_t nvmf_data_len;
183 : :
184 : : /*
185 : : * Whether or not shadow doorbells are used in the source. 0 is a valid DMA
186 : : * address.
187 : : */
188 : : uint32_t sdbl;
189 : :
190 : : /* Shadow doorbell DMA addresses. */
191 : : uint64_t shadow_doorbell_buffer;
192 : : uint64_t eventidx_buffer;
193 : :
194 : : /* Reserved memory space for new added fields, the
195 : : * field is always at the end of this data structure.
196 : : */
197 : : uint8_t unused[3856];
198 : : };
199 : : SPDK_STATIC_ASSERT(sizeof(struct vfio_user_nvme_migr_header) == 0x1000, "Incorrect size");
200 : :
201 : : struct vfio_user_nvme_migr_qp {
202 : : struct nvme_migr_sq_state sq;
203 : : struct nvme_migr_cq_state cq;
204 : : };
205 : :
206 : : /* NVMe state definition used to load/restore from/to NVMe migration BAR region */
207 : : struct vfio_user_nvme_migr_state {
208 : : struct vfio_user_nvme_migr_header ctrlr_header;
209 : : struct spdk_nvmf_ctrlr_migr_data nvmf_data;
210 : : struct vfio_user_nvme_migr_qp qps[NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR];
211 : : uint8_t doorbells[NVMF_VFIO_USER_DOORBELLS_SIZE];
212 : : uint8_t cfg[NVME_REG_CFG_SIZE];
213 : : };
214 : :
215 : : struct nvmf_vfio_user_req {
216 : : struct spdk_nvmf_request req;
217 : : struct spdk_nvme_cpl rsp;
218 : : struct spdk_nvme_cmd cmd;
219 : :
220 : : enum nvmf_vfio_user_req_state state;
221 : : nvmf_vfio_user_req_cb_fn cb_fn;
222 : : void *cb_arg;
223 : :
224 : : /* old CC before prop_set_cc fabric command */
225 : : union spdk_nvme_cc_register cc;
226 : :
227 : : TAILQ_ENTRY(nvmf_vfio_user_req) link;
228 : :
229 : : struct iovec iov[NVMF_VFIO_USER_MAX_IOVECS];
230 : : uint8_t iovcnt;
231 : :
232 : : /* NVMF_VFIO_USER_MAX_IOVECS worth of dma_sg_t. */
233 : : uint8_t sg[];
234 : : };
235 : :
236 : : #define MAP_R (0)
237 : : #define MAP_RW (1 << 0)
238 : : #define MAP_INITIALIZE (1 << 1)
239 : : #define MAP_QUIET (1 << 2)
240 : :
241 : : /*
242 : : * Mapping of an NVMe queue.
243 : : *
244 : : * This holds the information tracking a local process mapping of an NVMe queue
245 : : * shared by the client.
246 : : */
247 : : struct nvme_q_mapping {
248 : : /* iov of local process mapping. */
249 : : struct iovec iov;
250 : : /* Stored sg, needed for unmap. */
251 : : dma_sg_t *sg;
252 : : /* Client PRP of queue. */
253 : : uint64_t prp1;
254 : : /* Total length in bytes. */
255 : : uint64_t len;
256 : : };
257 : :
258 : : enum nvmf_vfio_user_sq_state {
259 : : VFIO_USER_SQ_UNUSED = 0,
260 : : VFIO_USER_SQ_CREATED,
261 : : VFIO_USER_SQ_DELETED,
262 : : VFIO_USER_SQ_ACTIVE,
263 : : VFIO_USER_SQ_INACTIVE
264 : : };
265 : :
266 : : enum nvmf_vfio_user_cq_state {
267 : : VFIO_USER_CQ_UNUSED = 0,
268 : : VFIO_USER_CQ_CREATED,
269 : : VFIO_USER_CQ_DELETED,
270 : : };
271 : :
272 : : enum nvmf_vfio_user_ctrlr_state {
273 : : VFIO_USER_CTRLR_CREATING = 0,
274 : : VFIO_USER_CTRLR_RUNNING,
275 : : /* Quiesce requested by libvfio-user */
276 : : VFIO_USER_CTRLR_PAUSING,
277 : : /* NVMf subsystem is paused, it's safe to do PCI reset, memory register,
278 : : * memory unergister, and vfio migration state transition in this state.
279 : : */
280 : : VFIO_USER_CTRLR_PAUSED,
281 : : /*
282 : : * Implies that the NVMf subsystem is paused. Device will be unquiesced (PCI
283 : : * reset, memory register and unregister, controller in destination VM has
284 : : * been restored). NVMf subsystem resume has been requested.
285 : : */
286 : : VFIO_USER_CTRLR_RESUMING,
287 : : /*
288 : : * Implies that the NVMf subsystem is paused. Both controller in source VM and
289 : : * destinatiom VM is in this state when doing live migration.
290 : : */
291 : : VFIO_USER_CTRLR_MIGRATING
292 : : };
293 : :
294 : : struct nvmf_vfio_user_sq {
295 : : struct spdk_nvmf_qpair qpair;
296 : : struct spdk_nvmf_transport_poll_group *group;
297 : : struct nvmf_vfio_user_ctrlr *ctrlr;
298 : :
299 : : uint32_t qid;
300 : : /* Number of entries in queue. */
301 : : uint32_t size;
302 : : struct nvme_q_mapping mapping;
303 : : enum nvmf_vfio_user_sq_state sq_state;
304 : :
305 : : uint32_t head;
306 : : volatile uint32_t *dbl_tailp;
307 : :
308 : : /* Whether a shadow doorbell eventidx needs setting. */
309 : : bool need_rearm;
310 : :
311 : : /* multiple SQs can be mapped to the same CQ */
312 : : uint16_t cqid;
313 : :
314 : : /* handle_queue_connect_rsp() can be used both for CREATE IO SQ response
315 : : * and SQ re-connect response in the destination VM, for the prior case,
316 : : * we will post a NVMe completion to VM, we will not set this flag when
317 : : * re-connecting SQs in the destination VM.
318 : : */
319 : : bool post_create_io_sq_completion;
320 : : /* Copy of Create IO SQ command, this field is used together with
321 : : * `post_create_io_sq_completion` flag.
322 : : */
323 : : struct spdk_nvme_cmd create_io_sq_cmd;
324 : :
325 : : struct vfio_user_delete_sq_ctx *delete_ctx;
326 : :
327 : : /* Currently unallocated reqs. */
328 : : TAILQ_HEAD(, nvmf_vfio_user_req) free_reqs;
329 : : /* Poll group entry */
330 : : TAILQ_ENTRY(nvmf_vfio_user_sq) link;
331 : : /* Connected SQ entry */
332 : : TAILQ_ENTRY(nvmf_vfio_user_sq) tailq;
333 : : };
334 : :
335 : : struct nvmf_vfio_user_cq {
336 : : struct spdk_nvmf_transport_poll_group *group;
337 : : int cq_ref;
338 : :
339 : : uint32_t qid;
340 : : /* Number of entries in queue. */
341 : : uint32_t size;
342 : : struct nvme_q_mapping mapping;
343 : : enum nvmf_vfio_user_cq_state cq_state;
344 : :
345 : : uint32_t tail;
346 : : volatile uint32_t *dbl_headp;
347 : :
348 : : bool phase;
349 : :
350 : : uint16_t iv;
351 : : bool ien;
352 : :
353 : : /* Number of outstanding IOs that will complete in this queue. */
354 : : size_t nr_outstanding;
355 : :
356 : : uint32_t last_head;
357 : : uint32_t last_trigger_irq_tail;
358 : : };
359 : :
360 : : struct nvmf_vfio_user_poll_group {
361 : : struct spdk_nvmf_transport_poll_group group;
362 : : TAILQ_ENTRY(nvmf_vfio_user_poll_group) link;
363 : : TAILQ_HEAD(, nvmf_vfio_user_sq) sqs;
364 : : struct spdk_interrupt *intr;
365 : : int intr_fd;
366 : : struct {
367 : :
368 : : /*
369 : : * ctrlr_intr and ctrlr_kicks will be zero for all other poll
370 : : * groups. However, they can be zero even for the poll group
371 : : * the controller belongs are if no vfio-user message has been
372 : : * received or the controller hasn't been kicked yet.
373 : : */
374 : :
375 : : /*
376 : : * Number of times vfio_user_ctrlr_intr() has run:
377 : : * vfio-user file descriptor has been ready or explicitly
378 : : * kicked (see below).
379 : : */
380 : : uint64_t ctrlr_intr;
381 : :
382 : : /*
383 : : * Kicks to the controller by ctrlr_kick().
384 : : * ctrlr_intr - ctrlr_kicks is the number of times the
385 : : * vfio-user poll file descriptor has been ready.
386 : : */
387 : : uint64_t ctrlr_kicks;
388 : :
389 : : /*
390 : : * Number of times this poll group was kicked.
391 : : */
392 : : uint64_t pg_kicks;
393 : :
394 : : /*
395 : : * How many times we won the race arming an SQ.
396 : : */
397 : : uint64_t won;
398 : :
399 : : /*
400 : : * How many times we lost the race arming an SQ
401 : : */
402 : : uint64_t lost;
403 : :
404 : : /*
405 : : * How many requests we processed in total each time we lost
406 : : * the rearm race.
407 : : */
408 : : uint64_t lost_count;
409 : :
410 : : /*
411 : : * Number of attempts we attempted to rearm all the SQs in the
412 : : * poll group.
413 : : */
414 : : uint64_t rearms;
415 : :
416 : : /*
417 : : * Number of times we had to apply flow control to this SQ.
418 : : */
419 : : uint64_t cq_full;
420 : :
421 : : uint64_t pg_process_count;
422 : : uint64_t intr;
423 : : uint64_t polls;
424 : : uint64_t polls_spurious;
425 : : uint64_t poll_reqs;
426 : : uint64_t poll_reqs_squared;
427 : : uint64_t cqh_admin_writes;
428 : : uint64_t cqh_io_writes;
429 : : } stats;
430 : :
431 : : /* Whether this PG needs kicking to wake up again. */
432 : : bool need_kick;
433 : : };
434 : :
435 : : struct nvmf_vfio_user_shadow_doorbells {
436 : : volatile uint32_t *shadow_doorbells;
437 : : volatile uint32_t *eventidxs;
438 : : dma_sg_t *sgs;
439 : : struct iovec *iovs;
440 : : };
441 : :
442 : : struct nvmf_vfio_user_ctrlr {
443 : : struct nvmf_vfio_user_endpoint *endpoint;
444 : : struct nvmf_vfio_user_transport *transport;
445 : :
446 : : /* Connected SQs list */
447 : : TAILQ_HEAD(, nvmf_vfio_user_sq) connected_sqs;
448 : : enum nvmf_vfio_user_ctrlr_state state;
449 : :
450 : : /*
451 : : * Tells whether live migration data have been prepared. This is used
452 : : * by the get_pending_bytes callback to tell whether or not the
453 : : * previous iteration finished.
454 : : */
455 : : bool migr_data_prepared;
456 : :
457 : : /* Controller is in source VM when doing live migration */
458 : : bool in_source_vm;
459 : :
460 : : struct spdk_thread *thread;
461 : : struct spdk_poller *vfu_ctx_poller;
462 : : struct spdk_interrupt *intr;
463 : : int intr_fd;
464 : :
465 : : bool queued_quiesce;
466 : :
467 : : bool reset_shn;
468 : : bool disconnect;
469 : :
470 : : uint16_t cntlid;
471 : : struct spdk_nvmf_ctrlr *ctrlr;
472 : :
473 : : struct nvmf_vfio_user_sq *sqs[NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR];
474 : : struct nvmf_vfio_user_cq *cqs[NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR];
475 : :
476 : : TAILQ_ENTRY(nvmf_vfio_user_ctrlr) link;
477 : :
478 : : volatile uint32_t *bar0_doorbells;
479 : : struct nvmf_vfio_user_shadow_doorbells *sdbl;
480 : : /*
481 : : * Shadow doorbells PRPs to provide during the stop-and-copy state.
482 : : */
483 : : uint64_t shadow_doorbell_buffer;
484 : : uint64_t eventidx_buffer;
485 : :
486 : : bool adaptive_irqs_enabled;
487 : : };
488 : :
489 : : /* Endpoint in vfio-user is associated with a socket file, which
490 : : * is the representative of a PCI endpoint.
491 : : */
492 : : struct nvmf_vfio_user_endpoint {
493 : : struct nvmf_vfio_user_transport *transport;
494 : : vfu_ctx_t *vfu_ctx;
495 : : struct spdk_poller *accept_poller;
496 : : struct spdk_thread *accept_thread;
497 : : bool interrupt_mode;
498 : : struct msixcap *msix;
499 : : vfu_pci_config_space_t *pci_config_space;
500 : : int devmem_fd;
501 : : int accept_intr_fd;
502 : : struct spdk_interrupt *accept_intr;
503 : :
504 : : volatile uint32_t *bar0_doorbells;
505 : :
506 : : int migr_fd;
507 : : void *migr_data;
508 : :
509 : : struct spdk_nvme_transport_id trid;
510 : : struct spdk_nvmf_subsystem *subsystem;
511 : :
512 : : /* Controller is associated with an active socket connection,
513 : : * the lifecycle of the controller is same as the VM.
514 : : * Currently we only support one active connection, as the NVMe
515 : : * specification defines, we may support multiple controllers in
516 : : * future, so that it can support e.g: RESERVATION.
517 : : */
518 : : struct nvmf_vfio_user_ctrlr *ctrlr;
519 : : pthread_mutex_t lock;
520 : :
521 : : bool need_async_destroy;
522 : : /* The subsystem is in PAUSED state and need to be resumed, TRUE
523 : : * only when migration is done successfully and the controller is
524 : : * in source VM.
525 : : */
526 : : bool need_resume;
527 : : /* Start the accept poller again after destroying the controller */
528 : : bool need_relisten;
529 : :
530 : : TAILQ_ENTRY(nvmf_vfio_user_endpoint) link;
531 : : };
532 : :
533 : : struct nvmf_vfio_user_transport_opts {
534 : : bool disable_mappable_bar0;
535 : : bool disable_adaptive_irq;
536 : : bool disable_shadow_doorbells;
537 : : bool disable_compare;
538 : : bool enable_intr_mode_sq_spreading;
539 : : };
540 : :
541 : : struct nvmf_vfio_user_transport {
542 : : struct spdk_nvmf_transport transport;
543 : : struct nvmf_vfio_user_transport_opts transport_opts;
544 : : bool intr_mode_supported;
545 : : pthread_mutex_t lock;
546 : : TAILQ_HEAD(, nvmf_vfio_user_endpoint) endpoints;
547 : :
548 : : pthread_mutex_t pg_lock;
549 : : TAILQ_HEAD(, nvmf_vfio_user_poll_group) poll_groups;
550 : : struct nvmf_vfio_user_poll_group *next_pg;
551 : : };
552 : :
553 : : /*
554 : : * function prototypes
555 : : */
556 : : static int nvmf_vfio_user_req_free(struct spdk_nvmf_request *req);
557 : :
558 : : static struct nvmf_vfio_user_req *get_nvmf_vfio_user_req(struct nvmf_vfio_user_sq *sq);
559 : :
560 : : /*
561 : : * Local process virtual address of a queue.
562 : : */
563 : : static inline void *
564 : 6650974 : q_addr(struct nvme_q_mapping *mapping)
565 : : {
566 [ + - + - : 6650974 : return mapping->iov.iov_base;
+ - ]
567 : : }
568 : :
569 : : static inline int
570 : 876 : queue_index(uint16_t qid, bool is_cq)
571 : : {
572 [ + - + - : 876 : return (qid * 2) + is_cq;
+ - ]
573 : : }
574 : :
575 : : static inline volatile uint32_t *
576 : 105917731 : sq_headp(struct nvmf_vfio_user_sq *sq)
577 : : {
578 [ + + # # ]: 105917731 : assert(sq != NULL);
579 [ + - ]: 105917731 : return &sq->head;
580 : : }
581 : :
582 : : static inline volatile uint32_t *
583 : 86492940 : sq_dbl_tailp(struct nvmf_vfio_user_sq *sq)
584 : : {
585 [ + + # # ]: 86492940 : assert(sq != NULL);
586 [ + - + - ]: 86492940 : return sq->dbl_tailp;
587 : : }
588 : :
589 : : static inline volatile uint32_t *
590 : 48071 : cq_dbl_headp(struct nvmf_vfio_user_cq *cq)
591 : : {
592 [ + + # # ]: 48071 : assert(cq != NULL);
593 [ + - + - ]: 48071 : return cq->dbl_headp;
594 : : }
595 : :
596 : : static inline volatile uint32_t *
597 : 48716758 : cq_tailp(struct nvmf_vfio_user_cq *cq)
598 : : {
599 [ + + # # ]: 48716758 : assert(cq != NULL);
600 [ + - ]: 48716758 : return &cq->tail;
601 : : }
602 : :
603 : : static inline void
604 : 3187270 : sq_head_advance(struct nvmf_vfio_user_sq *sq)
605 : : {
606 [ + + # # ]: 3187270 : assert(sq != NULL);
607 : :
608 [ + + + - : 3187270 : assert(*sq_headp(sq) < sq->size);
+ - # # ]
609 : 3187270 : (*sq_headp(sq))++;
610 : :
611 [ + + + - : 3187270 : if (spdk_unlikely(*sq_headp(sq) == sq->size)) {
+ + ]
612 : 13010 : *sq_headp(sq) = 0;
613 : 1164 : }
614 : 3187270 : }
615 : :
616 : : static inline void
617 : 3187090 : cq_tail_advance(struct nvmf_vfio_user_cq *cq)
618 : : {
619 [ + + # # ]: 3187090 : assert(cq != NULL);
620 : :
621 [ + + + - : 3187090 : assert(*cq_tailp(cq) < cq->size);
+ - # # ]
622 : 3187090 : (*cq_tailp(cq))++;
623 : :
624 [ + + + - : 3187090 : if (spdk_unlikely(*cq_tailp(cq) == cq->size)) {
+ + ]
625 : 13010 : *cq_tailp(cq) = 0;
626 [ + + + - : 13010 : cq->phase = !cq->phase;
+ - + - +
- ]
627 : 1164 : }
628 : 3187090 : }
629 : :
630 : : static bool
631 : 2116 : io_q_exists(struct nvmf_vfio_user_ctrlr *vu_ctrlr, const uint16_t qid, const bool is_cq)
632 : : {
633 [ + + # # ]: 2116 : assert(vu_ctrlr != NULL);
634 : :
635 [ + + + + ]: 2116 : if (qid == 0 || qid >= NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR) {
636 : 1158 : return false;
637 : : }
638 : :
639 [ + + + + ]: 958 : if (is_cq) {
640 [ + + + - : 575 : if (vu_ctrlr->cqs[qid] == NULL) {
+ - + - +
+ ]
641 : 195 : return false;
642 : : }
643 : :
644 [ + + + - : 755 : return (vu_ctrlr->cqs[qid]->cq_state != VFIO_USER_CQ_DELETED &&
+ - + - +
- + - -
+ ]
645 [ + + + - : 375 : vu_ctrlr->cqs[qid]->cq_state != VFIO_USER_CQ_UNUSED);
+ - + - +
- + - ]
646 : : }
647 : :
648 [ + + + - : 383 : if (vu_ctrlr->sqs[qid] == NULL) {
+ - + - +
+ ]
649 : 192 : return false;
650 : : }
651 : :
652 [ + + + - : 367 : return (vu_ctrlr->sqs[qid]->sq_state != VFIO_USER_SQ_DELETED &&
+ - + - +
- + - -
+ ]
653 [ + - + - : 176 : vu_ctrlr->sqs[qid]->sq_state != VFIO_USER_SQ_UNUSED);
+ - + - +
- + - ]
654 : 35 : }
655 : :
656 : : static char *
657 : 1749140 : endpoint_id(struct nvmf_vfio_user_endpoint *endpoint)
658 : : {
659 [ + - + - ]: 1749140 : return endpoint->trid.traddr;
660 : : }
661 : :
662 : : static char *
663 : 1748989 : ctrlr_id(struct nvmf_vfio_user_ctrlr *ctrlr)
664 : : {
665 [ + - + + : 1748989 : if (!ctrlr || !ctrlr->endpoint) {
+ - - + ]
666 : 0 : return "Null Ctrlr";
667 : : }
668 : :
669 [ + - + - ]: 1748989 : return endpoint_id(ctrlr->endpoint);
670 : 14 : }
671 : :
672 : : /* Return the poll group for the admin queue of the controller. */
673 : : static inline struct nvmf_vfio_user_poll_group *
674 : 0 : ctrlr_to_poll_group(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
675 : : {
676 [ # # # # : 0 : return SPDK_CONTAINEROF(vu_ctrlr->sqs[0]->group,
# # # # #
# # # ]
677 : : struct nvmf_vfio_user_poll_group,
678 : : group);
679 : : }
680 : :
681 : : static inline struct nvmf_vfio_user_poll_group *
682 : 0 : sq_to_poll_group(struct nvmf_vfio_user_sq *sq)
683 : : {
684 [ # # # # ]: 0 : return SPDK_CONTAINEROF(sq->group, struct nvmf_vfio_user_poll_group,
685 : : group);
686 : : }
687 : :
688 : : static inline struct spdk_thread *
689 : 944 : poll_group_to_thread(struct nvmf_vfio_user_poll_group *vu_pg)
690 : : {
691 [ # # # # : 944 : return vu_pg->group.group->thread;
# # # # #
# ]
692 : : }
693 : :
694 : : static dma_sg_t *
695 : 4588104 : index_to_sg_t(void *arr, size_t i)
696 : : {
697 : 4588104 : return (dma_sg_t *)((uintptr_t)arr + i * dma_sg_size());
698 : : }
699 : :
700 : : static inline size_t
701 : 155 : vfio_user_migr_data_len(void)
702 : : {
703 [ + - + - ]: 155 : return SPDK_ALIGN_CEIL(sizeof(struct vfio_user_nvme_migr_state), PAGE_SIZE);
704 : : }
705 : :
706 : : static inline bool
707 : 617 : in_interrupt_mode(struct nvmf_vfio_user_transport *vu_transport)
708 : : {
709 [ + + ]: 625 : return spdk_interrupt_mode_is_enabled() &&
710 [ - + + - : 8 : vu_transport->intr_mode_supported;
# # ]
711 : : }
712 : :
713 : : static int vfio_user_ctrlr_intr(void *ctx);
714 : :
715 : : static void
716 : 0 : vfio_user_ctrlr_intr_msg(void *ctx)
717 : : {
718 : 0 : vfio_user_ctrlr_intr(ctx);
719 : 0 : }
720 : :
721 : : /*
722 : : * Kick (force a wakeup) of all poll groups for this controller.
723 : : * vfio_user_ctrlr_intr() itself arranges for kicking other poll groups if
724 : : * needed.
725 : : */
726 : : static void
727 : 0 : ctrlr_kick(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
728 : : {
729 : : struct nvmf_vfio_user_poll_group *vu_ctrlr_group;
730 : :
731 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "%s: kicked\n", ctrlr_id(vu_ctrlr));
# # ]
732 : :
733 : 0 : vu_ctrlr_group = ctrlr_to_poll_group(vu_ctrlr);
734 : :
735 [ # # # # ]: 0 : vu_ctrlr_group->stats.ctrlr_kicks++;
736 : :
737 : 0 : spdk_thread_send_msg(poll_group_to_thread(vu_ctrlr_group),
738 : 0 : vfio_user_ctrlr_intr_msg, vu_ctrlr);
739 : 0 : }
740 : :
741 : : /*
742 : : * Force a wake-up for this particular poll group and its contained SQs.
743 : : */
744 : : static void
745 : 0 : poll_group_kick(struct nvmf_vfio_user_poll_group *vu_group)
746 : : {
747 [ # # # # ]: 0 : vu_group->stats.pg_kicks++;
748 [ # # # # : 0 : assert(vu_group->need_kick);
# # # # #
# ]
749 [ # # # # ]: 0 : vu_group->need_kick = false;
750 [ # # # # ]: 0 : eventfd_write(vu_group->intr_fd, 1);
751 : 0 : }
752 : :
753 : : /*
754 : : * Make the given DMA address and length available (locally mapped) via iov.
755 : : */
756 : : static void *
757 : 2743260 : map_one(vfu_ctx_t *ctx, uint64_t addr, uint64_t len, dma_sg_t *sg,
758 : : struct iovec *iov, int32_t flags)
759 : : {
760 : 2743260 : int prot = PROT_READ;
761 : : int ret;
762 : :
763 [ + + + - : 2743260 : if (flags & MAP_RW) {
+ + ]
764 : 2740743 : prot |= PROT_WRITE;
765 : 149584 : }
766 : :
767 [ + + # # ]: 2743260 : assert(ctx != NULL);
768 [ + + # # ]: 2743260 : assert(sg != NULL);
769 [ + + # # ]: 2743260 : assert(iov != NULL);
770 : :
771 : 2743260 : ret = vfu_addr_to_sgl(ctx, (void *)(uintptr_t)addr, len, sg, 1, prot);
772 [ + + ]: 2743260 : if (ret < 0) {
773 [ + - ]: 860930 : if (ret == -1) {
774 [ + + # # : 860930 : if (!(flags & MAP_QUIET)) {
# # ]
775 : 860880 : SPDK_ERRLOG("failed to translate IOVA [%#lx, %#lx) (prot=%d) to local VA: %m\n",
776 : : addr, addr + len, prot);
777 : 0 : }
778 : 0 : } else {
779 [ # # # # ]: 0 : SPDK_ERRLOG("failed to translate IOVA [%#lx, %#lx) (prot=%d) to local VA: %d segments needed\n",
780 : : addr, addr + len, prot, -(ret + 1));
781 : : }
782 : 860930 : return NULL;
783 : : }
784 : :
785 : 1882330 : ret = vfu_sgl_get(ctx, sg, iov, 1, 0);
786 [ - + ]: 1882330 : if (ret != 0) {
787 : 0 : SPDK_ERRLOG("failed to get iovec for IOVA [%#lx, %#lx): %m\n",
788 : : addr, addr + len);
789 : 0 : return NULL;
790 : : }
791 : :
792 [ + + + - : 1882330 : assert(iov->iov_base != NULL);
+ - # # ]
793 [ + - + - ]: 1882330 : return iov->iov_base;
794 : 149598 : }
795 : :
796 : : static int
797 : 871281 : nvme_cmd_map_prps(void *prv, struct spdk_nvme_cmd *cmd, struct iovec *iovs,
798 : : uint32_t max_iovcnt, uint32_t len, size_t mps,
799 : : void *(*gpa_to_vva)(void *prv, uint64_t addr, uint64_t len, uint32_t flags))
800 : : {
801 : : uint64_t prp1, prp2;
802 : : void *vva;
803 : : uint32_t i;
804 : : uint32_t residue_len, nents;
805 : : uint64_t *prp_list;
806 : : uint32_t iovcnt;
807 : :
808 [ + + # # ]: 871281 : assert(max_iovcnt > 0);
809 : :
810 [ + - + - : 871281 : prp1 = cmd->dptr.prp.prp1;
+ - + - ]
811 [ + - + - : 871281 : prp2 = cmd->dptr.prp.prp2;
+ - + - ]
812 : :
813 : : /* PRP1 may started with unaligned page address */
814 [ + + ]: 871281 : residue_len = mps - (prp1 % mps);
815 [ - + ]: 871281 : residue_len = spdk_min(len, residue_len);
816 : :
817 [ - + + - : 871281 : vva = gpa_to_vva(prv, prp1, residue_len, MAP_RW);
- + + - ]
818 [ + + ]: 871281 : if (spdk_unlikely(vva == NULL)) {
819 : 860880 : SPDK_ERRLOG("GPA to VVA failed\n");
820 : 860880 : return -EINVAL;
821 : : }
822 : 10401 : len -= residue_len;
823 [ + + - + ]: 10401 : if (len && max_iovcnt < 2) {
824 : 0 : SPDK_ERRLOG("Too many page entries, at least two iovs are required\n");
825 : 0 : return -ERANGE;
826 : : }
827 [ + - + - : 10401 : iovs[0].iov_base = vva;
+ - ]
828 [ + - + - : 10401 : iovs[0].iov_len = residue_len;
+ - ]
829 : :
830 [ + + ]: 10401 : if (len) {
831 [ + + ]: 4308 : if (spdk_unlikely(prp2 == 0)) {
832 : 1 : SPDK_ERRLOG("no PRP2, %d remaining\n", len);
833 : 1 : return -EINVAL;
834 : : }
835 : :
836 [ + + ]: 4307 : if (len <= mps) {
837 : : /* 2 PRP used */
838 : 2147 : iovcnt = 2;
839 [ # # # # : 2147 : vva = gpa_to_vva(prv, prp2, len, MAP_RW);
# # # # ]
840 [ - + ]: 2147 : if (spdk_unlikely(vva == NULL)) {
841 : 0 : SPDK_ERRLOG("no VVA for %#" PRIx64 ", len%#x\n",
842 : : prp2, len);
843 : 0 : return -EINVAL;
844 : : }
845 [ # # # # : 2147 : iovs[1].iov_base = vva;
# # ]
846 [ # # # # : 2147 : iovs[1].iov_len = len;
# # ]
847 : 0 : } else {
848 : : /* PRP list used */
849 [ - + ]: 2160 : nents = (len + mps - 1) / mps;
850 [ - + ]: 2160 : if (spdk_unlikely(nents + 1 > max_iovcnt)) {
851 : 0 : SPDK_ERRLOG("Too many page entries\n");
852 : 0 : return -ERANGE;
853 : : }
854 : :
855 [ # # # # ]: 2160 : vva = gpa_to_vva(prv, prp2, nents * sizeof(*prp_list), MAP_R);
856 [ - + ]: 2160 : if (spdk_unlikely(vva == NULL)) {
857 : 0 : SPDK_ERRLOG("no VVA for %#" PRIx64 ", nents=%#x\n",
858 : : prp2, nents);
859 : 0 : return -EINVAL;
860 : : }
861 : 2160 : prp_list = vva;
862 : 2160 : i = 0;
863 [ + + ]: 34040 : while (len != 0) {
864 [ # # ]: 31880 : residue_len = spdk_min(len, mps);
865 [ # # # # : 31880 : vva = gpa_to_vva(prv, prp_list[i], residue_len, MAP_RW);
# # # # #
# # # ]
866 [ - + ]: 31880 : if (spdk_unlikely(vva == NULL)) {
867 [ # # # # ]: 0 : SPDK_ERRLOG("no VVA for %#" PRIx64 ", residue_len=%#x\n",
868 : : prp_list[i], residue_len);
869 : 0 : return -EINVAL;
870 : : }
871 [ # # # # : 31880 : iovs[i + 1].iov_base = vva;
# # ]
872 [ # # # # : 31880 : iovs[i + 1].iov_len = residue_len;
# # ]
873 : 31880 : len -= residue_len;
874 : 31880 : i++;
875 : : }
876 : 2160 : iovcnt = i + 1;
877 : : }
878 : 0 : } else {
879 : : /* 1 PRP used */
880 : 6093 : iovcnt = 1;
881 : : }
882 : :
883 [ + + # # ]: 10400 : assert(iovcnt <= max_iovcnt);
884 : 10400 : return iovcnt;
885 : 28 : }
886 : :
887 : : static int
888 : 91 : nvme_cmd_map_sgls_data(void *prv, struct spdk_nvme_sgl_descriptor *sgls, uint32_t num_sgls,
889 : : struct iovec *iovs, uint32_t max_iovcnt,
890 : : void *(*gpa_to_vva)(void *prv, uint64_t addr, uint64_t len, uint32_t flags))
891 : : {
892 : : uint32_t i;
893 : : void *vva;
894 : :
895 [ - + ]: 91 : if (spdk_unlikely(max_iovcnt < num_sgls)) {
896 : 0 : return -ERANGE;
897 : : }
898 : :
899 [ + + ]: 282 : for (i = 0; i < num_sgls; i++) {
900 [ - + # # : 191 : if (spdk_unlikely(sgls[i].unkeyed.type != SPDK_NVME_SGL_TYPE_DATA_BLOCK)) {
# # # # #
# ]
901 [ # # # # : 0 : SPDK_ERRLOG("Invalid SGL type %u\n", sgls[i].unkeyed.type);
# # # # ]
902 : 0 : return -EINVAL;
903 : : }
904 [ # # # # : 191 : vva = gpa_to_vva(prv, sgls[i].address, sgls[i].unkeyed.length, MAP_RW);
# # # # #
# # # # #
# # # # #
# # # #
# ]
905 [ - + ]: 191 : if (spdk_unlikely(vva == NULL)) {
906 : 0 : SPDK_ERRLOG("GPA to VVA failed\n");
907 : 0 : return -EINVAL;
908 : : }
909 [ # # # # : 191 : iovs[i].iov_base = vva;
# # ]
910 [ # # # # : 191 : iovs[i].iov_len = sgls[i].unkeyed.length;
# # # # #
# # # # #
# # ]
911 : 0 : }
912 : :
913 : 91 : return num_sgls;
914 : 0 : }
915 : :
916 : : static int
917 : 1835022 : nvme_cmd_map_sgls(void *prv, struct spdk_nvme_cmd *cmd, struct iovec *iovs, uint32_t max_iovcnt,
918 : : uint32_t len, size_t mps,
919 : : void *(*gpa_to_vva)(void *prv, uint64_t addr, uint64_t len, uint32_t flags))
920 : : {
921 : : struct spdk_nvme_sgl_descriptor *sgl, *last_sgl;
922 : : uint32_t num_sgls, seg_len;
923 : : void *vva;
924 : : int ret;
925 : 1835022 : uint32_t total_iovcnt = 0;
926 : :
927 : : /* SGL cases */
928 [ + - + - ]: 1835022 : sgl = &cmd->dptr.sgl1;
929 : :
930 : : /* only one SGL segment */
931 [ + + + - : 1835022 : if (sgl->unkeyed.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) {
+ - - + ]
932 [ + + # # ]: 1834931 : assert(max_iovcnt > 0);
933 [ - + + - : 1834931 : vva = gpa_to_vva(prv, sgl->address, sgl->unkeyed.length, MAP_RW);
+ - + - +
- + - + -
+ - - + +
- ]
934 [ - + ]: 1834931 : if (spdk_unlikely(vva == NULL)) {
935 : 0 : SPDK_ERRLOG("GPA to VVA failed\n");
936 : 0 : return -EINVAL;
937 : : }
938 [ + - + - : 1834931 : iovs[0].iov_base = vva;
+ - ]
939 [ + - + - : 1834931 : iovs[0].iov_len = sgl->unkeyed.length;
+ - + - +
- + - +
- ]
940 [ + + + - : 1834931 : assert(sgl->unkeyed.length == len);
+ - + - +
- # # ]
941 : :
942 : 1834931 : return 1;
943 : : }
944 : :
945 : 0 : for (;;) {
946 [ + - - + : 91 : if (spdk_unlikely((sgl->unkeyed.type != SPDK_NVME_SGL_TYPE_SEGMENT) &&
# # # # #
# # # # #
# # ]
947 : : (sgl->unkeyed.type != SPDK_NVME_SGL_TYPE_LAST_SEGMENT))) {
948 [ # # # # : 0 : SPDK_ERRLOG("Invalid SGL type %u\n", sgl->unkeyed.type);
# # ]
949 : 0 : return -EINVAL;
950 : : }
951 : :
952 [ # # # # : 91 : seg_len = sgl->unkeyed.length;
# # # # ]
953 [ - + # # ]: 91 : if (spdk_unlikely(seg_len % sizeof(struct spdk_nvme_sgl_descriptor))) {
954 : 0 : SPDK_ERRLOG("Invalid SGL segment len %u\n", seg_len);
955 : 0 : return -EINVAL;
956 : : }
957 : :
958 [ # # ]: 91 : num_sgls = seg_len / sizeof(struct spdk_nvme_sgl_descriptor);
959 [ # # # # : 91 : vva = gpa_to_vva(prv, sgl->address, sgl->unkeyed.length, MAP_R);
# # # # #
# # # # #
# # ]
960 [ - + ]: 91 : if (spdk_unlikely(vva == NULL)) {
961 : 0 : SPDK_ERRLOG("GPA to VVA failed\n");
962 : 0 : return -EINVAL;
963 : : }
964 : :
965 : : /* sgl point to the first segment */
966 : 91 : sgl = (struct spdk_nvme_sgl_descriptor *)vva;
967 [ # # ]: 91 : last_sgl = &sgl[num_sgls - 1];
968 : :
969 : : /* we are done */
970 [ + - # # : 91 : if (last_sgl->unkeyed.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) {
# # # # ]
971 : : /* map whole sgl list */
972 [ # # ]: 91 : ret = nvme_cmd_map_sgls_data(prv, sgl, num_sgls, &iovs[total_iovcnt],
973 : 0 : max_iovcnt - total_iovcnt, gpa_to_vva);
974 [ - + ]: 91 : if (spdk_unlikely(ret < 0)) {
975 : 0 : return ret;
976 : : }
977 : 91 : total_iovcnt += ret;
978 : :
979 : 91 : return total_iovcnt;
980 : : }
981 : :
982 [ # # ]: 0 : if (num_sgls > 1) {
983 : : /* map whole sgl exclude last_sgl */
984 [ # # ]: 0 : ret = nvme_cmd_map_sgls_data(prv, sgl, num_sgls - 1, &iovs[total_iovcnt],
985 : 0 : max_iovcnt - total_iovcnt, gpa_to_vva);
986 [ # # ]: 0 : if (spdk_unlikely(ret < 0)) {
987 : 0 : return ret;
988 : : }
989 : 0 : total_iovcnt += ret;
990 : 0 : }
991 : :
992 : : /* move to next level's segments */
993 : 0 : sgl = last_sgl;
994 : : }
995 : :
996 : : return 0;
997 : 149542 : }
998 : :
999 : : static int
1000 : 2706303 : nvme_map_cmd(void *prv, struct spdk_nvme_cmd *cmd, struct iovec *iovs, uint32_t max_iovcnt,
1001 : : uint32_t len, size_t mps,
1002 : : void *(*gpa_to_vva)(void *prv, uint64_t addr, uint64_t len, uint32_t flags))
1003 : : {
1004 [ + + + + ]: 2706303 : if (cmd->psdt == SPDK_NVME_PSDT_PRP) {
1005 : 871281 : return nvme_cmd_map_prps(prv, cmd, iovs, max_iovcnt, len, mps, gpa_to_vva);
1006 : : }
1007 : :
1008 : 1835022 : return nvme_cmd_map_sgls(prv, cmd, iovs, max_iovcnt, len, mps, gpa_to_vva);
1009 : 149570 : }
1010 : :
1011 : : /*
1012 : : * For each queue, update the location of its doorbell to the correct location:
1013 : : * either our own BAR0, or the guest's configured shadow doorbell area.
1014 : : *
1015 : : * The Admin queue (qid: 0) does not ever use shadow doorbells.
1016 : : */
1017 : : static void
1018 : 56 : vfio_user_ctrlr_switch_doorbells(struct nvmf_vfio_user_ctrlr *ctrlr, bool shadow)
1019 : : {
1020 [ + + - + : 63 : volatile uint32_t *doorbells = shadow ? ctrlr->sdbl->shadow_doorbells :
# # # # #
# # # ]
1021 [ + - + - ]: 7 : ctrlr->bar0_doorbells;
1022 : :
1023 [ + + # # ]: 56 : assert(doorbells != NULL);
1024 : :
1025 [ + + ]: 7168 : for (size_t i = 1; i < NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR; i++) {
1026 [ + - + - : 7112 : struct nvmf_vfio_user_sq *sq = ctrlr->sqs[i];
+ - + - ]
1027 [ + - + - : 7112 : struct nvmf_vfio_user_cq *cq = ctrlr->cqs[i];
+ - + - ]
1028 : :
1029 [ + + ]: 7112 : if (sq != NULL) {
1030 [ + - + - : 184 : sq->dbl_tailp = doorbells + queue_index(sq->qid, false);
+ - + - +
- ]
1031 : :
1032 [ + - + - : 184 : ctrlr->sqs[i]->need_rearm = shadow;
+ - + - +
- + - +
- ]
1033 : 7 : }
1034 : :
1035 [ + + ]: 7112 : if (cq != NULL) {
1036 [ + - + - : 183 : cq->dbl_headp = doorbells + queue_index(cq->qid, true);
+ - + - +
- ]
1037 : 7 : }
1038 : 889 : }
1039 : 56 : }
1040 : :
1041 : : static void
1042 : 0 : unmap_sdbl(vfu_ctx_t *vfu_ctx, struct nvmf_vfio_user_shadow_doorbells *sdbl)
1043 : : {
1044 [ # # # # ]: 0 : assert(vfu_ctx != NULL);
1045 [ # # # # ]: 0 : assert(sdbl != NULL);
1046 : :
1047 : : /*
1048 : : * An allocation error would result in only one of the two being
1049 : : * non-NULL. If that is the case, no memory should have been mapped.
1050 : : */
1051 [ # # # # : 0 : if (sdbl->iovs == NULL || sdbl->sgs == NULL) {
# # # # #
# # # ]
1052 : 0 : return;
1053 : : }
1054 : :
1055 [ # # ]: 0 : for (size_t i = 0; i < NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT; ++i) {
1056 : : struct iovec *iov;
1057 : : dma_sg_t *sg;
1058 : :
1059 [ # # # # : 0 : if (!sdbl->iovs[i].iov_len) {
# # # # #
# # # ]
1060 : 0 : continue;
1061 : : }
1062 : :
1063 [ # # # # ]: 0 : sg = index_to_sg_t(sdbl->sgs, i);
1064 [ # # # # : 0 : iov = sdbl->iovs + i;
# # ]
1065 : :
1066 : 0 : vfu_sgl_put(vfu_ctx, sg, iov, 1);
1067 : 0 : }
1068 : 0 : }
1069 : :
1070 : : static void
1071 : 183 : free_sdbl(vfu_ctx_t *vfu_ctx, struct nvmf_vfio_user_shadow_doorbells *sdbl)
1072 : : {
1073 [ + + ]: 183 : if (sdbl == NULL) {
1074 : 183 : return;
1075 : : }
1076 : :
1077 : 0 : unmap_sdbl(vfu_ctx, sdbl);
1078 : :
1079 : : /*
1080 : : * sdbl->shadow_doorbells and sdbl->eventidxs were mapped,
1081 : : * not allocated, so don't free() them.
1082 : : */
1083 [ # # # # ]: 0 : free(sdbl->sgs);
1084 [ # # # # ]: 0 : free(sdbl->iovs);
1085 : 0 : free(sdbl);
1086 : 87 : }
1087 : :
1088 : : static struct nvmf_vfio_user_shadow_doorbells *
1089 : 0 : map_sdbl(vfu_ctx_t *vfu_ctx, uint64_t prp1, uint64_t prp2, size_t len)
1090 : : {
1091 : 0 : struct nvmf_vfio_user_shadow_doorbells *sdbl = NULL;
1092 : 0 : dma_sg_t *sg2 = NULL;
1093 : : void *p;
1094 : :
1095 [ # # # # ]: 0 : assert(vfu_ctx != NULL);
1096 : :
1097 : 0 : sdbl = calloc(1, sizeof(*sdbl));
1098 [ # # ]: 0 : if (sdbl == NULL) {
1099 : 0 : goto err;
1100 : : }
1101 : :
1102 [ # # # # ]: 0 : sdbl->sgs = calloc(NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT, dma_sg_size());
1103 [ # # # # ]: 0 : sdbl->iovs = calloc(NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT, sizeof(*sdbl->iovs));
1104 [ # # # # : 0 : if (sdbl->sgs == NULL || sdbl->iovs == NULL) {
# # # # #
# # # ]
1105 : 0 : goto err;
1106 : : }
1107 : :
1108 : : /* Map shadow doorbell buffer (PRP1). */
1109 [ # # # # : 0 : p = map_one(vfu_ctx, prp1, len, sdbl->sgs, sdbl->iovs, MAP_RW);
# # # # #
# # # ]
1110 : :
1111 [ # # ]: 0 : if (p == NULL) {
1112 : 0 : goto err;
1113 : : }
1114 : :
1115 : : /*
1116 : : * Map eventidx buffer (PRP2).
1117 : : * Should only be written to by the controller.
1118 : : */
1119 : :
1120 [ # # # # ]: 0 : sg2 = index_to_sg_t(sdbl->sgs, 1);
1121 : :
1122 [ # # # # : 0 : p = map_one(vfu_ctx, prp2, len, sg2, sdbl->iovs + 1, MAP_RW);
# # # # #
# ]
1123 : :
1124 [ # # ]: 0 : if (p == NULL) {
1125 : 0 : goto err;
1126 : : }
1127 : :
1128 [ # # # # : 0 : sdbl->shadow_doorbells = (uint32_t *)sdbl->iovs[0].iov_base;
# # # # #
# # # #
# ]
1129 [ # # # # : 0 : sdbl->eventidxs = (uint32_t *)sdbl->iovs[1].iov_base;
# # # # #
# # # #
# ]
1130 : :
1131 : 0 : return sdbl;
1132 : :
1133 : 0 : err:
1134 : 0 : free_sdbl(vfu_ctx, sdbl);
1135 : 0 : return NULL;
1136 : 0 : }
1137 : :
1138 : : /*
1139 : : * Copy doorbells from one buffer to the other, during switches between BAR0
1140 : : * doorbells and shadow doorbells.
1141 : : */
1142 : : static void
1143 : 0 : copy_doorbells(struct nvmf_vfio_user_ctrlr *ctrlr,
1144 : : const volatile uint32_t *from, volatile uint32_t *to)
1145 : : {
1146 [ # # # # ]: 0 : assert(ctrlr != NULL);
1147 [ # # # # ]: 0 : assert(from != NULL);
1148 [ # # # # ]: 0 : assert(to != NULL);
1149 : :
1150 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db,
# # ]
1151 : : "%s: migrating shadow doorbells from %p to %p\n",
1152 : : ctrlr_id(ctrlr), from, to);
1153 : :
1154 : : /* Can't use memcpy because it doesn't respect volatile semantics. */
1155 [ # # ]: 0 : for (size_t i = 0; i < NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR; ++i) {
1156 [ # # # # : 0 : if (ctrlr->sqs[i] != NULL) {
# # # # #
# ]
1157 [ # # # # ]: 0 : to[queue_index(i, false)] = from[queue_index(i, false)];
1158 : 0 : }
1159 : :
1160 [ # # # # : 0 : if (ctrlr->cqs[i] != NULL) {
# # # # #
# ]
1161 [ # # # # ]: 0 : to[queue_index(i, true)] = from[queue_index(i, true)];
1162 : 0 : }
1163 : 0 : }
1164 : 0 : }
1165 : :
1166 : : static void
1167 : 0 : fail_ctrlr(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
1168 : : {
1169 : : const struct spdk_nvmf_registers *regs;
1170 : :
1171 [ # # # # ]: 0 : assert(vu_ctrlr != NULL);
1172 [ # # # # : 0 : assert(vu_ctrlr->ctrlr != NULL);
# # # # ]
1173 : :
1174 [ # # # # ]: 0 : regs = spdk_nvmf_ctrlr_get_regs(vu_ctrlr->ctrlr);
1175 [ # # # # : 0 : if (regs->csts.bits.cfs == 0) {
# # # # ]
1176 : 0 : SPDK_ERRLOG(":%s failing controller\n", ctrlr_id(vu_ctrlr));
1177 : 0 : }
1178 : :
1179 [ # # # # ]: 0 : nvmf_ctrlr_set_fatal_status(vu_ctrlr->ctrlr);
1180 : 0 : }
1181 : :
1182 : : static inline bool
1183 : 36151776 : ctrlr_interrupt_enabled(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
1184 : : {
1185 [ + + # # ]: 36151776 : assert(vu_ctrlr != NULL);
1186 [ + + + - : 36151776 : assert(vu_ctrlr->endpoint != NULL);
+ - # # ]
1187 : :
1188 [ + - + - : 36151776 : vfu_pci_config_space_t *pci = vu_ctrlr->endpoint->pci_config_space;
+ - + - ]
1189 : :
1190 [ + + + + : 36151776 : return (!pci->hdr.cmd.id || vu_ctrlr->endpoint->msix->mxc.mxe);
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - ]
1191 : : }
1192 : :
1193 : : static void
1194 : 31 : nvmf_vfio_user_destroy_endpoint(struct nvmf_vfio_user_endpoint *endpoint)
1195 : : {
1196 [ + + + + : 31 : SPDK_DEBUGLOG(nvmf_vfio, "destroy endpoint %s\n", endpoint_id(endpoint));
+ - ]
1197 : :
1198 [ + - ]: 31 : spdk_interrupt_unregister(&endpoint->accept_intr);
1199 [ + - ]: 31 : spdk_poller_unregister(&endpoint->accept_poller);
1200 : :
1201 [ + - + - : 31 : if (endpoint->bar0_doorbells) {
+ - ]
1202 [ + - + - ]: 31 : munmap((void *)endpoint->bar0_doorbells, NVMF_VFIO_USER_DOORBELLS_SIZE);
1203 : 14 : }
1204 : :
1205 [ + - + - : 31 : if (endpoint->devmem_fd > 0) {
+ - ]
1206 [ + - + - ]: 31 : close(endpoint->devmem_fd);
1207 : 14 : }
1208 : :
1209 [ + - + - : 31 : if (endpoint->migr_data) {
+ - ]
1210 [ + - + - ]: 31 : munmap(endpoint->migr_data, vfio_user_migr_data_len());
1211 : 14 : }
1212 : :
1213 [ + - + - : 31 : if (endpoint->migr_fd > 0) {
+ - ]
1214 [ + - + - ]: 31 : close(endpoint->migr_fd);
1215 : 14 : }
1216 : :
1217 [ + - + - : 31 : if (endpoint->vfu_ctx) {
+ - ]
1218 [ + - + - ]: 31 : vfu_destroy_ctx(endpoint->vfu_ctx);
1219 : 14 : }
1220 : :
1221 [ + + + - ]: 31 : pthread_mutex_destroy(&endpoint->lock);
1222 : 31 : free(endpoint);
1223 : 31 : }
1224 : :
1225 : : /* called when process exits */
1226 : : static int
1227 : 14 : nvmf_vfio_user_destroy(struct spdk_nvmf_transport *transport,
1228 : : spdk_nvmf_transport_destroy_done_cb cb_fn, void *cb_arg)
1229 : : {
1230 : : struct nvmf_vfio_user_transport *vu_transport;
1231 : : struct nvmf_vfio_user_endpoint *endpoint, *tmp;
1232 : :
1233 [ + + + + : 14 : SPDK_DEBUGLOG(nvmf_vfio, "destroy transport\n");
+ - ]
1234 : :
1235 : 14 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
1236 : : transport);
1237 : :
1238 [ + + + - ]: 14 : pthread_mutex_destroy(&vu_transport->lock);
1239 [ + + + - ]: 14 : pthread_mutex_destroy(&vu_transport->pg_lock);
1240 : :
1241 [ + + + - : 14 : TAILQ_FOREACH_SAFE(endpoint, &vu_transport->endpoints, link, tmp) {
+ - - + #
# # # # #
+ - ]
1242 [ # # # # : 0 : TAILQ_REMOVE(&vu_transport->endpoints, endpoint, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1243 : 0 : nvmf_vfio_user_destroy_endpoint(endpoint);
1244 : 0 : }
1245 : :
1246 : 14 : free(vu_transport);
1247 : :
1248 [ + - ]: 14 : if (cb_fn) {
1249 [ - + + - ]: 14 : cb_fn(cb_arg);
1250 : 7 : }
1251 : :
1252 : 14 : return 0;
1253 : : }
1254 : :
1255 : : static const struct spdk_json_object_decoder vfio_user_transport_opts_decoder[] = {
1256 : : {
1257 : : "disable_mappable_bar0",
1258 : : offsetof(struct nvmf_vfio_user_transport, transport_opts.disable_mappable_bar0),
1259 : : spdk_json_decode_bool, true
1260 : : },
1261 : : {
1262 : : "disable_adaptive_irq",
1263 : : offsetof(struct nvmf_vfio_user_transport, transport_opts.disable_adaptive_irq),
1264 : : spdk_json_decode_bool, true
1265 : : },
1266 : : {
1267 : : "disable_shadow_doorbells",
1268 : : offsetof(struct nvmf_vfio_user_transport, transport_opts.disable_shadow_doorbells),
1269 : : spdk_json_decode_bool, true
1270 : : },
1271 : : {
1272 : : "disable_compare",
1273 : : offsetof(struct nvmf_vfio_user_transport, transport_opts.disable_compare),
1274 : : spdk_json_decode_bool, true
1275 : : },
1276 : : {
1277 : : "enable_intr_mode_sq_spreading",
1278 : : offsetof(struct nvmf_vfio_user_transport, transport_opts.enable_intr_mode_sq_spreading),
1279 : : spdk_json_decode_bool, true
1280 : : },
1281 : : };
1282 : :
1283 : : static struct spdk_nvmf_transport *
1284 : 14 : nvmf_vfio_user_create(struct spdk_nvmf_transport_opts *opts)
1285 : : {
1286 : : struct nvmf_vfio_user_transport *vu_transport;
1287 : : int err;
1288 : :
1289 [ + + + - : 14 : if (opts->max_qpairs_per_ctrlr > NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR) {
- + ]
1290 [ # # # # ]: 0 : SPDK_ERRLOG("Invalid max_qpairs_per_ctrlr=%d, supported max_qpairs_per_ctrlr=%d\n",
1291 : : opts->max_qpairs_per_ctrlr, NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR);
1292 : 0 : return NULL;
1293 : : }
1294 : :
1295 : 14 : vu_transport = calloc(1, sizeof(*vu_transport));
1296 [ + + ]: 14 : if (vu_transport == NULL) {
1297 : 0 : SPDK_ERRLOG("Transport alloc fail: %m\n");
1298 : 0 : return NULL;
1299 : : }
1300 : :
1301 [ + + + - ]: 14 : err = pthread_mutex_init(&vu_transport->lock, NULL);
1302 [ - + ]: 14 : if (err != 0) {
1303 : 0 : SPDK_ERRLOG("Pthread initialisation failed (%d)\n", err);
1304 : 0 : goto err;
1305 : : }
1306 [ + - + - : 14 : TAILQ_INIT(&vu_transport->endpoints);
+ - + - +
- + - + -
+ - ]
1307 : :
1308 [ + + + - ]: 14 : err = pthread_mutex_init(&vu_transport->pg_lock, NULL);
1309 [ - + ]: 14 : if (err != 0) {
1310 [ # # # # ]: 0 : pthread_mutex_destroy(&vu_transport->lock);
1311 : 0 : SPDK_ERRLOG("Pthread initialisation failed (%d)\n", err);
1312 : 0 : goto err;
1313 : : }
1314 [ + - + - : 14 : TAILQ_INIT(&vu_transport->poll_groups);
+ - + - +
- + - + -
+ - ]
1315 : :
1316 [ + - + + : 28 : if (opts->transport_specific != NULL &&
+ - + - ]
1317 [ + - + - ]: 14 : spdk_json_decode_object_relaxed(opts->transport_specific, vfio_user_transport_opts_decoder,
1318 : : SPDK_COUNTOF(vfio_user_transport_opts_decoder),
1319 : 7 : vu_transport)) {
1320 : 0 : SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
1321 : 0 : goto cleanup;
1322 : : }
1323 : :
1324 : : /*
1325 : : * To support interrupt mode, the transport must be configured with
1326 : : * mappable BAR0 disabled: we need a vfio-user message to wake us up
1327 : : * when a client writes new doorbell values to BAR0, via the
1328 : : * libvfio-user socket fd.
1329 : : */
1330 [ + - + - ]: 14 : vu_transport->intr_mode_supported =
1331 [ + + + - : 14 : vu_transport->transport_opts.disable_mappable_bar0;
+ - + - ]
1332 : :
1333 : : /*
1334 : : * If BAR0 is mappable, it doesn't make sense to support shadow
1335 : : * doorbells, so explicitly turn it off.
1336 : : */
1337 [ + + + + : 14 : if (!vu_transport->transport_opts.disable_mappable_bar0) {
+ - + - -
+ ]
1338 [ + - + - : 13 : vu_transport->transport_opts.disable_shadow_doorbells = true;
+ - ]
1339 : 7 : }
1340 : :
1341 [ + + ]: 14 : if (spdk_interrupt_mode_is_enabled()) {
1342 [ - + - + : 1 : if (!vu_transport->intr_mode_supported) {
# # # # ]
1343 : 0 : SPDK_ERRLOG("interrupt mode not supported\n");
1344 : 0 : goto cleanup;
1345 : : }
1346 : :
1347 : : /*
1348 : : * If we are in interrupt mode, we cannot support adaptive IRQs,
1349 : : * as there is no guarantee the SQ poller will run subsequently
1350 : : * to send pending IRQs.
1351 : : */
1352 [ # # # # : 1 : vu_transport->transport_opts.disable_adaptive_irq = true;
# # ]
1353 : 0 : }
1354 : :
1355 [ + + + + : 14 : SPDK_DEBUGLOG(nvmf_vfio, "vfio_user transport: disable_mappable_bar0=%d\n",
+ - # # #
# # # #
# ]
1356 : : vu_transport->transport_opts.disable_mappable_bar0);
1357 [ + + + + : 14 : SPDK_DEBUGLOG(nvmf_vfio, "vfio_user transport: disable_adaptive_irq=%d\n",
+ - # # #
# # # #
# ]
1358 : : vu_transport->transport_opts.disable_adaptive_irq);
1359 [ + + + + : 14 : SPDK_DEBUGLOG(nvmf_vfio, "vfio_user transport: disable_shadow_doorbells=%d\n",
+ - # # #
# # # #
# ]
1360 : : vu_transport->transport_opts.disable_shadow_doorbells);
1361 : :
1362 [ + - ]: 14 : return &vu_transport->transport;
1363 : :
1364 : 0 : cleanup:
1365 [ # # # # ]: 0 : pthread_mutex_destroy(&vu_transport->lock);
1366 [ # # # # ]: 0 : pthread_mutex_destroy(&vu_transport->pg_lock);
1367 : 0 : err:
1368 : 0 : free(vu_transport);
1369 : 0 : return NULL;
1370 : 7 : }
1371 : :
1372 : : static uint32_t
1373 : 398 : max_queue_size(struct nvmf_vfio_user_ctrlr const *vu_ctrlr)
1374 : : {
1375 [ + + # # ]: 398 : assert(vu_ctrlr != NULL);
1376 [ + + + - : 398 : assert(vu_ctrlr->ctrlr != NULL);
+ - # # ]
1377 : :
1378 [ + - + - : 398 : return vu_ctrlr->ctrlr->vcprop.cap.bits.mqes + 1;
+ - + - +
- + - +
- ]
1379 : : }
1380 : :
1381 : : static uint32_t
1382 : 0 : doorbell_stride(const struct nvmf_vfio_user_ctrlr *vu_ctrlr)
1383 : : {
1384 [ # # # # ]: 0 : assert(vu_ctrlr != NULL);
1385 [ # # # # : 0 : assert(vu_ctrlr->ctrlr != NULL);
# # # # ]
1386 : :
1387 [ # # # # : 0 : return vu_ctrlr->ctrlr->vcprop.cap.bits.dstrd;
# # # # #
# # # ]
1388 : : }
1389 : :
1390 : : static uintptr_t
1391 : 0 : memory_page_size(const struct nvmf_vfio_user_ctrlr *vu_ctrlr)
1392 : : {
1393 [ # # # # : 0 : uint32_t memory_page_shift = vu_ctrlr->ctrlr->vcprop.cc.bits.mps + 12;
# # # # #
# # # #
# ]
1394 [ # # ]: 0 : return 1ul << memory_page_shift;
1395 : : }
1396 : :
1397 : : static uintptr_t
1398 : 0 : memory_page_mask(const struct nvmf_vfio_user_ctrlr *ctrlr)
1399 : : {
1400 : 0 : return ~(memory_page_size(ctrlr) - 1);
1401 : : }
1402 : :
1403 : : static int
1404 : 579 : map_q(struct nvmf_vfio_user_ctrlr *vu_ctrlr, struct nvme_q_mapping *mapping,
1405 : : uint32_t flags)
1406 : : {
1407 : : void *ret;
1408 : :
1409 [ + + + - : 579 : assert(mapping->len != 0);
+ - # # ]
1410 [ + + # # ]: 579 : assert(q_addr(mapping) == NULL);
1411 : :
1412 [ + - + - : 607 : ret = map_one(vu_ctrlr->endpoint->vfu_ctx, mapping->prp1, mapping->len,
+ - + - +
- + - + -
+ - ]
1413 [ + - + - : 28 : mapping->sg, &mapping->iov, flags);
+ - ]
1414 [ + + ]: 579 : if (ret == NULL) {
1415 : 50 : return -EFAULT;
1416 : : }
1417 : :
1418 [ + + + - : 529 : if (flags & MAP_INITIALIZE) {
- + ]
1419 [ + + + - : 509 : memset(q_addr(mapping), 0, mapping->len);
+ - ]
1420 : 28 : }
1421 : :
1422 : 529 : return 0;
1423 : 28 : }
1424 : :
1425 : : static inline void
1426 : 1386 : unmap_q(struct nvmf_vfio_user_ctrlr *vu_ctrlr, struct nvme_q_mapping *mapping)
1427 : : {
1428 [ + + ]: 1386 : if (q_addr(mapping) != NULL) {
1429 [ + - + - : 529 : vfu_sgl_put(vu_ctrlr->endpoint->vfu_ctx, mapping->sg,
+ - + - +
- + - ]
1430 [ + - ]: 28 : &mapping->iov, 1);
1431 [ + - + - : 529 : mapping->iov.iov_base = NULL;
+ - ]
1432 : 28 : }
1433 : 1386 : }
1434 : :
1435 : : static int
1436 : 59 : asq_setup(struct nvmf_vfio_user_ctrlr *ctrlr)
1437 : : {
1438 : : struct nvmf_vfio_user_sq *sq;
1439 : : const struct spdk_nvmf_registers *regs;
1440 : : int ret;
1441 : :
1442 [ + + # # ]: 59 : assert(ctrlr != NULL);
1443 : :
1444 [ + - + - : 59 : sq = ctrlr->sqs[0];
+ - + - ]
1445 : :
1446 [ + + # # ]: 59 : assert(sq != NULL);
1447 [ + + + - : 59 : assert(q_addr(&sq->mapping) == NULL);
# # ]
1448 : : /* XXX ctrlr->asq == 0 is a valid memory address */
1449 : :
1450 [ + - + - ]: 59 : regs = spdk_nvmf_ctrlr_get_regs(ctrlr->ctrlr);
1451 [ + - + - ]: 59 : sq->qid = 0;
1452 [ + - + - : 59 : sq->size = regs->aqa.bits.asqs + 1;
+ - + - +
- + - ]
1453 [ + - + - : 59 : sq->mapping.prp1 = regs->asq;
+ - + - +
- ]
1454 [ + - + - : 59 : sq->mapping.len = sq->size * sizeof(struct spdk_nvme_cmd);
+ - + - +
- ]
1455 : 59 : *sq_headp(sq) = 0;
1456 [ + - + - ]: 59 : sq->cqid = 0;
1457 : :
1458 [ + - - + : 59 : ret = map_q(ctrlr, &sq->mapping, MAP_INITIALIZE);
+ - ]
1459 [ - + ]: 59 : if (ret) {
1460 : 0 : return ret;
1461 : : }
1462 : :
1463 : : /* The Admin queue (qid: 0) does not ever use shadow doorbells. */
1464 [ + - + - : 59 : sq->dbl_tailp = ctrlr->bar0_doorbells + queue_index(0, false);
+ - + - +
- ]
1465 : :
1466 : 59 : *sq_dbl_tailp(sq) = 0;
1467 : :
1468 : 59 : return 0;
1469 : 7 : }
1470 : :
1471 : : /*
1472 : : * Updates eventidx to set an SQ into interrupt or polling mode.
1473 : : *
1474 : : * Returns false if the current SQ tail does not match the SQ head, as
1475 : : * this means that the host has submitted more items to the queue while we were
1476 : : * not looking - or during the event index update. In that case, we must retry,
1477 : : * or otherwise make sure we are going to wake up again.
1478 : : */
1479 : : static bool
1480 : 0 : set_sq_eventidx(struct nvmf_vfio_user_sq *sq)
1481 : : {
1482 : : struct nvmf_vfio_user_ctrlr *ctrlr;
1483 : : volatile uint32_t *sq_tail_eidx;
1484 : : uint32_t old_tail, new_tail;
1485 : :
1486 [ # # # # ]: 0 : assert(sq != NULL);
1487 [ # # # # : 0 : assert(sq->ctrlr != NULL);
# # # # ]
1488 [ # # # # : 0 : assert(sq->ctrlr->sdbl != NULL);
# # # # #
# # # ]
1489 [ # # # # : 0 : assert(sq->need_rearm);
# # # # #
# ]
1490 [ # # # # : 0 : assert(sq->qid != 0);
# # # # ]
1491 : :
1492 [ # # # # ]: 0 : ctrlr = sq->ctrlr;
1493 : :
1494 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "%s: updating eventidx of sqid:%u\n",
# # # # #
# ]
1495 : : ctrlr_id(ctrlr), sq->qid);
1496 : :
1497 [ # # # # : 0 : sq_tail_eidx = ctrlr->sdbl->eventidxs + queue_index(sq->qid, false);
# # # # #
# # # #
# ]
1498 : :
1499 [ # # # # : 0 : assert(ctrlr->endpoint != NULL);
# # # # ]
1500 : :
1501 [ # # # # : 0 : if (!ctrlr->endpoint->interrupt_mode) {
# # # # #
# # # ]
1502 : : /* No synchronisation necessary. */
1503 : 0 : *sq_tail_eidx = NVMF_VFIO_USER_EVENTIDX_POLL;
1504 : 0 : return true;
1505 : : }
1506 : :
1507 : 0 : old_tail = *sq_dbl_tailp(sq);
1508 : 0 : *sq_tail_eidx = old_tail;
1509 : :
1510 : : /*
1511 : : * Ensure that the event index is updated before re-reading the tail
1512 : : * doorbell. If it's not, then the host might race us and update the
1513 : : * tail after the second read but before the event index is written, so
1514 : : * it won't write to BAR0 and we'll miss the update.
1515 : : *
1516 : : * The driver should provide similar ordering with an mb().
1517 : : */
1518 : 0 : spdk_mb();
1519 : :
1520 : : /*
1521 : : * Check if the host has updated the tail doorbell after we've read it
1522 : : * for the first time, but before the event index was written. If that's
1523 : : * the case, then we've lost the race and we need to update the event
1524 : : * index again (after polling the queue, since the host won't write to
1525 : : * BAR0).
1526 : : */
1527 : 0 : new_tail = *sq_dbl_tailp(sq);
1528 : :
1529 : : /*
1530 : : * We might poll the queue straight after this function returns if the
1531 : : * tail has been updated, so we need to ensure that any changes to the
1532 : : * queue will be visible to us if the doorbell has been updated.
1533 : : *
1534 : : * The driver should provide similar ordering with a wmb() to ensure
1535 : : * that the queue is written before it updates the tail doorbell.
1536 : : */
1537 : 0 : spdk_rmb();
1538 : :
1539 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "%s: sqid:%u, old_tail=%u, new_tail=%u, "
# # # # #
# ]
1540 : : "sq_head=%u\n", ctrlr_id(ctrlr), sq->qid, old_tail,
1541 : : new_tail, *sq_headp(sq));
1542 : :
1543 [ # # ]: 0 : if (new_tail == *sq_headp(sq)) {
1544 [ # # # # ]: 0 : sq->need_rearm = false;
1545 : 0 : return true;
1546 : : }
1547 : :
1548 : : /*
1549 : : * We've lost the race: the tail was updated since we last polled,
1550 : : * including if it happened within this routine.
1551 : : *
1552 : : * The caller should retry after polling (think of this as a cmpxchg
1553 : : * loop); if we go to sleep while the SQ is not empty, then we won't
1554 : : * process the remaining events.
1555 : : */
1556 : 0 : return false;
1557 : 0 : }
1558 : :
1559 : : static int nvmf_vfio_user_sq_poll(struct nvmf_vfio_user_sq *sq);
1560 : :
1561 : : /*
1562 : : * Arrange for an SQ to interrupt us if written. Returns non-zero if we
1563 : : * processed some SQ entries.
1564 : : */
1565 : : static int
1566 : 0 : vfio_user_sq_rearm(struct nvmf_vfio_user_ctrlr *ctrlr,
1567 : : struct nvmf_vfio_user_sq *sq,
1568 : : struct nvmf_vfio_user_poll_group *vu_group)
1569 : : {
1570 : 0 : int count = 0;
1571 : : size_t i;
1572 : :
1573 [ # # # # : 0 : assert(sq->need_rearm);
# # # # #
# ]
1574 : :
1575 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_SET_EVENTIDX_MAX_ATTEMPTS; i++) {
1576 : : int ret;
1577 : :
1578 [ # # ]: 0 : if (set_sq_eventidx(sq)) {
1579 : : /* We won the race and set eventidx; done. */
1580 [ # # # # ]: 0 : vu_group->stats.won++;
1581 : 0 : return count;
1582 : : }
1583 : :
1584 : 0 : ret = nvmf_vfio_user_sq_poll(sq);
1585 : :
1586 [ # # # # ]: 0 : count += (ret < 0) ? 1 : ret;
1587 : 0 : }
1588 : :
1589 : : /*
1590 : : * We couldn't arrange an eventidx guaranteed to cause a BAR0 write, as
1591 : : * we raced with the producer too many times; force ourselves to wake up
1592 : : * instead. We'll process all queues at that point.
1593 : : */
1594 [ # # # # ]: 0 : vu_group->need_kick = true;
1595 : :
1596 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db,
# # ]
1597 : : "%s: set_sq_eventidx() lost the race %zu times\n",
1598 : : ctrlr_id(ctrlr), i);
1599 : :
1600 [ # # # # ]: 0 : vu_group->stats.lost++;
1601 [ # # # # : 0 : vu_group->stats.lost_count += count;
# # ]
1602 : :
1603 : 0 : return count;
1604 : 0 : }
1605 : :
1606 : : /*
1607 : : * We're in interrupt mode, and potentially about to go to sleep. We need to
1608 : : * make sure any further I/O submissions are guaranteed to wake us up: for
1609 : : * shadow doorbells that means we may need to go through set_sq_eventidx() for
1610 : : * every SQ that needs re-arming.
1611 : : *
1612 : : * Returns non-zero if we processed something.
1613 : : */
1614 : : static int
1615 : 0 : vfio_user_poll_group_rearm(struct nvmf_vfio_user_poll_group *vu_group)
1616 : : {
1617 : : struct nvmf_vfio_user_sq *sq;
1618 : 0 : int count = 0;
1619 : :
1620 [ # # # # ]: 0 : vu_group->stats.rearms++;
1621 : :
1622 [ # # # # : 0 : TAILQ_FOREACH(sq, &vu_group->sqs, link) {
# # # # #
# # # #
# ]
1623 [ # # # # : 0 : if (spdk_unlikely(sq->sq_state != VFIO_USER_SQ_ACTIVE || !sq->size)) {
# # # # #
# # # ]
1624 : 0 : continue;
1625 : : }
1626 : :
1627 [ # # # # : 0 : if (sq->need_rearm) {
# # # # ]
1628 [ # # # # : 0 : count += vfio_user_sq_rearm(sq->ctrlr, sq, vu_group);
# # ]
1629 : 0 : }
1630 : 0 : }
1631 : :
1632 [ # # # # : 0 : if (vu_group->need_kick) {
# # # # ]
1633 : 0 : poll_group_kick(vu_group);
1634 : 0 : }
1635 : :
1636 : 0 : return count;
1637 : : }
1638 : :
1639 : : static int
1640 : 59 : acq_setup(struct nvmf_vfio_user_ctrlr *ctrlr)
1641 : : {
1642 : : struct nvmf_vfio_user_cq *cq;
1643 : : const struct spdk_nvmf_registers *regs;
1644 : : int ret;
1645 : :
1646 [ + + # # ]: 59 : assert(ctrlr != NULL);
1647 : :
1648 [ + - + - : 59 : cq = ctrlr->cqs[0];
+ - + - ]
1649 : :
1650 [ + + # # ]: 59 : assert(cq != NULL);
1651 : :
1652 [ + + + - : 59 : assert(q_addr(&cq->mapping) == NULL);
# # ]
1653 : :
1654 [ + - + - ]: 59 : regs = spdk_nvmf_ctrlr_get_regs(ctrlr->ctrlr);
1655 [ + + # # ]: 59 : assert(regs != NULL);
1656 [ + - + - ]: 59 : cq->qid = 0;
1657 [ + - + - : 59 : cq->size = regs->aqa.bits.acqs + 1;
+ - + - +
- + - ]
1658 [ + - + - : 59 : cq->mapping.prp1 = regs->acq;
+ - + - +
- ]
1659 [ + - + - : 59 : cq->mapping.len = cq->size * sizeof(struct spdk_nvme_cpl);
+ - + - +
- ]
1660 : 59 : *cq_tailp(cq) = 0;
1661 [ + - + - ]: 59 : cq->ien = true;
1662 [ + - + - ]: 59 : cq->phase = true;
1663 [ + - + - ]: 59 : cq->nr_outstanding = 0;
1664 : :
1665 [ + - - + : 59 : ret = map_q(ctrlr, &cq->mapping, MAP_RW | MAP_INITIALIZE);
+ - - + +
- ]
1666 [ - + ]: 59 : if (ret) {
1667 : 0 : return ret;
1668 : : }
1669 : :
1670 : : /* The Admin queue (qid: 0) does not ever use shadow doorbells. */
1671 [ + - + - : 59 : cq->dbl_headp = ctrlr->bar0_doorbells + queue_index(0, true);
+ - + - +
- ]
1672 : :
1673 : 59 : *cq_dbl_headp(cq) = 0;
1674 : :
1675 : 59 : return 0;
1676 : 7 : }
1677 : :
1678 : : static void *
1679 : 2742681 : _map_one(void *prv, uint64_t addr, uint64_t len, uint32_t flags)
1680 : : {
1681 : 2742681 : struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)prv;
1682 : : struct spdk_nvmf_qpair *qpair;
1683 : : struct nvmf_vfio_user_req *vu_req;
1684 : : struct nvmf_vfio_user_sq *sq;
1685 : : void *ret;
1686 : :
1687 [ + + # # ]: 2742681 : assert(req != NULL);
1688 [ + - + - ]: 2742681 : qpair = req->qpair;
1689 : 2742681 : vu_req = SPDK_CONTAINEROF(req, struct nvmf_vfio_user_req, req);
1690 : 2742681 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
1691 : :
1692 [ + + + - : 2742681 : assert(vu_req->iovcnt < NVMF_VFIO_USER_MAX_IOVECS);
+ - # # ]
1693 [ + - + - : 5485362 : ret = map_one(sq->ctrlr->endpoint->vfu_ctx, addr, len,
+ - + - +
- + - ]
1694 [ + - + - : 2742681 : index_to_sg_t(vu_req->sg, vu_req->iovcnt),
+ - ]
1695 [ + - + - : 2742681 : &vu_req->iov[vu_req->iovcnt], flags);
+ - + - +
- ]
1696 [ + + ]: 2742681 : if (spdk_likely(ret != NULL)) {
1697 [ + - ]: 1881801 : vu_req->iovcnt++;
1698 : 149570 : }
1699 : 2742681 : return ret;
1700 : : }
1701 : :
1702 : : static int
1703 : 2706303 : vfio_user_map_cmd(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvmf_request *req,
1704 : : struct iovec *iov, uint32_t length)
1705 : : {
1706 : : /* Map PRP list to from Guest physical memory to
1707 : : * virtual memory address.
1708 : : */
1709 [ + - + - : 2706303 : return nvme_map_cmd(req, &req->cmd->nvme_cmd, iov, NVMF_REQ_MAX_BUFFERS,
+ - ]
1710 : 149570 : length, 4096, _map_one);
1711 : : }
1712 : :
1713 : : static int handle_cmd_req(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd,
1714 : : struct nvmf_vfio_user_sq *sq);
1715 : :
1716 : : static uint32_t
1717 : 3475375 : cq_free_slots(struct nvmf_vfio_user_cq *cq)
1718 : : {
1719 : : uint32_t free_slots;
1720 : :
1721 [ + + # # ]: 3475375 : assert(cq != NULL);
1722 : :
1723 [ + + + - : 3475375 : if (cq->tail == cq->last_head) {
+ - + - +
+ ]
1724 [ + - + - ]: 21037 : free_slots = cq->size;
1725 [ + + + - : 3456714 : } else if (cq->tail > cq->last_head) {
+ - + - +
+ ]
1726 [ + - + - : 1936290 : free_slots = cq->size - (cq->tail - cq->last_head);
+ - + - +
- + - ]
1727 : 128514 : } else {
1728 [ + - + - : 1518048 : free_slots = cq->last_head - cq->tail;
+ - + - ]
1729 : : }
1730 [ + + # # ]: 3475375 : assert(free_slots > 0);
1731 : :
1732 : 3475375 : return free_slots - 1;
1733 : : }
1734 : :
1735 : : /*
1736 : : * Since reading the head doorbell is relatively expensive, we use the cached
1737 : : * value, so we only have to read it for real if it appears that we are full.
1738 : : */
1739 : : static inline bool
1740 : 3187090 : cq_is_full(struct nvmf_vfio_user_cq *cq)
1741 : : {
1742 : : uint32_t free_cq_slots;
1743 : :
1744 [ + + # # ]: 3187090 : assert(cq != NULL);
1745 : :
1746 : 3187090 : free_cq_slots = cq_free_slots(cq);
1747 : :
1748 [ + + ]: 3187090 : if (spdk_unlikely(free_cq_slots == 0)) {
1749 [ # # # # ]: 0 : cq->last_head = *cq_dbl_headp(cq);
1750 : 0 : free_cq_slots = cq_free_slots(cq);
1751 : 0 : }
1752 : :
1753 : 3187090 : return free_cq_slots == 0;
1754 : : }
1755 : :
1756 : : /*
1757 : : * Posts a CQE in the completion queue.
1758 : : *
1759 : : * @ctrlr: the vfio-user controller
1760 : : * @cq: the completion queue
1761 : : * @cdw0: cdw0 as reported by NVMf
1762 : : * @sqid: submission queue ID
1763 : : * @cid: command identifier in NVMe command
1764 : : * @sc: the NVMe CQE status code
1765 : : * @sct: the NVMe CQE status code type
1766 : : */
1767 : : static int
1768 : 3187262 : post_completion(struct nvmf_vfio_user_ctrlr *ctrlr, struct nvmf_vfio_user_cq *cq,
1769 : : uint32_t cdw0, uint16_t sqid, uint16_t cid, uint16_t sc, uint16_t sct)
1770 : : {
1771 : 3187262 : struct spdk_nvme_status cpl_status = { 0 };
1772 : : struct spdk_nvme_cpl *cpl;
1773 : : int err;
1774 : :
1775 [ + + # # ]: 3187262 : assert(ctrlr != NULL);
1776 : :
1777 [ + - + + : 3187262 : if (spdk_unlikely(cq == NULL || q_addr(&cq->mapping) == NULL)) {
+ + ]
1778 : 172 : return 0;
1779 : : }
1780 : :
1781 [ + + + - : 3187090 : if (cq->qid == 0) {
+ + ]
1782 [ + + + - : 162847 : assert(spdk_get_thread() == cq->group->group->thread);
+ - + - +
- + - + -
# # ]
1783 : 77 : }
1784 : :
1785 : : /*
1786 : : * As per NVMe Base spec 3.3.1.2.1, we are supposed to implement CQ flow
1787 : : * control: that is, we should handle running out of free CQ slots.
1788 : : *
1789 : : * Instead, we implement this by applying flow control on the submission
1790 : : * side: see handle_sq_tdbl_write().
1791 : : */
1792 [ - + ]: 3187090 : if (cq_is_full(cq)) {
1793 [ # # # # ]: 0 : SPDK_ERRLOG("%s: cqid:%d full (tail=%d, head=%d)\n",
1794 : : ctrlr_id(ctrlr), cq->qid, *cq_tailp(cq),
1795 : : *cq_dbl_headp(cq));
1796 : 0 : return -1;
1797 : : }
1798 : :
1799 [ + - + - ]: 3187090 : cpl = ((struct spdk_nvme_cpl *)q_addr(&cq->mapping)) + *cq_tailp(cq);
1800 : :
1801 [ + + + - : 3187090 : assert(ctrlr->sqs[sqid] != NULL);
+ - + - +
- # # ]
1802 [ + + + + : 3187090 : SPDK_DEBUGLOG(nvmf_vfio,
+ - # # #
# # # #
# ]
1803 : : "%s: request complete sqid:%d cid=%d status=%#x "
1804 : : "sqhead=%d cq tail=%d\n", ctrlr_id(ctrlr), sqid, cid, sc,
1805 : : *sq_headp(ctrlr->sqs[sqid]), *cq_tailp(cq));
1806 : :
1807 [ + - + - : 3187090 : cpl->sqhd = *sq_headp(ctrlr->sqs[sqid]);
+ - + - +
- + - ]
1808 [ + - + - ]: 3187090 : cpl->sqid = sqid;
1809 [ + - + - ]: 3187090 : cpl->cid = cid;
1810 [ + - + - ]: 3187090 : cpl->cdw0 = cdw0;
1811 : :
1812 : : /*
1813 : : * This is a bitfield: instead of setting the individual bits we need
1814 : : * directly in cpl->status, which would cause a read-modify-write cycle,
1815 : : * we'll avoid reading from the CPL altogether by filling in a local
1816 : : * cpl_status variable, then writing the whole thing.
1817 : : */
1818 : 3187090 : cpl_status.sct = sct;
1819 : 3187090 : cpl_status.sc = sc;
1820 [ + + + - : 3187090 : cpl_status.p = cq->phase;
+ - ]
1821 [ + - + - ]: 3187090 : cpl->status = cpl_status;
1822 : :
1823 [ + - ]: 3187090 : cq->nr_outstanding--;
1824 : :
1825 : : /* Ensure the Completion Queue Entry is visible. */
1826 : 3187090 : spdk_wmb();
1827 : 3187090 : cq_tail_advance(cq);
1828 : :
1829 [ + + + + : 3187167 : if ((cq->qid == 0 || !ctrlr->adaptive_irqs_enabled) &&
+ + + - +
- + - +
- ]
1830 [ + + + - : 162847 : cq->ien && ctrlr_interrupt_enabled(ctrlr)) {
+ + + - ]
1831 [ # # # # : 386 : err = vfu_irq_trigger(ctrlr->endpoint->vfu_ctx, cq->iv);
# # # # #
# # # ]
1832 [ - + ]: 386 : if (err != 0) {
1833 : 0 : SPDK_ERRLOG("%s: failed to trigger interrupt: %m\n",
1834 : : ctrlr_id(ctrlr));
1835 : 0 : return err;
1836 : : }
1837 : 0 : }
1838 : :
1839 : 3187090 : return 0;
1840 : 149647 : }
1841 : :
1842 : : static void
1843 : 641 : free_sq_reqs(struct nvmf_vfio_user_sq *sq)
1844 : : {
1845 [ + + + - : 20991 : while (!TAILQ_EMPTY(&sq->free_reqs)) {
+ - + + ]
1846 [ + - + - : 20350 : struct nvmf_vfio_user_req *vu_req = TAILQ_FIRST(&sq->free_reqs);
+ - ]
1847 [ + + + - : 20350 : TAILQ_REMOVE(&sq->free_reqs, vu_req, link);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - - + -
+ - + - +
- + - + +
- + - + -
+ - + - +
- + - ]
1848 : 20350 : free(vu_req);
1849 : : }
1850 : 641 : }
1851 : :
1852 : : static void
1853 : 297 : delete_cq_done(struct nvmf_vfio_user_ctrlr *ctrlr, struct nvmf_vfio_user_cq *cq)
1854 : : {
1855 [ + + + - : 297 : assert(cq->cq_ref == 0);
+ - # # ]
1856 [ + - ]: 297 : unmap_q(ctrlr, &cq->mapping);
1857 [ + - + - ]: 297 : cq->size = 0;
1858 [ + - + - ]: 297 : cq->cq_state = VFIO_USER_CQ_DELETED;
1859 [ + - + - ]: 297 : cq->group = NULL;
1860 [ + - + - ]: 297 : cq->nr_outstanding = 0;
1861 : 297 : }
1862 : :
1863 : : /* Deletes a SQ, if this SQ is the last user of the associated CQ
1864 : : * and the controller is being shut down/reset or vfio-user client disconnects,
1865 : : * then the CQ is also deleted.
1866 : : */
1867 : : static void
1868 : 324 : delete_sq_done(struct nvmf_vfio_user_ctrlr *vu_ctrlr, struct nvmf_vfio_user_sq *sq)
1869 : : {
1870 : : struct nvmf_vfio_user_cq *cq;
1871 : : uint16_t cqid;
1872 : :
1873 [ + + + + : 324 : SPDK_DEBUGLOG(nvmf_vfio, "%s: delete sqid:%d=%p done\n", ctrlr_id(vu_ctrlr),
+ - # # #
# ]
1874 : : sq->qid, sq);
1875 : :
1876 : : /* Free SQ resources */
1877 [ + - ]: 324 : unmap_q(vu_ctrlr, &sq->mapping);
1878 : :
1879 : 324 : free_sq_reqs(sq);
1880 : :
1881 [ + - + - ]: 324 : sq->size = 0;
1882 : :
1883 [ + - + - ]: 324 : sq->sq_state = VFIO_USER_SQ_DELETED;
1884 : :
1885 : : /* Controller RESET and SHUTDOWN are special cases,
1886 : : * VM may not send DELETE IO SQ/CQ commands, NVMf library
1887 : : * will disconnect IO queue pairs.
1888 : : */
1889 [ + + + + : 324 : if (vu_ctrlr->reset_shn || vu_ctrlr->disconnect) {
+ + + + +
- + - + -
+ + ]
1890 [ + - + - ]: 124 : cqid = sq->cqid;
1891 [ + - + - : 124 : cq = vu_ctrlr->cqs[cqid];
+ - + - ]
1892 : :
1893 [ + + + + : 124 : SPDK_DEBUGLOG(nvmf_vfio, "%s: try to delete cqid:%u=%p\n", ctrlr_id(vu_ctrlr),
+ - # # #
# ]
1894 : : cq->qid, cq);
1895 : :
1896 [ + + + - : 124 : assert(cq->cq_ref > 0);
+ - # # ]
1897 [ + - + - : 124 : if (--cq->cq_ref == 0) {
- + ]
1898 : 124 : delete_cq_done(vu_ctrlr, cq);
1899 : 80 : }
1900 : 80 : }
1901 : 324 : }
1902 : :
1903 : : static void
1904 : 65024 : free_qp(struct nvmf_vfio_user_ctrlr *ctrlr, uint16_t qid)
1905 : : {
1906 : : struct nvmf_vfio_user_sq *sq;
1907 : : struct nvmf_vfio_user_cq *cq;
1908 : :
1909 [ + + ]: 65024 : if (ctrlr == NULL) {
1910 : 0 : return;
1911 : : }
1912 : :
1913 [ + - + - : 65024 : sq = ctrlr->sqs[qid];
+ - + - ]
1914 [ + + ]: 65024 : if (sq) {
1915 [ + + + + : 317 : SPDK_DEBUGLOG(nvmf_vfio, "%s: Free sqid:%u\n", ctrlr_id(ctrlr), qid);
+ - ]
1916 [ + - ]: 317 : unmap_q(ctrlr, &sq->mapping);
1917 : :
1918 : 317 : free_sq_reqs(sq);
1919 : :
1920 [ + - + - : 317 : free(sq->mapping.sg);
+ - ]
1921 : 317 : free(sq);
1922 [ + - + - : 317 : ctrlr->sqs[qid] = NULL;
+ - + - ]
1923 : 87 : }
1924 : :
1925 [ + - + - : 65024 : cq = ctrlr->cqs[qid];
+ - + - ]
1926 [ + + ]: 65024 : if (cq) {
1927 [ + + + + : 316 : SPDK_DEBUGLOG(nvmf_vfio, "%s: Free cqid:%u\n", ctrlr_id(ctrlr), qid);
+ - ]
1928 [ + - ]: 316 : unmap_q(ctrlr, &cq->mapping);
1929 [ + - + - : 316 : free(cq->mapping.sg);
+ - ]
1930 : 316 : free(cq);
1931 [ + - + - : 316 : ctrlr->cqs[qid] = NULL;
+ - + - ]
1932 : 87 : }
1933 : 40960 : }
1934 : :
1935 : : static int
1936 : 317 : init_sq(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvmf_transport *transport,
1937 : : const uint16_t id)
1938 : : {
1939 : : struct nvmf_vfio_user_sq *sq;
1940 : :
1941 [ + + # # ]: 317 : assert(ctrlr != NULL);
1942 [ + + # # ]: 317 : assert(transport != NULL);
1943 [ + + + - : 317 : assert(ctrlr->sqs[id] == NULL);
+ - + - +
- # # ]
1944 : :
1945 : 317 : sq = calloc(1, sizeof(*sq));
1946 [ + + ]: 317 : if (sq == NULL) {
1947 : 0 : return -ENOMEM;
1948 : : }
1949 [ + - + - : 317 : sq->mapping.sg = calloc(1, dma_sg_size());
+ - ]
1950 [ + + + - : 317 : if (sq->mapping.sg == NULL) {
+ - + - ]
1951 : 0 : free(sq);
1952 : 0 : return -ENOMEM;
1953 : : }
1954 : :
1955 [ + - + - ]: 317 : sq->qid = id;
1956 [ + - + - : 317 : sq->qpair.qid = id;
+ - ]
1957 [ + - + - : 317 : sq->qpair.transport = transport;
+ - ]
1958 [ + - + - ]: 317 : sq->ctrlr = ctrlr;
1959 [ + - + - : 317 : ctrlr->sqs[id] = sq;
+ - + - ]
1960 : :
1961 [ + - + - : 317 : TAILQ_INIT(&sq->free_reqs);
+ - + - +
- + - + -
+ - ]
1962 : :
1963 : 317 : return 0;
1964 : 87 : }
1965 : :
1966 : : static int
1967 : 316 : init_cq(struct nvmf_vfio_user_ctrlr *vu_ctrlr, const uint16_t id)
1968 : : {
1969 : : struct nvmf_vfio_user_cq *cq;
1970 : :
1971 [ + + # # ]: 316 : assert(vu_ctrlr != NULL);
1972 [ + + + - : 316 : assert(vu_ctrlr->cqs[id] == NULL);
+ - + - +
- # # ]
1973 : :
1974 : 316 : cq = calloc(1, sizeof(*cq));
1975 [ + + ]: 316 : if (cq == NULL) {
1976 : 0 : return -ENOMEM;
1977 : : }
1978 [ + - + - : 316 : cq->mapping.sg = calloc(1, dma_sg_size());
+ - ]
1979 [ + + + - : 316 : if (cq->mapping.sg == NULL) {
+ - + - ]
1980 : 0 : free(cq);
1981 : 0 : return -ENOMEM;
1982 : : }
1983 : :
1984 [ + - + - ]: 316 : cq->qid = id;
1985 [ + - + - : 316 : vu_ctrlr->cqs[id] = cq;
+ - + - ]
1986 : :
1987 : 316 : return 0;
1988 : 87 : }
1989 : :
1990 : : static int
1991 : 324 : alloc_sq_reqs(struct nvmf_vfio_user_ctrlr *vu_ctrlr, struct nvmf_vfio_user_sq *sq)
1992 : : {
1993 : : struct nvmf_vfio_user_req *vu_req, *tmp;
1994 : : size_t req_size;
1995 : : uint32_t i;
1996 : :
1997 : 324 : req_size = sizeof(struct nvmf_vfio_user_req) +
1998 : 324 : (dma_sg_size() * NVMF_VFIO_USER_MAX_IOVECS);
1999 : :
2000 [ + + + - : 20674 : for (i = 0; i < sq->size; i++) {
+ + ]
2001 : : struct spdk_nvmf_request *req;
2002 : :
2003 : 20350 : vu_req = calloc(1, req_size);
2004 [ + + ]: 20350 : if (vu_req == NULL) {
2005 : 0 : goto err;
2006 : : }
2007 : :
2008 [ + - ]: 20350 : req = &vu_req->req;
2009 [ + - + - : 20350 : req->qpair = &sq->qpair;
+ - ]
2010 [ + - + - : 20350 : req->rsp = (union nvmf_c2h_msg *)&vu_req->rsp;
+ - ]
2011 [ + - + - : 20350 : req->cmd = (union nvmf_h2c_msg *)&vu_req->cmd;
+ - ]
2012 [ + - + - ]: 20350 : req->stripped_data = NULL;
2013 : :
2014 [ + - + - : 20350 : TAILQ_INSERT_TAIL(&sq->free_reqs, vu_req, link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
2015 : 3456 : }
2016 : :
2017 : 324 : return 0;
2018 : :
2019 : 0 : err:
2020 [ # # # # : 0 : TAILQ_FOREACH_SAFE(vu_req, &sq->free_reqs, link, tmp) {
# # # # #
# # # # #
# # ]
2021 : 0 : free(vu_req);
2022 : 0 : }
2023 : 0 : return -ENOMEM;
2024 : 87 : }
2025 : :
2026 : : static volatile uint32_t *
2027 : 391 : ctrlr_doorbell_ptr(struct nvmf_vfio_user_ctrlr *ctrlr)
2028 : : {
2029 [ + - + - : 405 : return ctrlr->sdbl != NULL ?
+ - ]
2030 [ - + # # : 377 : ctrlr->sdbl->shadow_doorbells :
# # # # ]
2031 [ + - + - ]: 14 : ctrlr->bar0_doorbells;
2032 : : }
2033 : :
2034 : : static uint16_t
2035 : 200 : handle_create_io_sq(struct nvmf_vfio_user_ctrlr *ctrlr,
2036 : : struct spdk_nvme_cmd *cmd, uint16_t *sct)
2037 : : {
2038 [ + - + - ]: 200 : struct nvmf_vfio_user_transport *vu_transport = ctrlr->transport;
2039 : : struct nvmf_vfio_user_sq *sq;
2040 : : uint32_t qsize;
2041 : : uint16_t cqid;
2042 : : uint16_t qid;
2043 : : int err;
2044 : :
2045 [ + - + - : 200 : qid = cmd->cdw10_bits.create_io_q.qid;
+ - + - ]
2046 [ + - + - : 200 : cqid = cmd->cdw11_bits.create_io_sq.cqid;
+ - + - ]
2047 [ + - + - : 200 : qsize = cmd->cdw10_bits.create_io_q.qsize + 1;
+ - + - +
- ]
2048 : :
2049 [ + + + - : 200 : if (ctrlr->sqs[qid] == NULL) {
+ - + - -
+ ]
2050 [ + - + - : 190 : err = init_sq(ctrlr, ctrlr->sqs[0]->qpair.transport, qid);
+ - + - +
- + - +
- ]
2051 [ - + ]: 190 : if (err != 0) {
2052 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2053 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2054 : : }
2055 : 7 : }
2056 : :
2057 [ + + + + : 200 : if (cqid == 0 || cqid >= vu_transport->transport.opts.max_qpairs_per_ctrlr) {
+ - + - +
- - + ]
2058 : 2 : SPDK_ERRLOG("%s: invalid cqid:%u\n", ctrlr_id(ctrlr), cqid);
2059 [ # # ]: 2 : *sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2060 : 2 : return SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
2061 : : }
2062 : :
2063 : : /* CQ must be created before SQ. */
2064 [ + + ]: 198 : if (!io_q_exists(ctrlr, cqid, true)) {
2065 : 0 : SPDK_ERRLOG("%s: cqid:%u does not exist\n", ctrlr_id(ctrlr), cqid);
2066 [ # # ]: 0 : *sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2067 : 0 : return SPDK_NVME_SC_COMPLETION_QUEUE_INVALID;
2068 : : }
2069 : :
2070 [ + + + - : 198 : if (cmd->cdw11_bits.create_io_sq.pc != 0x1) {
+ - + - -
+ ]
2071 : 1 : SPDK_ERRLOG("%s: non-PC SQ not supported\n", ctrlr_id(ctrlr));
2072 [ # # ]: 1 : *sct = SPDK_NVME_SCT_GENERIC;
2073 : 1 : return SPDK_NVME_SC_INVALID_FIELD;
2074 : : }
2075 : :
2076 [ + - + - : 197 : sq = ctrlr->sqs[qid];
+ - + - ]
2077 [ + - + - ]: 197 : sq->size = qsize;
2078 : :
2079 [ + + + + : 197 : SPDK_DEBUGLOG(nvmf_vfio, "%s: sqid:%d cqid:%d\n", ctrlr_id(ctrlr),
+ - ]
2080 : : qid, cqid);
2081 : :
2082 [ + - + - : 197 : sq->mapping.prp1 = cmd->dptr.prp.prp1;
+ - + - +
- + - +
- ]
2083 [ + - + - : 197 : sq->mapping.len = sq->size * sizeof(struct spdk_nvme_cmd);
+ - + - +
- ]
2084 : :
2085 [ + - - + : 197 : err = map_q(ctrlr, &sq->mapping, MAP_INITIALIZE);
+ - ]
2086 [ - + ]: 197 : if (err) {
2087 : 0 : SPDK_ERRLOG("%s: failed to map I/O queue: %m\n", ctrlr_id(ctrlr));
2088 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2089 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2090 : : }
2091 : :
2092 [ + + + + : 197 : SPDK_DEBUGLOG(nvmf_vfio, "%s: mapped sqid:%d IOVA=%#lx vaddr=%p\n",
+ - # # #
# # # # #
# # ]
2093 : : ctrlr_id(ctrlr), qid, cmd->dptr.prp.prp1,
2094 : : q_addr(&sq->mapping));
2095 : :
2096 : 197 : err = alloc_sq_reqs(ctrlr, sq);
2097 [ + + ]: 197 : if (err < 0) {
2098 : 0 : SPDK_ERRLOG("%s: failed to allocate SQ requests: %m\n", ctrlr_id(ctrlr));
2099 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2100 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2101 : : }
2102 : :
2103 [ + - + - ]: 197 : sq->cqid = cqid;
2104 [ + - + - : 197 : ctrlr->cqs[sq->cqid]->cq_ref++;
+ - + - +
- + - + -
+ - ]
2105 [ + - + - ]: 197 : sq->sq_state = VFIO_USER_SQ_CREATED;
2106 : 197 : *sq_headp(sq) = 0;
2107 : :
2108 [ + - + - : 197 : sq->dbl_tailp = ctrlr_doorbell_ptr(ctrlr) + queue_index(qid, false);
+ - ]
2109 : :
2110 : : /*
2111 : : * We should always reset the doorbells.
2112 : : *
2113 : : * The Specification prohibits the controller from writing to the shadow
2114 : : * doorbell buffer, however older versions of the Linux NVMe driver
2115 : : * don't reset the shadow doorbell buffer after a Queue-Level or
2116 : : * Controller-Level reset, which means that we're left with garbage
2117 : : * doorbell values.
2118 : : */
2119 : 197 : *sq_dbl_tailp(sq) = 0;
2120 : :
2121 [ + + + - : 197 : if (ctrlr->sdbl != NULL) {
+ - ]
2122 [ # # # # ]: 0 : sq->need_rearm = true;
2123 : :
2124 [ # # ]: 0 : if (!set_sq_eventidx(sq)) {
2125 : 0 : SPDK_ERRLOG("%s: host updated SQ tail doorbell before "
2126 : : "sqid:%hu was initialized\n",
2127 : : ctrlr_id(ctrlr), qid);
2128 : 0 : fail_ctrlr(ctrlr);
2129 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2130 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2131 : : }
2132 : 0 : }
2133 : :
2134 : : /*
2135 : : * Create our new I/O qpair. This asynchronously invokes, on a suitable
2136 : : * poll group, the nvmf_vfio_user_poll_group_add() callback, which will
2137 : : * call spdk_nvmf_request_exec() with a generated fabrics
2138 : : * connect command. This command is then eventually completed via
2139 : : * handle_queue_connect_rsp().
2140 : : */
2141 [ + - ]: 197 : sq->create_io_sq_cmd = *cmd;
2142 [ + - + - ]: 197 : sq->post_create_io_sq_completion = true;
2143 : :
2144 [ + - + - : 197 : spdk_nvmf_tgt_new_qpair(ctrlr->transport->transport.tgt,
+ - + - +
- ]
2145 [ + - ]: 7 : &sq->qpair);
2146 : :
2147 [ + - ]: 197 : *sct = SPDK_NVME_SCT_GENERIC;
2148 : 197 : return SPDK_NVME_SC_SUCCESS;
2149 : 7 : }
2150 : :
2151 : : static uint16_t
2152 : 196 : handle_create_io_cq(struct nvmf_vfio_user_ctrlr *ctrlr,
2153 : : struct spdk_nvme_cmd *cmd, uint16_t *sct)
2154 : : {
2155 : : struct nvmf_vfio_user_cq *cq;
2156 : : uint32_t qsize;
2157 : : uint16_t qid;
2158 : : int err;
2159 : :
2160 [ + - + - : 196 : qid = cmd->cdw10_bits.create_io_q.qid;
+ - + - ]
2161 [ + - + - : 196 : qsize = cmd->cdw10_bits.create_io_q.qsize + 1;
+ - + - +
- ]
2162 : :
2163 [ + + + - : 196 : if (ctrlr->cqs[qid] == NULL) {
+ - + - -
+ ]
2164 : 189 : err = init_cq(ctrlr, qid);
2165 [ - + ]: 189 : if (err != 0) {
2166 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2167 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2168 : : }
2169 : 7 : }
2170 : :
2171 [ + + + - : 196 : if (cmd->cdw11_bits.create_io_cq.pc != 0x1) {
+ - + - -
+ ]
2172 : 1 : SPDK_ERRLOG("%s: non-PC CQ not supported\n", ctrlr_id(ctrlr));
2173 [ # # ]: 1 : *sct = SPDK_NVME_SCT_GENERIC;
2174 : 1 : return SPDK_NVME_SC_INVALID_FIELD;
2175 : : }
2176 : :
2177 [ + + + - : 195 : if (cmd->cdw11_bits.create_io_cq.iv > NVMF_VFIO_USER_MSIX_NUM - 1) {
+ - + - -
+ ]
2178 : 1 : SPDK_ERRLOG("%s: IV is too big\n", ctrlr_id(ctrlr));
2179 [ # # ]: 1 : *sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2180 : 1 : return SPDK_NVME_SC_INVALID_INTERRUPT_VECTOR;
2181 : : }
2182 : :
2183 [ + - + - : 194 : cq = ctrlr->cqs[qid];
+ - + - ]
2184 [ + - + - ]: 194 : cq->size = qsize;
2185 : :
2186 [ + - + - : 194 : cq->mapping.prp1 = cmd->dptr.prp.prp1;
+ - + - +
- + - +
- ]
2187 [ + - + - : 194 : cq->mapping.len = cq->size * sizeof(struct spdk_nvme_cpl);
+ - + - +
- ]
2188 : :
2189 [ + - + - : 194 : cq->dbl_headp = ctrlr_doorbell_ptr(ctrlr) + queue_index(qid, true);
+ - ]
2190 : :
2191 [ + - - + : 194 : err = map_q(ctrlr, &cq->mapping, MAP_RW | MAP_INITIALIZE);
+ - - + +
- ]
2192 [ + + ]: 194 : if (err) {
2193 : 0 : SPDK_ERRLOG("%s: failed to map I/O queue: %m\n", ctrlr_id(ctrlr));
2194 [ # # ]: 0 : *sct = SPDK_NVME_SCT_GENERIC;
2195 : 0 : return SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2196 : : }
2197 : :
2198 [ + + + + : 194 : SPDK_DEBUGLOG(nvmf_vfio, "%s: mapped cqid:%u IOVA=%#lx vaddr=%p\n",
+ - # # #
# # # # #
# # ]
2199 : : ctrlr_id(ctrlr), qid, cmd->dptr.prp.prp1,
2200 : : q_addr(&cq->mapping));
2201 : :
2202 [ + - + - : 194 : cq->ien = cmd->cdw11_bits.create_io_cq.ien;
+ - + - +
- + - ]
2203 [ + - + - : 194 : cq->iv = cmd->cdw11_bits.create_io_cq.iv;
+ - + - +
- + - ]
2204 [ + - + - ]: 194 : cq->phase = true;
2205 [ + - + - ]: 194 : cq->cq_state = VFIO_USER_CQ_CREATED;
2206 : :
2207 : 194 : *cq_tailp(cq) = 0;
2208 : :
2209 : : /*
2210 : : * We should always reset the doorbells.
2211 : : *
2212 : : * The Specification prohibits the controller from writing to the shadow
2213 : : * doorbell buffer, however older versions of the Linux NVMe driver
2214 : : * don't reset the shadow doorbell buffer after a Queue-Level or
2215 : : * Controller-Level reset, which means that we're left with garbage
2216 : : * doorbell values.
2217 : : */
2218 : 194 : *cq_dbl_headp(cq) = 0;
2219 : :
2220 [ + - ]: 194 : *sct = SPDK_NVME_SCT_GENERIC;
2221 : 194 : return SPDK_NVME_SC_SUCCESS;
2222 : 7 : }
2223 : :
2224 : : /*
2225 : : * Creates a completion or submission I/O queue. Returns 0 on success, -errno
2226 : : * on error.
2227 : : */
2228 : : static int
2229 : 1558 : handle_create_io_q(struct nvmf_vfio_user_ctrlr *ctrlr,
2230 : : struct spdk_nvme_cmd *cmd, const bool is_cq)
2231 : : {
2232 [ + - + - ]: 1558 : struct nvmf_vfio_user_transport *vu_transport = ctrlr->transport;
2233 : 1558 : uint16_t sct = SPDK_NVME_SCT_GENERIC;
2234 : 1558 : uint16_t sc = SPDK_NVME_SC_SUCCESS;
2235 : : uint32_t qsize;
2236 : : uint16_t qid;
2237 : :
2238 [ + + # # ]: 1558 : assert(ctrlr != NULL);
2239 [ + + # # ]: 1558 : assert(cmd != NULL);
2240 : :
2241 [ + - + - : 1558 : qid = cmd->cdw10_bits.create_io_q.qid;
+ - + - ]
2242 [ + - + + : 1558 : if (qid == 0 || qid >= vu_transport->transport.opts.max_qpairs_per_ctrlr) {
+ - + - +
- - + ]
2243 [ # # # # : 1159 : SPDK_ERRLOG("%s: invalid qid=%d, max=%d\n", ctrlr_id(ctrlr),
# # # # ]
2244 : : qid, vu_transport->transport.opts.max_qpairs_per_ctrlr);
2245 : 1159 : sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2246 : 1159 : sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
2247 : 1159 : goto out;
2248 : : }
2249 : :
2250 [ + + - + ]: 399 : if (io_q_exists(ctrlr, qid, is_cq)) {
2251 [ # # ]: 0 : SPDK_ERRLOG("%s: %cqid:%d already exists\n", ctrlr_id(ctrlr),
2252 : : is_cq ? 'c' : 's', qid);
2253 : 0 : sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2254 : 0 : sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
2255 : 0 : goto out;
2256 : : }
2257 : :
2258 [ + - + - : 399 : qsize = cmd->cdw10_bits.create_io_q.qsize + 1;
+ - + - +
- ]
2259 [ + + + + ]: 399 : if (qsize == 1 || qsize > max_queue_size(ctrlr)) {
2260 : 3 : SPDK_ERRLOG("%s: invalid I/O queue size %u\n", ctrlr_id(ctrlr), qsize);
2261 : 3 : sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2262 : 3 : sc = SPDK_NVME_SC_INVALID_QUEUE_SIZE;
2263 : 3 : goto out;
2264 : : }
2265 : :
2266 [ + + + + ]: 403 : if (is_cq) {
2267 : 196 : sc = handle_create_io_cq(ctrlr, cmd, &sct);
2268 : 7 : } else {
2269 : 200 : sc = handle_create_io_sq(ctrlr, cmd, &sct);
2270 : :
2271 [ + + + + ]: 200 : if (sct == SPDK_NVME_SCT_GENERIC &&
2272 : 7 : sc == SPDK_NVME_SC_SUCCESS) {
2273 : : /* Completion posted asynchronously. */
2274 : 197 : return 0;
2275 : : }
2276 : : }
2277 : :
2278 : 32 : out:
2279 [ + - + - : 1361 : return post_completion(ctrlr, ctrlr->cqs[0], 0, 0, cmd->cid, sc, sct);
+ - + - +
- + - ]
2280 : 14 : }
2281 : :
2282 : : /* For ADMIN I/O DELETE SUBMISSION QUEUE the NVMf library will disconnect and free
2283 : : * queue pair, so save the command id and controller in a context.
2284 : : */
2285 : : struct vfio_user_delete_sq_ctx {
2286 : : struct nvmf_vfio_user_ctrlr *vu_ctrlr;
2287 : : uint16_t cid;
2288 : : };
2289 : :
2290 : : static void
2291 : 300 : vfio_user_qpair_delete_cb(void *cb_arg)
2292 : : {
2293 : 300 : struct vfio_user_delete_sq_ctx *ctx = cb_arg;
2294 [ + - + - ]: 300 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = ctx->vu_ctrlr;
2295 [ + - + - : 300 : struct nvmf_vfio_user_cq *admin_cq = vu_ctrlr->cqs[0];
+ - + - ]
2296 : :
2297 [ + + # # ]: 300 : assert(admin_cq != NULL);
2298 [ + + + - : 300 : assert(admin_cq->group != NULL);
+ - # # ]
2299 [ + + + - : 300 : assert(admin_cq->group->group->thread != NULL);
+ - + - +
- + - + -
# # ]
2300 [ + + + - : 300 : if (admin_cq->group->group->thread != spdk_get_thread()) {
+ - + - +
- + - -
+ ]
2301 [ # # # # : 124 : spdk_thread_send_msg(admin_cq->group->group->thread,
# # # # #
# # # ]
2302 : : vfio_user_qpair_delete_cb,
2303 : 0 : cb_arg);
2304 : 0 : } else {
2305 : 183 : post_completion(vu_ctrlr, admin_cq, 0, 0,
2306 [ + - + - ]: 176 : ctx->cid,
2307 : : SPDK_NVME_SC_SUCCESS, SPDK_NVME_SCT_GENERIC);
2308 : 176 : free(ctx);
2309 : : }
2310 : 300 : }
2311 : :
2312 : : /*
2313 : : * Deletes a completion or submission I/O queue.
2314 : : */
2315 : : static int
2316 : 1519 : handle_del_io_q(struct nvmf_vfio_user_ctrlr *ctrlr,
2317 : : struct spdk_nvme_cmd *cmd, const bool is_cq)
2318 : : {
2319 : 1519 : uint16_t sct = SPDK_NVME_SCT_GENERIC;
2320 : 1519 : uint16_t sc = SPDK_NVME_SC_SUCCESS;
2321 : : struct nvmf_vfio_user_sq *sq;
2322 : : struct nvmf_vfio_user_cq *cq;
2323 : :
2324 [ + + + + : 1519 : SPDK_DEBUGLOG(nvmf_vfio, "%s: delete I/O %cqid:%d\n",
+ - # # #
# # # # #
# # ]
2325 : : ctrlr_id(ctrlr), is_cq ? 'c' : 's',
2326 : : cmd->cdw10_bits.delete_io_q.qid);
2327 : :
2328 [ + + + - : 1519 : if (!io_q_exists(ctrlr, cmd->cdw10_bits.delete_io_q.qid, is_cq)) {
+ - + - +
- + - ]
2329 [ + + # # : 1168 : SPDK_ERRLOG("%s: I/O %cqid:%d does not exist\n", ctrlr_id(ctrlr),
# # # # #
# ]
2330 : : is_cq ? 'c' : 's', cmd->cdw10_bits.delete_io_q.qid);
2331 : 1168 : sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2332 : 1168 : sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER;
2333 : 1168 : goto out;
2334 : : }
2335 : :
2336 [ + + + + ]: 358 : if (is_cq) {
2337 [ + - + - : 175 : cq = ctrlr->cqs[cmd->cdw10_bits.delete_io_q.qid];
+ - + - +
- + - + -
+ - ]
2338 [ + + + - : 175 : if (cq->cq_ref) {
- + ]
2339 : 2 : SPDK_ERRLOG("%s: the associated SQ must be deleted first\n", ctrlr_id(ctrlr));
2340 : 2 : sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
2341 : 2 : sc = SPDK_NVME_SC_INVALID_QUEUE_DELETION;
2342 : 2 : goto out;
2343 : : }
2344 : 173 : delete_cq_done(ctrlr, cq);
2345 : 7 : } else {
2346 : : /*
2347 : : * Deletion of the CQ is only deferred to delete_sq_done() on
2348 : : * VM reboot or CC.EN change, so we have to delete it in all
2349 : : * other cases.
2350 : : */
2351 [ + - + - : 176 : sq = ctrlr->sqs[cmd->cdw10_bits.delete_io_q.qid];
+ - + - +
- + - + -
+ - ]
2352 [ + - + - ]: 176 : sq->delete_ctx = calloc(1, sizeof(*sq->delete_ctx));
2353 [ + + + - : 176 : if (!sq->delete_ctx) {
+ - ]
2354 : 0 : sct = SPDK_NVME_SCT_GENERIC;
2355 : 0 : sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2356 : 0 : goto out;
2357 : : }
2358 [ + - + - : 176 : sq->delete_ctx->vu_ctrlr = ctrlr;
+ - + - ]
2359 [ + - + - : 176 : sq->delete_ctx->cid = cmd->cid;
+ - + - +
- + - ]
2360 [ + - + - ]: 176 : sq->sq_state = VFIO_USER_SQ_DELETED;
2361 [ + + + - : 176 : assert(ctrlr->cqs[sq->cqid]->cq_ref);
+ - + - +
- + - + -
+ - + - #
# ]
2362 [ + - + - : 176 : ctrlr->cqs[sq->cqid]->cq_ref--;
+ - + - +
- + - + -
+ - ]
2363 : :
2364 [ + - ]: 176 : spdk_nvmf_qpair_disconnect(&sq->qpair);
2365 : 176 : return 0;
2366 : : }
2367 : :
2368 : 1336 : out:
2369 [ + - + - : 1343 : return post_completion(ctrlr, ctrlr->cqs[0], 0, 0, cmd->cid, sc, sct);
+ - + - +
- + - ]
2370 : 14 : }
2371 : :
2372 : : /*
2373 : : * Configures Shadow Doorbells.
2374 : : */
2375 : : static int
2376 : 0 : handle_doorbell_buffer_config(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd)
2377 : : {
2378 : 0 : struct nvmf_vfio_user_shadow_doorbells *sdbl = NULL;
2379 : : uint32_t dstrd;
2380 : : uintptr_t page_size, page_mask;
2381 : : uint64_t prp1, prp2;
2382 : 0 : uint16_t sct = SPDK_NVME_SCT_GENERIC;
2383 : 0 : uint16_t sc = SPDK_NVME_SC_INVALID_FIELD;
2384 : :
2385 [ # # # # ]: 0 : assert(ctrlr != NULL);
2386 [ # # # # : 0 : assert(ctrlr->endpoint != NULL);
# # # # ]
2387 [ # # # # ]: 0 : assert(cmd != NULL);
2388 : :
2389 : 0 : dstrd = doorbell_stride(ctrlr);
2390 : 0 : page_size = memory_page_size(ctrlr);
2391 : 0 : page_mask = memory_page_mask(ctrlr);
2392 : :
2393 : : /* FIXME: we don't check doorbell stride when setting queue doorbells. */
2394 [ # # # # ]: 0 : if ((4u << dstrd) * NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR > page_size) {
2395 : 0 : SPDK_ERRLOG("%s: doorbells do not fit in a single host page",
2396 : : ctrlr_id(ctrlr));
2397 : :
2398 : 0 : goto out;
2399 : : }
2400 : :
2401 : : /* Verify guest physical addresses passed as PRPs. */
2402 [ # # # # ]: 0 : if (cmd->psdt != SPDK_NVME_PSDT_PRP) {
2403 : 0 : SPDK_ERRLOG("%s: received Doorbell Buffer Config without PRPs",
2404 : : ctrlr_id(ctrlr));
2405 : :
2406 : 0 : goto out;
2407 : : }
2408 : :
2409 [ # # # # : 0 : prp1 = cmd->dptr.prp.prp1;
# # # # ]
2410 [ # # # # : 0 : prp2 = cmd->dptr.prp.prp2;
# # # # ]
2411 : :
2412 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio,
# # ]
2413 : : "%s: configuring shadow doorbells with PRP1=%#lx and PRP2=%#lx (GPAs)\n",
2414 : : ctrlr_id(ctrlr), prp1, prp2);
2415 : :
2416 [ # # ]: 0 : if (prp1 == prp2
2417 [ # # ]: 0 : || prp1 != (prp1 & page_mask)
2418 [ # # ]: 0 : || prp2 != (prp2 & page_mask)) {
2419 : 0 : SPDK_ERRLOG("%s: invalid shadow doorbell GPAs\n",
2420 : : ctrlr_id(ctrlr));
2421 : :
2422 : 0 : goto out;
2423 : : }
2424 : :
2425 : : /* Map guest physical addresses to our virtual address space. */
2426 [ # # # # : 0 : sdbl = map_sdbl(ctrlr->endpoint->vfu_ctx, prp1, prp2, page_size);
# # # # ]
2427 [ # # ]: 0 : if (sdbl == NULL) {
2428 : 0 : SPDK_ERRLOG("%s: failed to map shadow doorbell buffers\n",
2429 : : ctrlr_id(ctrlr));
2430 : :
2431 : 0 : goto out;
2432 : : }
2433 : :
2434 [ # # # # ]: 0 : ctrlr->shadow_doorbell_buffer = prp1;
2435 [ # # # # ]: 0 : ctrlr->eventidx_buffer = prp2;
2436 : :
2437 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
2438 : : "%s: mapped shadow doorbell buffers [%p, %p) and [%p, %p)\n",
2439 : : ctrlr_id(ctrlr),
2440 : : sdbl->iovs[0].iov_base,
2441 : : sdbl->iovs[0].iov_base + sdbl->iovs[0].iov_len,
2442 : : sdbl->iovs[1].iov_base,
2443 : : sdbl->iovs[1].iov_base + sdbl->iovs[1].iov_len);
2444 : :
2445 : :
2446 : : /*
2447 : : * Set all possible CQ head doorbells to polling mode now, such that we
2448 : : * don't have to worry about it later if the host creates more queues.
2449 : : *
2450 : : * We only ever want interrupts for writes to the SQ tail doorbells
2451 : : * (which are initialised in set_ctrlr_intr_mode() below).
2452 : : */
2453 [ # # ]: 0 : for (uint16_t i = 0; i < NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR; ++i) {
2454 [ # # # # : 0 : sdbl->eventidxs[queue_index(i, true)] = NVMF_VFIO_USER_EVENTIDX_POLL;
# # ]
2455 : 0 : }
2456 : :
2457 : : /* Update controller. */
2458 [ # # # # : 0 : SWAP(ctrlr->sdbl, sdbl);
# # # # ]
2459 : :
2460 : : /*
2461 : : * Copy doorbells from either the previous shadow doorbell buffer or the
2462 : : * BAR0 doorbells and make I/O queue doorbells point to the new buffer.
2463 : : *
2464 : : * This needs to account for older versions of the Linux NVMe driver,
2465 : : * which don't clear out the buffer after a controller reset.
2466 : : */
2467 [ # # ]: 0 : copy_doorbells(ctrlr, sdbl != NULL ?
2468 [ # # # # : 0 : sdbl->shadow_doorbells : ctrlr->bar0_doorbells,
# # # # ]
2469 [ # # # # : 0 : ctrlr->sdbl->shadow_doorbells);
# # # # ]
2470 : :
2471 : 0 : vfio_user_ctrlr_switch_doorbells(ctrlr, true);
2472 : :
2473 : 0 : ctrlr_kick(ctrlr);
2474 : :
2475 : 0 : sc = SPDK_NVME_SC_SUCCESS;
2476 : :
2477 : 0 : out:
2478 : : /*
2479 : : * Unmap existing buffers, in case Doorbell Buffer Config was sent
2480 : : * more than once (pointless, but not prohibited by the spec), or
2481 : : * in case of an error.
2482 : : *
2483 : : * If this is the first time Doorbell Buffer Config was processed,
2484 : : * then we've just swapped a NULL from ctrlr->sdbl into sdbl, so
2485 : : * free_sdbl() becomes a noop.
2486 : : */
2487 [ # # # # : 0 : free_sdbl(ctrlr->endpoint->vfu_ctx, sdbl);
# # # # ]
2488 : :
2489 [ # # # # : 0 : return post_completion(ctrlr, ctrlr->cqs[0], 0, 0, cmd->cid, sc, sct);
# # # # #
# # # ]
2490 : : }
2491 : :
2492 : : /* Returns 0 on success and -errno on error. */
2493 : : static int
2494 : 163027 : consume_admin_cmd(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd)
2495 : : {
2496 [ + + # # ]: 163027 : assert(ctrlr != NULL);
2497 [ + + # # ]: 163027 : assert(cmd != NULL);
2498 : :
2499 [ + + - + ]: 163027 : if (cmd->fuse != 0) {
2500 : : /* Fused admin commands are not supported. */
2501 [ # # # # : 2 : return post_completion(ctrlr, ctrlr->cqs[0], 0, 0, cmd->cid,
# # # # #
# # # ]
2502 : : SPDK_NVME_SC_INVALID_FIELD,
2503 : : SPDK_NVME_SCT_GENERIC);
2504 : : }
2505 : :
2506 [ + + + + : 163025 : switch (cmd->opc) {
+ - ]
2507 : 1544 : case SPDK_NVME_OPC_CREATE_IO_CQ:
2508 : : case SPDK_NVME_OPC_CREATE_IO_SQ:
2509 : 1558 : return handle_create_io_q(ctrlr, cmd,
2510 [ + - ]: 1558 : cmd->opc == SPDK_NVME_OPC_CREATE_IO_CQ);
2511 : 1505 : case SPDK_NVME_OPC_DELETE_IO_SQ:
2512 : : case SPDK_NVME_OPC_DELETE_IO_CQ:
2513 : 1519 : return handle_del_io_q(ctrlr, cmd,
2514 [ + - ]: 1519 : cmd->opc == SPDK_NVME_OPC_DELETE_IO_CQ);
2515 : 599 : case SPDK_NVME_OPC_DOORBELL_BUFFER_CONFIG:
2516 [ - + # # : 599 : SPDK_NOTICELOG("%s: requested shadow doorbells (supported: %d)\n",
# # # # #
# # # ]
2517 : : ctrlr_id(ctrlr),
2518 : : !ctrlr->transport->transport_opts.disable_shadow_doorbells);
2519 [ - + - + : 599 : if (!ctrlr->transport->transport_opts.disable_shadow_doorbells) {
# # # # #
# # # #
# ]
2520 : 0 : return handle_doorbell_buffer_config(ctrlr, cmd);
2521 : : }
2522 : : /* FALLTHROUGH */
2523 : : default:
2524 [ + - + - : 159948 : return handle_cmd_req(ctrlr, cmd, ctrlr->sqs[0]);
+ - + - ]
2525 : : }
2526 : 105 : }
2527 : :
2528 : : static int
2529 : 3184183 : handle_cmd_rsp(struct nvmf_vfio_user_req *vu_req, void *cb_arg)
2530 : : {
2531 : 3184183 : struct nvmf_vfio_user_sq *sq = cb_arg;
2532 [ + - + - ]: 3184183 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = sq->ctrlr;
2533 : : uint16_t sqid, cqid;
2534 : :
2535 [ + + # # ]: 3184183 : assert(sq != NULL);
2536 [ + + # # ]: 3184183 : assert(vu_req != NULL);
2537 [ + + # # ]: 3184183 : assert(vu_ctrlr != NULL);
2538 : :
2539 [ + + + - : 3184183 : if (spdk_likely(vu_req->iovcnt)) {
+ + ]
2540 [ + - + - : 1994993 : vfu_sgl_put(vu_ctrlr->endpoint->vfu_ctx,
+ - + - ]
2541 [ + - ]: 1845423 : index_to_sg_t(vu_req->sg, 0),
2542 [ + - + - : 1845423 : vu_req->iov, vu_req->iovcnt);
+ - ]
2543 : 149570 : }
2544 [ + - + - ]: 3184183 : sqid = sq->qid;
2545 [ + - + - ]: 3184183 : cqid = sq->cqid;
2546 : :
2547 [ + - + - : 3333802 : return post_completion(vu_ctrlr, vu_ctrlr->cqs[cqid],
+ - + - ]
2548 [ + - + - : 3184183 : vu_req->req.rsp->nvme_cpl.cdw0,
+ - + - +
- + - ]
2549 : 149619 : sqid,
2550 [ + - + - : 3184183 : vu_req->req.cmd->nvme_cmd.cid,
+ - + - +
- + - ]
2551 [ + - + - : 3184183 : vu_req->req.rsp->nvme_cpl.status.sc,
+ - + - +
- + - +
- ]
2552 [ + - + - : 3184183 : vu_req->req.rsp->nvme_cpl.status.sct);
+ - + - +
- + - +
- ]
2553 : : }
2554 : :
2555 : : static int
2556 : 3187270 : consume_cmd(struct nvmf_vfio_user_ctrlr *ctrlr, struct nvmf_vfio_user_sq *sq,
2557 : : struct spdk_nvme_cmd *cmd)
2558 : : {
2559 [ + + # # ]: 3187270 : assert(sq != NULL);
2560 [ + + + + ]: 3187270 : if (spdk_unlikely(nvmf_qpair_is_admin_queue(&sq->qpair))) {
2561 : 163027 : return consume_admin_cmd(ctrlr, cmd);
2562 : : }
2563 : :
2564 : 3024243 : return handle_cmd_req(ctrlr, cmd, sq);
2565 : 149647 : }
2566 : :
2567 : : /* Returns the number of commands processed, or a negative value on error. */
2568 : : static int
2569 : 270646 : handle_sq_tdbl_write(struct nvmf_vfio_user_ctrlr *ctrlr, const uint32_t new_tail,
2570 : : struct nvmf_vfio_user_sq *sq)
2571 : : {
2572 : : struct spdk_nvme_cmd *queue;
2573 [ + - + - : 270646 : struct nvmf_vfio_user_cq *cq = ctrlr->cqs[sq->cqid];
+ - + - +
- + - ]
2574 : 270646 : int count = 0;
2575 : : uint32_t free_cq_slots;
2576 : :
2577 [ + + # # ]: 270646 : assert(ctrlr != NULL);
2578 [ + + # # ]: 270646 : assert(sq != NULL);
2579 : :
2580 [ + + + - : 270646 : if (ctrlr->sdbl != NULL && sq->qid != 0) {
- + # # #
# # # ]
2581 : : /*
2582 : : * Submission queue index has moved past the event index, so it
2583 : : * needs to be re-armed before we go to sleep.
2584 : : */
2585 [ # # # # ]: 0 : sq->need_rearm = true;
2586 : 0 : }
2587 : :
2588 : 270646 : free_cq_slots = cq_free_slots(cq);
2589 [ + - ]: 270646 : queue = q_addr(&sq->mapping);
2590 [ + + ]: 3475555 : while (*sq_headp(sq) != new_tail) {
2591 : : int err;
2592 : : struct spdk_nvme_cmd *cmd;
2593 : :
2594 : : /*
2595 : : * At least the Linux nvme driver can submit more requests than
2596 : : * our current view of the available free CQ slots, although it
2597 : : * is not clear exactly why or how; it is relatively rare even
2598 : : * under high load.
2599 : : *
2600 : : * As we need to make sure we have free CQ slots (see
2601 : : * post_completion()), we implement flow control here: if the
2602 : : * number of currently outstanding requests for this SQ would
2603 : : * use all the available CQ slots, then we cannot submit this
2604 : : * new request.
2605 : : *
2606 : : * Instead we back off until the driver has informed us that CQ
2607 : : * slots are available.
2608 : : */
2609 [ + + + - : 3204909 : if ((free_cq_slots-- <= cq->nr_outstanding)) {
+ + ]
2610 : : struct nvmf_vfio_user_poll_group *vu_group;
2611 [ + - + - ]: 17639 : cq->last_head = *cq_dbl_headp(cq);
2612 : :
2613 : 17639 : free_cq_slots = cq_free_slots(cq);
2614 [ + - + - : 17639 : if (free_cq_slots > cq->nr_outstanding) {
+ - ]
2615 : 17639 : continue;
2616 : : }
2617 : :
2618 : 0 : vu_group = sq_to_poll_group(sq);
2619 : :
2620 [ # # # # ]: 0 : vu_group->stats.cq_full++;
2621 : :
2622 : : /*
2623 : : * There are no free CQ slots, so stop processing
2624 : : * submissions for this SQ until "a later time". In
2625 : : * interrupt mode, we need to kick ourselves, so that we
2626 : : * are guaranteed to wake up and come back here.
2627 : : */
2628 [ # # # # : 0 : if (in_interrupt_mode(ctrlr->transport)) {
# # ]
2629 [ # # # # ]: 0 : vu_group->need_kick = true;
2630 : 0 : }
2631 : 0 : break;
2632 : : }
2633 : :
2634 [ + - ]: 3187270 : cmd = &queue[*sq_headp(sq)];
2635 [ + - ]: 3187270 : count++;
2636 : :
2637 [ + - ]: 3187270 : cq->nr_outstanding++;
2638 : :
2639 : : /*
2640 : : * SQHD must contain the new head pointer, so we must increase
2641 : : * it before we generate a completion.
2642 : : */
2643 : 3187270 : sq_head_advance(sq);
2644 : :
2645 : 3187270 : err = consume_cmd(ctrlr, sq, cmd);
2646 [ + + ]: 3187270 : if (spdk_unlikely(err != 0)) {
2647 : 0 : return err;
2648 : : }
2649 : : }
2650 : :
2651 : 270646 : return count;
2652 : 149619 : }
2653 : :
2654 : : /* Checks whether endpoint is connected from the same process */
2655 : : static bool
2656 : 2206 : is_peer_same_process(struct nvmf_vfio_user_endpoint *endpoint)
2657 : : {
2658 : 176 : struct ucred ucred;
2659 : 2206 : socklen_t ucredlen = sizeof(ucred);
2660 : :
2661 [ + + ]: 2206 : if (endpoint == NULL) {
2662 : 0 : return false;
2663 : : }
2664 : :
2665 [ + + + - : 2206 : if (getsockopt(vfu_get_poll_fd(endpoint->vfu_ctx), SOL_SOCKET, SO_PEERCRED, &ucred,
+ - + - ]
2666 : 1922 : &ucredlen) < 0) {
2667 [ # # ]: 0 : SPDK_ERRLOG("getsockopt(SO_PEERCRED): %s\n", strerror(errno));
2668 : 0 : return false;
2669 : : }
2670 : :
2671 : 2206 : return ucred.pid == getpid();
2672 : 1922 : }
2673 : :
2674 : : static void
2675 : 1296 : memory_region_add_cb(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
2676 : : {
2677 : 1296 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
2678 : : struct nvmf_vfio_user_ctrlr *ctrlr;
2679 : : struct nvmf_vfio_user_sq *sq;
2680 : : struct nvmf_vfio_user_cq *cq;
2681 : : void *map_start, *map_end;
2682 : : int ret;
2683 : :
2684 : : /*
2685 : : * We're not interested in any DMA regions that aren't mappable (we don't
2686 : : * support clients that don't share their memory).
2687 : : */
2688 [ + + + - : 1296 : if (!info->vaddr) {
- + ]
2689 : 152 : return;
2690 : : }
2691 : :
2692 [ + - + - : 1144 : map_start = info->mapping.iov_base;
+ - ]
2693 [ + - + - : 1144 : map_end = info->mapping.iov_base + info->mapping.iov_len;
+ - + - +
- + - ]
2694 : :
2695 [ + - + - : 2105 : if (((uintptr_t)info->mapping.iov_base & MASK_2MB) ||
+ - + - +
- - + ]
2696 [ + + + - : 1144 : (info->mapping.iov_len & MASK_2MB)) {
+ - + - ]
2697 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "Invalid memory region vaddr %p, IOVA %p-%p\n",
# # # # #
# ]
2698 : : info->vaddr, map_start, map_end);
2699 : 0 : return;
2700 : : }
2701 : :
2702 [ + + # # ]: 1144 : assert(endpoint != NULL);
2703 [ + + + - : 1144 : if (endpoint->ctrlr == NULL) {
+ - ]
2704 : 0 : return;
2705 : : }
2706 [ + - + - ]: 1144 : ctrlr = endpoint->ctrlr;
2707 : :
2708 [ + + + + : 1144 : SPDK_DEBUGLOG(nvmf_vfio, "%s: map IOVA %p-%p\n", endpoint_id(endpoint),
+ - ]
2709 : : map_start, map_end);
2710 : :
2711 : : /* VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE are enabled when registering to VFIO, here we also
2712 : : * check the protection bits before registering. When vfio client and server are run in same process
2713 : : * there is no need to register the same memory again.
2714 : : */
2715 [ + + + - : 1144 : if (info->prot == (PROT_WRITE | PROT_READ) && !is_peer_same_process(endpoint)) {
+ - + - ]
2716 [ # # # # : 142 : ret = spdk_mem_register(info->mapping.iov_base, info->mapping.iov_len);
# # # # #
# # # ]
2717 [ - + ]: 142 : if (ret) {
2718 : 0 : SPDK_ERRLOG("Memory region register %p-%p failed, ret=%d\n",
2719 : : map_start, map_end, ret);
2720 : 0 : }
2721 : 0 : }
2722 : :
2723 [ + + + - ]: 1144 : pthread_mutex_lock(&endpoint->lock);
2724 [ + + + - : 3031 : TAILQ_FOREACH(sq, &ctrlr->connected_sqs, tailq) {
+ - + + +
- + - +
- ]
2725 [ + + + - : 1887 : if (sq->sq_state != VFIO_USER_SQ_INACTIVE) {
+ - ]
2726 : 1827 : continue;
2727 : : }
2728 : :
2729 [ # # # # : 60 : cq = ctrlr->cqs[sq->cqid];
# # # # #
# # # ]
2730 : :
2731 : : /* For shared CQ case, we will use q_addr() to avoid mapping CQ multiple times */
2732 [ + - + - : 60 : if (cq->size && q_addr(&cq->mapping) == NULL) {
# # # # #
# ]
2733 [ # # # # : 60 : ret = map_q(ctrlr, &cq->mapping, MAP_RW | MAP_QUIET);
# # # # #
# ]
2734 [ + + ]: 60 : if (ret) {
2735 [ - + - + : 50 : SPDK_DEBUGLOG(nvmf_vfio, "Memory isn't ready to remap cqid:%d %#lx-%#lx\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2736 : : cq->qid, cq->mapping.prp1,
2737 : : cq->mapping.prp1 + cq->mapping.len);
2738 : 50 : continue;
2739 : : }
2740 : 0 : }
2741 : :
2742 [ + - # # : 10 : if (sq->size) {
# # ]
2743 [ # # # # : 10 : ret = map_q(ctrlr, &sq->mapping, MAP_R | MAP_QUIET);
# # ]
2744 [ - + ]: 10 : if (ret) {
2745 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "Memory isn't ready to remap sqid:%d %#lx-%#lx\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2746 : : sq->qid, sq->mapping.prp1,
2747 : : sq->mapping.prp1 + sq->mapping.len);
2748 : 0 : continue;
2749 : : }
2750 : 0 : }
2751 [ # # # # ]: 10 : sq->sq_state = VFIO_USER_SQ_ACTIVE;
2752 [ - + - + : 10 : SPDK_DEBUGLOG(nvmf_vfio, "Remap sqid:%u successfully\n", sq->qid);
# # # # #
# ]
2753 : 0 : }
2754 [ + + + - ]: 1144 : pthread_mutex_unlock(&endpoint->lock);
2755 : 961 : }
2756 : :
2757 : : static void
2758 : 1296 : memory_region_remove_cb(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
2759 : : {
2760 : 1296 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
2761 : : struct nvmf_vfio_user_sq *sq;
2762 : : struct nvmf_vfio_user_cq *cq;
2763 : : void *map_start, *map_end;
2764 : 1296 : int ret = 0;
2765 : :
2766 [ + + + - : 1296 : if (!info->vaddr) {
- + ]
2767 : 152 : return;
2768 : : }
2769 : :
2770 [ + - + - : 1144 : map_start = info->mapping.iov_base;
+ - ]
2771 [ + - + - : 1144 : map_end = info->mapping.iov_base + info->mapping.iov_len;
+ - + - +
- + - ]
2772 : :
2773 [ + - + - : 2105 : if (((uintptr_t)info->mapping.iov_base & MASK_2MB) ||
+ - + - +
- - + ]
2774 [ + + + - : 1144 : (info->mapping.iov_len & MASK_2MB)) {
+ - + - ]
2775 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "Invalid memory region vaddr %p, IOVA %p-%p\n",
# # # # #
# ]
2776 : : info->vaddr, map_start, map_end);
2777 : 0 : return;
2778 : : }
2779 : :
2780 [ + + # # ]: 1144 : assert(endpoint != NULL);
2781 [ + + + + : 1144 : SPDK_DEBUGLOG(nvmf_vfio, "%s: unmap IOVA %p-%p\n", endpoint_id(endpoint),
+ - ]
2782 : : map_start, map_end);
2783 : :
2784 [ + + + - : 1144 : if (endpoint->ctrlr != NULL) {
+ - ]
2785 : : struct nvmf_vfio_user_ctrlr *ctrlr;
2786 [ + - + - ]: 1088 : ctrlr = endpoint->ctrlr;
2787 : :
2788 [ + + + - ]: 1088 : pthread_mutex_lock(&endpoint->lock);
2789 [ + + + - : 2723 : TAILQ_FOREACH(sq, &ctrlr->connected_sqs, tailq) {
+ - + + +
- + - +
- ]
2790 [ + + + + : 1635 : if (q_addr(&sq->mapping) >= map_start && q_addr(&sq->mapping) <= map_end) {
# # # # ]
2791 [ # # ]: 10 : unmap_q(ctrlr, &sq->mapping);
2792 [ # # # # ]: 10 : sq->sq_state = VFIO_USER_SQ_INACTIVE;
2793 : 0 : }
2794 : :
2795 [ + - + - : 1635 : cq = ctrlr->cqs[sq->cqid];
+ - + - +
- + - ]
2796 [ + + + + : 1635 : if (q_addr(&cq->mapping) >= map_start && q_addr(&cq->mapping) <= map_end) {
# # # # ]
2797 [ # # ]: 10 : unmap_q(ctrlr, &cq->mapping);
2798 : 0 : }
2799 : 961 : }
2800 : :
2801 [ + + + - : 1088 : if (ctrlr->sdbl != NULL) {
+ - ]
2802 : : size_t i;
2803 : :
2804 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT; i++) {
2805 [ # # # # : 0 : const void *const iov_base = ctrlr->sdbl->iovs[i].iov_base;
# # # # #
# # # #
# ]
2806 : :
2807 [ # # # # ]: 0 : if (iov_base >= map_start && iov_base < map_end) {
2808 : 0 : copy_doorbells(ctrlr,
2809 [ # # # # : 0 : ctrlr->sdbl->shadow_doorbells,
# # # # ]
2810 [ # # # # ]: 0 : ctrlr->bar0_doorbells);
2811 : 0 : vfio_user_ctrlr_switch_doorbells(ctrlr, false);
2812 [ # # # # : 0 : free_sdbl(endpoint->vfu_ctx, ctrlr->sdbl);
# # # # ]
2813 [ # # # # ]: 0 : ctrlr->sdbl = NULL;
2814 : 0 : break;
2815 : : }
2816 : 0 : }
2817 : 0 : }
2818 : :
2819 [ + + + - ]: 1088 : pthread_mutex_unlock(&endpoint->lock);
2820 : 961 : }
2821 : :
2822 [ + + + - : 1144 : if (info->prot == (PROT_WRITE | PROT_READ) && !is_peer_same_process(endpoint)) {
+ - + - ]
2823 [ # # # # : 142 : ret = spdk_mem_unregister(info->mapping.iov_base, info->mapping.iov_len);
# # # # #
# # # ]
2824 [ - + ]: 142 : if (ret) {
2825 : 0 : SPDK_ERRLOG("Memory region unregister %p-%p failed, ret=%d\n",
2826 : : map_start, map_end, ret);
2827 : 0 : }
2828 : 0 : }
2829 : 961 : }
2830 : :
2831 : : /* Used to initiate a controller-level reset or a controller shutdown. */
2832 : : static void
2833 : 56 : disable_ctrlr(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
2834 : : {
2835 : 56 : SPDK_NOTICELOG("%s: disabling controller\n", ctrlr_id(vu_ctrlr));
2836 : :
2837 : : /* Unmap Admin queue. */
2838 : :
2839 [ + + + - : 56 : assert(vu_ctrlr->sqs[0] != NULL);
+ - + - +
- # # ]
2840 [ + + + - : 56 : assert(vu_ctrlr->cqs[0] != NULL);
+ - + - +
- # # ]
2841 : :
2842 [ + - + - : 56 : unmap_q(vu_ctrlr, &vu_ctrlr->sqs[0]->mapping);
+ - + - +
- ]
2843 [ + - + - : 56 : unmap_q(vu_ctrlr, &vu_ctrlr->cqs[0]->mapping);
+ - + - +
- ]
2844 : :
2845 [ + - + - : 56 : vu_ctrlr->sqs[0]->size = 0;
+ - + - +
- + - ]
2846 [ + - + - : 56 : *sq_headp(vu_ctrlr->sqs[0]) = 0;
+ - + - ]
2847 : :
2848 [ + - + - : 56 : vu_ctrlr->sqs[0]->sq_state = VFIO_USER_SQ_INACTIVE;
+ - + - +
- + - ]
2849 : :
2850 [ + - + - : 56 : vu_ctrlr->cqs[0]->size = 0;
+ - + - +
- + - ]
2851 [ + - + - : 56 : *cq_tailp(vu_ctrlr->cqs[0]) = 0;
+ - + - ]
2852 : :
2853 : : /*
2854 : : * For PCIe controller reset or shutdown, we will drop all AER
2855 : : * responses.
2856 : : */
2857 [ + - + - ]: 56 : spdk_nvmf_ctrlr_abort_aer(vu_ctrlr->ctrlr);
2858 : :
2859 : : /* Free the shadow doorbell buffer. */
2860 : 56 : vfio_user_ctrlr_switch_doorbells(vu_ctrlr, false);
2861 [ + - + - : 56 : free_sdbl(vu_ctrlr->endpoint->vfu_ctx, vu_ctrlr->sdbl);
+ - + - +
- + - ]
2862 [ + - + - ]: 56 : vu_ctrlr->sdbl = NULL;
2863 : 56 : }
2864 : :
2865 : : /* Used to re-enable the controller after a controller-level reset. */
2866 : : static int
2867 : 59 : enable_ctrlr(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
2868 : : {
2869 : : int err;
2870 : :
2871 [ + + # # ]: 59 : assert(vu_ctrlr != NULL);
2872 : :
2873 : 59 : SPDK_NOTICELOG("%s: enabling controller\n", ctrlr_id(vu_ctrlr));
2874 : :
2875 : 59 : err = acq_setup(vu_ctrlr);
2876 [ - + ]: 59 : if (err != 0) {
2877 : 0 : return err;
2878 : : }
2879 : :
2880 : 59 : err = asq_setup(vu_ctrlr);
2881 [ - + ]: 59 : if (err != 0) {
2882 : 0 : return err;
2883 : : }
2884 : :
2885 [ + - + - : 59 : vu_ctrlr->sqs[0]->sq_state = VFIO_USER_SQ_ACTIVE;
+ - + - +
- + - ]
2886 : :
2887 : 59 : return 0;
2888 : 7 : }
2889 : :
2890 : : static int
2891 : 359 : nvmf_vfio_user_prop_req_rsp_set(struct nvmf_vfio_user_req *req,
2892 : : struct nvmf_vfio_user_sq *sq)
2893 : : {
2894 : : struct nvmf_vfio_user_ctrlr *vu_ctrlr;
2895 : : union spdk_nvme_cc_register cc, diff;
2896 : :
2897 [ + + + - : 359 : assert(req->req.cmd->prop_set_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET);
+ - + - +
- + - + -
# # ]
2898 [ + + + - : 359 : assert(sq->ctrlr != NULL);
+ - # # ]
2899 [ + - + - ]: 359 : vu_ctrlr = sq->ctrlr;
2900 : :
2901 [ + + + - : 359 : if (req->req.cmd->prop_set_cmd.ofst != offsetof(struct spdk_nvme_registers, cc)) {
+ - + - +
- + - +
+ ]
2902 : 225 : return 0;
2903 : : }
2904 : :
2905 [ + - + - : 134 : cc.raw = req->req.cmd->prop_set_cmd.value.u64;
+ - + - +
- + - +
- ]
2906 [ + - + - : 134 : diff.raw = cc.raw ^ req->cc.raw;
+ - ]
2907 : :
2908 [ + + ]: 134 : if (diff.bits.en) {
2909 [ + + ]: 64 : if (cc.bits.en) {
2910 : 59 : int ret = enable_ctrlr(vu_ctrlr);
2911 [ - + ]: 59 : if (ret) {
2912 : 0 : SPDK_ERRLOG("%s: failed to enable ctrlr\n", ctrlr_id(vu_ctrlr));
2913 : 0 : return ret;
2914 : : }
2915 [ + - + - ]: 59 : vu_ctrlr->reset_shn = false;
2916 : 7 : } else {
2917 [ # # # # ]: 5 : vu_ctrlr->reset_shn = true;
2918 : : }
2919 : 7 : }
2920 : :
2921 [ + + ]: 134 : if (diff.bits.shn) {
2922 [ - + - - ]: 46 : if (cc.bits.shn == SPDK_NVME_SHN_NORMAL || cc.bits.shn == SPDK_NVME_SHN_ABRUPT) {
2923 [ + - + - ]: 46 : vu_ctrlr->reset_shn = true;
2924 : 7 : }
2925 : 7 : }
2926 : :
2927 [ + + + + : 134 : if (vu_ctrlr->reset_shn) {
+ - + + ]
2928 : 56 : disable_ctrlr(vu_ctrlr);
2929 : 7 : }
2930 : 134 : return 0;
2931 : 49 : }
2932 : :
2933 : : static int
2934 : 948 : nvmf_vfio_user_prop_req_rsp(struct nvmf_vfio_user_req *req, void *cb_arg)
2935 : : {
2936 : 948 : struct nvmf_vfio_user_sq *sq = cb_arg;
2937 : :
2938 [ + + # # ]: 948 : assert(sq != NULL);
2939 [ + + # # ]: 948 : assert(req != NULL);
2940 : :
2941 [ + + + - : 948 : if (req->req.cmd->prop_get_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET) {
+ - + - +
- + - +
+ ]
2942 [ + + + - : 589 : assert(sq->ctrlr != NULL);
+ - # # ]
2943 [ + + # # ]: 589 : assert(req != NULL);
2944 : :
2945 [ + + + + : 659 : memcpy(req->req.iov[0].iov_base,
+ - + - +
- + - + -
+ - ]
2946 [ + - + - : 589 : &req->req.rsp->prop_get_rsp.value.u64,
+ - + - +
- + - ]
2947 [ + - + - : 589 : req->req.length);
+ - ]
2948 : 589 : return 0;
2949 : : }
2950 : :
2951 : 359 : return nvmf_vfio_user_prop_req_rsp_set(req, sq);
2952 : 119 : }
2953 : :
2954 : : /*
2955 : : * Handles a write at offset 0x1000 or more; this is the non-mapped path when a
2956 : : * doorbell is written via access_bar0_fn().
2957 : : *
2958 : : * DSTRD is set to fixed value 0 for NVMf.
2959 : : *
2960 : : */
2961 : : static int
2962 : 0 : handle_dbl_access(struct nvmf_vfio_user_ctrlr *ctrlr, uint32_t *buf,
2963 : : const size_t count, loff_t pos, const bool is_write)
2964 : : {
2965 : : struct nvmf_vfio_user_poll_group *group;
2966 : :
2967 [ # # # # ]: 0 : assert(ctrlr != NULL);
2968 [ # # # # ]: 0 : assert(buf != NULL);
2969 : :
2970 [ # # # # ]: 0 : if (spdk_unlikely(!is_write)) {
2971 : 0 : SPDK_WARNLOG("%s: host tried to read BAR0 doorbell %#lx\n",
2972 : : ctrlr_id(ctrlr), pos);
2973 [ # # ]: 0 : errno = EPERM;
2974 : 0 : return -1;
2975 : : }
2976 : :
2977 [ # # ]: 0 : if (spdk_unlikely(count != sizeof(uint32_t))) {
2978 : 0 : SPDK_ERRLOG("%s: bad doorbell buffer size %ld\n",
2979 : : ctrlr_id(ctrlr), count);
2980 [ # # ]: 0 : errno = EINVAL;
2981 : 0 : return -1;
2982 : : }
2983 : :
2984 [ # # ]: 0 : pos -= NVME_DOORBELLS_OFFSET;
2985 : :
2986 : : /* pos must be dword aligned */
2987 [ # # ]: 0 : if (spdk_unlikely((pos & 0x3) != 0)) {
2988 : 0 : SPDK_ERRLOG("%s: bad doorbell offset %#lx\n", ctrlr_id(ctrlr), pos);
2989 [ # # ]: 0 : errno = EINVAL;
2990 : 0 : return -1;
2991 : : }
2992 : :
2993 : : /* convert byte offset to array index */
2994 [ # # ]: 0 : pos >>= 2;
2995 : :
2996 [ # # ]: 0 : if (spdk_unlikely(pos >= NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR * 2)) {
2997 : 0 : SPDK_ERRLOG("%s: bad doorbell index %#lx\n", ctrlr_id(ctrlr), pos);
2998 [ # # ]: 0 : errno = EINVAL;
2999 : 0 : return -1;
3000 : : }
3001 : :
3002 [ # # # # : 0 : ctrlr->bar0_doorbells[pos] = *buf;
# # # # ]
3003 : 0 : spdk_wmb();
3004 : :
3005 : 0 : group = ctrlr_to_poll_group(ctrlr);
3006 [ # # ]: 0 : if (pos == 1) {
3007 [ # # # # ]: 0 : group->stats.cqh_admin_writes++;
3008 [ # # ]: 0 : } else if (pos & 1) {
3009 [ # # # # ]: 0 : group->stats.cqh_io_writes++;
3010 : 0 : }
3011 : :
3012 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "%s: updating BAR0 doorbell %s:%ld to %u\n",
# # # # #
# ]
3013 : : ctrlr_id(ctrlr), (pos & 1) ? "cqid" : "sqid",
3014 : : pos / 2, *buf);
3015 : :
3016 : :
3017 : 0 : return 0;
3018 : 0 : }
3019 : :
3020 : : static size_t
3021 : 948 : vfio_user_property_access(struct nvmf_vfio_user_ctrlr *vu_ctrlr,
3022 : : char *buf, size_t count, loff_t pos,
3023 : : bool is_write)
3024 : : {
3025 : : struct nvmf_vfio_user_req *req;
3026 : : const struct spdk_nvmf_registers *regs;
3027 : :
3028 [ + + + + ]: 948 : if ((count != 4) && (count != 8)) {
3029 [ # # ]: 0 : errno = EINVAL;
3030 : 0 : return -1;
3031 : : }
3032 : :
3033 : : /* Construct a Fabric Property Get/Set command and send it */
3034 [ + - + - : 948 : req = get_nvmf_vfio_user_req(vu_ctrlr->sqs[0]);
+ - + - ]
3035 [ + + ]: 948 : if (req == NULL) {
3036 [ # # ]: 0 : errno = ENOBUFS;
3037 : 0 : return -1;
3038 : : }
3039 [ + - + - ]: 948 : regs = spdk_nvmf_ctrlr_get_regs(vu_ctrlr->ctrlr);
3040 [ + - + - : 948 : req->cc.raw = regs->cc.raw;
+ - + - +
- + - ]
3041 : :
3042 [ + - + - ]: 948 : req->cb_fn = nvmf_vfio_user_prop_req_rsp;
3043 [ + - + - : 948 : req->cb_arg = vu_ctrlr->sqs[0];
+ - + - +
- + - ]
3044 [ + - + - : 948 : req->req.cmd->prop_set_cmd.opcode = SPDK_NVME_OPC_FABRIC;
+ - + - +
- + - ]
3045 [ + - + - : 948 : req->req.cmd->prop_set_cmd.cid = 0;
+ - + - +
- + - ]
3046 [ + + ]: 948 : if (count == 4) {
3047 [ + - + - : 780 : req->req.cmd->prop_set_cmd.attrib.size = 0;
+ - + - +
- + - ]
3048 : 91 : } else {
3049 [ + - + - : 168 : req->req.cmd->prop_set_cmd.attrib.size = 1;
+ - + - +
- + - ]
3050 : : }
3051 [ + - + - : 948 : req->req.cmd->prop_set_cmd.ofst = pos;
+ - + - +
- + - ]
3052 [ + + + + ]: 948 : if (is_write) {
3053 [ + - + - : 359 : req->req.cmd->prop_set_cmd.fctype = SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET;
+ - + - +
- + - ]
3054 [ + + + - : 359 : if (req->req.cmd->prop_set_cmd.attrib.size) {
+ - + - +
- + - +
+ ]
3055 [ + - + - : 84 : req->req.cmd->prop_set_cmd.value.u64 = *(uint64_t *)buf;
+ - + - +
- + - + -
+ - ]
3056 : 14 : } else {
3057 [ + - + - : 275 : req->req.cmd->prop_set_cmd.value.u32.high = 0;
+ - + - +
- + - + -
+ - ]
3058 [ + - + - : 275 : req->req.cmd->prop_set_cmd.value.u32.low = *(uint32_t *)buf;
+ - + - +
- + - + -
+ - + - ]
3059 : : }
3060 : 49 : } else {
3061 [ + - + - : 589 : req->req.cmd->prop_get_cmd.fctype = SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET;
+ - + - +
- + - ]
3062 : : }
3063 [ + - + - : 948 : req->req.length = count;
+ - ]
3064 [ + - + - : 948 : SPDK_IOV_ONE(req->req.iov, &req->req.iovcnt, buf, req->req.length);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
3065 : :
3066 [ + - ]: 948 : spdk_nvmf_request_exec(&req->req);
3067 : :
3068 : 948 : return count;
3069 : 119 : }
3070 : :
3071 : : static ssize_t
3072 : 948 : access_bar0_fn(vfu_ctx_t *vfu_ctx, char *buf, size_t count, loff_t pos,
3073 : : bool is_write)
3074 : : {
3075 : 948 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3076 : : struct nvmf_vfio_user_ctrlr *ctrlr;
3077 : : int ret;
3078 : :
3079 [ + - + - ]: 948 : ctrlr = endpoint->ctrlr;
3080 [ + + + - : 948 : if (spdk_unlikely(endpoint->need_async_destroy || !ctrlr)) {
+ + - + -
+ ]
3081 [ # # ]: 0 : errno = EIO;
3082 : 0 : return -1;
3083 : : }
3084 : :
3085 [ - + ]: 948 : if (pos >= NVME_DOORBELLS_OFFSET) {
3086 : : /*
3087 : : * The fact that the doorbells can be memory mapped doesn't mean
3088 : : * that the client (VFIO in QEMU) is obliged to memory map them,
3089 : : * it might still elect to access them via regular read/write;
3090 : : * we might also have had disable_mappable_bar0 set.
3091 : : */
3092 : 0 : ret = handle_dbl_access(ctrlr, (uint32_t *)buf, count,
3093 [ # # ]: 0 : pos, is_write);
3094 [ # # ]: 0 : if (ret == 0) {
3095 : 0 : return count;
3096 : : }
3097 : 0 : return ret;
3098 : : }
3099 : :
3100 [ + - ]: 948 : return vfio_user_property_access(ctrlr, buf, count, pos, is_write);
3101 : 119 : }
3102 : :
3103 : : static ssize_t
3104 : 12 : access_pci_config(vfu_ctx_t *vfu_ctx, char *buf, size_t count, loff_t offset,
3105 : : bool is_write)
3106 : : {
3107 : 12 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3108 : :
3109 [ - + # # ]: 12 : if (is_write) {
3110 : 0 : SPDK_ERRLOG("%s: write %#lx-%#lx not supported\n",
3111 : : endpoint_id(endpoint), offset, offset + count);
3112 [ # # ]: 0 : errno = EINVAL;
3113 : 0 : return -1;
3114 : : }
3115 : :
3116 [ - + ]: 12 : if (offset + count > NVME_REG_CFG_SIZE) {
3117 : 0 : SPDK_ERRLOG("%s: access past end of extended PCI configuration space, want=%ld+%ld, max=%d\n",
3118 : : endpoint_id(endpoint), offset, count,
3119 : : NVME_REG_CFG_SIZE);
3120 [ # # ]: 0 : errno = ERANGE;
3121 : 0 : return -1;
3122 : : }
3123 : :
3124 [ - + - + : 12 : memcpy(buf, ((unsigned char *)endpoint->pci_config_space) + offset, count);
# # # # #
# ]
3125 : :
3126 : 12 : return count;
3127 : 0 : }
3128 : :
3129 : : static void
3130 : 58 : vfio_user_log(vfu_ctx_t *vfu_ctx, int level, char const *msg)
3131 : : {
3132 : 58 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3133 : :
3134 [ - + ]: 58 : if (level >= LOG_DEBUG) {
3135 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: %s\n", endpoint_id(endpoint), msg);
# # ]
3136 [ - + ]: 58 : } else if (level >= LOG_INFO) {
3137 [ # # # # : 0 : SPDK_INFOLOG(nvmf_vfio, "%s: %s\n", endpoint_id(endpoint), msg);
# # ]
3138 [ - + ]: 58 : } else if (level >= LOG_NOTICE) {
3139 : 0 : SPDK_NOTICELOG("%s: %s\n", endpoint_id(endpoint), msg);
3140 [ - + ]: 58 : } else if (level >= LOG_WARNING) {
3141 : 0 : SPDK_WARNLOG("%s: %s\n", endpoint_id(endpoint), msg);
3142 : 0 : } else {
3143 : 58 : SPDK_ERRLOG("%s: %s\n", endpoint_id(endpoint), msg);
3144 : : }
3145 : 58 : }
3146 : :
3147 : : static int
3148 : 31 : vfio_user_get_log_level(void)
3149 : : {
3150 : : int level;
3151 : :
3152 [ - + ]: 31 : if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf_vfio")) {
3153 : 0 : return LOG_DEBUG;
3154 : : }
3155 : :
3156 : 31 : level = spdk_log_to_syslog_level(spdk_log_get_level());
3157 [ - + ]: 31 : if (level < 0) {
3158 : 0 : return LOG_ERR;
3159 : : }
3160 : :
3161 : 31 : return level;
3162 : 14 : }
3163 : :
3164 : : static void
3165 : 31 : init_pci_config_space(vfu_pci_config_space_t *p)
3166 : : {
3167 : : /* MLBAR */
3168 [ + - + - : 31 : p->hdr.bars[0].raw = 0x0;
+ - + - +
- + - + -
+ - ]
3169 : : /* MUBAR */
3170 [ + - + - : 31 : p->hdr.bars[1].raw = 0x0;
+ - + - +
- + - + -
+ - ]
3171 : :
3172 : : /* vendor specific, let's set them to zero for now */
3173 [ + - + - : 31 : p->hdr.bars[3].raw = 0x0;
+ - + - +
- + - + -
+ - ]
3174 [ + - + - : 31 : p->hdr.bars[4].raw = 0x0;
+ - + - +
- + - + -
+ - ]
3175 [ + - + - : 31 : p->hdr.bars[5].raw = 0x0;
+ - + - +
- + - + -
+ - ]
3176 : :
3177 : : /* enable INTx */
3178 [ + - + - : 31 : p->hdr.intr.ipin = 0x1;
+ - + - +
- + - +
- ]
3179 : 31 : }
3180 : :
3181 : : struct ctrlr_quiesce_ctx {
3182 : : struct nvmf_vfio_user_endpoint *endpoint;
3183 : : struct nvmf_vfio_user_poll_group *group;
3184 : : int status;
3185 : : };
3186 : :
3187 : : static void ctrlr_quiesce(struct nvmf_vfio_user_ctrlr *vu_ctrlr);
3188 : :
3189 : : static void
3190 : 246 : _vfio_user_endpoint_resume_done_msg(void *ctx)
3191 : : {
3192 : 246 : struct nvmf_vfio_user_endpoint *endpoint = ctx;
3193 [ # # # # ]: 246 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3194 : :
3195 [ # # # # ]: 246 : endpoint->need_resume = false;
3196 : :
3197 [ - + ]: 246 : if (!vu_ctrlr) {
3198 : 0 : return;
3199 : : }
3200 : :
3201 [ - + + + : 246 : if (!vu_ctrlr->queued_quiesce) {
# # # # ]
3202 [ # # # # ]: 241 : vu_ctrlr->state = VFIO_USER_CTRLR_RUNNING;
3203 : :
3204 : : /*
3205 : : * We might have ignored new SQ entries while we were quiesced:
3206 : : * kick ourselves so we'll definitely check again while in
3207 : : * VFIO_USER_CTRLR_RUNNING state.
3208 : : */
3209 [ - + # # : 241 : if (in_interrupt_mode(endpoint->transport)) {
# # ]
3210 : 0 : ctrlr_kick(vu_ctrlr);
3211 : 0 : }
3212 : 241 : return;
3213 : : }
3214 : :
3215 : :
3216 : : /*
3217 : : * Basically, once we call `vfu_device_quiesced` the device is
3218 : : * unquiesced from libvfio-user's perspective so from the moment
3219 : : * `vfio_user_quiesce_done` returns libvfio-user might quiesce the device
3220 : : * again. However, because the NVMf subsystem is an asynchronous
3221 : : * operation, this quiesce might come _before_ the NVMf subsystem has
3222 : : * been resumed, so in the callback of `spdk_nvmf_subsystem_resume` we
3223 : : * need to check whether a quiesce was requested.
3224 : : */
3225 [ - + - + : 5 : SPDK_DEBUGLOG(nvmf_vfio, "%s has queued quiesce event, quiesce again\n",
# # ]
3226 : : ctrlr_id(vu_ctrlr));
3227 : 5 : ctrlr_quiesce(vu_ctrlr);
3228 : 0 : }
3229 : :
3230 : : static void
3231 : 246 : vfio_user_endpoint_resume_done(struct spdk_nvmf_subsystem *subsystem,
3232 : : void *cb_arg, int status)
3233 : : {
3234 : 246 : struct nvmf_vfio_user_endpoint *endpoint = cb_arg;
3235 [ # # # # ]: 246 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3236 : :
3237 [ - + - + : 246 : SPDK_DEBUGLOG(nvmf_vfio, "%s resumed done with status %d\n", endpoint_id(endpoint), status);
# # ]
3238 : :
3239 [ - + ]: 246 : if (!vu_ctrlr) {
3240 : 0 : return;
3241 : : }
3242 : :
3243 [ # # # # ]: 246 : spdk_thread_send_msg(vu_ctrlr->thread, _vfio_user_endpoint_resume_done_msg, endpoint);
3244 : 0 : }
3245 : :
3246 : : static void
3247 : 246 : vfio_user_quiesce_done(void *ctx)
3248 : : {
3249 : 246 : struct ctrlr_quiesce_ctx *quiesce_ctx = ctx;
3250 [ # # # # ]: 246 : struct nvmf_vfio_user_endpoint *endpoint = quiesce_ctx->endpoint;
3251 [ # # # # ]: 246 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3252 : : int ret;
3253 : :
3254 [ - + ]: 246 : if (!vu_ctrlr) {
3255 : 0 : free(quiesce_ctx);
3256 : 0 : return;
3257 : : }
3258 : :
3259 [ - + - + : 246 : SPDK_DEBUGLOG(nvmf_vfio, "%s device quiesced\n", ctrlr_id(vu_ctrlr));
# # ]
3260 : :
3261 [ - + # # : 246 : assert(vu_ctrlr->state == VFIO_USER_CTRLR_PAUSING);
# # # # ]
3262 [ # # # # ]: 246 : vu_ctrlr->state = VFIO_USER_CTRLR_PAUSED;
3263 [ # # # # : 246 : vfu_device_quiesced(endpoint->vfu_ctx, quiesce_ctx->status);
# # # # ]
3264 [ # # # # ]: 246 : vu_ctrlr->queued_quiesce = false;
3265 : 246 : free(quiesce_ctx);
3266 : :
3267 : : /* `vfu_device_quiesced` can change the migration state,
3268 : : * so we need to re-check `vu_ctrlr->state`.
3269 : : */
3270 [ - + # # : 246 : if (vu_ctrlr->state == VFIO_USER_CTRLR_MIGRATING) {
# # ]
3271 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s is in MIGRATION state\n", ctrlr_id(vu_ctrlr));
# # ]
3272 : 0 : return;
3273 : : }
3274 : :
3275 [ - + - + : 246 : SPDK_DEBUGLOG(nvmf_vfio, "%s start to resume\n", ctrlr_id(vu_ctrlr));
# # ]
3276 [ # # # # ]: 246 : vu_ctrlr->state = VFIO_USER_CTRLR_RESUMING;
3277 [ # # # # ]: 246 : ret = spdk_nvmf_subsystem_resume((struct spdk_nvmf_subsystem *)endpoint->subsystem,
3278 : 0 : vfio_user_endpoint_resume_done, endpoint);
3279 [ - + ]: 246 : if (ret < 0) {
3280 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_PAUSED;
3281 : 0 : SPDK_ERRLOG("%s: failed to resume, ret=%d\n", endpoint_id(endpoint), ret);
3282 : 0 : }
3283 : 0 : }
3284 : :
3285 : : static void
3286 : 246 : vfio_user_pause_done(struct spdk_nvmf_subsystem *subsystem,
3287 : : void *ctx, int status)
3288 : : {
3289 : 246 : struct ctrlr_quiesce_ctx *quiesce_ctx = ctx;
3290 [ # # # # ]: 246 : struct nvmf_vfio_user_endpoint *endpoint = quiesce_ctx->endpoint;
3291 [ # # # # ]: 246 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3292 : :
3293 [ - + ]: 246 : if (!vu_ctrlr) {
3294 : 0 : free(quiesce_ctx);
3295 : 0 : return;
3296 : : }
3297 : :
3298 [ # # # # ]: 246 : quiesce_ctx->status = status;
3299 : :
3300 [ - + - + : 246 : SPDK_DEBUGLOG(nvmf_vfio, "%s pause done with status %d\n",
# # ]
3301 : : ctrlr_id(vu_ctrlr), status);
3302 : :
3303 [ # # # # ]: 246 : spdk_thread_send_msg(vu_ctrlr->thread,
3304 : 0 : vfio_user_quiesce_done, ctx);
3305 : 0 : }
3306 : :
3307 : : /*
3308 : : * Ensure that, for this PG, we've stopped running in nvmf_vfio_user_sq_poll();
3309 : : * we've already set ctrlr->state, so we won't process new entries, but we need
3310 : : * to ensure that this PG is quiesced. This only works because there's no
3311 : : * callback context set up between polling the SQ and spdk_nvmf_request_exec().
3312 : : *
3313 : : * Once we've walked all PGs, we need to pause any submitted I/O via
3314 : : * spdk_nvmf_subsystem_pause(SPDK_NVME_GLOBAL_NS_TAG).
3315 : : */
3316 : : static void
3317 : 944 : vfio_user_quiesce_pg(void *ctx)
3318 : : {
3319 : 944 : struct ctrlr_quiesce_ctx *quiesce_ctx = ctx;
3320 [ # # # # ]: 944 : struct nvmf_vfio_user_endpoint *endpoint = quiesce_ctx->endpoint;
3321 [ # # # # ]: 944 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3322 [ # # # # ]: 944 : struct nvmf_vfio_user_poll_group *vu_group = quiesce_ctx->group;
3323 [ # # # # ]: 944 : struct spdk_nvmf_subsystem *subsystem = endpoint->subsystem;
3324 : : int ret;
3325 : :
3326 [ - + - + : 944 : SPDK_DEBUGLOG(nvmf_vfio, "quiesced pg:%p\n", vu_group);
# # ]
3327 : :
3328 [ - + ]: 944 : if (!vu_ctrlr) {
3329 : 0 : free(quiesce_ctx);
3330 : 0 : return;
3331 : : }
3332 : :
3333 [ # # # # : 944 : quiesce_ctx->group = TAILQ_NEXT(vu_group, link);
# # # # #
# ]
3334 [ + + # # : 944 : if (quiesce_ctx->group != NULL) {
# # ]
3335 [ # # # # ]: 698 : spdk_thread_send_msg(poll_group_to_thread(quiesce_ctx->group),
3336 : 0 : vfio_user_quiesce_pg, quiesce_ctx);
3337 : 698 : return;
3338 : : }
3339 : :
3340 : 246 : ret = spdk_nvmf_subsystem_pause(subsystem, SPDK_NVME_GLOBAL_NS_TAG,
3341 : 0 : vfio_user_pause_done, quiesce_ctx);
3342 [ - + ]: 246 : if (ret < 0) {
3343 : 0 : SPDK_ERRLOG("%s: failed to pause, ret=%d\n",
3344 : : endpoint_id(endpoint), ret);
3345 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_RUNNING;
3346 : 0 : fail_ctrlr(vu_ctrlr);
3347 : 0 : free(quiesce_ctx);
3348 : 0 : }
3349 : 0 : }
3350 : :
3351 : : static void
3352 : 246 : ctrlr_quiesce(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
3353 : : {
3354 : : struct ctrlr_quiesce_ctx *quiesce_ctx;
3355 : :
3356 [ # # # # ]: 246 : vu_ctrlr->state = VFIO_USER_CTRLR_PAUSING;
3357 : :
3358 : 246 : quiesce_ctx = calloc(1, sizeof(*quiesce_ctx));
3359 [ - + ]: 246 : if (!quiesce_ctx) {
3360 : 0 : SPDK_ERRLOG("Failed to allocate subsystem pause context\n");
3361 [ # # ]: 0 : assert(false);
3362 : : return;
3363 : : }
3364 : :
3365 [ # # # # : 246 : quiesce_ctx->endpoint = vu_ctrlr->endpoint;
# # # # ]
3366 [ # # # # ]: 246 : quiesce_ctx->status = 0;
3367 [ # # # # : 246 : quiesce_ctx->group = TAILQ_FIRST(&vu_ctrlr->transport->poll_groups);
# # # # #
# # # #
# ]
3368 : :
3369 [ # # # # ]: 246 : spdk_thread_send_msg(poll_group_to_thread(quiesce_ctx->group),
3370 : 0 : vfio_user_quiesce_pg, quiesce_ctx);
3371 : 0 : }
3372 : :
3373 : : static int
3374 : 2595 : vfio_user_dev_quiesce_cb(vfu_ctx_t *vfu_ctx)
3375 : : {
3376 : 2595 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3377 [ + - + - ]: 2595 : struct spdk_nvmf_subsystem *subsystem = endpoint->subsystem;
3378 [ + - + - ]: 2595 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3379 : :
3380 [ + + ]: 2595 : if (!vu_ctrlr) {
3381 : 0 : return 0;
3382 : : }
3383 : :
3384 : : /* NVMf library will destruct controller when no
3385 : : * connected queue pairs.
3386 : : */
3387 [ - + - + : 2595 : if (!nvmf_subsystem_get_ctrlr(subsystem, vu_ctrlr->cntlid)) {
+ - ]
3388 : 0 : return 0;
3389 : : }
3390 : :
3391 [ + + + + : 2595 : SPDK_DEBUGLOG(nvmf_vfio, "%s starts to quiesce\n", ctrlr_id(vu_ctrlr));
+ - ]
3392 : :
3393 : : /* There is no race condition here as device quiesce callback
3394 : : * and nvmf_prop_set_cc() are running in the same thread context.
3395 : : */
3396 [ + + + - : 2595 : if (!vu_ctrlr->ctrlr->vcprop.cc.bits.en) {
+ - + - +
- + - +
+ ]
3397 : 2181 : return 0;
3398 [ + + + - : 414 : } else if (!vu_ctrlr->ctrlr->vcprop.csts.bits.rdy) {
+ - + - +
- + - -
+ ]
3399 : 0 : return 0;
3400 [ + + - + : 414 : } else if (vu_ctrlr->ctrlr->vcprop.csts.bits.shst == SPDK_NVME_SHST_COMPLETE) {
- + - + -
+ - + +
- ]
3401 : 168 : return 0;
3402 : : }
3403 : :
3404 [ - + + - : 246 : switch (vu_ctrlr->state) {
# # # # ]
3405 : 0 : case VFIO_USER_CTRLR_PAUSED:
3406 : : case VFIO_USER_CTRLR_MIGRATING:
3407 : 0 : return 0;
3408 : 241 : case VFIO_USER_CTRLR_RUNNING:
3409 : 241 : ctrlr_quiesce(vu_ctrlr);
3410 : 241 : break;
3411 : 5 : case VFIO_USER_CTRLR_RESUMING:
3412 [ # # # # ]: 5 : vu_ctrlr->queued_quiesce = true;
3413 [ - + - + : 5 : SPDK_DEBUGLOG(nvmf_vfio, "%s is busy to quiesce, current state %u\n", ctrlr_id(vu_ctrlr),
# # # # #
# ]
3414 : : vu_ctrlr->state);
3415 : 5 : break;
3416 : 0 : default:
3417 [ # # # # : 0 : assert(vu_ctrlr->state != VFIO_USER_CTRLR_PAUSING);
# # # # ]
3418 : 0 : break;
3419 : : }
3420 : :
3421 [ # # ]: 246 : errno = EBUSY;
3422 : 246 : return -1;
3423 : 2008 : }
3424 : :
3425 : : static void
3426 : 0 : vfio_user_ctrlr_dump_migr_data(const char *name,
3427 : : struct vfio_user_nvme_migr_state *migr_data,
3428 : : struct nvmf_vfio_user_shadow_doorbells *sdbl)
3429 : : {
3430 : : struct spdk_nvmf_registers *regs;
3431 : : struct nvme_migr_sq_state *sq;
3432 : : struct nvme_migr_cq_state *cq;
3433 : : uint32_t *doorbell_base;
3434 : : uint32_t i;
3435 : :
3436 : 0 : SPDK_NOTICELOG("Dump %s\n", name);
3437 : :
3438 [ # # # # ]: 0 : regs = &migr_data->nvmf_data.regs;
3439 [ # # ]: 0 : doorbell_base = (uint32_t *)&migr_data->doorbells;
3440 : :
3441 : 0 : SPDK_NOTICELOG("Registers\n");
3442 [ # # # # : 0 : SPDK_NOTICELOG("CSTS 0x%x\n", regs->csts.raw);
# # ]
3443 [ # # # # : 0 : SPDK_NOTICELOG("CAP 0x%"PRIx64"\n", regs->cap.raw);
# # ]
3444 [ # # # # : 0 : SPDK_NOTICELOG("VS 0x%x\n", regs->vs.raw);
# # ]
3445 [ # # # # : 0 : SPDK_NOTICELOG("CC 0x%x\n", regs->cc.raw);
# # ]
3446 [ # # # # : 0 : SPDK_NOTICELOG("AQA 0x%x\n", regs->aqa.raw);
# # ]
3447 [ # # # # ]: 0 : SPDK_NOTICELOG("ASQ 0x%"PRIx64"\n", regs->asq);
3448 [ # # # # ]: 0 : SPDK_NOTICELOG("ACQ 0x%"PRIx64"\n", regs->acq);
3449 : :
3450 [ # # # # : 0 : SPDK_NOTICELOG("Number of IO Queues %u\n", migr_data->ctrlr_header.num_io_queues);
# # ]
3451 : :
3452 [ # # ]: 0 : if (sdbl != NULL) {
3453 [ # # # # : 0 : SPDK_NOTICELOG("shadow doorbell buffer=%#lx\n",
# # ]
3454 : : migr_data->ctrlr_header.shadow_doorbell_buffer);
3455 [ # # # # : 0 : SPDK_NOTICELOG("eventidx buffer=%#lx\n",
# # ]
3456 : : migr_data->ctrlr_header.eventidx_buffer);
3457 : 0 : }
3458 : :
3459 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
3460 [ # # # # : 0 : sq = &migr_data->qps[i].sq;
# # # # ]
3461 [ # # # # : 0 : cq = &migr_data->qps[i].cq;
# # # # ]
3462 : :
3463 [ # # # # : 0 : if (sq->size) {
# # ]
3464 [ # # # # : 0 : SPDK_NOTICELOG("sqid:%u, bar0_doorbell:%u\n", sq->sqid, doorbell_base[i * 2]);
# # # # ]
3465 [ # # # # ]: 0 : if (i > 0 && sdbl != NULL) {
3466 [ # # # # : 0 : SPDK_NOTICELOG("sqid:%u, shadow_doorbell:%u, eventidx:%u\n",
# # # # #
# # # # #
# # ]
3467 : : sq->sqid,
3468 : : sdbl->shadow_doorbells[queue_index(i, false)],
3469 : : sdbl->eventidxs[queue_index(i, false)]);
3470 : 0 : }
3471 [ # # # # : 0 : SPDK_NOTICELOG("SQ sqid:%u, cqid:%u, sqhead:%u, size:%u, dma_addr:0x%"PRIx64"\n",
# # # # #
# # # # #
# # # # #
# ]
3472 : : sq->sqid, sq->cqid, sq->head, sq->size, sq->dma_addr);
3473 : 0 : }
3474 : :
3475 [ # # # # : 0 : if (cq->size) {
# # ]
3476 [ # # # # : 0 : SPDK_NOTICELOG("cqid:%u, bar0_doorbell:%u\n", cq->cqid, doorbell_base[i * 2 + 1]);
# # # # ]
3477 [ # # # # ]: 0 : if (i > 0 && sdbl != NULL) {
3478 [ # # # # : 0 : SPDK_NOTICELOG("cqid:%u, shadow_doorbell:%u, eventidx:%u\n",
# # # # #
# # # # #
# # ]
3479 : : cq->cqid,
3480 : : sdbl->shadow_doorbells[queue_index(i, true)],
3481 : : sdbl->eventidxs[queue_index(i, true)]);
3482 : 0 : }
3483 [ # # # # : 0 : SPDK_NOTICELOG("CQ cqid:%u, phase:%u, cqtail:%u, size:%u, iv:%u, ien:%u, dma_addr:0x%"PRIx64"\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
3484 : : cq->cqid, cq->phase, cq->tail, cq->size, cq->iv, cq->ien, cq->dma_addr);
3485 : 0 : }
3486 : 0 : }
3487 : :
3488 : 0 : SPDK_NOTICELOG("%s Dump Done\n", name);
3489 : 0 : }
3490 : :
3491 : : /* Read region 9 content and restore it to migration data structures */
3492 : : static int
3493 : 0 : vfio_user_migr_stream_to_data(struct nvmf_vfio_user_endpoint *endpoint,
3494 : : struct vfio_user_nvme_migr_state *migr_state)
3495 : : {
3496 [ # # # # ]: 0 : void *data_ptr = endpoint->migr_data;
3497 : :
3498 : : /* Load vfio_user_nvme_migr_header first */
3499 [ # # # # : 0 : memcpy(&migr_state->ctrlr_header, data_ptr, sizeof(struct vfio_user_nvme_migr_header));
# # ]
3500 : : /* TODO: version check */
3501 [ # # # # : 0 : if (migr_state->ctrlr_header.magic != VFIO_USER_NVME_MIGR_MAGIC) {
# # # # ]
3502 [ # # # # : 0 : SPDK_ERRLOG("%s: bad magic number %x\n", endpoint_id(endpoint), migr_state->ctrlr_header.magic);
# # ]
3503 : 0 : return -EINVAL;
3504 : : }
3505 : :
3506 : : /* Load nvmf controller data */
3507 [ # # # # : 0 : data_ptr = endpoint->migr_data + migr_state->ctrlr_header.nvmf_data_offset;
# # # # #
# ]
3508 [ # # # # : 0 : memcpy(&migr_state->nvmf_data, data_ptr, migr_state->ctrlr_header.nvmf_data_len);
# # # # #
# # # ]
3509 : :
3510 : : /* Load queue pairs */
3511 [ # # # # : 0 : data_ptr = endpoint->migr_data + migr_state->ctrlr_header.qp_offset;
# # # # #
# ]
3512 [ # # # # : 0 : memcpy(&migr_state->qps, data_ptr, migr_state->ctrlr_header.qp_len);
# # # # #
# # # ]
3513 : :
3514 : : /* Load doorbells */
3515 [ # # # # : 0 : data_ptr = endpoint->migr_data + migr_state->ctrlr_header.bar_offset[VFU_PCI_DEV_BAR0_REGION_IDX];
# # # # #
# # # #
# ]
3516 [ # # # # : 0 : memcpy(&migr_state->doorbells, data_ptr,
# # ]
3517 [ # # # # : 0 : migr_state->ctrlr_header.bar_len[VFU_PCI_DEV_BAR0_REGION_IDX]);
# # # # #
# ]
3518 : :
3519 : : /* Load CFG */
3520 [ # # # # : 0 : data_ptr = endpoint->migr_data + migr_state->ctrlr_header.bar_offset[VFU_PCI_DEV_CFG_REGION_IDX];
# # # # #
# # # #
# ]
3521 [ # # # # : 0 : memcpy(&migr_state->cfg, data_ptr, migr_state->ctrlr_header.bar_len[VFU_PCI_DEV_CFG_REGION_IDX]);
# # # # #
# # # # #
# # ]
3522 : :
3523 : 0 : return 0;
3524 : 0 : }
3525 : :
3526 : :
3527 : : static void
3528 : 0 : vfio_user_migr_ctrlr_save_data(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
3529 : : {
3530 [ # # # # ]: 0 : struct spdk_nvmf_ctrlr *ctrlr = vu_ctrlr->ctrlr;
3531 [ # # # # ]: 0 : struct nvmf_vfio_user_endpoint *endpoint = vu_ctrlr->endpoint;
3532 : : struct nvmf_vfio_user_sq *sq;
3533 : : struct nvmf_vfio_user_cq *cq;
3534 : : uint64_t data_offset;
3535 : : void *data_ptr;
3536 : : uint32_t *doorbell_base;
3537 : 0 : uint32_t i = 0;
3538 : : uint16_t sqid, cqid;
3539 : 0 : struct vfio_user_nvme_migr_state migr_state = {
3540 : : .nvmf_data = {
3541 : : .data_size = offsetof(struct spdk_nvmf_ctrlr_migr_data, unused),
3542 : : .regs_size = sizeof(struct spdk_nvmf_registers),
3543 : : .feat_size = sizeof(struct spdk_nvmf_ctrlr_feat)
3544 : : }
3545 : : };
3546 : :
3547 : : /* Save all data to vfio_user_nvme_migr_state first, then we will
3548 : : * copy it to device migration region at last.
3549 : : */
3550 : :
3551 : : /* save magic number */
3552 : 0 : migr_state.ctrlr_header.magic = VFIO_USER_NVME_MIGR_MAGIC;
3553 : :
3554 : : /* save controller data */
3555 : 0 : spdk_nvmf_ctrlr_save_migr_data(ctrlr, &migr_state.nvmf_data);
3556 : :
3557 : : /* save connected queue pairs */
3558 [ # # # # : 0 : TAILQ_FOREACH(sq, &vu_ctrlr->connected_sqs, tailq) {
# # # # #
# # # #
# ]
3559 : : /* save sq */
3560 [ # # # # ]: 0 : sqid = sq->qid;
3561 [ # # # # : 0 : migr_state.qps[sqid].sq.sqid = sq->qid;
# # # # #
# # # #
# ]
3562 [ # # # # : 0 : migr_state.qps[sqid].sq.cqid = sq->cqid;
# # # # #
# # # #
# ]
3563 [ # # # # : 0 : migr_state.qps[sqid].sq.head = *sq_headp(sq);
# # # # #
# ]
3564 [ # # # # : 0 : migr_state.qps[sqid].sq.size = sq->size;
# # # # #
# # # #
# ]
3565 [ # # # # : 0 : migr_state.qps[sqid].sq.dma_addr = sq->mapping.prp1;
# # # # #
# # # # #
# # ]
3566 : :
3567 : : /* save cq, for shared cq case, cq may be saved multiple times */
3568 [ # # # # ]: 0 : cqid = sq->cqid;
3569 [ # # # # : 0 : cq = vu_ctrlr->cqs[cqid];
# # # # ]
3570 [ # # # # : 0 : migr_state.qps[cqid].cq.cqid = cqid;
# # # # #
# ]
3571 [ # # # # : 0 : migr_state.qps[cqid].cq.tail = *cq_tailp(cq);
# # # # #
# ]
3572 [ # # # # : 0 : migr_state.qps[cqid].cq.ien = cq->ien;
# # # # #
# # # # #
# # ]
3573 [ # # # # : 0 : migr_state.qps[cqid].cq.iv = cq->iv;
# # # # #
# # # #
# ]
3574 [ # # # # : 0 : migr_state.qps[cqid].cq.size = cq->size;
# # # # #
# # # #
# ]
3575 [ # # # # : 0 : migr_state.qps[cqid].cq.phase = cq->phase;
# # # # #
# # # # #
# # ]
3576 [ # # # # : 0 : migr_state.qps[cqid].cq.dma_addr = cq->mapping.prp1;
# # # # #
# # # # #
# # ]
3577 : 0 : i++;
3578 : 0 : }
3579 : :
3580 [ # # # # ]: 0 : assert(i > 0);
3581 [ # # ]: 0 : migr_state.ctrlr_header.num_io_queues = i - 1;
3582 : :
3583 : : /* Save doorbells */
3584 : 0 : doorbell_base = (uint32_t *)&migr_state.doorbells;
3585 [ # # # # : 0 : memcpy(doorbell_base, (void *)vu_ctrlr->bar0_doorbells, NVMF_VFIO_USER_DOORBELLS_SIZE);
# # # # ]
3586 : :
3587 : : /* Save PCI configuration space */
3588 [ # # # # : 0 : memcpy(&migr_state.cfg, (void *)endpoint->pci_config_space, NVME_REG_CFG_SIZE);
# # # # ]
3589 : :
3590 : : /* Save all data to device migration region */
3591 [ # # # # ]: 0 : data_ptr = endpoint->migr_data;
3592 : :
3593 : : /* Copy nvmf controller data */
3594 : 0 : data_offset = sizeof(struct vfio_user_nvme_migr_header);
3595 : 0 : data_ptr += data_offset;
3596 [ # # ]: 0 : migr_state.ctrlr_header.nvmf_data_offset = data_offset;
3597 [ # # ]: 0 : migr_state.ctrlr_header.nvmf_data_len = sizeof(struct spdk_nvmf_ctrlr_migr_data);
3598 [ # # # # ]: 0 : memcpy(data_ptr, &migr_state.nvmf_data, sizeof(struct spdk_nvmf_ctrlr_migr_data));
3599 : :
3600 : : /* Copy queue pairs */
3601 : 0 : data_offset += sizeof(struct spdk_nvmf_ctrlr_migr_data);
3602 : 0 : data_ptr += sizeof(struct spdk_nvmf_ctrlr_migr_data);
3603 [ # # ]: 0 : migr_state.ctrlr_header.qp_offset = data_offset;
3604 [ # # ]: 0 : migr_state.ctrlr_header.qp_len = i * (sizeof(struct nvme_migr_sq_state) + sizeof(
3605 : : struct nvme_migr_cq_state));
3606 [ # # # # : 0 : memcpy(data_ptr, &migr_state.qps, migr_state.ctrlr_header.qp_len);
# # ]
3607 : :
3608 : : /* Copy doorbells */
3609 [ # # ]: 0 : data_offset += migr_state.ctrlr_header.qp_len;
3610 [ # # ]: 0 : data_ptr += migr_state.ctrlr_header.qp_len;
3611 [ # # # # : 0 : migr_state.ctrlr_header.bar_offset[VFU_PCI_DEV_BAR0_REGION_IDX] = data_offset;
# # ]
3612 [ # # # # : 0 : migr_state.ctrlr_header.bar_len[VFU_PCI_DEV_BAR0_REGION_IDX] = NVMF_VFIO_USER_DOORBELLS_SIZE;
# # ]
3613 [ # # # # ]: 0 : memcpy(data_ptr, &migr_state.doorbells, NVMF_VFIO_USER_DOORBELLS_SIZE);
3614 : :
3615 : : /* Copy CFG */
3616 : 0 : data_offset += NVMF_VFIO_USER_DOORBELLS_SIZE;
3617 : 0 : data_ptr += NVMF_VFIO_USER_DOORBELLS_SIZE;
3618 [ # # # # : 0 : migr_state.ctrlr_header.bar_offset[VFU_PCI_DEV_CFG_REGION_IDX] = data_offset;
# # ]
3619 [ # # # # : 0 : migr_state.ctrlr_header.bar_len[VFU_PCI_DEV_CFG_REGION_IDX] = NVME_REG_CFG_SIZE;
# # ]
3620 [ # # # # ]: 0 : memcpy(data_ptr, &migr_state.cfg, NVME_REG_CFG_SIZE);
3621 : :
3622 : : /* copy shadow doorbells */
3623 [ # # # # : 0 : if (vu_ctrlr->sdbl != NULL) {
# # ]
3624 [ # # ]: 0 : migr_state.ctrlr_header.sdbl = true;
3625 [ # # # # : 0 : migr_state.ctrlr_header.shadow_doorbell_buffer = vu_ctrlr->shadow_doorbell_buffer;
# # ]
3626 [ # # # # : 0 : migr_state.ctrlr_header.eventidx_buffer = vu_ctrlr->eventidx_buffer;
# # ]
3627 : 0 : }
3628 : :
3629 : : /* Copy nvme migration header finally */
3630 [ # # # # : 0 : memcpy(endpoint->migr_data, &migr_state.ctrlr_header, sizeof(struct vfio_user_nvme_migr_header));
# # # # ]
3631 : :
3632 [ # # ]: 0 : if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf_vfio")) {
3633 [ # # # # ]: 0 : vfio_user_ctrlr_dump_migr_data("SAVE", &migr_state, vu_ctrlr->sdbl);
3634 : 0 : }
3635 : 0 : }
3636 : :
3637 : : /*
3638 : : * If we are about to close the connection, we need to unregister the interrupt,
3639 : : * as the library will subsequently close the file descriptor we registered.
3640 : : */
3641 : : static int
3642 : 162 : vfio_user_device_reset(vfu_ctx_t *vfu_ctx, vfu_reset_type_t type)
3643 : : {
3644 : 162 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3645 [ + - + - ]: 162 : struct nvmf_vfio_user_ctrlr *ctrlr = endpoint->ctrlr;
3646 : :
3647 [ + + + + : 162 : SPDK_DEBUGLOG(nvmf_vfio, "Device reset type %u\n", type);
+ - ]
3648 : :
3649 [ + + ]: 162 : if (type == VFU_RESET_LOST_CONN) {
3650 [ + + ]: 150 : if (ctrlr != NULL) {
3651 [ + - ]: 119 : spdk_interrupt_unregister(&ctrlr->intr);
3652 [ + - + - ]: 119 : ctrlr->intr_fd = -1;
3653 : 80 : }
3654 : 150 : return 0;
3655 : : }
3656 : :
3657 : : /* FIXME: LOST_CONN case ? */
3658 [ - + # # : 12 : if (ctrlr->sdbl != NULL) {
# # ]
3659 : 0 : vfio_user_ctrlr_switch_doorbells(ctrlr, false);
3660 [ # # # # ]: 0 : free_sdbl(vfu_ctx, ctrlr->sdbl);
3661 [ # # # # ]: 0 : ctrlr->sdbl = NULL;
3662 : 0 : }
3663 : :
3664 : : /* FIXME: much more needed here. */
3665 : :
3666 : 12 : return 0;
3667 : 94 : }
3668 : :
3669 : : static int
3670 : 0 : vfio_user_migr_ctrlr_construct_qps(struct nvmf_vfio_user_ctrlr *vu_ctrlr,
3671 : : struct vfio_user_nvme_migr_state *migr_state)
3672 : : {
3673 : 0 : uint32_t i, qsize = 0;
3674 : : uint16_t sqid, cqid;
3675 : : struct vfio_user_nvme_migr_qp migr_qp;
3676 : : void *addr;
3677 : 0 : uint32_t cqs_ref[NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR] = {};
3678 : : int ret;
3679 : :
3680 [ # # ]: 0 : if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf_vfio")) {
3681 [ # # # # ]: 0 : vfio_user_ctrlr_dump_migr_data("RESUME", migr_state, vu_ctrlr->sdbl);
3682 : 0 : }
3683 : :
3684 : : /* restore submission queues */
3685 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
3686 [ # # # # : 0 : migr_qp = migr_state->qps[i];
# # ]
3687 : :
3688 [ # # ]: 0 : qsize = migr_qp.sq.size;
3689 [ # # ]: 0 : if (qsize) {
3690 : : struct nvmf_vfio_user_sq *sq;
3691 : :
3692 : 0 : sqid = migr_qp.sq.sqid;
3693 [ # # ]: 0 : if (sqid != i) {
3694 : 0 : SPDK_ERRLOG("Expected sqid %u while got %u", i, sqid);
3695 : 0 : return -EINVAL;
3696 : : }
3697 : :
3698 : : /* allocate sq if necessary */
3699 [ # # # # : 0 : if (vu_ctrlr->sqs[sqid] == NULL) {
# # # # #
# ]
3700 [ # # # # : 0 : ret = init_sq(vu_ctrlr, &vu_ctrlr->transport->transport, sqid);
# # ]
3701 [ # # ]: 0 : if (ret) {
3702 : 0 : SPDK_ERRLOG("Construct qpair with qid %u failed\n", sqid);
3703 : 0 : return -EFAULT;
3704 : : }
3705 : 0 : }
3706 : :
3707 [ # # # # : 0 : sq = vu_ctrlr->sqs[sqid];
# # # # ]
3708 [ # # # # ]: 0 : sq->size = qsize;
3709 : :
3710 : 0 : ret = alloc_sq_reqs(vu_ctrlr, sq);
3711 [ # # ]: 0 : if (ret) {
3712 : 0 : SPDK_ERRLOG("Construct sq with qid %u failed\n", sqid);
3713 : 0 : return -EFAULT;
3714 : : }
3715 : :
3716 : : /* restore sq */
3717 [ # # # # ]: 0 : sq->sq_state = VFIO_USER_SQ_CREATED;
3718 [ # # # # : 0 : sq->cqid = migr_qp.sq.cqid;
# # ]
3719 [ # # ]: 0 : *sq_headp(sq) = migr_qp.sq.head;
3720 [ # # # # : 0 : sq->mapping.prp1 = migr_qp.sq.dma_addr;
# # # # ]
3721 [ # # # # : 0 : sq->mapping.len = sq->size * sizeof(struct spdk_nvme_cmd);
# # # # #
# ]
3722 [ # # # # : 0 : addr = map_one(vu_ctrlr->endpoint->vfu_ctx,
# # # # ]
3723 [ # # # # : 0 : sq->mapping.prp1, sq->mapping.len,
# # # # #
# # # ]
3724 [ # # # # : 0 : sq->mapping.sg, &sq->mapping.iov,
# # # # #
# ]
3725 : : PROT_READ);
3726 [ # # ]: 0 : if (addr == NULL) {
3727 [ # # # # : 0 : SPDK_ERRLOG("Restore sq with qid %u PRP1 0x%"PRIx64" with size %u failed\n",
# # # # #
# ]
3728 : : sqid, sq->mapping.prp1, sq->size);
3729 : 0 : return -EFAULT;
3730 : : }
3731 [ # # # # : 0 : cqs_ref[sq->cqid]++;
# # # # ]
3732 : 0 : }
3733 : 0 : }
3734 : :
3735 : : /* restore completion queues */
3736 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
3737 [ # # # # : 0 : migr_qp = migr_state->qps[i];
# # ]
3738 : :
3739 [ # # # # ]: 0 : qsize = migr_qp.cq.size;
3740 [ # # ]: 0 : if (qsize) {
3741 : : struct nvmf_vfio_user_cq *cq;
3742 : :
3743 : : /* restore cq */
3744 [ # # ]: 0 : cqid = migr_qp.sq.cqid;
3745 [ # # # # ]: 0 : assert(cqid == i);
3746 : :
3747 : : /* allocate cq if necessary */
3748 [ # # # # : 0 : if (vu_ctrlr->cqs[cqid] == NULL) {
# # # # #
# ]
3749 : 0 : ret = init_cq(vu_ctrlr, cqid);
3750 [ # # ]: 0 : if (ret) {
3751 : 0 : SPDK_ERRLOG("Construct qpair with qid %u failed\n", cqid);
3752 : 0 : return -EFAULT;
3753 : : }
3754 : 0 : }
3755 : :
3756 [ # # # # : 0 : cq = vu_ctrlr->cqs[cqid];
# # # # ]
3757 : :
3758 [ # # # # ]: 0 : cq->size = qsize;
3759 : :
3760 [ # # # # ]: 0 : cq->cq_state = VFIO_USER_CQ_CREATED;
3761 [ # # # # : 0 : cq->cq_ref = cqs_ref[cqid];
# # # # #
# ]
3762 [ # # # # ]: 0 : *cq_tailp(cq) = migr_qp.cq.tail;
3763 [ # # # # : 0 : cq->mapping.prp1 = migr_qp.cq.dma_addr;
# # # # #
# ]
3764 [ # # # # : 0 : cq->mapping.len = cq->size * sizeof(struct spdk_nvme_cpl);
# # # # #
# ]
3765 [ # # # # : 0 : cq->ien = migr_qp.cq.ien;
# # # # ]
3766 [ # # # # : 0 : cq->iv = migr_qp.cq.iv;
# # # # ]
3767 [ # # # # : 0 : cq->phase = migr_qp.cq.phase;
# # # # ]
3768 [ # # # # : 0 : addr = map_one(vu_ctrlr->endpoint->vfu_ctx,
# # # # ]
3769 [ # # # # : 0 : cq->mapping.prp1, cq->mapping.len,
# # # # #
# # # ]
3770 [ # # # # : 0 : cq->mapping.sg, &cq->mapping.iov,
# # # # #
# ]
3771 : : PROT_READ | PROT_WRITE);
3772 [ # # ]: 0 : if (addr == NULL) {
3773 [ # # # # : 0 : SPDK_ERRLOG("Restore cq with qid %u PRP1 0x%"PRIx64" with size %u failed\n",
# # # # #
# ]
3774 : : cqid, cq->mapping.prp1, cq->size);
3775 : 0 : return -EFAULT;
3776 : : }
3777 : 0 : }
3778 : 0 : }
3779 : :
3780 : 0 : return 0;
3781 : 0 : }
3782 : :
3783 : : static int
3784 : 0 : vfio_user_migr_ctrlr_restore(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
3785 : : {
3786 [ # # # # ]: 0 : struct nvmf_vfio_user_endpoint *endpoint = vu_ctrlr->endpoint;
3787 [ # # # # ]: 0 : struct spdk_nvmf_ctrlr *ctrlr = vu_ctrlr->ctrlr;
3788 : : uint32_t *doorbell_base;
3789 : 0 : struct spdk_nvme_cmd cmd;
3790 : : uint16_t i;
3791 : 0 : int rc = 0;
3792 : 0 : struct vfio_user_nvme_migr_state migr_state = {
3793 : : .nvmf_data = {
3794 : : .data_size = offsetof(struct spdk_nvmf_ctrlr_migr_data, unused),
3795 : : .regs_size = sizeof(struct spdk_nvmf_registers),
3796 : : .feat_size = sizeof(struct spdk_nvmf_ctrlr_feat)
3797 : : }
3798 : : };
3799 : :
3800 [ # # # # : 0 : assert(endpoint->migr_data != NULL);
# # # # ]
3801 [ # # # # ]: 0 : assert(ctrlr != NULL);
3802 : 0 : rc = vfio_user_migr_stream_to_data(endpoint, &migr_state);
3803 [ # # ]: 0 : if (rc) {
3804 : 0 : return rc;
3805 : : }
3806 : :
3807 : : /* restore shadow doorbells */
3808 [ # # # # ]: 0 : if (migr_state.ctrlr_header.sdbl) {
3809 : : struct nvmf_vfio_user_shadow_doorbells *sdbl;
3810 [ # # # # : 0 : sdbl = map_sdbl(vu_ctrlr->endpoint->vfu_ctx,
# # # # ]
3811 [ # # ]: 0 : migr_state.ctrlr_header.shadow_doorbell_buffer,
3812 [ # # ]: 0 : migr_state.ctrlr_header.eventidx_buffer,
3813 : 0 : memory_page_size(vu_ctrlr));
3814 [ # # ]: 0 : if (sdbl == NULL) {
3815 : 0 : SPDK_ERRLOG("%s: failed to re-map shadow doorbell buffers\n",
3816 : : ctrlr_id(vu_ctrlr));
3817 : 0 : return -1;
3818 : : }
3819 : :
3820 [ # # # # : 0 : vu_ctrlr->shadow_doorbell_buffer = migr_state.ctrlr_header.shadow_doorbell_buffer;
# # ]
3821 [ # # # # : 0 : vu_ctrlr->eventidx_buffer = migr_state.ctrlr_header.eventidx_buffer;
# # ]
3822 : :
3823 [ # # # # : 0 : SWAP(vu_ctrlr->sdbl, sdbl);
# # # # ]
3824 : 0 : }
3825 : :
3826 : 0 : rc = vfio_user_migr_ctrlr_construct_qps(vu_ctrlr, &migr_state);
3827 [ # # ]: 0 : if (rc) {
3828 : 0 : return rc;
3829 : : }
3830 : :
3831 : : /* restore PCI configuration space */
3832 [ # # # # : 0 : memcpy((void *)endpoint->pci_config_space, &migr_state.cfg, NVME_REG_CFG_SIZE);
# # # # ]
3833 : :
3834 : 0 : doorbell_base = (uint32_t *)&migr_state.doorbells;
3835 : : /* restore doorbells from saved registers */
3836 [ # # # # : 0 : memcpy((void *)vu_ctrlr->bar0_doorbells, doorbell_base, NVMF_VFIO_USER_DOORBELLS_SIZE);
# # # # ]
3837 : :
3838 : : /* restore nvmf controller data */
3839 : 0 : rc = spdk_nvmf_ctrlr_restore_migr_data(ctrlr, &migr_state.nvmf_data);
3840 [ # # ]: 0 : if (rc) {
3841 : 0 : return rc;
3842 : : }
3843 : :
3844 : : /* resubmit pending AERs */
3845 [ # # # # : 0 : for (i = 0; i < migr_state.nvmf_data.num_aer_cids; i++) {
# # ]
3846 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s AER resubmit, CID %u\n", ctrlr_id(vu_ctrlr),
# # # # #
# # # #
# ]
3847 : : migr_state.nvmf_data.aer_cids[i]);
3848 [ # # ]: 0 : memset(&cmd, 0, sizeof(cmd));
3849 : 0 : cmd.opc = SPDK_NVME_OPC_ASYNC_EVENT_REQUEST;
3850 [ # # # # : 0 : cmd.cid = migr_state.nvmf_data.aer_cids[i];
# # # # #
# ]
3851 [ # # # # : 0 : rc = handle_cmd_req(vu_ctrlr, &cmd, vu_ctrlr->sqs[0]);
# # # # ]
3852 [ # # ]: 0 : if (spdk_unlikely(rc)) {
3853 : 0 : break;
3854 : : }
3855 : 0 : }
3856 : :
3857 : 0 : return rc;
3858 : 0 : }
3859 : :
3860 : : static void
3861 : 0 : vfio_user_migr_ctrlr_enable_sqs(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
3862 : : {
3863 : : uint32_t i;
3864 : : struct nvmf_vfio_user_sq *sq;
3865 : :
3866 : : /* The Admin queue (qid: 0) does not ever use shadow doorbells. */
3867 : :
3868 [ # # # # : 0 : if (vu_ctrlr->sqs[0] != NULL) {
# # # # #
# ]
3869 [ # # # # : 0 : vu_ctrlr->sqs[0]->dbl_tailp = vu_ctrlr->bar0_doorbells +
# # # # #
# # # # #
# # # # #
# ]
3870 : 0 : queue_index(0, false);
3871 : 0 : }
3872 : :
3873 [ # # # # : 0 : if (vu_ctrlr->cqs[0] != NULL) {
# # # # #
# ]
3874 [ # # # # : 0 : vu_ctrlr->cqs[0]->dbl_headp = vu_ctrlr->bar0_doorbells +
# # # # #
# # # # #
# # # # #
# ]
3875 : 0 : queue_index(0, true);
3876 : 0 : }
3877 : :
3878 [ # # # # ]: 0 : vfio_user_ctrlr_switch_doorbells(vu_ctrlr, vu_ctrlr->sdbl != NULL);
3879 : :
3880 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
3881 [ # # # # : 0 : sq = vu_ctrlr->sqs[i];
# # # # ]
3882 [ # # # # : 0 : if (!sq || !sq->size) {
# # # # ]
3883 : 0 : continue;
3884 : : }
3885 : :
3886 [ # # # # ]: 0 : if (nvmf_qpair_is_admin_queue(&sq->qpair)) {
3887 : : /* ADMIN queue pair is always in the poll group, just enable it */
3888 [ # # # # ]: 0 : sq->sq_state = VFIO_USER_SQ_ACTIVE;
3889 : 0 : } else {
3890 [ # # # # : 0 : spdk_nvmf_tgt_new_qpair(vu_ctrlr->transport->transport.tgt, &sq->qpair);
# # # # #
# # # ]
3891 : : }
3892 : 0 : }
3893 : 0 : }
3894 : :
3895 : : /*
3896 : : * We are in stop-and-copy state, but still potentially have some current dirty
3897 : : * sgls: while we're quiesced and thus should have no active requests, we still
3898 : : * have potentially dirty maps of the shadow doorbells and the CQs (SQs are
3899 : : * mapped read only).
3900 : : *
3901 : : * Since we won't be calling vfu_sgl_put() for them, we need to explicitly
3902 : : * mark them dirty now.
3903 : : */
3904 : : static void
3905 : 0 : vfio_user_migr_ctrlr_mark_dirty(struct nvmf_vfio_user_ctrlr *vu_ctrlr)
3906 : : {
3907 [ # # # # ]: 0 : struct nvmf_vfio_user_endpoint *endpoint = vu_ctrlr->endpoint;
3908 : :
3909 [ # # # # : 0 : assert(vu_ctrlr->state == VFIO_USER_CTRLR_MIGRATING);
# # # # ]
3910 : :
3911 [ # # ]: 0 : for (size_t i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
3912 [ # # # # : 0 : struct nvmf_vfio_user_cq *cq = vu_ctrlr->cqs[i];
# # # # ]
3913 : :
3914 [ # # # # : 0 : if (cq == NULL || q_addr(&cq->mapping) == NULL) {
# # ]
3915 : 0 : continue;
3916 : : }
3917 : :
3918 [ # # # # : 0 : vfu_sgl_mark_dirty(endpoint->vfu_ctx, cq->mapping.sg, 1);
# # # # #
# ]
3919 : 0 : }
3920 : :
3921 [ # # # # : 0 : if (vu_ctrlr->sdbl != NULL) {
# # ]
3922 : : dma_sg_t *sg;
3923 : : size_t i;
3924 : :
3925 [ # # ]: 0 : for (i = 0; i < NVMF_VFIO_USER_SHADOW_DOORBELLS_BUFFER_COUNT;
3926 : 0 : ++i) {
3927 : :
3928 [ # # # # : 0 : if (!vu_ctrlr->sdbl->iovs[i].iov_len) {
# # # # #
# # # # #
# # ]
3929 : 0 : continue;
3930 : : }
3931 : :
3932 [ # # # # : 0 : sg = index_to_sg_t(vu_ctrlr->sdbl->sgs, i);
# # # # ]
3933 : :
3934 [ # # # # ]: 0 : vfu_sgl_mark_dirty(endpoint->vfu_ctx, sg, 1);
3935 : 0 : }
3936 : 0 : }
3937 : 0 : }
3938 : :
3939 : : static int
3940 : 0 : vfio_user_migration_device_state_transition(vfu_ctx_t *vfu_ctx, vfu_migr_state_t state)
3941 : : {
3942 : 0 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
3943 [ # # # # ]: 0 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = endpoint->ctrlr;
3944 : : struct nvmf_vfio_user_sq *sq;
3945 : 0 : int ret = 0;
3946 : :
3947 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s controller state %u, migration state %u\n", endpoint_id(endpoint),
# # # # #
# ]
3948 : : vu_ctrlr->state, state);
3949 : :
3950 [ # # # # : 0 : switch (state) {
# # ]
3951 : 0 : case VFU_MIGR_STATE_STOP_AND_COPY:
3952 [ # # # # ]: 0 : vu_ctrlr->in_source_vm = true;
3953 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_MIGRATING;
3954 : 0 : vfio_user_migr_ctrlr_mark_dirty(vu_ctrlr);
3955 : 0 : vfio_user_migr_ctrlr_save_data(vu_ctrlr);
3956 : 0 : break;
3957 : 0 : case VFU_MIGR_STATE_STOP:
3958 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_MIGRATING;
3959 : : /* The controller associates with source VM is dead now, we will resume
3960 : : * the subsystem after destroying the controller data structure, then the
3961 : : * subsystem can be re-used for another new client.
3962 : : */
3963 [ # # # # : 0 : if (vu_ctrlr->in_source_vm) {
# # # # ]
3964 [ # # # # ]: 0 : endpoint->need_resume = true;
3965 : 0 : }
3966 : 0 : break;
3967 : 0 : case VFU_MIGR_STATE_PRE_COPY:
3968 [ # # # # : 0 : assert(vu_ctrlr->state == VFIO_USER_CTRLR_PAUSED);
# # # # ]
3969 : 0 : break;
3970 : 0 : case VFU_MIGR_STATE_RESUME:
3971 : : /*
3972 : : * Destination ADMIN queue pair is connected when starting the VM,
3973 : : * but the ADMIN queue pair isn't enabled in destination VM, the poll
3974 : : * group will do nothing to ADMIN queue pair for now.
3975 : : */
3976 [ # # # # : 0 : if (vu_ctrlr->state != VFIO_USER_CTRLR_RUNNING) {
# # ]
3977 : 0 : break;
3978 : : }
3979 : :
3980 [ # # # # : 0 : assert(!vu_ctrlr->in_source_vm);
# # # # #
# ]
3981 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_MIGRATING;
3982 : :
3983 [ # # # # : 0 : sq = TAILQ_FIRST(&vu_ctrlr->connected_sqs);
# # ]
3984 [ # # # # ]: 0 : assert(sq != NULL);
3985 [ # # # # : 0 : assert(sq->qpair.qid == 0);
# # # # #
# ]
3986 [ # # # # ]: 0 : sq->sq_state = VFIO_USER_SQ_INACTIVE;
3987 : :
3988 : : /* Free ADMIN SQ resources first, SQ resources will be
3989 : : * allocated based on queue size from source VM.
3990 : : */
3991 : 0 : free_sq_reqs(sq);
3992 [ # # # # ]: 0 : sq->size = 0;
3993 : 0 : break;
3994 : 0 : case VFU_MIGR_STATE_RUNNING:
3995 : :
3996 [ # # # # : 0 : if (vu_ctrlr->state != VFIO_USER_CTRLR_MIGRATING) {
# # ]
3997 : 0 : break;
3998 : : }
3999 : :
4000 [ # # # # : 0 : if (!vu_ctrlr->in_source_vm) {
# # # # ]
4001 : : /* Restore destination VM from BAR9 */
4002 : 0 : ret = vfio_user_migr_ctrlr_restore(vu_ctrlr);
4003 [ # # ]: 0 : if (ret) {
4004 : 0 : break;
4005 : : }
4006 : :
4007 : 0 : vfio_user_ctrlr_switch_doorbells(vu_ctrlr, false);
4008 : 0 : vfio_user_migr_ctrlr_enable_sqs(vu_ctrlr);
4009 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_RUNNING;
4010 : : /* FIXME where do we resume nvmf? */
4011 : 0 : } else {
4012 : : /* Rollback source VM */
4013 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_RESUMING;
4014 [ # # # # ]: 0 : ret = spdk_nvmf_subsystem_resume((struct spdk_nvmf_subsystem *)endpoint->subsystem,
4015 : 0 : vfio_user_endpoint_resume_done, endpoint);
4016 [ # # ]: 0 : if (ret < 0) {
4017 : : /* TODO: fail controller with CFS bit set */
4018 [ # # # # ]: 0 : vu_ctrlr->state = VFIO_USER_CTRLR_PAUSED;
4019 : 0 : SPDK_ERRLOG("%s: failed to resume, ret=%d\n", endpoint_id(endpoint), ret);
4020 : 0 : }
4021 : : }
4022 [ # # # # ]: 0 : vu_ctrlr->migr_data_prepared = false;
4023 [ # # # # ]: 0 : vu_ctrlr->in_source_vm = false;
4024 : 0 : break;
4025 : :
4026 : 0 : default:
4027 : 0 : return -EINVAL;
4028 : : }
4029 : :
4030 : 0 : return ret;
4031 : 0 : }
4032 : :
4033 : : static uint64_t
4034 : 0 : vfio_user_migration_get_pending_bytes(vfu_ctx_t *vfu_ctx)
4035 : : {
4036 : 0 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
4037 [ # # # # ]: 0 : struct nvmf_vfio_user_ctrlr *ctrlr = endpoint->ctrlr;
4038 : : uint64_t pending_bytes;
4039 : :
4040 [ # # # # : 0 : if (ctrlr->migr_data_prepared) {
# # # # ]
4041 [ # # # # : 0 : assert(ctrlr->state == VFIO_USER_CTRLR_MIGRATING);
# # # # ]
4042 : 0 : pending_bytes = 0;
4043 : 0 : } else {
4044 : 0 : pending_bytes = vfio_user_migr_data_len();
4045 : : }
4046 : :
4047 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio,
# # # # #
# ]
4048 : : "%s current state %u, pending bytes 0x%"PRIx64"\n",
4049 : : endpoint_id(endpoint), ctrlr->state, pending_bytes);
4050 : :
4051 : 0 : return pending_bytes;
4052 : : }
4053 : :
4054 : : static int
4055 : 0 : vfio_user_migration_prepare_data(vfu_ctx_t *vfu_ctx, uint64_t *offset, uint64_t *size)
4056 : : {
4057 : 0 : struct nvmf_vfio_user_endpoint *endpoint = vfu_get_private(vfu_ctx);
4058 [ # # # # ]: 0 : struct nvmf_vfio_user_ctrlr *ctrlr = endpoint->ctrlr;
4059 : :
4060 : : /*
4061 : : * When transitioning to pre-copy state we set pending_bytes to 0,
4062 : : * so the vfio-user client shouldn't attempt to read any migration
4063 : : * data. This is not yet guaranteed by libvfio-user.
4064 : : */
4065 [ # # # # : 0 : if (ctrlr->state != VFIO_USER_CTRLR_MIGRATING) {
# # ]
4066 [ # # # # ]: 0 : assert(size != NULL);
4067 [ # # ]: 0 : *offset = 0;
4068 [ # # ]: 0 : *size = 0;
4069 : 0 : return 0;
4070 : : }
4071 : :
4072 [ # # # # : 0 : if (ctrlr->in_source_vm) { /* migration source */
# # # # ]
4073 [ # # # # ]: 0 : assert(size != NULL);
4074 [ # # ]: 0 : *size = vfio_user_migr_data_len();
4075 : 0 : vfio_user_migr_ctrlr_save_data(ctrlr);
4076 : 0 : } else { /* migration destination */
4077 [ # # # # ]: 0 : assert(size == NULL);
4078 [ # # # # : 0 : assert(!ctrlr->migr_data_prepared);
# # # # #
# ]
4079 : : }
4080 [ # # ]: 0 : *offset = 0;
4081 [ # # # # ]: 0 : ctrlr->migr_data_prepared = true;
4082 : :
4083 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s current state %u\n", endpoint_id(endpoint), ctrlr->state);
# # # # #
# ]
4084 : :
4085 : 0 : return 0;
4086 : 0 : }
4087 : :
4088 : : static ssize_t
4089 : 0 : vfio_user_migration_read_data(vfu_ctx_t *vfu_ctx __attribute__((unused)),
4090 : : void *buf __attribute__((unused)),
4091 : : uint64_t count __attribute__((unused)),
4092 : : uint64_t offset __attribute__((unused)))
4093 : : {
4094 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: migration read data not supported\n",
# # ]
4095 : : endpoint_id(vfu_get_private(vfu_ctx)));
4096 [ # # ]: 0 : errno = ENOTSUP;
4097 : 0 : return -1;
4098 : : }
4099 : :
4100 : : static ssize_t
4101 : 0 : vfio_user_migration_write_data(vfu_ctx_t *vfu_ctx __attribute__((unused)),
4102 : : void *buf __attribute__((unused)),
4103 : : uint64_t count __attribute__((unused)),
4104 : : uint64_t offset __attribute__((unused)))
4105 : : {
4106 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: migration write data not supported\n",
# # ]
4107 : : endpoint_id(vfu_get_private(vfu_ctx)));
4108 [ # # ]: 0 : errno = ENOTSUP;
4109 : 0 : return -1;
4110 : : }
4111 : :
4112 : : static int
4113 : 0 : vfio_user_migration_data_written(vfu_ctx_t *vfu_ctx __attribute__((unused)),
4114 : : uint64_t count)
4115 : : {
4116 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "write 0x%"PRIx64"\n", (uint64_t)count);
# # ]
4117 : :
4118 [ # # ]: 0 : if (count != vfio_user_migr_data_len()) {
4119 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s bad count %#lx\n",
# # ]
4120 : : endpoint_id(vfu_get_private(vfu_ctx)), count);
4121 [ # # ]: 0 : errno = EINVAL;
4122 : 0 : return -1;
4123 : : }
4124 : :
4125 : 0 : return 0;
4126 : 0 : }
4127 : :
4128 : : static int
4129 : 31 : vfio_user_dev_info_fill(struct nvmf_vfio_user_transport *vu_transport,
4130 : : struct nvmf_vfio_user_endpoint *endpoint)
4131 : : {
4132 : : int ret;
4133 : : ssize_t cap_offset;
4134 [ + - + - ]: 31 : vfu_ctx_t *vfu_ctx = endpoint->vfu_ctx;
4135 : 31 : struct iovec migr_sparse_mmap = {};
4136 : :
4137 : 31 : struct pmcap pmcap = { .hdr.id = PCI_CAP_ID_PM, .pmcs.nsfrst = 0x1 };
4138 : 31 : struct pxcap pxcap = {
4139 : : .hdr.id = PCI_CAP_ID_EXP,
4140 : : .pxcaps.ver = 0x2,
4141 : : .pxdcap = {.rer = 0x1, .flrc = 0x1},
4142 : : .pxdcap2.ctds = 0x1
4143 : : };
4144 : :
4145 : 31 : struct msixcap msixcap = {
4146 : : .hdr.id = PCI_CAP_ID_MSIX,
4147 : : .mxc.ts = NVMF_VFIO_USER_MSIX_NUM - 1,
4148 : : .mtab = {.tbir = NVMF_VFIO_USER_MSIX_TABLE_BIR, .to = 0x0},
4149 : : .mpba = {.pbir = NVMF_VFIO_USER_MSIX_PBA_BIR, .pbao = 0x0}
4150 : : };
4151 : :
4152 : 31 : struct iovec sparse_mmap[] = {
4153 : : {
4154 : : .iov_base = (void *)NVME_DOORBELLS_OFFSET,
4155 : : .iov_len = NVMF_VFIO_USER_DOORBELLS_SIZE,
4156 : : },
4157 : : };
4158 : :
4159 : 31 : const vfu_migration_callbacks_t migr_callbacks = {
4160 : : .version = VFIO_USER_MIGR_CALLBACK_VERS,
4161 : : .transition = &vfio_user_migration_device_state_transition,
4162 : : .get_pending_bytes = &vfio_user_migration_get_pending_bytes,
4163 : : .prepare_data = &vfio_user_migration_prepare_data,
4164 : : .read_data = &vfio_user_migration_read_data,
4165 : : .data_written = &vfio_user_migration_data_written,
4166 : : .write_data = &vfio_user_migration_write_data
4167 : : };
4168 : :
4169 : 31 : ret = vfu_pci_init(vfu_ctx, VFU_PCI_TYPE_EXPRESS, PCI_HEADER_TYPE_NORMAL, 0);
4170 [ - + ]: 31 : if (ret < 0) {
4171 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to initialize PCI\n", vfu_ctx);
4172 : 0 : return ret;
4173 : : }
4174 : 31 : vfu_pci_set_id(vfu_ctx, SPDK_PCI_VID_NUTANIX, 0x0001, SPDK_PCI_VID_NUTANIX, 0);
4175 : : /*
4176 : : * 0x02, controller uses the NVM Express programming interface
4177 : : * 0x08, non-volatile memory controller
4178 : : * 0x01, mass storage controller
4179 : : */
4180 : 31 : vfu_pci_set_class(vfu_ctx, 0x01, 0x08, 0x02);
4181 : :
4182 : 31 : cap_offset = vfu_pci_add_capability(vfu_ctx, 0, 0, &pmcap);
4183 [ - + ]: 31 : if (cap_offset < 0) {
4184 : 0 : SPDK_ERRLOG("vfu_ctx %p failed add pmcap\n", vfu_ctx);
4185 : 0 : return ret;
4186 : : }
4187 : :
4188 : 31 : cap_offset = vfu_pci_add_capability(vfu_ctx, 0, 0, &pxcap);
4189 [ - + ]: 31 : if (cap_offset < 0) {
4190 : 0 : SPDK_ERRLOG("vfu_ctx %p failed add pxcap\n", vfu_ctx);
4191 : 0 : return ret;
4192 : : }
4193 : :
4194 : 31 : cap_offset = vfu_pci_add_capability(vfu_ctx, 0, 0, &msixcap);
4195 [ - + ]: 31 : if (cap_offset < 0) {
4196 : 0 : SPDK_ERRLOG("vfu_ctx %p failed add msixcap\n", vfu_ctx);
4197 : 0 : return ret;
4198 : : }
4199 : :
4200 : 45 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_CFG_REGION_IDX, NVME_REG_CFG_SIZE,
4201 [ - + + - : 14 : access_pci_config, VFU_REGION_FLAG_RW, NULL, 0, -1, 0);
- + + - ]
4202 [ - + ]: 31 : if (ret < 0) {
4203 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup cfg\n", vfu_ctx);
4204 : 0 : return ret;
4205 : : }
4206 : :
4207 [ + + + + : 31 : if (vu_transport->transport_opts.disable_mappable_bar0) {
+ - + - -
+ ]
4208 : 2 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_BAR0_REGION_IDX, NVME_REG_BAR0_SIZE,
4209 [ # # # # : 0 : access_bar0_fn, VFU_REGION_FLAG_RW | VFU_REGION_FLAG_MEM,
# # # # #
# # # ]
4210 : : NULL, 0, -1, 0);
4211 : 0 : } else {
4212 : 43 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_BAR0_REGION_IDX, NVME_REG_BAR0_SIZE,
4213 [ - + + - : 14 : access_bar0_fn, VFU_REGION_FLAG_RW | VFU_REGION_FLAG_MEM,
- + + - -
+ + - ]
4214 [ + - + - ]: 14 : sparse_mmap, 1, endpoint->devmem_fd, 0);
4215 : : }
4216 : :
4217 [ - + ]: 31 : if (ret < 0) {
4218 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup bar 0\n", vfu_ctx);
4219 : 0 : return ret;
4220 : : }
4221 : :
4222 : 45 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_BAR4_REGION_IDX, NVMF_VFIO_USER_BAR4_SIZE,
4223 [ - + + - : 14 : NULL, VFU_REGION_FLAG_RW, NULL, 0, -1, 0);
- + + - ]
4224 [ - + ]: 31 : if (ret < 0) {
4225 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup bar 4\n", vfu_ctx);
4226 : 0 : return ret;
4227 : : }
4228 : :
4229 : 45 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_BAR5_REGION_IDX, NVMF_VFIO_USER_BAR5_SIZE,
4230 [ - + + - : 14 : NULL, VFU_REGION_FLAG_RW, NULL, 0, -1, 0);
- + + - ]
4231 [ - + ]: 31 : if (ret < 0) {
4232 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup bar 5\n", vfu_ctx);
4233 : 0 : return ret;
4234 : : }
4235 : :
4236 : 31 : ret = vfu_setup_device_dma(vfu_ctx, memory_region_add_cb, memory_region_remove_cb);
4237 [ - + ]: 31 : if (ret < 0) {
4238 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup dma callback\n", vfu_ctx);
4239 : 0 : return ret;
4240 : : }
4241 : :
4242 : 31 : ret = vfu_setup_device_reset_cb(vfu_ctx, vfio_user_device_reset);
4243 [ - + ]: 31 : if (ret < 0) {
4244 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup reset callback\n", vfu_ctx);
4245 : 0 : return ret;
4246 : : }
4247 : :
4248 : 31 : ret = vfu_setup_device_nr_irqs(vfu_ctx, VFU_DEV_INTX_IRQ, 1);
4249 [ - + ]: 31 : if (ret < 0) {
4250 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup INTX\n", vfu_ctx);
4251 : 0 : return ret;
4252 : : }
4253 : :
4254 : 31 : ret = vfu_setup_device_nr_irqs(vfu_ctx, VFU_DEV_MSIX_IRQ, NVMF_VFIO_USER_MSIX_NUM);
4255 [ - + ]: 31 : if (ret < 0) {
4256 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup MSIX\n", vfu_ctx);
4257 : 0 : return ret;
4258 : : }
4259 : :
4260 : 31 : vfu_setup_device_quiesce_cb(vfu_ctx, vfio_user_dev_quiesce_cb);
4261 : :
4262 : 31 : migr_sparse_mmap.iov_base = (void *)4096;
4263 [ + - ]: 31 : migr_sparse_mmap.iov_len = vfio_user_migr_data_len();
4264 : 62 : ret = vfu_setup_region(vfu_ctx, VFU_PCI_DEV_MIGR_REGION_IDX,
4265 : 31 : vfu_get_migr_register_area_size() + vfio_user_migr_data_len(),
4266 [ - + + - : 14 : NULL, VFU_REGION_FLAG_RW | VFU_REGION_FLAG_MEM, &migr_sparse_mmap,
- + + - -
+ + - ]
4267 [ + - + - ]: 14 : 1, endpoint->migr_fd, 0);
4268 [ - + ]: 31 : if (ret < 0) {
4269 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup migration region\n", vfu_ctx);
4270 : 0 : return ret;
4271 : : }
4272 : :
4273 : 45 : ret = vfu_setup_device_migration_callbacks(vfu_ctx, &migr_callbacks,
4274 : 14 : vfu_get_migr_register_area_size());
4275 [ - + ]: 31 : if (ret < 0) {
4276 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to setup migration callbacks\n", vfu_ctx);
4277 : 0 : return ret;
4278 : : }
4279 : :
4280 : 31 : ret = vfu_realize_ctx(vfu_ctx);
4281 [ - + ]: 31 : if (ret < 0) {
4282 : 0 : SPDK_ERRLOG("vfu_ctx %p failed to realize\n", vfu_ctx);
4283 : 0 : return ret;
4284 : : }
4285 : :
4286 [ - + - + : 31 : endpoint->pci_config_space = vfu_pci_get_config_space(endpoint->vfu_ctx);
- + - + ]
4287 [ - + - + : 31 : assert(endpoint->pci_config_space != NULL);
+ - # # ]
4288 [ + - + - ]: 31 : init_pci_config_space(endpoint->pci_config_space);
4289 : :
4290 [ + + # # ]: 31 : assert(cap_offset != 0);
4291 [ + - + - : 31 : endpoint->msix = (struct msixcap *)((uint8_t *)endpoint->pci_config_space + cap_offset);
+ - + - +
- ]
4292 : :
4293 : 31 : return 0;
4294 : 14 : }
4295 : :
4296 : : static int nvmf_vfio_user_accept(void *ctx);
4297 : :
4298 : : /*
4299 : : * Register an "accept" poller: this is polling for incoming vfio-user socket
4300 : : * connections (on the listening socket).
4301 : : *
4302 : : * We need to do this on first listening, and also after destroying a
4303 : : * controller, so we can accept another connection.
4304 : : */
4305 : : static int
4306 : 143 : vfio_user_register_accept_poller(struct nvmf_vfio_user_endpoint *endpoint)
4307 : : {
4308 [ + - + - : 143 : uint64_t poll_rate_us = endpoint->transport->transport.opts.acceptor_poll_rate;
+ - + - +
- + - ]
4309 : :
4310 [ + + + + : 143 : SPDK_DEBUGLOG(nvmf_vfio, "registering accept poller\n");
+ - ]
4311 : :
4312 [ + - + - ]: 143 : endpoint->accept_poller = SPDK_POLLER_REGISTER(nvmf_vfio_user_accept,
4313 : : endpoint, poll_rate_us);
4314 : :
4315 [ + + + - : 143 : if (!endpoint->accept_poller) {
+ - ]
4316 : 0 : return -1;
4317 : : }
4318 : :
4319 [ + - + - ]: 143 : endpoint->accept_thread = spdk_get_thread();
4320 [ + - + - ]: 143 : endpoint->need_relisten = false;
4321 : :
4322 [ + + ]: 143 : if (!spdk_interrupt_mode_is_enabled()) {
4323 : 141 : return 0;
4324 : : }
4325 : :
4326 [ # # # # : 2 : endpoint->accept_intr_fd = vfu_get_poll_fd(endpoint->vfu_ctx);
# # # # ]
4327 [ - + # # : 2 : assert(endpoint->accept_intr_fd != -1);
# # # # ]
4328 : :
4329 [ # # # # : 2 : endpoint->accept_intr = SPDK_INTERRUPT_REGISTER(endpoint->accept_intr_fd,
# # # # ]
4330 : : nvmf_vfio_user_accept, endpoint);
4331 : :
4332 [ - + # # : 2 : assert(endpoint->accept_intr != NULL);
# # # # ]
4333 : :
4334 [ # # # # ]: 2 : spdk_poller_register_interrupt(endpoint->accept_poller, NULL, NULL);
4335 : 2 : return 0;
4336 : 87 : }
4337 : :
4338 : : static void
4339 : 112 : _vfio_user_relisten(void *ctx)
4340 : : {
4341 : 112 : struct nvmf_vfio_user_endpoint *endpoint = ctx;
4342 : :
4343 : 112 : vfio_user_register_accept_poller(endpoint);
4344 : 112 : }
4345 : :
4346 : : static void
4347 : 127 : _free_ctrlr(void *ctx)
4348 : : {
4349 : 127 : struct nvmf_vfio_user_ctrlr *ctrlr = ctx;
4350 [ + - + - ]: 127 : struct nvmf_vfio_user_endpoint *endpoint = ctrlr->endpoint;
4351 : :
4352 [ + - + - : 127 : free_sdbl(endpoint->vfu_ctx, ctrlr->sdbl);
+ - + - ]
4353 : :
4354 [ + - ]: 127 : spdk_interrupt_unregister(&ctrlr->intr);
4355 [ + - + - ]: 127 : ctrlr->intr_fd = -1;
4356 [ + - ]: 127 : spdk_poller_unregister(&ctrlr->vfu_ctx_poller);
4357 : :
4358 : 127 : free(ctrlr);
4359 : :
4360 [ + + + + : 127 : if (endpoint->need_async_destroy) {
+ - + + ]
4361 : 7 : nvmf_vfio_user_destroy_endpoint(endpoint);
4362 [ + + + + : 127 : } else if (endpoint->need_relisten) {
+ - - + ]
4363 [ + - + - ]: 112 : spdk_thread_send_msg(endpoint->accept_thread,
4364 : 73 : _vfio_user_relisten, endpoint);
4365 : 73 : }
4366 : 127 : }
4367 : :
4368 : : static void
4369 : 127 : free_ctrlr(struct nvmf_vfio_user_ctrlr *ctrlr)
4370 : : {
4371 : : struct spdk_thread *thread;
4372 : : int i;
4373 : :
4374 [ + + # # ]: 127 : assert(ctrlr != NULL);
4375 [ + - + - : 127 : thread = ctrlr->thread ? ctrlr->thread : spdk_get_thread();
+ - + - +
- ]
4376 : :
4377 [ + + + + : 127 : SPDK_DEBUGLOG(nvmf_vfio, "free %s\n", ctrlr_id(ctrlr));
+ - ]
4378 : :
4379 [ + + + - ]: 65151 : for (i = 0; i < NVMF_VFIO_USER_MAX_QPAIRS_PER_CTRLR; i++) {
4380 : 65024 : free_qp(ctrlr, i);
4381 : 40960 : }
4382 : :
4383 : 127 : spdk_thread_exec_msg(thread, _free_ctrlr, ctrlr);
4384 : 127 : }
4385 : :
4386 : : static int
4387 : 127 : nvmf_vfio_user_create_ctrlr(struct nvmf_vfio_user_transport *transport,
4388 : : struct nvmf_vfio_user_endpoint *endpoint)
4389 : : {
4390 : : struct nvmf_vfio_user_ctrlr *ctrlr;
4391 : 127 : int err = 0;
4392 : :
4393 [ + + + + : 127 : SPDK_DEBUGLOG(nvmf_vfio, "%s\n", endpoint_id(endpoint));
+ - ]
4394 : :
4395 : : /* First, construct a vfio-user CUSTOM transport controller */
4396 : 127 : ctrlr = calloc(1, sizeof(*ctrlr));
4397 [ + + ]: 127 : if (ctrlr == NULL) {
4398 : 0 : err = -ENOMEM;
4399 : 0 : goto out;
4400 : : }
4401 : : /*
4402 : : * We can only support one connection for now, but generate a unique cntlid in case vfio-user
4403 : : * transport is used together with RDMA or TCP transports in the same target
4404 : : */
4405 [ + - + - : 127 : ctrlr->cntlid = nvmf_subsystem_gen_cntlid(endpoint->subsystem);
+ - + - ]
4406 [ + - + - ]: 127 : ctrlr->intr_fd = -1;
4407 [ + - + - ]: 127 : ctrlr->transport = transport;
4408 [ + - + - ]: 127 : ctrlr->endpoint = endpoint;
4409 [ + - + - : 127 : ctrlr->bar0_doorbells = endpoint->bar0_doorbells;
+ - + - ]
4410 [ + - + - : 127 : TAILQ_INIT(&ctrlr->connected_sqs);
+ - + - +
- + - + -
+ - ]
4411 : :
4412 [ + - + - ]: 127 : ctrlr->adaptive_irqs_enabled =
4413 [ + + + - : 127 : !transport->transport_opts.disable_adaptive_irq;
+ - + - ]
4414 : :
4415 : : /* Then, construct an admin queue pair */
4416 [ + - ]: 127 : err = init_sq(ctrlr, &transport->transport, 0);
4417 [ - + ]: 127 : if (err != 0) {
4418 : 0 : free(ctrlr);
4419 : 0 : goto out;
4420 : : }
4421 : :
4422 : 127 : err = init_cq(ctrlr, 0);
4423 [ - + ]: 127 : if (err != 0) {
4424 : 0 : free(ctrlr);
4425 : 0 : goto out;
4426 : : }
4427 : :
4428 [ + - + - : 127 : ctrlr->sqs[0]->size = NVMF_VFIO_USER_DEFAULT_AQ_DEPTH;
+ - + - +
- + - ]
4429 : :
4430 [ + - + - : 127 : err = alloc_sq_reqs(ctrlr, ctrlr->sqs[0]);
+ - + - ]
4431 [ + + ]: 127 : if (err != 0) {
4432 : 0 : free(ctrlr);
4433 : 0 : goto out;
4434 : : }
4435 [ + - + - ]: 127 : endpoint->ctrlr = ctrlr;
4436 : :
4437 : : /* Notify the generic layer about the new admin queue pair */
4438 [ + - + - : 127 : spdk_nvmf_tgt_new_qpair(transport->transport.tgt, &ctrlr->sqs[0]->qpair);
+ - + - +
- + - + -
+ - ]
4439 : :
4440 : 47 : out:
4441 [ + + ]: 127 : if (err != 0) {
4442 [ # # ]: 0 : SPDK_ERRLOG("%s: failed to create vfio-user controller: %s\n",
4443 : : endpoint_id(endpoint), strerror(-err));
4444 : 0 : }
4445 : :
4446 : 127 : return err;
4447 : : }
4448 : :
4449 : : static int
4450 : 31 : nvmf_vfio_user_listen(struct spdk_nvmf_transport *transport,
4451 : : const struct spdk_nvme_transport_id *trid,
4452 : : struct spdk_nvmf_listen_opts *listen_opts)
4453 : : {
4454 : : struct nvmf_vfio_user_transport *vu_transport;
4455 : : struct nvmf_vfio_user_endpoint *endpoint, *tmp;
4456 : 31 : char path[PATH_MAX] = {};
4457 : 31 : char uuid[PATH_MAX] = {};
4458 : : int ret;
4459 : :
4460 : 31 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
4461 : : transport);
4462 : :
4463 [ + + + - ]: 31 : pthread_mutex_lock(&vu_transport->lock);
4464 [ + + + - : 45 : TAILQ_FOREACH_SAFE(endpoint, &vu_transport->endpoints, link, tmp) {
+ - + + +
- + - + -
+ + ]
4465 : : /* Only compare traddr */
4466 [ + + + + : 14 : if (strncmp(endpoint->trid.traddr, trid->traddr, sizeof(endpoint->trid.traddr)) == 0) {
+ + + - +
- - + ]
4467 [ # # # # ]: 0 : pthread_mutex_unlock(&vu_transport->lock);
4468 : 0 : return -EEXIST;
4469 : : }
4470 : 7 : }
4471 [ + + + - ]: 31 : pthread_mutex_unlock(&vu_transport->lock);
4472 : :
4473 : 31 : endpoint = calloc(1, sizeof(*endpoint));
4474 [ - + ]: 31 : if (!endpoint) {
4475 : 0 : return -ENOMEM;
4476 : : }
4477 : :
4478 [ + + + - ]: 31 : pthread_mutex_init(&endpoint->lock, NULL);
4479 [ + - + - ]: 31 : endpoint->devmem_fd = -1;
4480 [ + + + + : 31 : memcpy(&endpoint->trid, trid, sizeof(endpoint->trid));
+ - ]
4481 [ + - + - ]: 31 : endpoint->transport = vu_transport;
4482 : :
4483 : 31 : ret = snprintf(path, PATH_MAX, "%s/bar0", endpoint_id(endpoint));
4484 [ + - - + ]: 31 : if (ret < 0 || ret >= PATH_MAX) {
4485 [ # # ]: 0 : SPDK_ERRLOG("%s: error to get socket path: %s.\n", endpoint_id(endpoint), spdk_strerror(errno));
4486 : 0 : ret = -1;
4487 : 0 : goto out;
4488 : : }
4489 : :
4490 [ + - ]: 31 : ret = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
4491 [ - + ]: 31 : if (ret == -1) {
4492 [ # # ]: 0 : SPDK_ERRLOG("%s: failed to open device memory at %s: %s.\n",
4493 : : endpoint_id(endpoint), path, spdk_strerror(errno));
4494 : 0 : goto out;
4495 : : }
4496 [ + - ]: 31 : unlink(path);
4497 : :
4498 [ + - + - ]: 31 : endpoint->devmem_fd = ret;
4499 [ + - + - ]: 31 : ret = ftruncate(endpoint->devmem_fd,
4500 : : NVME_DOORBELLS_OFFSET + NVMF_VFIO_USER_DOORBELLS_SIZE);
4501 [ - + ]: 31 : if (ret != 0) {
4502 [ # # ]: 0 : SPDK_ERRLOG("%s: error to ftruncate file %s: %s.\n", endpoint_id(endpoint), path,
4503 : : spdk_strerror(errno));
4504 : 0 : goto out;
4505 : : }
4506 : :
4507 [ + - + - ]: 31 : endpoint->bar0_doorbells = mmap(NULL, NVMF_VFIO_USER_DOORBELLS_SIZE,
4508 [ + - + - ]: 14 : PROT_READ | PROT_WRITE, MAP_SHARED, endpoint->devmem_fd, NVME_DOORBELLS_OFFSET);
4509 [ + + + - : 31 : if (endpoint->bar0_doorbells == MAP_FAILED) {
- + ]
4510 [ # # ]: 0 : SPDK_ERRLOG("%s: error to mmap file %s: %s.\n", endpoint_id(endpoint), path, spdk_strerror(errno));
4511 [ # # # # ]: 0 : endpoint->bar0_doorbells = NULL;
4512 : 0 : ret = -1;
4513 : 0 : goto out;
4514 : : }
4515 : :
4516 : 31 : ret = snprintf(path, PATH_MAX, "%s/migr", endpoint_id(endpoint));
4517 [ + - - + ]: 31 : if (ret < 0 || ret >= PATH_MAX) {
4518 [ # # ]: 0 : SPDK_ERRLOG("%s: error to get migration file path: %s.\n", endpoint_id(endpoint),
4519 : : spdk_strerror(errno));
4520 : 0 : ret = -1;
4521 : 0 : goto out;
4522 : : }
4523 [ + - ]: 31 : ret = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
4524 [ + + ]: 31 : if (ret == -1) {
4525 [ # # ]: 0 : SPDK_ERRLOG("%s: failed to open device memory at %s: %s.\n",
4526 : : endpoint_id(endpoint), path, spdk_strerror(errno));
4527 : 0 : goto out;
4528 : : }
4529 [ + - ]: 31 : unlink(path);
4530 : :
4531 [ + - + - ]: 31 : endpoint->migr_fd = ret;
4532 [ + - + - ]: 31 : ret = ftruncate(endpoint->migr_fd,
4533 : 31 : vfu_get_migr_register_area_size() + vfio_user_migr_data_len());
4534 [ - + ]: 31 : if (ret != 0) {
4535 [ # # ]: 0 : SPDK_ERRLOG("%s: error to ftruncate migration file %s: %s.\n", endpoint_id(endpoint), path,
4536 : : spdk_strerror(errno));
4537 : 0 : goto out;
4538 : : }
4539 : :
4540 [ + - + - ]: 45 : endpoint->migr_data = mmap(NULL, vfio_user_migr_data_len(),
4541 [ + - + - ]: 31 : PROT_READ | PROT_WRITE, MAP_SHARED, endpoint->migr_fd, vfu_get_migr_register_area_size());
4542 [ + + + - : 31 : if (endpoint->migr_data == MAP_FAILED) {
+ - ]
4543 [ # # ]: 0 : SPDK_ERRLOG("%s: error to mmap file %s: %s.\n", endpoint_id(endpoint), path, spdk_strerror(errno));
4544 [ # # # # ]: 0 : endpoint->migr_data = NULL;
4545 : 0 : ret = -1;
4546 : 0 : goto out;
4547 : : }
4548 : :
4549 : 31 : ret = snprintf(uuid, PATH_MAX, "%s/cntrl", endpoint_id(endpoint));
4550 [ + - - + ]: 31 : if (ret < 0 || ret >= PATH_MAX) {
4551 [ # # ]: 0 : SPDK_ERRLOG("%s: error to get ctrlr file path: %s\n", endpoint_id(endpoint), spdk_strerror(errno));
4552 : 0 : ret = -1;
4553 : 0 : goto out;
4554 : : }
4555 : :
4556 [ - + + - : 31 : endpoint->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, uuid, LIBVFIO_USER_FLAG_ATTACH_NB,
+ - + - ]
4557 : 14 : endpoint, VFU_DEV_TYPE_PCI);
4558 [ + + + - : 31 : if (endpoint->vfu_ctx == NULL) {
+ - ]
4559 : 0 : SPDK_ERRLOG("%s: error creating libmuser context: %m\n",
4560 : : endpoint_id(endpoint));
4561 : 0 : ret = -1;
4562 : 0 : goto out;
4563 : : }
4564 : :
4565 [ + - + - ]: 31 : ret = vfu_setup_log(endpoint->vfu_ctx, vfio_user_log,
4566 : 14 : vfio_user_get_log_level());
4567 [ + + ]: 31 : if (ret < 0) {
4568 : 0 : goto out;
4569 : : }
4570 : :
4571 : :
4572 : 31 : ret = vfio_user_dev_info_fill(vu_transport, endpoint);
4573 [ + + ]: 31 : if (ret < 0) {
4574 : 0 : goto out;
4575 : : }
4576 : :
4577 : 31 : ret = vfio_user_register_accept_poller(endpoint);
4578 : :
4579 [ - + ]: 31 : if (ret != 0) {
4580 : 0 : goto out;
4581 : : }
4582 : :
4583 [ - + - + ]: 31 : pthread_mutex_lock(&vu_transport->lock);
4584 [ - + - + : 31 : TAILQ_INSERT_TAIL(&vu_transport->endpoints, endpoint, link);
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + ]
4585 [ - + - + ]: 31 : pthread_mutex_unlock(&vu_transport->lock);
4586 : :
4587 : 17 : out:
4588 [ - + ]: 31 : if (ret != 0) {
4589 : 0 : nvmf_vfio_user_destroy_endpoint(endpoint);
4590 : 0 : }
4591 : :
4592 : 31 : return ret;
4593 : 14 : }
4594 : :
4595 : : static void
4596 : 31 : nvmf_vfio_user_stop_listen(struct spdk_nvmf_transport *transport,
4597 : : const struct spdk_nvme_transport_id *trid)
4598 : : {
4599 : : struct nvmf_vfio_user_transport *vu_transport;
4600 : : struct nvmf_vfio_user_endpoint *endpoint, *tmp;
4601 : :
4602 [ + + # # ]: 31 : assert(trid != NULL);
4603 [ + + + - : 31 : assert(trid->traddr != NULL);
# # ]
4604 : :
4605 [ + + + + : 31 : SPDK_DEBUGLOG(nvmf_vfio, "%s: stop listen\n", trid->traddr);
+ - # # ]
4606 : :
4607 : 31 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
4608 : : transport);
4609 : :
4610 [ + + + - ]: 31 : pthread_mutex_lock(&vu_transport->lock);
4611 [ + - + - : 31 : TAILQ_FOREACH_SAFE(endpoint, &vu_transport->endpoints, link, tmp) {
+ - + - +
- + - + -
+ - ]
4612 [ + + + + : 31 : if (strcmp(trid->traddr, endpoint->trid.traddr) == 0) {
+ - + - +
- - + ]
4613 [ + + + - : 31 : TAILQ_REMOVE(&vu_transport->endpoints, endpoint, link);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
4614 : : /* Defer to free endpoint resources until the controller
4615 : : * is freed. There are two cases when running here:
4616 : : * 1. kill nvmf target while VM is connected
4617 : : * 2. remove listener via RPC call
4618 : : * nvmf library will disconnect all queue paris.
4619 : : */
4620 [ + + + - : 31 : if (endpoint->ctrlr) {
+ + ]
4621 [ + - + - : 7 : assert(!endpoint->need_async_destroy);
+ - + - #
# ]
4622 [ + - + - ]: 7 : endpoint->need_async_destroy = true;
4623 [ + - + - ]: 7 : pthread_mutex_unlock(&vu_transport->lock);
4624 : 7 : return;
4625 : : }
4626 : :
4627 : 24 : nvmf_vfio_user_destroy_endpoint(endpoint);
4628 [ + + + - ]: 24 : pthread_mutex_unlock(&vu_transport->lock);
4629 : 24 : return;
4630 : : }
4631 : 0 : }
4632 [ # # # # ]: 0 : pthread_mutex_unlock(&vu_transport->lock);
4633 : :
4634 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: not found\n", trid->traddr);
# # # # ]
4635 : 14 : }
4636 : :
4637 : : static void
4638 : 127 : nvmf_vfio_user_cdata_init(struct spdk_nvmf_transport *transport,
4639 : : struct spdk_nvmf_subsystem *subsystem,
4640 : : struct spdk_nvmf_ctrlr_data *cdata)
4641 : : {
4642 : : struct nvmf_vfio_user_transport *vu_transport;
4643 : :
4644 : 127 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport, transport);
4645 : :
4646 [ + - + - ]: 127 : cdata->vid = SPDK_PCI_VID_NUTANIX;
4647 [ + - + - ]: 127 : cdata->ssvid = SPDK_PCI_VID_NUTANIX;
4648 [ + - + - : 127 : cdata->ieee[0] = 0x8d;
+ - + - ]
4649 [ + - + - : 127 : cdata->ieee[1] = 0x6b;
+ - + - ]
4650 [ + - + - : 127 : cdata->ieee[2] = 0x50;
+ - + - ]
4651 [ + + + - ]: 127 : memset(&cdata->sgls, 0, sizeof(struct spdk_nvme_cdata_sgls));
4652 [ + - + - ]: 127 : cdata->sgls.supported = SPDK_NVME_SGLS_SUPPORTED_DWORD_ALIGNED;
4653 [ + + + - : 127 : cdata->oncs.compare = !vu_transport->transport_opts.disable_compare;
+ - + - +
- + - ]
4654 : : /* libvfio-user can only support 1 connection for now */
4655 [ + - + - ]: 127 : cdata->oncs.reservations = 0;
4656 [ + + + - : 127 : cdata->oacs.doorbell_buffer_config = !vu_transport->transport_opts.disable_shadow_doorbells;
+ - + - +
- + - ]
4657 [ + + + - : 127 : cdata->fuses.compare_and_write = !vu_transport->transport_opts.disable_compare;
+ - + - +
- + - ]
4658 : 127 : }
4659 : :
4660 : : static int
4661 : 31 : nvmf_vfio_user_listen_associate(struct spdk_nvmf_transport *transport,
4662 : : const struct spdk_nvmf_subsystem *subsystem,
4663 : : const struct spdk_nvme_transport_id *trid)
4664 : : {
4665 : : struct nvmf_vfio_user_transport *vu_transport;
4666 : : struct nvmf_vfio_user_endpoint *endpoint;
4667 : :
4668 : 31 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport, transport);
4669 : :
4670 [ + + + - ]: 31 : pthread_mutex_lock(&vu_transport->lock);
4671 [ + - + - : 45 : TAILQ_FOREACH(endpoint, &vu_transport->endpoints, link) {
+ - - + +
- + - +
- ]
4672 [ + + + + : 45 : if (strncmp(endpoint->trid.traddr, trid->traddr, sizeof(endpoint->trid.traddr)) == 0) {
+ + + - +
- + + ]
4673 : 31 : break;
4674 : : }
4675 : 7 : }
4676 [ + + + - ]: 31 : pthread_mutex_unlock(&vu_transport->lock);
4677 : :
4678 [ + + ]: 31 : if (endpoint == NULL) {
4679 : 0 : return -ENOENT;
4680 : : }
4681 : :
4682 : : /* Drop const - we will later need to pause/unpause. */
4683 [ + - + - ]: 31 : endpoint->subsystem = (struct spdk_nvmf_subsystem *)subsystem;
4684 : :
4685 : 31 : return 0;
4686 : 14 : }
4687 : :
4688 : : /*
4689 : : * Executed periodically at a default SPDK_NVMF_DEFAULT_ACCEPT_POLL_RATE_US
4690 : : * frequency.
4691 : : *
4692 : : * For this endpoint (which at the libvfio-user level corresponds to a socket),
4693 : : * if we don't currently have a controller set up, peek to see if the socket is
4694 : : * able to accept a new connection.
4695 : : */
4696 : : static int
4697 : 7445 : nvmf_vfio_user_accept(void *ctx)
4698 : : {
4699 : 7445 : struct nvmf_vfio_user_endpoint *endpoint = ctx;
4700 : : struct nvmf_vfio_user_transport *vu_transport;
4701 : : int err;
4702 : :
4703 [ + - + - ]: 7445 : vu_transport = endpoint->transport;
4704 : :
4705 [ + + + - : 7445 : if (endpoint->ctrlr != NULL) {
- + ]
4706 : 0 : return SPDK_POLLER_IDLE;
4707 : : }
4708 : :
4709 : : /* While we're here, the controller is already destroyed,
4710 : : * subsystem may still be in RESUMING state, we will wait
4711 : : * until the subsystem is in RUNNING state.
4712 : : */
4713 [ + + + + : 7445 : if (endpoint->need_resume) {
+ - - + ]
4714 : 0 : return SPDK_POLLER_IDLE;
4715 : : }
4716 : :
4717 [ + - + - ]: 7445 : err = vfu_attach_ctx(endpoint->vfu_ctx);
4718 [ + + ]: 7445 : if (err == 0) {
4719 [ + + + + : 127 : SPDK_DEBUGLOG(nvmf_vfio, "attach succeeded\n");
+ - ]
4720 : 127 : err = nvmf_vfio_user_create_ctrlr(vu_transport, endpoint);
4721 [ + + ]: 127 : if (err == 0) {
4722 : : /*
4723 : : * Unregister ourselves: now we've accepted a
4724 : : * connection, there is nothing for us to poll for, and
4725 : : * we will poll the connection via vfu_run_ctx()
4726 : : * instead.
4727 : : */
4728 [ - + ]: 127 : spdk_interrupt_unregister(&endpoint->accept_intr);
4729 [ - + ]: 127 : spdk_poller_unregister(&endpoint->accept_poller);
4730 : 80 : }
4731 : 127 : return SPDK_POLLER_BUSY;
4732 : : }
4733 : :
4734 [ + + - + : 7318 : if (errno == EAGAIN || errno == EWOULDBLOCK) {
# # # # ]
4735 : 7318 : return SPDK_POLLER_IDLE;
4736 : : }
4737 : :
4738 : 0 : return SPDK_POLLER_BUSY;
4739 : 487 : }
4740 : :
4741 : : static void
4742 : 0 : nvmf_vfio_user_discover(struct spdk_nvmf_transport *transport,
4743 : : struct spdk_nvme_transport_id *trid,
4744 : : struct spdk_nvmf_discovery_log_page_entry *entry)
4745 : 0 : { }
4746 : :
4747 : : static int vfio_user_poll_group_intr(void *ctx);
4748 : :
4749 : : static void
4750 : 4 : vfio_user_poll_group_add_intr(struct nvmf_vfio_user_poll_group *vu_group,
4751 : : struct spdk_nvmf_poll_group *group)
4752 : : {
4753 [ # # # # ]: 4 : vu_group->intr_fd = eventfd(0, EFD_NONBLOCK);
4754 [ - + # # : 4 : assert(vu_group->intr_fd != -1);
# # # # ]
4755 : :
4756 [ # # # # : 4 : vu_group->intr = SPDK_INTERRUPT_REGISTER(vu_group->intr_fd,
# # # # ]
4757 : : vfio_user_poll_group_intr, vu_group);
4758 [ - + # # : 4 : assert(vu_group->intr != NULL);
# # # # ]
4759 : 4 : }
4760 : :
4761 : : static struct spdk_nvmf_transport_poll_group *
4762 : 28 : nvmf_vfio_user_poll_group_create(struct spdk_nvmf_transport *transport,
4763 : : struct spdk_nvmf_poll_group *group)
4764 : : {
4765 : : struct nvmf_vfio_user_transport *vu_transport;
4766 : : struct nvmf_vfio_user_poll_group *vu_group;
4767 : :
4768 : 28 : vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
4769 : : transport);
4770 : :
4771 [ + + + + : 28 : SPDK_DEBUGLOG(nvmf_vfio, "create poll group\n");
+ - ]
4772 : :
4773 : 28 : vu_group = calloc(1, sizeof(*vu_group));
4774 [ + + ]: 28 : if (vu_group == NULL) {
4775 : 0 : SPDK_ERRLOG("Error allocating poll group: %m");
4776 : 0 : return NULL;
4777 : : }
4778 : :
4779 [ + + ]: 28 : if (in_interrupt_mode(vu_transport)) {
4780 : 4 : vfio_user_poll_group_add_intr(vu_group, group);
4781 : 0 : }
4782 : :
4783 [ + - + - : 28 : TAILQ_INIT(&vu_group->sqs);
+ - + - +
- + - + -
+ - ]
4784 : :
4785 [ + + + - ]: 28 : pthread_mutex_lock(&vu_transport->pg_lock);
4786 [ + - + - : 28 : TAILQ_INSERT_TAIL(&vu_transport->poll_groups, vu_group, link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
4787 [ + + + - : 28 : if (vu_transport->next_pg == NULL) {
+ - ]
4788 [ + - + - ]: 14 : vu_transport->next_pg = vu_group;
4789 : 7 : }
4790 [ + + + - ]: 28 : pthread_mutex_unlock(&vu_transport->pg_lock);
4791 : :
4792 [ + - ]: 28 : return &vu_group->group;
4793 : 7 : }
4794 : :
4795 : : static struct spdk_nvmf_transport_poll_group *
4796 : 324 : nvmf_vfio_user_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
4797 : : {
4798 : : struct nvmf_vfio_user_transport *vu_transport;
4799 : : struct nvmf_vfio_user_poll_group **vu_group;
4800 : : struct nvmf_vfio_user_sq *sq;
4801 : : struct nvmf_vfio_user_cq *cq;
4802 : :
4803 : 324 : struct spdk_nvmf_transport_poll_group *result = NULL;
4804 : :
4805 : 324 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
4806 [ + - + - : 324 : cq = sq->ctrlr->cqs[sq->cqid];
+ - + - +
- + - + -
+ - ]
4807 [ + + # # ]: 324 : assert(cq != NULL);
4808 [ + - + - ]: 324 : vu_transport = SPDK_CONTAINEROF(qpair->transport, struct nvmf_vfio_user_transport, transport);
4809 : :
4810 [ + + + - ]: 324 : pthread_mutex_lock(&vu_transport->pg_lock);
4811 [ + + + - : 324 : if (TAILQ_EMPTY(&vu_transport->poll_groups)) {
+ - - + ]
4812 : 0 : goto out;
4813 : : }
4814 : :
4815 [ + + ]: 324 : if (!nvmf_qpair_is_admin_queue(qpair)) {
4816 : : /*
4817 : : * If this is shared IO CQ case, just return the used CQ's poll
4818 : : * group, so I/O completions don't have to use
4819 : : * spdk_thread_send_msg().
4820 : : */
4821 [ + + + - : 197 : if (cq->group != NULL) {
- + ]
4822 [ # # # # ]: 4 : result = cq->group;
4823 : 4 : goto out;
4824 : : }
4825 : :
4826 : : /*
4827 : : * If we're in interrupt mode, align all qpairs for a controller
4828 : : * on the same poll group by default, unless requested. This can
4829 : : * be lower in performance than running on a single poll group,
4830 : : * so we disable spreading by default.
4831 : : */
4832 [ - + # # ]: 193 : if (in_interrupt_mode(vu_transport) &&
4833 [ # # # # : 0 : !vu_transport->transport_opts.enable_intr_mode_sq_spreading) {
# # # # ]
4834 [ # # # # : 0 : result = sq->ctrlr->sqs[0]->group;
# # # # #
# # # # #
# # ]
4835 : 0 : goto out;
4836 : : }
4837 : :
4838 : 7 : }
4839 : :
4840 [ + - ]: 320 : vu_group = &vu_transport->next_pg;
4841 [ + + + - : 320 : assert(*vu_group != NULL);
# # ]
4842 : :
4843 [ + - + - ]: 320 : result = &(*vu_group)->group;
4844 [ + - + - : 320 : *vu_group = TAILQ_NEXT(*vu_group, link);
+ - + - +
- ]
4845 [ + + + - ]: 407 : if (*vu_group == NULL) {
4846 [ + - + - : 174 : *vu_group = TAILQ_FIRST(&vu_transport->poll_groups);
+ - + - ]
4847 : 87 : }
4848 : :
4849 : 146 : out:
4850 [ + + + - : 324 : if (cq->group == NULL) {
- + ]
4851 [ + - + - ]: 320 : cq->group = result;
4852 : 87 : }
4853 : :
4854 [ + + + - ]: 324 : pthread_mutex_unlock(&vu_transport->pg_lock);
4855 : 324 : return result;
4856 : : }
4857 : :
4858 : : static void
4859 : 4 : vfio_user_poll_group_del_intr(struct nvmf_vfio_user_poll_group *vu_group)
4860 : : {
4861 [ - + # # : 4 : assert(vu_group->intr_fd != -1);
# # # # ]
4862 : :
4863 [ # # ]: 4 : spdk_interrupt_unregister(&vu_group->intr);
4864 : :
4865 [ # # # # ]: 4 : close(vu_group->intr_fd);
4866 [ # # # # ]: 4 : vu_group->intr_fd = -1;
4867 : 4 : }
4868 : :
4869 : : /* called when process exits */
4870 : : static void
4871 : 28 : nvmf_vfio_user_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group)
4872 : : {
4873 : : struct nvmf_vfio_user_poll_group *vu_group, *next_tgroup;
4874 : : struct nvmf_vfio_user_transport *vu_transport;
4875 : :
4876 [ + + + + : 28 : SPDK_DEBUGLOG(nvmf_vfio, "destroy poll group\n");
+ - ]
4877 : :
4878 : 28 : vu_group = SPDK_CONTAINEROF(group, struct nvmf_vfio_user_poll_group, group);
4879 [ + - + - : 28 : vu_transport = SPDK_CONTAINEROF(vu_group->group.transport, struct nvmf_vfio_user_transport,
+ - ]
4880 : : transport);
4881 : :
4882 [ + + ]: 28 : if (in_interrupt_mode(vu_transport)) {
4883 : 4 : vfio_user_poll_group_del_intr(vu_group);
4884 : 0 : }
4885 : :
4886 [ + + + - ]: 28 : pthread_mutex_lock(&vu_transport->pg_lock);
4887 [ + - + - : 28 : next_tgroup = TAILQ_NEXT(vu_group, link);
+ - ]
4888 [ + + + - : 28 : TAILQ_REMOVE(&vu_transport->poll_groups, vu_group, link);
+ - + - #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
4889 [ + + ]: 28 : if (next_tgroup == NULL) {
4890 [ + - + - : 18 : next_tgroup = TAILQ_FIRST(&vu_transport->poll_groups);
+ - ]
4891 : 7 : }
4892 [ + + + - : 28 : if (vu_transport->next_pg == vu_group) {
+ - ]
4893 [ + - + - ]: 21 : vu_transport->next_pg = next_tgroup;
4894 : 7 : }
4895 [ + + + - ]: 28 : pthread_mutex_unlock(&vu_transport->pg_lock);
4896 : :
4897 : 28 : free(vu_group);
4898 : 28 : }
4899 : :
4900 : : static void
4901 : 119 : _vfio_user_qpair_disconnect(void *ctx)
4902 : : {
4903 : 119 : struct nvmf_vfio_user_sq *sq = ctx;
4904 : :
4905 [ + - ]: 119 : spdk_nvmf_qpair_disconnect(&sq->qpair);
4906 : 119 : }
4907 : :
4908 : : /* The function is used when socket connection is destroyed */
4909 : : static int
4910 : 119 : vfio_user_destroy_ctrlr(struct nvmf_vfio_user_ctrlr *ctrlr)
4911 : : {
4912 : : struct nvmf_vfio_user_sq *sq;
4913 : : struct nvmf_vfio_user_endpoint *endpoint;
4914 : :
4915 [ + + + + : 119 : SPDK_DEBUGLOG(nvmf_vfio, "%s stop processing\n", ctrlr_id(ctrlr));
+ - ]
4916 : :
4917 [ + - + - ]: 119 : endpoint = ctrlr->endpoint;
4918 [ + + # # ]: 119 : assert(endpoint != NULL);
4919 : :
4920 [ + + + - ]: 119 : pthread_mutex_lock(&endpoint->lock);
4921 [ + - + - ]: 119 : endpoint->need_relisten = true;
4922 [ + - + - ]: 119 : ctrlr->disconnect = true;
4923 [ + + + - : 119 : if (TAILQ_EMPTY(&ctrlr->connected_sqs)) {
+ - + - ]
4924 [ # # # # ]: 0 : endpoint->ctrlr = NULL;
4925 : 0 : free_ctrlr(ctrlr);
4926 [ # # # # ]: 0 : pthread_mutex_unlock(&endpoint->lock);
4927 : 0 : return 0;
4928 : : }
4929 : :
4930 [ + + + - : 238 : TAILQ_FOREACH(sq, &ctrlr->connected_sqs, tailq) {
+ - + + +
- + - +
- ]
4931 : : /* add another round thread poll to avoid recursive endpoint lock */
4932 [ + - + - ]: 119 : spdk_thread_send_msg(ctrlr->thread, _vfio_user_qpair_disconnect, sq);
4933 : 80 : }
4934 [ + + + - ]: 119 : pthread_mutex_unlock(&endpoint->lock);
4935 : :
4936 : 119 : return 0;
4937 : 80 : }
4938 : :
4939 : : /*
4940 : : * Poll for and process any incoming vfio-user messages.
4941 : : */
4942 : : static int
4943 : 331750 : vfio_user_poll_vfu_ctx(void *ctx)
4944 : : {
4945 : 331750 : struct nvmf_vfio_user_ctrlr *ctrlr = ctx;
4946 : : int ret;
4947 : :
4948 [ + + # # ]: 331750 : assert(ctrlr != NULL);
4949 : :
4950 : : /* This will call access_bar0_fn() if there are any writes
4951 : : * to the portion of the BAR that is not mmap'd */
4952 [ + - + - : 331750 : ret = vfu_run_ctx(ctrlr->endpoint->vfu_ctx);
+ - + - ]
4953 [ + + ]: 331750 : if (spdk_unlikely(ret == -1)) {
4954 [ + + - + ]: 376 : if (errno == EBUSY) {
4955 : 257 : return SPDK_POLLER_IDLE;
4956 : : }
4957 : :
4958 [ + - ]: 119 : spdk_poller_unregister(&ctrlr->vfu_ctx_poller);
4959 : :
4960 : : /*
4961 : : * We lost the client; the reset callback will already have
4962 : : * unregistered the interrupt.
4963 : : */
4964 [ + - + - ]: 119 : if (errno == ENOTCONN) {
4965 : 119 : vfio_user_destroy_ctrlr(ctrlr);
4966 : 119 : return SPDK_POLLER_BUSY;
4967 : : }
4968 : :
4969 : : /*
4970 : : * We might not have got a reset callback in this case, so
4971 : : * explicitly unregister the interrupt here.
4972 : : */
4973 [ # # ]: 0 : spdk_interrupt_unregister(&ctrlr->intr);
4974 [ # # # # ]: 0 : ctrlr->intr_fd = -1;
4975 : 0 : fail_ctrlr(ctrlr);
4976 : 0 : }
4977 : :
4978 : 331374 : return ret != 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
4979 : 24531 : }
4980 : :
4981 : : struct vfio_user_post_cpl_ctx {
4982 : : struct nvmf_vfio_user_ctrlr *ctrlr;
4983 : : struct nvmf_vfio_user_cq *cq;
4984 : : struct spdk_nvme_cpl cpl;
4985 : : };
4986 : :
4987 : : static void
4988 : 131 : _post_completion_msg(void *ctx)
4989 : : {
4990 : 131 : struct vfio_user_post_cpl_ctx *cpl_ctx = ctx;
4991 : :
4992 [ # # # # : 131 : post_completion(cpl_ctx->ctrlr, cpl_ctx->cq, cpl_ctx->cpl.cdw0, cpl_ctx->cpl.sqid,
# # # # #
# # # # #
# # # # #
# ]
4993 [ # # # # : 131 : cpl_ctx->cpl.cid, cpl_ctx->cpl.status.sc, cpl_ctx->cpl.status.sct);
# # # # #
# # # # #
# # # # #
# # # ]
4994 : 131 : free(cpl_ctx);
4995 : 131 : }
4996 : :
4997 : : static int nvmf_vfio_user_poll_group_poll(struct spdk_nvmf_transport_poll_group *group);
4998 : :
4999 : : static int
5000 : 0 : vfio_user_poll_group_process(void *ctx)
5001 : : {
5002 : 0 : struct nvmf_vfio_user_poll_group *vu_group = ctx;
5003 : 0 : int ret = 0;
5004 : :
5005 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "pg:%p got intr\n", vu_group);
# # ]
5006 : :
5007 [ # # ]: 0 : ret |= nvmf_vfio_user_poll_group_poll(&vu_group->group);
5008 : :
5009 : : /*
5010 : : * Re-arm the event indexes. NB: this also could rearm other
5011 : : * controller's SQs.
5012 : : */
5013 : 0 : ret |= vfio_user_poll_group_rearm(vu_group);
5014 : :
5015 [ # # # # ]: 0 : vu_group->stats.pg_process_count++;
5016 : 0 : return ret != 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
5017 : : }
5018 : :
5019 : : static int
5020 : 0 : vfio_user_poll_group_intr(void *ctx)
5021 : : {
5022 : 0 : struct nvmf_vfio_user_poll_group *vu_group = ctx;
5023 : 0 : eventfd_t val;
5024 : :
5025 [ # # # # ]: 0 : eventfd_read(vu_group->intr_fd, &val);
5026 : :
5027 [ # # # # ]: 0 : vu_group->stats.intr++;
5028 : :
5029 : 0 : return vfio_user_poll_group_process(ctx);
5030 : : }
5031 : :
5032 : : /*
5033 : : * Handle an interrupt for the given controller: we must poll the vfu_ctx, and
5034 : : * the SQs assigned to our own poll group. Other poll groups are handled via
5035 : : * vfio_user_poll_group_intr().
5036 : : */
5037 : : static int
5038 : 0 : vfio_user_ctrlr_intr(void *ctx)
5039 : : {
5040 : : struct nvmf_vfio_user_poll_group *vu_ctrlr_group;
5041 : 0 : struct nvmf_vfio_user_ctrlr *vu_ctrlr = ctx;
5042 : : struct nvmf_vfio_user_poll_group *vu_group;
5043 : 0 : int ret = SPDK_POLLER_IDLE;
5044 : :
5045 : 0 : vu_ctrlr_group = ctrlr_to_poll_group(vu_ctrlr);
5046 : :
5047 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "ctrlr pg:%p got intr\n", vu_ctrlr_group);
# # ]
5048 : :
5049 [ # # # # ]: 0 : vu_ctrlr_group->stats.ctrlr_intr++;
5050 : :
5051 : : /*
5052 : : * Poll vfio-user for this controller. We need to do this before polling
5053 : : * any SQs, as this is where doorbell writes may be handled.
5054 : : */
5055 : 0 : ret = vfio_user_poll_vfu_ctx(vu_ctrlr);
5056 : :
5057 : : /*
5058 : : * `sqs[0]` could be set to NULL in vfio_user_poll_vfu_ctx() context,
5059 : : * just return for this case.
5060 : : */
5061 [ # # # # : 0 : if (vu_ctrlr->sqs[0] == NULL) {
# # # # #
# ]
5062 : 0 : return ret;
5063 : : }
5064 : :
5065 [ # # # # : 0 : if (vu_ctrlr->transport->transport_opts.enable_intr_mode_sq_spreading) {
# # # # #
# # # #
# ]
5066 : : /*
5067 : : * We may have just written to a doorbell owned by another
5068 : : * reactor: we need to prod them to make sure its SQs are polled
5069 : : * *after* the doorbell value is updated.
5070 : : */
5071 [ # # # # : 0 : TAILQ_FOREACH(vu_group, &vu_ctrlr->transport->poll_groups, link) {
# # # # #
# # # # #
# # # # ]
5072 [ # # ]: 0 : if (vu_group != vu_ctrlr_group) {
5073 [ # # # # : 0 : SPDK_DEBUGLOG(vfio_user_db, "prodding pg:%p\n", vu_group);
# # ]
5074 [ # # # # ]: 0 : eventfd_write(vu_group->intr_fd, 1);
5075 : 0 : }
5076 : 0 : }
5077 : 0 : }
5078 : :
5079 : 0 : ret |= vfio_user_poll_group_process(vu_ctrlr_group);
5080 : :
5081 : 0 : return ret;
5082 : 0 : }
5083 : :
5084 : : static void
5085 : 0 : vfio_user_ctrlr_set_intr_mode(struct spdk_poller *poller, void *ctx,
5086 : : bool interrupt_mode)
5087 : : {
5088 : 0 : struct nvmf_vfio_user_ctrlr *ctrlr = ctx;
5089 [ # # # # ]: 0 : assert(ctrlr != NULL);
5090 [ # # # # : 0 : assert(ctrlr->endpoint != NULL);
# # # # ]
5091 : :
5092 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: setting interrupt mode to %d\n",
# # # # ]
5093 : : ctrlr_id(ctrlr), interrupt_mode);
5094 : :
5095 : : /*
5096 : : * interrupt_mode needs to persist across controller resets, so store
5097 : : * it in the endpoint instead.
5098 : : */
5099 [ # # # # : 0 : ctrlr->endpoint->interrupt_mode = interrupt_mode;
# # # # #
# ]
5100 : :
5101 : 0 : vfio_user_poll_group_rearm(ctrlr_to_poll_group(ctrlr));
5102 : 0 : }
5103 : :
5104 : : /*
5105 : : * In response to the nvmf_vfio_user_create_ctrlr() path, the admin queue is now
5106 : : * set up and we can start operating on this controller.
5107 : : */
5108 : : static void
5109 : 127 : start_ctrlr(struct nvmf_vfio_user_ctrlr *vu_ctrlr,
5110 : : struct spdk_nvmf_ctrlr *ctrlr)
5111 : : {
5112 [ + - + - ]: 127 : struct nvmf_vfio_user_endpoint *endpoint = vu_ctrlr->endpoint;
5113 : :
5114 [ + - + - ]: 127 : vu_ctrlr->ctrlr = ctrlr;
5115 [ + - + - : 127 : vu_ctrlr->cntlid = ctrlr->cntlid;
+ - + - ]
5116 [ + - + - ]: 127 : vu_ctrlr->thread = spdk_get_thread();
5117 [ + - + - ]: 127 : vu_ctrlr->state = VFIO_USER_CTRLR_RUNNING;
5118 : :
5119 [ + - + - : 127 : if (!in_interrupt_mode(endpoint->transport)) {
- + ]
5120 [ + - + - ]: 127 : vu_ctrlr->vfu_ctx_poller = SPDK_POLLER_REGISTER(vfio_user_poll_vfu_ctx,
5121 : : vu_ctrlr, 1000);
5122 : 127 : return;
5123 : : }
5124 : :
5125 [ # # # # ]: 0 : vu_ctrlr->vfu_ctx_poller = SPDK_POLLER_REGISTER(vfio_user_poll_vfu_ctx,
5126 : : vu_ctrlr, 0);
5127 : :
5128 [ # # # # : 0 : vu_ctrlr->intr_fd = vfu_get_poll_fd(vu_ctrlr->endpoint->vfu_ctx);
# # # # #
# # # ]
5129 [ # # # # : 0 : assert(vu_ctrlr->intr_fd != -1);
# # # # ]
5130 : :
5131 [ # # # # : 0 : vu_ctrlr->intr = SPDK_INTERRUPT_REGISTER(vu_ctrlr->intr_fd,
# # # # ]
5132 : : vfio_user_ctrlr_intr, vu_ctrlr);
5133 : :
5134 [ # # # # : 0 : assert(vu_ctrlr->intr != NULL);
# # # # ]
5135 : :
5136 [ # # # # ]: 0 : spdk_poller_register_interrupt(vu_ctrlr->vfu_ctx_poller,
5137 : : vfio_user_ctrlr_set_intr_mode,
5138 : 0 : vu_ctrlr);
5139 : 80 : }
5140 : :
5141 : : static int
5142 : 324 : handle_queue_connect_rsp(struct nvmf_vfio_user_req *req, void *cb_arg)
5143 : : {
5144 : : struct nvmf_vfio_user_poll_group *vu_group;
5145 : 324 : struct nvmf_vfio_user_sq *sq = cb_arg;
5146 : : struct nvmf_vfio_user_cq *admin_cq;
5147 : : struct nvmf_vfio_user_ctrlr *vu_ctrlr;
5148 : : struct nvmf_vfio_user_endpoint *endpoint;
5149 : :
5150 [ + + # # ]: 324 : assert(sq != NULL);
5151 [ + + # # ]: 324 : assert(req != NULL);
5152 : :
5153 [ + - + - ]: 324 : vu_ctrlr = sq->ctrlr;
5154 [ + + # # ]: 324 : assert(vu_ctrlr != NULL);
5155 [ + - + - ]: 324 : endpoint = vu_ctrlr->endpoint;
5156 [ + + # # ]: 324 : assert(endpoint != NULL);
5157 : :
5158 [ + - + + : 324 : if (spdk_nvme_cpl_is_error(&req->req.rsp->nvme_cpl)) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - + ]
5159 [ # # # # : 0 : SPDK_ERRLOG("SC %u, SCT %u\n", req->req.rsp->nvme_cpl.status.sc, req->req.rsp->nvme_cpl.status.sct);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
5160 [ # # # # ]: 0 : endpoint->ctrlr = NULL;
5161 : 0 : free_ctrlr(vu_ctrlr);
5162 : 0 : return -1;
5163 : : }
5164 : :
5165 [ + - + - ]: 324 : vu_group = SPDK_CONTAINEROF(sq->group, struct nvmf_vfio_user_poll_group, group);
5166 [ + - + - : 324 : TAILQ_INSERT_TAIL(&vu_group->sqs, sq, link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
5167 : :
5168 [ + - + - : 324 : admin_cq = vu_ctrlr->cqs[0];
+ - + - ]
5169 [ + + # # ]: 324 : assert(admin_cq != NULL);
5170 [ + + + - : 324 : assert(admin_cq->group != NULL);
+ - # # ]
5171 [ + + + - : 324 : assert(admin_cq->group->group->thread != NULL);
+ - + - +
- + - + -
# # ]
5172 : :
5173 [ + + + - ]: 324 : pthread_mutex_lock(&endpoint->lock);
5174 [ + + + + ]: 324 : if (nvmf_qpair_is_admin_queue(&sq->qpair)) {
5175 [ + + + - : 127 : assert(admin_cq->group->group->thread == spdk_get_thread());
+ - + - +
- + - + -
# # ]
5176 : : /*
5177 : : * The admin queue is special as SQ0 and CQ0 are created
5178 : : * together.
5179 : : */
5180 [ + - + - ]: 127 : admin_cq->cq_ref = 1;
5181 [ + - + - : 127 : start_ctrlr(vu_ctrlr, sq->qpair.ctrlr);
+ - ]
5182 : 80 : } else {
5183 : : /* For I/O queues this command was generated in response to an
5184 : : * ADMIN I/O CREATE SUBMISSION QUEUE command which has not yet
5185 : : * been completed. Complete it now.
5186 : : */
5187 [ + + + - : 197 : if (sq->post_create_io_sq_completion) {
+ - + - ]
5188 [ + + + - : 197 : if (admin_cq->group->group->thread != spdk_get_thread()) {
+ - + - +
- + - -
+ ]
5189 : : struct vfio_user_post_cpl_ctx *cpl_ctx;
5190 : :
5191 : 131 : cpl_ctx = calloc(1, sizeof(*cpl_ctx));
5192 [ - + ]: 131 : if (!cpl_ctx) {
5193 : 0 : return -ENOMEM;
5194 : : }
5195 [ # # # # ]: 131 : cpl_ctx->ctrlr = vu_ctrlr;
5196 [ # # # # ]: 131 : cpl_ctx->cq = admin_cq;
5197 [ # # # # : 131 : cpl_ctx->cpl.sqid = 0;
# # ]
5198 [ # # # # : 131 : cpl_ctx->cpl.cdw0 = 0;
# # ]
5199 [ # # # # : 131 : cpl_ctx->cpl.cid = sq->create_io_sq_cmd.cid;
# # # # #
# # # ]
5200 [ # # # # : 131 : cpl_ctx->cpl.status.sc = SPDK_NVME_SC_SUCCESS;
# # # # ]
5201 [ # # # # : 131 : cpl_ctx->cpl.status.sct = SPDK_NVME_SCT_GENERIC;
# # # # ]
5202 : :
5203 [ # # # # : 131 : spdk_thread_send_msg(admin_cq->group->group->thread,
# # # # #
# # # ]
5204 : : _post_completion_msg,
5205 : 0 : cpl_ctx);
5206 : 0 : } else {
5207 : 73 : post_completion(vu_ctrlr, admin_cq, 0, 0,
5208 [ + - + - : 66 : sq->create_io_sq_cmd.cid, SPDK_NVME_SC_SUCCESS, SPDK_NVME_SCT_GENERIC);
+ - ]
5209 : : }
5210 [ + - + - ]: 197 : sq->post_create_io_sq_completion = false;
5211 [ # # # # : 7 : } else if (in_interrupt_mode(endpoint->transport)) {
# # ]
5212 : : /*
5213 : : * If we're live migrating a guest, there is a window
5214 : : * where the I/O queues haven't been set up but the
5215 : : * device is in running state, during which the guest
5216 : : * might write to a doorbell. This doorbell write will
5217 : : * go unnoticed, so let's poll the whole controller to
5218 : : * pick that up.
5219 : : */
5220 : 0 : ctrlr_kick(vu_ctrlr);
5221 : 0 : }
5222 [ + - + - ]: 197 : sq->sq_state = VFIO_USER_SQ_ACTIVE;
5223 : : }
5224 : :
5225 [ + - + - : 324 : TAILQ_INSERT_TAIL(&vu_ctrlr->connected_sqs, sq, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
5226 [ + + + - ]: 324 : pthread_mutex_unlock(&endpoint->lock);
5227 : :
5228 [ + - + - : 324 : free(req->req.iov[0].iov_base);
+ - + - +
- + - ]
5229 [ + - + - : 324 : req->req.iov[0].iov_base = NULL;
+ - + - +
- + - ]
5230 [ + - + - : 324 : req->req.iovcnt = 0;
+ - ]
5231 : :
5232 : 324 : return 0;
5233 : 87 : }
5234 : :
5235 : : static void
5236 : 324 : _nvmf_vfio_user_poll_group_add(void *req)
5237 : : {
5238 : 324 : spdk_nvmf_request_exec(req);
5239 : 324 : }
5240 : :
5241 : : /*
5242 : : * Add the given qpair to the given poll group. New qpairs are added via
5243 : : * spdk_nvmf_tgt_new_qpair(), which picks a poll group via
5244 : : * nvmf_vfio_user_get_optimal_poll_group(), then calls back here via
5245 : : * nvmf_transport_poll_group_add().
5246 : : */
5247 : : static int
5248 : 324 : nvmf_vfio_user_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
5249 : : struct spdk_nvmf_qpair *qpair)
5250 : : {
5251 : : struct nvmf_vfio_user_sq *sq;
5252 : : struct nvmf_vfio_user_req *vu_req;
5253 : : struct nvmf_vfio_user_ctrlr *ctrlr;
5254 : : struct spdk_nvmf_request *req;
5255 : : struct spdk_nvmf_fabric_connect_data *data;
5256 : : bool admin;
5257 : :
5258 : 324 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
5259 [ + - + - ]: 324 : sq->group = group;
5260 [ + - + - ]: 324 : ctrlr = sq->ctrlr;
5261 : :
5262 [ + + + + : 324 : SPDK_DEBUGLOG(nvmf_vfio, "%s: add QP%d=%p(%p) to poll_group=%p\n",
+ - # # #
# # # ]
5263 : : ctrlr_id(ctrlr), sq->qpair.qid,
5264 : : sq, qpair, group);
5265 : :
5266 [ + - ]: 324 : admin = nvmf_qpair_is_admin_queue(&sq->qpair);
5267 : :
5268 : 324 : vu_req = get_nvmf_vfio_user_req(sq);
5269 [ + + ]: 324 : if (vu_req == NULL) {
5270 : 0 : return -1;
5271 : : }
5272 : :
5273 [ + - ]: 324 : req = &vu_req->req;
5274 [ + - + - : 324 : req->cmd->connect_cmd.opcode = SPDK_NVME_OPC_FABRIC;
+ - + - +
- ]
5275 [ + - + - : 324 : req->cmd->connect_cmd.cid = 0;
+ - + - +
- ]
5276 [ + - + - : 324 : req->cmd->connect_cmd.fctype = SPDK_NVMF_FABRIC_COMMAND_CONNECT;
+ - + - +
- ]
5277 [ + - + - : 324 : req->cmd->connect_cmd.recfmt = 0;
+ - + - +
- ]
5278 [ + - + - : 324 : req->cmd->connect_cmd.sqsize = sq->size - 1;
+ - + - +
- + - +
- ]
5279 [ + + + + : 324 : req->cmd->connect_cmd.qid = admin ? 0 : qpair->qid;
+ - + - +
- + - + -
+ - + - ]
5280 : :
5281 [ + - + - ]: 324 : req->length = sizeof(struct spdk_nvmf_fabric_connect_data);
5282 : :
5283 [ + - + - ]: 324 : data = calloc(1, req->length);
5284 [ + + ]: 324 : if (data == NULL) {
5285 : 0 : nvmf_vfio_user_req_free(req);
5286 : 0 : return -ENOMEM;
5287 : : }
5288 : :
5289 [ + - + - : 324 : SPDK_IOV_ONE(req->iov, &req->iovcnt, data, req->length);
+ - + - +
- + - + -
+ - + - +
- ]
5290 : :
5291 [ + - + - : 324 : data->cntlid = ctrlr->cntlid;
+ - + - ]
5292 [ + + ]: 411 : snprintf(data->subnqn, sizeof(data->subnqn), "%s",
5293 [ + - + - : 324 : spdk_nvmf_subsystem_get_nqn(ctrlr->endpoint->subsystem));
+ - + - ]
5294 : :
5295 [ + - + - ]: 324 : vu_req->cb_fn = handle_queue_connect_rsp;
5296 [ + - + - ]: 324 : vu_req->cb_arg = sq;
5297 : :
5298 [ + + + + : 324 : SPDK_DEBUGLOG(nvmf_vfio,
+ - # # #
# # # #
# ]
5299 : : "%s: sending connect fabrics command for qid:%#x cntlid=%#x\n",
5300 : : ctrlr_id(ctrlr), qpair->qid, data->cntlid);
5301 : :
5302 : : /*
5303 : : * By the time transport's poll_group_add() callback is executed, the
5304 : : * qpair isn't in the ACTIVE state yet, so spdk_nvmf_request_exec()
5305 : : * would fail. The state changes to ACTIVE immediately after the
5306 : : * callback finishes, so delay spdk_nvmf_request_exec() by sending a
5307 : : * message.
5308 : : */
5309 : 324 : spdk_thread_send_msg(spdk_get_thread(), _nvmf_vfio_user_poll_group_add, req);
5310 : 324 : return 0;
5311 : 87 : }
5312 : :
5313 : : static int
5314 : 324 : nvmf_vfio_user_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
5315 : : struct spdk_nvmf_qpair *qpair)
5316 : : {
5317 : : struct nvmf_vfio_user_sq *sq;
5318 : : struct nvmf_vfio_user_poll_group *vu_group;
5319 : :
5320 : 324 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
5321 : :
5322 [ + + + + : 324 : SPDK_DEBUGLOG(nvmf_vfio,
+ - # # #
# # # #
# ]
5323 : : "%s: remove NVMf QP%d=%p from NVMf poll_group=%p\n",
5324 : : ctrlr_id(sq->ctrlr), qpair->qid, qpair, group);
5325 : :
5326 : :
5327 : 324 : vu_group = SPDK_CONTAINEROF(group, struct nvmf_vfio_user_poll_group, group);
5328 [ + + + - : 324 : TAILQ_REMOVE(&vu_group->sqs, sq, link);
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
5329 : :
5330 : 324 : return 0;
5331 : : }
5332 : :
5333 : : static void
5334 : 3185463 : _nvmf_vfio_user_req_free(struct nvmf_vfio_user_sq *sq, struct nvmf_vfio_user_req *vu_req)
5335 : : {
5336 [ + + + - ]: 3185463 : memset(&vu_req->cmd, 0, sizeof(vu_req->cmd));
5337 [ + + + - ]: 3185463 : memset(&vu_req->rsp, 0, sizeof(vu_req->rsp));
5338 [ + - + - ]: 3185463 : vu_req->iovcnt = 0;
5339 [ + - + - : 3185463 : vu_req->req.iovcnt = 0;
+ - ]
5340 [ + - + - : 3185463 : vu_req->req.length = 0;
+ - ]
5341 [ + - + - ]: 3185463 : vu_req->state = VFIO_USER_REQUEST_STATE_FREE;
5342 : :
5343 [ + - + - : 3185463 : TAILQ_INSERT_TAIL(&sq->free_reqs, vu_req, link);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
5344 : 3185463 : }
5345 : :
5346 : : static int
5347 : 8 : nvmf_vfio_user_req_free(struct spdk_nvmf_request *req)
5348 : : {
5349 : : struct nvmf_vfio_user_sq *sq;
5350 : : struct nvmf_vfio_user_req *vu_req;
5351 : :
5352 [ - + # # ]: 8 : assert(req != NULL);
5353 : :
5354 : 8 : vu_req = SPDK_CONTAINEROF(req, struct nvmf_vfio_user_req, req);
5355 [ # # # # ]: 8 : sq = SPDK_CONTAINEROF(req->qpair, struct nvmf_vfio_user_sq, qpair);
5356 : :
5357 : 8 : _nvmf_vfio_user_req_free(sq, vu_req);
5358 : :
5359 : 8 : return 0;
5360 : : }
5361 : :
5362 : : static int
5363 : 2300398 : nvmf_vfio_user_req_complete(struct spdk_nvmf_request *req)
5364 : : {
5365 : : struct nvmf_vfio_user_sq *sq;
5366 : : struct nvmf_vfio_user_req *vu_req;
5367 : :
5368 [ + + # # ]: 2300398 : assert(req != NULL);
5369 : :
5370 : 2300398 : vu_req = SPDK_CONTAINEROF(req, struct nvmf_vfio_user_req, req);
5371 [ + - + - ]: 2300398 : sq = SPDK_CONTAINEROF(req->qpair, struct nvmf_vfio_user_sq, qpair);
5372 : :
5373 [ + - + - : 2300398 : if (vu_req->cb_fn != NULL) {
- + ]
5374 [ + + + - : 2300398 : if (vu_req->cb_fn(vu_req, vu_req->cb_arg) != 0) {
- + + - +
- + - +
- ]
5375 [ # # # # ]: 0 : fail_ctrlr(sq->ctrlr);
5376 : 0 : }
5377 : 149825 : }
5378 : :
5379 : 2300398 : _nvmf_vfio_user_req_free(sq, vu_req);
5380 : :
5381 : 2300398 : return 0;
5382 : : }
5383 : :
5384 : : static void
5385 : 324 : nvmf_vfio_user_close_qpair(struct spdk_nvmf_qpair *qpair,
5386 : : spdk_nvmf_transport_qpair_fini_cb cb_fn, void *cb_arg)
5387 : : {
5388 : : struct nvmf_vfio_user_sq *sq;
5389 : : struct nvmf_vfio_user_ctrlr *vu_ctrlr;
5390 : : struct nvmf_vfio_user_endpoint *endpoint;
5391 : : struct vfio_user_delete_sq_ctx *del_ctx;
5392 : :
5393 [ + + # # ]: 324 : assert(qpair != NULL);
5394 : 324 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
5395 [ + - + - ]: 324 : vu_ctrlr = sq->ctrlr;
5396 [ + - + - ]: 324 : endpoint = vu_ctrlr->endpoint;
5397 [ + - + - ]: 324 : del_ctx = sq->delete_ctx;
5398 [ + - + - ]: 324 : sq->delete_ctx = NULL;
5399 : :
5400 [ + + + - ]: 324 : pthread_mutex_lock(&endpoint->lock);
5401 [ + + + - : 324 : TAILQ_REMOVE(&vu_ctrlr->connected_sqs, sq, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
5402 : 324 : delete_sq_done(vu_ctrlr, sq);
5403 [ + + + - : 324 : if (TAILQ_EMPTY(&vu_ctrlr->connected_sqs)) {
+ - + + ]
5404 [ + - + - ]: 127 : endpoint->ctrlr = NULL;
5405 [ + + + + : 127 : if (vu_ctrlr->in_source_vm && endpoint->need_resume) {
+ - - + #
# # # # #
# # ]
5406 : : /* The controller will be freed, we can resume the subsystem
5407 : : * now so that the endpoint can be ready to accept another
5408 : : * new connection.
5409 : : */
5410 [ # # # # ]: 0 : spdk_nvmf_subsystem_resume((struct spdk_nvmf_subsystem *)endpoint->subsystem,
5411 : 0 : vfio_user_endpoint_resume_done, endpoint);
5412 : 0 : }
5413 : 127 : free_ctrlr(vu_ctrlr);
5414 : 80 : }
5415 [ + + + - ]: 324 : pthread_mutex_unlock(&endpoint->lock);
5416 : :
5417 [ + + ]: 324 : if (del_ctx) {
5418 : 176 : vfio_user_qpair_delete_cb(del_ctx);
5419 : 7 : }
5420 : :
5421 [ + + ]: 324 : if (cb_fn) {
5422 [ - + + - ]: 324 : cb_fn(cb_arg);
5423 : 87 : }
5424 : 324 : }
5425 : :
5426 : : /**
5427 : : * Returns a preallocated request, or NULL if there isn't one available.
5428 : : */
5429 : : static struct nvmf_vfio_user_req *
5430 : 3185463 : get_nvmf_vfio_user_req(struct nvmf_vfio_user_sq *sq)
5431 : : {
5432 : : struct nvmf_vfio_user_req *req;
5433 : :
5434 [ + + ]: 3185463 : if (sq == NULL) {
5435 : 0 : return NULL;
5436 : : }
5437 : :
5438 [ + - + - : 3185463 : req = TAILQ_FIRST(&sq->free_reqs);
+ - ]
5439 [ + + ]: 3185463 : if (req == NULL) {
5440 : 0 : return NULL;
5441 : : }
5442 : :
5443 [ + - + - : 3185463 : TAILQ_REMOVE(&sq->free_reqs, req, link);
+ - - + +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # +
- + - + -
+ - + - +
- + - ]
5444 : :
5445 : 3185463 : return req;
5446 : 149825 : }
5447 : :
5448 : : static int
5449 : 2705065 : get_nvmf_io_req_length(struct spdk_nvmf_request *req)
5450 : : {
5451 : : uint16_t nr;
5452 : : uint32_t nlb, nsid;
5453 [ + - + - : 2705065 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
+ - ]
5454 [ + - + - : 2705065 : struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr;
+ - + - ]
5455 : : struct spdk_nvmf_ns *ns;
5456 : :
5457 [ + - + - ]: 2705065 : nsid = cmd->nsid;
5458 [ + - + - ]: 2705065 : ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid);
5459 [ + - + + : 2705065 : if (ns == NULL || ns->bdev == NULL) {
+ - - + ]
5460 [ # # # # ]: 0 : SPDK_ERRLOG("unsuccessful query for nsid %u\n", cmd->nsid);
5461 : 0 : return -EINVAL;
5462 : : }
5463 : :
5464 [ + + - + ]: 2705065 : if (cmd->opc == SPDK_NVME_OPC_DATASET_MANAGEMENT) {
5465 [ # # # # : 4607 : nr = cmd->cdw10_bits.dsm.nr + 1;
# # # # #
# ]
5466 : 4607 : return nr * sizeof(struct spdk_nvme_dsm_range);
5467 : : }
5468 : :
5469 [ + + - + ]: 2700458 : if (cmd->opc == SPDK_NVME_OPC_COPY) {
5470 [ # # # # : 4589 : nr = (cmd->cdw12 & 0x000000ffu) + 1;
# # ]
5471 : 4589 : return nr * sizeof(struct spdk_nvme_scc_source_range);
5472 : : }
5473 : :
5474 [ + - + - : 2695869 : nlb = (cmd->cdw12 & 0x0000ffffu) + 1;
+ - ]
5475 [ + - + - ]: 2695869 : return nlb * spdk_bdev_desc_get_block_size(ns->desc);
5476 : 149542 : }
5477 : :
5478 : : static int
5479 : 159948 : map_admin_cmd_req(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvmf_request *req)
5480 : : {
5481 [ + - + - : 159948 : struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
+ - ]
5482 : 159948 : uint32_t len = 0, numdw = 0;
5483 : : uint8_t fid;
5484 : : int iovcnt;
5485 : :
5486 [ + - + - : 159948 : req->xfer = spdk_nvme_opc_get_data_transfer(cmd->opc);
+ - ]
5487 : :
5488 [ + + + - : 159948 : if (req->xfer == SPDK_NVME_DATA_NONE) {
+ + ]
5489 : 36021 : return 0;
5490 : : }
5491 : :
5492 [ + + + + : 123927 : switch (cmd->opc) {
+ + - ]
5493 : 999 : case SPDK_NVME_OPC_IDENTIFY:
5494 : 1027 : len = 4096;
5495 : 1027 : break;
5496 : 643 : case SPDK_NVME_OPC_GET_LOG_PAGE:
5497 [ # # # # : 643 : numdw = ((((uint32_t)cmd->cdw11_bits.get_log_page.numdu << 16) |
# # # # #
# ]
5498 [ # # # # : 643 : cmd->cdw10_bits.get_log_page.numdl) + 1);
# # # # ]
5499 [ + + ]: 643 : if (numdw > UINT32_MAX / 4) {
5500 : 452 : return -EINVAL;
5501 : : }
5502 : 191 : len = numdw * 4;
5503 : 191 : break;
5504 : 13473 : case SPDK_NVME_OPC_GET_FEATURES:
5505 : : case SPDK_NVME_OPC_SET_FEATURES:
5506 [ + - + - : 13494 : fid = cmd->cdw10_bits.set_features.fid;
+ - + - ]
5507 [ + + + + : 13494 : switch (fid) {
+ + ]
5508 : 3 : case SPDK_NVME_FEAT_LBA_RANGE_TYPE:
5509 : 3 : len = 4096;
5510 : 3 : break;
5511 : 5 : case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION:
5512 : 5 : len = 256;
5513 : 5 : break;
5514 : 3 : case SPDK_NVME_FEAT_TIMESTAMP:
5515 : 3 : len = 8;
5516 : 3 : break;
5517 : 6 : case SPDK_NVME_FEAT_HOST_BEHAVIOR_SUPPORT:
5518 : 6 : len = 512;
5519 : 6 : break;
5520 : 3 : case SPDK_NVME_FEAT_HOST_IDENTIFIER:
5521 [ + + # # : 3 : if (cmd->cdw11_bits.feat_host_identifier.bits.exhid) {
# # # # #
# # # ]
5522 : 1 : len = 16;
5523 : 0 : } else {
5524 : 2 : len = 8;
5525 : : }
5526 : 3 : break;
5527 : 13453 : default:
5528 : 13474 : return 0;
5529 : : }
5530 : 20 : break;
5531 : 600 : case SPDK_NVME_OPC_FABRIC:
5532 : 600 : return -ENOTSUP;
5533 : 108163 : default:
5534 : 108163 : return 0;
5535 : : }
5536 : :
5537 : : /* ADMIN command will not use SGL */
5538 [ + + - + ]: 1238 : if (cmd->psdt != 0) {
5539 : 0 : return -EINVAL;
5540 : : }
5541 : :
5542 [ + - ]: 1238 : iovcnt = vfio_user_map_cmd(ctrlr, req, req->iov, len);
5543 [ + + ]: 1238 : if (iovcnt < 0) {
5544 [ # # ]: 759 : SPDK_ERRLOG("%s: map Admin Opc %x failed\n",
5545 : : ctrlr_id(ctrlr), cmd->opc);
5546 : 759 : return -1;
5547 : : }
5548 [ + - + - ]: 479 : req->length = len;
5549 [ + - + - ]: 479 : req->iovcnt = iovcnt;
5550 : :
5551 : 479 : return 0;
5552 : 77 : }
5553 : :
5554 : : /*
5555 : : * Map an I/O command's buffers.
5556 : : *
5557 : : * Returns 0 on success and -errno on failure.
5558 : : */
5559 : : static int
5560 : 3001119 : map_io_cmd_req(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvmf_request *req)
5561 : : {
5562 : : int len, iovcnt;
5563 : : struct spdk_nvme_cmd *cmd;
5564 : :
5565 [ + + # # ]: 3001119 : assert(ctrlr != NULL);
5566 [ + + # # ]: 3001119 : assert(req != NULL);
5567 : :
5568 [ + - + - : 3001119 : cmd = &req->cmd->nvme_cmd;
+ - ]
5569 [ + - + - : 3001119 : req->xfer = spdk_nvme_opc_get_data_transfer(cmd->opc);
+ - ]
5570 : :
5571 [ + + + - : 3001119 : if (spdk_unlikely(req->xfer == SPDK_NVME_DATA_NONE)) {
- + ]
5572 : 296054 : return 0;
5573 : : }
5574 : :
5575 : 2705065 : len = get_nvmf_io_req_length(req);
5576 [ + + ]: 2705065 : if (len < 0) {
5577 : 0 : return -EINVAL;
5578 : : }
5579 [ + - + - ]: 2705065 : req->length = len;
5580 : :
5581 [ + - + - : 2705065 : iovcnt = vfio_user_map_cmd(ctrlr, req, req->iov, req->length);
+ - ]
5582 [ + + ]: 2705065 : if (iovcnt < 0) {
5583 [ # # ]: 860122 : SPDK_ERRLOG("%s: failed to map IO OPC %u\n", ctrlr_id(ctrlr), cmd->opc);
5584 : 860122 : return -EFAULT;
5585 : : }
5586 [ + - + - ]: 1844943 : req->iovcnt = iovcnt;
5587 : :
5588 : 1844943 : return 0;
5589 : 149542 : }
5590 : :
5591 : : static int
5592 : 3184191 : handle_cmd_req(struct nvmf_vfio_user_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd,
5593 : : struct nvmf_vfio_user_sq *sq)
5594 : : {
5595 : : int err;
5596 : : struct nvmf_vfio_user_req *vu_req;
5597 : : struct spdk_nvmf_request *req;
5598 : :
5599 [ + + # # ]: 3184191 : assert(ctrlr != NULL);
5600 [ + + # # ]: 3184191 : assert(cmd != NULL);
5601 : :
5602 : 3184191 : vu_req = get_nvmf_vfio_user_req(sq);
5603 [ - + ]: 3184191 : if (spdk_unlikely(vu_req == NULL)) {
5604 [ # # ]: 0 : SPDK_ERRLOG("%s: no request for NVMe command opc 0x%x\n", ctrlr_id(ctrlr), cmd->opc);
5605 [ # # # # : 0 : return post_completion(ctrlr, ctrlr->cqs[sq->cqid], 0, 0, cmd->cid,
# # # # #
# # # # #
# # ]
5606 : : SPDK_NVME_SC_INTERNAL_DEVICE_ERROR, SPDK_NVME_SCT_GENERIC);
5607 : :
5608 : : }
5609 [ + - ]: 3184191 : req = &vu_req->req;
5610 : :
5611 [ + + + - : 3184191 : assert(req->qpair != NULL);
+ - # # ]
5612 [ + + + + : 3184191 : SPDK_DEBUGLOG(nvmf_vfio, "%s: handle sqid:%u, req opc=%#x cid=%d\n",
+ - # # #
# # # # #
# # # # #
# ]
5613 : : ctrlr_id(ctrlr), req->qpair->qid, cmd->opc, cmd->cid);
5614 : :
5615 [ + - + - ]: 3184191 : vu_req->cb_fn = handle_cmd_rsp;
5616 [ + - + - : 3184191 : vu_req->cb_arg = SPDK_CONTAINEROF(req->qpair, struct nvmf_vfio_user_sq, qpair);
+ - + - ]
5617 [ + - + - : 3184191 : req->cmd->nvme_cmd = *cmd;
+ - ]
5618 : :
5619 [ + + + - : 3184191 : if (nvmf_qpair_is_admin_queue(req->qpair)) {
+ + ]
5620 : 159948 : err = map_admin_cmd_req(ctrlr, req);
5621 : 77 : } else {
5622 [ + + + - ]: 3024243 : switch (cmd->opc) {
5623 : 23124 : case SPDK_NVME_OPC_RESERVATION_REGISTER:
5624 : : case SPDK_NVME_OPC_RESERVATION_REPORT:
5625 : : case SPDK_NVME_OPC_RESERVATION_ACQUIRE:
5626 : : case SPDK_NVME_OPC_RESERVATION_RELEASE:
5627 : : case SPDK_NVME_OPC_FABRIC:
5628 : 23124 : err = -ENOTSUP;
5629 : 23124 : break;
5630 : 2851577 : default:
5631 : 3001119 : err = map_io_cmd_req(ctrlr, req);
5632 : 3001119 : break;
5633 : : }
5634 : : }
5635 : :
5636 [ + + ]: 3184191 : if (spdk_unlikely(err < 0)) {
5637 [ # # ]: 885057 : SPDK_ERRLOG("%s: process NVMe command opc 0x%x failed\n",
5638 : : ctrlr_id(ctrlr), cmd->opc);
5639 [ # # # # : 885057 : req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
# # # # #
# # # ]
5640 [ + + # # : 885057 : req->rsp->nvme_cpl.status.sc = err == -ENOTSUP ?
# # # # #
# # # ]
5641 : : SPDK_NVME_SC_INVALID_OPCODE :
5642 : : SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
5643 [ # # # # ]: 885057 : err = handle_cmd_rsp(vu_req, vu_req->cb_arg);
5644 : 885057 : _nvmf_vfio_user_req_free(sq, vu_req);
5645 : 885057 : return err;
5646 : : }
5647 : :
5648 [ + - + - ]: 2299134 : vu_req->state = VFIO_USER_REQUEST_STATE_EXECUTING;
5649 : 2299134 : spdk_nvmf_request_exec(req);
5650 : :
5651 : 2299134 : return 0;
5652 : 149619 : }
5653 : :
5654 : : /*
5655 : : * If we suppressed an IRQ in post_completion(), check if it needs to be fired
5656 : : * here: if the host isn't up to date, and is apparently not actively processing
5657 : : * the queue (i.e. ->last_head isn't changing), we need an IRQ.
5658 : : */
5659 : : static void
5660 : 86492684 : handle_suppressed_irq(struct nvmf_vfio_user_ctrlr *ctrlr,
5661 : : struct nvmf_vfio_user_sq *sq)
5662 : : {
5663 [ + - + - : 86492684 : struct nvmf_vfio_user_cq *cq = ctrlr->cqs[sq->cqid];
+ - + - +
- + - ]
5664 : : uint32_t cq_head;
5665 : : uint32_t cq_tail;
5666 : :
5667 [ + + + + : 86492684 : if (!cq->ien || cq->qid == 0 || !ctrlr_interrupt_enabled(ctrlr)) {
+ + + + +
- + - - +
# # ]
5668 : 50537605 : return;
5669 : : }
5670 : :
5671 : 35955079 : cq_tail = *cq_tailp(cq);
5672 : :
5673 : : /* Already sent? */
5674 [ + + # # : 35955079 : if (cq_tail == cq->last_trigger_irq_tail) {
# # ]
5675 : 35924900 : return;
5676 : : }
5677 : :
5678 : : spdk_ivdt_dcache(cq_dbl_headp(cq));
5679 : 30179 : cq_head = *cq_dbl_headp(cq);
5680 : :
5681 [ + + + + : 30179 : if (cq_head != cq_tail && cq_head == cq->last_head) {
# # # # ]
5682 [ # # # # : 15600 : int err = vfu_irq_trigger(ctrlr->endpoint->vfu_ctx, cq->iv);
# # # # #
# # # ]
5683 [ - + ]: 15600 : if (err != 0) {
5684 : 0 : SPDK_ERRLOG("%s: failed to trigger interrupt: %m\n",
5685 : : ctrlr_id(ctrlr));
5686 : 0 : } else {
5687 [ # # # # ]: 15600 : cq->last_trigger_irq_tail = cq_tail;
5688 : : }
5689 : 0 : }
5690 : :
5691 [ # # # # ]: 30179 : cq->last_head = cq_head;
5692 : 840992 : }
5693 : :
5694 : : /* Returns the number of commands processed, or a negative value on error. */
5695 : : static int
5696 : 86971168 : nvmf_vfio_user_sq_poll(struct nvmf_vfio_user_sq *sq)
5697 : : {
5698 : : struct nvmf_vfio_user_ctrlr *ctrlr;
5699 : : uint32_t new_tail;
5700 : 86971168 : int count = 0;
5701 : :
5702 [ + + # # ]: 86971168 : assert(sq != NULL);
5703 : :
5704 [ + - + - ]: 86971168 : ctrlr = sq->ctrlr;
5705 : :
5706 : : /*
5707 : : * A quiesced, or migrating, controller should never process new
5708 : : * commands.
5709 : : */
5710 [ + + + - : 86971168 : if (ctrlr->state != VFIO_USER_CTRLR_RUNNING) {
- + ]
5711 : 478484 : return SPDK_POLLER_IDLE;
5712 : : }
5713 : :
5714 [ + + + - : 86492684 : if (ctrlr->adaptive_irqs_enabled) {
+ - - + ]
5715 : 86492684 : handle_suppressed_irq(ctrlr, sq);
5716 : 840992 : }
5717 : :
5718 : : /* On aarch64 platforms, doorbells update from guest VM may not be seen
5719 : : * on SPDK target side. This is because there is memory type mismatch
5720 : : * situation here. That is on guest VM side, the doorbells are treated as
5721 : : * device memory while on SPDK target side, it is treated as normal
5722 : : * memory. And this situation cause problem on ARM platform.
5723 : : * Refer to "https://developer.arm.com/documentation/102376/0100/
5724 : : * Memory-aliasing-and-mismatched-memory-types". Only using spdk_mb()
5725 : : * cannot fix this. Use "dc civac" to invalidate cache may solve
5726 : : * this.
5727 : : */
5728 : : spdk_ivdt_dcache(sq_dbl_tailp(sq));
5729 : :
5730 : : /* Load-Acquire. */
5731 : 86492684 : new_tail = *sq_dbl_tailp(sq);
5732 : :
5733 : 86492684 : new_tail = new_tail & 0xffffu;
5734 [ + + + - : 86492684 : if (spdk_unlikely(new_tail >= sq->size)) {
- + ]
5735 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio, "%s: invalid sqid:%u doorbell value %u\n", ctrlr_id(ctrlr), sq->qid,
# # # # #
# ]
5736 : : new_tail);
5737 [ # # # # ]: 0 : spdk_nvmf_ctrlr_async_event_error_event(ctrlr->ctrlr, SPDK_NVME_ASYNC_EVENT_INVALID_DB_WRITE);
5738 : :
5739 : 0 : return -1;
5740 : : }
5741 : :
5742 [ + + ]: 86492684 : if (*sq_headp(sq) == new_tail) {
5743 : 86222038 : return 0;
5744 : : }
5745 : :
5746 [ + + + + : 270646 : SPDK_DEBUGLOG(nvmf_vfio, "%s: sqid:%u doorbell old=%u new=%u\n",
+ - # # #
# ]
5747 : : ctrlr_id(ctrlr), sq->qid, *sq_headp(sq), new_tail);
5748 [ + + + - : 270646 : if (ctrlr->sdbl != NULL) {
+ - ]
5749 [ # # # # : 0 : SPDK_DEBUGLOG(nvmf_vfio,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
5750 : : "%s: sqid:%u bar0_doorbell=%u shadow_doorbell=%u eventidx=%u\n",
5751 : : ctrlr_id(ctrlr), sq->qid,
5752 : : ctrlr->bar0_doorbells[queue_index(sq->qid, false)],
5753 : : ctrlr->sdbl->shadow_doorbells[queue_index(sq->qid, false)],
5754 : : ctrlr->sdbl->eventidxs[queue_index(sq->qid, false)]);
5755 : 0 : }
5756 : :
5757 : : /*
5758 : : * Ensure that changes to the queue are visible to us.
5759 : : * The host driver should write the queue first, do a wmb(), and then
5760 : : * update the SQ tail doorbell (their Store-Release).
5761 : : */
5762 : 270646 : spdk_rmb();
5763 : :
5764 : 270646 : count = handle_sq_tdbl_write(ctrlr, new_tail, sq);
5765 [ + + ]: 270646 : if (spdk_unlikely(count < 0)) {
5766 : 0 : fail_ctrlr(ctrlr);
5767 : 0 : }
5768 : :
5769 : 270646 : return count;
5770 : 840992 : }
5771 : :
5772 : : /*
5773 : : * vfio-user transport poll handler. Note that the library context is polled in
5774 : : * a separate poller (->vfu_ctx_poller), so this poller only needs to poll the
5775 : : * active SQs.
5776 : : *
5777 : : * Returns the number of commands processed, or a negative value on error.
5778 : : */
5779 : : static int
5780 : 89057016 : nvmf_vfio_user_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
5781 : : {
5782 : : struct nvmf_vfio_user_poll_group *vu_group;
5783 : : struct nvmf_vfio_user_sq *sq, *tmp;
5784 : 89057016 : int count = 0;
5785 : :
5786 [ + + # # ]: 89057016 : assert(group != NULL);
5787 : :
5788 : 89057016 : vu_group = SPDK_CONTAINEROF(group, struct nvmf_vfio_user_poll_group, group);
5789 : :
5790 [ + + + + : 89057016 : SPDK_DEBUGLOG(vfio_user_db, "polling all SQs\n");
+ - ]
5791 : :
5792 [ + + + - : 177752789 : TAILQ_FOREACH_SAFE(sq, &vu_group->sqs, link, tmp) {
+ - + + +
- + - + -
+ + ]
5793 : : int ret;
5794 : :
5795 [ + + + + : 88695773 : if (spdk_unlikely(sq->sq_state != VFIO_USER_SQ_ACTIVE || !sq->size)) {
+ + + - +
- + + ]
5796 : 1724605 : continue;
5797 : : }
5798 : :
5799 : 86971168 : ret = nvmf_vfio_user_sq_poll(sq);
5800 : :
5801 [ - + ]: 86971168 : if (spdk_unlikely(ret < 0)) {
5802 : 0 : return ret;
5803 : : }
5804 : :
5805 [ + - ]: 86971168 : count += ret;
5806 : 840992 : }
5807 : :
5808 [ + - + - ]: 89057016 : vu_group->stats.polls++;
5809 [ + - + - : 89057016 : vu_group->stats.poll_reqs += count;
+ - ]
5810 [ + - + - : 89057016 : vu_group->stats.poll_reqs_squared += count * count;
+ - + - ]
5811 [ + + ]: 89057016 : if (count == 0) {
5812 [ + - + - ]: 88795769 : vu_group->stats.polls_spurious++;
5813 : 709345 : }
5814 : :
5815 [ + + + + : 89057016 : if (vu_group->need_kick) {
+ - + - ]
5816 : 0 : poll_group_kick(vu_group);
5817 : 0 : }
5818 : :
5819 : 89057016 : return count;
5820 : 858964 : }
5821 : :
5822 : : static int
5823 : 0 : nvmf_vfio_user_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
5824 : : struct spdk_nvme_transport_id *trid)
5825 : : {
5826 : : struct nvmf_vfio_user_sq *sq;
5827 : : struct nvmf_vfio_user_ctrlr *ctrlr;
5828 : :
5829 : 0 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
5830 [ # # # # ]: 0 : ctrlr = sq->ctrlr;
5831 : :
5832 [ # # # # : 0 : memcpy(trid, &ctrlr->endpoint->trid, sizeof(*trid));
# # # # #
# ]
5833 : 0 : return 0;
5834 : : }
5835 : :
5836 : : static int
5837 : 0 : nvmf_vfio_user_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
5838 : : struct spdk_nvme_transport_id *trid)
5839 : : {
5840 : 0 : return 0;
5841 : : }
5842 : :
5843 : : static int
5844 : 454 : nvmf_vfio_user_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
5845 : : struct spdk_nvme_transport_id *trid)
5846 : : {
5847 : : struct nvmf_vfio_user_sq *sq;
5848 : : struct nvmf_vfio_user_ctrlr *ctrlr;
5849 : :
5850 : 454 : sq = SPDK_CONTAINEROF(qpair, struct nvmf_vfio_user_sq, qpair);
5851 [ + - + - ]: 454 : ctrlr = sq->ctrlr;
5852 : :
5853 [ + + + + : 454 : memcpy(trid, &ctrlr->endpoint->trid, sizeof(*trid));
+ - + - +
- ]
5854 : 454 : return 0;
5855 : : }
5856 : :
5857 : : static void
5858 : 0 : nvmf_vfio_user_qpair_abort_request(struct spdk_nvmf_qpair *qpair,
5859 : : struct spdk_nvmf_request *req)
5860 : : {
5861 : 0 : struct spdk_nvmf_request *req_to_abort = NULL;
5862 : 0 : struct spdk_nvmf_request *temp_req = NULL;
5863 : : uint16_t cid;
5864 : :
5865 [ # # # # : 0 : cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid;
# # # # #
# # # #
# ]
5866 : :
5867 [ # # # # : 0 : TAILQ_FOREACH(temp_req, &qpair->outstanding, link) {
# # # # #
# # # #
# ]
5868 : : struct nvmf_vfio_user_req *vu_req;
5869 : :
5870 : 0 : vu_req = SPDK_CONTAINEROF(temp_req, struct nvmf_vfio_user_req, req);
5871 : :
5872 [ # # # # : 0 : if (vu_req->state == VFIO_USER_REQUEST_STATE_EXECUTING && vu_req->cmd.cid == cid) {
# # # # #
# # # #
# ]
5873 : 0 : req_to_abort = temp_req;
5874 : 0 : break;
5875 : : }
5876 : 0 : }
5877 : :
5878 [ # # ]: 0 : if (req_to_abort == NULL) {
5879 : 0 : spdk_nvmf_request_complete(req);
5880 : 0 : return;
5881 : : }
5882 : :
5883 [ # # # # ]: 0 : req->req_to_abort = req_to_abort;
5884 : 0 : nvmf_ctrlr_abort_request(req);
5885 : 0 : }
5886 : :
5887 : : static void
5888 : 0 : nvmf_vfio_user_poll_group_dump_stat(struct spdk_nvmf_transport_poll_group *group,
5889 : : struct spdk_json_write_ctx *w)
5890 : : {
5891 : 0 : struct nvmf_vfio_user_poll_group *vu_group = SPDK_CONTAINEROF(group,
5892 : : struct nvmf_vfio_user_poll_group, group);
5893 : : uint64_t polls_denom;
5894 : :
5895 [ # # # # : 0 : spdk_json_write_named_uint64(w, "ctrlr_intr", vu_group->stats.ctrlr_intr);
# # ]
5896 [ # # # # : 0 : spdk_json_write_named_uint64(w, "ctrlr_kicks", vu_group->stats.ctrlr_kicks);
# # ]
5897 [ # # # # : 0 : spdk_json_write_named_uint64(w, "pg_kicks", vu_group->stats.pg_kicks);
# # ]
5898 [ # # # # : 0 : spdk_json_write_named_uint64(w, "won", vu_group->stats.won);
# # ]
5899 [ # # # # : 0 : spdk_json_write_named_uint64(w, "lost", vu_group->stats.lost);
# # ]
5900 [ # # # # : 0 : spdk_json_write_named_uint64(w, "lost_count", vu_group->stats.lost_count);
# # ]
5901 [ # # # # : 0 : spdk_json_write_named_uint64(w, "rearms", vu_group->stats.rearms);
# # ]
5902 [ # # # # : 0 : spdk_json_write_named_uint64(w, "cq_full", vu_group->stats.cq_full);
# # ]
5903 [ # # # # : 0 : spdk_json_write_named_uint64(w, "pg_process_count", vu_group->stats.pg_process_count);
# # ]
5904 [ # # # # : 0 : spdk_json_write_named_uint64(w, "intr", vu_group->stats.intr);
# # ]
5905 [ # # # # : 0 : spdk_json_write_named_uint64(w, "polls", vu_group->stats.polls);
# # ]
5906 [ # # # # : 0 : spdk_json_write_named_uint64(w, "polls_spurious", vu_group->stats.polls_spurious);
# # ]
5907 [ # # # # : 0 : spdk_json_write_named_uint64(w, "poll_reqs", vu_group->stats.poll_reqs);
# # ]
5908 [ # # # # : 0 : polls_denom = vu_group->stats.polls * (vu_group->stats.polls - 1);
# # # # #
# # # ]
5909 [ # # ]: 0 : if (polls_denom) {
5910 [ # # # # : 0 : uint64_t n = vu_group->stats.polls * vu_group->stats.poll_reqs_squared - vu_group->stats.poll_reqs *
# # # # #
# # # # #
# # # # ]
5911 [ # # # # : 0 : vu_group->stats.poll_reqs;
# # ]
5912 [ # # ]: 0 : spdk_json_write_named_double(w, "poll_reqs_variance", sqrt(n / polls_denom));
5913 : 0 : }
5914 : :
5915 [ # # # # : 0 : spdk_json_write_named_uint64(w, "cqh_admin_writes", vu_group->stats.cqh_admin_writes);
# # ]
5916 [ # # # # : 0 : spdk_json_write_named_uint64(w, "cqh_io_writes", vu_group->stats.cqh_io_writes);
# # ]
5917 : 0 : }
5918 : :
5919 : : static void
5920 : 14 : nvmf_vfio_user_opts_init(struct spdk_nvmf_transport_opts *opts)
5921 : : {
5922 [ + - + - ]: 14 : opts->max_queue_depth = NVMF_VFIO_USER_DEFAULT_MAX_QUEUE_DEPTH;
5923 [ + - + - ]: 14 : opts->max_qpairs_per_ctrlr = NVMF_VFIO_USER_DEFAULT_MAX_QPAIRS_PER_CTRLR;
5924 [ + - + - ]: 14 : opts->in_capsule_data_size = 0;
5925 [ - + + - : 14 : opts->max_io_size = NVMF_VFIO_USER_DEFAULT_MAX_IO_SIZE;
+ - + - ]
5926 [ - + + - : 14 : opts->io_unit_size = NVMF_VFIO_USER_DEFAULT_IO_UNIT_SIZE;
+ - + - ]
5927 [ + - + - ]: 14 : opts->max_aq_depth = NVMF_VFIO_USER_DEFAULT_AQ_DEPTH;
5928 [ + - + - ]: 14 : opts->num_shared_buffers = 0;
5929 [ + - + - ]: 14 : opts->buf_cache_size = 0;
5930 [ + - + - ]: 14 : opts->association_timeout = 0;
5931 [ + - + - ]: 14 : opts->transport_specific = NULL;
5932 : 14 : }
5933 : :
5934 : : const struct spdk_nvmf_transport_ops spdk_nvmf_transport_vfio_user = {
5935 : : .name = "VFIOUSER",
5936 : : .type = SPDK_NVME_TRANSPORT_VFIOUSER,
5937 : : .opts_init = nvmf_vfio_user_opts_init,
5938 : : .create = nvmf_vfio_user_create,
5939 : : .destroy = nvmf_vfio_user_destroy,
5940 : :
5941 : : .listen = nvmf_vfio_user_listen,
5942 : : .stop_listen = nvmf_vfio_user_stop_listen,
5943 : : .cdata_init = nvmf_vfio_user_cdata_init,
5944 : : .listen_associate = nvmf_vfio_user_listen_associate,
5945 : :
5946 : : .listener_discover = nvmf_vfio_user_discover,
5947 : :
5948 : : .poll_group_create = nvmf_vfio_user_poll_group_create,
5949 : : .get_optimal_poll_group = nvmf_vfio_user_get_optimal_poll_group,
5950 : : .poll_group_destroy = nvmf_vfio_user_poll_group_destroy,
5951 : : .poll_group_add = nvmf_vfio_user_poll_group_add,
5952 : : .poll_group_remove = nvmf_vfio_user_poll_group_remove,
5953 : : .poll_group_poll = nvmf_vfio_user_poll_group_poll,
5954 : :
5955 : : .req_free = nvmf_vfio_user_req_free,
5956 : : .req_complete = nvmf_vfio_user_req_complete,
5957 : :
5958 : : .qpair_fini = nvmf_vfio_user_close_qpair,
5959 : : .qpair_get_local_trid = nvmf_vfio_user_qpair_get_local_trid,
5960 : : .qpair_get_peer_trid = nvmf_vfio_user_qpair_get_peer_trid,
5961 : : .qpair_get_listen_trid = nvmf_vfio_user_qpair_get_listen_trid,
5962 : : .qpair_abort_request = nvmf_vfio_user_qpair_abort_request,
5963 : :
5964 : : .poll_group_dump_stat = nvmf_vfio_user_poll_group_dump_stat,
5965 : : };
5966 : :
5967 : 212 : SPDK_NVMF_TRANSPORT_REGISTER(muser, &spdk_nvmf_transport_vfio_user);
5968 : 212 : SPDK_LOG_REGISTER_COMPONENT(nvmf_vfio)
5969 : 212 : SPDK_LOG_REGISTER_COMPONENT(vfio_user_db)
|