DPDK  16.07.2
rte_pci.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2015 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 /* BSD LICENSE
34  *
35  * Copyright 2013-2014 6WIND S.A.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  * * Redistributions of source code must retain the above copyright
42  * notice, this list of conditions and the following disclaimer.
43  * * Redistributions in binary form must reproduce the above copyright
44  * notice, this list of conditions and the following disclaimer in
45  * the documentation and/or other materials provided with the
46  * distribution.
47  * * Neither the name of 6WIND S.A. nor the names of its
48  * contributors may be used to endorse or promote products derived
49  * from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #ifndef _RTE_PCI_H_
65 #define _RTE_PCI_H_
66 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <limits.h>
80 #include <errno.h>
81 #include <sys/queue.h>
82 #include <stdint.h>
83 #include <inttypes.h>
84 
85 #include <rte_interrupts.h>
86 
87 TAILQ_HEAD(pci_device_list, rte_pci_device);
88 TAILQ_HEAD(pci_driver_list, rte_pci_driver);
90 extern struct pci_driver_list pci_driver_list;
91 extern struct pci_device_list pci_device_list;
94 const char *pci_get_sysfs_path(void);
95 
97 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
98 
100 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
101 
103 #define PCI_FMT_NVAL 4
104 
106 #define PCI_RESOURCE_FMT_NVAL 3
107 
112  uint64_t phys_addr;
113  uint64_t len;
114  void *addr;
115 };
116 
118 #define PCI_MAX_RESOURCE 6
119 
124 struct rte_pci_id {
125  uint32_t class_id;
126  uint16_t vendor_id;
127  uint16_t device_id;
130 };
131 
135 struct rte_pci_addr {
136  uint16_t domain;
137  uint8_t bus;
138  uint8_t devid;
139  uint8_t function;
140 };
141 
142 struct rte_devargs;
143 
144 enum rte_kernel_driver {
145  RTE_KDRV_UNKNOWN = 0,
146  RTE_KDRV_IGB_UIO,
147  RTE_KDRV_VFIO,
148  RTE_KDRV_UIO_GENERIC,
149  RTE_KDRV_NIC_UIO,
150  RTE_KDRV_NONE,
151 };
152 
159  struct rte_pci_id id;
161  struct rte_intr_handle intr_handle;
163  uint16_t max_vfs;
164  int numa_node;
166  enum rte_kernel_driver kdrv;
167 };
168 
170 #define PCI_ANY_ID (0xffff)
171 #define RTE_CLASS_ANY_ID (0xffffff)
172 
173 #ifdef __cplusplus
174 
175 #define RTE_PCI_DEVICE(vend, dev) \
176  RTE_CLASS_ANY_ID, \
177  (vend), \
178  (dev), \
179  PCI_ANY_ID, \
180  PCI_ANY_ID
181 #else
182 
183 #define RTE_PCI_DEVICE(vend, dev) \
184  .class_id = RTE_CLASS_ANY_ID, \
185  .vendor_id = (vend), \
186  .device_id = (dev), \
187  .subsystem_vendor_id = PCI_ANY_ID, \
188  .subsystem_device_id = PCI_ANY_ID
189 #endif
190 
191 struct rte_pci_driver;
192 
196 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
197 
201 typedef int (pci_devuninit_t)(struct rte_pci_device *);
202 
208  const char *name;
211  const struct rte_pci_id *id_table;
212  uint32_t drv_flags;
213 };
214 
216 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
217 
218 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
219 
220 #define RTE_PCI_DRV_INTR_LSC 0x0008
221 
222 #define RTE_PCI_DRV_DETACHABLE 0x0010
223 
227 struct pci_map {
228  void *addr;
229  char *path;
230  uint64_t offset;
231  uint64_t size;
232  uint64_t phaddr;
233 };
234 
241  TAILQ_ENTRY(mapped_pci_resource) next;
242 
243  struct rte_pci_addr pci_addr;
244  char path[PATH_MAX];
245  int nb_maps;
246  struct pci_map maps[PCI_MAX_RESOURCE];
247 };
248 
250 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
251 
253 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
254 do { \
255  unsigned long val; \
256  char *end; \
257  errno = 0; \
258  val = strtoul((in), &end, 16); \
259  if (errno != 0 || end[0] != (dlm) || val > (lim)) \
260  return -EINVAL; \
261  (fd) = (typeof (fd))val; \
262  (in) = end + 1; \
263 } while(0)
264 
278 static inline int
279 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
280 {
281  dev_addr->domain = 0;
282  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
283  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
284  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
285  return 0;
286 }
287 
300 static inline int
301 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
302 {
303  GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
304  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
305  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
306  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
307  return 0;
308 }
309 #undef GET_PCIADDR_FIELD
310 
311 /* Compare two PCI device addresses. */
324 static inline int
326  const struct rte_pci_addr *addr2)
327 {
328  uint64_t dev_addr, dev_addr2;
329 
330  if ((addr == NULL) || (addr2 == NULL))
331  return -1;
332 
333  dev_addr = (addr->domain << 24) | (addr->bus << 16) |
334  (addr->devid << 8) | addr->function;
335  dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
336  (addr2->devid << 8) | addr2->function;
337 
338  if (dev_addr > dev_addr2)
339  return 1;
340  else if (dev_addr < dev_addr2)
341  return -1;
342  else
343  return 0;
344 }
345 
353 int rte_eal_pci_scan(void);
354 
366 int rte_eal_pci_probe(void);
367 
383 int rte_eal_pci_map_device(struct rte_pci_device *dev);
384 
392 void rte_eal_pci_unmap_device(struct rte_pci_device *dev);
393 
412 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
413  size_t size, int additional_flags);
414 
424 void pci_unmap_resource(void *requested_addr, size_t size);
425 
439 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
440 
454 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
455 
462 void rte_eal_pci_dump(FILE *f);
463 
471 void rte_eal_pci_register(struct rte_pci_driver *driver);
472 
480 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
481 
495 int rte_eal_pci_read_config(const struct rte_pci_device *device,
496  void *buf, size_t len, off_t offset);
497 
511 int rte_eal_pci_write_config(const struct rte_pci_device *device,
512  const void *buf, size_t len, off_t offset);
513 
520  struct rte_pci_device *dev;
521  uint64_t base;
522  uint64_t len; /* only filled for memory mapped ports */
523 };
524 
540 int rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
541  struct rte_pci_ioport *p);
542 
552 
566  void *data, size_t len, off_t offset);
567 
581  const void *data, size_t len, off_t offset);
582 
583 #ifdef __cplusplus
584 }
585 #endif
586 
587 #endif /* _RTE_PCI_H_ */