c35feb4a5100aa92024a8b57ab43349b790da4b2
[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 #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_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
87 {
88         FILE *f;
89         char filename[PATH_MAX];
90         int ret;
91         unsigned major, minor;
92         dev_t dev;
93
94         /* get the name of the sysfs file that contains the major and minor
95          * of the uio device and read its content */
96         snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
97
98         f = fopen(filename, "r");
99         if (f == NULL) {
100                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
101                         __func__);
102                 return -1;
103         }
104
105         ret = fscanf(f, "%u:%u", &major, &minor);
106         if (ret != 2) {
107                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
108                         __func__);
109                 fclose(f);
110                 return -1;
111         }
112         fclose(f);
113
114         /* create the char device "mknod /dev/uioX c major minor" */
115         snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
116         dev = makedev(major, minor);
117         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
118         if (f == NULL) {
119                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
120                         __func__, strerror(errno));
121                 return -1;
122         }
123
124         return ret;
125 }
126
127 /*
128  * Return the uioX char device used for a pci device. On success, return
129  * the UIO number and fill dstbuf string with the path of the device in
130  * sysfs. On error, return a negative value. In this case dstbuf is
131  * invalid.
132  */
133 static int
134 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
135                            unsigned int buflen)
136 {
137         struct rte_pci_addr *loc = &dev->addr;
138         unsigned int uio_num;
139         struct dirent *e;
140         DIR *dir;
141         char dirname[PATH_MAX];
142
143         /* depending on kernel version, uio can be located in uio/uioX
144          * or uio:uioX */
145
146         snprintf(dirname, sizeof(dirname),
147                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
148                         loc->domain, loc->bus, loc->devid, loc->function);
149
150         dir = opendir(dirname);
151         if (dir == NULL) {
152                 /* retry with the parent directory */
153                 snprintf(dirname, sizeof(dirname),
154                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
155                                 loc->domain, loc->bus, loc->devid, loc->function);
156                 dir = opendir(dirname);
157
158                 if (dir == NULL) {
159                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
160                         return -1;
161                 }
162         }
163
164         /* take the first file starting with "uio" */
165         while ((e = readdir(dir)) != NULL) {
166                 /* format could be uio%d ...*/
167                 int shortprefix_len = sizeof("uio") - 1;
168                 /* ... or uio:uio%d */
169                 int longprefix_len = sizeof("uio:uio") - 1;
170                 char *endptr;
171
172                 if (strncmp(e->d_name, "uio", 3) != 0)
173                         continue;
174
175                 /* first try uio%d */
176                 errno = 0;
177                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
178                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
179                         snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
180                         break;
181                 }
182
183                 /* then try uio:uio%d */
184                 errno = 0;
185                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
186                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
187                         snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
188                         break;
189                 }
190         }
191         closedir(dir);
192
193         /* No uio resource found */
194         if (e == NULL)
195                 return -1;
196
197         /* create uio device if we've been asked to */
198         if (internal_config.create_uio_dev &&
199                         pci_mknod_uio_dev(dstbuf, uio_num) < 0)
200                 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
201
202         return uio_num;
203 }
204
205 void
206 pci_uio_free_resource(struct rte_pci_device *dev,
207                 struct mapped_pci_resource *uio_res)
208 {
209         rte_free(uio_res);
210
211         if (dev->intr_handle.uio_cfg_fd >= 0) {
212                 close(dev->intr_handle.uio_cfg_fd);
213                 dev->intr_handle.uio_cfg_fd = -1;
214         }
215         if (dev->intr_handle.fd) {
216                 close(dev->intr_handle.fd);
217                 dev->intr_handle.fd = -1;
218                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
219         }
220 }
221
222 int
223 pci_uio_alloc_resource(struct rte_pci_device *dev,
224                 struct mapped_pci_resource **uio_res)
225 {
226         char dirname[PATH_MAX];
227         char cfgname[PATH_MAX];
228         char devname[PATH_MAX]; /* contains the /dev/uioX */
229         int uio_num;
230         struct rte_pci_addr *loc;
231
232         loc = &dev->addr;
233
234         /* find uio resource */
235         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
236         if (uio_num < 0) {
237                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
238                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
239                 return 1;
240         }
241         snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
242
243         /* save fd if in primary process */
244         dev->intr_handle.fd = open(devname, O_RDWR);
245         if (dev->intr_handle.fd < 0) {
246                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
247                         devname, strerror(errno));
248                 goto error;
249         }
250
251         snprintf(cfgname, sizeof(cfgname),
252                         "/sys/class/uio/uio%u/device/config", uio_num);
253         dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
254         if (dev->intr_handle.uio_cfg_fd < 0) {
255                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
256                         cfgname, strerror(errno));
257                 goto error;
258         }
259
260         if (dev->kdrv == RTE_KDRV_IGB_UIO)
261                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
262         else {
263                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
264
265                 /* set bus master that is not done by uio_pci_generic */
266                 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
267                         RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
268                         goto error;
269                 }
270         }
271
272         /* allocate the mapping details for secondary processes*/
273         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
274         if (*uio_res == NULL) {
275                 RTE_LOG(ERR, EAL,
276                         "%s(): cannot store uio mmap details\n", __func__);
277                 goto error;
278         }
279
280         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
281         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
282
283         return 0;
284
285 error:
286         pci_uio_free_resource(dev, *uio_res);
287         return -1;
288 }
289
290 int
291 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
292                 struct mapped_pci_resource *uio_res, int map_idx)
293 {
294         int fd;
295         char devname[PATH_MAX]; /* contains the /dev/uioX */
296         void *mapaddr;
297         struct rte_pci_addr *loc;
298         struct pci_map *maps;
299
300         loc = &dev->addr;
301         maps = uio_res->maps;
302
303         /* update devname for mmap  */
304         snprintf(devname, sizeof(devname),
305                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
306                         loc->domain, loc->bus, loc->devid,
307                         loc->function, res_idx);
308
309         /* allocate memory to keep path */
310         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
311         if (maps[map_idx].path == NULL) {
312                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
313                                 strerror(errno));
314                 return -1;
315         }
316
317         /*
318          * open resource file, to mmap it
319          */
320         fd = open(devname, O_RDWR);
321         if (fd < 0) {
322                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
323                                 devname, strerror(errno));
324                 goto error;
325         }
326
327         /* try mapping somewhere close to the end of hugepages */
328         if (pci_map_addr == NULL)
329                 pci_map_addr = pci_find_max_end_va();
330
331         mapaddr = pci_map_resource(pci_map_addr, fd, 0,
332                         (size_t)dev->mem_resource[res_idx].len, 0);
333         close(fd);
334         if (mapaddr == MAP_FAILED)
335                 goto error;
336
337         pci_map_addr = RTE_PTR_ADD(mapaddr,
338                         (size_t)dev->mem_resource[res_idx].len);
339
340         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
341         maps[map_idx].size = dev->mem_resource[res_idx].len;
342         maps[map_idx].addr = mapaddr;
343         maps[map_idx].offset = 0;
344         strcpy(maps[map_idx].path, devname);
345         dev->mem_resource[res_idx].addr = mapaddr;
346
347         return 0;
348
349 error:
350         rte_free(maps[map_idx].path);
351         return -1;
352 }