DPDK  17.05.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_debug.h>
86 #include <rte_interrupts.h>
87 #include <rte_dev.h>
88 #include <rte_bus.h>
89 
91 const char *pci_get_sysfs_path(void);
92 
94 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
95 #define PCI_PRI_STR_SIZE sizeof("XXXX:XX:XX.X")
96 
98 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
99 
101 #define PCI_FMT_NVAL 4
102 
104 #define PCI_RESOURCE_FMT_NVAL 3
105 
107 #define PCI_MAX_RESOURCE 6
108 
110 #define PCI_BUS_NAME "PCI"
111 
112 /* Forward declarations */
113 struct rte_pci_device;
114 struct rte_pci_driver;
115 
117 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
119 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
120 
121 /* PCI Bus iterators */
122 #define FOREACH_DEVICE_ON_PCIBUS(p) \
123  TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
124 
125 #define FOREACH_DRIVER_ON_PCIBUS(p) \
126  TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
127 
132 struct rte_pci_id {
133  uint32_t class_id;
134  uint16_t vendor_id;
135  uint16_t device_id;
138 };
139 
143 struct rte_pci_addr {
144  uint16_t domain;
145  uint8_t bus;
146  uint8_t devid;
147  uint8_t function;
148 };
149 
150 struct rte_devargs;
151 
152 enum rte_kernel_driver {
153  RTE_KDRV_UNKNOWN = 0,
154  RTE_KDRV_IGB_UIO,
155  RTE_KDRV_VFIO,
156  RTE_KDRV_UIO_GENERIC,
157  RTE_KDRV_NIC_UIO,
158  RTE_KDRV_NONE,
159 };
160 
168  struct rte_pci_id id;
171  struct rte_intr_handle intr_handle;
173  uint16_t max_vfs;
174  enum rte_kernel_driver kdrv;
175  char name[PCI_PRI_STR_SIZE+1];
176 };
177 
182 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
183 
185 #define PCI_ANY_ID (0xffff)
186 #define RTE_CLASS_ANY_ID (0xffffff)
187 
188 #ifdef __cplusplus
189 
190 #define RTE_PCI_DEVICE(vend, dev) \
191  RTE_CLASS_ANY_ID, \
192  (vend), \
193  (dev), \
194  PCI_ANY_ID, \
195  PCI_ANY_ID
196 #else
197 
198 #define RTE_PCI_DEVICE(vend, dev) \
199  .class_id = RTE_CLASS_ANY_ID, \
200  .vendor_id = (vend), \
201  .device_id = (dev), \
202  .subsystem_vendor_id = PCI_ANY_ID, \
203  .subsystem_device_id = PCI_ANY_ID
204 #endif
205 
209 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
210 
214 typedef int (pci_remove_t)(struct rte_pci_device *);
215 
222  struct rte_pci_bus *bus;
224  pci_remove_t *remove;
225  const struct rte_pci_id *id_table;
226  uint32_t drv_flags;
227 };
228 
232 struct rte_pci_bus {
233  struct rte_bus bus;
234  struct rte_pci_device_list device_list;
235  struct rte_pci_driver_list driver_list;
236 };
237 
239 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
240 
241 #define RTE_PCI_DRV_INTR_LSC 0x0008
242 
243 #define RTE_PCI_DRV_INTR_RMV 0x0010
244 
248 struct pci_map {
249  void *addr;
250  char *path;
251  uint64_t offset;
252  uint64_t size;
253  uint64_t phaddr;
254 };
255 
262  TAILQ_ENTRY(mapped_pci_resource) next;
263 
264  struct rte_pci_addr pci_addr;
265  char path[PATH_MAX];
266  int nb_maps;
267  struct pci_map maps[PCI_MAX_RESOURCE];
268 };
269 
271 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
272 
274 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
275 do { \
276  unsigned long val; \
277  char *end; \
278  errno = 0; \
279  val = strtoul((in), &end, 16); \
280  if (errno != 0 || end[0] != (dlm) || val > (lim)) \
281  return -EINVAL; \
282  (fd) = (typeof (fd))val; \
283  (in) = end + 1; \
284 } while(0)
285 
299 static inline int
300 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
301 {
302  dev_addr->domain = 0;
303  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
304  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
305  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
306  return 0;
307 }
308 
321 static inline int
322 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
323 {
324  GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
325  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
326  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
327  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
328  return 0;
329 }
330 #undef GET_PCIADDR_FIELD
331 
344 static inline void
346  char *output, size_t size)
347 {
348  RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
349  RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
350  addr->domain, addr->bus,
351  addr->devid, addr->function) >= 0);
352 }
353 
354 /* Compare two PCI device addresses. */
367 static inline int
369  const struct rte_pci_addr *addr2)
370 {
371  uint64_t dev_addr, dev_addr2;
372 
373  if ((addr == NULL) || (addr2 == NULL))
374  return -1;
375 
376  dev_addr = (addr->domain << 24) | (addr->bus << 16) |
377  (addr->devid << 8) | addr->function;
378  dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
379  (addr2->devid << 8) | addr2->function;
380 
381  if (dev_addr > dev_addr2)
382  return 1;
383  else if (dev_addr < dev_addr2)
384  return -1;
385  else
386  return 0;
387 }
388 
396 int rte_pci_scan(void);
397 
405 int
406 rte_pci_probe(void);
407 
423 int rte_pci_map_device(struct rte_pci_device *dev);
424 
432 void rte_pci_unmap_device(struct rte_pci_device *dev);
433 
452 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
453  size_t size, int additional_flags);
454 
464 void pci_unmap_resource(void *requested_addr, size_t size);
465 
479 int rte_pci_probe_one(const struct rte_pci_addr *addr);
480 
494 int rte_pci_detach(const struct rte_pci_addr *addr);
495 
502 void rte_pci_dump(FILE *f);
503 
511 void rte_pci_register(struct rte_pci_driver *driver);
512 
514 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
515 RTE_INIT(pciinitfn_ ##nm); \
516 static void pciinitfn_ ##nm(void) \
517 {\
518  (pci_drv).driver.name = RTE_STR(nm);\
519  rte_pci_register(&pci_drv); \
520 } \
521 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
522 
530 void rte_pci_unregister(struct rte_pci_driver *driver);
531 
545 int rte_pci_read_config(const struct rte_pci_device *device,
546  void *buf, size_t len, off_t offset);
547 
561 int rte_pci_write_config(const struct rte_pci_device *device,
562  const void *buf, size_t len, off_t offset);
563 
570  struct rte_pci_device *dev;
571  uint64_t base;
572  uint64_t len; /* only filled for memory mapped ports */
573 };
574 
590 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
591  struct rte_pci_ioport *p);
592 
601 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
602 
615 void rte_pci_ioport_read(struct rte_pci_ioport *p,
616  void *data, size_t len, off_t offset);
617 
630 void rte_pci_ioport_write(struct rte_pci_ioport *p,
631  const void *data, size_t len, off_t offset);
632 
633 #ifdef __cplusplus
634 }
635 #endif
636 
637 #endif /* _RTE_PCI_H_ */