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