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