1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/mmio.h" 37 #include "spdk/string.h" 38 #include "spdk/env.h" 39 40 #include "spdk_internal/virtio.h" 41 42 struct virtio_hw { 43 uint8_t use_msix; 44 uint32_t notify_off_multiplier; 45 uint8_t *isr; 46 uint16_t *notify_base; 47 48 struct { 49 /** Mem-mapped resources from given PCI BAR */ 50 void *vaddr; 51 52 /** Length of the address space */ 53 uint32_t len; 54 } pci_bar[6]; 55 56 struct virtio_pci_common_cfg *common_cfg; 57 struct spdk_pci_device *pci_dev; 58 59 /** Device-specific PCI config space */ 60 void *dev_cfg; 61 }; 62 63 struct virtio_pci_probe_ctx { 64 virtio_pci_create_cb enum_cb; 65 void *enum_ctx; 66 uint16_t device_id; 67 }; 68 69 /* 70 * Following macros are derived from linux/pci_regs.h, however, 71 * we can't simply include that header here, as there is no such 72 * file for non-Linux platform. 73 */ 74 #define PCI_CAPABILITY_LIST 0x34 75 #define PCI_CAP_ID_VNDR 0x09 76 #define PCI_CAP_ID_MSIX 0x11 77 78 static inline int 79 check_vq_phys_addr_ok(struct virtqueue *vq) 80 { 81 /* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit, 82 * and only accepts 32 bit page frame number. 83 * Check if the allocated physical memory exceeds 16TB. 84 */ 85 if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >> 86 (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) { 87 SPDK_ERRLOG("vring address shouldn't be above 16TB!\n"); 88 return 0; 89 } 90 91 return 1; 92 } 93 94 static void 95 free_virtio_hw(struct virtio_hw *hw) 96 { 97 unsigned i; 98 99 for (i = 0; i < 6; ++i) { 100 if (hw->pci_bar[i].vaddr == NULL) { 101 continue; 102 } 103 104 spdk_pci_device_unmap_bar(hw->pci_dev, i, hw->pci_bar[i].vaddr); 105 } 106 107 free(hw); 108 } 109 110 static void 111 pci_dump_json_info(struct virtio_dev *dev, struct spdk_json_write_ctx *w) 112 { 113 struct virtio_hw *hw = dev->ctx; 114 struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr((struct spdk_pci_device *)hw->pci_dev); 115 char addr[32]; 116 117 spdk_json_write_name(w, "type"); 118 if (dev->modern) { 119 spdk_json_write_string(w, "pci-modern"); 120 } else { 121 spdk_json_write_string(w, "pci-legacy"); 122 } 123 124 spdk_json_write_name(w, "pci_address"); 125 spdk_pci_addr_fmt(addr, sizeof(addr), &pci_addr); 126 spdk_json_write_string(w, addr); 127 } 128 129 static void 130 pci_write_json_config(struct virtio_dev *dev, struct spdk_json_write_ctx *w) 131 { 132 struct virtio_hw *hw = dev->ctx; 133 struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr(hw->pci_dev); 134 char addr[32]; 135 136 spdk_pci_addr_fmt(addr, sizeof(addr), &pci_addr); 137 138 spdk_json_write_named_string(w, "trtype", "pci"); 139 spdk_json_write_named_string(w, "traddr", addr); 140 } 141 142 static inline void 143 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi) 144 { 145 spdk_mmio_write_4(lo, val & ((1ULL << 32) - 1)); 146 spdk_mmio_write_4(hi, val >> 32); 147 } 148 149 static int 150 modern_read_dev_config(struct virtio_dev *dev, size_t offset, 151 void *dst, int length) 152 { 153 struct virtio_hw *hw = dev->ctx; 154 int i; 155 uint8_t *p; 156 uint8_t old_gen, new_gen; 157 158 do { 159 old_gen = spdk_mmio_read_1(&hw->common_cfg->config_generation); 160 161 p = dst; 162 for (i = 0; i < length; i++) { 163 *p++ = spdk_mmio_read_1((uint8_t *)hw->dev_cfg + offset + i); 164 } 165 166 new_gen = spdk_mmio_read_1(&hw->common_cfg->config_generation); 167 } while (old_gen != new_gen); 168 169 return 0; 170 } 171 172 static int 173 modern_write_dev_config(struct virtio_dev *dev, size_t offset, 174 const void *src, int length) 175 { 176 struct virtio_hw *hw = dev->ctx; 177 int i; 178 const uint8_t *p = src; 179 180 for (i = 0; i < length; i++) { 181 spdk_mmio_write_1(((uint8_t *)hw->dev_cfg) + offset + i, *p++); 182 } 183 184 return 0; 185 } 186 187 static uint64_t 188 modern_get_features(struct virtio_dev *dev) 189 { 190 struct virtio_hw *hw = dev->ctx; 191 uint32_t features_lo, features_hi; 192 193 spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 0); 194 features_lo = spdk_mmio_read_4(&hw->common_cfg->device_feature); 195 196 spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 1); 197 features_hi = spdk_mmio_read_4(&hw->common_cfg->device_feature); 198 199 return ((uint64_t)features_hi << 32) | features_lo; 200 } 201 202 static int 203 modern_set_features(struct virtio_dev *dev, uint64_t features) 204 { 205 struct virtio_hw *hw = dev->ctx; 206 207 if ((features & (1ULL << VIRTIO_F_VERSION_1)) == 0) { 208 SPDK_ERRLOG("VIRTIO_F_VERSION_1 feature is not enabled.\n"); 209 return -1; 210 } 211 212 spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 0); 213 spdk_mmio_write_4(&hw->common_cfg->guest_feature, features & ((1ULL << 32) - 1)); 214 215 spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 1); 216 spdk_mmio_write_4(&hw->common_cfg->guest_feature, features >> 32); 217 218 dev->negotiated_features = features; 219 220 return 0; 221 } 222 223 static void 224 modern_destruct_dev(struct virtio_dev *vdev) 225 { 226 struct virtio_hw *hw = vdev->ctx; 227 struct spdk_pci_device *pci_dev = hw->pci_dev; 228 229 free_virtio_hw(hw); 230 spdk_pci_device_detach(pci_dev); 231 } 232 233 static uint8_t 234 modern_get_status(struct virtio_dev *dev) 235 { 236 struct virtio_hw *hw = dev->ctx; 237 238 return spdk_mmio_read_1(&hw->common_cfg->device_status); 239 } 240 241 static void 242 modern_set_status(struct virtio_dev *dev, uint8_t status) 243 { 244 struct virtio_hw *hw = dev->ctx; 245 246 spdk_mmio_write_1(&hw->common_cfg->device_status, status); 247 } 248 249 static uint16_t 250 modern_get_queue_size(struct virtio_dev *dev, uint16_t queue_id) 251 { 252 struct virtio_hw *hw = dev->ctx; 253 254 spdk_mmio_write_2(&hw->common_cfg->queue_select, queue_id); 255 return spdk_mmio_read_2(&hw->common_cfg->queue_size); 256 } 257 258 static int 259 modern_setup_queue(struct virtio_dev *dev, struct virtqueue *vq) 260 { 261 struct virtio_hw *hw = dev->ctx; 262 uint64_t desc_addr, avail_addr, used_addr; 263 uint16_t notify_off; 264 void *queue_mem; 265 uint64_t queue_mem_phys_addr; 266 267 /* To ensure physical address contiguity we make the queue occupy 268 * only a single hugepage (2MB). As of Virtio 1.0, the queue size 269 * always falls within this limit. 270 */ 271 if (vq->vq_ring_size > 0x200000) { 272 return -ENOMEM; 273 } 274 275 queue_mem = spdk_dma_zmalloc(vq->vq_ring_size, 0x200000, &queue_mem_phys_addr); 276 if (queue_mem == NULL) { 277 return -ENOMEM; 278 } 279 280 vq->vq_ring_mem = queue_mem_phys_addr; 281 vq->vq_ring_virt_mem = queue_mem; 282 283 if (!check_vq_phys_addr_ok(vq)) { 284 spdk_dma_free(queue_mem); 285 return -1; 286 } 287 288 desc_addr = vq->vq_ring_mem; 289 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc); 290 used_addr = (avail_addr + offsetof(struct vring_avail, ring[vq->vq_nentries]) 291 + VIRTIO_PCI_VRING_ALIGN - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1); 292 293 spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index); 294 295 io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo, 296 &hw->common_cfg->queue_desc_hi); 297 io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo, 298 &hw->common_cfg->queue_avail_hi); 299 io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo, 300 &hw->common_cfg->queue_used_hi); 301 302 notify_off = spdk_mmio_read_2(&hw->common_cfg->queue_notify_off); 303 vq->notify_addr = (void *)((uint8_t *)hw->notify_base + 304 notify_off * hw->notify_off_multiplier); 305 306 spdk_mmio_write_2(&hw->common_cfg->queue_enable, 1); 307 308 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "queue %"PRIu16" addresses:\n", vq->vq_queue_index); 309 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t desc_addr: %" PRIx64 "\n", desc_addr); 310 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t aval_addr: %" PRIx64 "\n", avail_addr); 311 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t used_addr: %" PRIx64 "\n", used_addr); 312 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t notify addr: %p (notify offset: %"PRIu16")\n", 313 vq->notify_addr, notify_off); 314 315 return 0; 316 } 317 318 static void 319 modern_del_queue(struct virtio_dev *dev, struct virtqueue *vq) 320 { 321 struct virtio_hw *hw = dev->ctx; 322 323 spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index); 324 325 io_write64_twopart(0, &hw->common_cfg->queue_desc_lo, 326 &hw->common_cfg->queue_desc_hi); 327 io_write64_twopart(0, &hw->common_cfg->queue_avail_lo, 328 &hw->common_cfg->queue_avail_hi); 329 io_write64_twopart(0, &hw->common_cfg->queue_used_lo, 330 &hw->common_cfg->queue_used_hi); 331 332 spdk_mmio_write_2(&hw->common_cfg->queue_enable, 0); 333 334 spdk_dma_free(vq->vq_ring_virt_mem); 335 } 336 337 static void 338 modern_notify_queue(struct virtio_dev *dev, struct virtqueue *vq) 339 { 340 spdk_mmio_write_2(vq->notify_addr, vq->vq_queue_index); 341 } 342 343 static const struct virtio_dev_ops modern_ops = { 344 .read_dev_cfg = modern_read_dev_config, 345 .write_dev_cfg = modern_write_dev_config, 346 .get_status = modern_get_status, 347 .set_status = modern_set_status, 348 .get_features = modern_get_features, 349 .set_features = modern_set_features, 350 .destruct_dev = modern_destruct_dev, 351 .get_queue_size = modern_get_queue_size, 352 .setup_queue = modern_setup_queue, 353 .del_queue = modern_del_queue, 354 .notify_queue = modern_notify_queue, 355 .dump_json_info = pci_dump_json_info, 356 .write_json_config = pci_write_json_config, 357 }; 358 359 static void * 360 get_cfg_addr(struct virtio_hw *hw, struct virtio_pci_cap *cap) 361 { 362 uint8_t bar = cap->bar; 363 uint32_t length = cap->length; 364 uint32_t offset = cap->offset; 365 366 if (bar > 5) { 367 SPDK_ERRLOG("invalid bar: %"PRIu8"\n", bar); 368 return NULL; 369 } 370 371 if (offset + length < offset) { 372 SPDK_ERRLOG("offset(%"PRIu32") + length(%"PRIu32") overflows\n", 373 offset, length); 374 return NULL; 375 } 376 377 if (offset + length > hw->pci_bar[bar].len) { 378 SPDK_ERRLOG("invalid cap: overflows bar space: %"PRIu32" > %"PRIu32"\n", 379 offset + length, hw->pci_bar[bar].len); 380 return NULL; 381 } 382 383 if (hw->pci_bar[bar].vaddr == NULL) { 384 SPDK_ERRLOG("bar %"PRIu8" base addr is NULL\n", bar); 385 return NULL; 386 } 387 388 return hw->pci_bar[bar].vaddr + offset; 389 } 390 391 static int 392 virtio_read_caps(struct virtio_hw *hw) 393 { 394 uint8_t pos; 395 struct virtio_pci_cap cap; 396 int ret; 397 398 ret = spdk_pci_device_cfg_read(hw->pci_dev, &pos, 1, PCI_CAPABILITY_LIST); 399 if (ret < 0) { 400 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "failed to read pci capability list\n"); 401 return -1; 402 } 403 404 while (pos) { 405 ret = spdk_pci_device_cfg_read(hw->pci_dev, &cap, sizeof(cap), pos); 406 if (ret < 0) { 407 SPDK_ERRLOG("failed to read pci cap at pos: %"PRIx8"\n", pos); 408 break; 409 } 410 411 if (cap.cap_vndr == PCI_CAP_ID_MSIX) { 412 hw->use_msix = 1; 413 } 414 415 if (cap.cap_vndr != PCI_CAP_ID_VNDR) { 416 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, 417 "[%2"PRIx8"] skipping non VNDR cap id: %02"PRIx8"\n", 418 pos, cap.cap_vndr); 419 goto next; 420 } 421 422 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, 423 "[%2"PRIx8"] cfg type: %"PRIu8", bar: %"PRIu8", offset: %04"PRIx32", len: %"PRIu32"\n", 424 pos, cap.cfg_type, cap.bar, cap.offset, cap.length); 425 426 switch (cap.cfg_type) { 427 case VIRTIO_PCI_CAP_COMMON_CFG: 428 hw->common_cfg = get_cfg_addr(hw, &cap); 429 break; 430 case VIRTIO_PCI_CAP_NOTIFY_CFG: 431 spdk_pci_device_cfg_read(hw->pci_dev, &hw->notify_off_multiplier, 432 4, pos + sizeof(cap)); 433 hw->notify_base = get_cfg_addr(hw, &cap); 434 break; 435 case VIRTIO_PCI_CAP_DEVICE_CFG: 436 hw->dev_cfg = get_cfg_addr(hw, &cap); 437 break; 438 case VIRTIO_PCI_CAP_ISR_CFG: 439 hw->isr = get_cfg_addr(hw, &cap); 440 break; 441 } 442 443 next: 444 pos = cap.cap_next; 445 } 446 447 if (hw->common_cfg == NULL || hw->notify_base == NULL || 448 hw->dev_cfg == NULL || hw->isr == NULL) { 449 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "no modern virtio pci device found.\n"); 450 return -1; 451 } 452 453 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "found modern virtio pci device.\n"); 454 455 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "common cfg mapped at: %p\n", hw->common_cfg); 456 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "device cfg mapped at: %p\n", hw->dev_cfg); 457 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "isr cfg mapped at: %p\n", hw->isr); 458 SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "notify base: %p, notify off multiplier: %u\n", 459 hw->notify_base, hw->notify_off_multiplier); 460 461 return 0; 462 } 463 464 static int 465 virtio_pci_dev_probe(struct spdk_pci_device *pci_dev, struct virtio_pci_probe_ctx *ctx) 466 { 467 struct virtio_hw *hw; 468 uint8_t *bar_vaddr; 469 uint64_t bar_paddr, bar_len; 470 int rc; 471 unsigned i; 472 char bdf[32]; 473 struct spdk_pci_addr addr; 474 475 addr = spdk_pci_device_get_addr(pci_dev); 476 rc = spdk_pci_addr_fmt(bdf, sizeof(bdf), &addr); 477 if (rc != 0) { 478 SPDK_ERRLOG("Ignoring a device with non-parseable PCI address\n"); 479 return -1; 480 } 481 482 hw = calloc(1, sizeof(*hw)); 483 if (hw == NULL) { 484 SPDK_ERRLOG("%s: calloc failed\n", bdf); 485 return -1; 486 } 487 488 hw->pci_dev = pci_dev; 489 490 for (i = 0; i < 6; ++i) { 491 rc = spdk_pci_device_map_bar(pci_dev, i, (void *) &bar_vaddr, &bar_paddr, 492 &bar_len); 493 if (rc != 0) { 494 SPDK_ERRLOG("%s: failed to memmap PCI BAR %u\n", bdf, i); 495 free_virtio_hw(hw); 496 return -1; 497 } 498 499 hw->pci_bar[i].vaddr = bar_vaddr; 500 hw->pci_bar[i].len = bar_len; 501 } 502 503 /* Virtio PCI caps exist only on modern PCI devices. 504 * Legacy devices are not supported. 505 */ 506 if (virtio_read_caps(hw) != 0) { 507 SPDK_NOTICELOG("Ignoring legacy PCI device at %s\n", bdf); 508 free_virtio_hw(hw); 509 return -1; 510 } 511 512 rc = ctx->enum_cb((struct virtio_pci_ctx *)hw, ctx->enum_ctx); 513 if (rc != 0) { 514 free_virtio_hw(hw); 515 } 516 517 return rc; 518 } 519 520 static int 521 virtio_pci_dev_probe_cb(void *probe_ctx, struct spdk_pci_device *pci_dev) 522 { 523 struct virtio_pci_probe_ctx *ctx = probe_ctx; 524 uint16_t pci_device_id = spdk_pci_device_get_device_id(pci_dev); 525 526 if (pci_device_id != ctx->device_id) { 527 return 1; 528 } 529 530 return virtio_pci_dev_probe(pci_dev, ctx); 531 } 532 533 int 534 virtio_pci_dev_enumerate(virtio_pci_create_cb enum_cb, void *enum_ctx, 535 uint16_t pci_device_id) 536 { 537 struct virtio_pci_probe_ctx ctx; 538 539 if (!spdk_process_is_primary()) { 540 SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n"); 541 return 0; 542 } 543 544 ctx.enum_cb = enum_cb; 545 ctx.enum_ctx = enum_ctx; 546 ctx.device_id = pci_device_id; 547 548 return spdk_pci_virtio_enumerate(virtio_pci_dev_probe_cb, &ctx); 549 } 550 551 int 552 virtio_pci_dev_attach(virtio_pci_create_cb enum_cb, void *enum_ctx, 553 uint16_t pci_device_id, struct spdk_pci_addr *pci_address) 554 { 555 struct virtio_pci_probe_ctx ctx; 556 557 if (!spdk_process_is_primary()) { 558 SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n"); 559 return 0; 560 } 561 562 ctx.enum_cb = enum_cb; 563 ctx.enum_ctx = enum_ctx; 564 ctx.device_id = pci_device_id; 565 566 return spdk_pci_virtio_device_attach(virtio_pci_dev_probe_cb, &ctx, pci_address); 567 } 568 569 int 570 virtio_pci_dev_init(struct virtio_dev *vdev, const char *name, 571 struct virtio_pci_ctx *pci_ctx) 572 { 573 int rc; 574 575 rc = virtio_dev_construct(vdev, name, &modern_ops, pci_ctx); 576 if (rc != 0) { 577 return -1; 578 } 579 580 vdev->is_hw = 1; 581 vdev->modern = 1; 582 583 return 0; 584 } 585 586 SPDK_LOG_REGISTER_COMPONENT("virtio_pci", SPDK_LOG_VIRTIO_PCI) 587