11 #include <rte_bus_pci.h>
12 #include <rte_malloc.h>
16 /* PF BAR and expansion BAR for the NSP interface */
17 #define NFP_CFG_PCIE_BAR 0
18 #define NFP_CFG_EXP_BAR 7
20 #define NFP_CFG_EXP_BAR_CFG_BASE 0x30000
21 #define NFP_LOCKFILE_PATH_FMT "%s/nfp%d"
23 /* get nfp lock file path (/var/lock if root, $HOME otherwise) */
25 nspu_get_lockfile_path(char *buffer, int bufsz, nfpu_desc_t *desc)
27 const char *dir = "/var/lock";
28 const char *home_dir = getenv("HOME");
30 if (getuid() != 0 && home_dir != NULL)
33 /* use current prefix as file path */
34 snprintf(buffer, bufsz, NFP_LOCKFILE_PATH_FMT, dir,
38 /* There could be other NFP userspace tools using the NSP interface.
39 * Make sure there is no other process using it and locking the access for
43 nspv_aquire_process_lock(nfpu_desc_t *desc)
49 nspu_get_lockfile_path(lockname, sizeof(lockname), desc);
51 /* Using S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH */
52 desc->lock = open(lockname, O_RDWR | O_CREAT, 0666);
57 lock.l_type = F_WRLCK;
58 lock.l_whence = SEEK_SET;
61 rc = fcntl(desc->lock, F_SETLK, &lock);
63 if ((errno != EAGAIN) && (errno != EACCES)) {
74 nfpu_open(struct rte_pci_device *pci_dev, nfpu_desc_t *desc, int nfp)
76 void *cfg_base, *mem_base;
83 ret = nspv_aquire_process_lock(desc);
87 barsz = pci_dev->mem_resource[0].len;
95 /* Sanity check: we can assume any bar size less than 1MB an error */
99 /* Getting address for NFP expansion BAR registers */
100 cfg_base = pci_dev->mem_resource[0].addr;
101 cfg_base = (uint8_t *)cfg_base + NFP_CFG_EXP_BAR_CFG_BASE;
103 /* Getting address for NFP NSP interface registers */
104 mem_base = pci_dev->mem_resource[0].addr;
105 mem_base = (uint8_t *)mem_base + (NFP_CFG_EXP_BAR << (barsz - 3));
108 desc->nspu = rte_malloc("nfp nspu", sizeof(nspu_desc_t), 0);
109 nfp_nspu_init(desc->nspu, desc->nfp, NFP_CFG_PCIE_BAR, barsz,
110 NFP_CFG_EXP_BAR, cfg_base, mem_base);
116 nfpu_close(nfpu_desc_t *desc)
120 rte_free(desc->nspu);
123 nspu_get_lockfile_path(lockname, sizeof(lockname), desc);