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