net/nfp: fix possible bad shif operation
[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
22 /* There could be other NFP userspace tools using the NSP interface.
23  * Make sure there is no other process using it and locking the access for
24  * avoiding problems.
25  */
26 static int
27 nspv_aquire_process_lock(nfpu_desc_t *desc)
28 {
29         int rc;
30         struct flock lock;
31         char lockname[30];
32
33         memset(&lock, 0, sizeof(lock));
34
35         snprintf(lockname, sizeof(lockname), "/var/lock/nfp%d", desc->nfp);
36
37         /* Using S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH */
38         desc->lock = open(lockname, O_RDWR | O_CREAT, 0666);
39
40         if (desc->lock < 0)
41                 return desc->lock;
42
43         lock.l_type = F_WRLCK;
44         lock.l_whence = SEEK_SET;
45         rc = -1;
46         while (rc != 0) {
47                 rc = fcntl(desc->lock, F_SETLK, &lock);
48                 if (rc < 0) {
49                         if ((errno != EAGAIN) && (errno != EACCES)) {
50                                 close(desc->lock);
51                                 return rc;
52                         }
53                 }
54         }
55
56         return 0;
57 }
58
59 int
60 nfpu_open(struct rte_pci_device *pci_dev, nfpu_desc_t *desc, int nfp)
61 {
62         void *cfg_base, *mem_base;
63         size_t barsz;
64         int ret = 0;
65         int i = 0;
66
67         desc->nfp = nfp;
68
69         ret = nspv_aquire_process_lock(desc);
70         if (ret)
71                 return -1;
72
73         barsz = pci_dev->mem_resource[0].len;
74
75         /* barsz in log2 */
76         while (barsz >>= 1)
77                 i++;
78
79         barsz = i;
80
81         /* Sanity check: we can assume any bar size less than 1MB an error */
82         if (barsz < 20)
83                 return -1;
84
85         /* Getting address for NFP expansion BAR registers */
86         cfg_base = pci_dev->mem_resource[0].addr;
87         cfg_base = (uint8_t *)cfg_base + NFP_CFG_EXP_BAR_CFG_BASE;
88
89         /* Getting address for NFP NSP interface registers */
90         mem_base = pci_dev->mem_resource[0].addr;
91         mem_base = (uint8_t *)mem_base + (NFP_CFG_EXP_BAR << (barsz - 3));
92
93
94         desc->nspu = rte_malloc("nfp nspu", sizeof(nspu_desc_t), 0);
95         nfp_nspu_init(desc->nspu, desc->nfp, NFP_CFG_PCIE_BAR, barsz,
96                       NFP_CFG_EXP_BAR, cfg_base, mem_base);
97
98         return ret;
99 }
100
101 int
102 nfpu_close(nfpu_desc_t *desc)
103 {
104         rte_free(desc->nspu);
105         close(desc->lock);
106         unlink("/var/lock/nfp0");
107         return 0;
108 }