1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
13 #include <rte_eal_memconfig.h>
14 #include <rte_malloc.h>
15 #include <rte_devargs.h>
16 #include <rte_memcpy.h>
18 #include <rte_bus_pci.h>
19 #include <rte_kvargs.h>
20 #include <rte_alarm.h>
22 #include <rte_errno.h>
23 #include <rte_per_lcore.h>
24 #include <rte_memory.h>
25 #include <rte_memzone.h>
27 #include <rte_common.h>
28 #include <rte_bus_vdev.h>
30 #include "base/opae_hw_api.h"
31 #include "rte_rawdev.h"
32 #include "rte_rawdev_pmd.h"
33 #include "rte_bus_ifpga.h"
34 #include "ifpga_common.h"
35 #include "ifpga_logs.h"
36 #include "ifpga_rawdev.h"
38 int ifpga_rawdev_logtype;
40 #define PCI_VENDOR_ID_INTEL 0x8086
42 #define PCIE_DEVICE_ID_PF_INT_5_X 0xBCBD
43 #define PCIE_DEVICE_ID_PF_INT_6_X 0xBCC0
44 #define PCIE_DEVICE_ID_PF_DSC_1_X 0x09C4
46 #define PCIE_DEVICE_ID_VF_INT_5_X 0xBCBF
47 #define PCIE_DEVICE_ID_VF_INT_6_X 0xBCC1
48 #define PCIE_DEVICE_ID_VF_DSC_1_X 0x09C5
49 #define RTE_MAX_RAW_DEVICE 10
51 static const struct rte_pci_id pci_ifpga_map[] = {
52 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_5_X) },
53 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_5_X) },
54 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_6_X) },
55 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_6_X) },
56 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_DSC_1_X) },
57 { RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_DSC_1_X) },
58 { .vendor_id = 0, /* sentinel */ },
62 ifpga_fill_afu_dev(struct opae_accelerator *acc,
63 struct rte_afu_device *afu_dev)
65 struct rte_mem_resource *res = afu_dev->mem_resource;
66 struct opae_acc_region_info region_info;
67 struct opae_acc_info info;
71 ret = opae_acc_get_info(acc, &info);
75 if (info.num_regions > PCI_MAX_RESOURCE)
78 afu_dev->num_region = info.num_regions;
80 for (i = 0; i < info.num_regions; i++) {
81 region_info.index = i;
82 ret = opae_acc_get_region_info(acc, ®ion_info);
86 if ((region_info.flags & ACC_REGION_MMIO) &&
87 (region_info.flags & ACC_REGION_READ) &&
88 (region_info.flags & ACC_REGION_WRITE)) {
89 res[i].phys_addr = region_info.phys_addr;
90 res[i].len = region_info.len;
91 res[i].addr = region_info.addr;
100 ifpga_rawdev_info_get(struct rte_rawdev *dev,
101 rte_rawdev_obj_t dev_info)
103 struct opae_adapter *adapter;
104 struct opae_accelerator *acc;
105 struct rte_afu_device *afu_dev;
107 IFPGA_RAWDEV_PMD_FUNC_TRACE();
110 IFPGA_RAWDEV_PMD_ERR("Invalid request");
114 adapter = ifpga_rawdev_get_priv(dev);
119 afu_dev->rawdev = dev;
121 /* find opae_accelerator and fill info into afu_device */
122 opae_adapter_for_each_acc(adapter, acc) {
123 if (acc->index != afu_dev->id.port)
126 if (ifpga_fill_afu_dev(acc, afu_dev)) {
127 IFPGA_RAWDEV_PMD_ERR("cannot get info\n");
134 ifpga_rawdev_configure(const struct rte_rawdev *dev,
135 rte_rawdev_obj_t config)
137 IFPGA_RAWDEV_PMD_FUNC_TRACE();
139 RTE_FUNC_PTR_OR_ERR_RET(dev, -EINVAL);
141 return config ? 0 : 1;
145 ifpga_rawdev_start(struct rte_rawdev *dev)
148 struct opae_adapter *adapter;
150 IFPGA_RAWDEV_PMD_FUNC_TRACE();
152 RTE_FUNC_PTR_OR_ERR_RET(dev, -EINVAL);
154 adapter = ifpga_rawdev_get_priv(dev);
162 ifpga_rawdev_stop(struct rte_rawdev *dev)
168 ifpga_rawdev_close(struct rte_rawdev *dev)
174 ifpga_rawdev_reset(struct rte_rawdev *dev)
180 fpga_pr(struct rte_rawdev *raw_dev, u32 port_id, u64 *buffer, u32 size,
184 struct opae_adapter *adapter;
185 struct opae_manager *mgr;
186 struct opae_accelerator *acc;
187 struct opae_bridge *br;
190 adapter = ifpga_rawdev_get_priv(raw_dev);
194 mgr = opae_adapter_get_mgr(adapter);
198 acc = opae_adapter_get_acc(adapter, port_id);
202 br = opae_acc_get_br(acc);
206 ret = opae_manager_flash(mgr, port_id, buffer, size, status);
208 IFPGA_RAWDEV_PMD_ERR("%s pr error %d\n", __func__, ret);
212 ret = opae_bridge_reset(br);
214 IFPGA_RAWDEV_PMD_ERR("%s reset port:%d error %d\n",
215 __func__, port_id, ret);
223 rte_fpga_do_pr(struct rte_rawdev *rawdev, int port_id,
224 const char *file_name)
226 struct stat file_stat;
236 file_fd = open(file_name, O_RDONLY);
238 IFPGA_RAWDEV_PMD_ERR("%s: open file error: %s\n",
239 __func__, file_name);
240 IFPGA_RAWDEV_PMD_ERR("Message : %s\n", strerror(errno));
243 ret = stat(file_name, &file_stat);
245 IFPGA_RAWDEV_PMD_ERR("stat on bitstream file failed: %s\n",
249 buffer_size = file_stat.st_size;
250 IFPGA_RAWDEV_PMD_INFO("bitstream file size: %zu\n", buffer_size);
251 buffer = rte_malloc(NULL, buffer_size, 0);
257 /*read the raw data*/
258 if (buffer_size != read(file_fd, (void *)buffer, buffer_size)) {
264 ret = fpga_pr(rawdev, port_id, buffer, buffer_size, &pr_error);
265 IFPGA_RAWDEV_PMD_INFO("downloading to device port %d....%s.\n", port_id,
266 ret ? "failed" : "success");
282 ifpga_rawdev_pr(struct rte_rawdev *dev,
283 rte_rawdev_obj_t pr_conf)
285 struct opae_adapter *adapter;
286 struct rte_afu_pr_conf *afu_pr_conf;
289 struct opae_accelerator *acc;
291 IFPGA_RAWDEV_PMD_FUNC_TRACE();
293 adapter = ifpga_rawdev_get_priv(dev);
300 afu_pr_conf = pr_conf;
302 if (afu_pr_conf->pr_enable) {
303 ret = rte_fpga_do_pr(dev,
304 afu_pr_conf->afu_id.port,
305 afu_pr_conf->bs_path);
307 IFPGA_RAWDEV_PMD_ERR("do pr error %d\n", ret);
312 acc = opae_adapter_get_acc(adapter, afu_pr_conf->afu_id.port);
316 ret = opae_acc_get_uuid(acc, &uuid);
320 memcpy(&afu_pr_conf->afu_id.uuid.uuid_low, uuid.b, sizeof(u64));
321 memcpy(&afu_pr_conf->afu_id.uuid.uuid_high, uuid.b + 8, sizeof(u64));
323 IFPGA_RAWDEV_PMD_INFO("%s: uuid_l=0x%lx, uuid_h=0x%lx\n", __func__,
324 (unsigned long)afu_pr_conf->afu_id.uuid.uuid_low,
325 (unsigned long)afu_pr_conf->afu_id.uuid.uuid_high);
330 static const struct rte_rawdev_ops ifpga_rawdev_ops = {
331 .dev_info_get = ifpga_rawdev_info_get,
332 .dev_configure = ifpga_rawdev_configure,
333 .dev_start = ifpga_rawdev_start,
334 .dev_stop = ifpga_rawdev_stop,
335 .dev_close = ifpga_rawdev_close,
336 .dev_reset = ifpga_rawdev_reset,
338 .queue_def_conf = NULL,
340 .queue_release = NULL,
345 .enqueue_bufs = NULL,
346 .dequeue_bufs = NULL,
351 .xstats_get_names = NULL,
352 .xstats_get_by_name = NULL,
353 .xstats_reset = NULL,
355 .firmware_status_get = NULL,
356 .firmware_version_get = NULL,
357 .firmware_load = ifpga_rawdev_pr,
358 .firmware_unload = NULL,
360 .dev_selftest = NULL,
364 ifpga_rawdev_create(struct rte_pci_device *pci_dev,
368 struct rte_rawdev *rawdev = NULL;
369 struct opae_adapter *adapter = NULL;
370 struct opae_manager *mgr = NULL;
371 struct opae_adapter_data_pci *data = NULL;
372 char name[RTE_RAWDEV_NAME_MAX_LEN];
376 IFPGA_RAWDEV_PMD_ERR("Invalid pci_dev of the device!");
381 memset(name, 0, sizeof(name));
382 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%x:%02x.%x",
383 pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
385 IFPGA_RAWDEV_PMD_INFO("Init %s on NUMA node %d", name, rte_socket_id());
387 /* Allocate device structure */
388 rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct opae_adapter),
390 if (rawdev == NULL) {
391 IFPGA_RAWDEV_PMD_ERR("Unable to allocate rawdevice");
396 /* alloc OPAE_FPGA_PCI data to register to OPAE hardware level API */
397 data = opae_adapter_data_alloc(OPAE_FPGA_PCI);
403 /* init opae_adapter_data_pci for device specific information */
404 for (i = 0; i < PCI_MAX_RESOURCE; i++) {
405 data->region[i].phys_addr = pci_dev->mem_resource[i].phys_addr;
406 data->region[i].len = pci_dev->mem_resource[i].len;
407 data->region[i].addr = pci_dev->mem_resource[i].addr;
409 data->device_id = pci_dev->id.device_id;
410 data->vendor_id = pci_dev->id.vendor_id;
412 /* create a opae_adapter based on above device data */
413 adapter = opae_adapter_alloc(pci_dev->device.name, data);
416 goto free_adapter_data;
419 rawdev->dev_ops = &ifpga_rawdev_ops;
420 rawdev->device = &pci_dev->device;
421 rawdev->driver_name = pci_dev->device.driver->name;
423 rawdev->dev_private = adapter;
425 /* must enumerate the adapter before use it */
426 ret = opae_adapter_enumerate(adapter);
430 /* get opae_manager to rawdev */
431 mgr = opae_adapter_get_mgr(adapter);
434 IFPGA_RAWDEV_PMD_INFO("this is a PF function");
441 opae_adapter_free(adapter);
444 opae_adapter_data_free(data);
447 rte_rawdev_pmd_release(rawdev);
453 ifpga_rawdev_destroy(struct rte_pci_device *pci_dev)
456 struct rte_rawdev *rawdev;
457 char name[RTE_RAWDEV_NAME_MAX_LEN];
458 struct opae_adapter *adapter;
461 IFPGA_RAWDEV_PMD_ERR("Invalid pci_dev of the device!");
466 memset(name, 0, sizeof(name));
467 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%x:%02x.%x",
468 pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function);
470 IFPGA_RAWDEV_PMD_INFO("Closing %s on NUMA node %d",
471 name, rte_socket_id());
473 rawdev = rte_rawdev_pmd_get_named_dev(name);
475 IFPGA_RAWDEV_PMD_ERR("Invalid device name (%s)", name);
479 adapter = ifpga_rawdev_get_priv(rawdev);
483 opae_adapter_data_free(adapter->data);
484 opae_adapter_free(adapter);
486 /* rte_rawdev_close is called by pmd_release */
487 ret = rte_rawdev_pmd_release(rawdev);
489 IFPGA_RAWDEV_PMD_DEBUG("Device cleanup failed");
495 ifpga_rawdev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
496 struct rte_pci_device *pci_dev)
499 IFPGA_RAWDEV_PMD_FUNC_TRACE();
500 return ifpga_rawdev_create(pci_dev, rte_socket_id());
504 ifpga_rawdev_pci_remove(struct rte_pci_device *pci_dev)
506 return ifpga_rawdev_destroy(pci_dev);
509 static struct rte_pci_driver rte_ifpga_rawdev_pmd = {
510 .id_table = pci_ifpga_map,
511 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
512 .probe = ifpga_rawdev_pci_probe,
513 .remove = ifpga_rawdev_pci_remove,
516 RTE_PMD_REGISTER_PCI(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
517 RTE_PMD_REGISTER_PCI_TABLE(ifpga_rawdev_pci_driver, rte_ifpga_rawdev_pmd);
518 RTE_PMD_REGISTER_KMOD_DEP(ifpga_rawdev_pci_driver, "* igb_uio | uio_pci_generic | vfio-pci");
520 RTE_INIT(ifpga_rawdev_init_log)
522 ifpga_rawdev_logtype = rte_log_register("driver.raw.init");
523 if (ifpga_rawdev_logtype >= 0)
524 rte_log_set_level(ifpga_rawdev_logtype, RTE_LOG_NOTICE);
527 static const char * const valid_args[] = {
528 #define IFPGA_ARG_NAME "ifpga"
530 #define IFPGA_ARG_PORT "port"
532 #define IFPGA_AFU_BTS "afu_bts"
538 ifpga_cfg_probe(struct rte_vdev_device *dev)
540 struct rte_devargs *devargs;
541 struct rte_kvargs *kvlist = NULL;
544 char dev_name[RTE_RAWDEV_NAME_MAX_LEN];
546 devargs = dev->device.devargs;
548 kvlist = rte_kvargs_parse(devargs->args, valid_args);
550 IFPGA_RAWDEV_PMD_LOG(ERR, "error when parsing param");
554 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
555 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
556 &rte_ifpga_get_string_arg, &name) < 0) {
557 IFPGA_RAWDEV_PMD_ERR("error to parse %s",
562 IFPGA_RAWDEV_PMD_ERR("arg %s is mandatory for ifpga bus",
567 if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
568 if (rte_kvargs_process(kvlist,
570 &rte_ifpga_get_integer32_arg,
572 IFPGA_RAWDEV_PMD_ERR("error to parse %s",
577 IFPGA_RAWDEV_PMD_ERR("arg %s is mandatory for ifpga bus",
582 memset(dev_name, 0, sizeof(dev_name));
583 snprintf(dev_name, RTE_RAWDEV_NAME_MAX_LEN, "%d|%s",
586 rte_eal_hotplug_add(RTE_STR(IFPGA_BUS_NAME),
587 dev_name, devargs->args);
590 rte_kvargs_free(kvlist);
598 ifpga_cfg_remove(struct rte_vdev_device *vdev)
600 IFPGA_RAWDEV_PMD_INFO("Remove ifpga_cfg %p",
606 static struct rte_vdev_driver ifpga_cfg_driver = {
607 .probe = ifpga_cfg_probe,
608 .remove = ifpga_cfg_remove,
611 RTE_PMD_REGISTER_VDEV(ifpga_rawdev_cfg, ifpga_cfg_driver);
612 RTE_PMD_REGISTER_ALIAS(ifpga_rawdev_cfg, ifpga_cfg);
613 RTE_PMD_REGISTER_PARAM_STRING(ifpga_rawdev_cfg,