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