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