net/virtio: optimize flags update for packed ring
[dpdk.git] / drivers / net / virtio / virtio_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <unistd.h>
10
11 #include <rte_ethdev_driver.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_memcpy.h>
14 #include <rte_string_fns.h>
15 #include <rte_memzone.h>
16 #include <rte_malloc.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_pci.h>
19 #include <rte_bus_pci.h>
20 #include <rte_ether.h>
21 #include <rte_ip.h>
22 #include <rte_arp.h>
23 #include <rte_common.h>
24 #include <rte_errno.h>
25 #include <rte_cpuflags.h>
26
27 #include <rte_memory.h>
28 #include <rte_eal.h>
29 #include <rte_dev.h>
30 #include <rte_cycles.h>
31 #include <rte_kvargs.h>
32
33 #include "virtio_ethdev.h"
34 #include "virtio_pci.h"
35 #include "virtio_logs.h"
36 #include "virtqueue.h"
37 #include "virtio_rxtx.h"
38
39 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
40 static int  virtio_dev_configure(struct rte_eth_dev *dev);
41 static int  virtio_dev_start(struct rte_eth_dev *dev);
42 static void virtio_dev_stop(struct rte_eth_dev *dev);
43 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
44 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
45 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
46 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
47 static void virtio_dev_info_get(struct rte_eth_dev *dev,
48                                 struct rte_eth_dev_info *dev_info);
49 static int virtio_dev_link_update(struct rte_eth_dev *dev,
50         int wait_to_complete);
51 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
52
53 static void virtio_set_hwaddr(struct virtio_hw *hw);
54 static void virtio_get_hwaddr(struct virtio_hw *hw);
55
56 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
57                                  struct rte_eth_stats *stats);
58 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
59                                  struct rte_eth_xstat *xstats, unsigned n);
60 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
61                                        struct rte_eth_xstat_name *xstats_names,
62                                        unsigned limit);
63 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
64 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
65 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
66                                 uint16_t vlan_id, int on);
67 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
68                                 struct ether_addr *mac_addr,
69                                 uint32_t index, uint32_t vmdq);
70 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
71 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
72                                 struct ether_addr *mac_addr);
73
74 static int virtio_intr_disable(struct rte_eth_dev *dev);
75
76 static int virtio_dev_queue_stats_mapping_set(
77         struct rte_eth_dev *eth_dev,
78         uint16_t queue_id,
79         uint8_t stat_idx,
80         uint8_t is_rx);
81
82 int virtio_logtype_init;
83 int virtio_logtype_driver;
84
85 static void virtio_notify_peers(struct rte_eth_dev *dev);
86 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
87
88 /*
89  * The set of PCI devices this driver supports
90  */
91 static const struct rte_pci_id pci_id_virtio_map[] = {
92         { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
93         { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
94         { .vendor_id = 0, /* sentinel */ },
95 };
96
97 struct rte_virtio_xstats_name_off {
98         char name[RTE_ETH_XSTATS_NAME_SIZE];
99         unsigned offset;
100 };
101
102 /* [rt]x_qX_ is prepended to the name string here */
103 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
104         {"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
105         {"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
106         {"errors",                 offsetof(struct virtnet_rx, stats.errors)},
107         {"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
108         {"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
109         {"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
110         {"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
111         {"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
112         {"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
113         {"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
114         {"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
115         {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
116         {"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
117 };
118
119 /* [rt]x_qX_ is prepended to the name string here */
120 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
121         {"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
122         {"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
123         {"errors",                 offsetof(struct virtnet_tx, stats.errors)},
124         {"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
125         {"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
126         {"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
127         {"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
128         {"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
129         {"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
130         {"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
131         {"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
132         {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
133         {"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
134 };
135
136 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
137                             sizeof(rte_virtio_rxq_stat_strings[0]))
138 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
139                             sizeof(rte_virtio_txq_stat_strings[0]))
140
141 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
142
143 static struct virtio_pmd_ctrl *
144 virtio_send_command_packed(struct virtnet_ctl *cvq,
145                            struct virtio_pmd_ctrl *ctrl,
146                            int *dlen, int pkt_num)
147 {
148         struct virtqueue *vq = cvq->vq;
149         int head;
150         struct vring_packed_desc *desc = vq->ring_packed.desc_packed;
151         struct virtio_pmd_ctrl *result;
152         uint16_t flags;
153         int sum = 0;
154         int nb_descs = 0;
155         int k;
156
157         /*
158          * Format is enforced in qemu code:
159          * One TX packet for header;
160          * At least one TX packet per argument;
161          * One RX packet for ACK.
162          */
163         head = vq->vq_avail_idx;
164         flags = vq->cached_flags;
165         desc[head].addr = cvq->virtio_net_hdr_mem;
166         desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
167         vq->vq_free_cnt--;
168         nb_descs++;
169         if (++vq->vq_avail_idx >= vq->vq_nentries) {
170                 vq->vq_avail_idx -= vq->vq_nentries;
171                 vq->cached_flags ^=
172                         VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
173         }
174
175         for (k = 0; k < pkt_num; k++) {
176                 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
177                         + sizeof(struct virtio_net_ctrl_hdr)
178                         + sizeof(ctrl->status) + sizeof(uint8_t) * sum;
179                 desc[vq->vq_avail_idx].len = dlen[k];
180                 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
181                         vq->cached_flags;
182                 sum += dlen[k];
183                 vq->vq_free_cnt--;
184                 nb_descs++;
185                 if (++vq->vq_avail_idx >= vq->vq_nentries) {
186                         vq->vq_avail_idx -= vq->vq_nentries;
187                         vq->cached_flags ^=
188                                 VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
189                 }
190         }
191
192         desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
193                 + sizeof(struct virtio_net_ctrl_hdr);
194         desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
195         desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE | vq->cached_flags;
196         vq->vq_free_cnt--;
197         nb_descs++;
198         if (++vq->vq_avail_idx >= vq->vq_nentries) {
199                 vq->vq_avail_idx -= vq->vq_nentries;
200                 vq->cached_flags ^=
201                         VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
202         }
203
204         virtio_wmb(vq->hw->weak_barriers);
205         desc[head].flags = VRING_DESC_F_NEXT | flags;
206
207         virtio_wmb(vq->hw->weak_barriers);
208         virtqueue_notify(vq);
209
210         /* wait for used descriptors in virtqueue */
211         while (!desc_is_used(&desc[head], vq))
212                 usleep(100);
213
214         virtio_rmb(vq->hw->weak_barriers);
215
216         /* now get used descriptors */
217         vq->vq_free_cnt += nb_descs;
218         vq->vq_used_cons_idx += nb_descs;
219         if (vq->vq_used_cons_idx >= vq->vq_nentries) {
220                 vq->vq_used_cons_idx -= vq->vq_nentries;
221                 vq->used_wrap_counter ^= 1;
222         }
223
224         PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
225                         "vq->vq_avail_idx=%d\n"
226                         "vq->vq_used_cons_idx=%d\n"
227                         "vq->cached_flags=0x%x\n"
228                         "vq->used_wrap_counter=%d\n",
229                         vq->vq_free_cnt,
230                         vq->vq_avail_idx,
231                         vq->vq_used_cons_idx,
232                         vq->cached_flags,
233                         vq->used_wrap_counter);
234
235         result = cvq->virtio_net_hdr_mz->addr;
236         return result;
237 }
238
239 static int
240 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
241                 int *dlen, int pkt_num)
242 {
243         uint32_t head, i;
244         int k, sum = 0;
245         virtio_net_ctrl_ack status = ~0;
246         struct virtio_pmd_ctrl *result;
247         struct virtqueue *vq;
248
249         ctrl->status = status;
250
251         if (!cvq || !cvq->vq) {
252                 PMD_INIT_LOG(ERR, "Control queue is not supported.");
253                 return -1;
254         }
255
256         rte_spinlock_lock(&cvq->lock);
257         vq = cvq->vq;
258         head = vq->vq_desc_head_idx;
259
260         PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
261                 "vq->hw->cvq = %p vq = %p",
262                 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
263
264         if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
265                 rte_spinlock_unlock(&cvq->lock);
266                 return -1;
267         }
268
269         memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
270                 sizeof(struct virtio_pmd_ctrl));
271
272         if (vtpci_packed_queue(vq->hw)) {
273                 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
274                 goto out_unlock;
275         }
276
277         /*
278          * Format is enforced in qemu code:
279          * One TX packet for header;
280          * At least one TX packet per argument;
281          * One RX packet for ACK.
282          */
283         vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
284         vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
285         vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
286         vq->vq_free_cnt--;
287         i = vq->vq_ring.desc[head].next;
288
289         for (k = 0; k < pkt_num; k++) {
290                 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
291                 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
292                         + sizeof(struct virtio_net_ctrl_hdr)
293                         + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
294                 vq->vq_ring.desc[i].len = dlen[k];
295                 sum += dlen[k];
296                 vq->vq_free_cnt--;
297                 i = vq->vq_ring.desc[i].next;
298         }
299
300         vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
301         vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
302                         + sizeof(struct virtio_net_ctrl_hdr);
303         vq->vq_ring.desc[i].len = sizeof(ctrl->status);
304         vq->vq_free_cnt--;
305
306         vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
307
308         vq_update_avail_ring(vq, head);
309         vq_update_avail_idx(vq);
310
311         PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
312
313         virtqueue_notify(vq);
314
315         rte_rmb();
316         while (VIRTQUEUE_NUSED(vq) == 0) {
317                 rte_rmb();
318                 usleep(100);
319         }
320
321         while (VIRTQUEUE_NUSED(vq)) {
322                 uint32_t idx, desc_idx, used_idx;
323                 struct vring_used_elem *uep;
324
325                 used_idx = (uint32_t)(vq->vq_used_cons_idx
326                                 & (vq->vq_nentries - 1));
327                 uep = &vq->vq_ring.used->ring[used_idx];
328                 idx = (uint32_t) uep->id;
329                 desc_idx = idx;
330
331                 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
332                         desc_idx = vq->vq_ring.desc[desc_idx].next;
333                         vq->vq_free_cnt++;
334                 }
335
336                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
337                 vq->vq_desc_head_idx = idx;
338
339                 vq->vq_used_cons_idx++;
340                 vq->vq_free_cnt++;
341         }
342
343         PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
344                         vq->vq_free_cnt, vq->vq_desc_head_idx);
345
346         result = cvq->virtio_net_hdr_mz->addr;
347
348 out_unlock:
349         rte_spinlock_unlock(&cvq->lock);
350         return result->status;
351 }
352
353 static int
354 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
355 {
356         struct virtio_hw *hw = dev->data->dev_private;
357         struct virtio_pmd_ctrl ctrl;
358         int dlen[1];
359         int ret;
360
361         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
362         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
363         memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
364
365         dlen[0] = sizeof(uint16_t);
366
367         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
368         if (ret) {
369                 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
370                           "failed, this is too late now...");
371                 return -EINVAL;
372         }
373
374         return 0;
375 }
376
377 static void
378 virtio_dev_queue_release(void *queue __rte_unused)
379 {
380         /* do nothing */
381 }
382
383 static uint16_t
384 virtio_get_nr_vq(struct virtio_hw *hw)
385 {
386         uint16_t nr_vq = hw->max_queue_pairs * 2;
387
388         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
389                 nr_vq += 1;
390
391         return nr_vq;
392 }
393
394 static void
395 virtio_init_vring(struct virtqueue *vq)
396 {
397         int size = vq->vq_nentries;
398         struct vring *vr = &vq->vq_ring;
399         uint8_t *ring_mem = vq->vq_ring_virt_mem;
400
401         PMD_INIT_FUNC_TRACE();
402
403         memset(ring_mem, 0, vq->vq_ring_size);
404
405         vq->vq_used_cons_idx = 0;
406         vq->vq_desc_head_idx = 0;
407         vq->vq_avail_idx = 0;
408         vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
409         vq->vq_free_cnt = vq->vq_nentries;
410         memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
411         if (vtpci_packed_queue(vq->hw)) {
412                 vring_init_packed(&vq->ring_packed, ring_mem,
413                                   VIRTIO_PCI_VRING_ALIGN, size);
414                 vring_desc_init_packed(vq, size);
415         } else {
416                 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
417                 vring_desc_init_split(vr->desc, size);
418         }
419         /*
420          * Disable device(host) interrupting guest
421          */
422         virtqueue_disable_intr(vq);
423 }
424
425 static int
426 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
427 {
428         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
429         char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
430         const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
431         unsigned int vq_size, size;
432         struct virtio_hw *hw = dev->data->dev_private;
433         struct virtnet_rx *rxvq = NULL;
434         struct virtnet_tx *txvq = NULL;
435         struct virtnet_ctl *cvq = NULL;
436         struct virtqueue *vq;
437         size_t sz_hdr_mz = 0;
438         void *sw_ring = NULL;
439         int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
440         int ret;
441         int numa_node = dev->device->numa_node;
442
443         PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
444                         vtpci_queue_idx, numa_node);
445
446         /*
447          * Read the virtqueue size from the Queue Size field
448          * Always power of 2 and if 0 virtqueue does not exist
449          */
450         vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
451         PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
452         if (vq_size == 0) {
453                 PMD_INIT_LOG(ERR, "virtqueue does not exist");
454                 return -EINVAL;
455         }
456
457         if (!rte_is_power_of_2(vq_size)) {
458                 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
459                 return -EINVAL;
460         }
461
462         snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
463                  dev->data->port_id, vtpci_queue_idx);
464
465         size = RTE_ALIGN_CEIL(sizeof(*vq) +
466                                 vq_size * sizeof(struct vq_desc_extra),
467                                 RTE_CACHE_LINE_SIZE);
468         if (queue_type == VTNET_TQ) {
469                 /*
470                  * For each xmit packet, allocate a virtio_net_hdr
471                  * and indirect ring elements
472                  */
473                 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
474         } else if (queue_type == VTNET_CQ) {
475                 /* Allocate a page for control vq command, data and status */
476                 sz_hdr_mz = PAGE_SIZE;
477         }
478
479         vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
480                                 numa_node);
481         if (vq == NULL) {
482                 PMD_INIT_LOG(ERR, "can not allocate vq");
483                 return -ENOMEM;
484         }
485         hw->vqs[vtpci_queue_idx] = vq;
486
487         vq->hw = hw;
488         vq->vq_queue_index = vtpci_queue_idx;
489         vq->vq_nentries = vq_size;
490         vq->event_flags_shadow = 0;
491         if (vtpci_packed_queue(hw)) {
492                 vq->used_wrap_counter = 1;
493                 vq->cached_flags = VRING_DESC_F_AVAIL(1);
494                 if (queue_type == VTNET_RQ)
495                         vq->cached_flags |= VRING_DESC_F_WRITE;
496         }
497
498         /*
499          * Reserve a memzone for vring elements
500          */
501         size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
502         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
503         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
504                      size, vq->vq_ring_size);
505
506         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
507                         numa_node, RTE_MEMZONE_IOVA_CONTIG,
508                         VIRTIO_PCI_VRING_ALIGN);
509         if (mz == NULL) {
510                 if (rte_errno == EEXIST)
511                         mz = rte_memzone_lookup(vq_name);
512                 if (mz == NULL) {
513                         ret = -ENOMEM;
514                         goto fail_q_alloc;
515                 }
516         }
517
518         memset(mz->addr, 0, mz->len);
519
520         vq->vq_ring_mem = mz->iova;
521         vq->vq_ring_virt_mem = mz->addr;
522         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
523                      (uint64_t)mz->iova);
524         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
525                      (uint64_t)(uintptr_t)mz->addr);
526
527         virtio_init_vring(vq);
528
529         if (sz_hdr_mz) {
530                 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
531                          dev->data->port_id, vtpci_queue_idx);
532                 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
533                                 numa_node, RTE_MEMZONE_IOVA_CONTIG,
534                                 RTE_CACHE_LINE_SIZE);
535                 if (hdr_mz == NULL) {
536                         if (rte_errno == EEXIST)
537                                 hdr_mz = rte_memzone_lookup(vq_hdr_name);
538                         if (hdr_mz == NULL) {
539                                 ret = -ENOMEM;
540                                 goto fail_q_alloc;
541                         }
542                 }
543         }
544
545         if (queue_type == VTNET_RQ) {
546                 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
547                                sizeof(vq->sw_ring[0]);
548
549                 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
550                                 RTE_CACHE_LINE_SIZE, numa_node);
551                 if (!sw_ring) {
552                         PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
553                         ret = -ENOMEM;
554                         goto fail_q_alloc;
555                 }
556
557                 vq->sw_ring = sw_ring;
558                 rxvq = &vq->rxq;
559                 rxvq->vq = vq;
560                 rxvq->port_id = dev->data->port_id;
561                 rxvq->mz = mz;
562         } else if (queue_type == VTNET_TQ) {
563                 txvq = &vq->txq;
564                 txvq->vq = vq;
565                 txvq->port_id = dev->data->port_id;
566                 txvq->mz = mz;
567                 txvq->virtio_net_hdr_mz = hdr_mz;
568                 txvq->virtio_net_hdr_mem = hdr_mz->iova;
569         } else if (queue_type == VTNET_CQ) {
570                 cvq = &vq->cq;
571                 cvq->vq = vq;
572                 cvq->mz = mz;
573                 cvq->virtio_net_hdr_mz = hdr_mz;
574                 cvq->virtio_net_hdr_mem = hdr_mz->iova;
575                 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
576
577                 hw->cvq = cvq;
578         }
579
580         /* For virtio_user case (that is when hw->dev is NULL), we use
581          * virtual address. And we need properly set _offset_, please see
582          * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
583          */
584         if (!hw->virtio_user_dev)
585                 vq->offset = offsetof(struct rte_mbuf, buf_iova);
586         else {
587                 vq->vq_ring_mem = (uintptr_t)mz->addr;
588                 vq->offset = offsetof(struct rte_mbuf, buf_addr);
589                 if (queue_type == VTNET_TQ)
590                         txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
591                 else if (queue_type == VTNET_CQ)
592                         cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
593         }
594
595         if (queue_type == VTNET_TQ) {
596                 struct virtio_tx_region *txr;
597                 unsigned int i;
598
599                 txr = hdr_mz->addr;
600                 memset(txr, 0, vq_size * sizeof(*txr));
601                 for (i = 0; i < vq_size; i++) {
602                         struct vring_desc *start_dp = txr[i].tx_indir;
603                         struct vring_packed_desc *start_dp_packed =
604                                 txr[i].tx_indir_pq;
605
606                         /* first indirect descriptor is always the tx header */
607                         if (vtpci_packed_queue(hw)) {
608                                 start_dp_packed->addr = txvq->virtio_net_hdr_mem
609                                         + i * sizeof(*txr)
610                                         + offsetof(struct virtio_tx_region,
611                                                    tx_hdr);
612                                 start_dp_packed->len = hw->vtnet_hdr_size;
613                         } else {
614                                 vring_desc_init_split(start_dp,
615                                                       RTE_DIM(txr[i].tx_indir));
616                                 start_dp->addr = txvq->virtio_net_hdr_mem
617                                         + i * sizeof(*txr)
618                                         + offsetof(struct virtio_tx_region,
619                                                    tx_hdr);
620                                 start_dp->len = hw->vtnet_hdr_size;
621                                 start_dp->flags = VRING_DESC_F_NEXT;
622                         }
623                 }
624         }
625
626         if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
627                 PMD_INIT_LOG(ERR, "setup_queue failed");
628                 return -EINVAL;
629         }
630
631         return 0;
632
633 fail_q_alloc:
634         rte_free(sw_ring);
635         rte_memzone_free(hdr_mz);
636         rte_memzone_free(mz);
637         rte_free(vq);
638
639         return ret;
640 }
641
642 static void
643 virtio_free_queues(struct virtio_hw *hw)
644 {
645         uint16_t nr_vq = virtio_get_nr_vq(hw);
646         struct virtqueue *vq;
647         int queue_type;
648         uint16_t i;
649
650         if (hw->vqs == NULL)
651                 return;
652
653         for (i = 0; i < nr_vq; i++) {
654                 vq = hw->vqs[i];
655                 if (!vq)
656                         continue;
657
658                 queue_type = virtio_get_queue_type(hw, i);
659                 if (queue_type == VTNET_RQ) {
660                         rte_free(vq->sw_ring);
661                         rte_memzone_free(vq->rxq.mz);
662                 } else if (queue_type == VTNET_TQ) {
663                         rte_memzone_free(vq->txq.mz);
664                         rte_memzone_free(vq->txq.virtio_net_hdr_mz);
665                 } else {
666                         rte_memzone_free(vq->cq.mz);
667                         rte_memzone_free(vq->cq.virtio_net_hdr_mz);
668                 }
669
670                 rte_free(vq);
671                 hw->vqs[i] = NULL;
672         }
673
674         rte_free(hw->vqs);
675         hw->vqs = NULL;
676 }
677
678 static int
679 virtio_alloc_queues(struct rte_eth_dev *dev)
680 {
681         struct virtio_hw *hw = dev->data->dev_private;
682         uint16_t nr_vq = virtio_get_nr_vq(hw);
683         uint16_t i;
684         int ret;
685
686         hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
687         if (!hw->vqs) {
688                 PMD_INIT_LOG(ERR, "failed to allocate vqs");
689                 return -ENOMEM;
690         }
691
692         for (i = 0; i < nr_vq; i++) {
693                 ret = virtio_init_queue(dev, i);
694                 if (ret < 0) {
695                         virtio_free_queues(hw);
696                         return ret;
697                 }
698         }
699
700         return 0;
701 }
702
703 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
704
705 static void
706 virtio_dev_close(struct rte_eth_dev *dev)
707 {
708         struct virtio_hw *hw = dev->data->dev_private;
709         struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
710
711         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
712
713         if (!hw->opened)
714                 return;
715         hw->opened = false;
716
717         /* reset the NIC */
718         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
719                 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
720         if (intr_conf->rxq)
721                 virtio_queues_unbind_intr(dev);
722
723         if (intr_conf->lsc || intr_conf->rxq) {
724                 virtio_intr_disable(dev);
725                 rte_intr_efd_disable(dev->intr_handle);
726                 rte_free(dev->intr_handle->intr_vec);
727                 dev->intr_handle->intr_vec = NULL;
728         }
729
730         vtpci_reset(hw);
731         virtio_dev_free_mbufs(dev);
732         virtio_free_queues(hw);
733 }
734
735 static void
736 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
737 {
738         struct virtio_hw *hw = dev->data->dev_private;
739         struct virtio_pmd_ctrl ctrl;
740         int dlen[1];
741         int ret;
742
743         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
744                 PMD_INIT_LOG(INFO, "host does not support rx control");
745                 return;
746         }
747
748         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
749         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
750         ctrl.data[0] = 1;
751         dlen[0] = 1;
752
753         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
754         if (ret)
755                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
756 }
757
758 static void
759 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
760 {
761         struct virtio_hw *hw = dev->data->dev_private;
762         struct virtio_pmd_ctrl ctrl;
763         int dlen[1];
764         int ret;
765
766         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
767                 PMD_INIT_LOG(INFO, "host does not support rx control");
768                 return;
769         }
770
771         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
772         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
773         ctrl.data[0] = 0;
774         dlen[0] = 1;
775
776         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
777         if (ret)
778                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
779 }
780
781 static void
782 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
783 {
784         struct virtio_hw *hw = dev->data->dev_private;
785         struct virtio_pmd_ctrl ctrl;
786         int dlen[1];
787         int ret;
788
789         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
790                 PMD_INIT_LOG(INFO, "host does not support rx control");
791                 return;
792         }
793
794         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
795         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
796         ctrl.data[0] = 1;
797         dlen[0] = 1;
798
799         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
800         if (ret)
801                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
802 }
803
804 static void
805 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
806 {
807         struct virtio_hw *hw = dev->data->dev_private;
808         struct virtio_pmd_ctrl ctrl;
809         int dlen[1];
810         int ret;
811
812         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
813                 PMD_INIT_LOG(INFO, "host does not support rx control");
814                 return;
815         }
816
817         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
818         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
819         ctrl.data[0] = 0;
820         dlen[0] = 1;
821
822         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
823         if (ret)
824                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
825 }
826
827 #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
828 static int
829 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
830 {
831         struct virtio_hw *hw = dev->data->dev_private;
832         uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
833                                  hw->vtnet_hdr_size;
834         uint32_t frame_size = mtu + ether_hdr_len;
835         uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
836
837         max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
838
839         if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
840                 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
841                         ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
842                 return -EINVAL;
843         }
844         return 0;
845 }
846
847 static int
848 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
849 {
850         struct virtio_hw *hw = dev->data->dev_private;
851         struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
852         struct virtqueue *vq = rxvq->vq;
853
854         virtqueue_enable_intr(vq);
855         virtio_mb(hw->weak_barriers);
856         return 0;
857 }
858
859 static int
860 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
861 {
862         struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
863         struct virtqueue *vq = rxvq->vq;
864
865         virtqueue_disable_intr(vq);
866         return 0;
867 }
868
869 /*
870  * dev_ops for virtio, bare necessities for basic operation
871  */
872 static const struct eth_dev_ops virtio_eth_dev_ops = {
873         .dev_configure           = virtio_dev_configure,
874         .dev_start               = virtio_dev_start,
875         .dev_stop                = virtio_dev_stop,
876         .dev_close               = virtio_dev_close,
877         .promiscuous_enable      = virtio_dev_promiscuous_enable,
878         .promiscuous_disable     = virtio_dev_promiscuous_disable,
879         .allmulticast_enable     = virtio_dev_allmulticast_enable,
880         .allmulticast_disable    = virtio_dev_allmulticast_disable,
881         .mtu_set                 = virtio_mtu_set,
882         .dev_infos_get           = virtio_dev_info_get,
883         .stats_get               = virtio_dev_stats_get,
884         .xstats_get              = virtio_dev_xstats_get,
885         .xstats_get_names        = virtio_dev_xstats_get_names,
886         .stats_reset             = virtio_dev_stats_reset,
887         .xstats_reset            = virtio_dev_stats_reset,
888         .link_update             = virtio_dev_link_update,
889         .vlan_offload_set        = virtio_dev_vlan_offload_set,
890         .rx_queue_setup          = virtio_dev_rx_queue_setup,
891         .rx_queue_intr_enable    = virtio_dev_rx_queue_intr_enable,
892         .rx_queue_intr_disable   = virtio_dev_rx_queue_intr_disable,
893         .rx_queue_release        = virtio_dev_queue_release,
894         .rx_descriptor_done      = virtio_dev_rx_queue_done,
895         .tx_queue_setup          = virtio_dev_tx_queue_setup,
896         .tx_queue_release        = virtio_dev_queue_release,
897         /* collect stats per queue */
898         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
899         .vlan_filter_set         = virtio_vlan_filter_set,
900         .mac_addr_add            = virtio_mac_addr_add,
901         .mac_addr_remove         = virtio_mac_addr_remove,
902         .mac_addr_set            = virtio_mac_addr_set,
903 };
904
905 static void
906 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
907 {
908         unsigned i;
909
910         for (i = 0; i < dev->data->nb_tx_queues; i++) {
911                 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
912                 if (txvq == NULL)
913                         continue;
914
915                 stats->opackets += txvq->stats.packets;
916                 stats->obytes += txvq->stats.bytes;
917                 stats->oerrors += txvq->stats.errors;
918
919                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
920                         stats->q_opackets[i] = txvq->stats.packets;
921                         stats->q_obytes[i] = txvq->stats.bytes;
922                 }
923         }
924
925         for (i = 0; i < dev->data->nb_rx_queues; i++) {
926                 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
927                 if (rxvq == NULL)
928                         continue;
929
930                 stats->ipackets += rxvq->stats.packets;
931                 stats->ibytes += rxvq->stats.bytes;
932                 stats->ierrors += rxvq->stats.errors;
933
934                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
935                         stats->q_ipackets[i] = rxvq->stats.packets;
936                         stats->q_ibytes[i] = rxvq->stats.bytes;
937                 }
938         }
939
940         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
941 }
942
943 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
944                                        struct rte_eth_xstat_name *xstats_names,
945                                        __rte_unused unsigned limit)
946 {
947         unsigned i;
948         unsigned count = 0;
949         unsigned t;
950
951         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
952                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
953
954         if (xstats_names != NULL) {
955                 /* Note: limit checked in rte_eth_xstats_names() */
956
957                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
958                         struct virtnet_rx *rxvq = dev->data->rx_queues[i];
959                         if (rxvq == NULL)
960                                 continue;
961                         for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
962                                 snprintf(xstats_names[count].name,
963                                         sizeof(xstats_names[count].name),
964                                         "rx_q%u_%s", i,
965                                         rte_virtio_rxq_stat_strings[t].name);
966                                 count++;
967                         }
968                 }
969
970                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
971                         struct virtnet_tx *txvq = dev->data->tx_queues[i];
972                         if (txvq == NULL)
973                                 continue;
974                         for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
975                                 snprintf(xstats_names[count].name,
976                                         sizeof(xstats_names[count].name),
977                                         "tx_q%u_%s", i,
978                                         rte_virtio_txq_stat_strings[t].name);
979                                 count++;
980                         }
981                 }
982                 return count;
983         }
984         return nstats;
985 }
986
987 static int
988 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
989                       unsigned n)
990 {
991         unsigned i;
992         unsigned count = 0;
993
994         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
995                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
996
997         if (n < nstats)
998                 return nstats;
999
1000         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1001                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1002
1003                 if (rxvq == NULL)
1004                         continue;
1005
1006                 unsigned t;
1007
1008                 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1009                         xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1010                                 rte_virtio_rxq_stat_strings[t].offset);
1011                         xstats[count].id = count;
1012                         count++;
1013                 }
1014         }
1015
1016         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1017                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1018
1019                 if (txvq == NULL)
1020                         continue;
1021
1022                 unsigned t;
1023
1024                 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1025                         xstats[count].value = *(uint64_t *)(((char *)txvq) +
1026                                 rte_virtio_txq_stat_strings[t].offset);
1027                         xstats[count].id = count;
1028                         count++;
1029                 }
1030         }
1031
1032         return count;
1033 }
1034
1035 static int
1036 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1037 {
1038         virtio_update_stats(dev, stats);
1039
1040         return 0;
1041 }
1042
1043 static void
1044 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1045 {
1046         unsigned int i;
1047
1048         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1049                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1050                 if (txvq == NULL)
1051                         continue;
1052
1053                 txvq->stats.packets = 0;
1054                 txvq->stats.bytes = 0;
1055                 txvq->stats.errors = 0;
1056                 txvq->stats.multicast = 0;
1057                 txvq->stats.broadcast = 0;
1058                 memset(txvq->stats.size_bins, 0,
1059                        sizeof(txvq->stats.size_bins[0]) * 8);
1060         }
1061
1062         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1063                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1064                 if (rxvq == NULL)
1065                         continue;
1066
1067                 rxvq->stats.packets = 0;
1068                 rxvq->stats.bytes = 0;
1069                 rxvq->stats.errors = 0;
1070                 rxvq->stats.multicast = 0;
1071                 rxvq->stats.broadcast = 0;
1072                 memset(rxvq->stats.size_bins, 0,
1073                        sizeof(rxvq->stats.size_bins[0]) * 8);
1074         }
1075 }
1076
1077 static void
1078 virtio_set_hwaddr(struct virtio_hw *hw)
1079 {
1080         vtpci_write_dev_config(hw,
1081                         offsetof(struct virtio_net_config, mac),
1082                         &hw->mac_addr, ETHER_ADDR_LEN);
1083 }
1084
1085 static void
1086 virtio_get_hwaddr(struct virtio_hw *hw)
1087 {
1088         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1089                 vtpci_read_dev_config(hw,
1090                         offsetof(struct virtio_net_config, mac),
1091                         &hw->mac_addr, ETHER_ADDR_LEN);
1092         } else {
1093                 eth_random_addr(&hw->mac_addr[0]);
1094                 virtio_set_hwaddr(hw);
1095         }
1096 }
1097
1098 static int
1099 virtio_mac_table_set(struct virtio_hw *hw,
1100                      const struct virtio_net_ctrl_mac *uc,
1101                      const struct virtio_net_ctrl_mac *mc)
1102 {
1103         struct virtio_pmd_ctrl ctrl;
1104         int err, len[2];
1105
1106         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1107                 PMD_DRV_LOG(INFO, "host does not support mac table");
1108                 return -1;
1109         }
1110
1111         ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1112         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1113
1114         len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1115         memcpy(ctrl.data, uc, len[0]);
1116
1117         len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1118         memcpy(ctrl.data + len[0], mc, len[1]);
1119
1120         err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1121         if (err != 0)
1122                 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1123         return err;
1124 }
1125
1126 static int
1127 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1128                     uint32_t index, uint32_t vmdq __rte_unused)
1129 {
1130         struct virtio_hw *hw = dev->data->dev_private;
1131         const struct ether_addr *addrs = dev->data->mac_addrs;
1132         unsigned int i;
1133         struct virtio_net_ctrl_mac *uc, *mc;
1134
1135         if (index >= VIRTIO_MAX_MAC_ADDRS) {
1136                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1137                 return -EINVAL;
1138         }
1139
1140         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1141         uc->entries = 0;
1142         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1143         mc->entries = 0;
1144
1145         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1146                 const struct ether_addr *addr
1147                         = (i == index) ? mac_addr : addrs + i;
1148                 struct virtio_net_ctrl_mac *tbl
1149                         = is_multicast_ether_addr(addr) ? mc : uc;
1150
1151                 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1152         }
1153
1154         return virtio_mac_table_set(hw, uc, mc);
1155 }
1156
1157 static void
1158 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1159 {
1160         struct virtio_hw *hw = dev->data->dev_private;
1161         struct ether_addr *addrs = dev->data->mac_addrs;
1162         struct virtio_net_ctrl_mac *uc, *mc;
1163         unsigned int i;
1164
1165         if (index >= VIRTIO_MAX_MAC_ADDRS) {
1166                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1167                 return;
1168         }
1169
1170         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1171         uc->entries = 0;
1172         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1173         mc->entries = 0;
1174
1175         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1176                 struct virtio_net_ctrl_mac *tbl;
1177
1178                 if (i == index || is_zero_ether_addr(addrs + i))
1179                         continue;
1180
1181                 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1182                 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1183         }
1184
1185         virtio_mac_table_set(hw, uc, mc);
1186 }
1187
1188 static int
1189 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1190 {
1191         struct virtio_hw *hw = dev->data->dev_private;
1192
1193         memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1194
1195         /* Use atomic update if available */
1196         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1197                 struct virtio_pmd_ctrl ctrl;
1198                 int len = ETHER_ADDR_LEN;
1199
1200                 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1201                 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1202
1203                 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1204                 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1205         }
1206
1207         if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1208                 return -ENOTSUP;
1209
1210         virtio_set_hwaddr(hw);
1211         return 0;
1212 }
1213
1214 static int
1215 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1216 {
1217         struct virtio_hw *hw = dev->data->dev_private;
1218         struct virtio_pmd_ctrl ctrl;
1219         int len;
1220
1221         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1222                 return -ENOTSUP;
1223
1224         ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1225         ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1226         memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1227         len = sizeof(vlan_id);
1228
1229         return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1230 }
1231
1232 static int
1233 virtio_intr_enable(struct rte_eth_dev *dev)
1234 {
1235         struct virtio_hw *hw = dev->data->dev_private;
1236
1237         if (rte_intr_enable(dev->intr_handle) < 0)
1238                 return -1;
1239
1240         if (!hw->virtio_user_dev)
1241                 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1242
1243         return 0;
1244 }
1245
1246 static int
1247 virtio_intr_disable(struct rte_eth_dev *dev)
1248 {
1249         struct virtio_hw *hw = dev->data->dev_private;
1250
1251         if (rte_intr_disable(dev->intr_handle) < 0)
1252                 return -1;
1253
1254         if (!hw->virtio_user_dev)
1255                 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1256
1257         return 0;
1258 }
1259
1260 static int
1261 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1262 {
1263         uint64_t host_features;
1264
1265         /* Prepare guest_features: feature that driver wants to support */
1266         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1267                 req_features);
1268
1269         /* Read device(host) feature bits */
1270         host_features = VTPCI_OPS(hw)->get_features(hw);
1271         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1272                 host_features);
1273
1274         /* If supported, ensure MTU value is valid before acknowledging it. */
1275         if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1276                 struct virtio_net_config config;
1277
1278                 vtpci_read_dev_config(hw,
1279                         offsetof(struct virtio_net_config, mtu),
1280                         &config.mtu, sizeof(config.mtu));
1281
1282                 if (config.mtu < ETHER_MIN_MTU)
1283                         req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1284         }
1285
1286         /*
1287          * Negotiate features: Subset of device feature bits are written back
1288          * guest feature bits.
1289          */
1290         hw->guest_features = req_features;
1291         hw->guest_features = vtpci_negotiate_features(hw, host_features);
1292         PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1293                 hw->guest_features);
1294
1295         if (hw->modern) {
1296                 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1297                         PMD_INIT_LOG(ERR,
1298                                 "VIRTIO_F_VERSION_1 features is not enabled.");
1299                         return -1;
1300                 }
1301                 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1302                 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1303                         PMD_INIT_LOG(ERR,
1304                                 "failed to set FEATURES_OK status!");
1305                         return -1;
1306                 }
1307         }
1308
1309         hw->req_guest_features = req_features;
1310
1311         return 0;
1312 }
1313
1314 int
1315 virtio_dev_pause(struct rte_eth_dev *dev)
1316 {
1317         struct virtio_hw *hw = dev->data->dev_private;
1318
1319         rte_spinlock_lock(&hw->state_lock);
1320
1321         if (hw->started == 0) {
1322                 /* Device is just stopped. */
1323                 rte_spinlock_unlock(&hw->state_lock);
1324                 return -1;
1325         }
1326         hw->started = 0;
1327         /*
1328          * Prevent the worker threads from touching queues to avoid contention,
1329          * 1 ms should be enough for the ongoing Tx function to finish.
1330          */
1331         rte_delay_ms(1);
1332         return 0;
1333 }
1334
1335 /*
1336  * Recover hw state to let the worker threads continue.
1337  */
1338 void
1339 virtio_dev_resume(struct rte_eth_dev *dev)
1340 {
1341         struct virtio_hw *hw = dev->data->dev_private;
1342
1343         hw->started = 1;
1344         rte_spinlock_unlock(&hw->state_lock);
1345 }
1346
1347 /*
1348  * Should be called only after device is paused.
1349  */
1350 int
1351 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1352                 int nb_pkts)
1353 {
1354         struct virtio_hw *hw = dev->data->dev_private;
1355         struct virtnet_tx *txvq = dev->data->tx_queues[0];
1356         int ret;
1357
1358         hw->inject_pkts = tx_pkts;
1359         ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1360         hw->inject_pkts = NULL;
1361
1362         return ret;
1363 }
1364
1365 static void
1366 virtio_notify_peers(struct rte_eth_dev *dev)
1367 {
1368         struct virtio_hw *hw = dev->data->dev_private;
1369         struct virtnet_rx *rxvq;
1370         struct rte_mbuf *rarp_mbuf;
1371
1372         if (!dev->data->rx_queues)
1373                 return;
1374
1375         rxvq = dev->data->rx_queues[0];
1376         if (!rxvq)
1377                 return;
1378
1379         rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1380                         (struct ether_addr *)hw->mac_addr);
1381         if (rarp_mbuf == NULL) {
1382                 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1383                 return;
1384         }
1385
1386         /* If virtio port just stopped, no need to send RARP */
1387         if (virtio_dev_pause(dev) < 0) {
1388                 rte_pktmbuf_free(rarp_mbuf);
1389                 return;
1390         }
1391
1392         virtio_inject_pkts(dev, &rarp_mbuf, 1);
1393         virtio_dev_resume(dev);
1394 }
1395
1396 static void
1397 virtio_ack_link_announce(struct rte_eth_dev *dev)
1398 {
1399         struct virtio_hw *hw = dev->data->dev_private;
1400         struct virtio_pmd_ctrl ctrl;
1401
1402         ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1403         ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1404
1405         virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1406 }
1407
1408 /*
1409  * Process virtio config changed interrupt. Call the callback
1410  * if link state changed, generate gratuitous RARP packet if
1411  * the status indicates an ANNOUNCE.
1412  */
1413 void
1414 virtio_interrupt_handler(void *param)
1415 {
1416         struct rte_eth_dev *dev = param;
1417         struct virtio_hw *hw = dev->data->dev_private;
1418         uint8_t isr;
1419         uint16_t status;
1420
1421         /* Read interrupt status which clears interrupt */
1422         isr = vtpci_isr(hw);
1423         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1424
1425         if (virtio_intr_enable(dev) < 0)
1426                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1427
1428         if (isr & VIRTIO_PCI_ISR_CONFIG) {
1429                 if (virtio_dev_link_update(dev, 0) == 0)
1430                         _rte_eth_dev_callback_process(dev,
1431                                                       RTE_ETH_EVENT_INTR_LSC,
1432                                                       NULL);
1433
1434                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1435                         vtpci_read_dev_config(hw,
1436                                 offsetof(struct virtio_net_config, status),
1437                                 &status, sizeof(status));
1438                         if (status & VIRTIO_NET_S_ANNOUNCE) {
1439                                 virtio_notify_peers(dev);
1440                                 if (hw->cvq)
1441                                         virtio_ack_link_announce(dev);
1442                         }
1443                 }
1444         }
1445 }
1446
1447 /* set rx and tx handlers according to what is supported */
1448 static void
1449 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1450 {
1451         struct virtio_hw *hw = eth_dev->data->dev_private;
1452
1453         if (vtpci_packed_queue(hw)) {
1454                 PMD_INIT_LOG(INFO,
1455                         "virtio: using packed ring %s Tx path on port %u",
1456                         hw->use_inorder_tx ? "inorder" : "standard",
1457                         eth_dev->data->port_id);
1458                 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1459         } else {
1460                 if (hw->use_inorder_tx) {
1461                         PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1462                                 eth_dev->data->port_id);
1463                         eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1464                 } else {
1465                         PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1466                                 eth_dev->data->port_id);
1467                         eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1468                 }
1469         }
1470
1471         if (vtpci_packed_queue(hw)) {
1472                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1473                         PMD_INIT_LOG(INFO,
1474                                 "virtio: using packed ring mergeable buffer Rx path on port %u",
1475                                 eth_dev->data->port_id);
1476                         eth_dev->rx_pkt_burst =
1477                                 &virtio_recv_mergeable_pkts_packed;
1478                 } else {
1479                         PMD_INIT_LOG(INFO,
1480                                 "virtio: using packed ring standard Rx path on port %u",
1481                                 eth_dev->data->port_id);
1482                         eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1483                 }
1484         } else {
1485                 if (hw->use_simple_rx) {
1486                         PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1487                                 eth_dev->data->port_id);
1488                         eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1489                 } else if (hw->use_inorder_rx) {
1490                         PMD_INIT_LOG(INFO,
1491                                 "virtio: using inorder Rx path on port %u",
1492                                 eth_dev->data->port_id);
1493                         eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1494                 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1495                         PMD_INIT_LOG(INFO,
1496                                 "virtio: using mergeable buffer Rx path on port %u",
1497                                 eth_dev->data->port_id);
1498                         eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1499                 } else {
1500                         PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1501                                 eth_dev->data->port_id);
1502                         eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1503                 }
1504         }
1505
1506 }
1507
1508 /* Only support 1:1 queue/interrupt mapping so far.
1509  * TODO: support n:1 queue/interrupt mapping when there are limited number of
1510  * interrupt vectors (<N+1).
1511  */
1512 static int
1513 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1514 {
1515         uint32_t i;
1516         struct virtio_hw *hw = dev->data->dev_private;
1517
1518         PMD_INIT_LOG(INFO, "queue/interrupt binding");
1519         for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1520                 dev->intr_handle->intr_vec[i] = i + 1;
1521                 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1522                                                  VIRTIO_MSI_NO_VECTOR) {
1523                         PMD_DRV_LOG(ERR, "failed to set queue vector");
1524                         return -EBUSY;
1525                 }
1526         }
1527
1528         return 0;
1529 }
1530
1531 static void
1532 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1533 {
1534         uint32_t i;
1535         struct virtio_hw *hw = dev->data->dev_private;
1536
1537         PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1538         for (i = 0; i < dev->data->nb_rx_queues; ++i)
1539                 VTPCI_OPS(hw)->set_queue_irq(hw,
1540                                              hw->vqs[i * VTNET_CQ],
1541                                              VIRTIO_MSI_NO_VECTOR);
1542 }
1543
1544 static int
1545 virtio_configure_intr(struct rte_eth_dev *dev)
1546 {
1547         struct virtio_hw *hw = dev->data->dev_private;
1548
1549         if (!rte_intr_cap_multiple(dev->intr_handle)) {
1550                 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1551                 return -ENOTSUP;
1552         }
1553
1554         if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1555                 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1556                 return -1;
1557         }
1558
1559         if (!dev->intr_handle->intr_vec) {
1560                 dev->intr_handle->intr_vec =
1561                         rte_zmalloc("intr_vec",
1562                                     hw->max_queue_pairs * sizeof(int), 0);
1563                 if (!dev->intr_handle->intr_vec) {
1564                         PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1565                                      hw->max_queue_pairs);
1566                         return -ENOMEM;
1567                 }
1568         }
1569
1570         /* Re-register callback to update max_intr */
1571         rte_intr_callback_unregister(dev->intr_handle,
1572                                      virtio_interrupt_handler,
1573                                      dev);
1574         rte_intr_callback_register(dev->intr_handle,
1575                                    virtio_interrupt_handler,
1576                                    dev);
1577
1578         /* DO NOT try to remove this! This function will enable msix, or QEMU
1579          * will encounter SIGSEGV when DRIVER_OK is sent.
1580          * And for legacy devices, this should be done before queue/vec binding
1581          * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1582          * (22) will be ignored.
1583          */
1584         if (virtio_intr_enable(dev) < 0) {
1585                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1586                 return -1;
1587         }
1588
1589         if (virtio_queues_bind_intr(dev) < 0) {
1590                 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1591                 return -1;
1592         }
1593
1594         return 0;
1595 }
1596
1597 /* reset device and renegotiate features if needed */
1598 static int
1599 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1600 {
1601         struct virtio_hw *hw = eth_dev->data->dev_private;
1602         struct virtio_net_config *config;
1603         struct virtio_net_config local_config;
1604         struct rte_pci_device *pci_dev = NULL;
1605         int ret;
1606
1607         /* Reset the device although not necessary at startup */
1608         vtpci_reset(hw);
1609
1610         if (hw->vqs) {
1611                 virtio_dev_free_mbufs(eth_dev);
1612                 virtio_free_queues(hw);
1613         }
1614
1615         /* Tell the host we've noticed this device. */
1616         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1617
1618         /* Tell the host we've known how to drive the device. */
1619         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1620         if (virtio_negotiate_features(hw, req_features) < 0)
1621                 return -1;
1622
1623         hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1624
1625         if (!hw->virtio_user_dev) {
1626                 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1627                 rte_eth_copy_pci_info(eth_dev, pci_dev);
1628         }
1629
1630         /* If host does not support both status and MSI-X then disable LSC */
1631         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1632             hw->use_msix != VIRTIO_MSIX_NONE)
1633                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1634         else
1635                 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1636
1637         /* Setting up rx_header size for the device */
1638         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1639             vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1640             vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1641                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1642         else
1643                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1644
1645         /* Copy the permanent MAC address to: virtio_hw */
1646         virtio_get_hwaddr(hw);
1647         ether_addr_copy((struct ether_addr *) hw->mac_addr,
1648                         &eth_dev->data->mac_addrs[0]);
1649         PMD_INIT_LOG(DEBUG,
1650                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1651                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1652                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1653
1654         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1655                 config = &local_config;
1656
1657                 vtpci_read_dev_config(hw,
1658                         offsetof(struct virtio_net_config, mac),
1659                         &config->mac, sizeof(config->mac));
1660
1661                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1662                         vtpci_read_dev_config(hw,
1663                                 offsetof(struct virtio_net_config, status),
1664                                 &config->status, sizeof(config->status));
1665                 } else {
1666                         PMD_INIT_LOG(DEBUG,
1667                                      "VIRTIO_NET_F_STATUS is not supported");
1668                         config->status = 0;
1669                 }
1670
1671                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1672                         vtpci_read_dev_config(hw,
1673                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1674                                 &config->max_virtqueue_pairs,
1675                                 sizeof(config->max_virtqueue_pairs));
1676                 } else {
1677                         PMD_INIT_LOG(DEBUG,
1678                                      "VIRTIO_NET_F_MQ is not supported");
1679                         config->max_virtqueue_pairs = 1;
1680                 }
1681
1682                 hw->max_queue_pairs = config->max_virtqueue_pairs;
1683
1684                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1685                         vtpci_read_dev_config(hw,
1686                                 offsetof(struct virtio_net_config, mtu),
1687                                 &config->mtu,
1688                                 sizeof(config->mtu));
1689
1690                         /*
1691                          * MTU value has already been checked at negotiation
1692                          * time, but check again in case it has changed since
1693                          * then, which should not happen.
1694                          */
1695                         if (config->mtu < ETHER_MIN_MTU) {
1696                                 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1697                                                 config->mtu);
1698                                 return -1;
1699                         }
1700
1701                         hw->max_mtu = config->mtu;
1702                         /* Set initial MTU to maximum one supported by vhost */
1703                         eth_dev->data->mtu = config->mtu;
1704
1705                 } else {
1706                         hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1707                                 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1708                 }
1709
1710                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1711                                 config->max_virtqueue_pairs);
1712                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1713                 PMD_INIT_LOG(DEBUG,
1714                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1715                                 config->mac[0], config->mac[1],
1716                                 config->mac[2], config->mac[3],
1717                                 config->mac[4], config->mac[5]);
1718         } else {
1719                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1720                 hw->max_queue_pairs = 1;
1721                 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1722                         VLAN_TAG_LEN - hw->vtnet_hdr_size;
1723         }
1724
1725         ret = virtio_alloc_queues(eth_dev);
1726         if (ret < 0)
1727                 return ret;
1728
1729         if (eth_dev->data->dev_conf.intr_conf.rxq) {
1730                 if (virtio_configure_intr(eth_dev) < 0) {
1731                         PMD_INIT_LOG(ERR, "failed to configure interrupt");
1732                         return -1;
1733                 }
1734         }
1735
1736         vtpci_reinit_complete(hw);
1737
1738         if (pci_dev)
1739                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1740                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1741                         pci_dev->id.device_id);
1742
1743         return 0;
1744 }
1745
1746 /*
1747  * Remap the PCI device again (IO port map for legacy device and
1748  * memory map for modern device), so that the secondary process
1749  * could have the PCI initiated correctly.
1750  */
1751 static int
1752 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1753 {
1754         if (hw->modern) {
1755                 /*
1756                  * We don't have to re-parse the PCI config space, since
1757                  * rte_pci_map_device() makes sure the mapped address
1758                  * in secondary process would equal to the one mapped in
1759                  * the primary process: error will be returned if that
1760                  * requirement is not met.
1761                  *
1762                  * That said, we could simply reuse all cap pointers
1763                  * (such as dev_cfg, common_cfg, etc.) parsed from the
1764                  * primary process, which is stored in shared memory.
1765                  */
1766                 if (rte_pci_map_device(pci_dev)) {
1767                         PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1768                         return -1;
1769                 }
1770         } else {
1771                 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1772                         return -1;
1773         }
1774
1775         return 0;
1776 }
1777
1778 static void
1779 virtio_set_vtpci_ops(struct virtio_hw *hw)
1780 {
1781 #ifdef RTE_VIRTIO_USER
1782         if (hw->virtio_user_dev)
1783                 VTPCI_OPS(hw) = &virtio_user_ops;
1784         else
1785 #endif
1786         if (hw->modern)
1787                 VTPCI_OPS(hw) = &modern_ops;
1788         else
1789                 VTPCI_OPS(hw) = &legacy_ops;
1790 }
1791
1792 /*
1793  * This function is based on probe() function in virtio_pci.c
1794  * It returns 0 on success.
1795  */
1796 int
1797 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1798 {
1799         struct virtio_hw *hw = eth_dev->data->dev_private;
1800         int ret;
1801
1802         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1803
1804         eth_dev->dev_ops = &virtio_eth_dev_ops;
1805
1806         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1807                 if (!hw->virtio_user_dev) {
1808                         ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1809                         if (ret)
1810                                 return ret;
1811                 }
1812
1813                 virtio_set_vtpci_ops(hw);
1814                 set_rxtx_funcs(eth_dev);
1815
1816                 return 0;
1817         }
1818
1819         /* Allocate memory for storing MAC addresses */
1820         eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1821         if (eth_dev->data->mac_addrs == NULL) {
1822                 PMD_INIT_LOG(ERR,
1823                         "Failed to allocate %d bytes needed to store MAC addresses",
1824                         VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1825                 return -ENOMEM;
1826         }
1827
1828         hw->port_id = eth_dev->data->port_id;
1829         /* For virtio_user case the hw->virtio_user_dev is populated by
1830          * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1831          */
1832         if (!hw->virtio_user_dev) {
1833                 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1834                 if (ret)
1835                         goto out;
1836         }
1837
1838         /* reset device and negotiate default features */
1839         ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1840         if (ret < 0)
1841                 goto out;
1842
1843         return 0;
1844
1845 out:
1846         rte_free(eth_dev->data->mac_addrs);
1847         return ret;
1848 }
1849
1850 static int
1851 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1852 {
1853         PMD_INIT_FUNC_TRACE();
1854
1855         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1856                 return 0;
1857
1858         virtio_dev_stop(eth_dev);
1859         virtio_dev_close(eth_dev);
1860
1861         eth_dev->dev_ops = NULL;
1862         eth_dev->tx_pkt_burst = NULL;
1863         eth_dev->rx_pkt_burst = NULL;
1864
1865         if (eth_dev->device)
1866                 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1867
1868         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1869
1870         return 0;
1871 }
1872
1873 static int vdpa_check_handler(__rte_unused const char *key,
1874                 const char *value, __rte_unused void *opaque)
1875 {
1876         if (strcmp(value, "1"))
1877                 return -1;
1878
1879         return 0;
1880 }
1881
1882 static int
1883 vdpa_mode_selected(struct rte_devargs *devargs)
1884 {
1885         struct rte_kvargs *kvlist;
1886         const char *key = "vdpa";
1887         int ret = 0;
1888
1889         if (devargs == NULL)
1890                 return 0;
1891
1892         kvlist = rte_kvargs_parse(devargs->args, NULL);
1893         if (kvlist == NULL)
1894                 return 0;
1895
1896         if (!rte_kvargs_count(kvlist, key))
1897                 goto exit;
1898
1899         /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1900         if (rte_kvargs_process(kvlist, key,
1901                                 vdpa_check_handler, NULL) < 0) {
1902                 goto exit;
1903         }
1904         ret = 1;
1905
1906 exit:
1907         rte_kvargs_free(kvlist);
1908         return ret;
1909 }
1910
1911 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1912         struct rte_pci_device *pci_dev)
1913 {
1914         if (rte_eal_iopl_init() != 0) {
1915                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1916                 return 1;
1917         }
1918
1919         /* virtio pmd skips probe if device needs to work in vdpa mode */
1920         if (vdpa_mode_selected(pci_dev->device.devargs))
1921                 return 1;
1922
1923         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1924                 eth_virtio_dev_init);
1925 }
1926
1927 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1928 {
1929         return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1930 }
1931
1932 static struct rte_pci_driver rte_virtio_pmd = {
1933         .driver = {
1934                 .name = "net_virtio",
1935         },
1936         .id_table = pci_id_virtio_map,
1937         .drv_flags = 0,
1938         .probe = eth_virtio_pci_probe,
1939         .remove = eth_virtio_pci_remove,
1940 };
1941
1942 RTE_INIT(rte_virtio_pmd_init)
1943 {
1944         rte_eal_iopl_init();
1945         rte_pci_register(&rte_virtio_pmd);
1946 }
1947
1948 static bool
1949 rx_offload_enabled(struct virtio_hw *hw)
1950 {
1951         return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1952                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1953                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1954 }
1955
1956 static bool
1957 tx_offload_enabled(struct virtio_hw *hw)
1958 {
1959         return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1960                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1961                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1962 }
1963
1964 /*
1965  * Configure virtio device
1966  * It returns 0 on success.
1967  */
1968 static int
1969 virtio_dev_configure(struct rte_eth_dev *dev)
1970 {
1971         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1972         const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1973         struct virtio_hw *hw = dev->data->dev_private;
1974         uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
1975                 hw->vtnet_hdr_size;
1976         uint64_t rx_offloads = rxmode->offloads;
1977         uint64_t tx_offloads = txmode->offloads;
1978         uint64_t req_features;
1979         int ret;
1980
1981         PMD_INIT_LOG(DEBUG, "configure");
1982         req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1983
1984         if (dev->data->dev_conf.intr_conf.rxq) {
1985                 ret = virtio_init_device(dev, hw->req_guest_features);
1986                 if (ret < 0)
1987                         return ret;
1988         }
1989
1990         if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
1991                 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1992
1993         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1994                            DEV_RX_OFFLOAD_TCP_CKSUM))
1995                 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1996
1997         if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
1998                 req_features |=
1999                         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2000                         (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2001
2002         if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2003                            DEV_TX_OFFLOAD_TCP_CKSUM))
2004                 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2005
2006         if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2007                 req_features |=
2008                         (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2009                         (1ULL << VIRTIO_NET_F_HOST_TSO6);
2010
2011         /* if request features changed, reinit the device */
2012         if (req_features != hw->req_guest_features) {
2013                 ret = virtio_init_device(dev, req_features);
2014                 if (ret < 0)
2015                         return ret;
2016         }
2017
2018         if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2019                             DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2020                 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2021                 PMD_DRV_LOG(ERR,
2022                         "rx checksum not available on this host");
2023                 return -ENOTSUP;
2024         }
2025
2026         if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2027                 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2028                  !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2029                 PMD_DRV_LOG(ERR,
2030                         "Large Receive Offload not available on this host");
2031                 return -ENOTSUP;
2032         }
2033
2034         /* start control queue */
2035         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2036                 virtio_dev_cq_start(dev);
2037
2038         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2039                 hw->vlan_strip = 1;
2040
2041         if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2042             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2043                 PMD_DRV_LOG(ERR,
2044                             "vlan filtering not available on this host");
2045                 return -ENOTSUP;
2046         }
2047
2048         hw->has_tx_offload = tx_offload_enabled(hw);
2049         hw->has_rx_offload = rx_offload_enabled(hw);
2050
2051         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2052                 /* Enable vector (0) for Link State Intrerrupt */
2053                 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2054                                 VIRTIO_MSI_NO_VECTOR) {
2055                         PMD_DRV_LOG(ERR, "failed to set config vector");
2056                         return -EBUSY;
2057                 }
2058
2059         rte_spinlock_init(&hw->state_lock);
2060
2061         hw->use_simple_rx = 1;
2062
2063         if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2064                 hw->use_inorder_tx = 1;
2065                 hw->use_inorder_rx = 1;
2066                 hw->use_simple_rx = 0;
2067         }
2068
2069         if (vtpci_packed_queue(hw)) {
2070                 hw->use_simple_rx = 0;
2071                 hw->use_inorder_rx = 0;
2072         }
2073
2074 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2075         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2076                 hw->use_simple_rx = 0;
2077         }
2078 #endif
2079         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2080                  hw->use_simple_rx = 0;
2081         }
2082
2083         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2084                            DEV_RX_OFFLOAD_TCP_CKSUM |
2085                            DEV_RX_OFFLOAD_TCP_LRO |
2086                            DEV_RX_OFFLOAD_VLAN_STRIP))
2087                 hw->use_simple_rx = 0;
2088
2089         hw->opened = true;
2090
2091         return 0;
2092 }
2093
2094
2095 static int
2096 virtio_dev_start(struct rte_eth_dev *dev)
2097 {
2098         uint16_t nb_queues, i;
2099         struct virtnet_rx *rxvq;
2100         struct virtnet_tx *txvq __rte_unused;
2101         struct virtio_hw *hw = dev->data->dev_private;
2102         int ret;
2103
2104         /* Finish the initialization of the queues */
2105         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2106                 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2107                 if (ret < 0)
2108                         return ret;
2109         }
2110         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2111                 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2112                 if (ret < 0)
2113                         return ret;
2114         }
2115
2116         /* check if lsc interrupt feature is enabled */
2117         if (dev->data->dev_conf.intr_conf.lsc) {
2118                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2119                         PMD_DRV_LOG(ERR, "link status not supported by host");
2120                         return -ENOTSUP;
2121                 }
2122         }
2123
2124         /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2125          * in device configure, but it could be unmapped  when device is
2126          * stopped.
2127          */
2128         if (dev->data->dev_conf.intr_conf.lsc ||
2129             dev->data->dev_conf.intr_conf.rxq) {
2130                 virtio_intr_disable(dev);
2131
2132                 /* Setup interrupt callback  */
2133                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2134                         rte_intr_callback_register(dev->intr_handle,
2135                                                    virtio_interrupt_handler,
2136                                                    dev);
2137
2138                 if (virtio_intr_enable(dev) < 0) {
2139                         PMD_DRV_LOG(ERR, "interrupt enable failed");
2140                         return -EIO;
2141                 }
2142         }
2143
2144         /*Notify the backend
2145          *Otherwise the tap backend might already stop its queue due to fullness.
2146          *vhost backend will have no chance to be waked up
2147          */
2148         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2149         if (hw->max_queue_pairs > 1) {
2150                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2151                         return -EINVAL;
2152         }
2153
2154         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2155
2156         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2157                 rxvq = dev->data->rx_queues[i];
2158                 /* Flush the old packets */
2159                 virtqueue_rxvq_flush(rxvq->vq);
2160                 virtqueue_notify(rxvq->vq);
2161         }
2162
2163         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2164                 txvq = dev->data->tx_queues[i];
2165                 virtqueue_notify(txvq->vq);
2166         }
2167
2168         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2169
2170         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2171                 rxvq = dev->data->rx_queues[i];
2172                 VIRTQUEUE_DUMP(rxvq->vq);
2173         }
2174
2175         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2176                 txvq = dev->data->tx_queues[i];
2177                 VIRTQUEUE_DUMP(txvq->vq);
2178         }
2179
2180         set_rxtx_funcs(dev);
2181         hw->started = true;
2182
2183         /* Initialize Link state */
2184         virtio_dev_link_update(dev, 0);
2185
2186         return 0;
2187 }
2188
2189 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2190 {
2191         struct virtio_hw *hw = dev->data->dev_private;
2192         uint16_t nr_vq = virtio_get_nr_vq(hw);
2193         const char *type __rte_unused;
2194         unsigned int i, mbuf_num = 0;
2195         struct virtqueue *vq;
2196         struct rte_mbuf *buf;
2197         int queue_type;
2198
2199         if (hw->vqs == NULL)
2200                 return;
2201
2202         for (i = 0; i < nr_vq; i++) {
2203                 vq = hw->vqs[i];
2204                 if (!vq)
2205                         continue;
2206
2207                 queue_type = virtio_get_queue_type(hw, i);
2208                 if (queue_type == VTNET_RQ)
2209                         type = "rxq";
2210                 else if (queue_type == VTNET_TQ)
2211                         type = "txq";
2212                 else
2213                         continue;
2214
2215                 PMD_INIT_LOG(DEBUG,
2216                         "Before freeing %s[%d] used and unused buf",
2217                         type, i);
2218                 VIRTQUEUE_DUMP(vq);
2219
2220                 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2221                         rte_pktmbuf_free(buf);
2222                         mbuf_num++;
2223                 }
2224
2225                 PMD_INIT_LOG(DEBUG,
2226                         "After freeing %s[%d] used and unused buf",
2227                         type, i);
2228                 VIRTQUEUE_DUMP(vq);
2229         }
2230
2231         PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2232 }
2233
2234 /*
2235  * Stop device: disable interrupt and mark link down
2236  */
2237 static void
2238 virtio_dev_stop(struct rte_eth_dev *dev)
2239 {
2240         struct virtio_hw *hw = dev->data->dev_private;
2241         struct rte_eth_link link;
2242         struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2243
2244         PMD_INIT_LOG(DEBUG, "stop");
2245
2246         rte_spinlock_lock(&hw->state_lock);
2247         if (!hw->started)
2248                 goto out_unlock;
2249         hw->started = false;
2250
2251         if (intr_conf->lsc || intr_conf->rxq) {
2252                 virtio_intr_disable(dev);
2253
2254                 /* Reset interrupt callback  */
2255                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2256                         rte_intr_callback_unregister(dev->intr_handle,
2257                                                      virtio_interrupt_handler,
2258                                                      dev);
2259                 }
2260         }
2261
2262         memset(&link, 0, sizeof(link));
2263         rte_eth_linkstatus_set(dev, &link);
2264 out_unlock:
2265         rte_spinlock_unlock(&hw->state_lock);
2266 }
2267
2268 static int
2269 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2270 {
2271         struct rte_eth_link link;
2272         uint16_t status;
2273         struct virtio_hw *hw = dev->data->dev_private;
2274
2275         memset(&link, 0, sizeof(link));
2276         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2277         link.link_speed  = ETH_SPEED_NUM_10G;
2278         link.link_autoneg = ETH_LINK_FIXED;
2279
2280         if (!hw->started) {
2281                 link.link_status = ETH_LINK_DOWN;
2282         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2283                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2284                 vtpci_read_dev_config(hw,
2285                                 offsetof(struct virtio_net_config, status),
2286                                 &status, sizeof(status));
2287                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2288                         link.link_status = ETH_LINK_DOWN;
2289                         PMD_INIT_LOG(DEBUG, "Port %d is down",
2290                                      dev->data->port_id);
2291                 } else {
2292                         link.link_status = ETH_LINK_UP;
2293                         PMD_INIT_LOG(DEBUG, "Port %d is up",
2294                                      dev->data->port_id);
2295                 }
2296         } else {
2297                 link.link_status = ETH_LINK_UP;
2298         }
2299
2300         return rte_eth_linkstatus_set(dev, &link);
2301 }
2302
2303 static int
2304 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2305 {
2306         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2307         struct virtio_hw *hw = dev->data->dev_private;
2308         uint64_t offloads = rxmode->offloads;
2309
2310         if (mask & ETH_VLAN_FILTER_MASK) {
2311                 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2312                                 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2313
2314                         PMD_DRV_LOG(NOTICE,
2315                                 "vlan filtering not available on this host");
2316
2317                         return -ENOTSUP;
2318                 }
2319         }
2320
2321         if (mask & ETH_VLAN_STRIP_MASK)
2322                 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2323
2324         return 0;
2325 }
2326
2327 static void
2328 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2329 {
2330         uint64_t tso_mask, host_features;
2331         struct virtio_hw *hw = dev->data->dev_private;
2332
2333         dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2334
2335         dev_info->max_rx_queues =
2336                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2337         dev_info->max_tx_queues =
2338                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2339         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2340         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2341         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2342
2343         host_features = VTPCI_OPS(hw)->get_features(hw);
2344         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2345         dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2346         if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2347                 dev_info->rx_offload_capa |=
2348                         DEV_RX_OFFLOAD_TCP_CKSUM |
2349                         DEV_RX_OFFLOAD_UDP_CKSUM;
2350         }
2351         if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2352                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2353         tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2354                 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2355         if ((host_features & tso_mask) == tso_mask)
2356                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2357
2358         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2359                                     DEV_TX_OFFLOAD_VLAN_INSERT;
2360         if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2361                 dev_info->tx_offload_capa |=
2362                         DEV_TX_OFFLOAD_UDP_CKSUM |
2363                         DEV_TX_OFFLOAD_TCP_CKSUM;
2364         }
2365         tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2366                 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2367         if ((host_features & tso_mask) == tso_mask)
2368                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2369 }
2370
2371 /*
2372  * It enables testpmd to collect per queue stats.
2373  */
2374 static int
2375 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2376 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2377 __rte_unused uint8_t is_rx)
2378 {
2379         return 0;
2380 }
2381
2382 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2383 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2384 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2385
2386 RTE_INIT(virtio_init_log)
2387 {
2388         virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2389         if (virtio_logtype_init >= 0)
2390                 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2391         virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2392         if (virtio_logtype_driver >= 0)
2393                 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);
2394 }