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 
165  TAILQ_ENTRY(rte_pci_device) next;
166  struct rte_device device;
167  struct rte_pci_addr addr;
168  struct rte_pci_id id;
169  struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
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 
220  TAILQ_ENTRY(rte_pci_driver) next;
221  struct rte_driver driver;
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 
245 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
246 
250 struct pci_map {
251  void *addr;
252  char *path;
253  uint64_t offset;
254  uint64_t size;
255  uint64_t phaddr;
256 };
257 
264  TAILQ_ENTRY(mapped_pci_resource) next;
265 
266  struct rte_pci_addr pci_addr;
267  char path[PATH_MAX];
268  int nb_maps;
269  struct pci_map maps[PCI_MAX_RESOURCE];
270 };
271 
273 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
274 
276 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
277 do { \
278  unsigned long val; \
279  char *end; \
280  errno = 0; \
281  val = strtoul((in), &end, 16); \
282  if (errno != 0 || end[0] != (dlm) || val > (lim)) \
283  return -EINVAL; \
284  (fd) = (typeof (fd))val; \
285  (in) = end + 1; \
286 } while(0)
287 
301 static inline int
302 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
303 {
304  dev_addr->domain = 0;
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 
323 static inline int
324 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
325 {
326  GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
327  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
328  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
329  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
330  return 0;
331 }
332 #undef GET_PCIADDR_FIELD
333 
346 static inline void
348  char *output, size_t size)
349 {
350  RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
351  RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
352  addr->domain, addr->bus,
353  addr->devid, addr->function) >= 0);
354 }
355 
356 /* Compare two PCI device addresses. */
369 static inline int
371  const struct rte_pci_addr *addr2)
372 {
373  uint64_t dev_addr, dev_addr2;
374 
375  if ((addr == NULL) || (addr2 == NULL))
376  return -1;
377 
378  dev_addr = (addr->domain << 24) | (addr->bus << 16) |
379  (addr->devid << 8) | addr->function;
380  dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
381  (addr2->devid << 8) | addr2->function;
382 
383  if (dev_addr > dev_addr2)
384  return 1;
385  else if (dev_addr < dev_addr2)
386  return -1;
387  else
388  return 0;
389 }
390 
398 int rte_pci_scan(void);
399 
407 int
408 rte_pci_probe(void);
409 
425 int rte_pci_map_device(struct rte_pci_device *dev);
426 
434 void rte_pci_unmap_device(struct rte_pci_device *dev);
435 
454 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
455  size_t size, int additional_flags);
456 
466 void pci_unmap_resource(void *requested_addr, size_t size);
467 
481 int rte_pci_probe_one(const struct rte_pci_addr *addr);
482 
496 int rte_pci_detach(const struct rte_pci_addr *addr);
497 
504 void rte_pci_dump(FILE *f);
505 
513 void rte_pci_register(struct rte_pci_driver *driver);
514 
516 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
517 RTE_INIT(pciinitfn_ ##nm); \
518 static void pciinitfn_ ##nm(void) \
519 {\
520  (pci_drv).driver.name = RTE_STR(nm);\
521  rte_pci_register(&pci_drv); \
522 } \
523 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
524 
532 void rte_pci_unregister(struct rte_pci_driver *driver);
533 
547 int rte_pci_read_config(const struct rte_pci_device *device,
548  void *buf, size_t len, off_t offset);
549 
563 int rte_pci_write_config(const struct rte_pci_device *device,
564  const void *buf, size_t len, off_t offset);
565 
572  struct rte_pci_device *dev;
573  uint64_t base;
574  uint64_t len; /* only filled for memory mapped ports */
575 };
576 
592 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
593  struct rte_pci_ioport *p);
594 
603 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
604 
617 void rte_pci_ioport_read(struct rte_pci_ioport *p,
618  void *data, size_t len, off_t offset);
619 
632 void rte_pci_ioport_write(struct rte_pci_ioport *p,
633  const void *data, size_t len, off_t offset);
634 
635 #ifdef __cplusplus
636 }
637 #endif
638 
639 #endif /* _RTE_PCI_H_ */
struct rte_pci_driver * driver
Definition: rte_pci.h:172
int rte_pci_probe_one(const struct rte_pci_addr *addr)
void rte_pci_ioport_write(struct rte_pci_ioport *p, const void *data, size_t len, off_t offset)
uint16_t domain
Definition: rte_pci.h:144
int( pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *)
Definition: rte_pci.h:209
int rte_pci_scan(void)
uint16_t device_id
Definition: rte_pci.h:135
uint8_t bus
Definition: rte_pci.h:145
uint8_t function
Definition: rte_pci.h:147
uint32_t class_id
Definition: rte_pci.h:133
#define PCI_PRI_FMT
Definition: rte_pci.h:94
uint8_t devid
Definition: rte_pci.h:146
static int eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
Definition: rte_pci.h:324
static int eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
Definition: rte_pci.h:302
const struct rte_pci_id * id_table
Definition: rte_pci.h:225
#define PCI_MAX_RESOURCE
Definition: rte_pci.h:107
uint16_t subsystem_device_id
Definition: rte_pci.h:137
void rte_pci_register(struct rte_pci_driver *driver)
int rte_pci_probe(void)
void rte_pci_unmap_device(struct rte_pci_device *dev)
int rte_pci_map_device(struct rte_pci_device *dev)
pci_probe_t * probe
Definition: rte_pci.h:223
void rte_pci_dump(FILE *f)
int rte_pci_detach(const struct rte_pci_addr *addr)
int rte_pci_write_config(const struct rte_pci_device *device, const void *buf, size_t len, off_t offset)
int rte_pci_ioport_unmap(struct rte_pci_ioport *p)
int rte_pci_ioport_map(struct rte_pci_device *dev, int bar, struct rte_pci_ioport *p)
struct rte_pci_bus * bus
Definition: rte_pci.h:222
TAILQ_HEAD(rte_pci_device_list, rte_pci_device)
uint32_t drv_flags
Definition: rte_pci.h:226
int( pci_remove_t)(struct rte_pci_device *)
Definition: rte_pci.h:214
int rte_pci_read_config(const struct rte_pci_device *device, void *buf, size_t len, off_t offset)
void rte_pci_unregister(struct rte_pci_driver *driver)
uint16_t subsystem_vendor_id
Definition: rte_pci.h:136
static void rte_pci_device_name(const struct rte_pci_addr *addr, char *output, size_t size)
Definition: rte_pci.h:347
void rte_pci_ioport_read(struct rte_pci_ioport *p, void *data, size_t len, off_t offset)
uint16_t vendor_id
Definition: rte_pci.h:134
uint16_t max_vfs
Definition: rte_pci.h:173
const char * pci_get_sysfs_path(void)
static int rte_eal_compare_pci_addr(const struct rte_pci_addr *addr, const struct rte_pci_addr *addr2)
Definition: rte_pci.h:370