8bbcef00eaa789405471f160ccd11556f95e8cf3
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci_uio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <linux/pci_regs.h>
41
42 #include <rte_log.h>
43 #include <rte_pci.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_common.h>
46 #include <rte_malloc.h>
47
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
50
51 void *pci_map_addr = NULL;
52
53 #define OFF_MAX              ((uint64_t)(off_t)-1)
54
55 static int
56 pci_uio_set_bus_master(int dev_fd)
57 {
58         uint16_t reg;
59         int ret;
60
61         ret = pread(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
62         if (ret != sizeof(reg)) {
63                 RTE_LOG(ERR, EAL,
64                         "Cannot read command from PCI config space!\n");
65                 return -1;
66         }
67
68         /* return if bus mastering is already on */
69         if (reg & PCI_COMMAND_MASTER)
70                 return 0;
71
72         reg |= PCI_COMMAND_MASTER;
73
74         ret = pwrite(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
75         if (ret != sizeof(reg)) {
76                 RTE_LOG(ERR, EAL,
77                         "Cannot write command to PCI config space!\n");
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 static int
85 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
86 {
87         FILE *f;
88         char filename[PATH_MAX];
89         int ret;
90         unsigned major, minor;
91         dev_t dev;
92
93         /* get the name of the sysfs file that contains the major and minor
94          * of the uio device and read its content */
95         snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
96
97         f = fopen(filename, "r");
98         if (f == NULL) {
99                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
100                         __func__);
101                 return -1;
102         }
103
104         ret = fscanf(f, "%u:%u", &major, &minor);
105         if (ret != 2) {
106                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
107                         __func__);
108                 fclose(f);
109                 return -1;
110         }
111         fclose(f);
112
113         /* create the char device "mknod /dev/uioX c major minor" */
114         snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
115         dev = makedev(major, minor);
116         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
117         if (f == NULL) {
118                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
119                         __func__, strerror(errno));
120                 return -1;
121         }
122
123         return ret;
124 }
125
126 /*
127  * Return the uioX char device used for a pci device. On success, return
128  * the UIO number and fill dstbuf string with the path of the device in
129  * sysfs. On error, return a negative value. In this case dstbuf is
130  * invalid.
131  */
132 static int
133 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
134                            unsigned int buflen)
135 {
136         struct rte_pci_addr *loc = &dev->addr;
137         unsigned int uio_num;
138         struct dirent *e;
139         DIR *dir;
140         char dirname[PATH_MAX];
141
142         /* depending on kernel version, uio can be located in uio/uioX
143          * or uio:uioX */
144
145         snprintf(dirname, sizeof(dirname),
146                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
147                         loc->domain, loc->bus, loc->devid, loc->function);
148
149         dir = opendir(dirname);
150         if (dir == NULL) {
151                 /* retry with the parent directory */
152                 snprintf(dirname, sizeof(dirname),
153                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
154                                 loc->domain, loc->bus, loc->devid, loc->function);
155                 dir = opendir(dirname);
156
157                 if (dir == NULL) {
158                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
159                         return -1;
160                 }
161         }
162
163         /* take the first file starting with "uio" */
164         while ((e = readdir(dir)) != NULL) {
165                 /* format could be uio%d ...*/
166                 int shortprefix_len = sizeof("uio") - 1;
167                 /* ... or uio:uio%d */
168                 int longprefix_len = sizeof("uio:uio") - 1;
169                 char *endptr;
170
171                 if (strncmp(e->d_name, "uio", 3) != 0)
172                         continue;
173
174                 /* first try uio%d */
175                 errno = 0;
176                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
177                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
178                         snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
179                         break;
180                 }
181
182                 /* then try uio:uio%d */
183                 errno = 0;
184                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
185                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
186                         snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
187                         break;
188                 }
189         }
190         closedir(dir);
191
192         /* No uio resource found */
193         if (e == NULL)
194                 return -1;
195
196         /* create uio device if we've been asked to */
197         if (internal_config.create_uio_dev &&
198                         pci_mknod_uio_dev(dstbuf, uio_num) < 0)
199                 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
200
201         return uio_num;
202 }
203
204 void
205 pci_uio_free_resource(struct rte_pci_device *dev,
206                 struct mapped_pci_resource *uio_res)
207 {
208         rte_free(uio_res);
209
210         if (dev->intr_handle.uio_cfg_fd >= 0) {
211                 close(dev->intr_handle.uio_cfg_fd);
212                 dev->intr_handle.uio_cfg_fd = -1;
213         }
214         if (dev->intr_handle.fd) {
215                 close(dev->intr_handle.fd);
216                 dev->intr_handle.fd = -1;
217                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
218         }
219 }
220
221 int
222 pci_uio_alloc_resource(struct rte_pci_device *dev,
223                 struct mapped_pci_resource **uio_res)
224 {
225         char dirname[PATH_MAX];
226         char cfgname[PATH_MAX];
227         char devname[PATH_MAX]; /* contains the /dev/uioX */
228         int uio_num;
229         struct rte_pci_addr *loc;
230
231         loc = &dev->addr;
232
233         /* find uio resource */
234         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
235         if (uio_num < 0) {
236                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
237                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
238                 return 1;
239         }
240         snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
241
242         /* save fd if in primary process */
243         dev->intr_handle.fd = open(devname, O_RDWR);
244         if (dev->intr_handle.fd < 0) {
245                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
246                         devname, strerror(errno));
247                 goto error;
248         }
249
250         snprintf(cfgname, sizeof(cfgname),
251                         "/sys/class/uio/uio%u/device/config", uio_num);
252         dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
253         if (dev->intr_handle.uio_cfg_fd < 0) {
254                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
255                         cfgname, strerror(errno));
256                 goto error;
257         }
258
259         if (dev->kdrv == RTE_KDRV_IGB_UIO)
260                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
261         else {
262                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
263
264                 /* set bus master that is not done by uio_pci_generic */
265                 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
266                         RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
267                         goto error;
268                 }
269         }
270
271         /* allocate the mapping details for secondary processes*/
272         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
273         if (*uio_res == NULL) {
274                 RTE_LOG(ERR, EAL,
275                         "%s(): cannot store uio mmap details\n", __func__);
276                 goto error;
277         }
278
279         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
280         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
281
282         return 0;
283
284 error:
285         pci_uio_free_resource(dev, *uio_res);
286         return -1;
287 }
288
289 int
290 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
291                 struct mapped_pci_resource *uio_res, int map_idx)
292 {
293         int fd;
294         char devname[PATH_MAX]; /* contains the /dev/uioX */
295         void *mapaddr;
296         struct rte_pci_addr *loc;
297         struct pci_map *maps;
298
299         loc = &dev->addr;
300         maps = uio_res->maps;
301
302         /* update devname for mmap  */
303         snprintf(devname, sizeof(devname),
304                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
305                         loc->domain, loc->bus, loc->devid,
306                         loc->function, res_idx);
307
308         /* allocate memory to keep path */
309         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
310         if (maps[map_idx].path == NULL) {
311                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
312                                 strerror(errno));
313                 return -1;
314         }
315
316         /*
317          * open resource file, to mmap it
318          */
319         fd = open(devname, O_RDWR);
320         if (fd < 0) {
321                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
322                                 devname, strerror(errno));
323                 goto error;
324         }
325
326         /* try mapping somewhere close to the end of hugepages */
327         if (pci_map_addr == NULL)
328                 pci_map_addr = pci_find_max_end_va();
329
330         mapaddr = pci_map_resource(pci_map_addr, fd, 0,
331                         (size_t)dev->mem_resource[res_idx].len, 0);
332         close(fd);
333         if (mapaddr == MAP_FAILED)
334                 goto error;
335
336         pci_map_addr = RTE_PTR_ADD(mapaddr,
337                         (size_t)dev->mem_resource[res_idx].len);
338
339         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
340         maps[map_idx].size = dev->mem_resource[res_idx].len;
341         maps[map_idx].addr = mapaddr;
342         maps[map_idx].offset = 0;
343         strcpy(maps[map_idx].path, devname);
344         dev->mem_resource[res_idx].addr = mapaddr;
345
346         return 0;
347
348 error:
349         rte_free(maps[map_idx].path);
350         return -1;
351 }