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