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