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