pci: set PCI memory enable before mapping PCI BAR 77/29577/5
authorMohammed Hawari <mohammed@hawari.fr>
Wed, 21 Oct 2020 14:41:30 +0000 (16:41 +0200)
committerDamjan Marion <dmarion@me.com>
Thu, 22 Oct 2020 15:48:14 +0000 (15:48 +0000)
This change mitigates software faults issued by some versions of the
linux kernel vfio-pci driver when VF PCI BARs are used without setting
the memory enable bit in the PCI configuration. This problem is
mentionned in https://lkml.org/lkml/2020/6/25/628

Change-Id: Idc177be4a5adb6ee467b4dd8f055f133ff267fe1
Type: improvement
Signed-off-by: Mohammed Hawari <mohammed@hawari.fr>
src/vlib/linux/pci.c

index 6078be2..1caf0c9 100644 (file)
@@ -1134,9 +1134,21 @@ vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
   clib_error_t *error;
   int flags = MAP_SHARED;
   u64 size = 0, offset = 0;
+  u16 command;
 
   pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
 
+  if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
+    return error;
+
+  if (!(command & PCI_COMMAND_MEMORY))
+    {
+      pci_log_debug (vm, p, "setting memory enable bit");
+      command |= PCI_COMMAND_MEMORY;
+      if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
+       return error;
+    }
+
   if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
     return error;