Imported Upstream version 16.07-rc1
[deb_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_memcpy.h>
42 #include <rte_string_fns.h>
43 #include <rte_memzone.h>
44 #include <rte_malloc.h>
45 #include <rte_atomic.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_pci.h>
48 #include <rte_ether.h>
49 #include <rte_common.h>
50 #include <rte_errno.h>
51
52 #include <rte_memory.h>
53 #include <rte_eal.h>
54 #include <rte_dev.h>
55
56 #include "virtio_ethdev.h"
57 #include "virtio_pci.h"
58 #include "virtio_logs.h"
59 #include "virtqueue.h"
60 #include "virtio_rxtx.h"
61
62 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
63 static int  virtio_dev_configure(struct rte_eth_dev *dev);
64 static int  virtio_dev_start(struct rte_eth_dev *dev);
65 static void virtio_dev_stop(struct rte_eth_dev *dev);
66 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
67 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
68 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
69 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
70 static void virtio_dev_info_get(struct rte_eth_dev *dev,
71                                 struct rte_eth_dev_info *dev_info);
72 static int virtio_dev_link_update(struct rte_eth_dev *dev,
73         __rte_unused int wait_to_complete);
74
75 static void virtio_set_hwaddr(struct virtio_hw *hw);
76 static void virtio_get_hwaddr(struct virtio_hw *hw);
77
78 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
79                                  struct rte_eth_stats *stats);
80 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
81                                  struct rte_eth_xstat *xstats, unsigned n);
82 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
83                                        struct rte_eth_xstat_name *xstats_names,
84                                        unsigned limit);
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
88                                 uint16_t vlan_id, int on);
89 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
90                                 struct ether_addr *mac_addr,
91                                 uint32_t index, uint32_t vmdq __rte_unused);
92 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
93 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94                                 struct ether_addr *mac_addr);
95
96 static int virtio_dev_queue_stats_mapping_set(
97         __rte_unused struct rte_eth_dev *eth_dev,
98         __rte_unused uint16_t queue_id,
99         __rte_unused uint8_t stat_idx,
100         __rte_unused uint8_t is_rx);
101
102 /*
103  * The set of PCI devices this driver supports
104  */
105 static const struct rte_pci_id pci_id_virtio_map[] = {
106
107 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
108 #include "rte_pci_dev_ids.h"
109
110 { .vendor_id = 0, /* sentinel */ },
111 };
112
113 struct rte_virtio_xstats_name_off {
114         char name[RTE_ETH_XSTATS_NAME_SIZE];
115         unsigned offset;
116 };
117
118 /* [rt]x_qX_ is prepended to the name string here */
119 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
120         {"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
121         {"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
122         {"errors",                 offsetof(struct virtnet_rx, stats.errors)},
123         {"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
124         {"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
125         {"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
126         {"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
127         {"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
128         {"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
129         {"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
130         {"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
131         {"size_1024_1517_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
132         {"size_1518_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
133 };
134
135 /* [rt]x_qX_ is prepended to the name string here */
136 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
137         {"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
138         {"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
139         {"errors",                 offsetof(struct virtnet_tx, stats.errors)},
140         {"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
141         {"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
142         {"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
143         {"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
144         {"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
145         {"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
146         {"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
147         {"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
148         {"size_1024_1517_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
149         {"size_1518_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
150 };
151
152 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
153                             sizeof(rte_virtio_rxq_stat_strings[0]))
154 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
155                             sizeof(rte_virtio_txq_stat_strings[0]))
156
157 static int
158 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
159                 int *dlen, int pkt_num)
160 {
161         uint32_t head, i;
162         int k, sum = 0;
163         virtio_net_ctrl_ack status = ~0;
164         struct virtio_pmd_ctrl result;
165         struct virtqueue *vq;
166
167         ctrl->status = status;
168
169         if (!cvq && !cvq->vq) {
170                 PMD_INIT_LOG(ERR, "Control queue is not supported.");
171                 return -1;
172         }
173         vq = cvq->vq;
174         head = vq->vq_desc_head_idx;
175
176         PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
177                 "vq->hw->cvq = %p vq = %p",
178                 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
179
180         if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
181                 return -1;
182
183         memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
184                 sizeof(struct virtio_pmd_ctrl));
185
186         /*
187          * Format is enforced in qemu code:
188          * One TX packet for header;
189          * At least one TX packet per argument;
190          * One RX packet for ACK.
191          */
192         vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
193         vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
194         vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
195         vq->vq_free_cnt--;
196         i = vq->vq_ring.desc[head].next;
197
198         for (k = 0; k < pkt_num; k++) {
199                 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
200                 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
201                         + sizeof(struct virtio_net_ctrl_hdr)
202                         + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
203                 vq->vq_ring.desc[i].len = dlen[k];
204                 sum += dlen[k];
205                 vq->vq_free_cnt--;
206                 i = vq->vq_ring.desc[i].next;
207         }
208
209         vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
210         vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
211                         + sizeof(struct virtio_net_ctrl_hdr);
212         vq->vq_ring.desc[i].len = sizeof(ctrl->status);
213         vq->vq_free_cnt--;
214
215         vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
216
217         vq_update_avail_ring(vq, head);
218         vq_update_avail_idx(vq);
219
220         PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
221
222         virtqueue_notify(vq);
223
224         rte_rmb();
225         while (VIRTQUEUE_NUSED(vq) == 0) {
226                 rte_rmb();
227                 usleep(100);
228         }
229
230         while (VIRTQUEUE_NUSED(vq)) {
231                 uint32_t idx, desc_idx, used_idx;
232                 struct vring_used_elem *uep;
233
234                 used_idx = (uint32_t)(vq->vq_used_cons_idx
235                                 & (vq->vq_nentries - 1));
236                 uep = &vq->vq_ring.used->ring[used_idx];
237                 idx = (uint32_t) uep->id;
238                 desc_idx = idx;
239
240                 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
241                         desc_idx = vq->vq_ring.desc[desc_idx].next;
242                         vq->vq_free_cnt++;
243                 }
244
245                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
246                 vq->vq_desc_head_idx = idx;
247
248                 vq->vq_used_cons_idx++;
249                 vq->vq_free_cnt++;
250         }
251
252         PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
253                         vq->vq_free_cnt, vq->vq_desc_head_idx);
254
255         memcpy(&result, cvq->virtio_net_hdr_mz->addr,
256                         sizeof(struct virtio_pmd_ctrl));
257
258         return result.status;
259 }
260
261 static int
262 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
263 {
264         struct virtio_hw *hw = dev->data->dev_private;
265         struct virtio_pmd_ctrl ctrl;
266         int dlen[1];
267         int ret;
268
269         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
270         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
271         memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
272
273         dlen[0] = sizeof(uint16_t);
274
275         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
276         if (ret) {
277                 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
278                           "failed, this is too late now...");
279                 return -EINVAL;
280         }
281
282         return 0;
283 }
284
285 void
286 virtio_dev_queue_release(struct virtqueue *vq)
287 {
288         struct virtio_hw *hw;
289
290         if (vq) {
291                 hw = vq->hw;
292                 if (vq->configured)
293                         hw->vtpci_ops->del_queue(hw, vq);
294
295                 rte_free(vq->sw_ring);
296                 rte_free(vq);
297         }
298 }
299
300 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
301                         int queue_type,
302                         uint16_t queue_idx,
303                         uint16_t vtpci_queue_idx,
304                         uint16_t nb_desc,
305                         unsigned int socket_id,
306                         void **pvq)
307 {
308         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
309         char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
310         const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
311         unsigned int vq_size, size;
312         struct virtio_hw *hw = dev->data->dev_private;
313         struct virtnet_rx *rxvq = NULL;
314         struct virtnet_tx *txvq = NULL;
315         struct virtnet_ctl *cvq = NULL;
316         struct virtqueue *vq;
317         const char *queue_names[] = {"rvq", "txq", "cvq"};
318         size_t sz_vq, sz_q = 0, sz_hdr_mz = 0;
319         void *sw_ring = NULL;
320         int ret;
321
322         PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
323
324         /*
325          * Read the virtqueue size from the Queue Size field
326          * Always power of 2 and if 0 virtqueue does not exist
327          */
328         vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
329         PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
330         if (vq_size == 0) {
331                 PMD_INIT_LOG(ERR, "virtqueue does not exist");
332                 return -EINVAL;
333         }
334
335         if (!rte_is_power_of_2(vq_size)) {
336                 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
337                 return -EINVAL;
338         }
339
340         snprintf(vq_name, sizeof(vq_name), "port%d_%s%d",
341                  dev->data->port_id, queue_names[queue_type], queue_idx);
342
343         sz_vq = RTE_ALIGN_CEIL(sizeof(*vq) +
344                                 vq_size * sizeof(struct vq_desc_extra),
345                                 RTE_CACHE_LINE_SIZE);
346         if (queue_type == VTNET_RQ) {
347                 sz_q = sz_vq + sizeof(*rxvq);
348         } else if (queue_type == VTNET_TQ) {
349                 sz_q = sz_vq + sizeof(*txvq);
350                 /*
351                  * For each xmit packet, allocate a virtio_net_hdr
352                  * and indirect ring elements
353                  */
354                 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
355         } else if (queue_type == VTNET_CQ) {
356                 sz_q = sz_vq + sizeof(*cvq);
357                 /* Allocate a page for control vq command, data and status */
358                 sz_hdr_mz = PAGE_SIZE;
359         }
360
361         vq = rte_zmalloc_socket(vq_name, sz_q, RTE_CACHE_LINE_SIZE, socket_id);
362         if (vq == NULL) {
363                 PMD_INIT_LOG(ERR, "can not allocate vq");
364                 return -ENOMEM;
365         }
366         vq->hw = hw;
367         vq->vq_queue_index = vtpci_queue_idx;
368         vq->vq_nentries = vq_size;
369
370         if (nb_desc == 0 || nb_desc > vq_size)
371                 nb_desc = vq_size;
372         vq->vq_free_cnt = nb_desc;
373
374         /*
375          * Reserve a memzone for vring elements
376          */
377         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
378         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
379         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
380                      size, vq->vq_ring_size);
381
382         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, socket_id,
383                                          0, VIRTIO_PCI_VRING_ALIGN);
384         if (mz == NULL) {
385                 if (rte_errno == EEXIST)
386                         mz = rte_memzone_lookup(vq_name);
387                 if (mz == NULL) {
388                         ret = -ENOMEM;
389                         goto fail_q_alloc;
390                 }
391         }
392
393         memset(mz->addr, 0, sizeof(mz->len));
394
395         vq->vq_ring_mem = mz->phys_addr;
396         vq->vq_ring_virt_mem = mz->addr;
397         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
398                      (uint64_t)mz->phys_addr);
399         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
400                      (uint64_t)(uintptr_t)mz->addr);
401
402         if (sz_hdr_mz) {
403                 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_%s%d_hdr",
404                          dev->data->port_id, queue_names[queue_type],
405                          queue_idx);
406                 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
407                                                      socket_id, 0,
408                                                      RTE_CACHE_LINE_SIZE);
409                 if (hdr_mz == NULL) {
410                         if (rte_errno == EEXIST)
411                                 hdr_mz = rte_memzone_lookup(vq_hdr_name);
412                         if (hdr_mz == NULL) {
413                                 ret = -ENOMEM;
414                                 goto fail_q_alloc;
415                         }
416                 }
417         }
418
419         if (queue_type == VTNET_RQ) {
420                 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
421                                sizeof(vq->sw_ring[0]);
422
423                 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
424                                              RTE_CACHE_LINE_SIZE, socket_id);
425                 if (!sw_ring) {
426                         PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
427                         ret = -ENOMEM;
428                         goto fail_q_alloc;
429                 }
430
431                 vq->sw_ring = sw_ring;
432                 rxvq = (struct virtnet_rx *)RTE_PTR_ADD(vq, sz_vq);
433                 rxvq->vq = vq;
434                 rxvq->port_id = dev->data->port_id;
435                 rxvq->queue_id = queue_idx;
436                 rxvq->mz = mz;
437                 *pvq = rxvq;
438         } else if (queue_type == VTNET_TQ) {
439                 txvq = (struct virtnet_tx *)RTE_PTR_ADD(vq, sz_vq);
440                 txvq->vq = vq;
441                 txvq->port_id = dev->data->port_id;
442                 txvq->queue_id = queue_idx;
443                 txvq->mz = mz;
444                 txvq->virtio_net_hdr_mz = hdr_mz;
445                 txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
446
447                 *pvq = txvq;
448         } else if (queue_type == VTNET_CQ) {
449                 cvq = (struct virtnet_ctl *)RTE_PTR_ADD(vq, sz_vq);
450                 cvq->vq = vq;
451                 cvq->mz = mz;
452                 cvq->virtio_net_hdr_mz = hdr_mz;
453                 cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
454                 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
455                 *pvq = cvq;
456         }
457
458         /* For virtio-user case (that is when dev->pci_dev is NULL), we use
459          * virtual address. And we need properly set _offset_, please see
460          * MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
461          */
462         if (dev->pci_dev)
463                 vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
464         else {
465                 vq->vq_ring_mem = (uintptr_t)mz->addr;
466                 vq->offset = offsetof(struct rte_mbuf, buf_addr);
467                 if (queue_type == VTNET_TQ)
468                         txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
469                 else if (queue_type == VTNET_CQ)
470                         cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
471         }
472
473         if (queue_type == VTNET_TQ) {
474                 struct virtio_tx_region *txr;
475                 unsigned int i;
476
477                 txr = hdr_mz->addr;
478                 memset(txr, 0, vq_size * sizeof(*txr));
479                 for (i = 0; i < vq_size; i++) {
480                         struct vring_desc *start_dp = txr[i].tx_indir;
481
482                         vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
483
484                         /* first indirect descriptor is always the tx header */
485                         start_dp->addr = txvq->virtio_net_hdr_mem
486                                 + i * sizeof(*txr)
487                                 + offsetof(struct virtio_tx_region, tx_hdr);
488
489                         start_dp->len = hw->vtnet_hdr_size;
490                         start_dp->flags = VRING_DESC_F_NEXT;
491                 }
492         }
493
494         if (hw->vtpci_ops->setup_queue(hw, vq) < 0) {
495                 PMD_INIT_LOG(ERR, "setup_queue failed");
496                 virtio_dev_queue_release(vq);
497                 return -EINVAL;
498         }
499
500         vq->configured = 1;
501         return 0;
502
503 fail_q_alloc:
504         rte_free(sw_ring);
505         rte_memzone_free(hdr_mz);
506         rte_memzone_free(mz);
507         rte_free(vq);
508
509         return ret;
510 }
511
512 static int
513 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
514                 uint32_t socket_id)
515 {
516         struct virtnet_ctl *cvq;
517         int ret;
518         struct virtio_hw *hw = dev->data->dev_private;
519
520         PMD_INIT_FUNC_TRACE();
521         ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
522                         vtpci_queue_idx, 0, socket_id, (void **)&cvq);
523         if (ret < 0) {
524                 PMD_INIT_LOG(ERR, "control vq initialization failed");
525                 return ret;
526         }
527
528         hw->cvq = cvq;
529         return 0;
530 }
531
532 static void
533 virtio_free_queues(struct rte_eth_dev *dev)
534 {
535         unsigned int i;
536
537         for (i = 0; i < dev->data->nb_rx_queues; i++)
538                 virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
539
540         dev->data->nb_rx_queues = 0;
541
542         for (i = 0; i < dev->data->nb_tx_queues; i++)
543                 virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
544
545         dev->data->nb_tx_queues = 0;
546 }
547
548 static void
549 virtio_dev_close(struct rte_eth_dev *dev)
550 {
551         struct virtio_hw *hw = dev->data->dev_private;
552
553         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
554
555         if (hw->started == 1)
556                 virtio_dev_stop(dev);
557
558         /* reset the NIC */
559         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
560                 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
561         vtpci_reset(hw);
562         virtio_dev_free_mbufs(dev);
563         virtio_free_queues(dev);
564 }
565
566 static void
567 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
568 {
569         struct virtio_hw *hw = dev->data->dev_private;
570         struct virtio_pmd_ctrl ctrl;
571         int dlen[1];
572         int ret;
573
574         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
575                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
576                 return;
577         }
578
579         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
580         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
581         ctrl.data[0] = 1;
582         dlen[0] = 1;
583
584         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
585         if (ret)
586                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
587 }
588
589 static void
590 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
591 {
592         struct virtio_hw *hw = dev->data->dev_private;
593         struct virtio_pmd_ctrl ctrl;
594         int dlen[1];
595         int ret;
596
597         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
598                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
599                 return;
600         }
601
602         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
603         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
604         ctrl.data[0] = 0;
605         dlen[0] = 1;
606
607         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
608         if (ret)
609                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
610 }
611
612 static void
613 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
614 {
615         struct virtio_hw *hw = dev->data->dev_private;
616         struct virtio_pmd_ctrl ctrl;
617         int dlen[1];
618         int ret;
619
620         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
621                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
622                 return;
623         }
624
625         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
626         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
627         ctrl.data[0] = 1;
628         dlen[0] = 1;
629
630         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
631         if (ret)
632                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
633 }
634
635 static void
636 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
637 {
638         struct virtio_hw *hw = dev->data->dev_private;
639         struct virtio_pmd_ctrl ctrl;
640         int dlen[1];
641         int ret;
642
643         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
644                 PMD_INIT_LOG(INFO, "host does not support rx control\n");
645                 return;
646         }
647
648         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
649         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
650         ctrl.data[0] = 0;
651         dlen[0] = 1;
652
653         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
654         if (ret)
655                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
656 }
657
658 /*
659  * dev_ops for virtio, bare necessities for basic operation
660  */
661 static const struct eth_dev_ops virtio_eth_dev_ops = {
662         .dev_configure           = virtio_dev_configure,
663         .dev_start               = virtio_dev_start,
664         .dev_stop                = virtio_dev_stop,
665         .dev_close               = virtio_dev_close,
666         .promiscuous_enable      = virtio_dev_promiscuous_enable,
667         .promiscuous_disable     = virtio_dev_promiscuous_disable,
668         .allmulticast_enable     = virtio_dev_allmulticast_enable,
669         .allmulticast_disable    = virtio_dev_allmulticast_disable,
670
671         .dev_infos_get           = virtio_dev_info_get,
672         .stats_get               = virtio_dev_stats_get,
673         .xstats_get              = virtio_dev_xstats_get,
674         .xstats_get_names        = virtio_dev_xstats_get_names,
675         .stats_reset             = virtio_dev_stats_reset,
676         .xstats_reset            = virtio_dev_stats_reset,
677         .link_update             = virtio_dev_link_update,
678         .rx_queue_setup          = virtio_dev_rx_queue_setup,
679         .rx_queue_release        = virtio_dev_rx_queue_release,
680         .tx_queue_setup          = virtio_dev_tx_queue_setup,
681         .tx_queue_release        = virtio_dev_tx_queue_release,
682         /* collect stats per queue */
683         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
684         .vlan_filter_set         = virtio_vlan_filter_set,
685         .mac_addr_add            = virtio_mac_addr_add,
686         .mac_addr_remove         = virtio_mac_addr_remove,
687         .mac_addr_set            = virtio_mac_addr_set,
688 };
689
690 static inline int
691 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
692                                 struct rte_eth_link *link)
693 {
694         struct rte_eth_link *dst = link;
695         struct rte_eth_link *src = &(dev->data->dev_link);
696
697         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
698                         *(uint64_t *)src) == 0)
699                 return -1;
700
701         return 0;
702 }
703
704 /**
705  * Atomically writes the link status information into global
706  * structure rte_eth_dev.
707  *
708  * @param dev
709  *   - Pointer to the structure rte_eth_dev to read from.
710  *   - Pointer to the buffer to be saved with the link status.
711  *
712  * @return
713  *   - On success, zero.
714  *   - On failure, negative value.
715  */
716 static inline int
717 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
718                 struct rte_eth_link *link)
719 {
720         struct rte_eth_link *dst = &(dev->data->dev_link);
721         struct rte_eth_link *src = link;
722
723         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
724                                         *(uint64_t *)src) == 0)
725                 return -1;
726
727         return 0;
728 }
729
730 static void
731 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
732 {
733         unsigned i;
734
735         for (i = 0; i < dev->data->nb_tx_queues; i++) {
736                 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
737                 if (txvq == NULL)
738                         continue;
739
740                 stats->opackets += txvq->stats.packets;
741                 stats->obytes += txvq->stats.bytes;
742                 stats->oerrors += txvq->stats.errors;
743
744                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
745                         stats->q_opackets[i] = txvq->stats.packets;
746                         stats->q_obytes[i] = txvq->stats.bytes;
747                 }
748         }
749
750         for (i = 0; i < dev->data->nb_rx_queues; i++) {
751                 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
752                 if (rxvq == NULL)
753                         continue;
754
755                 stats->ipackets += rxvq->stats.packets;
756                 stats->ibytes += rxvq->stats.bytes;
757                 stats->ierrors += rxvq->stats.errors;
758
759                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
760                         stats->q_ipackets[i] = rxvq->stats.packets;
761                         stats->q_ibytes[i] = rxvq->stats.bytes;
762                 }
763         }
764
765         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
766 }
767
768 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
769                                        struct rte_eth_xstat_name *xstats_names,
770                                        __rte_unused unsigned limit)
771 {
772         unsigned i;
773         unsigned count = 0;
774         unsigned t;
775
776         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
777                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
778
779         if (xstats_names != NULL) {
780                 /* Note: limit checked in rte_eth_xstats_names() */
781
782                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
783                         struct virtqueue *rxvq = dev->data->rx_queues[i];
784                         if (rxvq == NULL)
785                                 continue;
786                         for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
787                                 snprintf(xstats_names[count].name,
788                                         sizeof(xstats_names[count].name),
789                                         "rx_q%u_%s", i,
790                                         rte_virtio_rxq_stat_strings[t].name);
791                                 count++;
792                         }
793                 }
794
795                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
796                         struct virtqueue *txvq = dev->data->tx_queues[i];
797                         if (txvq == NULL)
798                                 continue;
799                         for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
800                                 snprintf(xstats_names[count].name,
801                                         sizeof(xstats_names[count].name),
802                                         "tx_q%u_%s", i,
803                                         rte_virtio_txq_stat_strings[t].name);
804                                 count++;
805                         }
806                 }
807                 return count;
808         }
809         return nstats;
810 }
811
812 static int
813 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
814                       unsigned n)
815 {
816         unsigned i;
817         unsigned count = 0;
818
819         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
820                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
821
822         if (n < nstats)
823                 return nstats;
824
825         for (i = 0; i < dev->data->nb_rx_queues; i++) {
826                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
827
828                 if (rxvq == NULL)
829                         continue;
830
831                 unsigned t;
832
833                 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
834                         xstats[count].value = *(uint64_t *)(((char *)rxvq) +
835                                 rte_virtio_rxq_stat_strings[t].offset);
836                         count++;
837                 }
838         }
839
840         for (i = 0; i < dev->data->nb_tx_queues; i++) {
841                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
842
843                 if (txvq == NULL)
844                         continue;
845
846                 unsigned t;
847
848                 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
849                         xstats[count].value = *(uint64_t *)(((char *)txvq) +
850                                 rte_virtio_txq_stat_strings[t].offset);
851                         count++;
852                 }
853         }
854
855         return count;
856 }
857
858 static void
859 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
860 {
861         virtio_update_stats(dev, stats);
862 }
863
864 static void
865 virtio_dev_stats_reset(struct rte_eth_dev *dev)
866 {
867         unsigned int i;
868
869         for (i = 0; i < dev->data->nb_tx_queues; i++) {
870                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
871                 if (txvq == NULL)
872                         continue;
873
874                 txvq->stats.packets = 0;
875                 txvq->stats.bytes = 0;
876                 txvq->stats.errors = 0;
877                 txvq->stats.multicast = 0;
878                 txvq->stats.broadcast = 0;
879                 memset(txvq->stats.size_bins, 0,
880                        sizeof(txvq->stats.size_bins[0]) * 8);
881         }
882
883         for (i = 0; i < dev->data->nb_rx_queues; i++) {
884                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
885                 if (rxvq == NULL)
886                         continue;
887
888                 rxvq->stats.packets = 0;
889                 rxvq->stats.bytes = 0;
890                 rxvq->stats.errors = 0;
891                 rxvq->stats.multicast = 0;
892                 rxvq->stats.broadcast = 0;
893                 memset(rxvq->stats.size_bins, 0,
894                        sizeof(rxvq->stats.size_bins[0]) * 8);
895         }
896 }
897
898 static void
899 virtio_set_hwaddr(struct virtio_hw *hw)
900 {
901         vtpci_write_dev_config(hw,
902                         offsetof(struct virtio_net_config, mac),
903                         &hw->mac_addr, ETHER_ADDR_LEN);
904 }
905
906 static void
907 virtio_get_hwaddr(struct virtio_hw *hw)
908 {
909         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
910                 vtpci_read_dev_config(hw,
911                         offsetof(struct virtio_net_config, mac),
912                         &hw->mac_addr, ETHER_ADDR_LEN);
913         } else {
914                 eth_random_addr(&hw->mac_addr[0]);
915                 virtio_set_hwaddr(hw);
916         }
917 }
918
919 static void
920 virtio_mac_table_set(struct virtio_hw *hw,
921                      const struct virtio_net_ctrl_mac *uc,
922                      const struct virtio_net_ctrl_mac *mc)
923 {
924         struct virtio_pmd_ctrl ctrl;
925         int err, len[2];
926
927         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
928                 PMD_DRV_LOG(INFO, "host does not support mac table");
929                 return;
930         }
931
932         ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
933         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
934
935         len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
936         memcpy(ctrl.data, uc, len[0]);
937
938         len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
939         memcpy(ctrl.data + len[0], mc, len[1]);
940
941         err = virtio_send_command(hw->cvq, &ctrl, len, 2);
942         if (err != 0)
943                 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
944 }
945
946 static void
947 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
948                     uint32_t index, uint32_t vmdq __rte_unused)
949 {
950         struct virtio_hw *hw = dev->data->dev_private;
951         const struct ether_addr *addrs = dev->data->mac_addrs;
952         unsigned int i;
953         struct virtio_net_ctrl_mac *uc, *mc;
954
955         if (index >= VIRTIO_MAX_MAC_ADDRS) {
956                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
957                 return;
958         }
959
960         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
961         uc->entries = 0;
962         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
963         mc->entries = 0;
964
965         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
966                 const struct ether_addr *addr
967                         = (i == index) ? mac_addr : addrs + i;
968                 struct virtio_net_ctrl_mac *tbl
969                         = is_multicast_ether_addr(addr) ? mc : uc;
970
971                 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
972         }
973
974         virtio_mac_table_set(hw, uc, mc);
975 }
976
977 static void
978 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
979 {
980         struct virtio_hw *hw = dev->data->dev_private;
981         struct ether_addr *addrs = dev->data->mac_addrs;
982         struct virtio_net_ctrl_mac *uc, *mc;
983         unsigned int i;
984
985         if (index >= VIRTIO_MAX_MAC_ADDRS) {
986                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
987                 return;
988         }
989
990         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
991         uc->entries = 0;
992         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
993         mc->entries = 0;
994
995         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
996                 struct virtio_net_ctrl_mac *tbl;
997
998                 if (i == index || is_zero_ether_addr(addrs + i))
999                         continue;
1000
1001                 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1002                 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1003         }
1004
1005         virtio_mac_table_set(hw, uc, mc);
1006 }
1007
1008 static void
1009 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1010 {
1011         struct virtio_hw *hw = dev->data->dev_private;
1012
1013         memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1014
1015         /* Use atomic update if available */
1016         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1017                 struct virtio_pmd_ctrl ctrl;
1018                 int len = ETHER_ADDR_LEN;
1019
1020                 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1021                 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1022
1023                 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1024                 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1025         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1026                 virtio_set_hwaddr(hw);
1027 }
1028
1029 static int
1030 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1031 {
1032         struct virtio_hw *hw = dev->data->dev_private;
1033         struct virtio_pmd_ctrl ctrl;
1034         int len;
1035
1036         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1037                 return -ENOTSUP;
1038
1039         ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1040         ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1041         memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1042         len = sizeof(vlan_id);
1043
1044         return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1045 }
1046
1047 static int
1048 virtio_negotiate_features(struct virtio_hw *hw)
1049 {
1050         uint64_t host_features;
1051
1052         /* Prepare guest_features: feature that driver wants to support */
1053         hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
1054         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1055                 hw->guest_features);
1056
1057         /* Read device(host) feature bits */
1058         host_features = hw->vtpci_ops->get_features(hw);
1059         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1060                 host_features);
1061
1062         /*
1063          * Negotiate features: Subset of device feature bits are written back
1064          * guest feature bits.
1065          */
1066         hw->guest_features = vtpci_negotiate_features(hw, host_features);
1067         PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1068                 hw->guest_features);
1069
1070         if (hw->modern) {
1071                 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1072                         PMD_INIT_LOG(ERR,
1073                                 "VIRTIO_F_VERSION_1 features is not enabled.");
1074                         return -1;
1075                 }
1076                 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1077                 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1078                         PMD_INIT_LOG(ERR,
1079                                 "failed to set FEATURES_OK status!");
1080                         return -1;
1081                 }
1082         }
1083
1084         return 0;
1085 }
1086
1087 /*
1088  * Process Virtio Config changed interrupt and call the callback
1089  * if link state changed.
1090  */
1091 static void
1092 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1093                          void *param)
1094 {
1095         struct rte_eth_dev *dev = param;
1096         struct virtio_hw *hw = dev->data->dev_private;
1097         uint8_t isr;
1098
1099         /* Read interrupt status which clears interrupt */
1100         isr = vtpci_isr(hw);
1101         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1102
1103         if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1104                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1105
1106         if (isr & VIRTIO_PCI_ISR_CONFIG) {
1107                 if (virtio_dev_link_update(dev, 0) == 0)
1108                         _rte_eth_dev_callback_process(dev,
1109                                                       RTE_ETH_EVENT_INTR_LSC);
1110         }
1111
1112 }
1113
1114 static void
1115 rx_func_get(struct rte_eth_dev *eth_dev)
1116 {
1117         struct virtio_hw *hw = eth_dev->data->dev_private;
1118         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1119                 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1120         else
1121                 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1122 }
1123
1124 /*
1125  * This function is based on probe() function in virtio_pci.c
1126  * It returns 0 on success.
1127  */
1128 int
1129 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1130 {
1131         struct virtio_hw *hw = eth_dev->data->dev_private;
1132         struct virtio_net_config *config;
1133         struct virtio_net_config local_config;
1134         struct rte_pci_device *pci_dev;
1135         uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1136         int ret;
1137
1138         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1139
1140         eth_dev->dev_ops = &virtio_eth_dev_ops;
1141         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1142
1143         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1144                 rx_func_get(eth_dev);
1145                 return 0;
1146         }
1147
1148         /* Allocate memory for storing MAC addresses */
1149         eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1150         if (eth_dev->data->mac_addrs == NULL) {
1151                 PMD_INIT_LOG(ERR,
1152                         "Failed to allocate %d bytes needed to store MAC addresses",
1153                         VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1154                 return -ENOMEM;
1155         }
1156
1157         pci_dev = eth_dev->pci_dev;
1158
1159         if (pci_dev) {
1160                 ret = vtpci_init(pci_dev, hw, &dev_flags);
1161                 if (ret)
1162                         return ret;
1163         }
1164
1165         /* Reset the device although not necessary at startup */
1166         vtpci_reset(hw);
1167
1168         /* Tell the host we've noticed this device. */
1169         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1170
1171         /* Tell the host we've known how to drive the device. */
1172         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1173         if (virtio_negotiate_features(hw) < 0)
1174                 return -1;
1175
1176         /* If host does not support status then disable LSC */
1177         if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1178                 dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1179
1180         rte_eth_copy_pci_info(eth_dev, pci_dev);
1181         eth_dev->data->dev_flags = dev_flags;
1182
1183         rx_func_get(eth_dev);
1184
1185         /* Setting up rx_header size for the device */
1186         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1187             vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1188                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1189         else
1190                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1191
1192         /* Copy the permanent MAC address to: virtio_hw */
1193         virtio_get_hwaddr(hw);
1194         ether_addr_copy((struct ether_addr *) hw->mac_addr,
1195                         &eth_dev->data->mac_addrs[0]);
1196         PMD_INIT_LOG(DEBUG,
1197                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1198                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1199                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1200
1201         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1202                 config = &local_config;
1203
1204                 vtpci_read_dev_config(hw,
1205                         offsetof(struct virtio_net_config, mac),
1206                         &config->mac, sizeof(config->mac));
1207
1208                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1209                         vtpci_read_dev_config(hw,
1210                                 offsetof(struct virtio_net_config, status),
1211                                 &config->status, sizeof(config->status));
1212                 } else {
1213                         PMD_INIT_LOG(DEBUG,
1214                                      "VIRTIO_NET_F_STATUS is not supported");
1215                         config->status = 0;
1216                 }
1217
1218                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1219                         vtpci_read_dev_config(hw,
1220                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1221                                 &config->max_virtqueue_pairs,
1222                                 sizeof(config->max_virtqueue_pairs));
1223                 } else {
1224                         PMD_INIT_LOG(DEBUG,
1225                                      "VIRTIO_NET_F_MQ is not supported");
1226                         config->max_virtqueue_pairs = 1;
1227                 }
1228
1229                 hw->max_rx_queues =
1230                         (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1231                         VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1232                 hw->max_tx_queues =
1233                         (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1234                         VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1235
1236                 virtio_dev_cq_queue_setup(eth_dev,
1237                                         config->max_virtqueue_pairs * 2,
1238                                         SOCKET_ID_ANY);
1239
1240                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1241                                 config->max_virtqueue_pairs);
1242                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1243                 PMD_INIT_LOG(DEBUG,
1244                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1245                                 config->mac[0], config->mac[1],
1246                                 config->mac[2], config->mac[3],
1247                                 config->mac[4], config->mac[5]);
1248         } else {
1249                 hw->max_rx_queues = 1;
1250                 hw->max_tx_queues = 1;
1251         }
1252
1253         PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
1254                         hw->max_rx_queues, hw->max_tx_queues);
1255         if (pci_dev)
1256                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1257                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1258                         pci_dev->id.device_id);
1259
1260         /* Setup interrupt callback  */
1261         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1262                 rte_intr_callback_register(&pci_dev->intr_handle,
1263                                    virtio_interrupt_handler, eth_dev);
1264
1265         virtio_dev_cq_start(eth_dev);
1266
1267         return 0;
1268 }
1269
1270 static int
1271 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1272 {
1273         struct rte_pci_device *pci_dev;
1274         struct virtio_hw *hw = eth_dev->data->dev_private;
1275
1276         PMD_INIT_FUNC_TRACE();
1277
1278         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1279                 return -EPERM;
1280
1281         /* Close it anyway since there's no way to know if closed */
1282         virtio_dev_close(eth_dev);
1283
1284         pci_dev = eth_dev->pci_dev;
1285
1286         eth_dev->dev_ops = NULL;
1287         eth_dev->tx_pkt_burst = NULL;
1288         eth_dev->rx_pkt_burst = NULL;
1289
1290         if (hw->cvq)
1291                 virtio_dev_queue_release(hw->cvq->vq);
1292
1293         rte_free(eth_dev->data->mac_addrs);
1294         eth_dev->data->mac_addrs = NULL;
1295
1296         /* reset interrupt callback  */
1297         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1298                 rte_intr_callback_unregister(&pci_dev->intr_handle,
1299                                                 virtio_interrupt_handler,
1300                                                 eth_dev);
1301         rte_eal_pci_unmap_device(pci_dev);
1302
1303         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1304
1305         return 0;
1306 }
1307
1308 static struct eth_driver rte_virtio_pmd = {
1309         .pci_drv = {
1310                 .name = "rte_virtio_pmd",
1311                 .id_table = pci_id_virtio_map,
1312                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1313         },
1314         .eth_dev_init = eth_virtio_dev_init,
1315         .eth_dev_uninit = eth_virtio_dev_uninit,
1316         .dev_private_size = sizeof(struct virtio_hw),
1317 };
1318
1319 /*
1320  * Driver initialization routine.
1321  * Invoked once at EAL init time.
1322  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1323  * Returns 0 on success.
1324  */
1325 static int
1326 rte_virtio_pmd_init(const char *name __rte_unused,
1327                     const char *param __rte_unused)
1328 {
1329         if (rte_eal_iopl_init() != 0) {
1330                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1331                 return -1;
1332         }
1333
1334         rte_eth_driver_register(&rte_virtio_pmd);
1335         return 0;
1336 }
1337
1338 /*
1339  * Configure virtio device
1340  * It returns 0 on success.
1341  */
1342 static int
1343 virtio_dev_configure(struct rte_eth_dev *dev)
1344 {
1345         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1346         struct virtio_hw *hw = dev->data->dev_private;
1347
1348         PMD_INIT_LOG(DEBUG, "configure");
1349
1350         if (rxmode->hw_ip_checksum) {
1351                 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1352                 return -EINVAL;
1353         }
1354
1355         hw->vlan_strip = rxmode->hw_vlan_strip;
1356
1357         if (rxmode->hw_vlan_filter
1358             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1359                 PMD_DRV_LOG(NOTICE,
1360                             "vlan filtering not available on this host");
1361                 return -ENOTSUP;
1362         }
1363
1364         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1365                 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1366                         PMD_DRV_LOG(ERR, "failed to set config vector");
1367                         return -EBUSY;
1368                 }
1369
1370         return 0;
1371 }
1372
1373
1374 static int
1375 virtio_dev_start(struct rte_eth_dev *dev)
1376 {
1377         uint16_t nb_queues, i;
1378         struct virtio_hw *hw = dev->data->dev_private;
1379         struct virtnet_rx *rxvq;
1380         struct virtnet_tx *txvq __rte_unused;
1381
1382         /* check if lsc interrupt feature is enabled */
1383         if (dev->data->dev_conf.intr_conf.lsc) {
1384                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1385                         PMD_DRV_LOG(ERR, "link status not supported by host");
1386                         return -ENOTSUP;
1387                 }
1388
1389                 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1390                         PMD_DRV_LOG(ERR, "interrupt enable failed");
1391                         return -EIO;
1392                 }
1393         }
1394
1395         /* Initialize Link state */
1396         virtio_dev_link_update(dev, 0);
1397
1398         /* On restart after stop do not touch queues */
1399         if (hw->started)
1400                 return 0;
1401
1402         /* Do final configuration before rx/tx engine starts */
1403         virtio_dev_rxtx_start(dev);
1404         vtpci_reinit_complete(hw);
1405
1406         hw->started = 1;
1407
1408         /*Notify the backend
1409          *Otherwise the tap backend might already stop its queue due to fullness.
1410          *vhost backend will have no chance to be waked up
1411          */
1412         nb_queues = dev->data->nb_rx_queues;
1413         if (nb_queues > 1) {
1414                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1415                         return -EINVAL;
1416         }
1417
1418         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1419
1420         for (i = 0; i < nb_queues; i++) {
1421                 rxvq = dev->data->rx_queues[i];
1422                 virtqueue_notify(rxvq->vq);
1423         }
1424
1425         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1426
1427         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1428                 rxvq = dev->data->rx_queues[i];
1429                 VIRTQUEUE_DUMP(rxvq->vq);
1430         }
1431
1432         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1433                 txvq = dev->data->tx_queues[i];
1434                 VIRTQUEUE_DUMP(txvq->vq);
1435         }
1436
1437         return 0;
1438 }
1439
1440 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1441 {
1442         struct rte_mbuf *buf;
1443         int i, mbuf_num = 0;
1444
1445         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1446                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1447
1448                 PMD_INIT_LOG(DEBUG,
1449                              "Before freeing rxq[%d] used and unused buf", i);
1450                 VIRTQUEUE_DUMP(rxvq->vq);
1451
1452                 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1453                 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1454                         rte_pktmbuf_free(buf);
1455                         mbuf_num++;
1456                 }
1457
1458                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1459                 PMD_INIT_LOG(DEBUG,
1460                              "After freeing rxq[%d] used and unused buf", i);
1461                 VIRTQUEUE_DUMP(rxvq->vq);
1462         }
1463
1464         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1465                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1466
1467                 PMD_INIT_LOG(DEBUG,
1468                              "Before freeing txq[%d] used and unused bufs",
1469                              i);
1470                 VIRTQUEUE_DUMP(txvq->vq);
1471
1472                 mbuf_num = 0;
1473                 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1474                         rte_pktmbuf_free(buf);
1475                         mbuf_num++;
1476                 }
1477
1478                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1479                 PMD_INIT_LOG(DEBUG,
1480                              "After freeing txq[%d] used and unused buf", i);
1481                 VIRTQUEUE_DUMP(txvq->vq);
1482         }
1483 }
1484
1485 /*
1486  * Stop device: disable interrupt and mark link down
1487  */
1488 static void
1489 virtio_dev_stop(struct rte_eth_dev *dev)
1490 {
1491         struct rte_eth_link link;
1492         struct virtio_hw *hw = dev->data->dev_private;
1493
1494         PMD_INIT_LOG(DEBUG, "stop");
1495
1496         hw->started = 0;
1497
1498         if (dev->data->dev_conf.intr_conf.lsc)
1499                 rte_intr_disable(&dev->pci_dev->intr_handle);
1500
1501         memset(&link, 0, sizeof(link));
1502         virtio_dev_atomic_write_link_status(dev, &link);
1503 }
1504
1505 static int
1506 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1507 {
1508         struct rte_eth_link link, old;
1509         uint16_t status;
1510         struct virtio_hw *hw = dev->data->dev_private;
1511         memset(&link, 0, sizeof(link));
1512         virtio_dev_atomic_read_link_status(dev, &link);
1513         old = link;
1514         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1515         link.link_speed  = SPEED_10G;
1516
1517         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1518                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1519                 vtpci_read_dev_config(hw,
1520                                 offsetof(struct virtio_net_config, status),
1521                                 &status, sizeof(status));
1522                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1523                         link.link_status = ETH_LINK_DOWN;
1524                         PMD_INIT_LOG(DEBUG, "Port %d is down",
1525                                      dev->data->port_id);
1526                 } else {
1527                         link.link_status = ETH_LINK_UP;
1528                         PMD_INIT_LOG(DEBUG, "Port %d is up",
1529                                      dev->data->port_id);
1530                 }
1531         } else {
1532                 link.link_status = ETH_LINK_UP;
1533         }
1534         virtio_dev_atomic_write_link_status(dev, &link);
1535
1536         return (old.link_status == link.link_status) ? -1 : 0;
1537 }
1538
1539 static void
1540 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1541 {
1542         struct virtio_hw *hw = dev->data->dev_private;
1543
1544         if (dev->pci_dev)
1545                 dev_info->driver_name = dev->driver->pci_drv.name;
1546         else
1547                 dev_info->driver_name = "virtio-user PMD";
1548         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1549         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1550         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1551         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1552         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1553         dev_info->default_txconf = (struct rte_eth_txconf) {
1554                 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1555         };
1556 }
1557
1558 /*
1559  * It enables testpmd to collect per queue stats.
1560  */
1561 static int
1562 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1563 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1564 __rte_unused uint8_t is_rx)
1565 {
1566         return 0;
1567 }
1568
1569 static struct rte_driver rte_virtio_driver = {
1570         .type = PMD_PDEV,
1571         .init = rte_virtio_pmd_init,
1572 };
1573
1574 PMD_REGISTER_DRIVER(rte_virtio_driver);