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