7c75593d97b18655a20f48cde090d39be935c667
[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 <fcntl.h>
36 #include <dirent.h>
37 #include <sys/stat.h>
38
39 #include <rte_log.h>
40 #include <rte_pci.h>
41 #include <rte_common.h>
42 #include <rte_malloc.h>
43 #include <rte_tailq.h>
44
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_pci_init.h"
48
49 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
50
51
52 #define OFF_MAX              ((uint64_t)(off_t)-1)
53 static int
54 pci_uio_get_mappings(const char *devname, struct pci_map maps[], int nb_maps)
55 {
56         int i;
57         char dirname[PATH_MAX];
58         char filename[PATH_MAX];
59         uint64_t offset, size;
60
61         for (i = 0; i != nb_maps; i++) {
62
63                 /* check if map directory exists */
64                 rte_snprintf(dirname, sizeof(dirname),
65                         "%s/maps/map%u", devname, i);
66
67                 if (access(dirname, F_OK) != 0)
68                         break;
69
70                 /* get mapping offset */
71                 rte_snprintf(filename, sizeof(filename),
72                         "%s/offset", dirname);
73                 if (pci_parse_sysfs_value(filename, &offset) < 0) {
74                         RTE_LOG(ERR, EAL,
75                                 "%s(): cannot parse offset of %s\n",
76                                 __func__, dirname);
77                         return -1;
78                 }
79
80                 /* get mapping size */
81                 rte_snprintf(filename, sizeof(filename),
82                         "%s/size", dirname);
83                 if (pci_parse_sysfs_value(filename, &size) < 0) {
84                         RTE_LOG(ERR, EAL,
85                                 "%s(): cannot parse size of %s\n",
86                                 __func__, dirname);
87                         return -1;
88                 }
89
90                 /* get mapping physical address */
91                 rte_snprintf(filename, sizeof(filename),
92                         "%s/addr", dirname);
93                 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
94                         RTE_LOG(ERR, EAL,
95                                 "%s(): cannot parse addr of %s\n",
96                                 __func__, dirname);
97                         return -1;
98                 }
99
100                 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
101                         RTE_LOG(ERR, EAL,
102                                 "%s(): offset/size exceed system max value\n",
103                                 __func__);
104                         return -1;
105                 }
106
107                 maps[i].offset = offset;
108                 maps[i].size = size;
109         }
110
111         return i;
112 }
113
114 static int
115 pci_uio_map_secondary(struct rte_pci_device *dev)
116 {
117         int fd, i;
118         struct mapped_pci_resource *uio_res;
119
120         TAILQ_FOREACH(uio_res, pci_res_list, next) {
121
122                 /* skip this element if it doesn't match our PCI address */
123                 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
124                         continue;
125
126                 for (i = 0; i != uio_res->nb_maps; i++) {
127                         /*
128                          * open devname, to mmap it
129                          */
130                         fd = open(uio_res->path, O_RDWR);
131                         if (fd < 0) {
132                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
133                                         uio_res->path, strerror(errno));
134                                 return -1;
135                         }
136
137                         if (pci_map_resource(uio_res->maps[i].addr, fd,
138                                              (off_t)uio_res->maps[i].offset,
139                                              (size_t)uio_res->maps[i].size)
140                             != uio_res->maps[i].addr) {
141                                 RTE_LOG(ERR, EAL,
142                                         "Cannot mmap device resource\n");
143                                 close(fd);
144                                 return -1;
145                         }
146                         /* fd is not needed in slave process, close it */
147                         close(fd);
148                 }
149                 return 0;
150         }
151
152         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
153         return -1;
154 }
155
156 static int
157 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
158 {
159         FILE *f;
160         char filename[PATH_MAX];
161         int ret;
162         unsigned major, minor;
163         dev_t dev;
164
165         /* get the name of the sysfs file that contains the major and minor
166          * of the uio device and read its content */
167         rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
168
169         f = fopen(filename, "r");
170         if (f == NULL) {
171                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
172                         __func__);
173                 return -1;
174         }
175
176         ret = fscanf(f, "%d:%d", &major, &minor);
177         if (ret != 2) {
178                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
179                         __func__);
180                 fclose(f);
181                 return -1;
182         }
183         fclose(f);
184
185         /* create the char device "mknod /dev/uioX c major minor" */
186         rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
187         dev = makedev(major, minor);
188         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
189         if (f == NULL) {
190                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
191                         __func__, strerror(errno));
192                 return -1;
193         }
194
195         return ret;
196 }
197
198 /*
199  * Return the uioX char device used for a pci device. On success, return
200  * the UIO number and fill dstbuf string with the path of the device in
201  * sysfs. On error, return a negative value. In this case dstbuf is
202  * invalid.
203  */
204 static int
205 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
206                            unsigned int buflen)
207 {
208         struct rte_pci_addr *loc = &dev->addr;
209         unsigned int uio_num;
210         struct dirent *e;
211         DIR *dir;
212         char dirname[PATH_MAX];
213
214         /* depending on kernel version, uio can be located in uio/uioX
215          * or uio:uioX */
216
217         rte_snprintf(dirname, sizeof(dirname),
218                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
219                         loc->domain, loc->bus, loc->devid, loc->function);
220
221         dir = opendir(dirname);
222         if (dir == NULL) {
223                 /* retry with the parent directory */
224                 rte_snprintf(dirname, sizeof(dirname),
225                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
226                                 loc->domain, loc->bus, loc->devid, loc->function);
227                 dir = opendir(dirname);
228
229                 if (dir == NULL) {
230                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
231                         return -1;
232                 }
233         }
234
235         /* take the first file starting with "uio" */
236         while ((e = readdir(dir)) != NULL) {
237                 /* format could be uio%d ...*/
238                 int shortprefix_len = sizeof("uio") - 1;
239                 /* ... or uio:uio%d */
240                 int longprefix_len = sizeof("uio:uio") - 1;
241                 char *endptr;
242
243                 if (strncmp(e->d_name, "uio", 3) != 0)
244                         continue;
245
246                 /* first try uio%d */
247                 errno = 0;
248                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
249                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
250                         rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
251                         break;
252                 }
253
254                 /* then try uio:uio%d */
255                 errno = 0;
256                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
257                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
258                         rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
259                         break;
260                 }
261         }
262         closedir(dir);
263
264         /* No uio resource found */
265         if (e == NULL)
266                 return -1;
267
268         /* create uio device if we've been asked to */
269         if (internal_config.create_uio_dev &&
270                         pci_mknod_uio_dev(dstbuf, uio_num) < 0)
271                 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
272
273         return uio_num;
274 }
275
276 /* map the PCI resource of a PCI device in virtual memory */
277 int
278 pci_uio_map_resource(struct rte_pci_device *dev)
279 {
280         int i, j;
281         char dirname[PATH_MAX];
282         char devname[PATH_MAX]; /* contains the /dev/uioX */
283         void *mapaddr;
284         int uio_num;
285         uint64_t phaddr;
286         uint64_t offset;
287         uint64_t pagesz;
288         int nb_maps;
289         struct rte_pci_addr *loc = &dev->addr;
290         struct mapped_pci_resource *uio_res;
291         struct pci_map *maps;
292
293         dev->intr_handle.fd = -1;
294         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
295
296         /* secondary processes - use already recorded details */
297         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
298                 return pci_uio_map_secondary(dev);
299
300         /* find uio resource */
301         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
302         if (uio_num < 0) {
303                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
304                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
305                 return -1;
306         }
307         rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
308
309         /* save fd if in primary process */
310         dev->intr_handle.fd = open(devname, O_RDWR);
311         if (dev->intr_handle.fd < 0) {
312                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
313                         devname, strerror(errno));
314                 return -1;
315         }
316         dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
317
318         /* allocate the mapping details for secondary processes*/
319         uio_res = rte_zmalloc("UIO_RES", sizeof(*uio_res), 0);
320         if (uio_res == NULL) {
321                 RTE_LOG(ERR, EAL,
322                         "%s(): cannot store uio mmap details\n", __func__);
323                 return -1;
324         }
325
326         rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
327         memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
328
329         /* collect info about device mappings */
330         nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
331                                        RTE_DIM(uio_res->maps));
332         if (nb_maps < 0) {
333                 rte_free(uio_res);
334                 return nb_maps;
335         }
336
337         uio_res->nb_maps = nb_maps;
338
339         /* Map all BARs */
340         pagesz = sysconf(_SC_PAGESIZE);
341
342         maps = uio_res->maps;
343         for (i = 0; i != PCI_MAX_RESOURCE; i++) {
344                 int fd;
345
346                 /* skip empty BAR */
347                 phaddr = dev->mem_resource[i].phys_addr;
348                 if (phaddr == 0)
349                         continue;
350
351                 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
352                                 dev->mem_resource[i].len != maps[j].size);
353                                 j++)
354                         ;
355
356                 /* if matching map is found, then use it */
357                 if (j != nb_maps) {
358                         int fail = 0;
359                         offset = j * pagesz;
360
361                         /*
362                          * open devname, to mmap it
363                          */
364                         fd = open(devname, O_RDWR);
365                         if (fd < 0) {
366                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
367                                         devname, strerror(errno));
368                                 return -1;
369                         }
370
371                         if (maps[j].addr != NULL)
372                                 fail = 1;
373                         else {
374                                 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
375                                                 (size_t)maps[j].size);
376                                 if (mapaddr == NULL)
377                                         fail = 1;
378                         }
379
380                         if (fail) {
381                                 rte_free(uio_res);
382                                 close(fd);
383                                 return -1;
384                         }
385                         close(fd);
386
387                         maps[j].addr = mapaddr;
388                         maps[j].offset = offset;
389                         dev->mem_resource[i].addr = mapaddr;
390                 }
391         }
392
393         TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);
394
395         return 0;
396 }
397
398 /*
399  * parse a sysfs file containing one integer value
400  * different to the eal version, as it needs to work with 64-bit values
401  */
402 static int
403 pci_parse_sysfs_value(const char *filename, uint64_t *val)
404 {
405         FILE *f;
406         char buf[BUFSIZ];
407         char *end = NULL;
408
409         f = fopen(filename, "r");
410         if (f == NULL) {
411                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
412                                 __func__, filename);
413                 return -1;
414         }
415
416         if (fgets(buf, sizeof(buf), f) == NULL) {
417                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
418                                 __func__, filename);
419                 fclose(f);
420                 return -1;
421         }
422         *val = strtoull(buf, &end, 0);
423         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
424                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
425                                 __func__, filename);
426                 fclose(f);
427                 return -1;
428         }
429         fclose(f);
430         return 0;
431 }