Imported Upstream version 16.04
[deb_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 #if defined(RTE_ARCH_X86)
43 #include <sys/io.h>
44 #endif
45
46 #include <rte_log.h>
47 #include <rte_pci.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_common.h>
50 #include <rte_malloc.h>
51
52 #include "eal_filesystem.h"
53 #include "eal_pci_init.h"
54
55 void *pci_map_addr = NULL;
56
57 #define OFF_MAX              ((uint64_t)(off_t)-1)
58
59 int
60 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
61                     void *buf, size_t len, off_t offset)
62 {
63         return pread(intr_handle->uio_cfg_fd, buf, len, offset);
64 }
65
66 int
67 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
68                      const void *buf, size_t len, off_t offset)
69 {
70         return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
71 }
72
73 static int
74 pci_uio_set_bus_master(int dev_fd)
75 {
76         uint16_t reg;
77         int ret;
78
79         ret = pread(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
80         if (ret != sizeof(reg)) {
81                 RTE_LOG(ERR, EAL,
82                         "Cannot read command from PCI config space!\n");
83                 return -1;
84         }
85
86         /* return if bus mastering is already on */
87         if (reg & PCI_COMMAND_MASTER)
88                 return 0;
89
90         reg |= PCI_COMMAND_MASTER;
91
92         ret = pwrite(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
93         if (ret != sizeof(reg)) {
94                 RTE_LOG(ERR, EAL,
95                         "Cannot write command to PCI config space!\n");
96                 return -1;
97         }
98
99         return 0;
100 }
101
102 static int
103 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
104 {
105         FILE *f;
106         char filename[PATH_MAX];
107         int ret;
108         unsigned major, minor;
109         dev_t dev;
110
111         /* get the name of the sysfs file that contains the major and minor
112          * of the uio device and read its content */
113         snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
114
115         f = fopen(filename, "r");
116         if (f == NULL) {
117                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
118                         __func__);
119                 return -1;
120         }
121
122         ret = fscanf(f, "%u:%u", &major, &minor);
123         if (ret != 2) {
124                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
125                         __func__);
126                 fclose(f);
127                 return -1;
128         }
129         fclose(f);
130
131         /* create the char device "mknod /dev/uioX c major minor" */
132         snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
133         dev = makedev(major, minor);
134         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
135         if (f == NULL) {
136                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
137                         __func__, strerror(errno));
138                 return -1;
139         }
140
141         return ret;
142 }
143
144 /*
145  * Return the uioX char device used for a pci device. On success, return
146  * the UIO number and fill dstbuf string with the path of the device in
147  * sysfs. On error, return a negative value. In this case dstbuf is
148  * invalid.
149  */
150 static int
151 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
152                            unsigned int buflen, int create)
153 {
154         struct rte_pci_addr *loc = &dev->addr;
155         unsigned int uio_num;
156         struct dirent *e;
157         DIR *dir;
158         char dirname[PATH_MAX];
159
160         /* depending on kernel version, uio can be located in uio/uioX
161          * or uio:uioX */
162
163         snprintf(dirname, sizeof(dirname),
164                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
165                         loc->domain, loc->bus, loc->devid, loc->function);
166
167         dir = opendir(dirname);
168         if (dir == NULL) {
169                 /* retry with the parent directory */
170                 snprintf(dirname, sizeof(dirname),
171                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
172                                 loc->domain, loc->bus, loc->devid, loc->function);
173                 dir = opendir(dirname);
174
175                 if (dir == NULL) {
176                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
177                         return -1;
178                 }
179         }
180
181         /* take the first file starting with "uio" */
182         while ((e = readdir(dir)) != NULL) {
183                 /* format could be uio%d ...*/
184                 int shortprefix_len = sizeof("uio") - 1;
185                 /* ... or uio:uio%d */
186                 int longprefix_len = sizeof("uio:uio") - 1;
187                 char *endptr;
188
189                 if (strncmp(e->d_name, "uio", 3) != 0)
190                         continue;
191
192                 /* first try uio%d */
193                 errno = 0;
194                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
195                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
196                         snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
197                         break;
198                 }
199
200                 /* then try uio:uio%d */
201                 errno = 0;
202                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
203                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
204                         snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
205                         break;
206                 }
207         }
208         closedir(dir);
209
210         /* No uio resource found */
211         if (e == NULL)
212                 return -1;
213
214         /* create uio device if we've been asked to */
215         if (internal_config.create_uio_dev && create &&
216                         pci_mknod_uio_dev(dstbuf, uio_num) < 0)
217                 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
218
219         return uio_num;
220 }
221
222 void
223 pci_uio_free_resource(struct rte_pci_device *dev,
224                 struct mapped_pci_resource *uio_res)
225 {
226         rte_free(uio_res);
227
228         if (dev->intr_handle.uio_cfg_fd >= 0) {
229                 close(dev->intr_handle.uio_cfg_fd);
230                 dev->intr_handle.uio_cfg_fd = -1;
231         }
232         if (dev->intr_handle.fd) {
233                 close(dev->intr_handle.fd);
234                 dev->intr_handle.fd = -1;
235                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
236         }
237 }
238
239 int
240 pci_uio_alloc_resource(struct rte_pci_device *dev,
241                 struct mapped_pci_resource **uio_res)
242 {
243         char dirname[PATH_MAX];
244         char cfgname[PATH_MAX];
245         char devname[PATH_MAX]; /* contains the /dev/uioX */
246         int uio_num;
247         struct rte_pci_addr *loc;
248
249         loc = &dev->addr;
250
251         /* find uio resource */
252         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
253         if (uio_num < 0) {
254                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
255                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
256                 return 1;
257         }
258         snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
259
260         /* save fd if in primary process */
261         dev->intr_handle.fd = open(devname, O_RDWR);
262         if (dev->intr_handle.fd < 0) {
263                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
264                         devname, strerror(errno));
265                 goto error;
266         }
267
268         snprintf(cfgname, sizeof(cfgname),
269                         "/sys/class/uio/uio%u/device/config", uio_num);
270         dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
271         if (dev->intr_handle.uio_cfg_fd < 0) {
272                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
273                         cfgname, strerror(errno));
274                 goto error;
275         }
276
277         if (dev->kdrv == RTE_KDRV_IGB_UIO)
278                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
279         else {
280                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
281
282                 /* set bus master that is not done by uio_pci_generic */
283                 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
284                         RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
285                         goto error;
286                 }
287         }
288
289         /* allocate the mapping details for secondary processes*/
290         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
291         if (*uio_res == NULL) {
292                 RTE_LOG(ERR, EAL,
293                         "%s(): cannot store uio mmap details\n", __func__);
294                 goto error;
295         }
296
297         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
298         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
299
300         return 0;
301
302 error:
303         pci_uio_free_resource(dev, *uio_res);
304         return -1;
305 }
306
307 int
308 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
309                 struct mapped_pci_resource *uio_res, int map_idx)
310 {
311         int fd;
312         char devname[PATH_MAX]; /* contains the /dev/uioX */
313         void *mapaddr;
314         struct rte_pci_addr *loc;
315         struct pci_map *maps;
316
317         loc = &dev->addr;
318         maps = uio_res->maps;
319
320         /* update devname for mmap  */
321         snprintf(devname, sizeof(devname),
322                         SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
323                         loc->domain, loc->bus, loc->devid,
324                         loc->function, res_idx);
325
326         /* allocate memory to keep path */
327         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
328         if (maps[map_idx].path == NULL) {
329                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
330                                 strerror(errno));
331                 return -1;
332         }
333
334         /*
335          * open resource file, to mmap it
336          */
337         fd = open(devname, O_RDWR);
338         if (fd < 0) {
339                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
340                                 devname, strerror(errno));
341                 goto error;
342         }
343
344         /* try mapping somewhere close to the end of hugepages */
345         if (pci_map_addr == NULL)
346                 pci_map_addr = pci_find_max_end_va();
347
348         mapaddr = pci_map_resource(pci_map_addr, fd, 0,
349                         (size_t)dev->mem_resource[res_idx].len, 0);
350         close(fd);
351         if (mapaddr == MAP_FAILED)
352                 goto error;
353
354         pci_map_addr = RTE_PTR_ADD(mapaddr,
355                         (size_t)dev->mem_resource[res_idx].len);
356
357         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
358         maps[map_idx].size = dev->mem_resource[res_idx].len;
359         maps[map_idx].addr = mapaddr;
360         maps[map_idx].offset = 0;
361         strcpy(maps[map_idx].path, devname);
362         dev->mem_resource[res_idx].addr = mapaddr;
363
364         return 0;
365
366 error:
367         rte_free(maps[map_idx].path);
368         return -1;
369 }
370
371 int
372 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
373                    struct rte_pci_ioport *p)
374 {
375 #if defined(RTE_ARCH_X86)
376         char dirname[PATH_MAX];
377         char filename[PATH_MAX];
378         int uio_num;
379         unsigned long start;
380
381         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
382         if (uio_num < 0)
383                 return -1;
384
385         /* get portio start */
386         snprintf(filename, sizeof(filename),
387                  "%s/portio/port%d/start", dirname, bar);
388         if (eal_parse_sysfs_value(filename, &start) < 0) {
389                 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
390                         __func__);
391                 return -1;
392         }
393         /* ensure we don't get anything funny here, read/write will cast to
394          * uin16_t */
395         if (start > UINT16_MAX)
396                 return -1;
397
398         /* FIXME only for primary process ? */
399         if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
400
401                 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
402                 dev->intr_handle.fd = open(filename, O_RDWR);
403                 if (dev->intr_handle.fd < 0) {
404                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
405                                 filename, strerror(errno));
406                         return -1;
407                 }
408                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
409         }
410
411         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
412
413         p->base = start;
414         return 0;
415 #else
416         RTE_SET_USED(dev);
417         RTE_SET_USED(bar);
418         RTE_SET_USED(p);
419         return -1;
420 #endif
421 }
422
423 void
424 pci_uio_ioport_read(struct rte_pci_ioport *p,
425                     void *data, size_t len, off_t offset)
426 {
427 #if defined(RTE_ARCH_X86)
428         uint8_t *d;
429         int size;
430         unsigned short reg = p->base + offset;
431
432         for (d = data; len > 0; d += size, reg += size, len -= size) {
433                 if (len >= 4) {
434                         size = 4;
435                         *(uint32_t *)d = inl(reg);
436                 } else if (len >= 2) {
437                         size = 2;
438                         *(uint16_t *)d = inw(reg);
439                 } else {
440                         size = 1;
441                         *d = inb(reg);
442                 }
443         }
444 #else
445         RTE_SET_USED(p);
446         RTE_SET_USED(data);
447         RTE_SET_USED(len);
448         RTE_SET_USED(offset);
449 #endif
450 }
451
452 void
453 pci_uio_ioport_write(struct rte_pci_ioport *p,
454                      const void *data, size_t len, off_t offset)
455 {
456 #if defined(RTE_ARCH_X86)
457         const uint8_t *s;
458         int size;
459         unsigned short reg = p->base + offset;
460
461         for (s = data; len > 0; s += size, reg += size, len -= size) {
462                 if (len >= 4) {
463                         size = 4;
464                         outl_p(*(const uint32_t *)s, reg);
465                 } else if (len >= 2) {
466                         size = 2;
467                         outw_p(*(const uint16_t *)s, reg);
468                 } else {
469                         size = 1;
470                         outb_p(*s, reg);
471                 }
472         }
473 #else
474         RTE_SET_USED(p);
475         RTE_SET_USED(data);
476         RTE_SET_USED(len);
477         RTE_SET_USED(offset);
478 #endif
479 }
480
481 int
482 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
483 {
484         RTE_SET_USED(p);
485 #if defined(RTE_ARCH_X86)
486         /* FIXME close intr fd ? */
487         return 0;
488 #else
489         return -1;
490 #endif
491 }