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