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