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