virtio: remove kernel virtio header dependencies
[vpp.git] / src / vnet / devices / virtio / pci.c
index 16c7f6d..5ba9f36 100644 (file)
@@ -194,20 +194,13 @@ device_status (vlib_main_t * vm, virtio_if_t * vif)
     }
 }
 
-struct virtio_ctrl_msg
-{
-  struct virtio_net_ctrl_hdr ctrl;
-  virtio_net_ctrl_ack status;
-  u8 data[1024];
-};
-
 static int
 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
-                         struct virtio_ctrl_msg *data, u32 len)
+                         virtio_ctrl_msg_t * data, u32 len)
 {
   virtio_vring_t *vring = vif->cxq_vring;
-  virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
-  struct virtio_ctrl_msg result;
+  virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
+  virtio_ctrl_msg_t result;
   u32 buffer_index;
   vlib_buffer_t *b;
   u16 used, next, avail;
@@ -217,7 +210,7 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
   used = vring->desc_in_use;
   next = vring->desc_next;
   avail = vring->avail->idx;
-  struct vring_desc *d = &vring->desc[next];
+  vring_desc_t *d = &vring->desc[next];
 
   if (vlib_buffer_alloc (vm, &buffer_index, 1))
     b = vlib_get_buffer (vm, buffer_index);
@@ -228,11 +221,10 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
    * previous offset.
    */
   b->current_data = 0;
-  clib_memcpy (vlib_buffer_get_current (b), data,
-              sizeof (struct virtio_ctrl_msg));
+  clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
   d->flags = VRING_DESC_F_NEXT;
   d->addr = vlib_buffer_get_current_pa (vm, b);
-  d->len = sizeof (struct virtio_net_ctrl_hdr);
+  d->len = sizeof (virtio_net_ctrl_hdr_t);
   vring->avail->ring[avail & mask] = next;
   avail++;
   next = (next + 1) & mask;
@@ -242,7 +234,7 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
   d = &vring->desc[next];
   d->flags = VRING_DESC_F_NEXT;
   d->addr = vlib_buffer_get_current_pa (vm, b) +
-    STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
+    STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
   d->len = len;
   next = (next + 1) & mask;
   d->next = next;
@@ -251,7 +243,7 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
   d = &vring->desc[next];
   d->flags = VRING_DESC_F_WRITE;
   d->addr = vlib_buffer_get_current_pa (vm, b) +
-    STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
+    STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
   d->len = sizeof (data->status);
   next = (next + 1) & mask;
   used++;
@@ -271,7 +263,7 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
 
   while (n_left)
     {
-      struct vring_used_elem *e = &vring->used->ring[last & mask];
+      vring_used_elem_t *e = &vring->used->ring[last & mask];
       u16 slot = e->id;
 
       d = &vring->desc[slot];
@@ -290,7 +282,7 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
 
   CLIB_MEMORY_BARRIER ();
   clib_memcpy (&result, vlib_buffer_get_current (b),
-              sizeof (struct virtio_ctrl_msg));
+              sizeof (virtio_ctrl_msg_t));
   virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
   status = result.status;
   vlib_buffer_free (vm, &buffer_index, 1);
@@ -300,8 +292,8 @@ virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
 static int
 virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
 {
-  struct virtio_ctrl_msg offload_hdr;
-  virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+  virtio_ctrl_msg_t offload_hdr;
+  virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
 
   offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
   offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
@@ -320,8 +312,8 @@ virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
 static int
 virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
 {
-  struct virtio_ctrl_msg csum_offload_hdr;
-  virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+  virtio_ctrl_msg_t csum_offload_hdr;
+  virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
 
   csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
   csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
@@ -341,8 +333,8 @@ virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
 static int
 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
 {
-  struct virtio_ctrl_msg gso_hdr;
-  virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+  virtio_ctrl_msg_t gso_hdr;
+  virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
 
   gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
   gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
@@ -424,8 +416,8 @@ static int
 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
                              u16 num_queues)
 {
-  struct virtio_ctrl_msg mq_hdr;
-  virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+  virtio_ctrl_msg_t mq_hdr;
+  virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
 
   mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
   mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
@@ -454,7 +446,7 @@ virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
   clib_error_t *error = 0;
   u16 queue_size = 0;
   virtio_vring_t *vring;
-  struct vring vr;
+  vring_t vr;
   u32 i = 0;
   void *ptr = NULL;
 
@@ -505,7 +497,7 @@ virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
   clib_error_t *error = 0;
   u16 queue_size = 0;
   virtio_vring_t *vring;
-  struct vring vr;
+  vring_t vr;
   u32 i = 0;
   void *ptr = NULL;
 
@@ -600,6 +592,9 @@ virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
     | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
     | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
 
+  if (vif->is_modern)
+    supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
+
   if (req_features == 0)
     {
       req_features = supported_features;
@@ -650,27 +645,28 @@ virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
    * Read the status and verify it
    */
   status = vif->virtio_pci_func->get_status (vm, vif);
-  if (!
-      ((status & VIRTIO_CONFIG_STATUS_ACK)
-       && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
+  if ((status & VIRTIO_CONFIG_STATUS_ACK)
+      && (status & VIRTIO_CONFIG_STATUS_DRIVER))
+    vif->status = status;
+  else
     return -1;
-  vif->status = status;
 
   return 0;
 }
 
 clib_error_t *
-virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
+virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
 {
   clib_error_t *error = 0;
-  struct virtio_pci_cap cap;
-  u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
+  virtio_pci_cap_t cap;
+  u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
 
   if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
     {
       virtio_log_error (vif, "error in reading capabilty list position");
-      clib_error_return (error, "error in reading capabilty list position");
+      return clib_error_return (error,
+                               "error in reading capabilty list position");
     }
   while (pos)
     {
@@ -680,8 +676,9 @@ virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
        {
          virtio_log_error (vif, "%s [%2x]",
                            "error in reading the capability at", pos);
-         clib_error_return (error,
-                            "error in reading the capability at [%2x]", pos);
+         return clib_error_return (error,
+                                   "error in reading the capability at [%2x]",
+                                   pos);
        }
 
       if (cap.cap_vndr == PCI_CAP_ID_MSIX)
@@ -691,9 +688,9 @@ virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
          if ((error =
               vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
                                           sizeof (flags))))
-           clib_error_return (error,
-                              "error in reading the capability at [%2x]",
-                              pos + 2);
+           return clib_error_return (error,
+                                     "error in reading the capability at [%2x]",
+                                     pos + 2);
 
          table_size = flags & table_size_mask;
          virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
@@ -723,18 +720,45 @@ virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
       virtio_log_debug (vif,
                        "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
                        pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
+
+      if (cap.bar >= 0 && cap.bar <= 5)
+       {
+         vif->bar = bar[cap.bar];
+         vif->bar_id = cap.bar;
+       }
+      else
+       return clib_error_return (error, "invalid bar %u", cap.bar);
+
       switch (cap.cfg_type)
        {
        case VIRTIO_PCI_CAP_COMMON_CFG:
+         vif->common_offset = cap.offset;
          common_cfg = 1;
          break;
        case VIRTIO_PCI_CAP_NOTIFY_CFG:
-         notify_base = 1;
+         if ((error =
+              vlib_pci_read_write_config (vm, h, VLIB_READ,
+                                          pos + sizeof (cap),
+                                          &vif->notify_off_multiplier,
+                                          sizeof
+                                          (vif->notify_off_multiplier))))
+           {
+             virtio_log_error (vif, "notify off multiplier is not given");
+           }
+         else
+           {
+             virtio_log_debug (vif, "notify off multiplier is %u",
+                               vif->notify_off_multiplier);
+             vif->notify_offset = cap.offset;
+             notify = 1;
+           }
          break;
        case VIRTIO_PCI_CAP_DEVICE_CFG:
+         vif->device_offset = cap.offset;
          dev_cfg = 1;
          break;
        case VIRTIO_PCI_CAP_ISR_CFG:
+         vif->isr_offset = cap.offset;
          isr = 1;
          break;
        case VIRTIO_PCI_CAP_PCI_CFG:
@@ -746,16 +770,18 @@ virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
       pos = cap.cap_next;
     }
 
-  vif->virtio_pci_func = &virtio_pci_legacy_func;
-
-  if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
+  if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
     {
+      vif->virtio_pci_func = &virtio_pci_legacy_func;
       virtio_log_debug (vif, "legacy virtio pci device found");
       return error;
     }
 
+  vif->is_modern = 1;
+  vif->virtio_pci_func = &virtio_pci_modern_func;
+
   if (!pci_cfg)
-    clib_error_return (error, "modern virtio pci device found");
+    virtio_log_debug (vif, "modern virtio pci device found");
 
   virtio_log_debug (vif, "transitional virtio pci device found");
   return error;
@@ -763,24 +789,24 @@ virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
 
 static clib_error_t *
 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
-                       virtio_pci_create_if_args_t * args)
+                       virtio_pci_create_if_args_t * args, void **bar)
 {
   clib_error_t *error = 0;
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   u8 status = 0;
 
-  if ((error = virtio_pci_read_caps (vm, vif)))
+  if ((error = virtio_pci_read_caps (vm, vif, bar)))
     {
       args->rv = VNET_API_ERROR_UNSUPPORTED;
       virtio_log_error (vif, "Device is not supported");
-      clib_error_return (error, "Device is not supported");
+      return clib_error_return (error, "Device is not supported");
     }
 
   if (virtio_pci_reset_device (vm, vif) < 0)
     {
       args->rv = VNET_API_ERROR_INIT_FAILED;
       virtio_log_error (vif, "Failed to reset the device");
-      clib_error_return (error, "Failed to reset the device");
+      return clib_error_return (error, "Failed to reset the device");
     }
   /*
    * read device features and negotiate (user) requested features
@@ -810,7 +836,8 @@ virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
       args->rv = VNET_API_ERROR_UNSUPPORTED;
       virtio_log_error (vif,
                        "error encountered: Device doesn't support requested features");
-      clib_error_return (error, "Device doesn't support requested features");
+      return clib_error_return (error,
+                               "Device doesn't support requested features");
     }
   vif->status = status;
 
@@ -1017,6 +1044,21 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
       goto error;
     }
 
+  void *bar[6];
+  for (u32 i = 0; i <= 5; i++)
+    {
+
+      if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
+       {
+         virtio_log_debug (vif, "no pci map region for bar %u", i);
+       }
+      else
+       {
+         virtio_log_debug (vif, "pci map region for bar %u at %p", i,
+                           bar[i]);
+       }
+    }
+
   if ((error = vlib_pci_io_region (vm, h, 0)))
     {
       virtio_log_error (vif, "error encountered on pci io region");
@@ -1084,7 +1126,7 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
       goto error;
     }
 
-  if ((error = virtio_pci_device_init (vm, vif, args)))
+  if ((error = virtio_pci_device_init (vm, vif, args, bar)))
     {
       virtio_log_error (vif, "error encountered on device init");
       goto error;
@@ -1169,7 +1211,8 @@ virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
     vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
 
-  vif->virtio_pci_func->device_reset (vm, vif);
+  if (vif->virtio_pci_func)
+    vif->virtio_pci_func->device_reset (vm, vif);
 
   if (vif->hw_if_index)
     {