aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlib
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2018-03-05 14:08:33 +0100
committerDamjan Marion <dmarion.lists@gmail.com>2018-03-05 14:40:57 +0000
commitf313b747ad4313a5cadfdb6aa7eb66ce8bf7958f (patch)
tree26164d3a067fcbeafa4d1cba28309bebd1143225 /src/vlib
parent9e09ff394ac0e731b5b33caf4d0cddff8de570b4 (diff)
vlib: add functions to dynamically open/close PCI device
Old code was only allowing PCIdevices to be scanned during startup, now driver can open and close device without restart of vpp. Change-Id: I1a06511e3f16f896101b43dac2bad420b6e6c35e Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vlib')
-rw-r--r--src/vlib/linux/pci.c124
-rw-r--r--src/vlib/pci/pci.h5
2 files changed, 126 insertions, 3 deletions
diff --git a/src/vlib/linux/pci.c b/src/vlib/linux/pci.c
index bc3e15ea00d..910f8c2f9cc 100644
--- a/src/vlib/linux/pci.c
+++ b/src/vlib/linux/pci.c
@@ -104,9 +104,6 @@ typedef struct
/* Minor device for uio device. */
u32 uio_minor;
- /* Index given by clib_file_add. */
- u32 clib_file_index;
-
/* Interrupt handlers */
linux_pci_irq_t intx_irq;
linux_pci_irq_t *msix_irqs;
@@ -852,6 +849,7 @@ add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
{
linux_pci_main_t *lpm = &linux_pci_main;
struct vfio_device_info device_info = { 0 };
+ struct vfio_region_info reg = { 0 };
clib_error_t *err = 0;
u8 *s = 0;
@@ -875,6 +873,17 @@ add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
goto error;
}
+ reg.argsz = sizeof (struct vfio_region_info);
+ reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
+ if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
+ {
+ err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
+ format_vlib_pci_addr, &di->addr);
+ goto error;
+ }
+ p->config_offset = reg.offset;
+ p->config_fd = p->fd;
+
/* reset if device supports it */
if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
@@ -1019,6 +1028,115 @@ vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr,
return (vlib_pci_map_region_int (h, resource, addr, result));
}
+clib_error_t *
+vlib_pci_device_open (vlib_pci_addr_t * addr,
+ pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
+{
+ linux_pci_main_t *lpm = &linux_pci_main;
+ vlib_pci_device_info_t *di;
+ linux_pci_device_t *p;
+ clib_error_t *err = 0;
+ pci_device_id_t *i;
+
+ di = vlib_pci_get_device_info (addr, &err);
+
+ if (err)
+ return err;
+ for (i = ids; i->vendor_id != 0; i++)
+ if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
+ break;
+
+ if (i->vendor_id == 0)
+ return clib_error_return (0, "Wrong vendor or device id");
+
+ pool_get (lpm->linux_pci_devices, p);
+ p->handle = p - lpm->linux_pci_devices;
+ p->intx_irq.fd = -1;
+
+ if (di->iommu_group != -1)
+ err = add_device_vfio (p, di, 0);
+ else
+ err = add_device_uio (p, di, 0);
+ if (err)
+ goto error;
+
+ *handle = p->handle;
+
+error:
+ vlib_pci_free_device_info (di);
+ if (err)
+ {
+ memset (p, 0, sizeof (linux_pci_device_t));
+ pool_put (lpm->linux_pci_devices, p);
+ }
+
+ return err;
+}
+
+void
+vlib_pci_device_close (vlib_pci_dev_handle_t h)
+{
+ linux_pci_main_t *lpm = &linux_pci_main;
+ linux_pci_device_t *p = linux_pci_get_device (h);
+ linux_pci_irq_t *irq;
+ linux_pci_region_t *res;
+ clib_error_t *err = 0;
+
+ if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
+ {
+ irq = &p->intx_irq;
+ clib_file_del_by_index (&file_main, irq->clib_file_index);
+ close (p->config_fd);
+ }
+ else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
+ {
+ irq = &p->intx_irq;
+ /* close INTx irqs */
+ if (irq->fd != -1)
+ {
+ err = vfio_set_irqs (p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
+ VFIO_IRQ_SET_ACTION_TRIGGER, 0);
+ clib_error_free (err);
+ clib_file_del_by_index (&file_main, irq->clib_file_index);
+ close (irq->fd);
+ }
+
+ /* close MSI-X irqs */
+ if (vec_len (p->msix_irqs))
+ {
+ err = vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
+ VFIO_IRQ_SET_ACTION_TRIGGER, 0);
+ clib_error_free (err);
+ /* *INDENT-OFF* */
+ vec_foreach (irq, p->msix_irqs)
+ {
+ if (irq->fd == -1)
+ continue;
+ clib_file_del_by_index (&file_main, irq->clib_file_index);
+ close (irq->fd);
+ }
+ /* *INDENT-ON* */
+ vec_free (p->msix_irqs);
+ }
+ }
+
+ /* *INDENT-OFF* */
+ vec_foreach (res, p->regions)
+ {
+ if (res->size == 0)
+ continue;
+ munmap (res->addr, res->size);
+ if (res->fd != -1)
+ close (res->fd);
+ }
+ /* *INDENT-ON* */
+ vec_free (p->regions);
+
+ close (p->fd);
+ memset (p, 0, sizeof (linux_pci_device_t));
+ pool_put (lpm->linux_pci_devices, p);
+}
+
void
init_device_from_registered (vlib_pci_device_info_t * di)
{
diff --git a/src/vlib/pci/pci.h b/src/vlib/pci/pci.h
index f4d3674b30d..144eb3f598e 100644
--- a/src/vlib/pci/pci.h
+++ b/src/vlib/pci/pci.h
@@ -249,6 +249,11 @@ vlib_pci_bus_master_enable (vlib_pci_dev_handle_t h)
return vlib_pci_write_config_u16 (h, 4, &command);
}
+clib_error_t *vlib_pci_device_open (vlib_pci_addr_t * addr,
+ pci_device_id_t ids[],
+ vlib_pci_dev_handle_t * handle);
+void vlib_pci_device_close (vlib_pci_dev_handle_t h);
+
clib_error_t *vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource,
void **result);