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