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