DPDK  16.04.0
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 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
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 
109 #define IORESOURCE_MEM 0x00000200
110 
115  uint64_t phys_addr;
116  uint64_t len;
117  void *addr;
118 };
119 
121 #define PCI_MAX_RESOURCE 6
122 
127 struct rte_pci_id {
128  uint16_t vendor_id;
129  uint16_t device_id;
132 };
133 
137 struct rte_pci_addr {
138  uint16_t domain;
139  uint8_t bus;
140  uint8_t devid;
141  uint8_t function;
142 };
143 
144 struct rte_devargs;
145 
146 enum rte_kernel_driver {
147  RTE_KDRV_UNKNOWN = 0,
148  RTE_KDRV_IGB_UIO,
149  RTE_KDRV_VFIO,
150  RTE_KDRV_UIO_GENERIC,
151  RTE_KDRV_NIC_UIO,
152  RTE_KDRV_NONE,
153 };
154 
161  struct rte_pci_id id;
163  struct rte_intr_handle intr_handle;
165  uint16_t max_vfs;
166  int numa_node;
168  enum rte_kernel_driver kdrv;
169 };
170 
172 #define PCI_ANY_ID (0xffff)
173 
174 #ifdef __cplusplus
175 
176 #define RTE_PCI_DEVICE(vend, dev) \
177  (vend), \
178  (dev), \
179  PCI_ANY_ID, \
180  PCI_ANY_ID
181 #else
182 
183 #define RTE_PCI_DEVICE(vend, dev) \
184  .vendor_id = (vend), \
185  .device_id = (dev), \
186  .subsystem_vendor_id = PCI_ANY_ID, \
187  .subsystem_device_id = PCI_ANY_ID
188 #endif
189 
190 struct rte_pci_driver;
191 
195 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
196 
200 typedef int (pci_devuninit_t)(struct rte_pci_device *);
201 
207  const char *name;
210  const struct rte_pci_id *id_table;
211  uint32_t drv_flags;
212 };
213 
215 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
216 
217 #pragma GCC poison RTE_PCI_DRV_MULTIPLE
218 
219 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
220 
221 #define RTE_PCI_DRV_INTR_LSC 0x0008
222 
223 #define RTE_PCI_DRV_DETACHABLE 0x0010
224 
228 struct pci_map {
229  void *addr;
230  char *path;
231  uint64_t offset;
232  uint64_t size;
233  uint64_t phaddr;
234 };
235 
242  TAILQ_ENTRY(mapped_pci_resource) next;
243 
244  struct rte_pci_addr pci_addr;
245  char path[PATH_MAX];
246  int nb_maps;
247  struct pci_map maps[PCI_MAX_RESOURCE];
248 };
249 
251 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
252 
254 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
255 do { \
256  unsigned long val; \
257  char *end; \
258  errno = 0; \
259  val = strtoul((in), &end, 16); \
260  if (errno != 0 || end[0] != (dlm) || val > (lim)) \
261  return -EINVAL; \
262  (fd) = (typeof (fd))val; \
263  (in) = end + 1; \
264 } while(0)
265 
279 static inline int
280 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
281 {
282  dev_addr->domain = 0;
283  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
284  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
285  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
286  return 0;
287 }
288 
301 static inline int
302 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
303 {
304  GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
305  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
306  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
307  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
308  return 0;
309 }
310 #undef GET_PCIADDR_FIELD
311 
312 /* Compare two PCI device addresses. */
325 static inline int
327  const struct rte_pci_addr *addr2)
328 {
329  uint64_t dev_addr, dev_addr2;
330 
331  if ((addr == NULL) || (addr2 == NULL))
332  return -1;
333 
334  dev_addr = (addr->domain << 24) | (addr->bus << 16) |
335  (addr->devid << 8) | addr->function;
336  dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
337  (addr2->devid << 8) | addr2->function;
338 
339  if (dev_addr > dev_addr2)
340  return 1;
341  else if (dev_addr < dev_addr2)
342  return -1;
343  else
344  return 0;
345 }
346 
354 int rte_eal_pci_scan(void);
355 
367 int rte_eal_pci_probe(void);
368 
384 int rte_eal_pci_map_device(struct rte_pci_device *dev);
385 
393 void rte_eal_pci_unmap_device(struct rte_pci_device *dev);
394 
413 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
414  size_t size, int additional_flags);
415 
425 void pci_unmap_resource(void *requested_addr, size_t size);
426 
440 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
441 
455 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
456 
463 void rte_eal_pci_dump(FILE *f);
464 
472 void rte_eal_pci_register(struct rte_pci_driver *driver);
473 
481 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
482 
496 int rte_eal_pci_read_config(const struct rte_pci_device *device,
497  void *buf, size_t len, off_t offset);
498 
512 int rte_eal_pci_write_config(const struct rte_pci_device *device,
513  const void *buf, size_t len, off_t offset);
514 
521  struct rte_pci_device *dev;
522  uint64_t base;
523 };
524 
539 int rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
540  struct rte_pci_ioport *p);
541 
549 
563  void *data, size_t len, off_t offset);
564 
578  const void *data, size_t len, off_t offset);
579 
580 #ifdef RTE_PCI_CONFIG
581 #include <rte_common.h>
591 void pci_config_space_set(struct rte_pci_device *dev) __rte_deprecated;
592 #endif /* RTE_PCI_CONFIG */
593 
594 #ifdef __cplusplus
595 }
596 #endif
597 
598 #endif /* _RTE_PCI_H_ */