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