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