New upstream version 17.11.3
[deb_dpdk.git] / drivers / net / nfp / nfp_nfpu.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10
11 #include <rte_bus_pci.h>
12 #include <rte_malloc.h>
13
14 #include "nfp_nfpu.h"
15
16 /* PF BAR and expansion BAR for the NSP interface */
17 #define NFP_CFG_PCIE_BAR        0
18 #define NFP_CFG_EXP_BAR         7
19
20 #define NFP_CFG_EXP_BAR_CFG_BASE        0x30000
21 #define NFP_LOCKFILE_PATH_FMT "%s/nfp%d"
22
23 /* get nfp lock file path (/var/lock if root, $HOME otherwise) */
24 static void
25 nspu_get_lockfile_path(char *buffer, int bufsz, nfpu_desc_t *desc)
26 {
27         const char *dir = "/var/lock";
28         const char *home_dir = getenv("HOME");
29
30         if (getuid() != 0 && home_dir != NULL)
31                 dir = home_dir;
32
33         /* use current prefix as file path */
34         snprintf(buffer, bufsz, NFP_LOCKFILE_PATH_FMT, dir,
35                         desc->nfp);
36 }
37
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
40  * avoiding problems.
41  */
42 static int
43 nspv_aquire_process_lock(nfpu_desc_t *desc)
44 {
45         int rc;
46         struct flock lock;
47         char lockname[30];
48
49         nspu_get_lockfile_path(lockname, sizeof(lockname), desc);
50
51         /* Using S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH */
52         desc->lock = open(lockname, O_RDWR | O_CREAT, 0666);
53
54         if (desc->lock < 0)
55                 return desc->lock;
56
57         lock.l_type = F_WRLCK;
58         lock.l_whence = SEEK_SET;
59         rc = -1;
60         while (rc != 0) {
61                 rc = fcntl(desc->lock, F_SETLK, &lock);
62                 if (rc < 0) {
63                         if ((errno != EAGAIN) && (errno != EACCES)) {
64                                 close(desc->lock);
65                                 return rc;
66                         }
67                 }
68         }
69
70         return 0;
71 }
72
73 int
74 nfpu_open(struct rte_pci_device *pci_dev, nfpu_desc_t *desc, int nfp)
75 {
76         void *cfg_base, *mem_base;
77         size_t barsz;
78         int ret = 0;
79         int i = 0;
80
81         desc->nfp = nfp;
82
83         ret = nspv_aquire_process_lock(desc);
84         if (ret)
85                 return -1;
86
87         barsz = pci_dev->mem_resource[0].len;
88
89         /* barsz in log2 */
90         while (barsz >>= 1)
91                 i++;
92
93         barsz = i;
94
95         /* Sanity check: we can assume any bar size less than 1MB an error */
96         if (barsz < 20)
97                 return -1;
98
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;
102
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));
106
107
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);
111
112         return ret;
113 }
114
115 int
116 nfpu_close(nfpu_desc_t *desc)
117 {
118         char lockname[30];
119
120         rte_free(desc->nspu);
121         close(desc->lock);
122
123         nspu_get_lockfile_path(lockname, sizeof(lockname), desc);
124         unlink(lockname);
125         return 0;
126 }