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