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