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