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