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