37766cbb6483351cfbea29cb069eba6f0e17212d
[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 #define SPEED_UNKNOWN    0xffffffff
1666 #define DUPLEX_UNKNOWN   0xff
1667 /* reset device and renegotiate features if needed */
1668 static int
1669 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1670 {
1671         struct virtio_hw *hw = eth_dev->data->dev_private;
1672         struct virtio_net_config *config;
1673         struct virtio_net_config local_config;
1674         struct rte_pci_device *pci_dev = NULL;
1675         int ret;
1676
1677         /* Reset the device although not necessary at startup */
1678         vtpci_reset(hw);
1679
1680         if (hw->vqs) {
1681                 virtio_dev_free_mbufs(eth_dev);
1682                 virtio_free_queues(hw);
1683         }
1684
1685         /* Tell the host we've noticed this device. */
1686         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1687
1688         /* Tell the host we've known how to drive the device. */
1689         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1690         if (virtio_negotiate_features(hw, req_features) < 0)
1691                 return -1;
1692
1693         hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1694
1695         if (!hw->virtio_user_dev)
1696                 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1697
1698         /* If host does not support both status and MSI-X then disable LSC */
1699         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1700             hw->use_msix != VIRTIO_MSIX_NONE)
1701                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1702         else
1703                 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1704
1705         /* Setting up rx_header size for the device */
1706         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1707             vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1708             vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1709                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1710         else
1711                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1712
1713         /* Copy the permanent MAC address to: virtio_hw */
1714         virtio_get_hwaddr(hw);
1715         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
1716                         &eth_dev->data->mac_addrs[0]);
1717         PMD_INIT_LOG(DEBUG,
1718                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1719                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1720                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1721
1722         if (hw->speed == SPEED_UNKNOWN) {
1723                 if (vtpci_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX)) {
1724                         config = &local_config;
1725                         vtpci_read_dev_config(hw,
1726                                 offsetof(struct virtio_net_config, speed),
1727                                 &config->speed, sizeof(config->speed));
1728                         vtpci_read_dev_config(hw,
1729                                 offsetof(struct virtio_net_config, duplex),
1730                                 &config->duplex, sizeof(config->duplex));
1731                         hw->speed = config->speed;
1732                         hw->duplex = config->duplex;
1733                 }
1734         }
1735         if (hw->speed == SPEED_UNKNOWN)
1736                 hw->speed = ETH_SPEED_NUM_10G;
1737         if (hw->duplex == DUPLEX_UNKNOWN)
1738                 hw->duplex = ETH_LINK_FULL_DUPLEX;
1739         PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
1740                 hw->speed, hw->duplex);
1741         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1742                 config = &local_config;
1743
1744                 vtpci_read_dev_config(hw,
1745                         offsetof(struct virtio_net_config, mac),
1746                         &config->mac, sizeof(config->mac));
1747
1748                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1749                         vtpci_read_dev_config(hw,
1750                                 offsetof(struct virtio_net_config, status),
1751                                 &config->status, sizeof(config->status));
1752                 } else {
1753                         PMD_INIT_LOG(DEBUG,
1754                                      "VIRTIO_NET_F_STATUS is not supported");
1755                         config->status = 0;
1756                 }
1757
1758                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1759                         vtpci_read_dev_config(hw,
1760                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1761                                 &config->max_virtqueue_pairs,
1762                                 sizeof(config->max_virtqueue_pairs));
1763                 } else {
1764                         PMD_INIT_LOG(DEBUG,
1765                                      "VIRTIO_NET_F_MQ is not supported");
1766                         config->max_virtqueue_pairs = 1;
1767                 }
1768
1769                 hw->max_queue_pairs = config->max_virtqueue_pairs;
1770
1771                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1772                         vtpci_read_dev_config(hw,
1773                                 offsetof(struct virtio_net_config, mtu),
1774                                 &config->mtu,
1775                                 sizeof(config->mtu));
1776
1777                         /*
1778                          * MTU value has already been checked at negotiation
1779                          * time, but check again in case it has changed since
1780                          * then, which should not happen.
1781                          */
1782                         if (config->mtu < RTE_ETHER_MIN_MTU) {
1783                                 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1784                                                 config->mtu);
1785                                 return -1;
1786                         }
1787
1788                         hw->max_mtu = config->mtu;
1789                         /* Set initial MTU to maximum one supported by vhost */
1790                         eth_dev->data->mtu = config->mtu;
1791
1792                 } else {
1793                         hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1794                                 VLAN_TAG_LEN - hw->vtnet_hdr_size;
1795                 }
1796
1797                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1798                                 config->max_virtqueue_pairs);
1799                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1800                 PMD_INIT_LOG(DEBUG,
1801                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1802                                 config->mac[0], config->mac[1],
1803                                 config->mac[2], config->mac[3],
1804                                 config->mac[4], config->mac[5]);
1805         } else {
1806                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1807                 hw->max_queue_pairs = 1;
1808                 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
1809                         VLAN_TAG_LEN - hw->vtnet_hdr_size;
1810         }
1811
1812         ret = virtio_alloc_queues(eth_dev);
1813         if (ret < 0)
1814                 return ret;
1815
1816         if (eth_dev->data->dev_conf.intr_conf.rxq) {
1817                 if (virtio_configure_intr(eth_dev) < 0) {
1818                         PMD_INIT_LOG(ERR, "failed to configure interrupt");
1819                         virtio_free_queues(hw);
1820                         return -1;
1821                 }
1822         }
1823
1824         vtpci_reinit_complete(hw);
1825
1826         if (pci_dev)
1827                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1828                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1829                         pci_dev->id.device_id);
1830
1831         return 0;
1832 }
1833
1834 /*
1835  * Remap the PCI device again (IO port map for legacy device and
1836  * memory map for modern device), so that the secondary process
1837  * could have the PCI initiated correctly.
1838  */
1839 static int
1840 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1841 {
1842         if (hw->modern) {
1843                 /*
1844                  * We don't have to re-parse the PCI config space, since
1845                  * rte_pci_map_device() makes sure the mapped address
1846                  * in secondary process would equal to the one mapped in
1847                  * the primary process: error will be returned if that
1848                  * requirement is not met.
1849                  *
1850                  * That said, we could simply reuse all cap pointers
1851                  * (such as dev_cfg, common_cfg, etc.) parsed from the
1852                  * primary process, which is stored in shared memory.
1853                  */
1854                 if (rte_pci_map_device(pci_dev)) {
1855                         PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1856                         return -1;
1857                 }
1858         } else {
1859                 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1860                         return -1;
1861         }
1862
1863         return 0;
1864 }
1865
1866 static void
1867 virtio_set_vtpci_ops(struct virtio_hw *hw)
1868 {
1869 #ifdef RTE_VIRTIO_USER
1870         if (hw->virtio_user_dev)
1871                 VTPCI_OPS(hw) = &virtio_user_ops;
1872         else
1873 #endif
1874         if (hw->modern)
1875                 VTPCI_OPS(hw) = &modern_ops;
1876         else
1877                 VTPCI_OPS(hw) = &legacy_ops;
1878 }
1879
1880 /*
1881  * This function is based on probe() function in virtio_pci.c
1882  * It returns 0 on success.
1883  */
1884 int
1885 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1886 {
1887         struct virtio_hw *hw = eth_dev->data->dev_private;
1888         uint32_t speed = SPEED_UNKNOWN;
1889         int ret;
1890
1891         if (sizeof(struct virtio_net_hdr_mrg_rxbuf) > RTE_PKTMBUF_HEADROOM) {
1892                 PMD_INIT_LOG(ERR,
1893                         "Not sufficient headroom required = %d, avail = %d",
1894                         (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
1895                         RTE_PKTMBUF_HEADROOM);
1896
1897                 return -1;
1898         }
1899
1900         eth_dev->dev_ops = &virtio_eth_dev_ops;
1901
1902         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1903                 if (!hw->virtio_user_dev) {
1904                         ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1905                         if (ret)
1906                                 return ret;
1907                 }
1908
1909                 virtio_set_vtpci_ops(hw);
1910                 set_rxtx_funcs(eth_dev);
1911
1912                 return 0;
1913         }
1914         ret = virtio_dev_devargs_parse(eth_dev->device->devargs,
1915                  NULL, &speed);
1916         if (ret < 0)
1917                 return ret;
1918         hw->speed = speed;
1919         /*
1920          * Pass the information to the rte_eth_dev_close() that it should also
1921          * release the private port resources.
1922          */
1923         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1924
1925         /* Allocate memory for storing MAC addresses */
1926         eth_dev->data->mac_addrs = rte_zmalloc("virtio",
1927                                 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
1928         if (eth_dev->data->mac_addrs == NULL) {
1929                 PMD_INIT_LOG(ERR,
1930                         "Failed to allocate %d bytes needed to store MAC addresses",
1931                         VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
1932                 return -ENOMEM;
1933         }
1934
1935         hw->port_id = eth_dev->data->port_id;
1936         /* For virtio_user case the hw->virtio_user_dev is populated by
1937          * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1938          */
1939         if (!hw->virtio_user_dev) {
1940                 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1941                 if (ret)
1942                         goto err_vtpci_init;
1943         }
1944
1945         rte_spinlock_init(&hw->state_lock);
1946
1947         /* reset device and negotiate default features */
1948         ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1949         if (ret < 0)
1950                 goto err_virtio_init;
1951
1952         hw->opened = true;
1953
1954         return 0;
1955
1956 err_virtio_init:
1957         if (!hw->virtio_user_dev) {
1958                 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1959                 if (!hw->modern)
1960                         rte_pci_ioport_unmap(VTPCI_IO(hw));
1961         }
1962 err_vtpci_init:
1963         rte_free(eth_dev->data->mac_addrs);
1964         eth_dev->data->mac_addrs = NULL;
1965         return ret;
1966 }
1967
1968 static int
1969 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1970 {
1971         PMD_INIT_FUNC_TRACE();
1972
1973         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1974                 return 0;
1975
1976         virtio_dev_stop(eth_dev);
1977         virtio_dev_close(eth_dev);
1978
1979         eth_dev->dev_ops = NULL;
1980         eth_dev->tx_pkt_burst = NULL;
1981         eth_dev->rx_pkt_burst = NULL;
1982
1983         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1984
1985         return 0;
1986 }
1987
1988
1989 static int vdpa_check_handler(__rte_unused const char *key,
1990                 const char *value, void *ret_val)
1991 {
1992         if (strcmp(value, "1") == 0)
1993                 *(int *)ret_val = 1;
1994         else
1995                 *(int *)ret_val = 0;
1996
1997         return 0;
1998 }
1999
2000
2001 static uint32_t
2002 virtio_dev_speed_capa_get(uint32_t speed)
2003 {
2004         switch (speed) {
2005         case ETH_SPEED_NUM_10G:
2006                 return ETH_LINK_SPEED_10G;
2007         case ETH_SPEED_NUM_20G:
2008                 return ETH_LINK_SPEED_20G;
2009         case ETH_SPEED_NUM_25G:
2010                 return ETH_LINK_SPEED_25G;
2011         case ETH_SPEED_NUM_40G:
2012                 return ETH_LINK_SPEED_40G;
2013         case ETH_SPEED_NUM_50G:
2014                 return ETH_LINK_SPEED_50G;
2015         case ETH_SPEED_NUM_56G:
2016                 return ETH_LINK_SPEED_56G;
2017         case ETH_SPEED_NUM_100G:
2018                 return ETH_LINK_SPEED_100G;
2019         default:
2020                 return 0;
2021         }
2022 }
2023
2024
2025 #define VIRTIO_ARG_SPEED      "speed"
2026 #define VIRTIO_ARG_VDPA       "vdpa"
2027
2028
2029 static int
2030 link_speed_handler(const char *key __rte_unused,
2031                 const char *value, void *ret_val)
2032 {
2033         uint32_t val;
2034         if (!value || !ret_val)
2035                 return -EINVAL;
2036         val = strtoul(value, NULL, 0);
2037         /* validate input */
2038         if (virtio_dev_speed_capa_get(val) == 0)
2039                 return -EINVAL;
2040         *(uint32_t *)ret_val = val;
2041
2042         return 0;
2043 }
2044
2045
2046 static int
2047 virtio_dev_devargs_parse(struct rte_devargs *devargs, int *vdpa,
2048         uint32_t *speed)
2049 {
2050         struct rte_kvargs *kvlist;
2051         int ret = 0;
2052
2053         if (devargs == NULL)
2054                 return 0;
2055
2056         kvlist = rte_kvargs_parse(devargs->args, NULL);
2057         if (kvlist == NULL) {
2058                 PMD_INIT_LOG(ERR, "error when parsing param");
2059                 return 0;
2060         }
2061         if (vdpa && rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) {
2062                 /* vdpa mode selected when there's a key-value pair:
2063                  * vdpa=1
2064                  */
2065                 ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA,
2066                                 vdpa_check_handler, vdpa);
2067                 if (ret < 0) {
2068                         PMD_INIT_LOG(ERR, "Failed to parse %s",
2069                                 VIRTIO_ARG_VDPA);
2070                         goto exit;
2071                 }
2072         }
2073         if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
2074                 ret = rte_kvargs_process(kvlist,
2075                                         VIRTIO_ARG_SPEED,
2076                                         link_speed_handler, speed);
2077                 if (ret < 0) {
2078                         PMD_INIT_LOG(ERR, "Failed to parse %s",
2079                                         VIRTIO_ARG_SPEED);
2080                         goto exit;
2081                 }
2082         }
2083
2084 exit:
2085         rte_kvargs_free(kvlist);
2086         return ret;
2087 }
2088
2089 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2090         struct rte_pci_device *pci_dev)
2091 {
2092         int vdpa = 0;
2093         int ret = 0;
2094
2095         ret = virtio_dev_devargs_parse(pci_dev->device.devargs, &vdpa, NULL);
2096         if (ret < 0) {
2097                 PMD_INIT_LOG(ERR, "devargs parsing is failed");
2098                 return ret;
2099         }
2100         /* virtio pmd skips probe if device needs to work in vdpa mode */
2101         if (vdpa == 1)
2102                 return 1;
2103
2104         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
2105                 eth_virtio_dev_init);
2106 }
2107
2108 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
2109 {
2110         int ret;
2111
2112         ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
2113         /* Port has already been released by close. */
2114         if (ret == -ENODEV)
2115                 ret = 0;
2116         return ret;
2117 }
2118
2119 static struct rte_pci_driver rte_virtio_pmd = {
2120         .driver = {
2121                 .name = "net_virtio",
2122         },
2123         .id_table = pci_id_virtio_map,
2124         .drv_flags = 0,
2125         .probe = eth_virtio_pci_probe,
2126         .remove = eth_virtio_pci_remove,
2127 };
2128
2129 RTE_INIT(rte_virtio_pmd_init)
2130 {
2131         rte_eal_iopl_init();
2132         rte_pci_register(&rte_virtio_pmd);
2133 }
2134
2135 static bool
2136 rx_offload_enabled(struct virtio_hw *hw)
2137 {
2138         return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
2139                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2140                 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
2141 }
2142
2143 static bool
2144 tx_offload_enabled(struct virtio_hw *hw)
2145 {
2146         return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
2147                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
2148                 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
2149 }
2150
2151 /*
2152  * Configure virtio device
2153  * It returns 0 on success.
2154  */
2155 static int
2156 virtio_dev_configure(struct rte_eth_dev *dev)
2157 {
2158         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2159         const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
2160         struct virtio_hw *hw = dev->data->dev_private;
2161         uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
2162                 hw->vtnet_hdr_size;
2163         uint64_t rx_offloads = rxmode->offloads;
2164         uint64_t tx_offloads = txmode->offloads;
2165         uint64_t req_features;
2166         int ret;
2167
2168         PMD_INIT_LOG(DEBUG, "configure");
2169         req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
2170
2171         if (rxmode->mq_mode != ETH_MQ_RX_NONE) {
2172                 PMD_DRV_LOG(ERR,
2173                         "Unsupported Rx multi queue mode %d",
2174                         rxmode->mq_mode);
2175                 return -EINVAL;
2176         }
2177
2178         if (txmode->mq_mode != ETH_MQ_TX_NONE) {
2179                 PMD_DRV_LOG(ERR,
2180                         "Unsupported Tx multi queue mode %d",
2181                         txmode->mq_mode);
2182                 return -EINVAL;
2183         }
2184
2185         if (dev->data->dev_conf.intr_conf.rxq) {
2186                 ret = virtio_init_device(dev, hw->req_guest_features);
2187                 if (ret < 0)
2188                         return ret;
2189         }
2190
2191         if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
2192                 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
2193
2194         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2195                            DEV_RX_OFFLOAD_TCP_CKSUM))
2196                 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2197
2198         if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2199                 req_features |=
2200                         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2201                         (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2202
2203         if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2204                            DEV_TX_OFFLOAD_TCP_CKSUM))
2205                 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2206
2207         if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2208                 req_features |=
2209                         (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2210                         (1ULL << VIRTIO_NET_F_HOST_TSO6);
2211
2212         /* if request features changed, reinit the device */
2213         if (req_features != hw->req_guest_features) {
2214                 ret = virtio_init_device(dev, req_features);
2215                 if (ret < 0)
2216                         return ret;
2217         }
2218
2219         if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2220                             DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2221                 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2222                 PMD_DRV_LOG(ERR,
2223                         "rx checksum not available on this host");
2224                 return -ENOTSUP;
2225         }
2226
2227         if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2228                 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2229                  !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2230                 PMD_DRV_LOG(ERR,
2231                         "Large Receive Offload not available on this host");
2232                 return -ENOTSUP;
2233         }
2234
2235         /* start control queue */
2236         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2237                 virtio_dev_cq_start(dev);
2238
2239         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2240                 hw->vlan_strip = 1;
2241
2242         if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2243             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2244                 PMD_DRV_LOG(ERR,
2245                             "vlan filtering not available on this host");
2246                 return -ENOTSUP;
2247         }
2248
2249         hw->has_tx_offload = tx_offload_enabled(hw);
2250         hw->has_rx_offload = rx_offload_enabled(hw);
2251
2252         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2253                 /* Enable vector (0) for Link State Intrerrupt */
2254                 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2255                                 VIRTIO_MSI_NO_VECTOR) {
2256                         PMD_DRV_LOG(ERR, "failed to set config vector");
2257                         return -EBUSY;
2258                 }
2259
2260         hw->use_simple_rx = 1;
2261
2262         if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2263                 hw->use_inorder_tx = 1;
2264                 hw->use_inorder_rx = 1;
2265                 hw->use_simple_rx = 0;
2266         }
2267
2268         if (vtpci_packed_queue(hw)) {
2269                 hw->use_simple_rx = 0;
2270                 hw->use_inorder_rx = 0;
2271         }
2272
2273 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2274         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2275                 hw->use_simple_rx = 0;
2276         }
2277 #endif
2278         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2279                  hw->use_simple_rx = 0;
2280         }
2281
2282         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2283                            DEV_RX_OFFLOAD_TCP_CKSUM |
2284                            DEV_RX_OFFLOAD_TCP_LRO |
2285                            DEV_RX_OFFLOAD_VLAN_STRIP))
2286                 hw->use_simple_rx = 0;
2287
2288         return 0;
2289 }
2290
2291
2292 static int
2293 virtio_dev_start(struct rte_eth_dev *dev)
2294 {
2295         uint16_t nb_queues, i;
2296         struct virtnet_rx *rxvq;
2297         struct virtnet_tx *txvq __rte_unused;
2298         struct virtio_hw *hw = dev->data->dev_private;
2299         int ret;
2300
2301         /* Finish the initialization of the queues */
2302         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2303                 ret = virtio_dev_rx_queue_setup_finish(dev, i);
2304                 if (ret < 0)
2305                         return ret;
2306         }
2307         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2308                 ret = virtio_dev_tx_queue_setup_finish(dev, i);
2309                 if (ret < 0)
2310                         return ret;
2311         }
2312
2313         /* check if lsc interrupt feature is enabled */
2314         if (dev->data->dev_conf.intr_conf.lsc) {
2315                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2316                         PMD_DRV_LOG(ERR, "link status not supported by host");
2317                         return -ENOTSUP;
2318                 }
2319         }
2320
2321         /* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2322          * in device configure, but it could be unmapped  when device is
2323          * stopped.
2324          */
2325         if (dev->data->dev_conf.intr_conf.lsc ||
2326             dev->data->dev_conf.intr_conf.rxq) {
2327                 virtio_intr_disable(dev);
2328
2329                 /* Setup interrupt callback  */
2330                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2331                         rte_intr_callback_register(dev->intr_handle,
2332                                                    virtio_interrupt_handler,
2333                                                    dev);
2334
2335                 if (virtio_intr_enable(dev) < 0) {
2336                         PMD_DRV_LOG(ERR, "interrupt enable failed");
2337                         return -EIO;
2338                 }
2339         }
2340
2341         /*Notify the backend
2342          *Otherwise the tap backend might already stop its queue due to fullness.
2343          *vhost backend will have no chance to be waked up
2344          */
2345         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2346         if (hw->max_queue_pairs > 1) {
2347                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2348                         return -EINVAL;
2349         }
2350
2351         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2352
2353         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2354                 rxvq = dev->data->rx_queues[i];
2355                 /* Flush the old packets */
2356                 virtqueue_rxvq_flush(rxvq->vq);
2357                 virtqueue_notify(rxvq->vq);
2358         }
2359
2360         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2361                 txvq = dev->data->tx_queues[i];
2362                 virtqueue_notify(txvq->vq);
2363         }
2364
2365         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2366
2367         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2368                 rxvq = dev->data->rx_queues[i];
2369                 VIRTQUEUE_DUMP(rxvq->vq);
2370         }
2371
2372         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2373                 txvq = dev->data->tx_queues[i];
2374                 VIRTQUEUE_DUMP(txvq->vq);
2375         }
2376
2377         set_rxtx_funcs(dev);
2378         hw->started = true;
2379
2380         /* Initialize Link state */
2381         virtio_dev_link_update(dev, 0);
2382
2383         return 0;
2384 }
2385
2386 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2387 {
2388         struct virtio_hw *hw = dev->data->dev_private;
2389         uint16_t nr_vq = virtio_get_nr_vq(hw);
2390         const char *type __rte_unused;
2391         unsigned int i, mbuf_num = 0;
2392         struct virtqueue *vq;
2393         struct rte_mbuf *buf;
2394         int queue_type;
2395
2396         if (hw->vqs == NULL)
2397                 return;
2398
2399         for (i = 0; i < nr_vq; i++) {
2400                 vq = hw->vqs[i];
2401                 if (!vq)
2402                         continue;
2403
2404                 queue_type = virtio_get_queue_type(hw, i);
2405                 if (queue_type == VTNET_RQ)
2406                         type = "rxq";
2407                 else if (queue_type == VTNET_TQ)
2408                         type = "txq";
2409                 else
2410                         continue;
2411
2412                 PMD_INIT_LOG(DEBUG,
2413                         "Before freeing %s[%d] used and unused buf",
2414                         type, i);
2415                 VIRTQUEUE_DUMP(vq);
2416
2417                 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2418                         rte_pktmbuf_free(buf);
2419                         mbuf_num++;
2420                 }
2421
2422                 PMD_INIT_LOG(DEBUG,
2423                         "After freeing %s[%d] used and unused buf",
2424                         type, i);
2425                 VIRTQUEUE_DUMP(vq);
2426         }
2427
2428         PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2429 }
2430
2431 /*
2432  * Stop device: disable interrupt and mark link down
2433  */
2434 static void
2435 virtio_dev_stop(struct rte_eth_dev *dev)
2436 {
2437         struct virtio_hw *hw = dev->data->dev_private;
2438         struct rte_eth_link link;
2439         struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2440
2441         PMD_INIT_LOG(DEBUG, "stop");
2442
2443         rte_spinlock_lock(&hw->state_lock);
2444         if (!hw->started)
2445                 goto out_unlock;
2446         hw->started = false;
2447
2448         if (intr_conf->lsc || intr_conf->rxq) {
2449                 virtio_intr_disable(dev);
2450
2451                 /* Reset interrupt callback  */
2452                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2453                         rte_intr_callback_unregister(dev->intr_handle,
2454                                                      virtio_interrupt_handler,
2455                                                      dev);
2456                 }
2457         }
2458
2459         memset(&link, 0, sizeof(link));
2460         rte_eth_linkstatus_set(dev, &link);
2461 out_unlock:
2462         rte_spinlock_unlock(&hw->state_lock);
2463 }
2464
2465 static int
2466 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2467 {
2468         struct rte_eth_link link;
2469         uint16_t status;
2470         struct virtio_hw *hw = dev->data->dev_private;
2471
2472         memset(&link, 0, sizeof(link));
2473         link.link_duplex = hw->duplex;
2474         link.link_speed  = hw->speed;
2475         link.link_autoneg = ETH_LINK_AUTONEG;
2476
2477         if (!hw->started) {
2478                 link.link_status = ETH_LINK_DOWN;
2479         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2480                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
2481                 vtpci_read_dev_config(hw,
2482                                 offsetof(struct virtio_net_config, status),
2483                                 &status, sizeof(status));
2484                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2485                         link.link_status = ETH_LINK_DOWN;
2486                         PMD_INIT_LOG(DEBUG, "Port %d is down",
2487                                      dev->data->port_id);
2488                 } else {
2489                         link.link_status = ETH_LINK_UP;
2490                         PMD_INIT_LOG(DEBUG, "Port %d is up",
2491                                      dev->data->port_id);
2492                 }
2493         } else {
2494                 link.link_status = ETH_LINK_UP;
2495         }
2496
2497         return rte_eth_linkstatus_set(dev, &link);
2498 }
2499
2500 static int
2501 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2502 {
2503         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2504         struct virtio_hw *hw = dev->data->dev_private;
2505         uint64_t offloads = rxmode->offloads;
2506
2507         if (mask & ETH_VLAN_FILTER_MASK) {
2508                 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2509                                 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2510
2511                         PMD_DRV_LOG(NOTICE,
2512                                 "vlan filtering not available on this host");
2513
2514                         return -ENOTSUP;
2515                 }
2516         }
2517
2518         if (mask & ETH_VLAN_STRIP_MASK)
2519                 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2520
2521         return 0;
2522 }
2523
2524 static int
2525 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2526 {
2527         uint64_t tso_mask, host_features;
2528         struct virtio_hw *hw = dev->data->dev_private;
2529         dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
2530
2531         dev_info->max_rx_queues =
2532                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2533         dev_info->max_tx_queues =
2534                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2535         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2536         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2537         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2538
2539         host_features = VTPCI_OPS(hw)->get_features(hw);
2540         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2541         dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2542         if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2543                 dev_info->rx_offload_capa |=
2544                         DEV_RX_OFFLOAD_TCP_CKSUM |
2545                         DEV_RX_OFFLOAD_UDP_CKSUM;
2546         }
2547         if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2548                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2549         tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2550                 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
2551         if ((host_features & tso_mask) == tso_mask)
2552                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2553
2554         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2555                                     DEV_TX_OFFLOAD_VLAN_INSERT;
2556         if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2557                 dev_info->tx_offload_capa |=
2558                         DEV_TX_OFFLOAD_UDP_CKSUM |
2559                         DEV_TX_OFFLOAD_TCP_CKSUM;
2560         }
2561         tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2562                 (1ULL << VIRTIO_NET_F_HOST_TSO6);
2563         if ((host_features & tso_mask) == tso_mask)
2564                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2565
2566         return 0;
2567 }
2568
2569 /*
2570  * It enables testpmd to collect per queue stats.
2571  */
2572 static int
2573 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2574 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2575 __rte_unused uint8_t is_rx)
2576 {
2577         return 0;
2578 }
2579
2580 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2581 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2582 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2583
2584 RTE_INIT(virtio_init_log)
2585 {
2586         virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2587         if (virtio_logtype_init >= 0)
2588                 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2589         virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2590         if (virtio_logtype_driver >= 0)
2591                 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);
2592 }