vlib: pci improvements
[vpp.git] / src / plugins / vmxnet3 / vmxnet3.c
index 74ef126..3752241 100644 (file)
@@ -77,6 +77,26 @@ vmxnet3_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
   return 0;
 }
 
+static void
+vmxnet3_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
+                                u32 node_index)
+{
+  vmxnet3_main_t *vmxm = &vmxnet3_main;
+  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
+  vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
+
+  /* Shut off redirection */
+  if (node_index == ~0)
+    {
+      vd->per_interface_next_index = node_index;
+      return;
+    }
+
+  vd->per_interface_next_index =
+    vlib_node_add_next (vlib_get_main (), vmxnet3_input_node.index,
+                       node_index);
+}
+
 static char *vmxnet3_tx_func_error_strings[] = {
 #define _(n,s) s,
   foreach_vmxnet3_tx_func_error
@@ -91,6 +111,7 @@ VNET_DEVICE_CLASS (vmxnet3_device_class,) =
   .format_device_name = format_vmxnet3_device_name,
   .admin_up_down_function = vmxnet3_interface_admin_up_down,
   .rx_mode_change_function = vmxnet3_interface_rx_mode_change,
+  .rx_redirect_to_node = vmxnet3_set_interface_next_node,
   .tx_function_n_errors = VMXNET3_TX_N_ERROR,
   .tx_function_error_strings = vmxnet3_tx_func_error_strings,
 };
@@ -164,6 +185,7 @@ vmxnet3_provision_driver_shared (vlib_main_t * vm, vmxnet3_device_t * vd)
   shared->misc.num_tx_queues = vd->num_tx_queues;
   shared->misc.num_rx_queues = vd->num_rx_queues;
   shared->interrupt.num_intrs = vd->num_intrs;
+  shared->interrupt.event_intr_index = 1;
   shared->interrupt.control = VMXNET3_IC_DISABLE_ALL;
   shared->rx_filter.mode = VMXNET3_RXMODE_UCAST | VMXNET3_RXMODE_BCAST |
     VMXNET3_RXMODE_ALL_MULTI;
@@ -231,6 +253,7 @@ vmxnet3_rxq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
 
       ring = &rxq->rx_ring[rid];
       ring->gen = VMXNET3_RXF_GEN;
+      ring->rid = rid;
       vec_validate_aligned (ring->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
     }
   rxq->rx_comp_ring.gen = VMXNET3_RXCF_GEN;
@@ -252,6 +275,7 @@ vmxnet3_txq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
       if (txq->lock == 0)
        clib_spinlock_init (&txq->lock);
       vd->flags |= VMXNET3_DEVICE_F_SHARED_TXQ_LOCK;
+      return 0;
     }
 
   vec_validate_aligned (vd->txqs, qid, CLIB_CACHE_LINE_BYTES);
@@ -288,7 +312,7 @@ vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
 
   vd->num_tx_queues = 1;
   vd->num_rx_queues = 1;
-  vd->num_intrs = 1;
+  vd->num_intrs = 2;
 
   /* Quiesce the device */
   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
@@ -330,6 +354,18 @@ vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
       return error;
     }
 
+  vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
+  ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
+  if (ret & 1)
+    {
+      vd->flags |= VMXNET3_DEVICE_F_LINK_UP;
+      vd->link_speed = ret >> 16;
+    }
+  else
+    {
+      vd->flags &= ~VMXNET3_DEVICE_F_LINK_UP;
+    }
+
   /* Get the mac address */
   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_MACL);
   clib_memcpy (vd->mac_addr, &ret, 4);
@@ -392,11 +428,11 @@ vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
 }
 
 static void
-vmxnet3_irq_handler (vlib_pci_dev_handle_t h, u16 line)
+vmxnet3_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
 {
   vnet_main_t *vnm = vnet_get_main ();
   vmxnet3_main_t *vmxm = &vmxnet3_main;
-  uword pd = vlib_pci_get_private_data (h);
+  uword pd = vlib_pci_get_private_data (vm, h);
   vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, pd);
   u16 qid = line;
 
@@ -404,6 +440,31 @@ vmxnet3_irq_handler (vlib_pci_dev_handle_t h, u16 line)
     vnet_device_input_set_interrupt_pending (vnm, vd->hw_if_index, qid);
 }
 
+static void
+vmxnet3_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  vmxnet3_main_t *vmxm = &vmxnet3_main;
+  uword pd = vlib_pci_get_private_data (vm, h);
+  vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, pd);
+  u32 ret;
+
+  vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
+  ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
+  if (ret & 1)
+    {
+      vd->flags |= VMXNET3_DEVICE_F_LINK_UP;
+      vd->link_speed = ret >> 16;
+      vnet_hw_interface_set_flags (vnm, vd->hw_if_index,
+                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
+    }
+  else
+    {
+      vd->flags &= ~VMXNET3_DEVICE_F_LINK_UP;
+      vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
+    }
+}
+
 static u8
 vmxnet3_queue_size_valid (u16 qsz)
 {
@@ -460,7 +521,7 @@ vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
     vd->flags |= VMXNET3_DEVICE_F_ELOG;
 
   if ((error =
-       vlib_pci_device_open (&args->addr, vmxnet3_pci_device_ids, &h)))
+       vlib_pci_device_open (vm, &args->addr, vmxnet3_pci_device_ids, &h)))
     {
       pool_put (vmxm->devices, vd);
       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
@@ -471,25 +532,29 @@ vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
     }
   vd->pci_dev_handle = h;
 
-  vlib_pci_set_private_data (h, vd->dev_instance);
+  vlib_pci_set_private_data (vm, h, vd->dev_instance);
 
-  if ((error = vlib_pci_bus_master_enable (h)))
+  if ((error = vlib_pci_bus_master_enable (vm, h)))
     goto error;
 
-  if ((error = vlib_pci_map_region (h, 0, (void **) &vd->bar[0])))
+  if ((error = vlib_pci_map_region (vm, h, 0, (void **) &vd->bar[0])))
     goto error;
 
-  if ((error = vlib_pci_map_region (h, 1, (void **) &vd->bar[1])))
+  if ((error = vlib_pci_map_region (vm, h, 1, (void **) &vd->bar[1])))
     goto error;
 
-  if ((error = vlib_pci_register_msix_handler (h, 0, 1,
-                                              &vmxnet3_irq_handler)))
+  if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
+                                              &vmxnet3_irq_0_handler)))
     goto error;
 
-  if ((error = vlib_pci_enable_msix_irq (h, 0, 1)))
+  if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
+                                              &vmxnet3_irq_1_handler)))
     goto error;
 
-  if ((error = vlib_pci_intr_enable (h)))
+  if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
+    goto error;
+
+  if ((error = vlib_pci_intr_enable (vm, h)))
     goto error;
 
   if ((error = vmxnet3_device_init (vm, vd, args)))
@@ -512,6 +577,11 @@ vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
   vnet_hw_interface_set_input_node (vnm, vd->hw_if_index,
                                    vmxnet3_input_node.index);
   vnet_hw_interface_assign_rx_thread (vnm, vd->hw_if_index, 0, ~0);
+  if (vd->flags & VMXNET3_DEVICE_F_LINK_UP)
+    vnet_hw_interface_set_flags (vnm, vd->hw_if_index,
+                                VNET_HW_INTERFACE_FLAG_LINK_UP);
+  else
+    vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
   return;
 
 error:
@@ -541,7 +611,7 @@ vmxnet3_delete_if (vlib_main_t * vm, vmxnet3_device_t * vd)
       ethernet_delete_interface (vnm, vd->hw_if_index);
     }
 
-  vlib_pci_device_close (vd->pci_dev_handle);
+  vlib_pci_device_close (vm, vd->pci_dev_handle);
 
   /* *INDENT-OFF* */
   vec_foreach_index (i, vd->rxqs)
@@ -555,15 +625,9 @@ vmxnet3_delete_if (vlib_main_t * vm, vmxnet3_device_t * vd)
          vmxnet3_rx_ring *ring;
 
          ring = &rxq->rx_ring[rid];
-         desc_idx = ring->consume;
-         while (ring->fill)
-           {
-             desc_idx &= mask;
-             bi = ring->bufs[desc_idx];
-             vlib_buffer_free_no_next (vm, &bi, 1);
-             ring->fill--;
-             desc_idx++;
-           }
+         desc_idx = (ring->consume + 1) & mask;
+         vlib_buffer_free_from_ring (vm, ring->bufs, desc_idx, rxq->size,
+                                     ring->fill);
          vec_free (ring->bufs);
          vlib_physmem_free (vm, vmxm->physmem_region, rxq->rx_desc[rid]);
        }