1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for FPGA Device Feature List (DFL) PCIe device
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
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>
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>
27 #define DRV_VERSION "0.8"
28 #define DRV_NAME "dfl-pci"
31 struct dfl_fpga_cdev
*cdev
; /* container device */
34 static void __iomem
*cci_pci_ioremap_bar0(struct pci_dev
*pcidev
)
36 if (pcim_iomap_regions(pcidev
, BIT(0), DRV_NAME
))
39 return pcim_iomap_table(pcidev
)[0];
42 static int cci_pci_alloc_irq(struct pci_dev
*pcidev
)
44 int ret
, nvec
= pci_msix_vec_count(pcidev
);
47 dev_dbg(&pcidev
->dev
, "fpga interrupt not supported\n");
51 ret
= pci_alloc_irq_vectors(pcidev
, nvec
, nvec
, PCI_IRQ_MSIX
);
58 static void cci_pci_free_irq(struct pci_dev
*pcidev
)
60 pci_free_irq_vectors(pcidev
);
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 #define PCIE_DEVICE_ID_INTEL_PAC_N3000 0x0B30
69 #define PCIE_DEVICE_ID_VF_INT_5_X 0xBCBF
70 #define PCIE_DEVICE_ID_VF_INT_6_X 0xBCC1
71 #define PCIE_DEVICE_ID_VF_DSC_1_X 0x09C5
73 static struct pci_device_id cci_pcie_id_tbl
[] = {
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_PF_INT_5_X
),},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_VF_INT_5_X
),},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_PF_INT_6_X
),},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_VF_INT_6_X
),},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_PF_DSC_1_X
),},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_VF_DSC_1_X
),},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCIE_DEVICE_ID_INTEL_PAC_N3000
),},
83 MODULE_DEVICE_TABLE(pci
, cci_pcie_id_tbl
);
85 static int cci_init_drvdata(struct pci_dev
*pcidev
)
87 struct cci_drvdata
*drvdata
;
89 drvdata
= devm_kzalloc(&pcidev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
93 pci_set_drvdata(pcidev
, drvdata
);
98 static void cci_remove_feature_devs(struct pci_dev
*pcidev
)
100 struct cci_drvdata
*drvdata
= pci_get_drvdata(pcidev
);
102 /* remove all children feature devices */
103 dfl_fpga_feature_devs_remove(drvdata
->cdev
);
104 cci_pci_free_irq(pcidev
);
107 static int *cci_pci_create_irq_table(struct pci_dev
*pcidev
, unsigned int nvec
)
112 table
= kcalloc(nvec
, sizeof(int), GFP_KERNEL
);
116 for (i
= 0; i
< nvec
; i
++)
117 table
[i
] = pci_irq_vector(pcidev
, i
);
122 /* enumerate feature devices under pci device */
123 static int cci_enumerate_feature_devs(struct pci_dev
*pcidev
)
125 struct cci_drvdata
*drvdata
= pci_get_drvdata(pcidev
);
126 int port_num
, bar
, i
, nvec
, ret
= 0;
127 struct dfl_fpga_enum_info
*info
;
128 struct dfl_fpga_cdev
*cdev
;
129 resource_size_t start
, len
;
135 /* allocate enumeration info via pci_dev */
136 info
= dfl_fpga_enum_info_alloc(&pcidev
->dev
);
140 /* add irq info for enumeration if the device support irq */
141 nvec
= cci_pci_alloc_irq(pcidev
);
143 dev_err(&pcidev
->dev
, "Fail to alloc irq %d.\n", nvec
);
145 goto enum_info_free_exit
;
147 irq_table
= cci_pci_create_irq_table(pcidev
, nvec
);
153 ret
= dfl_fpga_enum_info_add_irq(info
, nvec
, irq_table
);
159 /* start to find Device Feature List in Bar 0 */
160 base
= cci_pci_ioremap_bar0(pcidev
);
167 * PF device has FME and Ports/AFUs, and VF device only has one
168 * Port/AFU. Check them and add related "Device Feature List" info
169 * for the next step enumeration.
171 if (dfl_feature_is_fme(base
)) {
172 start
= pci_resource_start(pcidev
, 0);
173 len
= pci_resource_len(pcidev
, 0);
175 dfl_fpga_enum_info_add_dfl(info
, start
, len
);
178 * find more Device Feature Lists (e.g. Ports) per information
179 * indicated by FME module.
181 v
= readq(base
+ FME_HDR_CAP
);
182 port_num
= FIELD_GET(FME_CAP_NUM_PORTS
, v
);
184 WARN_ON(port_num
> MAX_DFL_FPGA_PORT_NUM
);
186 for (i
= 0; i
< port_num
; i
++) {
187 v
= readq(base
+ FME_HDR_PORT_OFST(i
));
189 /* skip ports which are not implemented. */
190 if (!(v
& FME_PORT_OFST_IMP
))
194 * add Port's Device Feature List information for next
197 bar
= FIELD_GET(FME_PORT_OFST_BAR_ID
, v
);
198 offset
= FIELD_GET(FME_PORT_OFST_DFH_OFST
, v
);
199 start
= pci_resource_start(pcidev
, bar
) + offset
;
200 len
= pci_resource_len(pcidev
, bar
) - offset
;
202 dfl_fpga_enum_info_add_dfl(info
, start
, len
);
204 } else if (dfl_feature_is_port(base
)) {
205 start
= pci_resource_start(pcidev
, 0);
206 len
= pci_resource_len(pcidev
, 0);
208 dfl_fpga_enum_info_add_dfl(info
, start
, len
);
214 /* release I/O mappings for next step enumeration */
215 pcim_iounmap_regions(pcidev
, BIT(0));
217 /* start enumeration with prepared enumeration information */
218 cdev
= dfl_fpga_feature_devs_enumerate(info
);
220 dev_err(&pcidev
->dev
, "Enumeration failure\n");
225 drvdata
->cdev
= cdev
;
229 cci_pci_free_irq(pcidev
);
231 dfl_fpga_enum_info_free(info
);
237 int cci_pci_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pcidevid
)
241 ret
= pcim_enable_device(pcidev
);
243 dev_err(&pcidev
->dev
, "Failed to enable device %d.\n", ret
);
247 ret
= pci_enable_pcie_error_reporting(pcidev
);
248 if (ret
&& ret
!= -EINVAL
)
249 dev_info(&pcidev
->dev
, "PCIE AER unavailable %d.\n", ret
);
251 pci_set_master(pcidev
);
253 if (!pci_set_dma_mask(pcidev
, DMA_BIT_MASK(64))) {
254 ret
= pci_set_consistent_dma_mask(pcidev
, DMA_BIT_MASK(64));
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));
260 goto disable_error_report_exit
;
263 dev_err(&pcidev
->dev
, "No suitable DMA support available.\n");
264 goto disable_error_report_exit
;
267 ret
= cci_init_drvdata(pcidev
);
269 dev_err(&pcidev
->dev
, "Fail to init drvdata %d.\n", ret
);
270 goto disable_error_report_exit
;
273 ret
= cci_enumerate_feature_devs(pcidev
);
277 dev_err(&pcidev
->dev
, "enumeration failure %d.\n", ret
);
279 disable_error_report_exit
:
280 pci_disable_pcie_error_reporting(pcidev
);
284 static int cci_pci_sriov_configure(struct pci_dev
*pcidev
, int num_vfs
)
286 struct cci_drvdata
*drvdata
= pci_get_drvdata(pcidev
);
287 struct dfl_fpga_cdev
*cdev
= drvdata
->cdev
;
291 * disable SRIOV and then put released ports back to default
294 pci_disable_sriov(pcidev
);
296 dfl_fpga_cdev_config_ports_pf(cdev
);
302 * before enable SRIOV, put released ports into VF access mode
305 ret
= dfl_fpga_cdev_config_ports_vf(cdev
, num_vfs
);
309 ret
= pci_enable_sriov(pcidev
, num_vfs
);
311 dfl_fpga_cdev_config_ports_pf(cdev
);
319 static void cci_pci_remove(struct pci_dev
*pcidev
)
321 if (dev_is_pf(&pcidev
->dev
))
322 cci_pci_sriov_configure(pcidev
, 0);
324 cci_remove_feature_devs(pcidev
);
325 pci_disable_pcie_error_reporting(pcidev
);
328 static struct pci_driver cci_pci_driver
= {
330 .id_table
= cci_pcie_id_tbl
,
331 .probe
= cci_pci_probe
,
332 .remove
= cci_pci_remove
,
333 .sriov_configure
= cci_pci_sriov_configure
,
336 module_pci_driver(cci_pci_driver
);
338 MODULE_DESCRIPTION("FPGA DFL PCIe Device Driver");
339 MODULE_AUTHOR("Intel Corporation");
340 MODULE_LICENSE("GPL v2");