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