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