#include "ifpga_rawdev.h"
#include "ipn3ke_rawdev_api.h"
-#define RTE_PCI_EXT_CAP_ID_ERR 0x01 /* Advanced Error Reporting */
-#define RTE_PCI_CFG_SPACE_SIZE 256
-#define RTE_PCI_CFG_SPACE_EXP_SIZE 4096
-#define RTE_PCI_EXT_CAP_ID(header) (int)(header & 0x0000ffff)
-#define RTE_PCI_EXT_CAP_NEXT(header) ((header >> 20) & 0xffc)
-
-int ifpga_rawdev_logtype;
-
#define PCI_VENDOR_ID_INTEL 0x8086
/* PCI Device ID */
#define PCIE_DEVICE_ID_PF_INT_5_X 0xBCBD
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;
static struct ifpga_rawdev *
static int set_surprise_link_check_aer(
struct ifpga_rawdev *ifpga_rdev, int force_disable);
static int ifpga_pci_find_next_ext_capability(unsigned int fd,
- int start, int cap);
-static int ifpga_pci_find_ext_capability(unsigned int fd, int cap);
+ int start, uint32_t cap);
+static int ifpga_pci_find_ext_capability(unsigned int fd, uint32_t cap);
struct ifpga_rawdev *
ifpga_rawdev_get(const struct rte_rawdev *rawdev)
{
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;
}
-static int ifpga_pci_find_next_ext_capability(unsigned int fd,
-int start, int cap)
+static int
+ifpga_pci_find_next_ext_capability(unsigned int fd, int start, uint32_t cap)
{
uint32_t header;
int ttl;
return 0;
}
-static int ifpga_pci_find_ext_capability(unsigned int fd, int cap)
+static int
+ifpga_pci_find_ext_capability(unsigned int fd, uint32_t cap)
{
return ifpga_pci_find_next_ext_capability(fd, 0, cap);
}
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));
- if (ret == -1)
+ if ((ret < 0) || ((unsigned int)ret > (sizeof(link)-1)))
return -1;
+ link[ret] = 0; /* terminate string with null character */
strlcpy(link1, link, sizeof(link1));
memset(ifpga_dev->parent_bdf, 0, 16);
point = strlen(link);
if (!mgr)
return -ENODEV;
- opae_mgr_for_each_sensor(sensor) {
+ opae_mgr_for_each_sensor(mgr, sensor) {
if (!(sensor->flags & OPAE_SENSOR_VALID))
goto fail;
goto fail;
if (value == 0xdeadbeef) {
- IFPGA_RAWDEV_PMD_ERR("sensor %s is invalid value %x\n",
- sensor->name, value);
+ IFPGA_RAWDEV_PMD_ERR("dev_id %d sensor %s value %x\n",
+ raw_dev->dev_id, sensor->name, value);
continue;
}
/* 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);
if (HIGH_WARN(sensor, value) ||
LOW_WARN(sensor, value)) {
- IFPGA_RAWDEV_PMD_INFO("%s reach theshold %d\n",
+ IFPGA_RAWDEV_PMD_INFO("%s reach threshold %d\n",
sensor->name, value);
*gsd_start = true;
break;
/* monitor 12V AUX sensor */
if (!strcmp(sensor->name, "12V AUX Voltage")) {
if (value < AUX_VOLTAGE_WARN) {
- IFPGA_RAWDEV_PMD_INFO("%s reach theshold %d\n",
- sensor->name, value);
+ IFPGA_RAWDEV_PMD_INFO(
+ "%s reach threshold %d mV\n",
+ sensor->name, value);
*gsd_start = true;
break;
}
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;
}
pos = ifpga_pci_find_ext_capability(fd, RTE_PCI_EXT_CAP_ID_ERR);
if (!pos)
goto end;
- /* save previout ECAP_AER+0x08 */
+ /* save previous ECAP_AER+0x08 */
ret = pread(fd, &data, sizeof(data), pos+0x08);
if (ret == -1)
goto end;
ifpga_rdev->aer_old[0] = data;
- /* save previout ECAP_AER+0x14 */
+ /* save previous ECAP_AER+0x14 */
ret = pread(fd, &data, sizeof(data), pos+0x14);
if (ret == -1)
goto end;
int gsd_enable, ret;
#define MS 1000
- while (1) {
+ 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 (ifpga_monitor_start == 0) {
- ret = pthread_create(&ifpga_monitor_start_thread,
- NULL,
- ifpga_rawdev_gsd_handle, NULL);
- if (ret) {
+ 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 nonitor thread");
+ "Fail to create ifpga monitor thread");
return -1;
}
- ifpga_monitor_start = 1;
}
return 0;
}
+
static int
-ifpga_monitor_stop_func(void)
+ifpga_monitor_stop_func(struct ifpga_rawdev *dev)
{
int ret;
- if (ifpga_monitor_start == 1) {
+ 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");
if (ret)
IFPGA_RAWDEV_PMD_ERR("Can't join the thread");
- ifpga_monitor_start = 0;
-
return ret;
}
return 0;
}
-static void
+static int
ifpga_rawdev_info_get(struct rte_rawdev *dev,
- rte_rawdev_obj_t dev_info)
+ rte_rawdev_obj_t dev_info,
+ size_t dev_info_size)
{
struct opae_adapter *adapter;
struct opae_accelerator *acc;
IFPGA_RAWDEV_PMD_FUNC_TRACE();
- if (!dev_info) {
+ if (!dev_info || dev_info_size != sizeof(*afu_dev)) {
IFPGA_RAWDEV_PMD_ERR("Invalid request");
- return;
+ return -EINVAL;
}
adapter = ifpga_rawdev_get_priv(dev);
if (!adapter)
- return;
+ return -ENOENT;
afu_dev = dev_info;
afu_dev->rawdev = dev;
if (ifpga_fill_afu_dev(acc, afu_dev)) {
IFPGA_RAWDEV_PMD_ERR("cannot get info\n");
- return;
+ return -ENOENT;
}
}
/* get LineSide BAR Index */
if (opae_manager_get_eth_group_region_info(mgr, 0,
&opae_lside_eth_info)) {
- return;
+ return -ENOENT;
}
lside_bar_idx = opae_lside_eth_info.mem_idx;
/* get NICSide BAR Index */
if (opae_manager_get_eth_group_region_info(mgr, 1,
&opae_nside_eth_info)) {
- return;
+ return -ENOENT;
}
nside_bar_idx = opae_nside_eth_info.mem_idx;
if (lside_bar_idx >= PCI_MAX_RESOURCE ||
nside_bar_idx >= PCI_MAX_RESOURCE ||
lside_bar_idx == nside_bar_idx)
- return;
+ return -ENOENT;
/* fill LineSide BAR Index */
afu_dev->mem_resource[lside_bar_idx].phys_addr =
afu_dev->mem_resource[nside_bar_idx].addr =
opae_nside_eth_info.addr;
}
+ return 0;
}
static int
ifpga_rawdev_configure(const struct rte_rawdev *dev,
- rte_rawdev_obj_t config)
+ rte_rawdev_obj_t config,
+ size_t config_size __rte_unused)
{
IFPGA_RAWDEV_PMD_FUNC_TRACE();
static int
ifpga_rawdev_close(struct rte_rawdev *dev)
{
+ 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);
+ opae_adapter_data_free(adapter->data);
+ }
+ }
+
return dev ? 0:1;
}
int file_fd;
int ret = 0;
ssize_t buffer_size;
- void *buffer;
+ void *buffer, *buf_to_free;
u64 pr_error;
if (!file_name)
ret = -ENOMEM;
goto close_fd;
}
+ buf_to_free = buffer;
/*read the raw data*/
if (buffer_size != read(file_fd, (void *)buffer, buffer_size)) {
}
free_buffer:
- if (buffer)
- rte_free(buffer);
+ rte_free(buf_to_free);
close_fd:
close(file_fd);
file_fd = 0;
fme_err_handle_catfatal_error(mgr);
}
-static struct rte_intr_handle fme_intr_handle;
+int
+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 = 0;
+ int i = vec_start + 1;
-static int ifpga_register_fme_interrupt(struct opae_manager *mgr)
+ if (!dev)
+ return -ENODEV;
+
+ if (type == IFPGA_FME_IRQ)
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[0];
+ else if (type == IFPGA_AFU_IRQ)
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[i];
+ else
+ return -EINVAL;
+
+ 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;
+ }
+
+ 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;
+ }
+
+ return rc;
+}
+
+int
+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 fpga_fme_err_irq_set err_irq_set;
+ 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;
- fme_intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
+ if (!dev || !dev->rawdev)
+ return -ENODEV;
- ret = rte_intr_efd_enable(&fme_intr_handle, 1);
- if (ret)
+ adapter = ifpga_rawdev_get_priv(dev->rawdev);
+ if (!adapter)
+ return -ENODEV;
+
+ mgr = opae_adapter_get_mgr(adapter);
+ if (!mgr)
+ return -ENODEV;
+
+ if (type == IFPGA_FME_IRQ) {
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[0];
+ count = 1;
+ } else if (type == IFPGA_AFU_IRQ) {
+ i = vec_start + 1;
+ intr_handle = (struct rte_intr_handle **)&dev->intr_handle[i];
+ } else {
return -EINVAL;
+ }
- fme_intr_handle.fd = fme_intr_handle.efds[0];
+ if (*intr_handle)
+ return -EBUSY;
- IFPGA_RAWDEV_PMD_DEBUG("vfio_dev_fd=%d, efd=%d, fd=%d\n",
- fme_intr_handle.vfio_dev_fd,
- fme_intr_handle.efds[0], fme_intr_handle.fd);
+ *intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
+ if (!(*intr_handle))
+ return -ENOMEM;
- err_irq_set.evtfd = fme_intr_handle.efds[0];
- ret = opae_manager_ifpga_set_err_irq(mgr, &err_irq_set);
- if (ret)
- return -EINVAL;
+ if (rte_intr_type_set(*intr_handle, RTE_INTR_HANDLE_VFIO_MSIX))
+ return -rte_errno;
- /* register FME interrupt using DPDK API */
- ret = rte_intr_callback_register(&fme_intr_handle,
- fme_interrupt_handler,
- (void *)mgr);
+ ret = rte_intr_efd_enable(*intr_handle, count);
if (ret)
- return -EINVAL;
+ return -ENODEV;
- IFPGA_RAWDEV_PMD_INFO("success register fme interrupt\n");
+ if (rte_intr_fd_set(*intr_handle,
+ rte_intr_efds_index_get(*intr_handle, 0)))
+ return -rte_errno;
- return 0;
-}
+ 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));
-static int
-ifpga_unregister_fme_interrupt(struct opae_manager *mgr)
-{
- rte_intr_efd_disable(&fme_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,
+ 0);
- return rte_intr_callback_unregister(&fme_intr_handle,
- fme_interrupt_handler,
- (void *)mgr);
+ ret = opae_manager_ifpga_set_err_irq(mgr, &err_irq_set);
+ if (ret)
+ return -EINVAL;
+ } else if (type == IFPGA_AFU_IRQ) {
+ acc = opae_adapter_get_acc(adapter, port_id);
+ if (!acc)
+ return -EINVAL;
+
+ 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);
+
+ ret = opae_acc_set_irq(acc, vec_start, count, intr_efds);
+ if (ret) {
+ free(intr_efds);
+ return -EINVAL;
+ }
+ }
+
+ /* register interrupt handler using DPDK API */
+ ret = rte_intr_callback_register(*intr_handle,
+ handler, (void *)arg);
+ if (ret) {
+ free(intr_efds);
+ return -EINVAL;
+ }
+
+ IFPGA_RAWDEV_PMD_INFO("success register %s interrupt\n", name);
+
+ free(intr_efds);
+ return 0;
}
static int
}
memset(name, 0, sizeof(name));
- snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%02x:%02x.%x",
+ snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, IFPGA_RAWDEV_NAME_FMT,
pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
IFPGA_RAWDEV_PMD_INFO("Init %s on NUMA node %d", name, rte_socket_id());
}
data->device_id = pci_dev->id.device_id;
data->vendor_id = pci_dev->id.vendor_id;
- data->vfio_dev_fd = pci_dev->intr_handle.vfio_dev_fd;
+ data->bus = pci_dev->addr.bus;
+ data->devid = pci_dev->addr.devid;
+ data->function = pci_dev->addr.function;
+ data->vfio_dev_fd = rte_intr_dev_fd_get(pci_dev->intr_handle);
adapter = rawdev->dev_private;
/* create a opae_adapter based on above device data */
IFPGA_RAWDEV_PMD_INFO("this is a PF function");
}
- ret = ifpga_register_fme_interrupt(mgr);
+ 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;
char name[RTE_RAWDEV_NAME_MAX_LEN];
struct opae_adapter *adapter;
struct opae_manager *mgr;
+ struct ifpga_rawdev *dev;
if (!pci_dev) {
IFPGA_RAWDEV_PMD_ERR("Invalid pci_dev of the device!");
}
memset(name, 0, sizeof(name));
- snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%x:%02x.%x",
+ snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, IFPGA_RAWDEV_NAME_FMT,
pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
IFPGA_RAWDEV_PMD_INFO("Closing %s on NUMA node %d",
IFPGA_RAWDEV_PMD_ERR("Invalid device name (%s)", name);
return -EINVAL;
}
+ dev = ifpga_rawdev_get(rawdev);
+ if (dev)
+ dev->rawdev = NULL;
adapter = ifpga_rawdev_get_priv(rawdev);
if (!adapter)
if (!mgr)
return -ENODEV;
- if (ifpga_unregister_fme_interrupt(mgr))
+ if (ifpga_unregister_msix_irq(dev, IFPGA_FME_IRQ, 0,
+ fme_interrupt_handler, mgr) < 0)
return -EINVAL;
- opae_adapter_data_free(adapter->data);
- opae_adapter_free(adapter);
-
/* rte_rawdev_close is called by pmd_release */
ret = rte_rawdev_pmd_release(rawdev);
if (ret)
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);
}
RTE_PMD_REGISTER_PCI(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
RTE_PMD_REGISTER_PCI_TABLE(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
RTE_PMD_REGISTER_KMOD_DEP(ifpga_rawdev_pci_driver, "* igb_uio | uio_pci_generic | vfio-pci");
-
-RTE_INIT(ifpga_rawdev_init_log)
-{
- ifpga_rawdev_logtype = rte_log_register("driver.raw.init");
- if (ifpga_rawdev_logtype >= 0)
- rte_log_set_level(ifpga_rawdev_logtype, RTE_LOG_NOTICE);
-}
+RTE_LOG_REGISTER_DEFAULT(ifpga_rawdev_logtype, NOTICE);
static const char * const valid_args[] = {
#define IFPGA_ARG_NAME "ifpga"
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",
ret = rte_eal_hotplug_add(RTE_STR(IFPGA_BUS_NAME),
dev_name, devargs->args);
end:
- if (kvlist)
- rte_kvargs_free(kvlist);
- if (name)
- free(name);
+ rte_kvargs_free(kvlist);
+ free(name);
return ret;
}
"ifpga=<string> "
"port=<int> "
"afu_bts=<path>");
+
+struct rte_pci_bus *ifpga_get_pci_bus(void)
+{
+ return rte_ifpga_rawdev_pmd.bus;
+}
+
+int ifpga_rawdev_partial_reconfigure(struct rte_rawdev *dev, int port,
+ const char *file)
+{
+ if (!dev) {
+ IFPGA_RAWDEV_PMD_ERR("Input parameter is invalid");
+ return -EINVAL;
+ }
+
+ return rte_fpga_do_pr(dev, port, file);
+}
+
+void ifpga_rawdev_cleanup(void)
+{
+ struct ifpga_rawdev *dev;
+ unsigned int i;
+
+ for (i = 0; i < IFPGA_RAWDEV_NUM; i++) {
+ dev = &ifpga_rawdevices[i];
+ if (dev->rawdev) {
+ rte_rawdev_pmd_release(dev->rawdev);
+ dev->rawdev = NULL;
+ }
+ }
+}