net/virtio: support platform memory ordering
[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 Rx path on port %u",
1485                                 eth_dev->data->port_id);
1486                         eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder;
1487                 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1488                         PMD_INIT_LOG(INFO,
1489                                 "virtio: using mergeable buffer Rx path on port %u",
1490                                 eth_dev->data->port_id);
1491                         eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1492                 } else {
1493                         PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1494                                 eth_dev->data->port_id);
1495                         eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1496                 }
1497         }
1498
1499 }
1500
1501 /* Only support 1:1 queue/interrupt mapping so far.
1502  * TODO: support n:1 queue/interrupt mapping when there are limited number of
1503  * interrupt vectors (<N+1).
1504  */
1505 static int
1506 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1507 {
1508         uint32_t i;
1509         struct virtio_hw *hw = dev->data->dev_private;
1510
1511         PMD_INIT_LOG(INFO, "queue/interrupt binding");
1512         for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1513                 dev->intr_handle->intr_vec[i] = i + 1;
1514                 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1515                                                  VIRTIO_MSI_NO_VECTOR) {
1516                         PMD_DRV_LOG(ERR, "failed to set queue vector");
1517                         return -EBUSY;
1518                 }
1519         }
1520
1521         return 0;
1522 }
1523
1524 static void
1525 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1526 {
1527         uint32_t i;
1528         struct virtio_hw *hw = dev->data->dev_private;
1529
1530         PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1531         for (i = 0; i < dev->data->nb_rx_queues; ++i)
1532                 VTPCI_OPS(hw)->set_queue_irq(hw,
1533                                              hw->vqs[i * VTNET_CQ],
1534                                              VIRTIO_MSI_NO_VECTOR);
1535 }
1536
1537 static int
1538 virtio_configure_intr(struct rte_eth_dev *dev)
1539 {
1540         struct virtio_hw *hw = dev->data->dev_private;
1541
1542         if (!rte_intr_cap_multiple(dev->intr_handle)) {
1543                 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1544                 return -ENOTSUP;
1545         }
1546
1547         if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1548                 PMD_INIT_LOG(ERR, "Fail to create eventfd");
1549                 return -1;
1550         }
1551
1552         if (!dev->intr_handle->intr_vec) {
1553                 dev->intr_handle->intr_vec =
1554                         rte_zmalloc("intr_vec",
1555                                     hw->max_queue_pairs * sizeof(int), 0);
1556                 if (!dev->intr_handle->intr_vec) {
1557                         PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1558                                      hw->max_queue_pairs);
1559                         return -ENOMEM;
1560                 }
1561         }
1562
1563         /* Re-register callback to update max_intr */
1564         rte_intr_callback_unregister(dev->intr_handle,
1565                                      virtio_interrupt_handler,
1566                                      dev);
1567         rte_intr_callback_register(dev->intr_handle,
1568                                    virtio_interrupt_handler,
1569                                    dev);
1570
1571         /* DO NOT try to remove this! This function will enable msix, or QEMU
1572          * will encounter SIGSEGV when DRIVER_OK is sent.
1573          * And for legacy devices, this should be done before queue/vec binding
1574          * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1575          * (22) will be ignored.
1576          */
1577         if (virtio_intr_enable(dev) < 0) {
1578                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1579                 return -1;
1580         }
1581
1582         if (virtio_queues_bind_intr(dev) < 0) {
1583                 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1584                 return -1;
1585         }
1586
1587         return 0;
1588 }
1589
1590 /* reset device and renegotiate features if needed */
1591 static int
1592 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1593 {
1594         struct virtio_hw *hw = eth_dev->data->dev_private;
1595         struct virtio_net_config *config;
1596         struct virtio_net_config local_config;
1597         struct rte_pci_device *pci_dev = NULL;
1598         int ret;
1599
1600         /* Reset the device although not necessary at startup */
1601         vtpci_reset(hw);
1602
1603         if (hw->vqs) {
1604                 virtio_dev_free_mbufs(eth_dev);
1605                 virtio_free_queues(hw);
1606         }
1607
1608         /* Tell the host we've noticed this device. */
1609         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1610
1611         /* Tell the host we've known how to drive the device. */
1612         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1613         if (virtio_negotiate_features(hw, req_features) < 0)
1614                 return -1;
1615
1616         hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1617
1618         if (!hw->virtio_user_dev) {
1619                 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1620                 rte_eth_copy_pci_info(eth_dev, pci_dev);
1621         }
1622
1623         /* If host does not support both status and MSI-X then disable LSC */
1624         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1625             hw->use_msix != VIRTIO_MSIX_NONE)
1626                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1627         else
1628                 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1629
1630         /* Setting up rx_header size for the device */
1631         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1632             vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1633             vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1634                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1635         else
1636                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1637
1638         /* Copy the permanent MAC address to: virtio_hw */
1639         virtio_get_hwaddr(hw);
1640         ether_addr_copy((struct ether_addr *) hw->mac_addr,
1641                         &eth_dev->data->mac_addrs[0]);
1642         PMD_INIT_LOG(DEBUG,
1643                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1644                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1645                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1646
1647         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1648                 config = &local_config;
1649
1650                 vtpci_read_dev_config(hw,
1651                         offsetof(struct virtio_net_config, mac),
1652                         &config->mac, sizeof(config->mac));
1653
1654                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1655                         vtpci_read_dev_config(hw,
1656                                 offsetof(struct virtio_net_config, status),
1657                                 &config->status, sizeof(config->status));
1658                 } else {
1659                         PMD_INIT_LOG(DEBUG,
1660                                      "VIRTIO_NET_F_STATUS is not supported");
1661                         config->status = 0;
1662                 }
1663
1664                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1665                         vtpci_read_dev_config(hw,
1666                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1667                                 &config->max_virtqueue_pairs,
1668                                 sizeof(config->max_virtqueue_pairs));
1669                 } else {
1670                         PMD_INIT_LOG(DEBUG,
1671                                      "VIRTIO_NET_F_MQ is not supported");
1672                         config->max_virtqueue_pairs = 1;
1673                 }
1674
1675                 hw->max_queue_pairs = config->max_virtqueue_pairs;
1676
1677                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1678                         vtpci_read_dev_config(hw,
1679                                 offsetof(struct virtio_net_config, mtu),
1680                                 &config->mtu,
1681                                 sizeof(config->mtu));
1682
1683                         /*
1684                          * MTU value has already been checked at negotiation
1685                          * time, but check again in case it has changed since
1686                          * then, which should not happen.
1687                          */
1688                         if (config->mtu < ETHER_MIN_MTU) {
1689                                 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1690                                                 config->mtu);
1691                                 return -1;
1692                         }
1693
1694                         hw->max_mtu = config->mtu;
1695                         /* Set initial MTU to maximum one supported by vhost */
1696                         eth_dev->data->mtu = config->mtu;
1697
1698                 } else {
1699                         hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1700                                 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1701                 }
1702
1703                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1704                                 config->max_virtqueue_pairs);
1705                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1706                 PMD_INIT_LOG(DEBUG,
1707                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1708                                 config->mac[0], config->mac[1],
1709                                 config->mac[2], config->mac[3],
1710                                 config->mac[4], config->mac[5]);
1711         } else {
1712                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1713                 hw->max_queue_pairs = 1;
1714                 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1715                         VLAN_TAG_LEN - hw->vtnet_hdr_size;
1716         }
1717
1718         ret = virtio_alloc_queues(eth_dev);
1719         if (ret < 0)
1720                 return ret;
1721
1722         if (eth_dev->data->dev_conf.intr_conf.rxq) {
1723                 if (virtio_configure_intr(eth_dev) < 0) {
1724                         PMD_INIT_LOG(ERR, "failed to configure interrupt");
1725                         return -1;
1726                 }
1727         }
1728
1729         vtpci_reinit_complete(hw);
1730
1731         if (pci_dev)
1732                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1733                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1734                         pci_dev->id.device_id);
1735
1736         return 0;
1737 }
1738
1739 /*
1740  * Remap the PCI device again (IO port map for legacy device and
1741  * memory map for modern device), so that the secondary process
1742  * could have the PCI initiated correctly.
1743  */
1744 static int
1745 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1746 {
1747         if (hw->modern) {
1748                 /*
1749                  * We don't have to re-parse the PCI config space, since
1750                  * rte_pci_map_device() makes sure the mapped address
1751                  * in secondary process would equal to the one mapped in
1752                  * the primary process: error will be returned if that
1753                  * requirement is not met.
1754                  *
1755                  * That said, we could simply reuse all cap pointers
1756                  * (such as dev_cfg, common_cfg, etc.) parsed from the
1757                  * primary process, which is stored in shared memory.
1758                  */
1759                 if (rte_pci_map_device(pci_dev)) {
1760                         PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1761                         return -1;
1762                 }
1763         } else {
1764                 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1765                         return -1;
1766         }
1767
1768         return 0;
1769 }
1770
1771 static void
1772 virtio_set_vtpci_ops(struct virtio_hw *hw)
1773 {
1774 #ifdef RTE_VIRTIO_USER
1775         if (hw->virtio_user_dev)
1776                 VTPCI_OPS(hw) = &virtio_user_ops;
1777         else
1778 #endif
1779         if (hw->modern)
1780                 VTPCI_OPS(hw) = &modern_ops;
1781         else
1782                 VTPCI_OPS(hw) = &legacy_ops;
1783 }
1784
1785 /*
1786  * This function is based on probe() function in virtio_pci.c
1787  * It returns 0 on success.
1788  */
1789 int
1790 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1791 {
1792         struct virtio_hw *hw = eth_dev->data->dev_private;
1793         int ret;
1794
1795         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1796
1797         eth_dev->dev_ops = &virtio_eth_dev_ops;
1798
1799         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1800                 if (!hw->virtio_user_dev) {
1801                         ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1802                         if (ret)
1803                                 return ret;
1804                 }
1805
1806                 virtio_set_vtpci_ops(hw);
1807                 set_rxtx_funcs(eth_dev);
1808
1809                 return 0;
1810         }
1811
1812         /* Allocate memory for storing MAC addresses */
1813         eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1814         if (eth_dev->data->mac_addrs == NULL) {
1815                 PMD_INIT_LOG(ERR,
1816                         "Failed to allocate %d bytes needed to store MAC addresses",
1817                         VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1818                 return -ENOMEM;
1819         }
1820
1821         hw->port_id = eth_dev->data->port_id;
1822         /* For virtio_user case the hw->virtio_user_dev is populated by
1823          * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1824          */
1825         if (!hw->virtio_user_dev) {
1826                 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1827                 if (ret)
1828                         goto out;
1829         }
1830
1831         /* reset device and negotiate default features */
1832         ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1833         if (ret < 0)
1834                 goto out;
1835
1836         return 0;
1837
1838 out:
1839         rte_free(eth_dev->data->mac_addrs);
1840         return ret;
1841 }
1842
1843 static int
1844 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1845 {
1846         PMD_INIT_FUNC_TRACE();
1847
1848         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1849                 return 0;
1850
1851         virtio_dev_stop(eth_dev);
1852         virtio_dev_close(eth_dev);
1853
1854         eth_dev->dev_ops = NULL;
1855         eth_dev->tx_pkt_burst = NULL;
1856         eth_dev->rx_pkt_burst = NULL;
1857
1858         if (eth_dev->device)
1859                 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1860
1861         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1862
1863         return 0;
1864 }
1865
1866 static int vdpa_check_handler(__rte_unused const char *key,
1867                 const char *value, __rte_unused void *opaque)
1868 {
1869         if (strcmp(value, "1"))
1870                 return -1;
1871
1872         return 0;
1873 }
1874
1875 static int
1876 vdpa_mode_selected(struct rte_devargs *devargs)
1877 {
1878         struct rte_kvargs *kvlist;
1879         const char *key = "vdpa";
1880         int ret = 0;
1881
1882         if (devargs == NULL)
1883                 return 0;
1884
1885         kvlist = rte_kvargs_parse(devargs->args, NULL);
1886         if (kvlist == NULL)
1887                 return 0;
1888
1889         if (!rte_kvargs_count(kvlist, key))
1890                 goto exit;
1891
1892         /* vdpa mode selected when there's a key-value pair: vdpa=1 */
1893         if (rte_kvargs_process(kvlist, key,
1894                                 vdpa_check_handler, NULL) < 0) {
1895                 goto exit;
1896         }
1897         ret = 1;
1898
1899 exit:
1900         rte_kvargs_free(kvlist);
1901         return ret;
1902 }
1903
1904 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1905         struct rte_pci_device *pci_dev)
1906 {
1907         if (rte_eal_iopl_init() != 0) {
1908                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1909                 return 1;
1910         }
1911
1912         /* virtio pmd skips probe if device needs to work in vdpa mode */
1913         if (vdpa_mode_selected(pci_dev->device.devargs))
1914                 return 1;
1915
1916         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1917                 eth_virtio_dev_init);
1918 }
1919
1920 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1921 {
1922         return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1923 }
1924
1925 static struct rte_pci_driver rte_virtio_pmd = {
1926         .driver = {
1927                 .name = "net_virtio",
1928         },
1929         .id_table = pci_id_virtio_map,
1930         .drv_flags = 0,
1931         .probe = eth_virtio_pci_probe,
1932         .remove = eth_virtio_pci_remove,
1933 };
1934
1935 RTE_INIT(rte_virtio_pmd_init)
1936 {
1937         rte_eal_iopl_init();
1938         rte_pci_register(&rte_virtio_pmd);
1939 }
1940
1941 static bool
1942 rx_offload_enabled(struct virtio_hw *hw)
1943 {
1944         return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1945                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1946                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1947 }
1948
1949 static bool
1950 tx_offload_enabled(struct virtio_hw *hw)
1951 {
1952         return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1953                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1954                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1955 }
1956
1957 /*
1958  * Configure virtio device
1959  * It returns 0 on success.
1960  */
1961 static int
1962 virtio_dev_configure(struct rte_eth_dev *dev)
1963 {
1964         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1965         const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1966         struct virtio_hw *hw = dev->data->dev_private;
1967         uint64_t rx_offloads = rxmode->offloads;
1968         uint64_t tx_offloads = txmode->offloads;
1969         uint64_t req_features;
1970         int ret;
1971
1972         PMD_INIT_LOG(DEBUG, "configure");
1973         req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1974
1975         if (dev->data->dev_conf.intr_conf.rxq) {
1976                 ret = virtio_init_device(dev, hw->req_guest_features);
1977                 if (ret < 0)
1978                         return ret;
1979         }
1980
1981         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1982                            DEV_RX_OFFLOAD_TCP_CKSUM))
1983                 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1984
1985         if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
1986                 req_features |=
1987                         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1988                         (1ULL << VIRTIO_NET_F_GUEST_TSO6);
1989
1990         if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
1991                            DEV_TX_OFFLOAD_TCP_CKSUM))
1992                 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
1993
1994         if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
1995                 req_features |=
1996                         (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1997                         (1ULL << VIRTIO_NET_F_HOST_TSO6);
1998
1999         /* if request features changed, reinit the device */
2000         if (req_features != hw->req_guest_features) {
2001                 ret = virtio_init_device(dev, req_features);
2002                 if (ret < 0)
2003                         return ret;
2004         }
2005
2006         if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2007                             DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2008                 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2009                 PMD_DRV_LOG(ERR,
2010                         "rx checksum not available on this host");
2011                 return -ENOTSUP;
2012         }
2013
2014         if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2015                 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2016                  !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2017                 PMD_DRV_LOG(ERR,
2018                         "Large Receive Offload not available on this host");
2019                 return -ENOTSUP;
2020         }
2021
2022         /* start control queue */
2023         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2024                 virtio_dev_cq_start(dev);
2025
2026         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2027                 hw->vlan_strip = 1;
2028
2029         if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2030             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2031                 PMD_DRV_LOG(ERR,
2032                             "vlan filtering not available on this host");
2033                 return -ENOTSUP;
2034         }
2035
2036         hw->has_tx_offload = tx_offload_enabled(hw);
2037         hw->has_rx_offload = rx_offload_enabled(hw);
2038
2039         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2040                 /* Enable vector (0) for Link State Intrerrupt */
2041                 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2042                                 VIRTIO_MSI_NO_VECTOR) {
2043                         PMD_DRV_LOG(ERR, "failed to set config vector");
2044                         return -EBUSY;
2045                 }
2046
2047         rte_spinlock_init(&hw->state_lock);
2048
2049         hw->use_simple_rx = 1;
2050
2051         if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2052                 hw->use_inorder_tx = 1;
2053                 hw->use_inorder_rx = 1;
2054                 hw->use_simple_rx = 0;
2055         }
2056
2057         if (vtpci_packed_queue(hw)) {
2058                 hw->use_simple_rx = 0;
2059                 hw->use_inorder_rx = 0;
2060                 hw->use_inorder_tx = 0;
2061         }
2062
2063 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2064         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2065                 hw->use_simple_rx = 0;
2066         }
2067 #endif
2068         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2069                  hw->use_simple_rx = 0;
2070         }
2071
2072         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2073                            DEV_RX_OFFLOAD_TCP_CKSUM |
2074                            DEV_RX_OFFLOAD_TCP_LRO |
2075                            DEV_RX_OFFLOAD_VLAN_STRIP))
2076                 hw->use_simple_rx = 0;
2077
2078         hw->opened = true;
2079
2080         return 0;
2081 }
2082
2083
2084 static int
2085 virtio_dev_start(struct rte_eth_dev *dev)
2086 {
2087         uint16_t nb_queues, i;
2088         struct virtnet_rx *rxvq;
2089         struct virtnet_tx *txvq __rte_unused;
2090         struct virtio_hw *hw = dev->data->dev_private;
2091         int ret;
2092
2093         /* Finish the initialization of the queues */
2094         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2095                 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2096                 if (ret < 0)
2097                         return ret;
2098         }
2099         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2100                 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2101                 if (ret < 0)
2102                         return ret;
2103         }
2104
2105         /* check if lsc interrupt feature is enabled */
2106         if (dev->data->dev_conf.intr_conf.lsc) {
2107                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2108                         PMD_DRV_LOG(ERR, "link status not supported by host");
2109                         return -ENOTSUP;
2110                 }
2111         }
2112
2113         /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2114          * in device configure, but it could be unmapped  when device is
2115          * stopped.
2116          */
2117         if (dev->data->dev_conf.intr_conf.lsc ||
2118             dev->data->dev_conf.intr_conf.rxq) {
2119                 virtio_intr_disable(dev);
2120
2121                 /* Setup interrupt callback  */
2122                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2123                         rte_intr_callback_register(dev->intr_handle,
2124                                                    virtio_interrupt_handler,
2125                                                    dev);
2126
2127                 if (virtio_intr_enable(dev) < 0) {
2128                         PMD_DRV_LOG(ERR, "interrupt enable failed");
2129                         return -EIO;
2130                 }
2131         }
2132
2133         /*Notify the backend
2134          *Otherwise the tap backend might already stop its queue due to fullness.
2135          *vhost backend will have no chance to be waked up
2136          */
2137         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2138         if (hw->max_queue_pairs > 1) {
2139                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2140                         return -EINVAL;
2141         }
2142
2143         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2144
2145         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2146                 rxvq = dev->data->rx_queues[i];
2147                 /* Flush the old packets */
2148                 virtqueue_rxvq_flush(rxvq->vq);
2149                 virtqueue_notify(rxvq->vq);
2150         }
2151
2152         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2153                 txvq = dev->data->tx_queues[i];
2154                 virtqueue_notify(txvq->vq);
2155         }
2156
2157         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2158
2159         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2160                 rxvq = dev->data->rx_queues[i];
2161                 VIRTQUEUE_DUMP(rxvq->vq);
2162         }
2163
2164         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2165                 txvq = dev->data->tx_queues[i];
2166                 VIRTQUEUE_DUMP(txvq->vq);
2167         }
2168
2169         set_rxtx_funcs(dev);
2170         hw->started = true;
2171
2172         /* Initialize Link state */
2173         virtio_dev_link_update(dev, 0);
2174
2175         return 0;
2176 }
2177
2178 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2179 {
2180         struct virtio_hw *hw = dev->data->dev_private;
2181         uint16_t nr_vq = virtio_get_nr_vq(hw);
2182         const char *type __rte_unused;
2183         unsigned int i, mbuf_num = 0;
2184         struct virtqueue *vq;
2185         struct rte_mbuf *buf;
2186         int queue_type;
2187
2188         if (hw->vqs == NULL)
2189                 return;
2190
2191         for (i = 0; i < nr_vq; i++) {
2192                 vq = hw->vqs[i];
2193                 if (!vq)
2194                         continue;
2195
2196                 queue_type = virtio_get_queue_type(hw, i);
2197                 if (queue_type == VTNET_RQ)
2198                         type = "rxq";
2199                 else if (queue_type == VTNET_TQ)
2200                         type = "txq";
2201                 else
2202                         continue;
2203
2204                 PMD_INIT_LOG(DEBUG,
2205                         "Before freeing %s[%d] used and unused buf",
2206                         type, i);
2207                 VIRTQUEUE_DUMP(vq);
2208
2209                 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2210                         rte_pktmbuf_free(buf);
2211                         mbuf_num++;
2212                 }
2213
2214                 PMD_INIT_LOG(DEBUG,
2215                         "After freeing %s[%d] used and unused buf",
2216                         type, i);
2217                 VIRTQUEUE_DUMP(vq);
2218         }
2219
2220         PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2221 }
2222
2223 /*
2224  * Stop device: disable interrupt and mark link down
2225  */
2226 static void
2227 virtio_dev_stop(struct rte_eth_dev *dev)
2228 {
2229         struct virtio_hw *hw = dev->data->dev_private;
2230         struct rte_eth_link link;
2231         struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2232
2233         PMD_INIT_LOG(DEBUG, "stop");
2234
2235         rte_spinlock_lock(&hw->state_lock);
2236         if (!hw->started)
2237                 goto out_unlock;
2238         hw->started = false;
2239
2240         if (intr_conf->lsc || intr_conf->rxq) {
2241                 virtio_intr_disable(dev);
2242
2243                 /* Reset interrupt callback  */
2244                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2245                         rte_intr_callback_unregister(dev->intr_handle,
2246                                                      virtio_interrupt_handler,
2247                                                      dev);
2248                 }
2249         }
2250
2251         memset(&link, 0, sizeof(link));
2252         rte_eth_linkstatus_set(dev, &link);
2253 out_unlock:
2254         rte_spinlock_unlock(&hw->state_lock);
2255 }
2256
2257 static int
2258 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2259 {
2260         struct rte_eth_link link;
2261         uint16_t status;
2262         struct virtio_hw *hw = dev->data->dev_private;
2263
2264         memset(&link, 0, sizeof(link));
2265         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2266         link.link_speed  = ETH_SPEED_NUM_10G;
2267         link.link_autoneg = ETH_LINK_FIXED;
2268
2269         if (!hw->started) {
2270                 link.link_status = ETH_LINK_DOWN;
2271         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2272                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2273                 vtpci_read_dev_config(hw,
2274                                 offsetof(struct virtio_net_config, status),
2275                                 &status, sizeof(status));
2276                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2277                         link.link_status = ETH_LINK_DOWN;
2278                         PMD_INIT_LOG(DEBUG, "Port %d is down",
2279                                      dev->data->port_id);
2280                 } else {
2281                         link.link_status = ETH_LINK_UP;
2282                         PMD_INIT_LOG(DEBUG, "Port %d is up",
2283                                      dev->data->port_id);
2284                 }
2285         } else {
2286                 link.link_status = ETH_LINK_UP;
2287         }
2288
2289         return rte_eth_linkstatus_set(dev, &link);
2290 }
2291
2292 static int
2293 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2294 {
2295         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2296         struct virtio_hw *hw = dev->data->dev_private;
2297         uint64_t offloads = rxmode->offloads;
2298
2299         if (mask & ETH_VLAN_FILTER_MASK) {
2300                 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2301                                 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2302
2303                         PMD_DRV_LOG(NOTICE,
2304                                 "vlan filtering not available on this host");
2305
2306                         return -ENOTSUP;
2307                 }
2308         }
2309
2310         if (mask & ETH_VLAN_STRIP_MASK)
2311                 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2312
2313         return 0;
2314 }
2315
2316 static void
2317 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2318 {
2319         uint64_t tso_mask, host_features;
2320         struct virtio_hw *hw = dev->data->dev_private;
2321
2322         dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2323
2324         dev_info->max_rx_queues =
2325                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2326         dev_info->max_tx_queues =
2327                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2328         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2329         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2330         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2331
2332         host_features = VTPCI_OPS(hw)->get_features(hw);
2333         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2334         if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2335                 dev_info->rx_offload_capa |=
2336                         DEV_RX_OFFLOAD_TCP_CKSUM |
2337                         DEV_RX_OFFLOAD_UDP_CKSUM;
2338         }
2339         if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2340                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2341         tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2342                 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2343         if ((host_features & tso_mask) == tso_mask)
2344                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2345
2346         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2347                                     DEV_TX_OFFLOAD_VLAN_INSERT;
2348         if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2349                 dev_info->tx_offload_capa |=
2350                         DEV_TX_OFFLOAD_UDP_CKSUM |
2351                         DEV_TX_OFFLOAD_TCP_CKSUM;
2352         }
2353         tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2354                 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2355         if ((host_features & tso_mask) == tso_mask)
2356                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2357 }
2358
2359 /*
2360  * It enables testpmd to collect per queue stats.
2361  */
2362 static int
2363 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2364 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2365 __rte_unused uint8_t is_rx)
2366 {
2367         return 0;
2368 }
2369
2370 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2371 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2372 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2373
2374 RTE_INIT(virtio_init_log)
2375 {
2376         virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2377         if (virtio_logtype_init >= 0)
2378                 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2379         virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2380         if (virtio_logtype_driver >= 0)
2381                 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);
2382 }