net/enic: refactor Tx mbuf recycling
[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                                 xstats_names[count].id = count;
792                                 count++;
793                         }
794                 }
795
796                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
797                         struct virtqueue *txvq = dev->data->tx_queues[i];
798                         if (txvq == NULL)
799                                 continue;
800                         for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
801                                 snprintf(xstats_names[count].name,
802                                         sizeof(xstats_names[count].name),
803                                         "tx_q%u_%s", i,
804                                         rte_virtio_txq_stat_strings[t].name);
805                                 xstats_names[count].id = count;
806                                 count++;
807                         }
808                 }
809                 return count;
810         }
811         return nstats;
812 }
813
814 static int
815 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
816                       unsigned n)
817 {
818         unsigned i;
819         unsigned count = 0;
820
821         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
822                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
823
824         if (n < nstats)
825                 return nstats;
826
827         for (i = 0; i < dev->data->nb_rx_queues; i++) {
828                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
829
830                 if (rxvq == NULL)
831                         continue;
832
833                 unsigned t;
834
835                 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
836                         xstats[count].id = count;
837                         xstats[count].value = *(uint64_t *)(((char *)rxvq) +
838                                 rte_virtio_rxq_stat_strings[t].offset);
839                         count++;
840                 }
841         }
842
843         for (i = 0; i < dev->data->nb_tx_queues; i++) {
844                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
845
846                 if (txvq == NULL)
847                         continue;
848
849                 unsigned t;
850
851                 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
852                         xstats[count].id = count;
853                         xstats[count].value = *(uint64_t *)(((char *)txvq) +
854                                 rte_virtio_txq_stat_strings[t].offset);
855                         count++;
856                 }
857         }
858
859         return count;
860 }
861
862 static void
863 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
864 {
865         virtio_update_stats(dev, stats);
866 }
867
868 static void
869 virtio_dev_stats_reset(struct rte_eth_dev *dev)
870 {
871         unsigned int i;
872
873         for (i = 0; i < dev->data->nb_tx_queues; i++) {
874                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
875                 if (txvq == NULL)
876                         continue;
877
878                 txvq->stats.packets = 0;
879                 txvq->stats.bytes = 0;
880                 txvq->stats.errors = 0;
881                 txvq->stats.multicast = 0;
882                 txvq->stats.broadcast = 0;
883                 memset(txvq->stats.size_bins, 0,
884                        sizeof(txvq->stats.size_bins[0]) * 8);
885         }
886
887         for (i = 0; i < dev->data->nb_rx_queues; i++) {
888                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
889                 if (rxvq == NULL)
890                         continue;
891
892                 rxvq->stats.packets = 0;
893                 rxvq->stats.bytes = 0;
894                 rxvq->stats.errors = 0;
895                 rxvq->stats.multicast = 0;
896                 rxvq->stats.broadcast = 0;
897                 memset(rxvq->stats.size_bins, 0,
898                        sizeof(rxvq->stats.size_bins[0]) * 8);
899         }
900 }
901
902 static void
903 virtio_set_hwaddr(struct virtio_hw *hw)
904 {
905         vtpci_write_dev_config(hw,
906                         offsetof(struct virtio_net_config, mac),
907                         &hw->mac_addr, ETHER_ADDR_LEN);
908 }
909
910 static void
911 virtio_get_hwaddr(struct virtio_hw *hw)
912 {
913         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
914                 vtpci_read_dev_config(hw,
915                         offsetof(struct virtio_net_config, mac),
916                         &hw->mac_addr, ETHER_ADDR_LEN);
917         } else {
918                 eth_random_addr(&hw->mac_addr[0]);
919                 virtio_set_hwaddr(hw);
920         }
921 }
922
923 static void
924 virtio_mac_table_set(struct virtio_hw *hw,
925                      const struct virtio_net_ctrl_mac *uc,
926                      const struct virtio_net_ctrl_mac *mc)
927 {
928         struct virtio_pmd_ctrl ctrl;
929         int err, len[2];
930
931         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
932                 PMD_DRV_LOG(INFO, "host does not support mac table");
933                 return;
934         }
935
936         ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
937         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
938
939         len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
940         memcpy(ctrl.data, uc, len[0]);
941
942         len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
943         memcpy(ctrl.data + len[0], mc, len[1]);
944
945         err = virtio_send_command(hw->cvq, &ctrl, len, 2);
946         if (err != 0)
947                 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
948 }
949
950 static void
951 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
952                     uint32_t index, uint32_t vmdq __rte_unused)
953 {
954         struct virtio_hw *hw = dev->data->dev_private;
955         const struct ether_addr *addrs = dev->data->mac_addrs;
956         unsigned int i;
957         struct virtio_net_ctrl_mac *uc, *mc;
958
959         if (index >= VIRTIO_MAX_MAC_ADDRS) {
960                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
961                 return;
962         }
963
964         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
965         uc->entries = 0;
966         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
967         mc->entries = 0;
968
969         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
970                 const struct ether_addr *addr
971                         = (i == index) ? mac_addr : addrs + i;
972                 struct virtio_net_ctrl_mac *tbl
973                         = is_multicast_ether_addr(addr) ? mc : uc;
974
975                 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
976         }
977
978         virtio_mac_table_set(hw, uc, mc);
979 }
980
981 static void
982 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
983 {
984         struct virtio_hw *hw = dev->data->dev_private;
985         struct ether_addr *addrs = dev->data->mac_addrs;
986         struct virtio_net_ctrl_mac *uc, *mc;
987         unsigned int i;
988
989         if (index >= VIRTIO_MAX_MAC_ADDRS) {
990                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
991                 return;
992         }
993
994         uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
995         uc->entries = 0;
996         mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
997         mc->entries = 0;
998
999         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1000                 struct virtio_net_ctrl_mac *tbl;
1001
1002                 if (i == index || is_zero_ether_addr(addrs + i))
1003                         continue;
1004
1005                 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1006                 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1007         }
1008
1009         virtio_mac_table_set(hw, uc, mc);
1010 }
1011
1012 static void
1013 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1014 {
1015         struct virtio_hw *hw = dev->data->dev_private;
1016
1017         memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1018
1019         /* Use atomic update if available */
1020         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1021                 struct virtio_pmd_ctrl ctrl;
1022                 int len = ETHER_ADDR_LEN;
1023
1024                 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1025                 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1026
1027                 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1028                 virtio_send_command(hw->cvq, &ctrl, &len, 1);
1029         } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1030                 virtio_set_hwaddr(hw);
1031 }
1032
1033 static int
1034 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1035 {
1036         struct virtio_hw *hw = dev->data->dev_private;
1037         struct virtio_pmd_ctrl ctrl;
1038         int len;
1039
1040         if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1041                 return -ENOTSUP;
1042
1043         ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1044         ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1045         memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1046         len = sizeof(vlan_id);
1047
1048         return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1049 }
1050
1051 static int
1052 virtio_negotiate_features(struct virtio_hw *hw)
1053 {
1054         uint64_t host_features;
1055
1056         /* Prepare guest_features: feature that driver wants to support */
1057         hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
1058         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1059                 hw->guest_features);
1060
1061         /* Read device(host) feature bits */
1062         host_features = hw->vtpci_ops->get_features(hw);
1063         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1064                 host_features);
1065
1066         /*
1067          * Negotiate features: Subset of device feature bits are written back
1068          * guest feature bits.
1069          */
1070         hw->guest_features = vtpci_negotiate_features(hw, host_features);
1071         PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1072                 hw->guest_features);
1073
1074         if (hw->modern) {
1075                 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1076                         PMD_INIT_LOG(ERR,
1077                                 "VIRTIO_F_VERSION_1 features is not enabled.");
1078                         return -1;
1079                 }
1080                 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1081                 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1082                         PMD_INIT_LOG(ERR,
1083                                 "failed to set FEATURES_OK status!");
1084                         return -1;
1085                 }
1086         }
1087
1088         return 0;
1089 }
1090
1091 /*
1092  * Process Virtio Config changed interrupt and call the callback
1093  * if link state changed.
1094  */
1095 static void
1096 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1097                          void *param)
1098 {
1099         struct rte_eth_dev *dev = param;
1100         struct virtio_hw *hw = dev->data->dev_private;
1101         uint8_t isr;
1102
1103         /* Read interrupt status which clears interrupt */
1104         isr = vtpci_isr(hw);
1105         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1106
1107         if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1108                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1109
1110         if (isr & VIRTIO_PCI_ISR_CONFIG) {
1111                 if (virtio_dev_link_update(dev, 0) == 0)
1112                         _rte_eth_dev_callback_process(dev,
1113                                                       RTE_ETH_EVENT_INTR_LSC);
1114         }
1115
1116 }
1117
1118 static void
1119 rx_func_get(struct rte_eth_dev *eth_dev)
1120 {
1121         struct virtio_hw *hw = eth_dev->data->dev_private;
1122         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1123                 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1124         else
1125                 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1126 }
1127
1128 /*
1129  * This function is based on probe() function in virtio_pci.c
1130  * It returns 0 on success.
1131  */
1132 int
1133 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1134 {
1135         struct virtio_hw *hw = eth_dev->data->dev_private;
1136         struct virtio_net_config *config;
1137         struct virtio_net_config local_config;
1138         struct rte_pci_device *pci_dev;
1139         uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1140         int ret;
1141
1142         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1143
1144         eth_dev->dev_ops = &virtio_eth_dev_ops;
1145         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1146
1147         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1148                 rx_func_get(eth_dev);
1149                 return 0;
1150         }
1151
1152         /* Allocate memory for storing MAC addresses */
1153         eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1154         if (eth_dev->data->mac_addrs == NULL) {
1155                 PMD_INIT_LOG(ERR,
1156                         "Failed to allocate %d bytes needed to store MAC addresses",
1157                         VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1158                 return -ENOMEM;
1159         }
1160
1161         pci_dev = eth_dev->pci_dev;
1162
1163         if (pci_dev) {
1164                 ret = vtpci_init(pci_dev, hw, &dev_flags);
1165                 if (ret)
1166                         return ret;
1167         }
1168
1169         /* Reset the device although not necessary at startup */
1170         vtpci_reset(hw);
1171
1172         /* Tell the host we've noticed this device. */
1173         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1174
1175         /* Tell the host we've known how to drive the device. */
1176         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1177         if (virtio_negotiate_features(hw) < 0)
1178                 return -1;
1179
1180         /* If host does not support status then disable LSC */
1181         if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1182                 dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1183
1184         rte_eth_copy_pci_info(eth_dev, pci_dev);
1185         eth_dev->data->dev_flags = dev_flags;
1186
1187         rx_func_get(eth_dev);
1188
1189         /* Setting up rx_header size for the device */
1190         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1191             vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
1192                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1193         else
1194                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1195
1196         /* Copy the permanent MAC address to: virtio_hw */
1197         virtio_get_hwaddr(hw);
1198         ether_addr_copy((struct ether_addr *) hw->mac_addr,
1199                         &eth_dev->data->mac_addrs[0]);
1200         PMD_INIT_LOG(DEBUG,
1201                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1202                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1203                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1204
1205         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1206                 config = &local_config;
1207
1208                 vtpci_read_dev_config(hw,
1209                         offsetof(struct virtio_net_config, mac),
1210                         &config->mac, sizeof(config->mac));
1211
1212                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1213                         vtpci_read_dev_config(hw,
1214                                 offsetof(struct virtio_net_config, status),
1215                                 &config->status, sizeof(config->status));
1216                 } else {
1217                         PMD_INIT_LOG(DEBUG,
1218                                      "VIRTIO_NET_F_STATUS is not supported");
1219                         config->status = 0;
1220                 }
1221
1222                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1223                         vtpci_read_dev_config(hw,
1224                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1225                                 &config->max_virtqueue_pairs,
1226                                 sizeof(config->max_virtqueue_pairs));
1227                 } else {
1228                         PMD_INIT_LOG(DEBUG,
1229                                      "VIRTIO_NET_F_MQ is not supported");
1230                         config->max_virtqueue_pairs = 1;
1231                 }
1232
1233                 hw->max_rx_queues =
1234                         (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1235                         VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1236                 hw->max_tx_queues =
1237                         (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1238                         VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1239
1240                 virtio_dev_cq_queue_setup(eth_dev,
1241                                         config->max_virtqueue_pairs * 2,
1242                                         SOCKET_ID_ANY);
1243
1244                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1245                                 config->max_virtqueue_pairs);
1246                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1247                 PMD_INIT_LOG(DEBUG,
1248                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1249                                 config->mac[0], config->mac[1],
1250                                 config->mac[2], config->mac[3],
1251                                 config->mac[4], config->mac[5]);
1252         } else {
1253                 hw->max_rx_queues = 1;
1254                 hw->max_tx_queues = 1;
1255         }
1256
1257         PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
1258                         hw->max_rx_queues, hw->max_tx_queues);
1259         if (pci_dev)
1260                 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1261                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1262                         pci_dev->id.device_id);
1263
1264         /* Setup interrupt callback  */
1265         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1266                 rte_intr_callback_register(&pci_dev->intr_handle,
1267                                    virtio_interrupt_handler, eth_dev);
1268
1269         virtio_dev_cq_start(eth_dev);
1270
1271         return 0;
1272 }
1273
1274 static int
1275 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1276 {
1277         struct rte_pci_device *pci_dev;
1278         struct virtio_hw *hw = eth_dev->data->dev_private;
1279
1280         PMD_INIT_FUNC_TRACE();
1281
1282         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1283                 return -EPERM;
1284
1285         /* Close it anyway since there's no way to know if closed */
1286         virtio_dev_close(eth_dev);
1287
1288         pci_dev = eth_dev->pci_dev;
1289
1290         eth_dev->dev_ops = NULL;
1291         eth_dev->tx_pkt_burst = NULL;
1292         eth_dev->rx_pkt_burst = NULL;
1293
1294         if (hw->cvq)
1295                 virtio_dev_queue_release(hw->cvq->vq);
1296
1297         rte_free(eth_dev->data->mac_addrs);
1298         eth_dev->data->mac_addrs = NULL;
1299
1300         /* reset interrupt callback  */
1301         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1302                 rte_intr_callback_unregister(&pci_dev->intr_handle,
1303                                                 virtio_interrupt_handler,
1304                                                 eth_dev);
1305         rte_eal_pci_unmap_device(pci_dev);
1306
1307         PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1308
1309         return 0;
1310 }
1311
1312 static struct eth_driver rte_virtio_pmd = {
1313         .pci_drv = {
1314                 .name = "rte_virtio_pmd",
1315                 .id_table = pci_id_virtio_map,
1316                 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1317         },
1318         .eth_dev_init = eth_virtio_dev_init,
1319         .eth_dev_uninit = eth_virtio_dev_uninit,
1320         .dev_private_size = sizeof(struct virtio_hw),
1321 };
1322
1323 /*
1324  * Driver initialization routine.
1325  * Invoked once at EAL init time.
1326  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1327  * Returns 0 on success.
1328  */
1329 static int
1330 rte_virtio_pmd_init(const char *name __rte_unused,
1331                     const char *param __rte_unused)
1332 {
1333         if (rte_eal_iopl_init() != 0) {
1334                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1335                 return -1;
1336         }
1337
1338         rte_eth_driver_register(&rte_virtio_pmd);
1339         return 0;
1340 }
1341
1342 /*
1343  * Configure virtio device
1344  * It returns 0 on success.
1345  */
1346 static int
1347 virtio_dev_configure(struct rte_eth_dev *dev)
1348 {
1349         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1350         struct virtio_hw *hw = dev->data->dev_private;
1351
1352         PMD_INIT_LOG(DEBUG, "configure");
1353
1354         if (rxmode->hw_ip_checksum) {
1355                 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1356                 return -EINVAL;
1357         }
1358
1359         hw->vlan_strip = rxmode->hw_vlan_strip;
1360
1361         if (rxmode->hw_vlan_filter
1362             && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1363                 PMD_DRV_LOG(NOTICE,
1364                             "vlan filtering not available on this host");
1365                 return -ENOTSUP;
1366         }
1367
1368         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1369                 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1370                         PMD_DRV_LOG(ERR, "failed to set config vector");
1371                         return -EBUSY;
1372                 }
1373
1374         return 0;
1375 }
1376
1377
1378 static int
1379 virtio_dev_start(struct rte_eth_dev *dev)
1380 {
1381         uint16_t nb_queues, i;
1382         struct virtio_hw *hw = dev->data->dev_private;
1383         struct virtnet_rx *rxvq;
1384         struct virtnet_tx *txvq __rte_unused;
1385
1386         /* check if lsc interrupt feature is enabled */
1387         if (dev->data->dev_conf.intr_conf.lsc) {
1388                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1389                         PMD_DRV_LOG(ERR, "link status not supported by host");
1390                         return -ENOTSUP;
1391                 }
1392
1393                 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1394                         PMD_DRV_LOG(ERR, "interrupt enable failed");
1395                         return -EIO;
1396                 }
1397         }
1398
1399         /* Initialize Link state */
1400         virtio_dev_link_update(dev, 0);
1401
1402         /* On restart after stop do not touch queues */
1403         if (hw->started)
1404                 return 0;
1405
1406         /* Do final configuration before rx/tx engine starts */
1407         virtio_dev_rxtx_start(dev);
1408         vtpci_reinit_complete(hw);
1409
1410         hw->started = 1;
1411
1412         /*Notify the backend
1413          *Otherwise the tap backend might already stop its queue due to fullness.
1414          *vhost backend will have no chance to be waked up
1415          */
1416         nb_queues = dev->data->nb_rx_queues;
1417         if (nb_queues > 1) {
1418                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1419                         return -EINVAL;
1420         }
1421
1422         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1423
1424         for (i = 0; i < nb_queues; i++) {
1425                 rxvq = dev->data->rx_queues[i];
1426                 virtqueue_notify(rxvq->vq);
1427         }
1428
1429         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1430
1431         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1432                 rxvq = dev->data->rx_queues[i];
1433                 VIRTQUEUE_DUMP(rxvq->vq);
1434         }
1435
1436         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1437                 txvq = dev->data->tx_queues[i];
1438                 VIRTQUEUE_DUMP(txvq->vq);
1439         }
1440
1441         return 0;
1442 }
1443
1444 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1445 {
1446         struct rte_mbuf *buf;
1447         int i, mbuf_num = 0;
1448
1449         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1450                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1451
1452                 PMD_INIT_LOG(DEBUG,
1453                              "Before freeing rxq[%d] used and unused buf", i);
1454                 VIRTQUEUE_DUMP(rxvq->vq);
1455
1456                 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
1457                 while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
1458                         rte_pktmbuf_free(buf);
1459                         mbuf_num++;
1460                 }
1461
1462                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1463                 PMD_INIT_LOG(DEBUG,
1464                              "After freeing rxq[%d] used and unused buf", i);
1465                 VIRTQUEUE_DUMP(rxvq->vq);
1466         }
1467
1468         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1469                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
1470
1471                 PMD_INIT_LOG(DEBUG,
1472                              "Before freeing txq[%d] used and unused bufs",
1473                              i);
1474                 VIRTQUEUE_DUMP(txvq->vq);
1475
1476                 mbuf_num = 0;
1477                 while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
1478                         rte_pktmbuf_free(buf);
1479                         mbuf_num++;
1480                 }
1481
1482                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1483                 PMD_INIT_LOG(DEBUG,
1484                              "After freeing txq[%d] used and unused buf", i);
1485                 VIRTQUEUE_DUMP(txvq->vq);
1486         }
1487 }
1488
1489 /*
1490  * Stop device: disable interrupt and mark link down
1491  */
1492 static void
1493 virtio_dev_stop(struct rte_eth_dev *dev)
1494 {
1495         struct rte_eth_link link;
1496         struct virtio_hw *hw = dev->data->dev_private;
1497
1498         PMD_INIT_LOG(DEBUG, "stop");
1499
1500         hw->started = 0;
1501
1502         if (dev->data->dev_conf.intr_conf.lsc)
1503                 rte_intr_disable(&dev->pci_dev->intr_handle);
1504
1505         memset(&link, 0, sizeof(link));
1506         virtio_dev_atomic_write_link_status(dev, &link);
1507 }
1508
1509 static int
1510 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1511 {
1512         struct rte_eth_link link, old;
1513         uint16_t status;
1514         struct virtio_hw *hw = dev->data->dev_private;
1515         memset(&link, 0, sizeof(link));
1516         virtio_dev_atomic_read_link_status(dev, &link);
1517         old = link;
1518         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1519         link.link_speed  = SPEED_10G;
1520
1521         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1522                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1523                 vtpci_read_dev_config(hw,
1524                                 offsetof(struct virtio_net_config, status),
1525                                 &status, sizeof(status));
1526                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1527                         link.link_status = ETH_LINK_DOWN;
1528                         PMD_INIT_LOG(DEBUG, "Port %d is down",
1529                                      dev->data->port_id);
1530                 } else {
1531                         link.link_status = ETH_LINK_UP;
1532                         PMD_INIT_LOG(DEBUG, "Port %d is up",
1533                                      dev->data->port_id);
1534                 }
1535         } else {
1536                 link.link_status = ETH_LINK_UP;
1537         }
1538         virtio_dev_atomic_write_link_status(dev, &link);
1539
1540         return (old.link_status == link.link_status) ? -1 : 0;
1541 }
1542
1543 static void
1544 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1545 {
1546         struct virtio_hw *hw = dev->data->dev_private;
1547
1548         if (dev->pci_dev)
1549                 dev_info->driver_name = dev->driver->pci_drv.name;
1550         else
1551                 dev_info->driver_name = "virtio-user PMD";
1552         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1553         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1554         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1555         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1556         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1557         dev_info->default_txconf = (struct rte_eth_txconf) {
1558                 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1559         };
1560 }
1561
1562 /*
1563  * It enables testpmd to collect per queue stats.
1564  */
1565 static int
1566 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1567 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1568 __rte_unused uint8_t is_rx)
1569 {
1570         return 0;
1571 }
1572
1573 static struct rte_driver rte_virtio_driver = {
1574         .type = PMD_PDEV,
1575         .init = rte_virtio_pmd_init,
1576 };
1577
1578 PMD_REGISTER_DRIVER(rte_virtio_driver);