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