4351abc596d223191b3efb9ff6446f839254e918
[dpdk.git] / lib / librte_pci / rte_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <string.h>
36 #include <inttypes.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <sys/queue.h>
41 #include <sys/mman.h>
42
43 #include <rte_errno.h>
44 #include <rte_interrupts.h>
45 #include <rte_log.h>
46 #include <rte_bus.h>
47 #include <rte_per_lcore.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_eal.h>
51 #include <rte_string_fns.h>
52 #include <rte_common.h>
53
54 #include "rte_pci.h"
55
56 static inline const char *
57 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
58 {
59         unsigned long val;
60         uint8_t *u8 = _u8;
61         char *end;
62
63         errno = 0;
64         val = strtoul(in, &end, 16);
65         if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
66                 errno = errno ? errno : EINVAL;
67                 return NULL;
68         }
69         *u8 = (uint8_t)val;
70         return end + 1;
71 }
72
73 static int
74 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
75 {
76         const char *in = input;
77
78         dev_addr->domain = 0;
79         in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
80         if (in == NULL)
81                 return -EINVAL;
82         in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
83         if (in == NULL)
84                 return -EINVAL;
85         in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
86         if (in == NULL)
87                 return -EINVAL;
88         return 0;
89 }
90
91 static int
92 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
93 {
94         const char *in = input;
95         unsigned long val;
96         char *end;
97
98         errno = 0;
99         val = strtoul(in, &end, 16);
100         if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
101                 return -EINVAL;
102         dev_addr->domain = (uint16_t)val;
103         in = end + 1;
104         in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
105         if (in == NULL)
106                 return -EINVAL;
107         in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
108         if (in == NULL)
109                 return -EINVAL;
110         in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
111         if (in == NULL)
112                 return -EINVAL;
113         return 0;
114 }
115
116 int
117 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
118 {
119         return pci_bdf_parse(input, dev_addr);
120 }
121
122 int
123 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
124 {
125         return pci_dbdf_parse(input, dev_addr);
126 }
127
128 void
129 rte_pci_device_name(const struct rte_pci_addr *addr,
130                 char *output, size_t size)
131 {
132         RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
133         RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
134                             addr->domain, addr->bus,
135                             addr->devid, addr->function) >= 0);
136 }
137
138 int
139 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
140                          const struct rte_pci_addr *addr2)
141 {
142         return rte_pci_addr_cmp(addr, addr2);
143 }
144
145 int
146 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
147              const struct rte_pci_addr *addr2)
148 {
149         uint64_t dev_addr, dev_addr2;
150
151         if ((addr == NULL) || (addr2 == NULL))
152                 return -1;
153
154         dev_addr = ((uint64_t)addr->domain << 24) |
155                 (addr->bus << 16) | (addr->devid << 8) | addr->function;
156         dev_addr2 = ((uint64_t)addr2->domain << 24) |
157                 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
158
159         if (dev_addr > dev_addr2)
160                 return 1;
161         else if (dev_addr < dev_addr2)
162                 return -1;
163         else
164                 return 0;
165 }
166
167 int
168 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
169 {
170         if (pci_bdf_parse(str, addr) == 0 ||
171             pci_dbdf_parse(str, addr) == 0)
172                 return 0;
173         return -1;
174 }
175
176
177 /* map a particular resource from a file */
178 void *
179 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
180                  int additional_flags)
181 {
182         void *mapaddr;
183
184         /* Map the PCI memory resource of device */
185         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
186                         MAP_SHARED | additional_flags, fd, offset);
187         if (mapaddr == MAP_FAILED) {
188                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
189                         __func__, fd, requested_addr,
190                         (unsigned long)size, (unsigned long)offset,
191                         strerror(errno), mapaddr);
192         } else
193                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
194
195         return mapaddr;
196 }
197
198 /* unmap a particular resource */
199 void
200 pci_unmap_resource(void *requested_addr, size_t size)
201 {
202         if (requested_addr == NULL)
203                 return;
204
205         /* Unmap the PCI memory resource of device */
206         if (munmap(requested_addr, size)) {
207                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
208                         __func__, requested_addr, (unsigned long)size,
209                         strerror(errno));
210         } else
211                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
212                                 requested_addr);
213 }