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