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