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