Commit | Line | Data |
---|---|---|
72ddd9f3 ZY |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Driver for FPGA Device Feature List (DFL) PCIe device | |
4 | * | |
5 | * Copyright (C) 2017-2018 Intel Corporation, Inc. | |
6 | * | |
7 | * Authors: | |
8 | * Zhang Yi <Yi.Z.Zhang@intel.com> | |
9 | * Xiao Guangrong <guangrong.xiao@linux.intel.com> | |
10 | * Joseph Grecco <joe.grecco@intel.com> | |
11 | * Enno Luebbers <enno.luebbers@intel.com> | |
12 | * Tim Whisonant <tim.whisonant@intel.com> | |
13 | * Ananda Ravuri <ananda.ravuri@intel.com> | |
14 | * Henry Mitchel <henry.mitchel@intel.com> | |
15 | */ | |
16 | ||
17 | #include <linux/pci.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/stddef.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/aer.h> | |
24 | ||
968b8199 WH |
25 | #include "dfl.h" |
26 | ||
72ddd9f3 ZY |
27 | #define DRV_VERSION "0.8" |
28 | #define DRV_NAME "dfl-pci" | |
29 | ||
968b8199 WH |
30 | struct cci_drvdata { |
31 | struct dfl_fpga_cdev *cdev; /* container device */ | |
32 | }; | |
33 | ||
34 | static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pcidev, int bar) | |
35 | { | |
36 | if (pcim_iomap_regions(pcidev, BIT(bar), DRV_NAME)) | |
37 | return NULL; | |
38 | ||
39 | return pcim_iomap_table(pcidev)[bar]; | |
40 | } | |
41 | ||
bfef946d XY |
42 | static int cci_pci_alloc_irq(struct pci_dev *pcidev) |
43 | { | |
44 | int ret, nvec = pci_msix_vec_count(pcidev); | |
45 | ||
46 | if (nvec <= 0) { | |
47 | dev_dbg(&pcidev->dev, "fpga interrupt not supported\n"); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | ret = pci_alloc_irq_vectors(pcidev, nvec, nvec, PCI_IRQ_MSIX); | |
52 | if (ret < 0) | |
53 | return ret; | |
54 | ||
55 | return nvec; | |
56 | } | |
57 | ||
58 | static void cci_pci_free_irq(struct pci_dev *pcidev) | |
59 | { | |
60 | pci_free_irq_vectors(pcidev); | |
61 | } | |
62 | ||
72ddd9f3 ZY |
63 | /* PCI Device ID */ |
64 | #define PCIE_DEVICE_ID_PF_INT_5_X 0xBCBD | |
65 | #define PCIE_DEVICE_ID_PF_INT_6_X 0xBCC0 | |
66 | #define PCIE_DEVICE_ID_PF_DSC_1_X 0x09C4 | |
67 | /* VF Device */ | |
68 | #define PCIE_DEVICE_ID_VF_INT_5_X 0xBCBF | |
69 | #define PCIE_DEVICE_ID_VF_INT_6_X 0xBCC1 | |
70 | #define PCIE_DEVICE_ID_VF_DSC_1_X 0x09C5 | |
71 | ||
72 | static struct pci_device_id cci_pcie_id_tbl[] = { | |
73 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_5_X),}, | |
74 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_5_X),}, | |
75 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_INT_6_X),}, | |
76 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_INT_6_X),}, | |
77 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_PF_DSC_1_X),}, | |
78 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIE_DEVICE_ID_VF_DSC_1_X),}, | |
79 | {0,} | |
80 | }; | |
81 | MODULE_DEVICE_TABLE(pci, cci_pcie_id_tbl); | |
82 | ||
968b8199 WH |
83 | static int cci_init_drvdata(struct pci_dev *pcidev) |
84 | { | |
85 | struct cci_drvdata *drvdata; | |
86 | ||
87 | drvdata = devm_kzalloc(&pcidev->dev, sizeof(*drvdata), GFP_KERNEL); | |
88 | if (!drvdata) | |
89 | return -ENOMEM; | |
90 | ||
91 | pci_set_drvdata(pcidev, drvdata); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static void cci_remove_feature_devs(struct pci_dev *pcidev) | |
97 | { | |
98 | struct cci_drvdata *drvdata = pci_get_drvdata(pcidev); | |
99 | ||
100 | /* remove all children feature devices */ | |
101 | dfl_fpga_feature_devs_remove(drvdata->cdev); | |
bfef946d XY |
102 | cci_pci_free_irq(pcidev); |
103 | } | |
104 | ||
105 | static int *cci_pci_create_irq_table(struct pci_dev *pcidev, unsigned int nvec) | |
106 | { | |
107 | unsigned int i; | |
108 | int *table; | |
109 | ||
110 | table = kcalloc(nvec, sizeof(int), GFP_KERNEL); | |
111 | if (!table) | |
112 | return table; | |
113 | ||
114 | for (i = 0; i < nvec; i++) | |
115 | table[i] = pci_irq_vector(pcidev, i); | |
116 | ||
117 | return table; | |
968b8199 WH |
118 | } |
119 | ||
120 | /* enumerate feature devices under pci device */ | |
121 | static int cci_enumerate_feature_devs(struct pci_dev *pcidev) | |
122 | { | |
123 | struct cci_drvdata *drvdata = pci_get_drvdata(pcidev); | |
bfef946d | 124 | int port_num, bar, i, nvec, ret = 0; |
968b8199 WH |
125 | struct dfl_fpga_enum_info *info; |
126 | struct dfl_fpga_cdev *cdev; | |
127 | resource_size_t start, len; | |
968b8199 | 128 | void __iomem *base; |
bfef946d | 129 | int *irq_table; |
968b8199 WH |
130 | u32 offset; |
131 | u64 v; | |
132 | ||
133 | /* allocate enumeration info via pci_dev */ | |
134 | info = dfl_fpga_enum_info_alloc(&pcidev->dev); | |
135 | if (!info) | |
136 | return -ENOMEM; | |
137 | ||
bfef946d XY |
138 | /* add irq info for enumeration if the device support irq */ |
139 | nvec = cci_pci_alloc_irq(pcidev); | |
140 | if (nvec < 0) { | |
141 | dev_err(&pcidev->dev, "Fail to alloc irq %d.\n", nvec); | |
142 | ret = nvec; | |
143 | goto enum_info_free_exit; | |
144 | } else if (nvec) { | |
145 | irq_table = cci_pci_create_irq_table(pcidev, nvec); | |
146 | if (!irq_table) { | |
147 | ret = -ENOMEM; | |
148 | goto irq_free_exit; | |
149 | } | |
150 | ||
151 | ret = dfl_fpga_enum_info_add_irq(info, nvec, irq_table); | |
152 | kfree(irq_table); | |
153 | if (ret) | |
154 | goto irq_free_exit; | |
155 | } | |
156 | ||
968b8199 WH |
157 | /* start to find Device Feature List from Bar 0 */ |
158 | base = cci_pci_ioremap_bar(pcidev, 0); | |
159 | if (!base) { | |
160 | ret = -ENOMEM; | |
bfef946d | 161 | goto irq_free_exit; |
968b8199 WH |
162 | } |
163 | ||
164 | /* | |
165 | * PF device has FME and Ports/AFUs, and VF device only has one | |
166 | * Port/AFU. Check them and add related "Device Feature List" info | |
167 | * for the next step enumeration. | |
168 | */ | |
169 | if (dfl_feature_is_fme(base)) { | |
170 | start = pci_resource_start(pcidev, 0); | |
171 | len = pci_resource_len(pcidev, 0); | |
172 | ||
173 | dfl_fpga_enum_info_add_dfl(info, start, len, base); | |
174 | ||
175 | /* | |
176 | * find more Device Feature Lists (e.g. Ports) per information | |
177 | * indicated by FME module. | |
178 | */ | |
179 | v = readq(base + FME_HDR_CAP); | |
180 | port_num = FIELD_GET(FME_CAP_NUM_PORTS, v); | |
181 | ||
182 | WARN_ON(port_num > MAX_DFL_FPGA_PORT_NUM); | |
183 | ||
184 | for (i = 0; i < port_num; i++) { | |
185 | v = readq(base + FME_HDR_PORT_OFST(i)); | |
186 | ||
187 | /* skip ports which are not implemented. */ | |
188 | if (!(v & FME_PORT_OFST_IMP)) | |
189 | continue; | |
190 | ||
191 | /* | |
192 | * add Port's Device Feature List information for next | |
193 | * step enumeration. | |
194 | */ | |
195 | bar = FIELD_GET(FME_PORT_OFST_BAR_ID, v); | |
196 | offset = FIELD_GET(FME_PORT_OFST_DFH_OFST, v); | |
197 | base = cci_pci_ioremap_bar(pcidev, bar); | |
198 | if (!base) | |
199 | continue; | |
200 | ||
201 | start = pci_resource_start(pcidev, bar) + offset; | |
202 | len = pci_resource_len(pcidev, bar) - offset; | |
203 | ||
204 | dfl_fpga_enum_info_add_dfl(info, start, len, | |
205 | base + offset); | |
206 | } | |
207 | } else if (dfl_feature_is_port(base)) { | |
208 | start = pci_resource_start(pcidev, 0); | |
209 | len = pci_resource_len(pcidev, 0); | |
210 | ||
211 | dfl_fpga_enum_info_add_dfl(info, start, len, base); | |
212 | } else { | |
213 | ret = -ENODEV; | |
bfef946d | 214 | goto irq_free_exit; |
968b8199 WH |
215 | } |
216 | ||
217 | /* start enumeration with prepared enumeration information */ | |
218 | cdev = dfl_fpga_feature_devs_enumerate(info); | |
219 | if (IS_ERR(cdev)) { | |
220 | dev_err(&pcidev->dev, "Enumeration failure\n"); | |
221 | ret = PTR_ERR(cdev); | |
bfef946d | 222 | goto irq_free_exit; |
968b8199 WH |
223 | } |
224 | ||
225 | drvdata->cdev = cdev; | |
226 | ||
bfef946d XY |
227 | irq_free_exit: |
228 | if (ret) | |
229 | cci_pci_free_irq(pcidev); | |
968b8199 WH |
230 | enum_info_free_exit: |
231 | dfl_fpga_enum_info_free(info); | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
72ddd9f3 ZY |
236 | static |
237 | int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid) | |
238 | { | |
239 | int ret; | |
240 | ||
241 | ret = pcim_enable_device(pcidev); | |
242 | if (ret < 0) { | |
243 | dev_err(&pcidev->dev, "Failed to enable device %d.\n", ret); | |
244 | return ret; | |
245 | } | |
246 | ||
247 | ret = pci_enable_pcie_error_reporting(pcidev); | |
248 | if (ret && ret != -EINVAL) | |
249 | dev_info(&pcidev->dev, "PCIE AER unavailable %d.\n", ret); | |
250 | ||
251 | pci_set_master(pcidev); | |
252 | ||
253 | if (!pci_set_dma_mask(pcidev, DMA_BIT_MASK(64))) { | |
254 | ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64)); | |
255 | if (ret) | |
256 | goto disable_error_report_exit; | |
257 | } else if (!pci_set_dma_mask(pcidev, DMA_BIT_MASK(32))) { | |
258 | ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32)); | |
259 | if (ret) | |
260 | goto disable_error_report_exit; | |
261 | } else { | |
262 | ret = -EIO; | |
263 | dev_err(&pcidev->dev, "No suitable DMA support available.\n"); | |
264 | goto disable_error_report_exit; | |
265 | } | |
266 | ||
968b8199 WH |
267 | ret = cci_init_drvdata(pcidev); |
268 | if (ret) { | |
269 | dev_err(&pcidev->dev, "Fail to init drvdata %d.\n", ret); | |
270 | goto disable_error_report_exit; | |
271 | } | |
272 | ||
273 | ret = cci_enumerate_feature_devs(pcidev); | |
bfef946d XY |
274 | if (!ret) |
275 | return ret; | |
968b8199 | 276 | |
bfef946d | 277 | dev_err(&pcidev->dev, "enumeration failure %d.\n", ret); |
72ddd9f3 ZY |
278 | |
279 | disable_error_report_exit: | |
280 | pci_disable_pcie_error_reporting(pcidev); | |
281 | return ret; | |
282 | } | |
283 | ||
bdd4f307 WH |
284 | static int cci_pci_sriov_configure(struct pci_dev *pcidev, int num_vfs) |
285 | { | |
286 | struct cci_drvdata *drvdata = pci_get_drvdata(pcidev); | |
287 | struct dfl_fpga_cdev *cdev = drvdata->cdev; | |
288 | int ret = 0; | |
289 | ||
290 | if (!num_vfs) { | |
291 | /* | |
292 | * disable SRIOV and then put released ports back to default | |
293 | * PF access mode. | |
294 | */ | |
295 | pci_disable_sriov(pcidev); | |
296 | ||
297 | dfl_fpga_cdev_config_ports_pf(cdev); | |
298 | ||
299 | } else { | |
300 | /* | |
301 | * before enable SRIOV, put released ports into VF access mode | |
302 | * first of all. | |
303 | */ | |
304 | ret = dfl_fpga_cdev_config_ports_vf(cdev, num_vfs); | |
305 | if (ret) | |
306 | return ret; | |
307 | ||
308 | ret = pci_enable_sriov(pcidev, num_vfs); | |
3c2760b7 | 309 | if (ret) { |
bdd4f307 | 310 | dfl_fpga_cdev_config_ports_pf(cdev); |
3c2760b7 XY |
311 | return ret; |
312 | } | |
bdd4f307 WH |
313 | } |
314 | ||
3c2760b7 | 315 | return num_vfs; |
bdd4f307 WH |
316 | } |
317 | ||
72ddd9f3 ZY |
318 | static void cci_pci_remove(struct pci_dev *pcidev) |
319 | { | |
bdd4f307 WH |
320 | if (dev_is_pf(&pcidev->dev)) |
321 | cci_pci_sriov_configure(pcidev, 0); | |
322 | ||
968b8199 | 323 | cci_remove_feature_devs(pcidev); |
72ddd9f3 ZY |
324 | pci_disable_pcie_error_reporting(pcidev); |
325 | } | |
326 | ||
327 | static struct pci_driver cci_pci_driver = { | |
328 | .name = DRV_NAME, | |
329 | .id_table = cci_pcie_id_tbl, | |
330 | .probe = cci_pci_probe, | |
331 | .remove = cci_pci_remove, | |
bdd4f307 | 332 | .sriov_configure = cci_pci_sriov_configure, |
72ddd9f3 ZY |
333 | }; |
334 | ||
335 | module_pci_driver(cci_pci_driver); | |
336 | ||
337 | MODULE_DESCRIPTION("FPGA DFL PCIe Device Driver"); | |
338 | MODULE_AUTHOR("Intel Corporation"); | |
339 | MODULE_LICENSE("GPL v2"); |