1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2015 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 <stdint.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <errno.h> 38 #include <unistd.h> 39 40 #include <rte_ethdev.h> 41 #include <rte_memcpy.h> 42 #include <rte_string_fns.h> 43 #include <rte_memzone.h> 44 #include <rte_malloc.h> 45 #include <rte_atomic.h> 46 #include <rte_branch_prediction.h> 47 #include <rte_pci.h> 48 #include <rte_ether.h> 49 #include <rte_common.h> 50 #include <rte_errno.h> 51 52 #include <rte_memory.h> 53 #include <rte_eal.h> 54 #include <rte_dev.h> 55 56 #include "virtio_ethdev.h" 57 #include "virtio_pci.h" 58 #include "virtio_logs.h" 59 #include "virtqueue.h" 60 #include "virtio_rxtx.h" 61 62 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev); 63 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev); 64 static int virtio_dev_configure(struct rte_eth_dev *dev); 65 static int virtio_dev_start(struct rte_eth_dev *dev); 66 static void virtio_dev_stop(struct rte_eth_dev *dev); 67 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev); 68 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev); 69 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev); 70 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev); 71 static void virtio_dev_info_get(struct rte_eth_dev *dev, 72 struct rte_eth_dev_info *dev_info); 73 static int virtio_dev_link_update(struct rte_eth_dev *dev, 74 __rte_unused int wait_to_complete); 75 76 static void virtio_set_hwaddr(struct virtio_hw *hw); 77 static void virtio_get_hwaddr(struct virtio_hw *hw); 78 79 static void virtio_dev_stats_get(struct rte_eth_dev *dev, 80 struct rte_eth_stats *stats); 81 static int virtio_dev_xstats_get(struct rte_eth_dev *dev, 82 struct rte_eth_xstats *xstats, unsigned n); 83 static void virtio_dev_stats_reset(struct rte_eth_dev *dev); 84 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev); 85 static int virtio_vlan_filter_set(struct rte_eth_dev *dev, 86 uint16_t vlan_id, int on); 87 static void virtio_mac_addr_add(struct rte_eth_dev *dev, 88 struct ether_addr *mac_addr, 89 uint32_t index, uint32_t vmdq __rte_unused); 90 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index); 91 static void virtio_mac_addr_set(struct rte_eth_dev *dev, 92 struct ether_addr *mac_addr); 93 94 static int virtio_dev_queue_stats_mapping_set( 95 __rte_unused struct rte_eth_dev *eth_dev, 96 __rte_unused uint16_t queue_id, 97 __rte_unused uint8_t stat_idx, 98 __rte_unused uint8_t is_rx); 99 100 /* 101 * The set of PCI devices this driver supports 102 */ 103 static const struct rte_pci_id pci_id_virtio_map[] = { 104 105 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)}, 106 #include "rte_pci_dev_ids.h" 107 108 { .vendor_id = 0, /* sentinel */ }, 109 }; 110 111 struct rte_virtio_xstats_name_off { 112 char name[RTE_ETH_XSTATS_NAME_SIZE]; 113 unsigned offset; 114 }; 115 116 /* [rt]x_qX_ is prepended to the name string here */ 117 static const struct rte_virtio_xstats_name_off rte_virtio_q_stat_strings[] = { 118 {"good_packets", offsetof(struct virtqueue, packets)}, 119 {"good_bytes", offsetof(struct virtqueue, bytes)}, 120 {"errors", offsetof(struct virtqueue, errors)}, 121 {"multicast_packets", offsetof(struct virtqueue, multicast)}, 122 {"broadcast_packets", offsetof(struct virtqueue, broadcast)}, 123 {"undersize_packets", offsetof(struct virtqueue, size_bins[0])}, 124 {"size_64_packets", offsetof(struct virtqueue, size_bins[1])}, 125 {"size_65_127_packets", offsetof(struct virtqueue, size_bins[2])}, 126 {"size_128_255_packets", offsetof(struct virtqueue, size_bins[3])}, 127 {"size_256_511_packets", offsetof(struct virtqueue, size_bins[4])}, 128 {"size_512_1023_packets", offsetof(struct virtqueue, size_bins[5])}, 129 {"size_1024_1517_packets", offsetof(struct virtqueue, size_bins[6])}, 130 {"size_1518_max_packets", offsetof(struct virtqueue, size_bins[7])}, 131 }; 132 133 #define VIRTIO_NB_Q_XSTATS (sizeof(rte_virtio_q_stat_strings) / \ 134 sizeof(rte_virtio_q_stat_strings[0])) 135 136 static int 137 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl, 138 int *dlen, int pkt_num) 139 { 140 uint32_t head, i; 141 int k, sum = 0; 142 virtio_net_ctrl_ack status = ~0; 143 struct virtio_pmd_ctrl result; 144 145 ctrl->status = status; 146 147 if (!(vq && vq->hw->cvq)) { 148 PMD_INIT_LOG(ERR, "Control queue is not supported."); 149 return -1; 150 } 151 head = vq->vq_desc_head_idx; 152 153 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, " 154 "vq->hw->cvq = %p vq = %p", 155 vq->vq_desc_head_idx, status, vq->hw->cvq, vq); 156 157 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1)) 158 return -1; 159 160 memcpy(vq->virtio_net_hdr_mz->addr, ctrl, 161 sizeof(struct virtio_pmd_ctrl)); 162 163 /* 164 * Format is enforced in qemu code: 165 * One TX packet for header; 166 * At least one TX packet per argument; 167 * One RX packet for ACK. 168 */ 169 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT; 170 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr; 171 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr); 172 vq->vq_free_cnt--; 173 i = vq->vq_ring.desc[head].next; 174 175 for (k = 0; k < pkt_num; k++) { 176 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT; 177 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr 178 + sizeof(struct virtio_net_ctrl_hdr) 179 + sizeof(ctrl->status) + sizeof(uint8_t)*sum; 180 vq->vq_ring.desc[i].len = dlen[k]; 181 sum += dlen[k]; 182 vq->vq_free_cnt--; 183 i = vq->vq_ring.desc[i].next; 184 } 185 186 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE; 187 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr 188 + sizeof(struct virtio_net_ctrl_hdr); 189 vq->vq_ring.desc[i].len = sizeof(ctrl->status); 190 vq->vq_free_cnt--; 191 192 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next; 193 194 vq_update_avail_ring(vq, head); 195 vq_update_avail_idx(vq); 196 197 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index); 198 199 virtqueue_notify(vq); 200 201 rte_rmb(); 202 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) { 203 rte_rmb(); 204 usleep(100); 205 } 206 207 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) { 208 uint32_t idx, desc_idx, used_idx; 209 struct vring_used_elem *uep; 210 211 used_idx = (uint32_t)(vq->vq_used_cons_idx 212 & (vq->vq_nentries - 1)); 213 uep = &vq->vq_ring.used->ring[used_idx]; 214 idx = (uint32_t) uep->id; 215 desc_idx = idx; 216 217 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) { 218 desc_idx = vq->vq_ring.desc[desc_idx].next; 219 vq->vq_free_cnt++; 220 } 221 222 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx; 223 vq->vq_desc_head_idx = idx; 224 225 vq->vq_used_cons_idx++; 226 vq->vq_free_cnt++; 227 } 228 229 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d", 230 vq->vq_free_cnt, vq->vq_desc_head_idx); 231 232 memcpy(&result, vq->virtio_net_hdr_mz->addr, 233 sizeof(struct virtio_pmd_ctrl)); 234 235 return result.status; 236 } 237 238 static int 239 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues) 240 { 241 struct virtio_hw *hw = dev->data->dev_private; 242 struct virtio_pmd_ctrl ctrl; 243 int dlen[1]; 244 int ret; 245 246 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ; 247 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 248 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t)); 249 250 dlen[0] = sizeof(uint16_t); 251 252 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 253 if (ret) { 254 PMD_INIT_LOG(ERR, "Multiqueue configured but send command " 255 "failed, this is too late now..."); 256 return -EINVAL; 257 } 258 259 return 0; 260 } 261 262 void 263 virtio_dev_queue_release(struct virtqueue *vq) 264 { 265 struct virtio_hw *hw; 266 267 if (vq) { 268 hw = vq->hw; 269 if (vq->configured) 270 hw->vtpci_ops->del_queue(hw, vq); 271 272 rte_memzone_free(vq->mz); 273 if (vq->virtio_net_hdr_mz) 274 rte_memzone_free(vq->virtio_net_hdr_mz); 275 276 rte_free(vq->sw_ring); 277 rte_free(vq); 278 } 279 } 280 281 int virtio_dev_queue_setup(struct rte_eth_dev *dev, 282 int queue_type, 283 uint16_t queue_idx, 284 uint16_t vtpci_queue_idx, 285 uint16_t nb_desc, 286 unsigned int socket_id, 287 struct virtqueue **pvq) 288 { 289 char vq_name[VIRTQUEUE_MAX_NAME_SZ]; 290 const struct rte_memzone *mz; 291 unsigned int vq_size, size; 292 struct virtio_hw *hw = dev->data->dev_private; 293 struct virtqueue *vq = NULL; 294 const char *queue_names[] = {"rvq", "txq", "cvq"}; 295 296 PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx); 297 298 /* 299 * Read the virtqueue size from the Queue Size field 300 * Always power of 2 and if 0 virtqueue does not exist 301 */ 302 vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx); 303 PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc); 304 if (vq_size == 0) { 305 PMD_INIT_LOG(ERR, "virtqueue does not exist"); 306 return -EINVAL; 307 } 308 309 if (!rte_is_power_of_2(vq_size)) { 310 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2"); 311 return -EINVAL; 312 } 313 314 snprintf(vq_name, sizeof(vq_name), "port%d_%s%d", 315 dev->data->port_id, queue_names[queue_type], queue_idx); 316 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) + 317 vq_size * sizeof(struct vq_desc_extra), 318 RTE_CACHE_LINE_SIZE); 319 if (vq == NULL) { 320 PMD_INIT_LOG(ERR, "Can not allocate virtqueue"); 321 return -ENOMEM; 322 } 323 324 if (queue_type == VTNET_RQ) { 325 size_t sz_sw; 326 327 sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) * 328 sizeof(vq->sw_ring[0]); 329 vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring", sz_sw, 330 RTE_CACHE_LINE_SIZE, 331 socket_id); 332 if (!vq->sw_ring) { 333 PMD_INIT_LOG(ERR, "Can not allocate RX soft ring"); 334 virtio_dev_queue_release(vq); 335 return -ENOMEM; 336 } 337 } 338 339 vq->hw = hw; 340 vq->port_id = dev->data->port_id; 341 vq->queue_id = queue_idx; 342 vq->vq_queue_index = vtpci_queue_idx; 343 vq->vq_nentries = vq_size; 344 345 if (nb_desc == 0 || nb_desc > vq_size) 346 nb_desc = vq_size; 347 vq->vq_free_cnt = nb_desc; 348 349 /* 350 * Reserve a memzone for vring elements 351 */ 352 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN); 353 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN); 354 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size); 355 356 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, 357 socket_id, 0, VIRTIO_PCI_VRING_ALIGN); 358 if (mz == NULL) { 359 if (rte_errno == EEXIST) 360 mz = rte_memzone_lookup(vq_name); 361 if (mz == NULL) { 362 virtio_dev_queue_release(vq); 363 return -ENOMEM; 364 } 365 } 366 367 /* 368 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit, 369 * and only accepts 32 bit page frame number. 370 * Check if the allocated physical memory exceeds 16TB. 371 */ 372 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) { 373 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!"); 374 virtio_dev_queue_release(vq); 375 return -ENOMEM; 376 } 377 378 memset(mz->addr, 0, sizeof(mz->len)); 379 vq->mz = mz; 380 vq->vq_ring_mem = mz->phys_addr; 381 vq->vq_ring_virt_mem = mz->addr; 382 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr); 383 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr); 384 vq->virtio_net_hdr_mz = NULL; 385 vq->virtio_net_hdr_mem = 0; 386 387 if (queue_type == VTNET_TQ) { 388 const struct rte_memzone *hdr_mz; 389 struct virtio_tx_region *txr; 390 unsigned int i; 391 392 /* 393 * For each xmit packet, allocate a virtio_net_hdr 394 * and indirect ring elements 395 */ 396 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone", 397 dev->data->port_id, queue_idx); 398 hdr_mz = rte_memzone_reserve_aligned(vq_name, 399 vq_size * sizeof(*txr), 400 socket_id, 0, 401 RTE_CACHE_LINE_SIZE); 402 if (hdr_mz == NULL) { 403 if (rte_errno == EEXIST) 404 hdr_mz = rte_memzone_lookup(vq_name); 405 if (hdr_mz == NULL) { 406 virtio_dev_queue_release(vq); 407 return -ENOMEM; 408 } 409 } 410 vq->virtio_net_hdr_mz = hdr_mz; 411 vq->virtio_net_hdr_mem = hdr_mz->phys_addr; 412 413 txr = hdr_mz->addr; 414 memset(txr, 0, vq_size * sizeof(*txr)); 415 for (i = 0; i < vq_size; i++) { 416 struct vring_desc *start_dp = txr[i].tx_indir; 417 418 vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir)); 419 420 /* first indirect descriptor is always the tx header */ 421 start_dp->addr = vq->virtio_net_hdr_mem 422 + i * sizeof(*txr) 423 + offsetof(struct virtio_tx_region, tx_hdr); 424 425 start_dp->len = vq->hw->vtnet_hdr_size; 426 start_dp->flags = VRING_DESC_F_NEXT; 427 } 428 429 } else if (queue_type == VTNET_CQ) { 430 /* Allocate a page for control vq command, data and status */ 431 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone", 432 dev->data->port_id); 433 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name, 434 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE); 435 if (vq->virtio_net_hdr_mz == NULL) { 436 if (rte_errno == EEXIST) 437 vq->virtio_net_hdr_mz = 438 rte_memzone_lookup(vq_name); 439 if (vq->virtio_net_hdr_mz == NULL) { 440 virtio_dev_queue_release(vq); 441 return -ENOMEM; 442 } 443 } 444 vq->virtio_net_hdr_mem = 445 vq->virtio_net_hdr_mz->phys_addr; 446 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE); 447 } 448 449 hw->vtpci_ops->setup_queue(hw, vq); 450 451 vq->configured = 1; 452 *pvq = vq; 453 return 0; 454 } 455 456 static int 457 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx, 458 uint32_t socket_id) 459 { 460 struct virtqueue *vq; 461 int ret; 462 struct virtio_hw *hw = dev->data->dev_private; 463 464 PMD_INIT_FUNC_TRACE(); 465 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX, 466 vtpci_queue_idx, 0, socket_id, &vq); 467 if (ret < 0) { 468 PMD_INIT_LOG(ERR, "control vq initialization failed"); 469 return ret; 470 } 471 472 hw->cvq = vq; 473 return 0; 474 } 475 476 static void 477 virtio_free_queues(struct rte_eth_dev *dev) 478 { 479 unsigned int i; 480 481 for (i = 0; i < dev->data->nb_rx_queues; i++) 482 virtio_dev_rx_queue_release(dev->data->rx_queues[i]); 483 484 dev->data->nb_rx_queues = 0; 485 486 for (i = 0; i < dev->data->nb_tx_queues; i++) 487 virtio_dev_tx_queue_release(dev->data->tx_queues[i]); 488 489 dev->data->nb_tx_queues = 0; 490 } 491 492 static void 493 virtio_dev_close(struct rte_eth_dev *dev) 494 { 495 struct virtio_hw *hw = dev->data->dev_private; 496 497 PMD_INIT_LOG(DEBUG, "virtio_dev_close"); 498 499 if (hw->started == 1) 500 virtio_dev_stop(dev); 501 502 /* reset the NIC */ 503 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 504 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR); 505 vtpci_reset(hw); 506 virtio_dev_free_mbufs(dev); 507 virtio_free_queues(dev); 508 } 509 510 static void 511 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev) 512 { 513 struct virtio_hw *hw = dev->data->dev_private; 514 struct virtio_pmd_ctrl ctrl; 515 int dlen[1]; 516 int ret; 517 518 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 519 PMD_INIT_LOG(INFO, "host does not support rx control\n"); 520 return; 521 } 522 523 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 524 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 525 ctrl.data[0] = 1; 526 dlen[0] = 1; 527 528 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 529 if (ret) 530 PMD_INIT_LOG(ERR, "Failed to enable promisc"); 531 } 532 533 static void 534 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev) 535 { 536 struct virtio_hw *hw = dev->data->dev_private; 537 struct virtio_pmd_ctrl ctrl; 538 int dlen[1]; 539 int ret; 540 541 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 542 PMD_INIT_LOG(INFO, "host does not support rx control\n"); 543 return; 544 } 545 546 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 547 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 548 ctrl.data[0] = 0; 549 dlen[0] = 1; 550 551 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 552 if (ret) 553 PMD_INIT_LOG(ERR, "Failed to disable promisc"); 554 } 555 556 static void 557 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev) 558 { 559 struct virtio_hw *hw = dev->data->dev_private; 560 struct virtio_pmd_ctrl ctrl; 561 int dlen[1]; 562 int ret; 563 564 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 565 PMD_INIT_LOG(INFO, "host does not support rx control\n"); 566 return; 567 } 568 569 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 570 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 571 ctrl.data[0] = 1; 572 dlen[0] = 1; 573 574 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 575 if (ret) 576 PMD_INIT_LOG(ERR, "Failed to enable allmulticast"); 577 } 578 579 static void 580 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev) 581 { 582 struct virtio_hw *hw = dev->data->dev_private; 583 struct virtio_pmd_ctrl ctrl; 584 int dlen[1]; 585 int ret; 586 587 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 588 PMD_INIT_LOG(INFO, "host does not support rx control\n"); 589 return; 590 } 591 592 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 593 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 594 ctrl.data[0] = 0; 595 dlen[0] = 1; 596 597 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 598 if (ret) 599 PMD_INIT_LOG(ERR, "Failed to disable allmulticast"); 600 } 601 602 /* 603 * dev_ops for virtio, bare necessities for basic operation 604 */ 605 static const struct eth_dev_ops virtio_eth_dev_ops = { 606 .dev_configure = virtio_dev_configure, 607 .dev_start = virtio_dev_start, 608 .dev_stop = virtio_dev_stop, 609 .dev_close = virtio_dev_close, 610 .promiscuous_enable = virtio_dev_promiscuous_enable, 611 .promiscuous_disable = virtio_dev_promiscuous_disable, 612 .allmulticast_enable = virtio_dev_allmulticast_enable, 613 .allmulticast_disable = virtio_dev_allmulticast_disable, 614 615 .dev_infos_get = virtio_dev_info_get, 616 .stats_get = virtio_dev_stats_get, 617 .xstats_get = virtio_dev_xstats_get, 618 .stats_reset = virtio_dev_stats_reset, 619 .xstats_reset = virtio_dev_stats_reset, 620 .link_update = virtio_dev_link_update, 621 .rx_queue_setup = virtio_dev_rx_queue_setup, 622 .rx_queue_release = virtio_dev_rx_queue_release, 623 .tx_queue_setup = virtio_dev_tx_queue_setup, 624 .tx_queue_release = virtio_dev_tx_queue_release, 625 /* collect stats per queue */ 626 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set, 627 .vlan_filter_set = virtio_vlan_filter_set, 628 .mac_addr_add = virtio_mac_addr_add, 629 .mac_addr_remove = virtio_mac_addr_remove, 630 .mac_addr_set = virtio_mac_addr_set, 631 }; 632 633 static inline int 634 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev, 635 struct rte_eth_link *link) 636 { 637 struct rte_eth_link *dst = link; 638 struct rte_eth_link *src = &(dev->data->dev_link); 639 640 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 641 *(uint64_t *)src) == 0) 642 return -1; 643 644 return 0; 645 } 646 647 /** 648 * Atomically writes the link status information into global 649 * structure rte_eth_dev. 650 * 651 * @param dev 652 * - Pointer to the structure rte_eth_dev to read from. 653 * - Pointer to the buffer to be saved with the link status. 654 * 655 * @return 656 * - On success, zero. 657 * - On failure, negative value. 658 */ 659 static inline int 660 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev, 661 struct rte_eth_link *link) 662 { 663 struct rte_eth_link *dst = &(dev->data->dev_link); 664 struct rte_eth_link *src = link; 665 666 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 667 *(uint64_t *)src) == 0) 668 return -1; 669 670 return 0; 671 } 672 673 static void 674 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 675 { 676 unsigned i; 677 678 for (i = 0; i < dev->data->nb_tx_queues; i++) { 679 const struct virtqueue *txvq = dev->data->tx_queues[i]; 680 if (txvq == NULL) 681 continue; 682 683 stats->opackets += txvq->packets; 684 stats->obytes += txvq->bytes; 685 stats->oerrors += txvq->errors; 686 687 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 688 stats->q_opackets[i] = txvq->packets; 689 stats->q_obytes[i] = txvq->bytes; 690 } 691 } 692 693 for (i = 0; i < dev->data->nb_rx_queues; i++) { 694 const struct virtqueue *rxvq = dev->data->rx_queues[i]; 695 if (rxvq == NULL) 696 continue; 697 698 stats->ipackets += rxvq->packets; 699 stats->ibytes += rxvq->bytes; 700 stats->ierrors += rxvq->errors; 701 702 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 703 stats->q_ipackets[i] = rxvq->packets; 704 stats->q_ibytes[i] = rxvq->bytes; 705 } 706 } 707 708 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 709 } 710 711 static int 712 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats, 713 unsigned n) 714 { 715 unsigned i; 716 unsigned count = 0; 717 718 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_Q_XSTATS + 719 dev->data->nb_rx_queues * VIRTIO_NB_Q_XSTATS; 720 721 if (n < nstats) 722 return nstats; 723 724 for (i = 0; i < dev->data->nb_rx_queues; i++) { 725 struct virtqueue *rxvq = dev->data->rx_queues[i]; 726 727 if (rxvq == NULL) 728 continue; 729 730 unsigned t; 731 732 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) { 733 snprintf(xstats[count].name, sizeof(xstats[count].name), 734 "rx_q%u_%s", i, 735 rte_virtio_q_stat_strings[t].name); 736 xstats[count].value = *(uint64_t *)(((char *)rxvq) + 737 rte_virtio_q_stat_strings[t].offset); 738 count++; 739 } 740 } 741 742 for (i = 0; i < dev->data->nb_tx_queues; i++) { 743 struct virtqueue *txvq = dev->data->tx_queues[i]; 744 745 if (txvq == NULL) 746 continue; 747 748 unsigned t; 749 750 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) { 751 snprintf(xstats[count].name, sizeof(xstats[count].name), 752 "tx_q%u_%s", i, 753 rte_virtio_q_stat_strings[t].name); 754 xstats[count].value = *(uint64_t *)(((char *)txvq) + 755 rte_virtio_q_stat_strings[t].offset); 756 count++; 757 } 758 } 759 760 return count; 761 } 762 763 static void 764 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 765 { 766 virtio_update_stats(dev, stats); 767 } 768 769 static void 770 virtio_dev_stats_reset(struct rte_eth_dev *dev) 771 { 772 unsigned int i; 773 774 for (i = 0; i < dev->data->nb_tx_queues; i++) { 775 struct virtqueue *txvq = dev->data->tx_queues[i]; 776 if (txvq == NULL) 777 continue; 778 779 txvq->packets = 0; 780 txvq->bytes = 0; 781 txvq->errors = 0; 782 txvq->multicast = 0; 783 txvq->broadcast = 0; 784 memset(txvq->size_bins, 0, sizeof(txvq->size_bins[0]) * 8); 785 } 786 787 for (i = 0; i < dev->data->nb_rx_queues; i++) { 788 struct virtqueue *rxvq = dev->data->rx_queues[i]; 789 if (rxvq == NULL) 790 continue; 791 792 rxvq->packets = 0; 793 rxvq->bytes = 0; 794 rxvq->errors = 0; 795 rxvq->multicast = 0; 796 rxvq->broadcast = 0; 797 memset(rxvq->size_bins, 0, sizeof(rxvq->size_bins[0]) * 8); 798 } 799 } 800 801 static void 802 virtio_set_hwaddr(struct virtio_hw *hw) 803 { 804 vtpci_write_dev_config(hw, 805 offsetof(struct virtio_net_config, mac), 806 &hw->mac_addr, ETHER_ADDR_LEN); 807 } 808 809 static void 810 virtio_get_hwaddr(struct virtio_hw *hw) 811 { 812 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) { 813 vtpci_read_dev_config(hw, 814 offsetof(struct virtio_net_config, mac), 815 &hw->mac_addr, ETHER_ADDR_LEN); 816 } else { 817 eth_random_addr(&hw->mac_addr[0]); 818 virtio_set_hwaddr(hw); 819 } 820 } 821 822 static void 823 virtio_mac_table_set(struct virtio_hw *hw, 824 const struct virtio_net_ctrl_mac *uc, 825 const struct virtio_net_ctrl_mac *mc) 826 { 827 struct virtio_pmd_ctrl ctrl; 828 int err, len[2]; 829 830 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 831 PMD_DRV_LOG(INFO, "host does not support mac table"); 832 return; 833 } 834 835 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 836 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 837 838 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries); 839 memcpy(ctrl.data, uc, len[0]); 840 841 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries); 842 memcpy(ctrl.data + len[0], mc, len[1]); 843 844 err = virtio_send_command(hw->cvq, &ctrl, len, 2); 845 if (err != 0) 846 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err); 847 } 848 849 static void 850 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, 851 uint32_t index, uint32_t vmdq __rte_unused) 852 { 853 struct virtio_hw *hw = dev->data->dev_private; 854 const struct ether_addr *addrs = dev->data->mac_addrs; 855 unsigned int i; 856 struct virtio_net_ctrl_mac *uc, *mc; 857 858 if (index >= VIRTIO_MAX_MAC_ADDRS) { 859 PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 860 return; 861 } 862 863 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 864 uc->entries = 0; 865 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 866 mc->entries = 0; 867 868 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 869 const struct ether_addr *addr 870 = (i == index) ? mac_addr : addrs + i; 871 struct virtio_net_ctrl_mac *tbl 872 = is_multicast_ether_addr(addr) ? mc : uc; 873 874 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN); 875 } 876 877 virtio_mac_table_set(hw, uc, mc); 878 } 879 880 static void 881 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index) 882 { 883 struct virtio_hw *hw = dev->data->dev_private; 884 struct ether_addr *addrs = dev->data->mac_addrs; 885 struct virtio_net_ctrl_mac *uc, *mc; 886 unsigned int i; 887 888 if (index >= VIRTIO_MAX_MAC_ADDRS) { 889 PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 890 return; 891 } 892 893 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 894 uc->entries = 0; 895 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 896 mc->entries = 0; 897 898 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 899 struct virtio_net_ctrl_mac *tbl; 900 901 if (i == index || is_zero_ether_addr(addrs + i)) 902 continue; 903 904 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc; 905 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN); 906 } 907 908 virtio_mac_table_set(hw, uc, mc); 909 } 910 911 static void 912 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr) 913 { 914 struct virtio_hw *hw = dev->data->dev_private; 915 916 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN); 917 918 /* Use atomic update if available */ 919 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 920 struct virtio_pmd_ctrl ctrl; 921 int len = ETHER_ADDR_LEN; 922 923 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 924 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 925 926 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN); 927 virtio_send_command(hw->cvq, &ctrl, &len, 1); 928 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) 929 virtio_set_hwaddr(hw); 930 } 931 932 static int 933 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on) 934 { 935 struct virtio_hw *hw = dev->data->dev_private; 936 struct virtio_pmd_ctrl ctrl; 937 int len; 938 939 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) 940 return -ENOTSUP; 941 942 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN; 943 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 944 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id)); 945 len = sizeof(vlan_id); 946 947 return virtio_send_command(hw->cvq, &ctrl, &len, 1); 948 } 949 950 static int 951 virtio_negotiate_features(struct virtio_hw *hw) 952 { 953 uint64_t host_features; 954 955 /* Prepare guest_features: feature that driver wants to support */ 956 hw->guest_features = VIRTIO_PMD_GUEST_FEATURES; 957 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64, 958 hw->guest_features); 959 960 /* Read device(host) feature bits */ 961 host_features = hw->vtpci_ops->get_features(hw); 962 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64, 963 host_features); 964 965 /* 966 * Negotiate features: Subset of device feature bits are written back 967 * guest feature bits. 968 */ 969 hw->guest_features = vtpci_negotiate_features(hw, host_features); 970 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64, 971 hw->guest_features); 972 973 if (hw->modern) { 974 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) { 975 PMD_INIT_LOG(ERR, 976 "VIRTIO_F_VERSION_1 features is not enabled."); 977 return -1; 978 } 979 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK); 980 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) { 981 PMD_INIT_LOG(ERR, 982 "failed to set FEATURES_OK status!"); 983 return -1; 984 } 985 } 986 987 return 0; 988 } 989 990 /* 991 * Process Virtio Config changed interrupt and call the callback 992 * if link state changed. 993 */ 994 static void 995 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle, 996 void *param) 997 { 998 struct rte_eth_dev *dev = param; 999 struct virtio_hw *hw = dev->data->dev_private; 1000 uint8_t isr; 1001 1002 /* Read interrupt status which clears interrupt */ 1003 isr = vtpci_isr(hw); 1004 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr); 1005 1006 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) 1007 PMD_DRV_LOG(ERR, "interrupt enable failed"); 1008 1009 if (isr & VIRTIO_PCI_ISR_CONFIG) { 1010 if (virtio_dev_link_update(dev, 0) == 0) 1011 _rte_eth_dev_callback_process(dev, 1012 RTE_ETH_EVENT_INTR_LSC); 1013 } 1014 1015 } 1016 1017 static void 1018 rx_func_get(struct rte_eth_dev *eth_dev) 1019 { 1020 struct virtio_hw *hw = eth_dev->data->dev_private; 1021 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) 1022 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts; 1023 else 1024 eth_dev->rx_pkt_burst = &virtio_recv_pkts; 1025 } 1026 1027 /* 1028 * This function is based on probe() function in virtio_pci.c 1029 * It returns 0 on success. 1030 */ 1031 static int 1032 eth_virtio_dev_init(struct rte_eth_dev *eth_dev) 1033 { 1034 struct virtio_hw *hw = eth_dev->data->dev_private; 1035 struct virtio_net_config *config; 1036 struct virtio_net_config local_config; 1037 struct rte_pci_device *pci_dev; 1038 uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE; 1039 int ret; 1040 1041 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf)); 1042 1043 eth_dev->dev_ops = &virtio_eth_dev_ops; 1044 eth_dev->tx_pkt_burst = &virtio_xmit_pkts; 1045 1046 if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 1047 rx_func_get(eth_dev); 1048 return 0; 1049 } 1050 1051 /* Allocate memory for storing MAC addresses */ 1052 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0); 1053 if (eth_dev->data->mac_addrs == NULL) { 1054 PMD_INIT_LOG(ERR, 1055 "Failed to allocate %d bytes needed to store MAC addresses", 1056 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN); 1057 return -ENOMEM; 1058 } 1059 1060 pci_dev = eth_dev->pci_dev; 1061 1062 ret = vtpci_init(pci_dev, hw, &dev_flags); 1063 if (ret) 1064 return ret; 1065 1066 /* Reset the device although not necessary at startup */ 1067 vtpci_reset(hw); 1068 1069 /* Tell the host we've noticed this device. */ 1070 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK); 1071 1072 /* Tell the host we've known how to drive the device. */ 1073 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER); 1074 if (virtio_negotiate_features(hw) < 0) 1075 return -1; 1076 1077 /* If host does not support status then disable LSC */ 1078 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) 1079 dev_flags &= ~RTE_ETH_DEV_INTR_LSC; 1080 1081 rte_eth_copy_pci_info(eth_dev, pci_dev); 1082 eth_dev->data->dev_flags = dev_flags; 1083 1084 rx_func_get(eth_dev); 1085 1086 /* Setting up rx_header size for the device */ 1087 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) || 1088 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) 1089 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1090 else 1091 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 1092 1093 /* Copy the permanent MAC address to: virtio_hw */ 1094 virtio_get_hwaddr(hw); 1095 ether_addr_copy((struct ether_addr *) hw->mac_addr, 1096 ð_dev->data->mac_addrs[0]); 1097 PMD_INIT_LOG(DEBUG, 1098 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 1099 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2], 1100 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]); 1101 1102 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) { 1103 config = &local_config; 1104 1105 vtpci_read_dev_config(hw, 1106 offsetof(struct virtio_net_config, mac), 1107 &config->mac, sizeof(config->mac)); 1108 1109 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 1110 vtpci_read_dev_config(hw, 1111 offsetof(struct virtio_net_config, status), 1112 &config->status, sizeof(config->status)); 1113 } else { 1114 PMD_INIT_LOG(DEBUG, 1115 "VIRTIO_NET_F_STATUS is not supported"); 1116 config->status = 0; 1117 } 1118 1119 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) { 1120 vtpci_read_dev_config(hw, 1121 offsetof(struct virtio_net_config, max_virtqueue_pairs), 1122 &config->max_virtqueue_pairs, 1123 sizeof(config->max_virtqueue_pairs)); 1124 } else { 1125 PMD_INIT_LOG(DEBUG, 1126 "VIRTIO_NET_F_MQ is not supported"); 1127 config->max_virtqueue_pairs = 1; 1128 } 1129 1130 hw->max_rx_queues = 1131 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ? 1132 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs; 1133 hw->max_tx_queues = 1134 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ? 1135 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs; 1136 1137 virtio_dev_cq_queue_setup(eth_dev, 1138 config->max_virtqueue_pairs * 2, 1139 SOCKET_ID_ANY); 1140 1141 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d", 1142 config->max_virtqueue_pairs); 1143 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status); 1144 PMD_INIT_LOG(DEBUG, 1145 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 1146 config->mac[0], config->mac[1], 1147 config->mac[2], config->mac[3], 1148 config->mac[4], config->mac[5]); 1149 } else { 1150 hw->max_rx_queues = 1; 1151 hw->max_tx_queues = 1; 1152 } 1153 1154 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d", 1155 hw->max_rx_queues, hw->max_tx_queues); 1156 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x", 1157 eth_dev->data->port_id, pci_dev->id.vendor_id, 1158 pci_dev->id.device_id); 1159 1160 /* Setup interrupt callback */ 1161 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 1162 rte_intr_callback_register(&pci_dev->intr_handle, 1163 virtio_interrupt_handler, eth_dev); 1164 1165 virtio_dev_cq_start(eth_dev); 1166 1167 return 0; 1168 } 1169 1170 static int 1171 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev) 1172 { 1173 struct rte_pci_device *pci_dev; 1174 struct virtio_hw *hw = eth_dev->data->dev_private; 1175 1176 PMD_INIT_FUNC_TRACE(); 1177 1178 if (rte_eal_process_type() == RTE_PROC_SECONDARY) 1179 return -EPERM; 1180 1181 /* Close it anyway since there's no way to know if closed */ 1182 virtio_dev_close(eth_dev); 1183 1184 pci_dev = eth_dev->pci_dev; 1185 1186 eth_dev->dev_ops = NULL; 1187 eth_dev->tx_pkt_burst = NULL; 1188 eth_dev->rx_pkt_burst = NULL; 1189 1190 virtio_dev_queue_release(hw->cvq); 1191 1192 rte_free(eth_dev->data->mac_addrs); 1193 eth_dev->data->mac_addrs = NULL; 1194 1195 /* reset interrupt callback */ 1196 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 1197 rte_intr_callback_unregister(&pci_dev->intr_handle, 1198 virtio_interrupt_handler, 1199 eth_dev); 1200 rte_eal_pci_unmap_device(pci_dev); 1201 1202 PMD_INIT_LOG(DEBUG, "dev_uninit completed"); 1203 1204 return 0; 1205 } 1206 1207 static struct eth_driver rte_virtio_pmd = { 1208 .pci_drv = { 1209 .name = "rte_virtio_pmd", 1210 .id_table = pci_id_virtio_map, 1211 .drv_flags = RTE_PCI_DRV_DETACHABLE, 1212 }, 1213 .eth_dev_init = eth_virtio_dev_init, 1214 .eth_dev_uninit = eth_virtio_dev_uninit, 1215 .dev_private_size = sizeof(struct virtio_hw), 1216 }; 1217 1218 /* 1219 * Driver initialization routine. 1220 * Invoked once at EAL init time. 1221 * Register itself as the [Poll Mode] Driver of PCI virtio devices. 1222 * Returns 0 on success. 1223 */ 1224 static int 1225 rte_virtio_pmd_init(const char *name __rte_unused, 1226 const char *param __rte_unused) 1227 { 1228 if (rte_eal_iopl_init() != 0) { 1229 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD"); 1230 return -1; 1231 } 1232 1233 rte_eth_driver_register(&rte_virtio_pmd); 1234 return 0; 1235 } 1236 1237 /* 1238 * Configure virtio device 1239 * It returns 0 on success. 1240 */ 1241 static int 1242 virtio_dev_configure(struct rte_eth_dev *dev) 1243 { 1244 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 1245 struct virtio_hw *hw = dev->data->dev_private; 1246 1247 PMD_INIT_LOG(DEBUG, "configure"); 1248 1249 if (rxmode->hw_ip_checksum) { 1250 PMD_DRV_LOG(ERR, "HW IP checksum not supported"); 1251 return -EINVAL; 1252 } 1253 1254 hw->vlan_strip = rxmode->hw_vlan_strip; 1255 1256 if (rxmode->hw_vlan_filter 1257 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) { 1258 PMD_DRV_LOG(NOTICE, 1259 "vlan filtering not available on this host"); 1260 return -ENOTSUP; 1261 } 1262 1263 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 1264 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) { 1265 PMD_DRV_LOG(ERR, "failed to set config vector"); 1266 return -EBUSY; 1267 } 1268 1269 return 0; 1270 } 1271 1272 1273 static int 1274 virtio_dev_start(struct rte_eth_dev *dev) 1275 { 1276 uint16_t nb_queues, i; 1277 struct virtio_hw *hw = dev->data->dev_private; 1278 1279 /* check if lsc interrupt feature is enabled */ 1280 if (dev->data->dev_conf.intr_conf.lsc) { 1281 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) { 1282 PMD_DRV_LOG(ERR, "link status not supported by host"); 1283 return -ENOTSUP; 1284 } 1285 1286 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) { 1287 PMD_DRV_LOG(ERR, "interrupt enable failed"); 1288 return -EIO; 1289 } 1290 } 1291 1292 /* Initialize Link state */ 1293 virtio_dev_link_update(dev, 0); 1294 1295 /* On restart after stop do not touch queues */ 1296 if (hw->started) 1297 return 0; 1298 1299 /* Do final configuration before rx/tx engine starts */ 1300 virtio_dev_rxtx_start(dev); 1301 vtpci_reinit_complete(hw); 1302 1303 hw->started = 1; 1304 1305 /*Notify the backend 1306 *Otherwise the tap backend might already stop its queue due to fullness. 1307 *vhost backend will have no chance to be waked up 1308 */ 1309 nb_queues = dev->data->nb_rx_queues; 1310 if (nb_queues > 1) { 1311 if (virtio_set_multiple_queues(dev, nb_queues) != 0) 1312 return -EINVAL; 1313 } 1314 1315 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues); 1316 1317 for (i = 0; i < nb_queues; i++) 1318 virtqueue_notify(dev->data->rx_queues[i]); 1319 1320 PMD_INIT_LOG(DEBUG, "Notified backend at initialization"); 1321 1322 for (i = 0; i < dev->data->nb_rx_queues; i++) 1323 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]); 1324 1325 for (i = 0; i < dev->data->nb_tx_queues; i++) 1326 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]); 1327 1328 return 0; 1329 } 1330 1331 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev) 1332 { 1333 struct rte_mbuf *buf; 1334 int i, mbuf_num = 0; 1335 1336 for (i = 0; i < dev->data->nb_rx_queues; i++) { 1337 PMD_INIT_LOG(DEBUG, 1338 "Before freeing rxq[%d] used and unused buf", i); 1339 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]); 1340 1341 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", 1342 i, dev->data->rx_queues[i]); 1343 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused( 1344 dev->data->rx_queues[i])) != NULL) { 1345 rte_pktmbuf_free(buf); 1346 mbuf_num++; 1347 } 1348 1349 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 1350 PMD_INIT_LOG(DEBUG, 1351 "After freeing rxq[%d] used and unused buf", i); 1352 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]); 1353 } 1354 1355 for (i = 0; i < dev->data->nb_tx_queues; i++) { 1356 PMD_INIT_LOG(DEBUG, 1357 "Before freeing txq[%d] used and unused bufs", 1358 i); 1359 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]); 1360 1361 mbuf_num = 0; 1362 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused( 1363 dev->data->tx_queues[i])) != NULL) { 1364 rte_pktmbuf_free(buf); 1365 1366 mbuf_num++; 1367 } 1368 1369 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 1370 PMD_INIT_LOG(DEBUG, 1371 "After freeing txq[%d] used and unused buf", i); 1372 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]); 1373 } 1374 } 1375 1376 /* 1377 * Stop device: disable interrupt and mark link down 1378 */ 1379 static void 1380 virtio_dev_stop(struct rte_eth_dev *dev) 1381 { 1382 struct rte_eth_link link; 1383 struct virtio_hw *hw = dev->data->dev_private; 1384 1385 PMD_INIT_LOG(DEBUG, "stop"); 1386 1387 hw->started = 0; 1388 1389 if (dev->data->dev_conf.intr_conf.lsc) 1390 rte_intr_disable(&dev->pci_dev->intr_handle); 1391 1392 memset(&link, 0, sizeof(link)); 1393 virtio_dev_atomic_write_link_status(dev, &link); 1394 } 1395 1396 static int 1397 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 1398 { 1399 struct rte_eth_link link, old; 1400 uint16_t status; 1401 struct virtio_hw *hw = dev->data->dev_private; 1402 memset(&link, 0, sizeof(link)); 1403 virtio_dev_atomic_read_link_status(dev, &link); 1404 old = link; 1405 link.link_duplex = ETH_LINK_FULL_DUPLEX; 1406 link.link_speed = SPEED_10G; 1407 1408 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 1409 PMD_INIT_LOG(DEBUG, "Get link status from hw"); 1410 vtpci_read_dev_config(hw, 1411 offsetof(struct virtio_net_config, status), 1412 &status, sizeof(status)); 1413 if ((status & VIRTIO_NET_S_LINK_UP) == 0) { 1414 link.link_status = ETH_LINK_DOWN; 1415 PMD_INIT_LOG(DEBUG, "Port %d is down", 1416 dev->data->port_id); 1417 } else { 1418 link.link_status = ETH_LINK_UP; 1419 PMD_INIT_LOG(DEBUG, "Port %d is up", 1420 dev->data->port_id); 1421 } 1422 } else { 1423 link.link_status = ETH_LINK_UP; 1424 } 1425 virtio_dev_atomic_write_link_status(dev, &link); 1426 1427 return (old.link_status == link.link_status) ? -1 : 0; 1428 } 1429 1430 static void 1431 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 1432 { 1433 struct virtio_hw *hw = dev->data->dev_private; 1434 1435 dev_info->driver_name = dev->driver->pci_drv.name; 1436 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues; 1437 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues; 1438 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE; 1439 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN; 1440 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS; 1441 dev_info->default_txconf = (struct rte_eth_txconf) { 1442 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS 1443 }; 1444 } 1445 1446 /* 1447 * It enables testpmd to collect per queue stats. 1448 */ 1449 static int 1450 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev, 1451 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx, 1452 __rte_unused uint8_t is_rx) 1453 { 1454 return 0; 1455 } 1456 1457 static struct rte_driver rte_virtio_driver = { 1458 .type = PMD_PDEV, 1459 .init = rte_virtio_pmd_init, 1460 }; 1461 1462 PMD_REGISTER_DRIVER(rte_virtio_driver); 1463