New upstream version 16.11.9
[deb_dpdk.git] / drivers / net / virtio / virtio_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <unistd.h>
39
40 #include <rte_ethdev.h>
41 #include <rte_memcpy.h>
42 #include <rte_string_fns.h>
43 #include <rte_memzone.h>
44 #include <rte_malloc.h>
45 #include <rte_atomic.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_pci.h>
48 #include <rte_ether.h>
49 #include <rte_common.h>
50 #include <rte_errno.h>
51
52 #include <rte_memory.h>
53 #include <rte_eal.h>
54 #include <rte_dev.h>
55
56 #include "virtio_ethdev.h"
57 #include "virtio_pci.h"
58 #include "virtio_logs.h"
59 #include "virtqueue.h"
60 #include "virtio_rxtx.h"
61
62 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
63 static int  virtio_dev_configure(struct rte_eth_dev *dev);
64 static int  virtio_dev_start(struct rte_eth_dev *dev);
65 static void virtio_dev_stop(struct rte_eth_dev *dev);
66 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
67 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
68 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
69 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
70 static void virtio_dev_info_get(struct rte_eth_dev *dev,
71                                 struct rte_eth_dev_info *dev_info);
72 static int virtio_dev_link_update(struct rte_eth_dev *dev,
73         __rte_unused int wait_to_complete);
74
75 static void virtio_set_hwaddr(struct virtio_hw *hw);
76 static void virtio_get_hwaddr(struct virtio_hw *hw);
77
78 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
79                                  struct rte_eth_stats *stats);
80 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
81                                  struct rte_eth_xstat *xstats, unsigned n);
82 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
83                                        struct rte_eth_xstat_name *xstats_names,
84                                        unsigned limit);
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
88                                 uint16_t vlan_id, int on);
89 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
90                                 struct ether_addr *mac_addr,
91                                 uint32_t index, uint32_t vmdq __rte_unused);
92 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
93 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94                                 struct ether_addr *mac_addr);
95
96 static int virtio_dev_queue_stats_mapping_set(
97         __rte_unused struct rte_eth_dev *eth_dev,
98         __rte_unused uint16_t queue_id,
99         __rte_unused uint8_t stat_idx,
100         __rte_unused uint8_t is_rx);
101
102 /*
103  * The set of PCI devices this driver supports
104  */
105 static const struct rte_pci_id pci_id_virtio_map[] = {
106         { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
107         { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
108         { .vendor_id = 0, /* sentinel */ },
109 };
110
111 struct rte_virtio_xstats_name_off {
112         char name[RTE_ETH_XSTATS_NAME_SIZE];
113         unsigned offset;
114 };
115
116 /* [rt]x_qX_ is prepended to the name string here */
117 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
118         {"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
119         {"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
120         {"errors",                 offsetof(struct virtnet_rx, stats.errors)},
121         {"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
122         {"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
123         {"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
124         {"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
125         {"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
126         {"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
127         {"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
128         {"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
129         {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
130         {"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
131 };
132
133 /* [rt]x_qX_ is prepended to the name string here */
134 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
135         {"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
136         {"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
137         {"errors",                 offsetof(struct virtnet_tx, stats.errors)},
138         {"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
139         {"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
140         {"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
141         {"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
142         {"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
143         {"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
144         {"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
145         {"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
146         {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
147         {"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
148 };
149
150 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
151                             sizeof(rte_virtio_rxq_stat_strings[0]))
152 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
153                             sizeof(rte_virtio_txq_stat_strings[0]))
154
155 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
156
157 static int
158 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
159                 int *dlen, int pkt_num)
160 {
161         uint32_t head, i;
162         int k, sum = 0;
163         virtio_net_ctrl_ack status = ~0;
164         struct virtio_pmd_ctrl result;
165         struct virtqueue *vq;
166
167         ctrl->status = status;
168
169         if (!cvq || !cvq->vq) {
170                 PMD_INIT_LOG(ERR, "Control queue is not supported.");
171                 return -1;
172         }
173         vq = cvq->vq;
174         head = vq->vq_desc_head_idx;
175
176         PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
177                 "vq->hw->cvq = %p vq = %p",
178                 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
179
180         if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
181                 return -1;
182
183         memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
184                 sizeof(struct virtio_pmd_ctrl));
185
186         /*
187          * Format is enforced in qemu code:
188          * One TX packet for header;
189          * At least one TX packet per argument;
190          * One RX packet for ACK.
191          */
192         vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
193         vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
194         vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
195         vq->vq_free_cnt--;
196         i = vq->vq_ring.desc[head].next;
197
198         for (k = 0; k < pkt_num; k++) {
199                 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
200                 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
201                         + sizeof(struct virtio_net_ctrl_hdr)
202                         + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
203                 vq->vq_ring.desc[i].len = dlen[k];
204                 sum += dlen[k];
205                 vq->vq_free_cnt--;
206                 i = vq->vq_ring.desc[i].next;
207         }
208
209         vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
210         vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
211                         + sizeof(struct virtio_net_ctrl_hdr);
212         vq->vq_ring.desc[i].len = sizeof(ctrl->status);
213         vq->vq_free_cnt--;
214
215         vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
216
217         vq_update_avail_ring(vq, head);
218         vq_update_avail_idx(vq);
219
220         PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
221
222         virtqueue_notify(vq);
223
224         rte_rmb();
225         while (VIRTQUEUE_NUSED(vq) == 0) {
226                 rte_rmb();
227                 usleep(100);
228         }
229
230         while (VIRTQUEUE_NUSED(vq)) {
231                 uint32_t idx, desc_idx, used_idx;
232                 struct vring_used_elem *uep;
233
234                 used_idx = (uint32_t)(vq->vq_used_cons_idx
235                                 & (vq->vq_nentries - 1));
236                 uep = &vq->vq_ring.used->ring[used_idx];
237                 idx = (uint32_t) uep->id;
238                 desc_idx = idx;
239
240                 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
241                         desc_idx = vq->vq_ring.desc[desc_idx].next;
242                         vq->vq_free_cnt++;
243                 }
244
245                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
246                 vq->vq_desc_head_idx = idx;
247
248                 vq->vq_used_cons_idx++;
249                 vq->vq_free_cnt++;
250         }
251
252         PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
253                         vq->vq_free_cnt, vq->vq_desc_head_idx);
254
255         memcpy(&result, cvq->virtio_net_hdr_mz->addr,
256                         sizeof(struct virtio_pmd_ctrl));
257
258         return result.status;
259 }
260
261 static int
262 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
263 {
264         struct virtio_hw *hw = dev->data->dev_private;
265         struct virtio_pmd_ctrl ctrl;
266         int dlen[1];
267         int ret;
268
269         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
270         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
271         memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
272
273         dlen[0] = sizeof(uint16_t);
274
275         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
276         if (ret) {
277                 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
278                           "failed, this is too late now...");
279                 return -EINVAL;
280         }
281
282         return 0;
283 }
284
285 static void
286 virtio_dev_queue_release(void *queue __rte_unused)
287 {
288         /* do nothing */
289 }
290
291 static uint16_t
292 virtio_get_nr_vq(struct virtio_hw *hw)
293 {
294         uint16_t nr_vq = hw->max_queue_pairs * 2;
295
296         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
297                 nr_vq += 1;
298
299         return nr_vq;
300 }
301
302 static void
303 virtio_init_vring(struct virtqueue *vq)
304 {
305         int size = vq->vq_nentries;
306         struct vring *vr = &vq->vq_ring;
307         uint8_t *ring_mem = vq->vq_ring_virt_mem;
308
309         PMD_INIT_FUNC_TRACE();
310
311         /*
312          * Reinitialise since virtio port might have been stopped and restarted
313          */
314         memset(ring_mem, 0, vq->vq_ring_size);
315         vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
316         vq->vq_used_cons_idx = 0;
317         vq->vq_desc_head_idx = 0;
318         vq->vq_avail_idx = 0;
319         vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
320         vq->vq_free_cnt = vq->vq_nentries;
321         memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
322
323         vring_desc_init(vr->desc, size);
324
325         /*
326          * Disable device(host) interrupting guest
327          */
328         virtqueue_disable_intr(vq);
329 }
330
331 static int
332 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
333 {
334         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
335         char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
336         const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
337         unsigned int vq_size, size;
338         struct virtio_hw *hw = dev->data->dev_private;
339         struct virtnet_rx *rxvq = NULL;
340         struct virtnet_tx *txvq = NULL;
341         struct virtnet_ctl *cvq = NULL;
342         struct virtqueue *vq;
343         size_t sz_hdr_mz = 0;
344         void *sw_ring = NULL;
345         int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
346         int ret;
347
348         PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
349
350         /*
351          * Read the virtqueue size from the Queue Size field
352          * Always power of 2 and if 0 virtqueue does not exist
353          */
354         vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
355         PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
356         if (vq_size == 0) {
357                 PMD_INIT_LOG(ERR, "virtqueue does not exist");
358                 return -EINVAL;
359         }
360
361         if (!rte_is_power_of_2(vq_size)) {
362                 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
363                 return -EINVAL;
364         }
365
366         snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
367                  dev->data->port_id, vtpci_queue_idx);
368
369         size = RTE_ALIGN_CEIL(sizeof(*vq) +
370                                 vq_size * sizeof(struct vq_desc_extra),
371                                 RTE_CACHE_LINE_SIZE);
372         if (queue_type == VTNET_TQ) {
373                 /*
374                  * For each xmit packet, allocate a virtio_net_hdr
375                  * and indirect ring elements
376                  */
377                 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
378         } else if (queue_type == VTNET_CQ) {
379                 /* Allocate a page for control vq command, data and status */
380                 sz_hdr_mz = PAGE_SIZE;
381         }
382
383         vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
384                                 SOCKET_ID_ANY);
385         if (vq == NULL) {
386                 PMD_INIT_LOG(ERR, "can not allocate vq");
387                 return -ENOMEM;
388         }
389         hw->vqs[vtpci_queue_idx] = vq;
390
391         vq->hw = hw;
392         vq->vq_queue_index = vtpci_queue_idx;
393         vq->vq_nentries = vq_size;
394
395         /*
396          * Reserve a memzone for vring elements
397          */
398         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
399         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
400         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
401                      size, vq->vq_ring_size);
402
403         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
404                                          SOCKET_ID_ANY,
405                                          0, VIRTIO_PCI_VRING_ALIGN);
406         if (mz == NULL) {
407                 if (rte_errno == EEXIST)
408                         mz = rte_memzone_lookup(vq_name);
409                 if (mz == NULL) {
410                         ret = -ENOMEM;
411                         goto fail_q_alloc;
412                 }
413         }
414
415         memset(mz->addr, 0, mz->len);
416
417         vq->vq_ring_mem = mz->phys_addr;
418         vq->vq_ring_virt_mem = mz->addr;
419         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
420                      (uint64_t)mz->phys_addr);
421         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
422                      (uint64_t)(uintptr_t)mz->addr);
423
424         virtio_init_vring(vq);
425
426         if (sz_hdr_mz) {
427                 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
428                          dev->data->port_id, vtpci_queue_idx);
429                 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
430                                                      SOCKET_ID_ANY, 0,
431                                                      RTE_CACHE_LINE_SIZE);
432                 if (hdr_mz == NULL) {
433                         if (rte_errno == EEXIST)
434                                 hdr_mz = rte_memzone_lookup(vq_hdr_name);
435                         if (hdr_mz == NULL) {
436                                 ret = -ENOMEM;
437                                 goto fail_q_alloc;
438                         }
439                 }
440         }
441
442         if (queue_type == VTNET_RQ) {
443                 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
444                                sizeof(vq->sw_ring[0]);
445
446                 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
447                                 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
448                 if (!sw_ring) {
449                         PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
450                         ret = -ENOMEM;
451                         goto fail_q_alloc;
452                 }
453
454                 vq->sw_ring = sw_ring;
455                 rxvq = &vq->rxq;
456                 rxvq->vq = vq;
457                 rxvq->port_id = dev->data->port_id;
458                 rxvq->mz = mz;
459         } else if (queue_type == VTNET_TQ) {
460                 txvq = &vq->txq;
461                 txvq->vq = vq;
462                 txvq->port_id = dev->data->port_id;
463                 txvq->mz = mz;
464                 txvq->virtio_net_hdr_mz = hdr_mz;
465                 txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
466         } else if (queue_type == VTNET_CQ) {
467                 cvq = &vq->cq;
468                 cvq->vq = vq;
469                 cvq->mz = mz;
470                 cvq->virtio_net_hdr_mz = hdr_mz;
471                 cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
472                 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
473
474                 hw->cvq = cvq;
475         }
476
477         /* For virtio_user case (that is when dev->pci_dev is NULL), we use
478          * virtual address. And we need properly set _offset_, please see
479          * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
480          */
481         if (dev->pci_dev)
482                 vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
483         else {
484                 vq->vq_ring_mem = (uintptr_t)mz->addr;
485                 vq->offset = offsetof(struct rte_mbuf, buf_addr);
486                 if (queue_type == VTNET_TQ)
487                         txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
488                 else if (queue_type == VTNET_CQ)
489                         cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
490         }
491
492         if (queue_type == VTNET_TQ) {
493                 struct virtio_tx_region *txr;
494                 unsigned int i;
495
496                 txr = hdr_mz->addr;
497                 memset(txr, 0, vq_size * sizeof(*txr));
498                 for (i = 0; i < vq_size; i++) {
499                         struct vring_desc *start_dp = txr[i].tx_indir;
500
501                         vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
502
503                         /* first indirect descriptor is always the tx header */
504                         start_dp->addr = txvq->virtio_net_hdr_mem
505                                 + i * sizeof(*txr)
506                                 + offsetof(struct virtio_tx_region, tx_hdr);
507
508                         start_dp->len = hw->vtnet_hdr_size;
509                         start_dp->flags = VRING_DESC_F_NEXT;
510                 }
511         }
512
513         if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
514                 PMD_INIT_LOG(ERR, "setup_queue failed");
515                 return -EINVAL;
516         }
517
518         return 0;
519
520 fail_q_alloc:
521         rte_free(sw_ring);
522         rte_memzone_free(hdr_mz);
523         rte_memzone_free(mz);
524         rte_free(vq);
525
526         return ret;
527 }
528
529 static void
530 virtio_free_queues(struct virtio_hw *hw)
531 {
532         uint16_t nr_vq = virtio_get_nr_vq(hw);
533         struct virtqueue *vq;
534         int queue_type;
535         uint16_t i;
536
537         if (hw->vqs == NULL)
538                 return;
539
540         for (i = 0; i < nr_vq; i++) {
541                 vq = hw->vqs[i];
542                 if (!vq)
543                         continue;
544
545                 queue_type = virtio_get_queue_type(hw, i);
546                 if (queue_type == VTNET_RQ) {
547                         rte_free(vq->sw_ring);
548                         rte_memzone_free(vq->rxq.mz);
549                 } else if (queue_type == VTNET_TQ) {
550                         rte_memzone_free(vq->txq.mz);
551                         rte_memzone_free(vq->txq.virtio_net_hdr_mz);
552                 } else {
553                         rte_memzone_free(vq->cq.mz);
554                         rte_memzone_free(vq->cq.virtio_net_hdr_mz);
555                 }
556
557                 rte_free(vq);
558                 hw->vqs[i] = NULL;
559         }
560
561         rte_free(hw->vqs);
562         hw->vqs = NULL;
563 }
564
565 static int
566 virtio_alloc_queues(struct rte_eth_dev *dev)
567 {
568         struct virtio_hw *hw = dev->data->dev_private;
569         uint16_t nr_vq = virtio_get_nr_vq(hw);
570         uint16_t i;
571         int ret;
572
573         hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
574         if (!hw->vqs) {
575                 PMD_INIT_LOG(ERR, "failed to allocate vqs");
576                 return -ENOMEM;
577         }
578
579         for (i = 0; i < nr_vq; i++) {
580                 ret = virtio_init_queue(dev, i);
581                 if (ret < 0) {
582                         virtio_free_queues(hw);
583                         return ret;
584                 }
585         }
586
587         return 0;
588 }
589
590 static void
591 virtio_dev_close(struct rte_eth_dev *dev)
592 {
593         struct virtio_hw *hw = dev->data->dev_private;
594
595         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
596
597         /* reset the NIC */
598         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
599                 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
600         vtpci_reset(hw);
601         virtio_dev_free_mbufs(dev);
602         virtio_free_queues(hw);
603 }
604
605 static void
606 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
607 {
608         struct virtio_hw *hw = dev->data->dev_private;
609         struct virtio_pmd_ctrl ctrl;
610         int dlen[1];
611         int ret;
612
613         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
614                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
615                 return;
616         }
617
618         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
619         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
620         ctrl.data[0] = 1;
621         dlen[0] = 1;
622
623         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
624         if (ret)
625                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
626 }
627
628 static void
629 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
630 {
631         struct virtio_hw *hw = dev->data->dev_private;
632         struct virtio_pmd_ctrl ctrl;
633         int dlen[1];
634         int ret;
635
636         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
637                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
638                 return;
639         }
640
641         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
642         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
643         ctrl.data[0] = 0;
644         dlen[0] = 1;
645
646         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
647         if (ret)
648                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
649 }
650
651 static void
652 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
653 {
654         struct virtio_hw *hw = dev->data->dev_private;
655         struct virtio_pmd_ctrl ctrl;
656         int dlen[1];
657         int ret;
658
659         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
660                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
661                 return;
662         }
663
664         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
665         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
666         ctrl.data[0] = 1;
667         dlen[0] = 1;
668
669         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
670         if (ret)
671                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
672 }
673
674 static void
675 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
676 {
677         struct virtio_hw *hw = dev->data->dev_private;
678         struct virtio_pmd_ctrl ctrl;
679         int dlen[1];
680         int ret;
681
682         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
683                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
684                 return;
685         }
686
687         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
688         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
689         ctrl.data[0] = 0;
690         dlen[0] = 1;
691
692         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
693         if (ret)
694                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
695 }
696
697 #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
698 static int
699 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
700 {
701         struct virtio_hw *hw = dev->data->dev_private;
702         uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
703                                  hw->vtnet_hdr_size;
704         uint32_t frame_size = mtu + ether_hdr_len;
705
706         if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
707                 PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
708                         ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
709                 return -EINVAL;
710         }
711         return 0;
712 }
713
714 /*
715  * dev_ops for virtio, bare necessities for basic operation
716  */
717 static const struct eth_dev_ops virtio_eth_dev_ops = {
718         .dev_configure           = virtio_dev_configure,
719         .dev_start               = virtio_dev_start,
720         .dev_stop                = virtio_dev_stop,
721         .dev_close               = virtio_dev_close,
722         .promiscuous_enable      = virtio_dev_promiscuous_enable,
723         .promiscuous_disable     = virtio_dev_promiscuous_disable,
724         .allmulticast_enable     = virtio_dev_allmulticast_enable,
725         .allmulticast_disable    = virtio_dev_allmulticast_disable,
726         .mtu_set                 = virtio_mtu_set,
727         .dev_infos_get           = virtio_dev_info_get,
728         .stats_get               = virtio_dev_stats_get,
729         .xstats_get              = virtio_dev_xstats_get,
730         .xstats_get_names        = virtio_dev_xstats_get_names,
731         .stats_reset             = virtio_dev_stats_reset,
732         .xstats_reset            = virtio_dev_stats_reset,
733         .link_update             = virtio_dev_link_update,
734         .rx_queue_setup          = virtio_dev_rx_queue_setup,
735         .rx_queue_release        = virtio_dev_queue_release,
736         .tx_queue_setup          = virtio_dev_tx_queue_setup,
737         .tx_queue_release        = virtio_dev_queue_release,
738         /* collect stats per queue */
739         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
740         .vlan_filter_set         = virtio_vlan_filter_set,
741         .mac_addr_add            = virtio_mac_addr_add,
742         .mac_addr_remove         = virtio_mac_addr_remove,
743         .mac_addr_set            = virtio_mac_addr_set,
744 };
745
746 static inline int
747 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
748                                 struct rte_eth_link *link)
749 {
750         struct rte_eth_link *dst = link;
751         struct rte_eth_link *src = &(dev->data->dev_link);
752
753         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
754                         *(uint64_t *)src) == 0)
755                 return -1;
756
757         return 0;
758 }
759
760 /**
761  * Atomically writes the link status information into global
762  * structure rte_eth_dev.
763  *
764  * @param dev
765  *   - Pointer to the structure rte_eth_dev to read from.
766  *   - Pointer to the buffer to be saved with the link status.
767  *
768  * @return
769  *   - On success, zero.
770  *   - On failure, negative value.
771  */
772 static inline int
773 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
774                 struct rte_eth_link *link)
775 {
776         struct rte_eth_link *dst = &(dev->data->dev_link);
777         struct rte_eth_link *src = link;
778
779         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
780                                         *(uint64_t *)src) == 0)
781                 return -1;
782
783         return 0;
784 }
785
786 static void
787 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
788 {
789         unsigned i;
790
791         for (i = 0; i < dev->data->nb_tx_queues; i++) {
792                 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
793                 if (txvq == NULL)
794                         continue;
795
796                 stats->opackets += txvq->stats.packets;
797                 stats->obytes += txvq->stats.bytes;
798                 stats->oerrors += txvq->stats.errors;
799
800                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
801                         stats->q_opackets[i] = txvq->stats.packets;
802                         stats->q_obytes[i] = txvq->stats.bytes;
803                 }
804         }
805
806         for (i = 0; i < dev->data->nb_rx_queues; i++) {
807                 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
808                 if (rxvq == NULL)
809                         continue;
810
811                 stats->ipackets += rxvq->stats.packets;
812                 stats->ibytes += rxvq->stats.bytes;
813                 stats->ierrors += rxvq->stats.errors;
814
815                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
816                         stats->q_ipackets[i] = rxvq->stats.packets;
817                         stats->q_ibytes[i] = rxvq->stats.bytes;
818                 }
819         }
820
821         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
822 }
823
824 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
825                                        struct rte_eth_xstat_name *xstats_names,
826                                        __rte_unused unsigned limit)
827 {
828         unsigned i;
829         unsigned count = 0;
830         unsigned t;
831
832         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
833                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
834
835         if (xstats_names != NULL) {
836                 /* Note: limit checked in rte_eth_xstats_names() */
837
838                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
839                         struct virtnet_rx *rxvq = dev->data->rx_queues[i];
840                         if (rxvq == NULL)
841                                 continue;
842                         for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
843                                 snprintf(xstats_names[count].name,
844                                         sizeof(xstats_names[count].name),
845                                         "rx_q%u_%s", i,
846                                         rte_virtio_rxq_stat_strings[t].name);
847                                 count++;
848                         }
849                 }
850
851                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
852                         struct virtnet_tx *txvq = dev->data->tx_queues[i];
853                         if (txvq == NULL)
854                                 continue;
855                         for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
856                                 snprintf(xstats_names[count].name,
857                                         sizeof(xstats_names[count].name),
858                                         "tx_q%u_%s", i,
859                                         rte_virtio_txq_stat_strings[t].name);
860                                 count++;
861                         }
862                 }
863                 return count;
864         }
865         return nstats;
866 }
867
868 static int
869 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
870                       unsigned n)
871 {
872         unsigned i;
873         unsigned count = 0;
874
875         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
876                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
877
878         if (n < nstats)
879                 return nstats;
880
881         for (i = 0; i < dev->data->nb_rx_queues; i++) {
882                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
883
884                 if (rxvq == NULL)
885                         continue;
886
887                 unsigned t;
888
889                 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
890                         xstats[count].value = *(uint64_t *)(((char *)rxvq) +
891                                 rte_virtio_rxq_stat_strings[t].offset);
892                         xstats[count].id = count;
893                         count++;
894                 }
895         }
896
897         for (i = 0; i < dev->data->nb_tx_queues; i++) {
898                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
899
900                 if (txvq == NULL)
901                         continue;
902
903                 unsigned t;
904
905                 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
906                         xstats[count].value = *(uint64_t *)(((char *)txvq) +
907                                 rte_virtio_txq_stat_strings[t].offset);
908                         xstats[count].id = count;
909                         count++;
910                 }
911         }
912
913         return count;
914 }
915
916 static void
917 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
918 {
919         virtio_update_stats(dev, stats);
920 }
921
922 static void
923 virtio_dev_stats_reset(struct rte_eth_dev *dev)
924 {
925         unsigned int i;
926
927         for (i = 0; i < dev->data->nb_tx_queues; i++) {
928                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
929                 if (txvq == NULL)
930                         continue;
931
932                 txvq->stats.packets = 0;
933                 txvq->stats.bytes = 0;
934                 txvq->stats.errors = 0;
935                 txvq->stats.multicast = 0;
936                 txvq->stats.broadcast = 0;
937                 memset(txvq->stats.size_bins, 0,
938                        sizeof(txvq->stats.size_bins[0]) * 8);
939         }
940
941         for (i = 0; i < dev->data->nb_rx_queues; i++) {
942                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
943                 if (rxvq == NULL)
944                         continue;
945
946                 rxvq->stats.packets = 0;
947                 rxvq->stats.bytes = 0;
948                 rxvq->stats.errors = 0;
949                 rxvq->stats.multicast = 0;
950                 rxvq->stats.broadcast = 0;
951                 memset(rxvq->stats.size_bins, 0,
952                        sizeof(rxvq->stats.size_bins[0]) * 8);
953         }
954 }
955
956 static void
957 virtio_set_hwaddr(struct virtio_hw *hw)
958 {
959         vtpci_write_dev_config(hw,
960                         offsetof(struct virtio_net_config, mac),
961                         &hw->mac_addr, ETHER_ADDR_LEN);
962 }
963
964 static void
965 virtio_get_hwaddr(struct virtio_hw *hw)
966 {
967         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
968                 vtpci_read_dev_config(hw,
969                         offsetof(struct virtio_net_config, mac),
970                         &hw->mac_addr, ETHER_ADDR_LEN);
971         } else {
972                 eth_random_addr(&hw->mac_addr[0]);
973                 virtio_set_hwaddr(hw);
974         }
975 }
976
977 static void
978 virtio_mac_table_set(struct virtio_hw *hw,
979                      const struct virtio_net_ctrl_mac *uc,
980                      const struct virtio_net_ctrl_mac *mc)
981 {
982         struct virtio_pmd_ctrl ctrl;
983         int err, len[2];
984
985         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
986                 PMD_DRV_LOG(INFO, "host does not support mac table");
987                 return;
988         }
989
990         ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
991         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
992
993         len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
994         memcpy(ctrl.data, uc, len[0]);
995
996         len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
997         memcpy(ctrl.data + len[0], mc, len[1]);
998
999         err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1000         if (err != 0)
1001                 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1002 }
1003
1004 static void
1005 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1006                     uint32_t index, uint32_t vmdq __rte_unused)
1007 {
1008         struct virtio_hw *hw = dev->data->dev_private;
1009         const struct ether_addr *addrs = dev->data->mac_addrs;
1010         unsigned int i;
1011         struct virtio_net_ctrl_mac *uc, *mc;
1012
1013         if (index >= VIRTIO_MAX_MAC_ADDRS) {
1014                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1015                 return;
1016         }
1017
1018         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1019         uc->entries = 0;
1020         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1021         mc->entries = 0;
1022
1023         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1024                 const struct ether_addr *addr
1025                         = (i == index) ? mac_addr : addrs + i;
1026                 struct virtio_net_ctrl_mac *tbl
1027                         = is_multicast_ether_addr(addr) ? mc : uc;
1028
1029                 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1030         }
1031
1032         virtio_mac_table_set(hw, uc, mc);
1033 }
1034
1035 static void
1036 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1037 {
1038         struct virtio_hw *hw = dev->data->dev_private;
1039         struct ether_addr *addrs = dev->data->mac_addrs;
1040         struct virtio_net_ctrl_mac *uc, *mc;
1041         unsigned int i;
1042
1043         if (index >= VIRTIO_MAX_MAC_ADDRS) {
1044                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1045                 return;
1046         }
1047
1048         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1049         uc->entries = 0;
1050         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1051         mc->entries = 0;
1052
1053         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1054                 struct virtio_net_ctrl_mac *tbl;
1055
1056                 if (i == index || is_zero_ether_addr(addrs + i))
1057                         continue;
1058
1059                 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1060                 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1061         }
1062
1063         virtio_mac_table_set(hw, uc, mc);
1064 }
1065
1066 static void
1067 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1068 {
1069         struct virtio_hw *hw = dev->data->dev_private;
1070
1071         memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1072
1073         /* Use atomic update if available */
1074         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1075                 struct virtio_pmd_ctrl ctrl;
1076                 int len = ETHER_ADDR_LEN;
1077
1078                 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1079                 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1080
1081                 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1082                 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1083         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1084                 virtio_set_hwaddr(hw);
1085 }
1086
1087 static int
1088 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1089 {
1090         struct virtio_hw *hw = dev->data->dev_private;
1091         struct virtio_pmd_ctrl ctrl;
1092         int len;
1093
1094         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1095                 return -ENOTSUP;
1096
1097         ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1098         ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1099         memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1100         len = sizeof(vlan_id);
1101
1102         return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1103 }
1104
1105 static int
1106 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1107 {
1108         uint64_t host_features;
1109
1110         /* Prepare guest_features: feature that driver wants to support */
1111         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1112                 req_features);
1113
1114         /* Read device(host) feature bits */
1115         host_features = VTPCI_OPS(hw)->get_features(hw);
1116         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1117                 host_features);
1118
1119         /*
1120          * Negotiate features: Subset of device feature bits are written back
1121          * guest feature bits.
1122          */
1123         hw->guest_features = req_features;
1124         hw->guest_features = vtpci_negotiate_features(hw, host_features);
1125         PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1126                 hw->guest_features);
1127
1128         if (hw->modern) {
1129                 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1130                         PMD_INIT_LOG(ERR,
1131                                 "VIRTIO_F_VERSION_1 features is not enabled.");
1132                         return -1;
1133                 }
1134                 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1135                 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1136                         PMD_INIT_LOG(ERR,
1137                                 "failed to set FEATURES_OK status!");
1138                         return -1;
1139                 }
1140         }
1141
1142         hw->req_guest_features = req_features;
1143
1144         return 0;
1145 }
1146
1147 /*
1148  * Process Virtio Config changed interrupt and call the callback
1149  * if link state changed.
1150  */
1151 static void
1152 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1153                          void *param)
1154 {
1155         struct rte_eth_dev *dev = param;
1156         struct virtio_hw *hw = dev->data->dev_private;
1157         uint8_t isr;
1158
1159         /* Read interrupt status which clears interrupt */
1160         isr = vtpci_isr(hw);
1161         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1162
1163         if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1164                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1165
1166         if (isr & VIRTIO_PCI_ISR_CONFIG) {
1167                 if (virtio_dev_link_update(dev, 0) == 0)
1168                         _rte_eth_dev_callback_process(dev,
1169                                                       RTE_ETH_EVENT_INTR_LSC, NULL);
1170         }
1171
1172 }
1173
1174 static void
1175 rx_func_get(struct rte_eth_dev *eth_dev)
1176 {
1177         struct virtio_hw *hw = eth_dev->data->dev_private;
1178         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1179                 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1180         else
1181                 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1182 }
1183
1184 /* reset device and renegotiate features if needed */
1185 static int
1186 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1187 {
1188         struct virtio_hw *hw = eth_dev->data->dev_private;
1189         struct virtio_net_config *config;
1190         struct virtio_net_config local_config;
1191         struct rte_pci_device *pci_dev = eth_dev->pci_dev;
1192         int ret;
1193
1194         /* Reset the device although not necessary at startup */
1195         vtpci_reset(hw);
1196
1197         if (hw->vqs) {
1198                 virtio_dev_free_mbufs(eth_dev);
1199                 virtio_free_queues(hw);
1200         }
1201
1202         /* Tell the host we've noticed this device. */
1203         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1204
1205         /* Tell the host we've known how to drive the device. */
1206         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1207         if (virtio_negotiate_features(hw, req_features) < 0)
1208                 return -1;
1209
1210         rte_eth_copy_pci_info(eth_dev, pci_dev);
1211
1212         /* If host does not support both status and MSI-X then disable LSC */
1213         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->use_msix)
1214                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1215         else
1216                 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1217
1218         rx_func_get(eth_dev);
1219
1220         /* Setting up rx_header size for the device */
1221         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1222             vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1223                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1224         else
1225                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1226
1227         /* Copy the permanent MAC address to: virtio_hw */
1228         virtio_get_hwaddr(hw);
1229         ether_addr_copy((struct ether_addr *) hw->mac_addr,
1230                         &eth_dev->data->mac_addrs[0]);
1231         PMD_INIT_LOG(DEBUG,
1232                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1233                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1234                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1235
1236         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1237                 config = &local_config;
1238
1239                 vtpci_read_dev_config(hw,
1240                         offsetof(struct virtio_net_config, mac),
1241                         &config->mac, sizeof(config->mac));
1242
1243                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1244                         vtpci_read_dev_config(hw,
1245                                 offsetof(struct virtio_net_config, status),
1246                                 &config->status, sizeof(config->status));
1247                 } else {
1248                         PMD_INIT_LOG(DEBUG,
1249                                      "VIRTIO_NET_F_STATUS is not supported");
1250                         config->status = 0;
1251                 }
1252
1253                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1254                         vtpci_read_dev_config(hw,
1255                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1256                                 &config->max_virtqueue_pairs,
1257                                 sizeof(config->max_virtqueue_pairs));
1258                 } else {
1259                         PMD_INIT_LOG(DEBUG,
1260                                      "VIRTIO_NET_F_MQ is not supported");
1261                         config->max_virtqueue_pairs = 1;
1262                 }
1263
1264                 hw->max_queue_pairs = config->max_virtqueue_pairs;
1265
1266                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1267                                 config->max_virtqueue_pairs);
1268                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1269                 PMD_INIT_LOG(DEBUG,
1270                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1271                                 config->mac[0], config->mac[1],
1272                                 config->mac[2], config->mac[3],
1273                                 config->mac[4], config->mac[5]);
1274         } else {
1275                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1276                 hw->max_queue_pairs = 1;
1277         }
1278
1279         ret = virtio_alloc_queues(eth_dev);
1280         if (ret < 0)
1281                 return ret;
1282         vtpci_reinit_complete(hw);
1283
1284         if (pci_dev)
1285                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1286                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1287                         pci_dev->id.device_id);
1288
1289         return 0;
1290 }
1291
1292 /*
1293  * Remap the PCI device again (IO port map for legacy device and
1294  * memory map for modern device), so that the secondary process
1295  * could have the PCI initiated correctly.
1296  */
1297 static int
1298 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1299 {
1300         if (hw->modern) {
1301                 /*
1302                  * We don't have to re-parse the PCI config space, since
1303                  * rte_eal_pci_map_device() makes sure the mapped address
1304                  * in secondary process would equal to the one mapped in
1305                  * the primary process: error will be returned if that
1306                  * requirement is not met.
1307                  *
1308                  * That said, we could simply reuse all cap pointers
1309                  * (such as dev_cfg, common_cfg, etc.) parsed from the
1310                  * primary process, which is stored in shared memory.
1311                  */
1312                 if (rte_eal_pci_map_device(pci_dev)) {
1313                         PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1314                         return -1;
1315                 }
1316         } else {
1317                 if (rte_eal_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1318                         return -1;
1319         }
1320
1321         return 0;
1322 }
1323
1324 static void
1325 virtio_set_vtpci_ops(struct virtio_hw *hw)
1326 {
1327 #ifdef RTE_VIRTIO_USER
1328         if (hw->virtio_user_dev)
1329                 VTPCI_OPS(hw) = &virtio_user_ops;
1330         else
1331 #endif
1332         if (hw->modern)
1333                 VTPCI_OPS(hw) = &modern_ops;
1334         else
1335                 VTPCI_OPS(hw) = &legacy_ops;
1336 }
1337
1338 /*
1339  * This function is based on probe() function in virtio_pci.c
1340  * It returns 0 on success.
1341  */
1342 int
1343 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1344 {
1345         struct virtio_hw *hw = eth_dev->data->dev_private;
1346         struct rte_pci_device *pci_dev;
1347         uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1348         int ret;
1349
1350         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1351
1352         eth_dev->dev_ops = &virtio_eth_dev_ops;
1353         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1354
1355         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1356                 if (!hw->virtio_user_dev) {
1357                         ret = virtio_remap_pci(eth_dev->pci_dev, hw);
1358                         if (ret)
1359                                 return ret;
1360                 }
1361
1362                 virtio_set_vtpci_ops(hw);
1363                 if (hw->use_simple_rxtx) {
1364                         eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1365                         eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1366                 } else {
1367                         rx_func_get(eth_dev);
1368                 }
1369                 return 0;
1370         }
1371
1372         /* Allocate memory for storing MAC addresses */
1373         eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1374         if (eth_dev->data->mac_addrs == NULL) {
1375                 PMD_INIT_LOG(ERR,
1376                         "Failed to allocate %d bytes needed to store MAC addresses",
1377                         VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1378                 return -ENOMEM;
1379         }
1380
1381         pci_dev = eth_dev->pci_dev;
1382         hw->port_id = eth_dev->data->port_id;
1383
1384         if (pci_dev) {
1385                 ret = vtpci_init(pci_dev, hw, &dev_flags);
1386                 if (ret)
1387                         return ret;
1388         }
1389
1390         eth_dev->data->dev_flags = dev_flags;
1391
1392         /* reset device and negotiate default features */
1393         ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1394         if (ret < 0)
1395                 return ret;
1396
1397         return 0;
1398 }
1399
1400 static int
1401 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1402 {
1403         struct rte_pci_device *pci_dev;
1404
1405         PMD_INIT_FUNC_TRACE();
1406
1407         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1408                 return -EPERM;
1409
1410         virtio_dev_stop(eth_dev);
1411         virtio_dev_close(eth_dev);
1412         pci_dev = eth_dev->pci_dev;
1413
1414         eth_dev->dev_ops = NULL;
1415         eth_dev->tx_pkt_burst = NULL;
1416         eth_dev->rx_pkt_burst = NULL;
1417
1418         rte_free(eth_dev->data->mac_addrs);
1419         eth_dev->data->mac_addrs = NULL;
1420
1421         rte_eal_pci_unmap_device(pci_dev);
1422
1423         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1424
1425         return 0;
1426 }
1427
1428 static struct eth_driver rte_virtio_pmd = {
1429         .pci_drv = {
1430                 .driver = {
1431                         .name = "net_virtio",
1432                 },
1433                 .id_table = pci_id_virtio_map,
1434                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1435                 .probe = rte_eth_dev_pci_probe,
1436                 .remove = rte_eth_dev_pci_remove,
1437         },
1438         .eth_dev_init = eth_virtio_dev_init,
1439         .eth_dev_uninit = eth_virtio_dev_uninit,
1440         .dev_private_size = sizeof(struct virtio_hw),
1441 };
1442
1443 RTE_INIT(rte_virtio_pmd_init);
1444 static void
1445 rte_virtio_pmd_init(void)
1446 {
1447         if (rte_eal_iopl_init() != 0) {
1448                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1449                 return;
1450         }
1451
1452         rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
1453 }
1454
1455 /*
1456  * Configure virtio device
1457  * It returns 0 on success.
1458  */
1459 static int
1460 virtio_dev_configure(struct rte_eth_dev *dev)
1461 {
1462         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1463         struct virtio_hw *hw = dev->data->dev_private;
1464
1465         PMD_INIT_LOG(DEBUG, "configure");
1466
1467         /* Virtio does L4 checksum but not L3! */
1468         if (rxmode->hw_ip_checksum) {
1469                 PMD_DRV_LOG(NOTICE,
1470                             "virtio does not support IP checksum");
1471                 return -ENOTSUP;
1472         }
1473
1474         if (rxmode->enable_lro) {
1475                 PMD_DRV_LOG(NOTICE,
1476                             "virtio does not support Large Receive Offload");
1477                 return -ENOTSUP;
1478         }
1479
1480         /* start control queue */
1481         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
1482                 virtio_dev_cq_start(dev);
1483
1484         hw->vlan_strip = rxmode->hw_vlan_strip;
1485
1486         if (rxmode->hw_vlan_filter
1487             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1488                 PMD_DRV_LOG(NOTICE,
1489                             "vlan filtering not available on this host");
1490                 return -ENOTSUP;
1491         }
1492
1493         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1494                 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1495                         PMD_DRV_LOG(ERR, "failed to set config vector");
1496                         return -EBUSY;
1497                 }
1498
1499         return 0;
1500 }
1501
1502
1503 static int
1504 virtio_dev_start(struct rte_eth_dev *dev)
1505 {
1506         uint16_t nb_queues, i;
1507         struct virtnet_rx *rxvq;
1508         struct virtnet_tx *txvq __rte_unused;
1509         struct virtio_hw *hw = dev->data->dev_private;
1510         int ret;
1511
1512         /* Finish the initialization of the queues */
1513         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1514                 ret = virtio_dev_rx_queue_setup_finish(dev, i);
1515                 if (ret < 0)
1516                         return ret;
1517         }
1518         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1519                 ret = virtio_dev_tx_queue_setup_finish(dev, i);
1520                 if (ret < 0)
1521                         return ret;
1522         }
1523
1524         /* check if lsc interrupt feature is enabled */
1525         if (dev->data->dev_conf.intr_conf.lsc) {
1526                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1527                         PMD_DRV_LOG(ERR, "link status not supported by host");
1528                         return -ENOTSUP;
1529                 }
1530
1531                 /* Setup interrupt callback  */
1532                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1533                         rte_intr_callback_register(&dev->pci_dev->intr_handle,
1534                                                    virtio_interrupt_handler,
1535                                                    dev);
1536
1537                 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1538                         PMD_DRV_LOG(ERR, "interrupt enable failed");
1539                         return -EIO;
1540                 }
1541         }
1542
1543         /*Notify the backend
1544          *Otherwise the tap backend might already stop its queue due to fullness.
1545          *vhost backend will have no chance to be waked up
1546          */
1547         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1548         if (hw->max_queue_pairs > 1) {
1549                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1550                         return -EINVAL;
1551         }
1552
1553         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1554
1555         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1556                 rxvq = dev->data->rx_queues[i];
1557                 /* Flush the old packets */
1558                 virtqueue_rxvq_flush(rxvq->vq);
1559                 virtqueue_notify(rxvq->vq);
1560         }
1561
1562         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1563
1564         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1565                 rxvq = dev->data->rx_queues[i];
1566                 VIRTQUEUE_DUMP(rxvq->vq);
1567         }
1568
1569         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1570                 txvq = dev->data->tx_queues[i];
1571                 VIRTQUEUE_DUMP(txvq->vq);
1572         }
1573
1574         hw->started = 1;
1575
1576         /* Initialize Link state */
1577         virtio_dev_link_update(dev, 0);
1578
1579         return 0;
1580 }
1581
1582 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1583 {
1584         struct rte_mbuf *buf;
1585         int i, mbuf_num = 0;
1586
1587         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1588                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1589
1590                 if (rxvq == NULL || rxvq->vq == NULL)
1591                         continue;
1592
1593                 PMD_INIT_LOG(DEBUG,
1594                              "Before freeing rxq[%d] used and unused buf", i);
1595                 VIRTQUEUE_DUMP(rxvq->vq);
1596
1597                 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1598                 while ((buf = virtqueue_detach_unused(rxvq->vq)) != NULL) {
1599                         rte_pktmbuf_free(buf);
1600                         mbuf_num++;
1601                 }
1602
1603                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1604                 PMD_INIT_LOG(DEBUG,
1605                              "After freeing rxq[%d] used and unused buf", i);
1606                 VIRTQUEUE_DUMP(rxvq->vq);
1607         }
1608
1609         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1610                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1611
1612                 if (txvq == NULL || txvq->vq == NULL)
1613                         continue;
1614
1615                 PMD_INIT_LOG(DEBUG,
1616                              "Before freeing txq[%d] used and unused bufs",
1617                              i);
1618                 VIRTQUEUE_DUMP(txvq->vq);
1619
1620                 mbuf_num = 0;
1621                 while ((buf = virtqueue_detach_unused(txvq->vq)) != NULL) {
1622                         rte_pktmbuf_free(buf);
1623                         mbuf_num++;
1624                 }
1625
1626                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1627                 PMD_INIT_LOG(DEBUG,
1628                              "After freeing txq[%d] used and unused buf", i);
1629                 VIRTQUEUE_DUMP(txvq->vq);
1630         }
1631 }
1632
1633 /*
1634  * Stop device: disable interrupt and mark link down
1635  */
1636 static void
1637 virtio_dev_stop(struct rte_eth_dev *dev)
1638 {
1639         struct virtio_hw *hw = dev->data->dev_private;
1640         struct rte_eth_link link;
1641
1642         PMD_INIT_LOG(DEBUG, "stop");
1643
1644         if (dev->data->dev_conf.intr_conf.lsc) {
1645                 rte_intr_disable(&dev->pci_dev->intr_handle);
1646
1647                 /* Reset interrupt callback  */
1648                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1649                         rte_intr_callback_unregister(&dev->pci_dev->intr_handle,
1650                                                      virtio_interrupt_handler,
1651                                                      dev);
1652                 }
1653         }
1654
1655         hw->started = 0;
1656         memset(&link, 0, sizeof(link));
1657         virtio_dev_atomic_write_link_status(dev, &link);
1658 }
1659
1660 static int
1661 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1662 {
1663         struct rte_eth_link link, old;
1664         uint16_t status;
1665         struct virtio_hw *hw = dev->data->dev_private;
1666         memset(&link, 0, sizeof(link));
1667         virtio_dev_atomic_read_link_status(dev, &link);
1668         old = link;
1669         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1670         link.link_speed  = SPEED_10G;
1671
1672         if (hw->started == 0) {
1673                 link.link_status = ETH_LINK_DOWN;
1674         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1675                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1676                 vtpci_read_dev_config(hw,
1677                                 offsetof(struct virtio_net_config, status),
1678                                 &status, sizeof(status));
1679                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1680                         link.link_status = ETH_LINK_DOWN;
1681                         PMD_INIT_LOG(DEBUG, "Port %d is down",
1682                                      dev->data->port_id);
1683                 } else {
1684                         link.link_status = ETH_LINK_UP;
1685                         PMD_INIT_LOG(DEBUG, "Port %d is up",
1686                                      dev->data->port_id);
1687                 }
1688         } else {
1689                 link.link_status = ETH_LINK_UP;
1690         }
1691         virtio_dev_atomic_write_link_status(dev, &link);
1692
1693         return (old.link_status == link.link_status) ? -1 : 0;
1694 }
1695
1696 static void
1697 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1698 {
1699         uint64_t tso_mask;
1700         struct virtio_hw *hw = dev->data->dev_private;
1701
1702         if (dev->pci_dev)
1703                 dev_info->driver_name = dev->driver->pci_drv.driver.name;
1704         else
1705                 dev_info->driver_name = "virtio_user PMD";
1706         dev_info->max_rx_queues =
1707                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
1708         dev_info->max_tx_queues =
1709                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
1710         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1711         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1712         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1713         dev_info->default_txconf = (struct rte_eth_txconf) {
1714                 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1715         };
1716         dev_info->rx_offload_capa =
1717                 DEV_RX_OFFLOAD_TCP_CKSUM |
1718                 DEV_RX_OFFLOAD_UDP_CKSUM;
1719
1720         dev_info->tx_offload_capa = 0;
1721
1722         if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
1723                 dev_info->tx_offload_capa |=
1724                         DEV_TX_OFFLOAD_UDP_CKSUM |
1725                         DEV_TX_OFFLOAD_TCP_CKSUM;
1726         }
1727
1728         tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1729                 (1ULL << VIRTIO_NET_F_HOST_TSO6);
1730         if ((hw->guest_features & tso_mask) == tso_mask)
1731                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
1732 }
1733
1734 /*
1735  * It enables testpmd to collect per queue stats.
1736  */
1737 static int
1738 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1739 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1740 __rte_unused uint8_t is_rx)
1741 {
1742         return 0;
1743 }
1744
1745 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
1746 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);