virtio: Native virtio driver

Change-Id: Id7fccf2f805e578fb05032aeb2b649a74c3c0e56
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
diff --git a/src/vnet/devices/virtio/virtio.h b/src/vnet/devices/virtio/virtio.h
index 841441b..f4a8103 100644
--- a/src/vnet/devices/virtio/virtio.h
+++ b/src/vnet/devices/virtio/virtio.h
@@ -18,6 +18,11 @@
 #ifndef _VNET_DEVICES_VIRTIO_VIRTIO_H_
 #define _VNET_DEVICES_VIRTIO_VIRTIO_H_
 
+#include <linux/virtio_config.h>
+#include <linux/virtio_net.h>
+#include <linux/virtio_pci.h>
+#include <linux/virtio_ring.h>
+
 #define foreach_virtio_net_features      \
   _ (VIRTIO_NET_F_CSUM, 0)	/* Host handles pkts w/ partial csum */ \
   _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \
@@ -53,6 +58,7 @@
   _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
   _ (VIRTIO_F_VERSION_1, 32)
 
+
 #define foreach_virtio_if_flag		\
   _(0, ADMIN_UP, "admin-up")		\
   _(1, DELETING, "deleting")
@@ -64,15 +70,31 @@
 #undef _
 } virtio_if_flag_t;
 
+#define VIRTIO_NUM_RX_DESC 256
+#define VIRTIO_NUM_TX_DESC 256
+
+#define VIRTIO_FEATURE(X) (1ULL << X)
+
 typedef enum
 {
   VIRTIO_IF_TYPE_TAP,
+  VIRTIO_IF_TYPE_PCI,
   VIRTIO_IF_N_TYPES,
 } virtio_if_type_t;
 
 
 typedef struct
 {
+  u8 mac[6];
+  u16 status;
+  u16 max_virtqueue_pairs;
+  u16 mtu;
+} virtio_net_config_t;
+
+#define VIRTIO_RING_FLAG_MASK_INT 1
+
+typedef struct
+{
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
   struct vring_desc *desc;
   struct vring_used *used;
@@ -82,33 +104,62 @@
   int kick_fd;
   int call_fd;
   u16 size;
-#define VIRTIO_RING_FLAG_MASK_INT 1
-  u32 flags;
+  u16 queue_id;
+  u16 flags;
   u32 call_file_index;
   u32 *buffers;
+  u32 *indirect_buffers;
   u16 last_used_idx;
   u16 last_kick_avail_idx;
 } virtio_vring_t;
 
+typedef union
+{
+  struct
+  {
+    u16 domain;
+    u8 bus;
+    u8 slot:5;
+    u8 function:3;
+  };
+  u32 as_u32;
+} pci_addr_t;
+
 typedef struct
 {
+  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
   u32 flags;
   clib_spinlock_t lockp;
 
-  u32 id;
   u32 dev_instance;
   u32 hw_if_index;
   u32 sw_if_index;
+  u16 virtio_net_hdr_sz;
+  virtio_if_type_t type;
+  union
+  {
+    u32 id;
+    pci_addr_t pci_addr;
+  };
   u32 per_interface_next_index;
   int fd;
-  int tap_fd;
+  union
+  {
+    int tap_fd;
+    u32 pci_dev_handle;
+  };
   virtio_vring_t *vrings;
 
   u64 features, remote_features;
 
-  virtio_if_type_t type;
+  /* error */
+  clib_error_t *error;
+  u16 max_queue_pairs;
   u16 tx_ring_sz;
   u16 rx_ring_sz;
+  u8 status;
+  u8 mac_addr[6];
+  u64 bar[2];
   u8 *host_if_name;
   u8 *net_ns;
   u8 *host_bridge;
@@ -135,17 +186,27 @@
 clib_error_t *virtio_vring_free (vlib_main_t * vm, virtio_if_t * vif,
 				 u32 idx);
 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
-
+extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
+extern void virtio_set_net_hdr_size (virtio_if_t * vif);
+extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
+			 u32 type);
+extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
+					    virtio_if_t * vif, u16 queue_id);
 format_function_t format_virtio_device_name;
 
 static_always_inline void
-virtio_kick (virtio_vring_t * vring)
+virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
 {
-  u64 x = 1;
-  int __clib_unused r;
+  if (vif->type == VIRTIO_IF_TYPE_PCI)
+    virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
+  else
+    {
+      u64 x = 1;
+      int __clib_unused r;
 
-  r = write (vring->kick_fd, &x, sizeof (x));
-  vring->last_kick_avail_idx = vring->avail->idx;
+      r = write (vring->kick_fd, &x, sizeof (x));
+      vring->last_kick_avail_idx = vring->avail->idx;
+    }
 }
 
 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */