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