pci: fix igb_uio mapping for virtio_uio and vmxnet3_uio
[dpdk.git] / lib / librte_pmd_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
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
51 #include <rte_memory.h>
52 #include <rte_eal.h>
53
54 #include "virtio_ethdev.h"
55 #include "virtio_pci.h"
56 #include "virtio_logs.h"
57 #include "virtqueue.h"
58
59
60 static int eth_virtio_dev_init(struct eth_driver *eth_drv,
61                 struct rte_eth_dev *eth_dev);
62 static int  virtio_dev_configure(struct rte_eth_dev *dev);
63 static int  virtio_dev_start(struct rte_eth_dev *dev);
64 static void virtio_dev_stop(struct rte_eth_dev *dev);
65 static void virtio_dev_info_get(struct rte_eth_dev *dev,
66                                 struct rte_eth_dev_info *dev_info);
67 static int virtio_dev_link_update(struct rte_eth_dev *dev,
68         __rte_unused int wait_to_complete);
69
70 static void virtio_set_hwaddr(struct virtio_hw *hw);
71 static void virtio_get_hwaddr(struct virtio_hw *hw);
72
73 static void virtio_dev_rx_queue_release(__rte_unused void *rxq);
74 static void virtio_dev_tx_queue_release(__rte_unused void *txq);
75
76 static void virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
77 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
78 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
79
80 /*
81  * The set of PCI devices this driver supports
82  */
83 static struct rte_pci_id pci_id_virtio_map[] = {
84
85 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
86 #include "rte_pci_dev_ids.h"
87
88 { .vendor_id = 0, /* sentinel */ },
89 };
90
91 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
92                         int queue_type,
93                         uint16_t queue_idx,
94                         uint8_t  vtpci_queue_idx,
95                         uint16_t nb_desc,
96                         unsigned int socket_id,
97                         struct virtqueue **pvq)
98 {
99         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
100         const struct rte_memzone *mz;
101         uint16_t vq_size;
102         int size;
103         struct virtio_hw *hw = 
104                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
105         struct virtqueue  *vq = NULL;
106
107         /* Write the virtqueue index to the Queue Select Field */
108         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
109         PMD_INIT_LOG(DEBUG, "selecting queue: %d\n", vtpci_queue_idx);
110
111         /*
112          * Read the virtqueue size from the Queue Size field
113          * Always power of 2 and if 0 virtqueue does not exist
114          */
115         vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
116         PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d\n", vq_size, nb_desc);
117         if (nb_desc == 0)
118                 nb_desc = vq_size;
119         if (vq_size == 0) {
120                 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist\n", __func__);
121                 return (-EINVAL);
122         } else if (!rte_is_power_of_2(vq_size)) {
123                 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2\n", __func__);
124                 return (-EINVAL);
125         } else if (nb_desc != vq_size) {
126                 PMD_INIT_LOG(ERR, "Warning: nb_desc(%d) is not equal to vq size (%d), fall to vq size\n",
127                         nb_desc, vq_size);
128                 nb_desc = vq_size;
129         }
130
131         if (queue_type == VTNET_RQ) {
132                 rte_snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
133                                 dev->data->port_id, queue_idx);
134                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
135                         vq_size * sizeof(struct vq_desc_extra), CACHE_LINE_SIZE);
136                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
137         } else if(queue_type == VTNET_TQ) {
138                 rte_snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
139                         dev->data->port_id, queue_idx);
140                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
141                         vq_size * sizeof(struct vq_desc_extra), CACHE_LINE_SIZE);
142                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
143         } else if(queue_type == VTNET_CQ) {
144                 rte_snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
145                                 dev->data->port_id);
146                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue),
147                         CACHE_LINE_SIZE);
148                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
149         }
150         if (vq == NULL) {
151                 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue\n", __func__);
152                 return (-ENOMEM); 
153         }
154         vq->hw = hw;
155         vq->port_id = dev->data->port_id;
156         vq->queue_id = queue_idx;
157         vq->vq_queue_index = vtpci_queue_idx;
158         vq->vq_alignment = VIRTIO_PCI_VRING_ALIGN;
159         vq->vq_nentries = vq_size;
160         vq->vq_free_cnt = vq_size;
161
162         /*
163          * Reserve a memzone for vring elements
164          */
165         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
166         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
167         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d\n", size, vq->vq_ring_size);
168
169         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
170                         socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
171         if (mz == NULL) {
172                 rte_free(vq);
173                 return (-ENOMEM);
174         }
175         /*
176         * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
177         * and only accepts 32 bit page frame number. 
178         * Check if the allocated physical memory exceeds 16TB.
179         */
180         if ( (mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32) ) {
181                 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!\n");
182                 rte_free(vq);
183                 return (-ENOMEM);
184         }
185         memset(mz->addr, 0, sizeof(mz->len));
186         vq->mz = mz;
187         vq->vq_ring_mem = mz->phys_addr;
188         vq->vq_ring_virt_mem = mz->addr;
189         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64"\n", (uint64_t)mz->phys_addr);
190         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64"\n", (uint64_t)mz->addr);
191         vq->virtio_net_hdr_mz  = NULL;
192         vq->virtio_net_hdr_mem = (void *)NULL;
193
194         if (queue_type == VTNET_TQ) {
195                 /* 
196                 * For each xmit packet, allocate a virtio_net_hdr
197                 */
198                 rte_snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
199                         dev->data->port_id, queue_idx);
200                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
201                         vq_size * sizeof(struct virtio_net_hdr),
202                         socket_id, 0, CACHE_LINE_SIZE);
203                 if (vq->virtio_net_hdr_mz == NULL) {
204                         rte_free(vq);
205                         return (-ENOMEM);
206                 }
207                 vq->virtio_net_hdr_mem = (void *)(uintptr_t)vq->virtio_net_hdr_mz->phys_addr;
208                 memset(vq->virtio_net_hdr_mz->addr, 0, vq_size * sizeof(struct virtio_net_hdr));
209         } else if (queue_type == VTNET_CQ) {
210                 /* Allocate a page for control vq command, data and status */
211                 rte_snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
212                         dev->data->port_id);
213                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
214                         PAGE_SIZE, socket_id, 0, CACHE_LINE_SIZE);
215                 if (vq->virtio_net_hdr_mz == NULL) {
216                         rte_free(vq);
217                         return (-ENOMEM);
218                 }
219                 vq->virtio_net_hdr_mem = (void *)(uintptr_t)vq->virtio_net_hdr_mz->phys_addr;
220                 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
221         }
222
223         /*
224          * Set guest physical address of the virtqueue
225          * in VIRTIO_PCI_QUEUE_PFN config register of device
226          */
227         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
228                         mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
229         *pvq = vq;
230         return (0);
231 }
232
233 static int
234 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev,
235                 unsigned int socket_id)
236 {
237         struct virtqueue *vq;
238         uint16_t nb_desc = 0;
239         int ret;
240         struct virtio_hw *hw =
241                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
242
243         PMD_INIT_FUNC_TRACE();
244         ret = virtio_dev_queue_setup(dev, VTNET_CQ, 0, VTNET_SQ_CQ_QUEUE_IDX,
245                         nb_desc, socket_id, &vq);
246         if (ret < 0) {
247                 PMD_INIT_LOG(ERR, "control vq initialization failed\n");
248                 return ret;
249         }
250
251         hw->cvq = vq;
252         return (0);
253 }
254
255 static void
256 virtio_dev_close(struct rte_eth_dev *dev)
257 {
258         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
259
260         virtio_dev_stop(dev);
261 }
262
263
264 /*
265  * dev_ops for virtio, bare necessities for basic operation
266  */
267 static struct eth_dev_ops virtio_eth_dev_ops = {
268         .dev_configure         = virtio_dev_configure,
269         .dev_start             = virtio_dev_start,
270         .dev_stop              = virtio_dev_stop,
271         .dev_close             = virtio_dev_close,
272
273         .dev_infos_get         = virtio_dev_info_get,
274         .stats_get             = virtio_dev_stats_get,
275         .stats_reset           = virtio_dev_stats_reset,
276         .link_update           = virtio_dev_link_update,
277         .mac_addr_add          = NULL,
278         .mac_addr_remove       = NULL,
279         .rx_queue_setup        = virtio_dev_rx_queue_setup,
280         .rx_queue_release      = virtio_dev_rx_queue_release,  /* meaningfull only to multiple queue */
281         .tx_queue_setup        = virtio_dev_tx_queue_setup,
282         .tx_queue_release      = virtio_dev_tx_queue_release /* meaningfull only to multiple queue */
283 };
284
285 static inline int
286 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
287                                 struct rte_eth_link *link)
288 {
289         struct rte_eth_link *dst = link;
290         struct rte_eth_link *src = &(dev->data->dev_link);
291
292         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
293                         *(uint64_t *)src) == 0)
294                 return (-1);
295
296         return (0);
297 }
298
299 /**
300  * Atomically writes the link status information into global
301  * structure rte_eth_dev.
302  *
303  * @param dev
304  *   - Pointer to the structure rte_eth_dev to read from.
305  *   - Pointer to the buffer to be saved with the link status.
306  *
307  * @return
308  *   - On success, zero.
309  *   - On failure, negative value.
310  */
311 static inline int
312 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
313                 struct rte_eth_link *link)
314 {
315         struct rte_eth_link *dst = &(dev->data->dev_link);
316         struct rte_eth_link *src = link;
317
318         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
319                                         *(uint64_t *)src) == 0)
320                 return (-1);
321
322         return (0);
323 }
324
325 static void
326 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
327 {
328         struct virtio_hw *hw =
329                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
330         if(stats)
331                 memcpy(stats, &hw->eth_stats, sizeof(*stats));
332 }
333
334 static void
335 virtio_dev_stats_reset(struct rte_eth_dev *dev)
336 {
337         struct virtio_hw *hw =
338                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
339         /* Reset software totals */
340         memset(&hw->eth_stats, 0, sizeof(hw->eth_stats));
341 }
342
343 static void
344 virtio_set_hwaddr(struct virtio_hw *hw)
345 {
346         vtpci_write_dev_config(hw,
347                         offsetof(struct virtio_net_config, mac),
348                         &hw->mac_addr, ETHER_ADDR_LEN);
349 }
350
351 static void
352 virtio_get_hwaddr(struct virtio_hw *hw)
353 {
354         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
355                 vtpci_read_dev_config(hw,
356                         offsetof(struct virtio_net_config, mac),
357                         &hw->mac_addr, ETHER_ADDR_LEN);
358         } else {
359                 eth_random_addr(&hw->mac_addr[0]);
360                 virtio_set_hwaddr(hw);
361         }
362 }
363
364
365 static void
366 virtio_negotiate_features(struct virtio_hw *hw)
367 {
368         uint32_t guest_features, mask;
369         mask = VIRTIO_NET_F_CTRL_VQ | VIRTIO_NET_F_CTRL_RX | VIRTIO_NET_F_CTRL_VLAN;
370         mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM ;
371
372         /* TSO and LRO are only available when their corresponding
373          * checksum offload feature is also negotiated.
374          */
375         mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN;
376         mask |= VIRTIO_NET_F_GUEST_TSO4 | VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN;
377         mask |= VTNET_LRO_FEATURES;
378
379         /* rx_mbuf should not be in multiple merged segments */
380         mask |= VIRTIO_NET_F_MRG_RXBUF;
381
382         /* not negotiating INDIRECT descriptor table support */
383         mask |= VIRTIO_RING_F_INDIRECT_DESC;
384
385         /* Prepare guest_features: feature that driver wants to support */
386         guest_features = VTNET_FEATURES & ~mask;
387
388         /* Read device(host) feature bits */
389         hw->host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
390
391         /* Negotiate features: Subset of device feature bits are written back (guest feature bits) */
392         hw->guest_features = vtpci_negotiate_features(hw, guest_features);
393 }
394
395 /*
396  * This function is based on probe() function in virtio_pci.c
397  * It returns 0 on success.
398  */
399 static int
400 eth_virtio_dev_init(__rte_unused struct eth_driver *eth_drv,
401                 struct rte_eth_dev *eth_dev)
402 {
403         struct rte_pci_device *pci_dev;
404         struct virtio_hw *hw =
405                 VIRTIO_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
406         if (RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr) ) {
407                 PMD_INIT_LOG(ERR, 
408                         "MBUF HEADROOM should be enough to hold virtio net hdr\n");
409                 return (-1); 
410         }
411
412         if (! (rte_eal_get_configuration()->flags & EAL_FLG_HIGH_IOPL)) {
413                 PMD_INIT_LOG(ERR,
414                         "IOPL call failed in EAL init - cannot use virtio PMD driver\n");
415                 return (-1);
416         }
417
418         eth_dev->dev_ops = &virtio_eth_dev_ops;
419         eth_dev->rx_pkt_burst = &virtio_recv_pkts;
420         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
421
422         if(rte_eal_process_type() == RTE_PROC_SECONDARY)
423                 return 0;
424
425         pci_dev = eth_dev->pci_dev;
426
427         hw->device_id = pci_dev->id.device_id;
428         hw->vendor_id = pci_dev->id.vendor_id;
429         hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
430
431         hw->max_rx_queues = VIRTIO_MAX_RX_QUEUES;
432         hw->max_tx_queues = VIRTIO_MAX_TX_QUEUES;
433
434         /* Reset the device although not necessary at startup */
435         vtpci_reset(hw);
436
437         /* Tell the host we've noticed this device. */
438         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
439
440         /* Tell the host we've known how to drive the device. */
441         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
442         virtio_negotiate_features(hw);
443         /* Setting up rx_header size for the device */
444         if(vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
445                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
446         else
447                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
448
449         /* Allocate memory for storing MAC addresses */
450         eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
451         if (eth_dev->data->mac_addrs == NULL) {
452                 PMD_INIT_LOG(ERR,
453                         "Failed to allocate %d bytes needed to store MAC addresses",
454                         ETHER_ADDR_LEN);
455                 return (-ENOMEM);
456         }
457         /* Copy the permanent MAC address to: virtio_hw */
458         virtio_get_hwaddr(hw);
459         ether_addr_copy((struct ether_addr *) hw->mac_addr,
460                         &eth_dev->data->mac_addrs[0]);
461         PMD_INIT_LOG(DEBUG, "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", hw->mac_addr[0],
462                         hw->mac_addr[1],hw->mac_addr[2], hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
463
464         if(vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
465                 virtio_dev_cq_queue_setup(eth_dev, SOCKET_ID_ANY);
466
467         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
468                         eth_dev->data->port_id, pci_dev->id.vendor_id,
469                         pci_dev->id.device_id);
470         return (0);
471 }
472
473 static struct eth_driver rte_virtio_pmd = {
474         {
475                 .name = "rte_virtio_pmd",
476                 .id_table = pci_id_virtio_map,
477                 .drv_flags = RTE_PCI_DRV_NEED_IGB_UIO,
478         },
479         .eth_dev_init = eth_virtio_dev_init,
480         .dev_private_size = sizeof(struct virtio_adapter),
481 };
482
483 /*
484  * Driver initialization routine.
485  * Invoked once at EAL init time.
486  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
487  * Returns 0 on success.
488  */
489 int
490 rte_virtio_pmd_init(void)
491 {
492         rte_eth_driver_register(&rte_virtio_pmd);
493         return (0);
494 }
495
496 /*
497  * Only 1 queue is supported, no queue release related operation
498  */
499 static void
500 virtio_dev_rx_queue_release(__rte_unused void *rxq)
501 {
502 }
503
504 static void
505 virtio_dev_tx_queue_release(__rte_unused void *txq)
506 {
507 }
508
509 /*
510  * Configure virtio device
511  * It returns 0 on success.
512  */
513 static int
514 virtio_dev_configure(__rte_unused struct rte_eth_dev *dev)
515 {
516         return (0);
517 }
518
519
520 static int
521 virtio_dev_start(struct rte_eth_dev *dev)
522 {
523         uint16_t status;
524         struct virtio_hw *hw =
525                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
526
527         /* Tell the host we've noticed this device. */
528         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
529
530         /* Tell the host we've known how to drive the device. */
531         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
532
533         hw->adapter_stopped = 0;
534
535         /* Do final configuration before rx/tx engine starts */
536         virtio_dev_rxtx_start(dev);
537
538         /* Check VIRTIO_NET_F_STATUS for link status*/
539         if(vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
540
541                 vtpci_read_dev_config(hw,
542                                 offsetof(struct virtio_net_config, status),
543                                 &status, sizeof(status));
544                 if((status & VIRTIO_NET_S_LINK_UP) == 0) {
545                         PMD_INIT_LOG(ERR,     "Port: %d Link is DOWN\n", dev->data->port_id);
546                         return (-EIO);
547                 } else {
548                         PMD_INIT_LOG(DEBUG, "Port: %d Link is UP\n",  dev->data->port_id);
549                 }
550         }
551         vtpci_reinit_complete(hw);
552
553         /*Notify the backend
554          *Otherwise the tap backend might already stop its queue due to fullness.
555          *vhost backend will have no chance to be waked up
556          */
557         virtqueue_notify(dev->data->rx_queues[0]);
558         PMD_INIT_LOG(DEBUG, "Notified backend at initialization\n");
559         return (0);
560 }
561
562 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
563 {
564         struct rte_mbuf * buf;
565         int i = 0;
566         PMD_INIT_LOG(DEBUG, "Before freeing rxq used and unused buf \n");
567         VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[0]);
568         while( (buf =(struct rte_mbuf *)virtqueue_detatch_unused(dev->data->rx_queues[0])) != NULL) {
569                 rte_pktmbuf_free_seg(buf);
570                 i++;
571         }
572         PMD_INIT_LOG(DEBUG, "free %d mbufs\n", i);
573         PMD_INIT_LOG(DEBUG, "After freeing rxq used and unused buf\n");
574         VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[0]);
575         PMD_INIT_LOG(DEBUG, "Before freeing txq used and unused bufs\n");
576         VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[0]);
577         i = 0;
578         while( (buf = (struct rte_mbuf *)virtqueue_detatch_unused(dev->data->tx_queues[0])) != NULL) {
579                 rte_pktmbuf_free_seg(buf);
580                 i++;
581         }
582         PMD_INIT_LOG(DEBUG, "free %d mbufs\n", i);
583         PMD_INIT_LOG(DEBUG, "After freeing txq used and unused buf\n");
584         VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[0]);
585 }
586
587 /*
588  * Stop device: disable rx and tx functions to allow for reconfiguring.
589  */
590 static void
591 virtio_dev_stop(struct rte_eth_dev *dev)
592 {
593         struct virtio_hw *hw =
594                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
595
596         /* reset the NIC */
597         vtpci_reset(hw);
598         virtio_dev_free_mbufs(dev);
599 }
600
601 static int
602 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
603 {
604         struct rte_eth_link link, old;
605         uint16_t status;
606         struct virtio_hw *hw =
607                 VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
608         memset(&link, 0, sizeof(link));
609         virtio_dev_atomic_read_link_status(dev, &link);
610         old = link;
611         link.link_duplex = FULL_DUPLEX ;
612         link.link_speed  = SPEED_10G ;
613         if(vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
614                 PMD_INIT_LOG(DEBUG, "Get link status from hw\n");
615                 vtpci_read_dev_config(hw,
616                                 offsetof(struct virtio_net_config, status),
617                                 &status, sizeof(status));
618                 if((status & VIRTIO_NET_S_LINK_UP) == 0) {
619                         link.link_status = 0;
620                         PMD_INIT_LOG(DEBUG, "Port %d is down\n",dev->data->port_id);
621                 } else {
622                         link.link_status = 1;
623                         PMD_INIT_LOG(DEBUG, "Port %d is up\n",dev->data->port_id);
624                 }
625         } else {
626                 link.link_status = 1;   //Link up
627         }
628         virtio_dev_atomic_write_link_status(dev, &link);
629         if(old.link_status == link.link_status)
630                 return (-1);
631         /*changed*/
632         return (0);
633 }
634
635 static void
636 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
637 {
638         struct virtio_hw *hw = VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
639         dev_info->driver_name = dev->driver->pci_drv.name;
640         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
641         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
642         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
643         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
644         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
645 }