pci: fix coding style
[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;
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;
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                 return -1;
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                 return -1;
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                         return -1;
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                 return -1;
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, map_idx = 0; i != PCI_MAX_RESOURCE; i++) {
340                 int fd;
341                 int fail = 0;
342
343                 /* skip empty BAR */
344                 phaddr = dev->mem_resource[i].phys_addr;
345                 if (phaddr == 0)
346                         continue;
347
348
349                 /* update devname for mmap  */
350                 snprintf(devname, sizeof(devname),
351                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
352                                 loc->domain, loc->bus, loc->devid, loc->function,
353                                 i);
354
355                 /*
356                  * open resource file, to mmap it
357                  */
358                 fd = open(devname, O_RDWR);
359                 if (fd < 0) {
360                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
361                                         devname, strerror(errno));
362                         return -1;
363                 }
364
365                 /* try mapping somewhere close to the end of hugepages */
366                 if (pci_map_addr == NULL)
367                         pci_map_addr = pci_find_max_end_va();
368
369                 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
370                                 (size_t)dev->mem_resource[i].len, 0);
371                 if (mapaddr == MAP_FAILED)
372                         fail = 1;
373
374                 pci_map_addr = RTE_PTR_ADD(mapaddr,
375                                 (size_t)dev->mem_resource[i].len);
376
377                 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
378                 if (maps[map_idx].path == NULL)
379                         fail = 1;
380
381                 if (fail) {
382                         rte_free(uio_res);
383                         close(fd);
384                         return -1;
385                 }
386                 close(fd);
387
388                 maps[map_idx].phaddr = dev->mem_resource[i].phys_addr;
389                 maps[map_idx].size = dev->mem_resource[i].len;
390                 maps[map_idx].addr = mapaddr;
391                 maps[map_idx].offset = 0;
392                 strcpy(maps[map_idx].path, devname);
393                 map_idx++;
394                 dev->mem_resource[i].addr = mapaddr;
395         }
396
397         uio_res->nb_maps = map_idx;
398
399         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
400
401         return 0;
402 }
403
404 #ifdef RTE_LIBRTE_EAL_HOTPLUG
405 static void
406 pci_uio_unmap(struct mapped_pci_resource *uio_res)
407 {
408         int i;
409
410         if (uio_res == NULL)
411                 return;
412
413         for (i = 0; i != uio_res->nb_maps; i++)
414                 pci_unmap_resource(uio_res->maps[i].addr,
415                                 (size_t)uio_res->maps[i].size);
416 }
417
418 static struct mapped_pci_resource *
419 pci_uio_find_resource(struct rte_pci_device *dev)
420 {
421         struct mapped_pci_resource *uio_res;
422         struct mapped_pci_res_list *uio_res_list =
423                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
424
425         if (dev == NULL)
426                 return NULL;
427
428         TAILQ_FOREACH(uio_res, uio_res_list, next) {
429
430                 /* skip this element if it doesn't match our PCI address */
431                 if (!rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
432                         return uio_res;
433         }
434         return NULL;
435 }
436
437 /* unmap the PCI resource of a PCI device in virtual memory */
438 void
439 pci_uio_unmap_resource(struct rte_pci_device *dev)
440 {
441         struct mapped_pci_resource *uio_res;
442         struct mapped_pci_res_list *uio_res_list =
443                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
444
445         if (dev == NULL)
446                 return;
447
448         /* find an entry for the device */
449         uio_res = pci_uio_find_resource(dev);
450         if (uio_res == NULL)
451                 return;
452
453         /* secondary processes - just free maps */
454         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
455                 return pci_uio_unmap(uio_res);
456
457         TAILQ_REMOVE(uio_res_list, uio_res, next);
458
459         /* unmap all resources */
460         pci_uio_unmap(uio_res);
461
462         /* free uio resource */
463         rte_free(uio_res);
464
465         /* close fd if in primary process */
466         close(dev->intr_handle.fd);
467
468         dev->intr_handle.fd = -1;
469         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
470 }
471 #endif /* RTE_LIBRTE_EAL_HOTPLUG */