e416714b32ff3b9995834815adf0fa011a7cae91
[dpdk.git] / lib / librte_eal / common / include / rte_pci.h
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
38 /**
39  * @file
40  *
41  * RTE PCI Interface
42  */
43
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
61 /** Pathname of PCI devices directory. */
62 const char *pci_get_sysfs_path(void);
63
64 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
65 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
66 #define PCI_PRI_STR_SIZE sizeof("XXXXXXXX:XX:XX.X")
67
68 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
69 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
70
71 /** Nb. of values in PCI device identifier format string. */
72 #define PCI_FMT_NVAL 4
73
74 /** Nb. of values in PCI resource format. */
75 #define PCI_RESOURCE_FMT_NVAL 3
76
77 /** Maximum number of PCI resources. */
78 #define PCI_MAX_RESOURCE 6
79
80 /** Name of PCI Bus */
81 #define PCI_BUS_NAME "PCI"
82
83 /* Forward declarations */
84 struct rte_pci_device;
85 struct rte_pci_driver;
86
87 /** List of PCI devices */
88 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
89 /** List of PCI drivers */
90 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
91
92 /* PCI Bus iterators */
93 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
94                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
95
96 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
97                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
98
99 /**
100  * A structure describing an ID for a PCI driver. Each driver provides a
101  * table of these IDs for each device that it supports.
102  */
103 struct rte_pci_id {
104         uint32_t class_id;            /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
105         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
106         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
107         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
108         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
109 };
110
111 /**
112  * A structure describing the location of a PCI device.
113  */
114 struct rte_pci_addr {
115         uint32_t domain;                /**< Device domain */
116         uint8_t bus;                    /**< Device bus */
117         uint8_t devid;                  /**< Device ID */
118         uint8_t function;               /**< Device function. */
119 };
120
121 struct rte_devargs;
122
123 enum rte_kernel_driver {
124         RTE_KDRV_UNKNOWN = 0,
125         RTE_KDRV_IGB_UIO,
126         RTE_KDRV_VFIO,
127         RTE_KDRV_UIO_GENERIC,
128         RTE_KDRV_NIC_UIO,
129         RTE_KDRV_NONE,
130 };
131
132 /**
133  * A structure describing a PCI device.
134  */
135 struct rte_pci_device {
136         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
137         struct rte_device device;               /**< Inherit core device */
138         struct rte_pci_addr addr;               /**< PCI location. */
139         struct rte_pci_id id;                   /**< PCI ID. */
140         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
141                                                 /**< PCI Memory Resource */
142         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
143         struct rte_pci_driver *driver;          /**< Associated driver */
144         uint16_t max_vfs;                       /**< sriov enable if not zero */
145         enum rte_kernel_driver kdrv;            /**< Kernel driver passthrough */
146         char name[PCI_PRI_STR_SIZE+1];          /**< PCI location (ASCII) */
147 };
148
149 /**
150  * @internal
151  * Helper macro for drivers that need to convert to struct rte_pci_device.
152  */
153 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
154
155 /** Any PCI device identifier (vendor, device, ...) */
156 #define PCI_ANY_ID (0xffff)
157 #define RTE_CLASS_ANY_ID (0xffffff)
158
159 #ifdef __cplusplus
160 /** C++ macro used to help building up tables of device IDs */
161 #define RTE_PCI_DEVICE(vend, dev) \
162         RTE_CLASS_ANY_ID,         \
163         (vend),                   \
164         (dev),                    \
165         PCI_ANY_ID,               \
166         PCI_ANY_ID
167 #else
168 /** Macro used to help building up tables of device IDs */
169 #define RTE_PCI_DEVICE(vend, dev)          \
170         .class_id = RTE_CLASS_ANY_ID,      \
171         .vendor_id = (vend),               \
172         .device_id = (dev),                \
173         .subsystem_vendor_id = PCI_ANY_ID, \
174         .subsystem_device_id = PCI_ANY_ID
175 #endif
176
177 /**
178  * Initialisation function for the driver called during PCI probing.
179  */
180 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
181
182 /**
183  * Uninitialisation function for the driver called during hotplugging.
184  */
185 typedef int (pci_remove_t)(struct rte_pci_device *);
186
187 /**
188  * A structure describing a PCI driver.
189  */
190 struct rte_pci_driver {
191         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
192         struct rte_driver driver;               /**< Inherit core driver. */
193         struct rte_pci_bus *bus;                /**< PCI bus reference. */
194         pci_probe_t *probe;                     /**< Device Probe function. */
195         pci_remove_t *remove;                   /**< Device Remove function. */
196         const struct rte_pci_id *id_table;      /**< ID table, NULL terminated. */
197         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
198 };
199
200 /**
201  * Structure describing the PCI bus
202  */
203 struct rte_pci_bus {
204         struct rte_bus bus;               /**< Inherit the generic class */
205         struct rte_pci_device_list device_list;  /**< List of PCI devices */
206         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
207 };
208
209 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
210 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
211 /** Device driver supports link state interrupt */
212 #define RTE_PCI_DRV_INTR_LSC    0x0008
213 /** Device driver supports device removal interrupt */
214 #define RTE_PCI_DRV_INTR_RMV 0x0010
215 /** Device driver needs to keep mapped resources if unsupported dev detected */
216 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
217
218 /**
219  * A structure describing a PCI mapping.
220  */
221 struct pci_map {
222         void *addr;
223         char *path;
224         uint64_t offset;
225         uint64_t size;
226         uint64_t phaddr;
227 };
228
229 /**
230  * A structure describing a mapped PCI resource.
231  * For multi-process we need to reproduce all PCI mappings in secondary
232  * processes, so save them in a tailq.
233  */
234 struct mapped_pci_resource {
235         TAILQ_ENTRY(mapped_pci_resource) next;
236
237         struct rte_pci_addr pci_addr;
238         char path[PATH_MAX];
239         int nb_maps;
240         struct pci_map maps[PCI_MAX_RESOURCE];
241 };
242
243 /** mapped pci device list */
244 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
245
246 /**< Internal use only - Macro used by pci addr parsing functions **/
247 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
248 do {                                                               \
249         unsigned long val;                                      \
250         char *end;                                              \
251         errno = 0;                                              \
252         val = strtoul((in), &end, 16);                          \
253         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
254                 return -EINVAL;                                 \
255         (fd) = (typeof (fd))val;                                \
256         (in) = end + 1;                                         \
257 } while(0)
258
259 /**
260  * Utility function to produce a PCI Bus-Device-Function value
261  * given a string representation. Assumes that the BDF is provided without
262  * a domain prefix (i.e. domain returned is always 0)
263  *
264  * @param input
265  *      The input string to be parsed. Should have the format XX:XX.X
266  * @param dev_addr
267  *      The PCI Bus-Device-Function address to be returned. Domain will always be
268  *      returned as 0
269  * @return
270  *  0 on success, negative on error.
271  */
272 static inline int
273 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
274 {
275         dev_addr->domain = 0;
276         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
277         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
278         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
279         return 0;
280 }
281
282 /**
283  * Utility function to produce a PCI Bus-Device-Function value
284  * given a string representation. Assumes that the BDF is provided including
285  * a domain prefix.
286  *
287  * @param input
288  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
289  * @param dev_addr
290  *      The PCI Bus-Device-Function address to be returned
291  * @return
292  *  0 on success, negative on error.
293  */
294 static inline int
295 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
296 {
297         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
298         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
299         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
300         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
301         return 0;
302 }
303 #undef GET_PCIADDR_FIELD
304
305 /**
306  * Utility function to write a pci device name, this device name can later be
307  * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
308  * BDF helpers.
309  *
310  * @param addr
311  *      The PCI Bus-Device-Function address
312  * @param output
313  *      The output buffer string
314  * @param size
315  *      The output buffer size
316  */
317 static inline void
318 rte_pci_device_name(const struct rte_pci_addr *addr,
319                 char *output, size_t size)
320 {
321         RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
322         RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
323                             addr->domain, addr->bus,
324                             addr->devid, addr->function) >= 0);
325 }
326
327 /* Compare two PCI device addresses. */
328 /**
329  * Utility function to compare two PCI device addresses.
330  *
331  * @param addr
332  *      The PCI Bus-Device-Function address to compare
333  * @param addr2
334  *      The PCI Bus-Device-Function address to compare
335  * @return
336  *      0 on equal PCI address.
337  *      Positive on addr is greater than addr2.
338  *      Negative on addr is less than addr2, or error.
339  */
340 static inline int
341 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
342                          const struct rte_pci_addr *addr2)
343 {
344         uint64_t dev_addr, dev_addr2;
345
346         if ((addr == NULL) || (addr2 == NULL))
347                 return -1;
348
349         dev_addr = ((uint64_t)addr->domain << 24) |
350                 (addr->bus << 16) | (addr->devid << 8) | addr->function;
351         dev_addr2 = ((uint64_t)addr2->domain << 24) |
352                 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
353
354         if (dev_addr > dev_addr2)
355                 return 1;
356         else if (dev_addr < dev_addr2)
357                 return -1;
358         else
359                 return 0;
360 }
361
362 /**
363  * Scan the content of the PCI bus, and the devices in the devices
364  * list
365  *
366  * @return
367  *  0 on success, negative on error
368  */
369 int rte_pci_scan(void);
370
371 /**
372  * Probe the PCI bus
373  *
374  * @return
375  *   - 0 on success.
376  *   - !0 on error.
377  */
378 int
379 rte_pci_probe(void);
380
381 /**
382  * Map the PCI device resources in user space virtual memory address
383  *
384  * Note that driver should not call this function when flag
385  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
386  * you when it's on.
387  *
388  * @param dev
389  *   A pointer to a rte_pci_device structure describing the device
390  *   to use
391  *
392  * @return
393  *   0 on success, negative on error and positive if no driver
394  *   is found for the device.
395  */
396 int rte_pci_map_device(struct rte_pci_device *dev);
397
398 /**
399  * Unmap this device
400  *
401  * @param dev
402  *   A pointer to a rte_pci_device structure describing the device
403  *   to use
404  */
405 void rte_pci_unmap_device(struct rte_pci_device *dev);
406
407 /**
408  * @internal
409  * Map a particular resource from a file.
410  *
411  * @param requested_addr
412  *      The starting address for the new mapping range.
413  * @param fd
414  *      The file descriptor.
415  * @param offset
416  *      The offset for the mapping range.
417  * @param size
418  *      The size for the mapping range.
419  * @param additional_flags
420  *      The additional flags for the mapping range.
421  * @return
422  *   - On success, the function returns a pointer to the mapped area.
423  *   - On error, the value MAP_FAILED is returned.
424  */
425 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
426                 size_t size, int additional_flags);
427
428 /**
429  * @internal
430  * Unmap a particular resource.
431  *
432  * @param requested_addr
433  *      The address for the unmapping range.
434  * @param size
435  *      The size for the unmapping range.
436  */
437 void pci_unmap_resource(void *requested_addr, size_t size);
438
439 /**
440  * Probe the single PCI device.
441  *
442  * Scan the content of the PCI bus, and find the pci device specified by pci
443  * address, then call the probe() function for registered driver that has a
444  * matching entry in its id_table for discovered device.
445  *
446  * @param addr
447  *      The PCI Bus-Device-Function address to probe.
448  * @return
449  *   - 0 on success.
450  *   - Negative on error.
451  */
452 int rte_pci_probe_one(const struct rte_pci_addr *addr);
453
454 /**
455  * Close the single PCI device.
456  *
457  * Scan the content of the PCI bus, and find the pci device specified by pci
458  * address, then call the remove() function for registered driver that has a
459  * matching entry in its id_table for discovered device.
460  *
461  * @param addr
462  *      The PCI Bus-Device-Function address to close.
463  * @return
464  *   - 0 on success.
465  *   - Negative on error.
466  */
467 int rte_pci_detach(const struct rte_pci_addr *addr);
468
469 /**
470  * Dump the content of the PCI bus.
471  *
472  * @param f
473  *   A pointer to a file for output
474  */
475 void rte_pci_dump(FILE *f);
476
477 /**
478  * Register a PCI driver.
479  *
480  * @param driver
481  *   A pointer to a rte_pci_driver structure describing the driver
482  *   to be registered.
483  */
484 void rte_pci_register(struct rte_pci_driver *driver);
485
486 /** Helper for PCI device registration from driver (eth, crypto) instance */
487 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
488 RTE_INIT(pciinitfn_ ##nm); \
489 static void pciinitfn_ ##nm(void) \
490 {\
491         (pci_drv).driver.name = RTE_STR(nm);\
492         rte_pci_register(&pci_drv); \
493 } \
494 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
495
496 /**
497  * Unregister a PCI driver.
498  *
499  * @param driver
500  *   A pointer to a rte_pci_driver structure describing the driver
501  *   to be unregistered.
502  */
503 void rte_pci_unregister(struct rte_pci_driver *driver);
504
505 /**
506  * Read PCI config space.
507  *
508  * @param device
509  *   A pointer to a rte_pci_device structure describing the device
510  *   to use
511  * @param buf
512  *   A data buffer where the bytes should be read into
513  * @param len
514  *   The length of the data buffer.
515  * @param offset
516  *   The offset into PCI config space
517  */
518 int rte_pci_read_config(const struct rte_pci_device *device,
519                 void *buf, size_t len, off_t offset);
520
521 /**
522  * Write PCI config space.
523  *
524  * @param device
525  *   A pointer to a rte_pci_device structure describing the device
526  *   to use
527  * @param buf
528  *   A data buffer containing the bytes should be written
529  * @param len
530  *   The length of the data buffer.
531  * @param offset
532  *   The offset into PCI config space
533  */
534 int rte_pci_write_config(const struct rte_pci_device *device,
535                 const void *buf, size_t len, off_t offset);
536
537 /**
538  * A structure used to access io resources for a pci device.
539  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
540  * of pci ioport api.
541  */
542 struct rte_pci_ioport {
543         struct rte_pci_device *dev;
544         uint64_t base;
545         uint64_t len; /* only filled for memory mapped ports */
546 };
547
548 /**
549  * Initialize a rte_pci_ioport object for a pci device io resource.
550  *
551  * This object is then used to gain access to those io resources (see below).
552  *
553  * @param dev
554  *   A pointer to a rte_pci_device structure describing the device
555  *   to use.
556  * @param bar
557  *   Index of the io pci resource we want to access.
558  * @param p
559  *   The rte_pci_ioport object to be initialized.
560  * @return
561  *  0 on success, negative on error.
562  */
563 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
564                 struct rte_pci_ioport *p);
565
566 /**
567  * Release any resources used in a rte_pci_ioport object.
568  *
569  * @param p
570  *   The rte_pci_ioport object to be uninitialized.
571  * @return
572  *  0 on success, negative on error.
573  */
574 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
575
576 /**
577  * Read from a io pci resource.
578  *
579  * @param p
580  *   The rte_pci_ioport object from which we want to read.
581  * @param data
582  *   A data buffer where the bytes should be read into
583  * @param len
584  *   The length of the data buffer.
585  * @param offset
586  *   The offset into the pci io resource.
587  */
588 void rte_pci_ioport_read(struct rte_pci_ioport *p,
589                 void *data, size_t len, off_t offset);
590
591 /**
592  * Write to a io pci resource.
593  *
594  * @param p
595  *   The rte_pci_ioport object to which we want to write.
596  * @param data
597  *   A data buffer where the bytes should be read into
598  * @param len
599  *   The length of the data buffer.
600  * @param offset
601  *   The offset into the pci io resource.
602  */
603 void rte_pci_ioport_write(struct rte_pci_ioport *p,
604                 const void *data, size_t len, off_t offset);
605
606 #ifdef __cplusplus
607 }
608 #endif
609
610 #endif /* _RTE_PCI_H_ */