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