pci: reject negative values in PCI id
[dpdk.git] / lib / librte_pci / rte_pci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #include <string.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12 #include <sys/mman.h>
13
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
16 #include <rte_log.h>
17 #include <rte_bus.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
20 #include <rte_eal.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.h>
23 #include <rte_debug.h>
24
25 #include "rte_pci.h"
26
27 static inline const char *
28 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
29 {
30         unsigned long val;
31         uint8_t *u8 = _u8;
32         char *end;
33
34         /* empty string is an error though strtoul() returns 0 */
35         if (*in == '\0')
36                 return NULL;
37
38         /* PCI field starting with spaces is forbidden.
39          * Negative wrap-around is not reported as an error by strtoul.
40          */
41         if (*in == ' ' || *in == '-')
42                 return NULL;
43
44         errno = 0;
45         val = strtoul(in, &end, 16);
46         if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
47                 errno = errno ? errno : EINVAL;
48                 return NULL;
49         }
50         *u8 = (uint8_t)val;
51         return end + 1;
52 }
53
54 static int
55 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
56 {
57         const char *in = input;
58
59         dev_addr->domain = 0;
60         in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
61         if (in == NULL)
62                 return -EINVAL;
63         in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
64         if (in == NULL)
65                 return -EINVAL;
66         in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
67         if (in == NULL)
68                 return -EINVAL;
69         return 0;
70 }
71
72 static int
73 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
74 {
75         const char *in = input;
76         unsigned long val;
77         char *end;
78
79         /* PCI id starting with spaces is forbidden.
80          * Negative wrap-around is not reported as an error by strtoul.
81          */
82         if (*in == ' ' || *in == '-')
83                 return -EINVAL;
84
85         errno = 0;
86         val = strtoul(in, &end, 16);
87         if (errno != 0 || end[0] != ':' || val > UINT32_MAX)
88                 return -EINVAL;
89         dev_addr->domain = (uint32_t)val;
90         in = end + 1;
91         in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
92         if (in == NULL)
93                 return -EINVAL;
94         in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
95         if (in == NULL)
96                 return -EINVAL;
97         in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
98         if (in == NULL)
99                 return -EINVAL;
100         return 0;
101 }
102
103 void
104 rte_pci_device_name(const struct rte_pci_addr *addr,
105                 char *output, size_t size)
106 {
107         RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
108         RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
109                             addr->domain, addr->bus,
110                             addr->devid, addr->function) >= 0);
111 }
112
113 int
114 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
115              const struct rte_pci_addr *addr2)
116 {
117         uint64_t dev_addr, dev_addr2;
118
119         if ((addr == NULL) || (addr2 == NULL))
120                 return -1;
121
122         dev_addr = ((uint64_t)addr->domain << 24) |
123                 (addr->bus << 16) | (addr->devid << 8) | addr->function;
124         dev_addr2 = ((uint64_t)addr2->domain << 24) |
125                 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
126
127         if (dev_addr > dev_addr2)
128                 return 1;
129         else if (dev_addr < dev_addr2)
130                 return -1;
131         else
132                 return 0;
133 }
134
135 int
136 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
137 {
138         if (pci_bdf_parse(str, addr) == 0 ||
139             pci_dbdf_parse(str, addr) == 0)
140                 return 0;
141         return -1;
142 }
143
144
145 /* map a particular resource from a file */
146 void *
147 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
148                  int additional_flags)
149 {
150         void *mapaddr;
151
152         /* Map the PCI memory resource of device */
153         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
154                         MAP_SHARED | additional_flags, fd, offset);
155         if (mapaddr == MAP_FAILED) {
156                 RTE_LOG(ERR, EAL,
157                         "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
158                         __func__, fd, requested_addr, size,
159                         (unsigned long long)offset,
160                         strerror(errno), mapaddr);
161         } else
162                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
163
164         return mapaddr;
165 }
166
167 /* unmap a particular resource */
168 void
169 pci_unmap_resource(void *requested_addr, size_t size)
170 {
171         if (requested_addr == NULL)
172                 return;
173
174         /* Unmap the PCI memory resource of device */
175         if (munmap(requested_addr, size)) {
176                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
177                         __func__, requested_addr, size,
178                         strerror(errno));
179         } else
180                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
181                                 requested_addr);
182 }