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