New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / linuxapp / igb_uio / compat.h
1 /*
2  * Minimal wrappers to allow compiling igb_uio on older kernels.
3  */
4
5 #ifndef RHEL_RELEASE_VERSION
6 #define RHEL_RELEASE_VERSION(a, b) (((a) << 8) + (b))
7 #endif
8
9 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
10 #define pci_cfg_access_lock   pci_block_user_cfg_access
11 #define pci_cfg_access_unlock pci_unblock_user_cfg_access
12 #endif
13
14 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
15 #define HAVE_PTE_MASK_PAGE_IOMAP
16 #endif
17
18 #ifndef PCI_MSIX_ENTRY_SIZE
19 #define PCI_MSIX_ENTRY_SIZE            16
20 #define PCI_MSIX_ENTRY_VECTOR_CTRL     12
21 #define PCI_MSIX_ENTRY_CTRL_MASKBIT    1
22 #endif
23
24 /*
25  * for kernels < 2.6.38 and backported patch that moves MSI-X entry definition
26  * to pci_regs.h Those kernels has PCI_MSIX_ENTRY_SIZE defined but not
27  * PCI_MSIX_ENTRY_CTRL_MASKBIT
28  */
29 #ifndef PCI_MSIX_ENTRY_CTRL_MASKBIT
30 #define PCI_MSIX_ENTRY_CTRL_MASKBIT    1
31 #endif
32
33 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34) && \
34         (!(defined(RHEL_RELEASE_CODE) && \
35          RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5, 9)))
36
37 static int pci_num_vf(struct pci_dev *dev)
38 {
39         struct iov {
40                 int pos;
41                 int nres;
42                 u32 cap;
43                 u16 ctrl;
44                 u16 total;
45                 u16 initial;
46                 u16 nr_virtfn;
47         } *iov = (struct iov *)dev->sriov;
48
49         if (!dev->is_physfn)
50                 return 0;
51
52         return iov->nr_virtfn;
53 }
54
55 #endif /* < 2.6.34 */
56
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39) && \
58         (!(defined(RHEL_RELEASE_CODE) && \
59            RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6, 4)))
60
61 #define kstrtoul strict_strtoul
62
63 #endif /* < 2.6.39 */
64
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) && \
66         (!(defined(RHEL_RELEASE_CODE) && \
67            RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6, 3)))
68
69 /* Check if INTX works to control irq's.
70  * Set's INTX_DISABLE flag and reads it back
71  */
72 static bool pci_intx_mask_supported(struct pci_dev *pdev)
73 {
74         bool mask_supported = false;
75         uint16_t orig, new;
76
77         pci_block_user_cfg_access(pdev);
78         pci_read_config_word(pdev, PCI_COMMAND, &orig);
79         pci_write_config_word(pdev, PCI_COMMAND,
80                               orig ^ PCI_COMMAND_INTX_DISABLE);
81         pci_read_config_word(pdev, PCI_COMMAND, &new);
82
83         if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
84                 dev_err(&pdev->dev, "Command register changed from "
85                         "0x%x to 0x%x: driver or hardware bug?\n", orig, new);
86         } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) {
87                 mask_supported = true;
88                 pci_write_config_word(pdev, PCI_COMMAND, orig);
89         }
90         pci_unblock_user_cfg_access(pdev);
91
92         return mask_supported;
93 }
94
95 static bool pci_check_and_mask_intx(struct pci_dev *pdev)
96 {
97         bool pending;
98         uint32_t status;
99
100         pci_block_user_cfg_access(pdev);
101         pci_read_config_dword(pdev, PCI_COMMAND, &status);
102
103         /* interrupt is not ours, goes to out */
104         pending = (((status >> 16) & PCI_STATUS_INTERRUPT) != 0);
105         if (pending) {
106                 uint16_t old, new;
107
108                 old = status;
109                 if (status != 0)
110                         new = old & (~PCI_COMMAND_INTX_DISABLE);
111                 else
112                         new = old | PCI_COMMAND_INTX_DISABLE;
113
114                 if (old != new)
115                         pci_write_config_word(pdev, PCI_COMMAND, new);
116         }
117         pci_unblock_user_cfg_access(pdev);
118
119         return pending;
120 }
121
122 #endif /* < 3.3.0 */
123
124 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
125 #define HAVE_ALLOC_IRQ_VECTORS 1
126 #endif
127
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
129 #define HAVE_MSI_LIST_IN_GENERIC_DEVICE 1
130 #endif
131
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
133 #define HAVE_PCI_MSI_MASK_IRQ 1
134 #endif