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