DPDK  17.08.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  * Copyright 2013-2014 6WIND S.A.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  * * Neither the name of Intel Corporation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _RTE_PCI_H_
36 #define _RTE_PCI_H_
37 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <errno.h>
52 #include <sys/queue.h>
53 #include <stdint.h>
54 #include <inttypes.h>
55 
56 #include <rte_debug.h>
57 #include <rte_interrupts.h>
58 #include <rte_dev.h>
59 #include <rte_bus.h>
60 
62 const char *pci_get_sysfs_path(void);
63 
65 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
66 #define PCI_PRI_STR_SIZE sizeof("XXXXXXXX:XX:XX.X")
67 
69 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
70 
72 #define PCI_FMT_NVAL 4
73 
75 #define PCI_RESOURCE_FMT_NVAL 3
76 
78 #define PCI_MAX_RESOURCE 6
79 
80 /* Forward declarations */
81 struct rte_pci_device;
82 struct rte_pci_driver;
83 
85 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
87 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
88 
89 /* PCI Bus iterators */
90 #define FOREACH_DEVICE_ON_PCIBUS(p) \
91  TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
92 
93 #define FOREACH_DRIVER_ON_PCIBUS(p) \
94  TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
95 
100 struct rte_pci_id {
101  uint32_t class_id;
102  uint16_t vendor_id;
103  uint16_t device_id;
106 };
107 
111 struct rte_pci_addr {
112  uint32_t domain;
113  uint8_t bus;
114  uint8_t devid;
115  uint8_t function;
116 };
117 
118 struct rte_devargs;
119 
127  struct rte_pci_id id;
130  struct rte_intr_handle intr_handle;
132  uint16_t max_vfs;
134  char name[PCI_PRI_STR_SIZE+1];
135 };
136 
141 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
142 
144 #define PCI_ANY_ID (0xffff)
145 #define RTE_CLASS_ANY_ID (0xffffff)
146 
147 #ifdef __cplusplus
148 
149 #define RTE_PCI_DEVICE(vend, dev) \
150  RTE_CLASS_ANY_ID, \
151  (vend), \
152  (dev), \
153  PCI_ANY_ID, \
154  PCI_ANY_ID
155 #else
156 
157 #define RTE_PCI_DEVICE(vend, dev) \
158  .class_id = RTE_CLASS_ANY_ID, \
159  .vendor_id = (vend), \
160  .device_id = (dev), \
161  .subsystem_vendor_id = PCI_ANY_ID, \
162  .subsystem_device_id = PCI_ANY_ID
163 #endif
164 
168 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
169 
173 typedef int (pci_remove_t)(struct rte_pci_device *);
174 
181  struct rte_pci_bus *bus;
183  pci_remove_t *remove;
184  const struct rte_pci_id *id_table;
185  uint32_t drv_flags;
186 };
187 
191 struct rte_pci_bus {
192  struct rte_bus bus;
193  struct rte_pci_device_list device_list;
194  struct rte_pci_driver_list driver_list;
195 };
196 
198 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
199 
200 #define RTE_PCI_DRV_INTR_LSC 0x0008
201 
202 #define RTE_PCI_DRV_INTR_RMV 0x0010
203 
204 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
205 
209 struct pci_map {
210  void *addr;
211  char *path;
212  uint64_t offset;
213  uint64_t size;
214  uint64_t phaddr;
215 };
216 
223  TAILQ_ENTRY(mapped_pci_resource) next;
224 
225  struct rte_pci_addr pci_addr;
226  char path[PATH_MAX];
227  int nb_maps;
228  struct pci_map maps[PCI_MAX_RESOURCE];
229 };
230 
232 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
233 
235 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
236 do { \
237  unsigned long val; \
238  char *end; \
239  errno = 0; \
240  val = strtoul((in), &end, 16); \
241  if (errno != 0 || end[0] != (dlm) || val > (lim)) \
242  return -EINVAL; \
243  (fd) = (typeof (fd))val; \
244  (in) = end + 1; \
245 } while(0)
246 
260 static inline int
261 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
262 {
263  dev_addr->domain = 0;
264  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
265  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
266  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
267  return 0;
268 }
269 
282 static inline int
283 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
284 {
285  GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
286  GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
287  GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
288  GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
289  return 0;
290 }
291 #undef GET_PCIADDR_FIELD
292 
305 static inline void
307  char *output, size_t size)
308 {
309  RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
310  RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
311  addr->domain, addr->bus,
312  addr->devid, addr->function) >= 0);
313 }
314 
315 /* Compare two PCI device addresses. */
328 static inline int
330  const struct rte_pci_addr *addr2)
331 {
332  uint64_t dev_addr, dev_addr2;
333 
334  if ((addr == NULL) || (addr2 == NULL))
335  return -1;
336 
337  dev_addr = ((uint64_t)addr->domain << 24) |
338  (addr->bus << 16) | (addr->devid << 8) | addr->function;
339  dev_addr2 = ((uint64_t)addr2->domain << 24) |
340  (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
341 
342  if (dev_addr > dev_addr2)
343  return 1;
344  else if (dev_addr < dev_addr2)
345  return -1;
346  else
347  return 0;
348 }
349 
357 int rte_pci_scan(void);
358 
366 int
367 rte_pci_probe(void);
368 
384 int rte_pci_map_device(struct rte_pci_device *dev);
385 
393 void rte_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_pci_probe_one(const struct rte_pci_addr *addr);
441 
455 int rte_pci_detach(const struct rte_pci_addr *addr);
456 
463 void rte_pci_dump(FILE *f);
464 
472 void rte_pci_register(struct rte_pci_driver *driver);
473 
475 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
476 RTE_INIT(pciinitfn_ ##nm); \
477 static void pciinitfn_ ##nm(void) \
478 {\
479  (pci_drv).driver.name = RTE_STR(nm);\
480  rte_pci_register(&pci_drv); \
481 } \
482 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
483 
491 void rte_pci_unregister(struct rte_pci_driver *driver);
492 
506 int rte_pci_read_config(const struct rte_pci_device *device,
507  void *buf, size_t len, off_t offset);
508 
522 int rte_pci_write_config(const struct rte_pci_device *device,
523  const void *buf, size_t len, off_t offset);
524 
531  struct rte_pci_device *dev;
532  uint64_t base;
533  uint64_t len; /* only filled for memory mapped ports */
534 };
535 
551 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
552  struct rte_pci_ioport *p);
553 
562 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
563 
576 void rte_pci_ioport_read(struct rte_pci_ioport *p,
577  void *data, size_t len, off_t offset);
578 
591 void rte_pci_ioport_write(struct rte_pci_ioport *p,
592  const void *data, size_t len, off_t offset);
593 
594 #ifdef __cplusplus
595 }
596 #endif
597 
598 #endif /* _RTE_PCI_H_ */