static struct ifpga_rawdev ifpga_rawdevices[IFPGA_RAWDEV_NUM];
-static int ifpga_monitor_start;
+static int ifpga_monitor_refcnt;
static pthread_t ifpga_monitor_start_thread;
-#define IFPGA_MAX_IRQ 12
-/* 0 for FME interrupt, others are reserved for AFU irq */
-static struct rte_intr_handle *ifpga_irq_handle[IFPGA_MAX_IRQ];
-
static struct ifpga_rawdev *
ifpga_rawdev_allocate(struct rte_rawdev *rawdev);
static int set_surprise_link_check_aer(
{
struct ifpga_rawdev *dev;
uint16_t dev_id;
+ int i = 0;
dev = ifpga_rawdev_get(rawdev);
if (dev != NULL) {
dev = &ifpga_rawdevices[dev_id];
dev->rawdev = rawdev;
dev->dev_id = dev_id;
+ for (i = 0; i < IFPGA_MAX_IRQ; i++)
+ dev->intr_handle[i] = NULL;
+ dev->poll_enabled = 0;
return dev;
}
return 0;
}
-static int ifpga_rawdev_fill_info(struct ifpga_rawdev *ifpga_dev,
- const char *bdf)
+
+static int ifpga_rawdev_fill_info(struct ifpga_rawdev *ifpga_dev)
{
- char path[1024] = "/sys/bus/pci/devices/0000:";
+ struct opae_adapter *adapter = NULL;
+ char path[1024] = "/sys/bus/pci/devices/";
char link[1024], link1[1024];
char dir[1024] = "/sys/devices/";
char *c;
int ret;
- char sub_brg_bdf[4][16];
+ char sub_brg_bdf[4][16] = {{0}};
int point;
DIR *dp = NULL;
struct dirent *entry;
unsigned int dom, bus, dev;
int func;
- uint32_t dev_id, vendor_id;
+ uint32_t dev_id = 0;
+ uint32_t vendor_id = 0;
- strlcat(path, bdf, sizeof(path));
+ adapter = ifpga_dev ? ifpga_rawdev_get_priv(ifpga_dev->rawdev) : NULL;
+ if (!adapter)
+ return -ENODEV;
+
+ strlcat(path, adapter->name, sizeof(path));
memset(link, 0, sizeof(link));
memset(link1, 0, sizeof(link1));
ret = readlink(path, link, (sizeof(link)-1));
/* monitor temperature sensors */
if (!strcmp(sensor->name, "Board Temperature") ||
!strcmp(sensor->name, "FPGA Die Temperature")) {
- IFPGA_RAWDEV_PMD_INFO("read sensor %s %d %d %d\n",
+ IFPGA_RAWDEV_PMD_DEBUG("read sensor %s %d %d %d\n",
sensor->name, value, sensor->high_warn,
sensor->high_fatal);
bool enable = 0;
uint32_t aer_new0, aer_new1;
- if (!ifpga_rdev) {
+ if (!ifpga_rdev || !ifpga_rdev->rawdev) {
printf("\n device does not exist\n");
return -EFAULT;
}
int gsd_enable, ret;
#define MS 1000
- while (__atomic_load_n(&ifpga_monitor_start, __ATOMIC_RELAXED)) {
+ while (__atomic_load_n(&ifpga_monitor_refcnt, __ATOMIC_RELAXED)) {
gsd_enable = 0;
for (i = 0; i < IFPGA_RAWDEV_NUM; i++) {
ifpga_rdev = &ifpga_rawdevices[i];
- if (ifpga_rdev->rawdev) {
+ if (ifpga_rdev->poll_enabled) {
ret = set_surprise_link_check_aer(ifpga_rdev,
gsd_enable);
if (ret == 1 && !gsd_enable) {
}
static int
-ifpga_monitor_start_func(void)
+ifpga_monitor_start_func(struct ifpga_rawdev *dev)
{
int ret;
- if (!__atomic_load_n(&ifpga_monitor_start, __ATOMIC_RELAXED)) {
+ if (!dev)
+ return -ENODEV;
+
+ ret = ifpga_rawdev_fill_info(dev);
+ if (ret)
+ return ret;
+
+ dev->poll_enabled = 1;
+
+ if (!__atomic_fetch_add(&ifpga_monitor_refcnt, 1, __ATOMIC_RELAXED)) {
ret = rte_ctrl_thread_create(&ifpga_monitor_start_thread,
"ifpga-monitor", NULL,
ifpga_rawdev_gsd_handle, NULL);
if (ret != 0) {
+ ifpga_monitor_start_thread = 0;
IFPGA_RAWDEV_PMD_ERR(
"Fail to create ifpga monitor thread");
return -1;
}
- __atomic_store_n(&ifpga_monitor_start, 1, __ATOMIC_RELAXED);
}
return 0;
}
+
static int
-ifpga_monitor_stop_func(void)
+ifpga_monitor_stop_func(struct ifpga_rawdev *dev)
{
int ret;
- if (__atomic_load_n(&ifpga_monitor_start, __ATOMIC_RELAXED)) {
- __atomic_store_n(&ifpga_monitor_start, 0, __ATOMIC_RELAXED);
+ if (!dev || !dev->poll_enabled)
+ return 0;
+
+ dev->poll_enabled = 0;
+ if (!__atomic_sub_fetch(&ifpga_monitor_refcnt, 1, __ATOMIC_RELAXED) &&
+ ifpga_monitor_start_thread) {
ret = pthread_cancel(ifpga_monitor_start_thread);
if (ret)
IFPGA_RAWDEV_PMD_ERR("Can't cancel the thread");
struct opae_adapter *adapter;
if (dev) {
+ ifpga_monitor_stop_func(ifpga_rawdev_get(dev));
adapter = ifpga_rawdev_get_priv(dev);
if (adapter) {
opae_adapter_destroy(adapter);
}
int
-ifpga_unregister_msix_irq(enum ifpga_irq_type type,
+ifpga_unregister_msix_irq(struct ifpga_rawdev *dev, enum ifpga_irq_type type,
int vec_start, rte_intr_callback_fn handler, void *arg)
{
- struct rte_intr_handle *intr_handle;
- int rc, i;
+ struct rte_intr_handle **intr_handle;
+ int rc = 0;
+ int i = vec_start + 1;
+
+ if (!dev)
+ return -ENODEV;
if (type == IFPGA_FME_IRQ)
- intr_handle = ifpga_irq_handle[0];
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[0];
else if (type == IFPGA_AFU_IRQ)
- intr_handle = ifpga_irq_handle[vec_start + 1];
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[i];
else
- return 0;
+ return -EINVAL;
- rte_intr_efd_disable(intr_handle);
+ if ((*intr_handle) == NULL) {
+ IFPGA_RAWDEV_PMD_ERR("%s interrupt %d not registered\n",
+ type == IFPGA_FME_IRQ ? "FME" : "AFU",
+ type == IFPGA_FME_IRQ ? 0 : vec_start);
+ return -ENOENT;
+ }
- rc = rte_intr_callback_unregister(intr_handle, handler, arg);
+ rte_intr_efd_disable(*intr_handle);
+
+ rc = rte_intr_callback_unregister(*intr_handle, handler, arg);
+ if (rc < 0) {
+ IFPGA_RAWDEV_PMD_ERR("Failed to unregister %s interrupt %d\n",
+ type == IFPGA_FME_IRQ ? "FME" : "AFU",
+ type == IFPGA_FME_IRQ ? 0 : vec_start);
+ } else {
+ rte_intr_instance_free(*intr_handle);
+ *intr_handle = NULL;
+ }
- for (i = 0; i < IFPGA_MAX_IRQ; i++)
- rte_intr_instance_free(ifpga_irq_handle[i]);
return rc;
}
int
-ifpga_register_msix_irq(struct rte_rawdev *dev, int port_id,
+ifpga_register_msix_irq(struct ifpga_rawdev *dev, int port_id,
enum ifpga_irq_type type, int vec_start, int count,
rte_intr_callback_fn handler, const char *name,
void *arg)
{
int ret;
- struct rte_intr_handle *intr_handle;
+ struct rte_intr_handle **intr_handle;
struct opae_adapter *adapter;
struct opae_manager *mgr;
struct opae_accelerator *acc;
int *intr_efds = NULL, nb_intr, i;
- for (i = 0; i < IFPGA_MAX_IRQ; i++) {
- ifpga_irq_handle[i] =
- rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
- if (ifpga_irq_handle[i] == NULL)
- return -ENOMEM;
- }
+ if (!dev || !dev->rawdev)
+ return -ENODEV;
- adapter = ifpga_rawdev_get_priv(dev);
+ adapter = ifpga_rawdev_get_priv(dev->rawdev);
if (!adapter)
return -ENODEV;
return -ENODEV;
if (type == IFPGA_FME_IRQ) {
- intr_handle = ifpga_irq_handle[0];
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[0];
count = 1;
} else if (type == IFPGA_AFU_IRQ) {
- intr_handle = ifpga_irq_handle[vec_start + 1];
+ i = vec_start + 1;
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[i];
} else {
return -EINVAL;
}
- if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_VFIO_MSIX))
+ if (*intr_handle)
+ return -EBUSY;
+
+ *intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
+ if (!(*intr_handle))
+ return -ENOMEM;
+
+ if (rte_intr_type_set(*intr_handle, RTE_INTR_HANDLE_VFIO_MSIX))
return -rte_errno;
- ret = rte_intr_efd_enable(intr_handle, count);
+ ret = rte_intr_efd_enable(*intr_handle, count);
if (ret)
return -ENODEV;
- if (rte_intr_fd_set(intr_handle,
- rte_intr_efds_index_get(intr_handle, 0)))
+ if (rte_intr_fd_set(*intr_handle,
+ rte_intr_efds_index_get(*intr_handle, 0)))
return -rte_errno;
IFPGA_RAWDEV_PMD_DEBUG("register %s irq, vfio_fd=%d, fd=%d\n",
- name, rte_intr_dev_fd_get(intr_handle),
- rte_intr_fd_get(intr_handle));
+ name, rte_intr_dev_fd_get(*intr_handle),
+ rte_intr_fd_get(*intr_handle));
if (type == IFPGA_FME_IRQ) {
struct fpga_fme_err_irq_set err_irq_set;
- err_irq_set.evtfd = rte_intr_efds_index_get(intr_handle,
+ err_irq_set.evtfd = rte_intr_efds_index_get(*intr_handle,
0);
ret = opae_manager_ifpga_set_err_irq(mgr, &err_irq_set);
if (!acc)
return -EINVAL;
- nb_intr = rte_intr_nb_intr_get(intr_handle);
+ nb_intr = rte_intr_nb_intr_get(*intr_handle);
intr_efds = calloc(nb_intr, sizeof(int));
if (!intr_efds)
return -ENOMEM;
for (i = 0; i < nb_intr; i++)
- intr_efds[i] = rte_intr_efds_index_get(intr_handle, i);
+ intr_efds[i] = rte_intr_efds_index_get(*intr_handle, i);
ret = opae_acc_set_irq(acc, vec_start, count, intr_efds);
if (ret) {
}
/* register interrupt handler using DPDK API */
- ret = rte_intr_callback_register(intr_handle,
+ ret = rte_intr_callback_register(*intr_handle,
handler, (void *)arg);
if (ret) {
free(intr_efds);
IFPGA_RAWDEV_PMD_INFO("this is a PF function");
}
- ret = ifpga_register_msix_irq(rawdev, 0, IFPGA_FME_IRQ, 0, 0,
+ ret = ifpga_register_msix_irq(dev, 0, IFPGA_FME_IRQ, 0, 0,
fme_interrupt_handler, "fme_irq", mgr);
if (ret)
goto free_adapter_data;
+ ret = ifpga_monitor_start_func(dev);
+ if (ret)
+ goto free_adapter_data;
+
return ret;
free_adapter_data:
if (!mgr)
return -ENODEV;
- if (ifpga_unregister_msix_irq(IFPGA_FME_IRQ, 0,
+ if (ifpga_unregister_msix_irq(dev, IFPGA_FME_IRQ, 0,
fme_interrupt_handler, mgr) < 0)
return -EINVAL;
static int
ifpga_rawdev_pci_remove(struct rte_pci_device *pci_dev)
{
- ifpga_monitor_stop_func();
+ IFPGA_RAWDEV_PMD_INFO("remove pci_dev %s", pci_dev->device.name);
return ifpga_rawdev_destroy(pci_dev);
}
struct ifpga_rawdev *ifpga_dev;
int port;
char *name = NULL;
- const char *bdf;
char dev_name[RTE_RAWDEV_NAME_MAX_LEN];
int ret = -1;
ifpga_dev = ifpga_rawdev_get(rawdev);
if (!ifpga_dev)
goto end;
- bdf = name;
- ifpga_rawdev_fill_info(ifpga_dev, bdf);
-
- ifpga_monitor_start_func();
memset(dev_name, 0, sizeof(dev_name));
snprintf(dev_name, RTE_RAWDEV_NAME_MAX_LEN, "%d|%s",